class NagiosLoggerRunTests(TestCase): def setUp(self): self.stdout = self.capture_stdout() self.mock_func = Mock(spec=lambda: None) def test_raises_system_exit(self): self.assertRaises(SystemExit, NagiosLogger.run, lambda: None) def test_exits_ok(self): err = _guarded_run() self.assertEqual(err.code, 0) def test_exits_warning(self): err = _guarded_run(lambda: NagiosLogger.warning('A warning')) self.assertEqual(err.code, 1) def test_exits_critical(self): err = _guarded_run(lambda: NagiosLogger.error('An error')) self.assertEqual(err.code, 2) def test_exits_unknown(self): err = _guarded_run(lambda: NagiosLogger.unknown_stop('reason')) self.assertEqual(err.code, 3) @patch('pignacio_scripts.nagios.logger.logging.basicConfig', autospec=True) def test_logging_is_configured_on_info(self, mock_config): _guarded_run() self.assertSoftCalledWith(mock_config, level=logging.INFO) @patch('pignacio_scripts.nagios.logger.logging.basicConfig', autospec=True) def test_debug_sets_logging_to_debug(self, mock_config): _guarded_run(debug=True) self.assertSoftCalledWith(mock_config, level=logging.DEBUG) def test_func_is_called(self): # pylint: disable=no-self-use _guarded_run(self.mock_func) self.mock_func.assert_called_once_with() def test_does_not_output_empty_lines(self): def run(): NagiosLogger.important("important\n") for x in xrange(10): print() _guarded_run(run) lines = self.stdout.getvalue().splitlines() for index, line in enumerate(lines): self.assertNotEqual(line, "", "Line #{} was empty".format(index))
def test_capture_stdout(self, mock_capture): patcher = Mock() patcher.start.return_value = sentinel.patched mock_capture.return_value = patcher patched = self.testcase.capture_stdout() self.assertEqual(patched, sentinel.patched) mock_capture.assert_called_once_with() patcher.start.assert_called_once_with() self.mock_cleanup.assert_any_call(patcher.stop)
def test_patch(self, mock_patch): patcher = Mock() patcher.start.return_value = sentinel.patched mock_patch.return_value = patcher patched = self.testcase.patch(sentinel.arg, kwarg=sentinel.kwvalue) self.assertEqual(patched, sentinel.patched) mock_patch.assert_called_once_with(sentinel.arg, kwarg=sentinel.kwvalue) patcher.start.assert_called_once_with() self.mock_cleanup.assert_any_call(patcher.stop)
def setUp(self): self.testcase = self.MyTestCase() fail_patcher = patch.object(self.testcase, 'fail', autospec=True) self.addCleanup(fail_patcher.stop) self.mock_fail = fail_patcher.start() self.mock_func = Mock()
def setUp(self): self.stdout = self.capture_stdout() self.mock_func = Mock(spec=lambda: None)