Skip to content

Commit

Permalink
Add better help output when CLI params are missing
Browse files Browse the repository at this point in the history
  • Loading branch information
martialblog committed Sep 17, 2024
1 parent d515d5c commit de39343
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
9 changes: 8 additions & 1 deletion check_bareos.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,14 @@ def environ_or_required(key):
statusParser.add_argument('-s', '--size', dest='size', action='store', help='Border value for oversized backups [default=2]', default=2)
statusParser.add_argument('-u', '--unit', dest='unit', choices=['MB', 'GB', 'TB', 'PB', 'EB'], default='TB', help='display unit [default=TB]')

return parser.parse_args(args)
parsed = parser.parse_args(args)

if not hasattr(parsed, 'func'):
print("[UNKNOWN] - Error: Object to check is missing")
parser.print_help()
sys.exit(3)

return parsed


def checkConnection(cursor):
Expand Down
9 changes: 7 additions & 2 deletions test_check_bareos.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,20 @@
class CLITesting(unittest.TestCase):

def test_commandline(self):
actual = commandline(['-H', 'localhost', '-U', 'bareos'])
actual = commandline(['-H', 'localhost', '-U', 'bareos', 'status', '-fb'])
self.assertEqual(actual.host, 'localhost')
self.assertEqual(actual.user, 'bareos')

@mock.patch('builtins.print')
@mock.patch('sys.stdout')
def test_commandline_with_missing(self, mock_print, mock_out):
with self.assertRaises(SystemExit) as sysexit:
commandline(['-H', 'localhost', '-U', 'bareos', '--password', 'foobar'])

def test_commandline_fromenv(self):
os.environ['CHECK_BAREOS_DATABASE_PASSWORD'] = 'secret'

actual = commandline(['-H', 'localhost', '-U', 'bareos'])
actual = commandline(['-H', 'localhost', '-U', 'bareos', 'status', '-fb'])
self.assertEqual(actual.user, 'bareos')
self.assertEqual(actual.password, 'secret')

Expand Down

0 comments on commit de39343

Please sign in to comment.