def test_no_state_change_outside_time_constraint(self): self._set_all_alarms('insufficient data') self.alarms[0].time_constraints = [ {'name': 'test', 'description': 'test', 'start': '0 11 * * *', # daily at 11:00 'duration': 10800, # 3 hours 'timezone': 'Europe/Ljubljana'} ] self.alarms[1].time_constraints = self.alarms[0].time_constraints dt = datetime.datetime(2014, 1, 1, 15, 0, 0, tzinfo=pytz.timezone('Europe/Ljubljana')) with mock.patch('ceilometerclient.client.get_client', return_value=self.api_client): timeutils.set_time_override(dt.astimezone(pytz.UTC)) self.api_client.alarms.get.side_effect = [ self._get_alarm('ok'), self._get_alarm('ok'), self._get_alarm('ok'), self._get_alarm('ok'), ] self._evaluate_all_alarms() update_calls = self.api_client.alarms.set_state.call_args_list self.assertEqual([], update_calls, "Alarm should not change state if the current " " time is outside its time constraint.") self.assertEqual([], self.notifier.notify.call_args_list)
def test_message_to_event_missing_keys(self): now = timeutils.utcnow() timeutils.set_time_override(now) message = {'event_type': "foo", 'message_id': "abc", 'publisher_id': "1"} mock_dispatcher = MagicMock() self.srv.dispatcher_manager = test_manager.TestExtensionManager( [extension.Extension('test', None, None, mock_dispatcher ), ]) with patch('ceilometer.collector.service.LOG') as mylog: self.srv._message_to_event(message) self.assertFalse(mylog.exception.called) events = mock_dispatcher.record_events.call_args[0] self.assertEqual(1, len(events)) event = events[0] self.assertEqual("foo", event.event_name) self.assertEqual(now, event.generated) self.assertEqual(1, len(event.traits))
def test_message_to_event_missing_keys(self): now = timeutils.utcnow() timeutils.set_time_override(now) message = {'event_type': "foo", 'message_id': "abc"} self.srv.storage_conn = MagicMock() with patch('ceilometer.collector.service.LOG') as mylog: self.srv._message_to_event(message) self.assertFalse(mylog.exception.called) events = self.srv.storage_conn.record_events.call_args[0] self.assertEquals(1, len(events)) event = events[0][0] self.assertEquals("foo", event.event_name) self.assertEquals(now, event.generated) self.assertEquals(1, len(event.traits))
def test_converter_missing_keys(self): # test a malformed notification now = timeutils.utcnow() timeutils.set_time_override(now) c = converter.NotificationEventsConverter( [], self.fake_plugin_mgr, add_catchall=True) message = {'event_type': "foo", 'message_id': "abc", 'publisher_id': "1"} e = c.to_event(message) self.assertIsValidEvent(e, message) self.assertEqual(len(e.traits), 1) self.assertEqual("foo", e.event_type) self.assertEqual(now, e.generated)
def test_extract_when(self): now = timeutils.utcnow() modified = now + datetime.timedelta(minutes=1) timeutils.set_time_override(now) body = {"timestamp": str(modified)} self.assertEqual(service.CollectorService._extract_when(body), modified) body = {"_context_timestamp": str(modified)} self.assertEqual(service.CollectorService._extract_when(body), modified) then = now + datetime.timedelta(hours=1) body = {"timestamp": str(modified), "_context_timestamp": str(then)} self.assertEqual(service.CollectorService._extract_when(body), modified) self.assertEqual(service.CollectorService._extract_when({}), now)
def test_base_time_constraints_timezone(self): alarm = mock.MagicMock() alarm.time_constraints = [ { "name": "test", "description": "test", "start": "0 11 * * *", # daily at 11:00 "duration": 10800, # 3 hours "timezone": "Europe/Ljubljana", } ] cls = evaluator.Evaluator dt_eu = datetime.datetime(2014, 1, 1, 12, 0, 0, tzinfo=pytz.timezone("Europe/Ljubljana")) dt_us = datetime.datetime(2014, 1, 1, 12, 0, 0, tzinfo=pytz.timezone("US/Eastern")) timeutils.set_time_override(dt_eu.astimezone(pytz.UTC)) self.assertTrue(cls.within_time_constraint(alarm)) timeutils.set_time_override(dt_us.astimezone(pytz.UTC)) self.assertFalse(cls.within_time_constraint(alarm))
def test_extract_when(self): now = timeutils.utcnow() modified = now + datetime.timedelta(minutes=1) timeutils.set_time_override(now) body = {"timestamp": str(modified)} when = converter.EventDefinition._extract_when(body) self.assertTimestampEqual(modified, when) body = {"_context_timestamp": str(modified)} when = converter.EventDefinition._extract_when(body) self.assertTimestampEqual(modified, when) then = now + datetime.timedelta(hours=1) body = {"timestamp": str(modified), "_context_timestamp": str(then)} when = converter.EventDefinition._extract_when(body) self.assertTimestampEqual(modified, when) when = converter.EventDefinition._extract_when({}) self.assertTimestampEqual(now, when)
def test_base_time_constraints(self): alarm = mock.MagicMock() alarm.time_constraints = [ { "name": "test", "description": "test", "start": "0 11 * * *", # daily at 11:00 "duration": 10800, # 3 hours "timezone": "", }, { "name": "test2", "description": "test", "start": "0 23 * * *", # daily at 23:00 "duration": 10800, # 3 hours "timezone": "", }, ] cls = evaluator.Evaluator timeutils.set_time_override(datetime.datetime(2014, 1, 1, 12, 0, 0)) self.assertTrue(cls.within_time_constraint(alarm)) timeutils.set_time_override(datetime.datetime(2014, 1, 2, 1, 0, 0)) self.assertTrue(cls.within_time_constraint(alarm)) timeutils.set_time_override(datetime.datetime(2014, 1, 2, 5, 0, 0)) self.assertFalse(cls.within_time_constraint(alarm))
def test_message_to_event_missing_keys(self): now = timeutils.utcnow() timeutils.set_time_override(now) message = {'event_type': "foo", 'message_id': "abc", 'publisher_id': "1"} mock_dispatcher = mock.MagicMock() self.srv.dispatcher_manager = test_manager.TestExtensionManager( [extension.Extension('test', None, None, mock_dispatcher ), ]) self.srv._message_to_event(message) events = mock_dispatcher.record_events.call_args[0] self.assertEqual(1, len(events)) event = events[0] self.assertEqual("foo", event.event_type) self.assertEqual(now, event.generated) self.assertEqual(1, len(event.traits))
def test_no_state_change_outside_time_constraint(self): self._set_all_alarms('ok') self.alarms[0].time_constraints = [ {'name': 'test', 'description': 'test', 'start': '0 11 * * *', # daily at 11:00 'duration': 10800, # 3 hours 'timezone': 'Europe/Ljubljana'} ] self.alarms[1].time_constraints = self.alarms[0].time_constraints dt = datetime.datetime(2014, 1, 1, 15, 0, 0, tzinfo=pytz.timezone('Europe/Ljubljana')) with mock.patch('ceilometerclient.client.get_client', return_value=self.api_client): timeutils.set_time_override(dt.astimezone(pytz.UTC)) self.api_client.statistics.list.return_value = [] self._evaluate_all_alarms() self._assert_all_alarms('ok') update_calls = self.api_client.alarms.set_state.call_args_list self.assertEqual([], update_calls, "Alarm should not change state if the current " " time is outside its time constraint.") self.assertEqual([], self.notifier.notify.call_args_list)
def test_state_change_inside_time_constraint(self): self._set_all_alarms('insufficient data') self.alarms[0].time_constraints = [ {'name': 'test', 'description': 'test', 'start': '0 11 * * *', # daily at 11:00 'duration': 10800, # 3 hours 'timezone': 'Europe/Ljubljana'} ] self.alarms[1].time_constraints = self.alarms[0].time_constraints dt = datetime.datetime(2014, 1, 1, 12, 0, 0, tzinfo=pytz.timezone('Europe/Ljubljana')) with mock.patch('ceilometerclient.client.get_client', return_value=self.api_client): timeutils.set_time_override(dt.astimezone(pytz.UTC)) self.api_client.alarms.get.side_effect = [ self._get_alarm('ok'), self._get_alarm('ok'), self._get_alarm('ok'), self._get_alarm('ok'), ] self._evaluate_all_alarms() expected = [mock.call(alarm.alarm_id, state='ok') for alarm in self.alarms] update_calls = self.api_client.alarms.set_state.call_args_list self.assertEqual(expected, update_calls, "Alarm should change state if the current " "time is inside its time constraint.") reasons, reason_datas = self._combination_transition_reason( 'ok', self.alarms[0].rule['alarm_ids'], self.alarms[1].rule['alarm_ids']) expected = [mock.call(alarm, 'insufficient data', reason, reason_data) for alarm, reason, reason_data in zip(self.alarms, reasons, reason_datas)] self.assertEqual(expected, self.notifier.notify.call_args_list)
def test_extract_when(self): now = timeutils.utcnow() modified = now + datetime.timedelta(minutes=1) timeutils.set_time_override(now) body = {"timestamp": str(modified)} when = service.CollectorService._extract_when(body) self.assertAlmostEqual(timeutils.delta_seconds(modified, when), 0.0, places=5) body = {"_context_timestamp": str(modified)} when = service.CollectorService._extract_when(body) self.assertAlmostEqual(timeutils.delta_seconds(modified, when), 0.0, places=5) then = now + datetime.timedelta(hours=1) body = {"timestamp": str(modified), "_context_timestamp": str(then)} when = service.CollectorService._extract_when(body) self.assertAlmostEqual(timeutils.delta_seconds(modified, when), 0.0, places=5) when = service.CollectorService._extract_when({}) self.assertAlmostEqual(timeutils.delta_seconds(now, when), 0.0, places=5)
def test_state_change_inside_time_constraint(self): self._set_all_alarms('ok') self.alarms[0].time_constraints = [ {'name': 'test', 'description': 'test', 'start': '0 11 * * *', # daily at 11:00 'duration': 10800, # 3 hours 'timezone': 'Europe/Ljubljana'} ] self.alarms[1].time_constraints = self.alarms[0].time_constraints dt = datetime.datetime(2014, 1, 1, 12, 0, 0, tzinfo=pytz.timezone('Europe/Ljubljana')) with mock.patch('ceilometerclient.client.get_client', return_value=self.api_client): timeutils.set_time_override(dt.astimezone(pytz.UTC)) # the following part based on test_simple_insufficient self.api_client.statistics.list.return_value = [] self._evaluate_all_alarms() self._assert_all_alarms('insufficient data') expected = [mock.call(alarm.alarm_id, state='insufficient data') for alarm in self.alarms] update_calls = self.api_client.alarms.set_state.call_args_list self.assertEqual(expected, update_calls, "Alarm should change state if the current " "time is inside its time constraint.") expected = [mock.call( alarm, 'ok', ('%d datapoints are unknown' % alarm.rule['evaluation_periods']), self._reason_data('unknown', alarm.rule['evaluation_periods'], None)) for alarm in self.alarms] self.assertEqual(expected, self.notifier.notify.call_args_list)
def test_base_time_constraints_complex(self): alarm = mock.MagicMock() alarm.time_constraints = [{ 'name': 'test', 'description': 'test', # Every consecutive 2 minutes (from the 3rd to the 57th) past # every consecutive 2 hours (between 3:00 and 12:59) on every day. 'start': '3-57/2 3-12/2 * * *', 'duration': 30, 'timezone': '' }] cls = evaluator.Evaluator # test minutes inside timeutils.set_time_override(datetime.datetime(2014, 1, 5, 3, 3, 0)) self.assertTrue(cls.within_time_constraint(alarm)) timeutils.set_time_override(datetime.datetime(2014, 1, 5, 3, 31, 0)) self.assertTrue(cls.within_time_constraint(alarm)) timeutils.set_time_override(datetime.datetime(2014, 1, 5, 3, 57, 0)) self.assertTrue(cls.within_time_constraint(alarm)) # test minutes outside timeutils.set_time_override(datetime.datetime(2014, 1, 5, 3, 2, 0)) self.assertFalse(cls.within_time_constraint(alarm)) timeutils.set_time_override(datetime.datetime(2014, 1, 5, 3, 4, 0)) self.assertFalse(cls.within_time_constraint(alarm)) timeutils.set_time_override(datetime.datetime(2014, 1, 5, 3, 58, 0)) self.assertFalse(cls.within_time_constraint(alarm)) # test hours inside timeutils.set_time_override(datetime.datetime(2014, 1, 5, 3, 31, 0)) self.assertTrue(cls.within_time_constraint(alarm)) timeutils.set_time_override(datetime.datetime(2014, 1, 5, 5, 31, 0)) self.assertTrue(cls.within_time_constraint(alarm)) timeutils.set_time_override(datetime.datetime(2014, 1, 5, 11, 31, 0)) self.assertTrue(cls.within_time_constraint(alarm)) # test hours outside timeutils.set_time_override(datetime.datetime(2014, 1, 5, 1, 31, 0)) self.assertFalse(cls.within_time_constraint(alarm)) timeutils.set_time_override(datetime.datetime(2014, 1, 5, 4, 31, 0)) self.assertFalse(cls.within_time_constraint(alarm)) timeutils.set_time_override(datetime.datetime(2014, 1, 5, 12, 31, 0)) self.assertFalse(cls.within_time_constraint(alarm))
def test_base_time_constraints_complex(self): alarm = mock.MagicMock() alarm.time_constraints = [ { "name": "test", "description": "test", # Every consecutive 2 minutes (from the 3rd to the 57th) past # every consecutive 2 hours (between 3:00 and 12:59) on every day. "start": "3-57/2 3-12/2 * * *", "duration": 30, "timezone": "", } ] cls = evaluator.Evaluator # test minutes inside timeutils.set_time_override(datetime.datetime(2014, 1, 5, 3, 3, 0)) self.assertTrue(cls.within_time_constraint(alarm)) timeutils.set_time_override(datetime.datetime(2014, 1, 5, 3, 31, 0)) self.assertTrue(cls.within_time_constraint(alarm)) timeutils.set_time_override(datetime.datetime(2014, 1, 5, 3, 57, 0)) self.assertTrue(cls.within_time_constraint(alarm)) # test minutes outside timeutils.set_time_override(datetime.datetime(2014, 1, 5, 3, 2, 0)) self.assertFalse(cls.within_time_constraint(alarm)) timeutils.set_time_override(datetime.datetime(2014, 1, 5, 3, 4, 0)) self.assertFalse(cls.within_time_constraint(alarm)) timeutils.set_time_override(datetime.datetime(2014, 1, 5, 3, 58, 0)) self.assertFalse(cls.within_time_constraint(alarm)) # test hours inside timeutils.set_time_override(datetime.datetime(2014, 1, 5, 3, 31, 0)) self.assertTrue(cls.within_time_constraint(alarm)) timeutils.set_time_override(datetime.datetime(2014, 1, 5, 5, 31, 0)) self.assertTrue(cls.within_time_constraint(alarm)) timeutils.set_time_override(datetime.datetime(2014, 1, 5, 11, 31, 0)) self.assertTrue(cls.within_time_constraint(alarm)) # test hours outside timeutils.set_time_override(datetime.datetime(2014, 1, 5, 1, 31, 0)) self.assertFalse(cls.within_time_constraint(alarm)) timeutils.set_time_override(datetime.datetime(2014, 1, 5, 4, 31, 0)) self.assertFalse(cls.within_time_constraint(alarm)) timeutils.set_time_override(datetime.datetime(2014, 1, 5, 12, 31, 0)) self.assertFalse(cls.within_time_constraint(alarm))