From de393439d590d8777fdf41b1f9ee8fad64b0448d Mon Sep 17 00:00:00 2001 From: Markus Opolka Date: Tue, 17 Sep 2024 16:53:25 +0200 Subject: [PATCH] Add better help output when CLI params are missing --- check_bareos.py | 9 ++++++++- test_check_bareos.py | 9 +++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/check_bareos.py b/check_bareos.py index ccd19b7..a502d62 100755 --- a/check_bareos.py +++ b/check_bareos.py @@ -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): diff --git a/test_check_bareos.py b/test_check_bareos.py index 7f35642..e5a9e83 100644 --- a/test_check_bareos.py +++ b/test_check_bareos.py @@ -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')