Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend Tests and better usage help #39

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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