Skip to content

Commit

Permalink
Extend SQL Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
martialblog committed Jul 7, 2023
1 parent f48ed5a commit fb0cd7e
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 9 deletions.
10 changes: 5 additions & 5 deletions check_bareos.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ def checkSingleJob(cursor, name, state, kind, time, warning, critical):
return checkState


def checkRunTimeJobs(cursor,state,time,warning,critical):
def checkRunTimeJobs(cursor, state, time, warning, critical):
checkState = {}

if time is None:
Expand Down Expand Up @@ -438,7 +438,7 @@ def checkExpiredTapes(cursor, warning, critical):
checkState["returnCode"] = 0
checkState["returnMessage"] = "[OK]"

checkState["returnMessage"] += " - Tapes " + str(result) + " expired"
checkState["returnMessage"] += " - " + str(result) + " Tapes are expired"

checkState["performanceData"] = "bareos.tape.expired=" + str(result) + ";" + str(warning) + ";" + str(critical) + ";;"

Expand Down Expand Up @@ -467,7 +467,7 @@ def checkWillExpiredTapes(cursor, time, warning, critical):
checkState["returnCode"] = 0
checkState["returnMessage"] = "[OK]"

checkState["returnMessage"] += " - Tapes " + str(result) + " will expire in next " + str(time) + " days"
checkState["returnMessage"] += " - " + str(result) + " Tapes will expire in " + str(time) + " days"

checkState["performanceData"] = "bareos.tape.willexpire=" + str(result) + ";" + str(warning) + ";" + str(critical) + ";;"

Expand Down Expand Up @@ -496,7 +496,7 @@ def checkReplaceTapes(cursor, mounts, warning, critical):
checkState["returnCode"] = 0
checkState["returnMessage"] = "[OK]"

checkState["returnMessage"] += " - " + str(result) + " Taples have to be replaced in the near future"
checkState["returnMessage"] += " - " + str(result) + " Tapes might need replacement"

checkState["performanceData"] = "bareos.tape.replace=" + str(result) + ";" + str(warning) + ";" + str(critical) + ";;"

Expand Down Expand Up @@ -529,7 +529,7 @@ def checkEmptyTapes(cursor, warning, critical):
checkState["returnCode"] = 0
checkState["returnMessage"] = "[OK]"

checkState["returnMessage"] += " - " + str(result) + " Tapes are empty in the Storage"
checkState["returnMessage"] += " - " + str(result) + " Tapes are empty"

checkState["performanceData"] = "bareos.tape.empty=" + str(result) + ";" + str(warning) + ";" + str(critical) + ";;"

Expand Down
117 changes: 113 additions & 4 deletions test_check_bareos.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,23 @@
from check_bareos import createBackupKindString
from check_bareos import createFactor
from check_bareos import printNagiosOutput
from check_bareos import checkConnection
from check_bareos import connectDB
from check_bareos import Threshold

from check_bareos import checkFailedBackups
from check_bareos import checkBackupSize
from check_bareos import checkTotalBackupSize
from check_bareos import checkEmptyBackups
from check_bareos import checkEmptyTapes
from check_bareos import checkExpiredTapes
from check_bareos import checkFailedBackups
from check_bareos import checkJobs
from check_bareos import checkOversizedBackups
from check_bareos import checkReplaceTapes
from check_bareos import checkRunTimeJobs
from check_bareos import checkSingleJob
from check_bareos import checkJobs
from check_bareos import checkEmptyBackups
from check_bareos import checkTapesInStorage
from check_bareos import checkTotalBackupSize
from check_bareos import checkWillExpiredTapes


class CLITesting(unittest.TestCase):
Expand All @@ -32,6 +40,20 @@ def test_commandline(self):

class UtilTesting(unittest.TestCase):

# TODO checkConnection(cursor)
# TODO def checkTape(args)
# TODO def checkJob(args)

@mock.patch('check_bareos.psycopg2')
def test_connectDB(self, mock_sql):
con = mock.MagicMock()
con.cursor.return_value = "cursor"
mock_sql.connect.return_value = con
actual = connectDB("user", "password", "localhost", "bareos", 5432)

expected = "cursor"
self.assertEqual(actual, expected)

def test_createBackupKindString(self):
actual = createBackupKindString(True, True, True)
expected = "'F','I','D'"
Expand All @@ -55,6 +77,93 @@ def test_printNagiosOutput(self, mock_print):

class SQLTesting(unittest.TestCase):

def test_checkEmptyTapes(self):

c = mock.MagicMock()

c.fetchone.return_value = [2]
actual = checkEmptyTapes(c, Threshold(3), Threshold(5))
expected = {'returnCode': 0, 'returnMessage': '[OK] - 2.0 Tapes are empty', 'performanceData': 'bareos.tape.empty=2.0;3;5;;'}
self.assertEqual(actual, expected)

c.fetchone.return_value = [10]
actual = checkEmptyTapes(c, Threshold(3), Threshold(5))
expected = {'returnCode': 2, 'returnMessage': '[CRITICAL] - 10.0 Tapes are empty', 'performanceData': 'bareos.tape.empty=10.0;3;5;;'}
self.assertEqual(actual, expected)

def test_checkReplaceTapes(self):

c = mock.MagicMock()

c.fetchone.return_value = [2]
actual = checkReplaceTapes(c, "foo", Threshold(3), Threshold(5))
expected = {'returnCode': 0, 'returnMessage': '[OK] - 2.0 Tapes might need replacement', 'performanceData': 'bareos.tape.replace=2.0;3;5;;'}
self.assertEqual(actual, expected)

c.fetchone.return_value = [10]
actual = checkReplaceTapes(c, "foo", Threshold(3), Threshold(5))
expected = {'returnCode': 2, 'returnMessage': '[CRITICAL] - 10.0 Tapes might need replacement', 'performanceData': 'bareos.tape.replace=10.0;3;5;;'}
self.assertEqual(actual, expected)


def test_checkWillExpiredTapes(self):

c = mock.MagicMock()

c.fetchone.return_value = [2]
actual = checkWillExpiredTapes(c, 1, Threshold(3), Threshold(5))
expected = {'returnCode': 0, 'returnMessage': '[OK] - 2.0 Tapes will expire in 1 days', 'performanceData': 'bareos.tape.willexpire=2.0;3;5;;'}
self.assertEqual(actual, expected)

c.fetchone.return_value = [10]
actual = checkWillExpiredTapes(c, 1, Threshold(3), Threshold(5))
expected = {'returnCode': 2, 'returnMessage': '[CRITICAL] - 10.0 Tapes will expire in 1 days', 'performanceData': 'bareos.tape.willexpire=10.0;3;5;;'}
self.assertEqual(actual, expected)

def test_checkExpiredTapes(self):

c = mock.MagicMock()

c.fetchone.return_value = [2]
actual = checkExpiredTapes(c, Threshold(3), Threshold(5))
expected = {'returnCode': 0, 'returnMessage': '[OK] - 2.0 Tapes are expired', 'performanceData': 'bareos.tape.expired=2.0;3;5;;'}
self.assertEqual(actual, expected)

c.fetchone.return_value = [10]
actual = checkExpiredTapes(c, Threshold(3), Threshold(5))
expected = {'returnCode': 2, 'returnMessage': '[CRITICAL] - 10.0 Tapes are expired', 'performanceData': 'bareos.tape.expired=10.0;3;5;;'}
self.assertEqual(actual, expected)

def test_checkTapesInStorage(self):

c = mock.MagicMock()

c.fetchone.return_value = [2]
actual = checkTapesInStorage(c, Threshold(3), Threshold(5))
expected = {'returnCode': 0, 'returnMessage': '[OK] - 2.0 Tapes are in the Storage', 'performanceData': 'bareos.tape.instorage=2.0;3;5;;'}
self.assertEqual(actual, expected)

c.fetchone.return_value = [10]
actual = checkTapesInStorage(c, Threshold(3), Threshold(5))
expected = {'returnCode': 2, 'returnMessage': '[CRITICAL] - 10.0 Tapes are in the Storage', 'performanceData': 'bareos.tape.instorage=10.0;3;5;;'}
self.assertEqual(actual, expected)


def test_checkRunTimeJobs(self):

c = mock.MagicMock()

c.fetchone.return_value = [2]
actual = checkRunTimeJobs(c, "'F','I','D'", 1, Threshold(3), Threshold(5))
expected = {'returnCode': 0, 'returnMessage': '[OK] - 2.0 Jobs are running longer than 1 days', 'performanceData': 'bareos.job.count=2.0;3;5;;'}
self.assertEqual(actual, expected)

c.fetchone.return_value = [10]
actual = checkRunTimeJobs(c, "'F','I','D'", 1, Threshold(3), Threshold(5))
expected = {'returnCode': 2, 'returnMessage': '[CRITICAL] - 10.0 Jobs are running longer than 1 days', 'performanceData': 'bareos.job.count=10.0;3;5;;'}
self.assertEqual(actual, expected)


def test_checkEmptyBackups(self):

c = mock.MagicMock()
Expand Down

0 comments on commit fb0cd7e

Please sign in to comment.