Skip to content

Commit 32840c2

Browse files
committed
fix: check all exit codes in a loop
1 parent f86b8ae commit 32840c2

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

test/nagios.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,35 +69,36 @@ def test_eval(self):
6969
def test_cache(self):
7070
"""Test the caching mechanism in the reporter."""
7171
length = random.randint(1, 30)
72-
exit_code = random.randint(0, 3)
73-
threshold = None
7472

7573
message = ''.join(random.choice(string.printable) for x in range(length))
7674
message = message.rstrip()
7775

76+
threshold = None
7877
filename = PurePath(next(tempfile._get_candidate_names()))
7978

8079
logging.info(f"Reporter using file {filename}")
8180
reporter = NagiosReporter('test_cache', filename, threshold, self.nagios_user)
8281

83-
nagios_exit = [NAGIOS_EXIT_OK, NAGIOS_EXIT_WARNING, NAGIOS_EXIT_CRITICAL, NAGIOS_EXIT_UNKNOWN][exit_code]
82+
for exit_code in range(0, 4):
8483

85-
reporter.cache(nagios_exit, message)
84+
nagios_exit = [NAGIOS_EXIT_OK, NAGIOS_EXIT_WARNING, NAGIOS_EXIT_CRITICAL, NAGIOS_EXIT_UNKNOWN][exit_code]
85+
reporter.cache(nagios_exit, message)
8686

87-
old_stdout = sys.stdout
88-
buffer = StringIO()
89-
sys.stdout = buffer
87+
old_stdout = sys.stdout
88+
buffer = StringIO()
89+
sys.stdout = buffer
9090

91-
try:
92-
reporter_test = NagiosReporter('test_cache', filename, threshold, self.nagios_user)
93-
reporter_test.report_and_exit()
94-
except SystemExit as err:
95-
line = buffer.getvalue().rstrip()
96-
self.assertTrue(err.code == nagios_exit[0])
91+
try:
92+
reporter_test = NagiosReporter('test_cache', filename, threshold, self.nagios_user)
93+
reporter_test.report_and_exit()
94+
except SystemExit as err:
95+
line = buffer.getvalue().rstrip()
96+
logging.info("Retrieved buffer value: %s", line)
97+
self.assertTrue(err.code == nagios_exit[0])
9798

98-
self.assertTrue(line == "%s %s" % (nagios_exit[1], message))
99-
sys.stdout = old_stdout
100-
buffer.close()
99+
self.assertTrue(line == "%s %s" % (nagios_exit[1], message))
100+
sys.stdout = old_stdout
101+
buffer.close()
101102

102103
shutil.rmtree(filename)
103104

0 commit comments

Comments
 (0)