def test_load(self): # Insert two dummy watch rules into the DB values = {'stack_id': self.stack_id, 'state': 'NORMAL', 'name': u'HttpFailureAlarm', 'rule': {u'EvaluationPeriods': u'1', u'AlarmActions': [u'WebServerRestartPolicy'], u'AlarmDescription': u'Restart the WikiDatabase', u'Namespace': u'system/linux', u'Period': u'300', u'ComparisonOperator': u'GreaterThanThreshold', u'Statistic': u'SampleCount', u'Threshold': u'2', u'MetricName': u'ServiceFailure'}} db_ret = db_api.watch_rule_create(self.ctx, values) self.assertNotEqual(db_ret, None) values['name'] = 'AnotherWatch' db_ret = db_api.watch_rule_create(self.ctx, values) self.assertNotEqual(db_ret, None) # Then use WatchRule.load() to retrieve each by name # and check that the object properties match the data above for wn in ('HttpFailureAlarm', 'AnotherWatch'): wr = watchrule.WatchRule.load(self.ctx, wn) self.assertEqual(type(wr), watchrule.WatchRule) self.assertEqual(wr.name, wn) self.assertEqual(wr.state, values['state']) self.assertEqual(wr.rule, values['rule']) self.assertEqual(wr.timeperiod, datetime.timedelta( seconds=int(values['rule']['Period']))) # Cleanup db_api.watch_rule_delete(self.ctx, 'HttpFailureAlarm') db_api.watch_rule_delete(self.ctx, 'AnotherWatch')
def test_set_watch_state_badstate(self): # Insert dummy watch rule into the DB values = {'stack_id': self.stack.id, 'state': 'NORMAL', 'name': u'OverrideAlarm2', 'rule': {u'EvaluationPeriods': u'1', u'AlarmActions': [u'WebServerRestartPolicy'], u'AlarmDescription': u'Restart the WikiDatabase', u'Namespace': u'system/linux', u'Period': u'300', u'ComparisonOperator': u'GreaterThanThreshold', u'Statistic': u'SampleCount', u'Threshold': u'2', u'MetricName': u'ServiceFailure'}} db_ret = db_api.watch_rule_create(self.ctx, values) self.assertNotEqual(db_ret, None) for state in ["HGJHGJHG", "1234", "!\*(&%"]: self.assertRaises(ValueError, self.man.set_watch_state, self.ctx, watch_name="OverrideAlarm2", state=state) # Cleanup, delete the dummy rule db_api.watch_rule_delete(self.ctx, "OverrideAlarm2")
def test_set_watch_state(self): # Insert dummy watch rule into the DB values = { 'stack_id': self.stack.id, 'state': 'NORMAL', 'name': u'OverrideAlarm', 'rule': { u'EvaluationPeriods': u'1', u'AlarmActions': [u'WebServerRestartPolicy'], u'AlarmDescription': u'Restart the WikiDatabase', u'Namespace': u'system/linux', u'Period': u'300', u'ComparisonOperator': u'GreaterThanThreshold', u'Statistic': u'SampleCount', u'Threshold': u'2', u'MetricName': u'ServiceFailure' } } db_ret = db_api.watch_rule_create(self.ctx, values) self.assertNotEqual(db_ret, None) class DummyAction: alarm = "dummyfoo" dummy_action = DummyAction() self.m.StubOutWithMock(parser.Stack, '__getitem__') parser.Stack.__getitem__('WebServerRestartPolicy').AndReturn( dummy_action) # Replace the real stack threadgroup with a dummy one, so we can # check the function returned on ALARM is correctly scheduled self.eng.stg[self.stack.id] = DummyThreadGroup() self.m.ReplayAll() state = watchrule.WatchRule.NODATA result = self.eng.set_watch_state(self.ctx, watch_name="OverrideAlarm", state=state) self.assertEqual(result[engine_api.WATCH_STATE_VALUE], state) self.assertEqual(self.eng.stg[self.stack.id].threads, []) state = watchrule.WatchRule.NORMAL result = self.eng.set_watch_state(self.ctx, watch_name="OverrideAlarm", state=state) self.assertEqual(result[engine_api.WATCH_STATE_VALUE], state) self.assertEqual(self.eng.stg[self.stack.id].threads, []) state = watchrule.WatchRule.ALARM result = self.eng.set_watch_state(self.ctx, watch_name="OverrideAlarm", state=state) self.assertEqual(result[engine_api.WATCH_STATE_VALUE], state) self.assertEqual(self.eng.stg[self.stack.id].threads, [DummyAction.alarm]) self.m.VerifyAll() # Cleanup, delete the dummy rule db_api.watch_rule_delete(self.ctx, "OverrideAlarm")
def test_set_watch_state_badstate(self): # Insert dummy watch rule into the DB values = { 'stack_id': self.stack.id, 'state': 'NORMAL', 'name': u'OverrideAlarm2', 'rule': { u'EvaluationPeriods': u'1', u'AlarmActions': [u'WebServerRestartPolicy'], u'AlarmDescription': u'Restart the WikiDatabase', u'Namespace': u'system/linux', u'Period': u'300', u'ComparisonOperator': u'GreaterThanThreshold', u'Statistic': u'SampleCount', u'Threshold': u'2', u'MetricName': u'ServiceFailure' } } db_ret = db_api.watch_rule_create(self.ctx, values) self.assertNotEqual(db_ret, None) for state in ["HGJHGJHG", "1234", "!\*(&%"]: self.assertRaises(ValueError, self.man.set_watch_state, self.ctx, watch_name="OverrideAlarm2", state=state) # Cleanup, delete the dummy rule db_api.watch_rule_delete(self.ctx, "OverrideAlarm2")
def test_show_watch(self): # Insert two dummy watch rules into the DB values = { 'stack_id': self.stack.id, 'state': 'NORMAL', 'name': u'HttpFailureAlarm', 'rule': { u'EvaluationPeriods': u'1', u'AlarmActions': [u'WebServerRestartPolicy'], u'AlarmDescription': u'Restart the WikiDatabase', u'Namespace': u'system/linux', u'Period': u'300', u'ComparisonOperator': u'GreaterThanThreshold', u'Statistic': u'SampleCount', u'Threshold': u'2', u'MetricName': u'ServiceFailure' } } db_ret = db_api.watch_rule_create(self.ctx, values) self.assertNotEqual(db_ret, None) values['name'] = "AnotherWatch" db_ret = db_api.watch_rule_create(self.ctx, values) self.assertNotEqual(db_ret, None) # watch_name=None should return both watches result = self.eng.show_watch(self.ctx, watch_name=None) self.assertEqual(2, len(result)) # watch_name="HttpFailureAlarm" should return only one result = self.eng.show_watch(self.ctx, watch_name="HttpFailureAlarm") self.assertEqual(1, len(result)) self.assertRaises(exception.WatchRuleNotFound, self.eng.show_watch, self.ctx, watch_name="nonexistent") # Check the response has all keys defined in the engine API for key in engine_api.WATCH_KEYS: self.assertTrue(key in result[0]) # Cleanup, delete the dummy rules db_api.watch_rule_delete(self.ctx, "HttpFailureAlarm") db_api.watch_rule_delete(self.ctx, "AnotherWatch")
def test_set_watch_state(self): # Insert dummy watch rule into the DB values = {'stack_id': self.stack.id, 'state': 'NORMAL', 'name': u'OverrideAlarm', 'rule': {u'EvaluationPeriods': u'1', u'AlarmActions': [u'WebServerRestartPolicy'], u'AlarmDescription': u'Restart the WikiDatabase', u'Namespace': u'system/linux', u'Period': u'300', u'ComparisonOperator': u'GreaterThanThreshold', u'Statistic': u'SampleCount', u'Threshold': u'2', u'MetricName': u'ServiceFailure'}} db_ret = db_api.watch_rule_create(self.ctx, values) self.assertNotEqual(db_ret, None) class DummyAction: alarm = "dummyfoo" dummy_action = DummyAction() self.m.StubOutWithMock(parser.Stack, '__getitem__') parser.Stack.__getitem__( 'WebServerRestartPolicy').AndReturn(dummy_action) # Replace the real stack threadgroup with a dummy one, so we can # check the function returned on ALARM is correctly scheduled self.eng.stg[self.stack.id] = DummyThreadGroup() self.m.ReplayAll() state = watchrule.WatchRule.NODATA result = self.eng.set_watch_state(self.ctx, watch_name="OverrideAlarm", state=state) self.assertEqual(result[engine_api.WATCH_STATE_VALUE], state) self.assertEqual(self.eng.stg[self.stack.id].threads, []) state = watchrule.WatchRule.NORMAL result = self.eng.set_watch_state(self.ctx, watch_name="OverrideAlarm", state=state) self.assertEqual(result[engine_api.WATCH_STATE_VALUE], state) self.assertEqual(self.eng.stg[self.stack.id].threads, []) state = watchrule.WatchRule.ALARM result = self.eng.set_watch_state(self.ctx, watch_name="OverrideAlarm", state=state) self.assertEqual(result[engine_api.WATCH_STATE_VALUE], state) self.assertEqual(self.eng.stg[self.stack.id].threads, [DummyAction.alarm]) self.m.VerifyAll() # Cleanup, delete the dummy rule db_api.watch_rule_delete(self.ctx, "OverrideAlarm")
def handle_create(self): wr_values = { 'name': self.name, 'rule': self.parsed_template('Properties'), 'state': 'NORMAL', 'stack_name': self.stack.name } wr = db_api.watch_rule_create(self.context, wr_values) self.instance_id = wr.id
def test_show_watch_metric(self): # Insert dummy watch rule into the DB values = { 'stack_id': self.stack.id, 'state': 'NORMAL', 'name': u'HttpFailureAlarm', 'rule': { u'EvaluationPeriods': u'1', u'AlarmActions': [u'WebServerRestartPolicy'], u'AlarmDescription': u'Restart the WikiDatabase', u'Namespace': u'system/linux', u'Period': u'300', u'ComparisonOperator': u'GreaterThanThreshold', u'Statistic': u'SampleCount', u'Threshold': u'2', u'MetricName': u'ServiceFailure' } } db_ret = db_api.watch_rule_create(self.ctx, values) self.assertNotEqual(db_ret, None) # And add a metric datapoint watch = db_api.watch_rule_get_by_name(self.ctx, "HttpFailureAlarm") self.assertNotEqual(watch, None) values = { 'watch_rule_id': watch.id, 'data': { u'Namespace': u'system/linux', u'ServiceFailure': { u'Units': u'Counter', u'Value': 1 } } } watch = db_api.watch_data_create(self.ctx, values) # Check there is one result returned result = self.eng.show_watch_metric(self.ctx, metric_namespace=None, metric_name=None) self.assertEqual(1, len(result)) # Create another metric datapoint and check we get two watch = db_api.watch_data_create(self.ctx, values) result = self.eng.show_watch_metric(self.ctx, metric_namespace=None, metric_name=None) self.assertEqual(2, len(result)) # Cleanup, delete the dummy rule db_api.watch_rule_delete(self.ctx, "HttpFailureAlarm") # Check the response has all keys defined in the engine API for key in engine_api.WATCH_DATA_KEYS: self.assertTrue(key in result[0])
def test_show_watch(self): # Insert two dummy watch rules into the DB values = {'stack_id': self.stack.id, 'state': 'NORMAL', 'name': u'HttpFailureAlarm', 'rule': {u'EvaluationPeriods': u'1', u'AlarmActions': [u'WebServerRestartPolicy'], u'AlarmDescription': u'Restart the WikiDatabase', u'Namespace': u'system/linux', u'Period': u'300', u'ComparisonOperator': u'GreaterThanThreshold', u'Statistic': u'SampleCount', u'Threshold': u'2', u'MetricName': u'ServiceFailure'}} db_ret = db_api.watch_rule_create(self.ctx, values) self.assertNotEqual(db_ret, None) values['name'] = "AnotherWatch" db_ret = db_api.watch_rule_create(self.ctx, values) self.assertNotEqual(db_ret, None) # watch_name=None should return both watches result = self.man.show_watch(self.ctx, watch_name=None) self.assertEqual(2, len(result)) # watch_name="HttpFailureAlarm" should return only one result = self.man.show_watch(self.ctx, watch_name="HttpFailureAlarm") self.assertEqual(1, len(result)) self.assertRaises(exception.WatchRuleNotFound, self.man.show_watch, self.ctx, watch_name="nonexistent") # Check the response has all keys defined in the engine API for key in engine_api.WATCH_KEYS: self.assertTrue(key in result[0]) # Cleanup, delete the dummy rules db_api.watch_rule_delete(self.ctx, "HttpFailureAlarm") db_api.watch_rule_delete(self.ctx, "AnotherWatch")
def test_load(self): # Insert two dummy watch rules into the DB values = { 'stack_id': self.stack_id, 'state': 'NORMAL', 'name': u'HttpFailureAlarm', 'rule': { u'EvaluationPeriods': u'1', u'AlarmActions': [u'WebServerRestartPolicy'], u'AlarmDescription': u'Restart the WikiDatabase', u'Namespace': u'system/linux', u'Period': u'300', u'ComparisonOperator': u'GreaterThanThreshold', u'Statistic': u'SampleCount', u'Threshold': u'2', u'MetricName': u'ServiceFailure' } } db_ret = db_api.watch_rule_create(self.ctx, values) self.assertNotEqual(db_ret, None) values['name'] = 'AnotherWatch' db_ret = db_api.watch_rule_create(self.ctx, values) self.assertNotEqual(db_ret, None) # Then use WatchRule.load() to retrieve each by name # and check that the object properties match the data above for wn in ('HttpFailureAlarm', 'AnotherWatch'): wr = watchrule.WatchRule.load(self.ctx, wn) self.assertEqual(type(wr), watchrule.WatchRule) self.assertEqual(wr.name, wn) self.assertEqual(wr.state, values['state']) self.assertEqual(wr.rule, values['rule']) self.assertEqual( wr.timeperiod, datetime.timedelta(seconds=int(values['rule']['Period']))) # Cleanup db_api.watch_rule_delete(self.ctx, 'HttpFailureAlarm') db_api.watch_rule_delete(self.ctx, 'AnotherWatch')
def test_show_watch_metric(self): # Insert dummy watch rule into the DB values = {'stack_id': self.stack.id, 'state': 'NORMAL', 'name': u'HttpFailureAlarm', 'rule': {u'EvaluationPeriods': u'1', u'AlarmActions': [u'WebServerRestartPolicy'], u'AlarmDescription': u'Restart the WikiDatabase', u'Namespace': u'system/linux', u'Period': u'300', u'ComparisonOperator': u'GreaterThanThreshold', u'Statistic': u'SampleCount', u'Threshold': u'2', u'MetricName': u'ServiceFailure'}} db_ret = db_api.watch_rule_create(self.ctx, values) self.assertNotEqual(db_ret, None) # And add a metric datapoint watch = db_api.watch_rule_get_by_name(self.ctx, "HttpFailureAlarm") self.assertNotEqual(watch, None) values = {'watch_rule_id': watch.id, 'data': {u'Namespace': u'system/linux', u'ServiceFailure': { u'Units': u'Counter', u'Value': 1}}} watch = db_api.watch_data_create(self.ctx, values) # Check there is one result returned result = self.eng.show_watch_metric(self.ctx, metric_namespace=None, metric_name=None) self.assertEqual(1, len(result)) # Create another metric datapoint and check we get two watch = db_api.watch_data_create(self.ctx, values) result = self.eng.show_watch_metric(self.ctx, metric_namespace=None, metric_name=None) self.assertEqual(2, len(result)) # Cleanup, delete the dummy rule db_api.watch_rule_delete(self.ctx, "HttpFailureAlarm") # Check the response has all keys defined in the engine API for key in engine_api.WATCH_DATA_KEYS: self.assertTrue(key in result[0])
def store(self): ''' Store the watchrule in the database and return its ID If self.id is set, we update the existing rule ''' wr_values = { 'name': self.name, 'rule': self.rule, 'state': self.state, 'stack_id': self.stack_id } if not self.id: wr = db_api.watch_rule_create(self.context, wr_values) self.id = wr.id else: db_api.watch_rule_update(self.context, self.id, wr_values)
def create(cls, context, values): return cls._from_db_object(context, cls(), db_api.watch_rule_create(context, values))