def test_fatal_exception_handling(self): # Test a fatal exception def throw_test_exception(cert_extensions): raise FatalException("this is just a test exception") self.mocks['check_cert.CertStore'].lookup_certs.side_effect = throw_test_exception with self.assertRaises(SystemExit) as e: check_cert.main(config_file='./check_cert.conf') self.assertEqual(e.exception.code, 1) self.assertTrue(self.mocks['logging.error'].called) self.assertFalse(self.mocks['check_cert.ScriptStatus'].notify_immediate.called)
def test_ok_cert_detection(self): def fake_cert_expiration(cert): return datetime.utcnow() + timedelta(days=40) self.mocks['check_cert.get_cert_expiration'].side_effect = fake_cert_expiration with self.assertRaises(SystemExit) as e: check_cert.main(config_file='./check_cert.conf') self.assertEqual(e.exception.code, 0) self.assertFalse(self.mocks['check_cert.ScriptStatus'].notify_immediate.called) self.assertTrue(self.mocks['check_cert.ScriptStatus'].notify_agregated.called) # All certs were ok, so a 'default' message should be send to Rieman self.assertFalse(self.mocks['check_cert.ScriptStatus'].update.called)
def test_unsuported_cert_detection(self): self.mocks['check_cert.CertStore'].lookup_certs.side_effect = \ self._unsupported_cert_git_repo with self.assertRaises(SystemExit) as e: check_cert.main(config_file='./check_cert.conf') self.assertEqual(e.exception.code, 0) self.assertFalse( self.mocks['check_cert.ScriptStatus'].notify_immediate.called) self.assertTrue( self.mocks['check_cert.ScriptStatus'].notify_agregated.called) self.assertEqual( self.mocks['check_cert.ScriptStatus'].update.call_args[0][0], 'unknown')
def test_crit_is_zero(self): self.mocks['check_cert.ScriptConfiguration'].get_val.side_effect, \ self.mocks['check_cert.ScriptConfiguration'].get_config.side_effect = \ self._script_conf_factory(critical_treshold=-1) with self.assertRaises(SystemExit) as e: check_cert.main(config_file='./check_cert.conf') self.assertEqual(e.exception.code, -216) self.assertTrue( self.mocks['check_cert.ScriptStatus'].notify_immediate.called) self.assertEqual( self.mocks['check_cert.ScriptStatus'].notify_immediate.call_args[0][0], 'unknown')
def test_crit_is_zero(self): self.mocks['check_cert.ScriptConfiguration'].get_val.side_effect, \ self.mocks['check_cert.ScriptConfiguration'].get_config.side_effect = \ self._script_conf_factory(critical_treshold=-1) with self.assertRaises(SystemExit) as e: check_cert.main(config_file='./check_cert.conf') self.assertEqual(e.exception.code, -216) self.assertTrue( self.mocks['check_cert.ScriptStatus'].notify_immediate.called) self.assertEqual( self.mocks['check_cert.ScriptStatus'].notify_immediate.call_args[0] [0], 'unknown')
def test_expire_today_cert_detection(self): def fake_cert_expiration(cert): return datetime.utcnow() self.mocks['check_cert.get_cert_expiration'].side_effect = fake_cert_expiration with self.assertRaises(SystemExit) as e: check_cert.main(config_file='./check_cert.conf') self.assertEqual(e.exception.code, 0) self.assertFalse( self.mocks['check_cert.ScriptStatus'].notify_immediate.called) self.assertTrue( self.mocks['check_cert.ScriptStatus'].notify_agregated.called) self.assertEqual( self.mocks['check_cert.ScriptStatus'].update.call_args[0][0], 'critical')
def test_fatal_exception_handling(self): # Test a fatal exception def throw_test_exception(cert_extensions): raise FatalException("this is just a test exception") self.mocks[ 'check_cert.CertStore'].lookup_certs.side_effect = throw_test_exception with self.assertRaises(SystemExit) as e: check_cert.main(config_file='./check_cert.conf') self.assertEqual(e.exception.code, 1) self.assertTrue(self.mocks['logging.error'].called) self.assertFalse( self.mocks['check_cert.ScriptStatus'].notify_immediate.called)
def test_malformed_cert_detection(self): def fake_cert_expiration(cert): raise RecoverableException() self.mocks['check_cert.get_cert_expiration'].side_effect = fake_cert_expiration with self.assertRaises(SystemExit) as e: check_cert.main(config_file='./check_cert.conf') self.assertEqual(e.exception.code, 0) self.assertFalse( self.mocks['check_cert.ScriptStatus'].notify_immediate.called) self.assertTrue( self.mocks['check_cert.ScriptStatus'].notify_agregated.called) self.assertEqual( self.mocks['check_cert.ScriptStatus'].update.call_args[0][0], 'unknown')
def test_ignored_cert_detection(self): # simulate a git repo with an ignored cert: self.mocks['check_cert.CertStore'].lookup_certs.side_effect = \ self._ignored_cert_git_repo with self.assertRaises(SystemExit) as e: check_cert.main(config_file='./check_cert.conf') self.assertEqual(e.exception.code, 0) self.assertFalse( self.mocks['check_cert.ScriptStatus'].notify_immediate.called) self.assertTrue( self.mocks['check_cert.ScriptStatus'].notify_agregated.called) # All certs were ok, so a 'default' message should be send to # monitoring self.assertFalse(self.mocks['check_cert.ScriptStatus'].update.called)
def test_ok_cert_detection(self): def fake_cert_expiration(cert): return datetime.utcnow() + timedelta(days=40) self.mocks[ 'check_cert.get_cert_expiration'].side_effect = fake_cert_expiration with self.assertRaises(SystemExit) as e: check_cert.main(config_file='./check_cert.conf') self.assertEqual(e.exception.code, 0) self.assertFalse( self.mocks['check_cert.ScriptStatus'].notify_immediate.called) self.assertTrue( self.mocks['check_cert.ScriptStatus'].notify_agregated.called) # All certs were ok, so a 'default' message should be send to Rieman self.assertFalse(self.mocks['check_cert.ScriptStatus'].update.called)
def test_recoverable_exception_handling(self): # Test an exception from which we can recover: def throw_test_exception(cert_extensions): raise RecoverableException("this is just a test exception") self.mocks['check_cert.CertStore'].lookup_certs.side_effect = throw_test_exception with self.assertRaises(SystemExit) as e: check_cert.main(config_file='./check_cert.conf') self.assertEqual(e.exception.code, -216) self.assertTrue( self.mocks['logging.error'].called) self.assertTrue( self.mocks['check_cert.ScriptStatus'].notify_immediate.called) self.assertEqual( self.mocks['check_cert.ScriptStatus'].notify_immediate.call_args[0][0], 'unknown')
def test_recoverable_exception_handling(self): # Test an exception from which we can recover: def throw_test_exception(cert_extensions): raise RecoverableException("this is just a test exception") self.mocks[ 'check_cert.CertStore'].lookup_certs.side_effect = throw_test_exception with self.assertRaises(SystemExit) as e: check_cert.main(config_file='./check_cert.conf') self.assertEqual(e.exception.code, -216) self.assertTrue(self.mocks['logging.error'].called) self.assertTrue( self.mocks['check_cert.ScriptStatus'].notify_immediate.called) self.assertEqual( self.mocks['check_cert.ScriptStatus'].notify_immediate.call_args[0] [0], 'unknown')
def test_malformed_cert_detection(self): def fake_cert_expiration(cert): raise RecoverableException() self.mocks[ 'check_cert.get_cert_expiration'].side_effect = fake_cert_expiration with self.assertRaises(SystemExit) as e: check_cert.main(config_file='./check_cert.conf') self.assertEqual(e.exception.code, 0) self.assertFalse( self.mocks['check_cert.ScriptStatus'].notify_immediate.called) self.assertTrue( self.mocks['check_cert.ScriptStatus'].notify_agregated.called) self.assertEqual( self.mocks['check_cert.ScriptStatus'].update.call_args[0][0], 'unknown')
def test_expire_today_cert_detection(self): def fake_cert_expiration(cert): return datetime.utcnow() self.mocks[ 'check_cert.get_cert_expiration'].side_effect = fake_cert_expiration with self.assertRaises(SystemExit) as e: check_cert.main(config_file='./check_cert.conf') self.assertEqual(e.exception.code, 0) self.assertFalse( self.mocks['check_cert.ScriptStatus'].notify_immediate.called) self.assertTrue( self.mocks['check_cert.ScriptStatus'].notify_agregated.called) self.assertEqual( self.mocks['check_cert.ScriptStatus'].update.call_args[0][0], 'critical')
def test_script_init(self): """ Test if script initializes its dependencies properly """ with self.assertRaises(SystemExit) as e: check_cert.main(config_file='./check_cert.conf') self.assertEqual(e.exception.code, 0) proper_init_call = dict(riemann_enabled=True, riemann_ttl=60, riemann_service_name='check_cert', riemann_hosts_config={ 'static': [ '1.2.3.4:1:udp', '2.3.4.5:5555:tcp', ] }, riemann_tags=['abc', 'def'], nrpe_enabled=True, debug=False) self.mocks[ 'check_cert.ScriptConfiguration'].load_config.assert_called_once_with( './check_cert.conf') self.mocks['check_cert.ScriptLock'].init.assert_called_once_with( "./fake_lock.pid") self.mocks['check_cert.ScriptLock'].aqquire.assert_called_once_with() self.mocks[ 'check_cert.ScriptStatus'].initialize.assert_called_once_with( **proper_init_call) proper_init_call = dict( host="git.foo.net", port=22, pubkey="./foo", username="******", repo_localdir="/tmp/foo", repo_url="/foo-puppet", repo_masterbranch="refs/heads/foo", ) self.mocks['check_cert.CertStore'].initialize.assert_called_once_with( **proper_init_call)
def test_script_init(self): """ Test if script initializes its dependencies properly """ with self.assertRaises(SystemExit) as e: check_cert.main(config_file='./check_cert.conf') self.assertEqual(e.exception.code, 0) proper_init_call = dict(riemann_enabled=True, riemann_ttl=60, riemann_service_name='check_cert', riemann_hosts_config={ 'static': ['1.2.3.4:1:udp', '2.3.4.5:5555:tcp', ] }, riemann_tags=['abc', 'def'], nrpe_enabled=True, debug=False) self.mocks['check_cert.ScriptConfiguration'].load_config.assert_called_once_with( './check_cert.conf') self.mocks['check_cert.ScriptLock'].init.assert_called_once_with( "./fake_lock.pid") self.mocks['check_cert.ScriptLock'].aqquire.assert_called_once_with() self.mocks['check_cert.ScriptStatus'].initialize.assert_called_once_with( **proper_init_call) proper_init_call = dict(host="git.foo.net", port=22, pubkey="./foo", username="******", repo_localdir="/tmp/foo", repo_url="/foo-puppet", repo_masterbranch="refs/heads/foo",) self.mocks['check_cert.CertStore'].initialize.assert_called_once_with( **proper_init_call)