def test_check_write_nrpe_exportdir_not_accessible(self): self.patched['exists'].return_value = False check = nrpe.Check('shortname', 'description', '/some/command') self.assertEqual(None, check.write('testctx', 'hostname', 'testsgrps')) expected = ('Not writing service config as ' '/var/lib/nagios/export is not accessible') self.patched['log'].assert_has_calls([call(expected)], any_order=True) self.check_call_counts(log=2, open=1)
def test_run(self): self.patched['exists'].return_value = True command = '/usr/bin/wget foo' check = nrpe.Check('shortname', 'description', command) self.assertEqual(None, check.run()) self.check_call_counts(exists=1, call=1) self.assertEqual(command, self.patched['call'].call_args[0][0])
def test_valid_shortname(self): cases = [ '1_number_is_fine', 'dashes-ok', '5', ] for shortname in cases: check = nrpe.Check(shortname, 'description', '/some/command') self.assertEqual(shortname, check.shortname)
def test_write_removes_existing_config(self): self.patched['listdir'].return_value = [ 'foo', 'bar.cfg', '_check_shortname.cfg'] check = nrpe.Check('shortname', 'description', '/some/command') self.assertEqual(None, check.write('testctx', 'hostname', 'testsgrp')) expected = '/var/lib/nagios/export/_check_shortname.cfg' self.patched['remove'].assert_called_once_with(expected) self.check_call_counts(exists=3, remove=1, open=2, listdir=1)
def test_locate_cmd_argument_escape(self): """Test that `_locate_cmd` properly escapes arguments. If arguments contain symbols that have special meaning in shell, like '*',';' or '`', they should be properly quoted to prevent unexpected behavior. """ self.patched['exists'].return_value = True raw_cmd = "check_test -a * -b ; -c `/bin/bash` -d foo -e 1" prefix = "/usr/lib/nagios/plugins/" safe_cmd = "check_test -a '*' -b ';' -c '`/bin/bash`' -d foo -e 1" check = nrpe.Check('shortname', 'description', raw_cmd) self.assertEqual(check.check_cmd, prefix + safe_cmd)
def test_locate_cmd_not_found(self): self.patched['exists'].return_value = False check = nrpe.Check('shortname', 'description', 'check_http -x -y -z') self.assertEqual('', check.check_cmd) self.assertEqual(2, self.patched['exists'].call_count) expected = [ '/usr/lib/nagios/plugins/check_http', '/usr/local/lib/nagios/plugins/check_http', ] actual = [x[0][0] for x in self.patched['exists'].call_args_list] self.assertEqual(expected, actual) self.check_call_counts(exists=2, log=1) expected = 'Check command not found: check_http' self.assertEqual(expected, self.patched['log'].call_args[0][0])
def test_locate_cmd_no_args(self): self.patched['exists'].return_value = True check = nrpe.Check('shortname', 'description', '/bin/ls') self.assertEqual('/bin/ls', check.check_cmd)