From d515d5cff8c79a70fc14f9bcbe8b8c5c0c9047c4 Mon Sep 17 00:00:00 2001 From: Markus Opolka Date: Tue, 17 Sep 2024 16:40:22 +0200 Subject: [PATCH 1/2] Extend tests --- test_check_bareos.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/test_check_bareos.py b/test_check_bareos.py index f3040ca..7f35642 100644 --- a/test_check_bareos.py +++ b/test_check_bareos.py @@ -66,6 +66,11 @@ def test_thresholds(self): self.assertEqual(check_threshold(5, Threshold("10:20"), Threshold("50")), 1) self.assertEqual(check_threshold(10, Threshold("@10:20"), Threshold("50")), 1) + self.assertEqual(repr(Threshold("@10:20")), 'Threshold(@10:20)') + + def test_thresholds_with_error(self): + with self.assertRaises(ValueError): + Threshold("()*!#$209810") class UtilTesting(unittest.TestCase): @@ -84,8 +89,12 @@ def test_connectDB(self, mock_sql): self.assertEqual(actual, expected) def test_createBackupKindString(self): - actual = createBackupKindString(True, True, True) - expected = "'F','I','D'" + actual = createBackupKindString(True, True, False) + expected = "'F','I'" + self.assertEqual(actual, expected) + + actual = createBackupKindString(False, False, False) + expected = "'F','D','I'" self.assertEqual(actual, expected) def test_createFactor(self): @@ -108,6 +117,9 @@ def test_read_password_from_file(self): expected = 'secretpassword' self.assertEqual(actual, expected) + with self.assertRaises(ValueError) as sysexit: + read_password_from_file('contrib/icinga2-commands-example.conf') + with self.assertRaises(FileNotFoundError) as sysexit: read_password_from_file('contrib/nosuch') From de393439d590d8777fdf41b1f9ee8fad64b0448d Mon Sep 17 00:00:00 2001 From: Markus Opolka Date: Tue, 17 Sep 2024 16:53:25 +0200 Subject: [PATCH 2/2] 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')