Skip to content

Commit

Permalink
Merge pull request #39 from NETWAYS/chore/extend-tests
Browse files Browse the repository at this point in the history
Extend Tests and better usage help
  • Loading branch information
martialblog authored Sep 30, 2024
2 parents bd40556 + de39343 commit fac4720
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 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
25 changes: 21 additions & 4 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 All @@ -66,6 +71,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):

Expand All @@ -84,8 +94,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):
Expand All @@ -108,6 +122,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')

Expand Down

0 comments on commit fac4720

Please sign in to comment.