Ejemplo n.º 1
0
    def test_ave(self):
        rule = {
            'EvaluationPeriods': '1',
            'MetricName': 'test_metric',
            'Period': '300',
            'Statistic': 'Average',
            'ComparisonOperator': 'GreaterThanThreshold',
            'Threshold': '100'
        }

        now = timeutils.utcnow()
        last = now - datetime.timedelta(seconds=320)
        data = [WatchData(117, now - datetime.timedelta(seconds=100))]
        data.append(WatchData(23, now - datetime.timedelta(seconds=150)))

        self.wr = watchrule.WatchRule(context=self.ctx,
                                      watch_name="testwatch",
                                      rule=rule,
                                      watch_data=data,
                                      stack_id=self.stack_id,
                                      last_evaluated=last)
        self.wr.now = now
        new_state = self.wr.get_alarm_state()
        self.assertEqual('NORMAL', new_state)

        data.append(WatchData(195, now - datetime.timedelta(seconds=250)))
        self.wr = watchrule.WatchRule(context=self.ctx,
                                      watch_name="testwatch",
                                      rule=rule,
                                      watch_data=data,
                                      stack_id=self.stack_id,
                                      last_evaluated=last)
        self.wr.now = now
        new_state = self.wr.get_alarm_state()
        self.assertEqual('ALARM', new_state)
Ejemplo n.º 2
0
    def test_sum(self):
        rule = {'EvaluationPeriods': '1',
                'MetricName': 'test_metric',
                'Period': '300',
                'Statistic': 'Sum',
                'ComparisonOperator': 'GreaterThanOrEqualToThreshold',
                'Threshold': '100'}

        now = timeutils.utcnow()
        last = now - datetime.timedelta(seconds=320)
        data = [WatchData(17, now - datetime.timedelta(seconds=100))]
        data.append(WatchData(23, now - datetime.timedelta(seconds=150)))

        # all < 40 -> NORMAL
        watcher = watchrule.WatchRule(context=self.ctx,
                                      watch_name="testwatch",
                                      rule=rule,
                                      watch_data=data,
                                      stack_id=self.stack_id,
                                      last_evaluated=last)
        watcher.now = now
        new_state = watcher.get_alarm_state()
        self.assertEqual(new_state, 'NORMAL')

        # sum > 100 -> ALARM
        data.append(WatchData(85, now - datetime.timedelta(seconds=150)))
        watcher = watchrule.WatchRule(context=self.ctx,
                                      watch_name="testwatch",
                                      rule=rule,
                                      watch_data=data,
                                      stack_id=self.stack_id,
                                      last_evaluated=last)
        watcher.now = now
        new_state = watcher.get_alarm_state()
        self.assertEqual(new_state, 'ALARM')
Ejemplo n.º 3
0
    def test_minimum(self):
        rule = {
            'EvaluationPeriods': '1',
            'MetricName': 'test_metric',
            'Period': '300',
            'Statistic': 'Minimum',
            'ComparisonOperator': 'LessThanOrEqualToThreshold',
            'Threshold': '50'
        }

        now = timeutils.utcnow()
        last = now - datetime.timedelta(seconds=320)
        data = [WatchData(77, now - datetime.timedelta(seconds=100))]
        data.append(WatchData(53, now - datetime.timedelta(seconds=150)))

        # all > 50 -> NORMAL
        self.wr = watchrule.WatchRule(context=self.ctx,
                                      watch_name="testwatch",
                                      rule=rule,
                                      watch_data=data,
                                      stack_id=self.stack_id,
                                      last_evaluated=last)
        new_state = self.wr.get_alarm_state()
        self.assertEqual('NORMAL', new_state)

        data.append(WatchData(25, now - datetime.timedelta(seconds=250)))
        self.wr = watchrule.WatchRule(context=self.ctx,
                                      watch_name="testwatch",
                                      rule=rule,
                                      watch_data=data,
                                      stack_id=self.stack_id,
                                      last_evaluated=last)
        new_state = self.wr.get_alarm_state()
        self.assertEqual('ALARM', new_state)
Ejemplo n.º 4
0
    def test_minimum(self):
        # Setup
        rule = {'EvaluationPeriods': '1',
                'MetricName': 'test_metric',
                'Period': '300',
                'Statistic': 'Minimum',
                'ComparisonOperator': 'LessThanOrEqualToThreshold',
                'Threshold': '50'}

        now = timeutils.utcnow()
        last = now - datetime.timedelta(seconds=320)
        data = [WatchData(77, now - datetime.timedelta(seconds=100))]

        # Test 1 - Values greater than 0 are normal
        data.append(WatchData(53, now - datetime.timedelta(seconds=150)))
        wr = watchrule.WatchRule(self.ctx,
                                 'testwatch',
                                 rule,
                                 watch_data=data,
                                 stack_id=self.stack_id,
                                 last_evaluated=last)
        new_state = wr.get_alarm_state()
        self.assertEqual('NORMAL', new_state)

        # Test 2
        data.append(WatchData(25, now - datetime.timedelta(seconds=250)))
        wr = watchrule.WatchRule(self.ctx,
                                 'testwatch',
                                 rule,
                                 watch_data=data,
                                 stack_id=self.stack_id,
                                 last_evaluated=last)
        new_state = wr.get_alarm_state()
        self.assertEqual('ALARM', new_state)
Ejemplo n.º 5
0
    def test_evaluate(self):
        rule = {
            'EvaluationPeriods': '1',
            'MetricName': 'test_metric',
            'Period': '300',
            'Statistic': 'Maximum',
            'ComparisonOperator': 'GreaterThanOrEqualToThreshold',
            'Threshold': '30'
        }

        now = timeutils.utcnow()
        self.m.StubOutWithMock(timeutils, 'utcnow')
        timeutils.utcnow().MultipleTimes().AndReturn(now)
        self.m.ReplayAll()

        # It's not time to evaluate, so should stay NODATA
        last = now - datetime.timedelta(seconds=299)
        data = WatchData(25, now - datetime.timedelta(seconds=150))
        self.wr = watchrule.WatchRule(context=self.ctx,
                                      watch_name="testwatch",
                                      rule=rule,
                                      watch_data=[data],
                                      stack_id=self.stack_id,
                                      last_evaluated=last)

        actions = self.wr.evaluate()
        self.assertEqual('NODATA', self.wr.state)
        self.assertEqual([], actions)

        # now - last == Period, so should set NORMAL
        last = now - datetime.timedelta(seconds=300)
        data = WatchData(25, now - datetime.timedelta(seconds=150))
        self.wr = watchrule.WatchRule(context=self.ctx,
                                      watch_name="testwatch",
                                      rule=rule,
                                      watch_data=[data],
                                      stack_id=self.stack_id,
                                      last_evaluated=last)

        actions = self.wr.evaluate()
        self.assertEqual('NORMAL', self.wr.state)
        self.assertEqual(now, self.wr.last_evaluated)
        self.assertEqual([], actions)

        # Now data breaches Threshold, so should set ALARM
        last = now - datetime.timedelta(seconds=300)
        data = WatchData(35, now - datetime.timedelta(seconds=150))
        self.wr = watchrule.WatchRule(context=self.ctx,
                                      watch_name="testwatch",
                                      rule=rule,
                                      watch_data=[data],
                                      stack_id=self.stack_id,
                                      last_evaluated=last)

        actions = self.wr.evaluate()
        self.assertEqual('ALARM', self.wr.state)
        self.assertEqual(now, self.wr.last_evaluated)
        self.assertEqual([], actions)
Ejemplo n.º 6
0
    def test_evaluate(self):
        # Setup
        rule = {
            'EvaluationPeriods': '1',
            'MetricName': 'test_metric',
            'Period': '300',
            'Statistic': 'Maximum',
            'ComparisonOperator': 'GreaterThanOrEqualToThreshold',
            'Threshold': '30'
        }

        now = timeutils.utcnow()
        timeutils.set_time_override(now)
        self.addCleanup(timeutils.clear_time_override)

        # Test 1 - It's not time to evaluate, so should stay NODATA
        last = now - datetime.timedelta(seconds=299)
        data = WatchData(25, now - datetime.timedelta(seconds=150))
        wr = watchrule.WatchRule(context=self.ctx,
                                 watch_name="testwatch",
                                 rule=rule,
                                 watch_data=[data],
                                 stack_id=self.stack_id,
                                 last_evaluated=last)

        actions = wr.evaluate()
        self.assertEqual('NODATA', wr.state)
        self.assertEqual([], actions)

        # Test 2 - now - last == Period, so should set NORMAL
        last = now - datetime.timedelta(seconds=300)
        data = WatchData(25, now - datetime.timedelta(seconds=150))
        wr = watchrule.WatchRule(context=self.ctx,
                                 watch_name="testwatch",
                                 rule=rule,
                                 watch_data=[data],
                                 stack_id=self.stack_id,
                                 last_evaluated=last)

        actions = wr.evaluate()
        self.assertEqual('NORMAL', wr.state)
        self.assertEqual(now, wr.last_evaluated)
        self.assertEqual([], actions)

        # Test 3 - Now data breaches Threshold, so should set ALARM
        last = now - datetime.timedelta(seconds=300)
        data = WatchData(35, now - datetime.timedelta(seconds=150))
        wr = watchrule.WatchRule(context=self.ctx,
                                 watch_name="testwatch",
                                 rule=rule,
                                 watch_data=[data],
                                 stack_id=self.stack_id,
                                 last_evaluated=last)

        actions = wr.evaluate()
        self.assertEqual('ALARM', wr.state)
        self.assertEqual(now, wr.last_evaluated)
        self.assertEqual([], actions)
Ejemplo n.º 7
0
    def test_show_watch(self):
        # Insert two dummy watch rules into the DB
        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'
        }
        self.wr = []
        self.wr.append(
            watchrule.WatchRule(context=self.ctx,
                                watch_name='show_watch_1',
                                rule=rule,
                                watch_data=[],
                                stack_id=self.stack.id,
                                state='NORMAL'))
        self.wr[0].store()

        self.wr.append(
            watchrule.WatchRule(context=self.ctx,
                                watch_name='show_watch_2',
                                rule=rule,
                                watch_data=[],
                                stack_id=self.stack.id,
                                state='NORMAL'))
        self.wr[1].store()

        # watch_name=None should return all watches
        result = self.eng.show_watch(self.ctx, watch_name=None)
        result_names = [r.get('name') for r in result]
        self.assertIn('show_watch_1', result_names)
        self.assertIn('show_watch_2', result_names)

        result = self.eng.show_watch(self.ctx, watch_name="show_watch_1")
        self.assertEqual(1, len(result))
        self.assertIn('name', result[0])
        self.assertEqual('show_watch_1', result[0]['name'])

        result = self.eng.show_watch(self.ctx, watch_name="show_watch_2")
        self.assertEqual(1, len(result))
        self.assertIn('name', result[0])
        self.assertEqual('show_watch_2', result[0]['name'])

        ex = self.assertRaises(dispatcher.ExpectedException,
                               self.eng.show_watch,
                               self.ctx,
                               watch_name="nonexistent")
        self.assertEqual(exception.EntityNotFound, ex.exc_info[0])

        # Check the response has all keys defined in the engine API
        for key in rpc_api.WATCH_KEYS:
            self.assertIn(key, result[0])
Ejemplo n.º 8
0
    def test_samplecount(self):

        rule = {
            'EvaluationPeriods': '1',
            'MetricName': 'test_metric',
            'Period': '300',
            'Statistic': 'SampleCount',
            'ComparisonOperator': 'GreaterThanOrEqualToThreshold',
            'Threshold': '3'
        }

        now = timeutils.utcnow()
        last = now - datetime.timedelta(seconds=320)
        data = [WatchData(1, now - datetime.timedelta(seconds=100))]
        data.append(WatchData(1, now - datetime.timedelta(seconds=150)))

        # only 2 samples -> NORMAL
        watcher = watchrule.WatchRule(context=self.ctx,
                                      watch_name="testwatch",
                                      rule=rule,
                                      watch_data=data,
                                      stack_id=self.stack_id,
                                      last_evaluated=last)
        watcher.now = now
        new_state = watcher.get_alarm_state()
        logger.info(new_state)
        self.assertEqual(new_state, 'NORMAL')

        # only 3 samples -> ALARM
        data.append(WatchData(1, now - datetime.timedelta(seconds=200)))
        watcher = watchrule.WatchRule(context=self.ctx,
                                      watch_name="testwatch",
                                      rule=rule,
                                      watch_data=data,
                                      stack_id=self.stack_id,
                                      last_evaluated=last)
        watcher.now = now
        new_state = watcher.get_alarm_state()
        logger.info(new_state)
        self.assertEqual(new_state, 'ALARM')

        # only 3 samples (one old) -> NORMAL
        data.pop(0)
        data.append(WatchData(1, now - datetime.timedelta(seconds=400)))
        watcher = watchrule.WatchRule(context=self.ctx,
                                      watch_name="testwatch",
                                      rule=rule,
                                      watch_data=data,
                                      stack_id=self.stack_id,
                                      last_evaluated=last)
        watcher.now = now
        new_state = watcher.get_alarm_state()
        logger.info(new_state)
        self.assertEqual(new_state, 'NORMAL')
Ejemplo n.º 9
0
    def test_samplecount(self):
        # Setup
        rule = {
            'EvaluationPeriods': '1',
            'MetricName': 'test_metric',
            'Period': '300',
            'Statistic': 'SampleCount',
            'ComparisonOperator': 'GreaterThanOrEqualToThreshold',
            'Threshold': '3'
        }

        now = timeutils.utcnow()
        last = now - datetime.timedelta(seconds=320)
        data = [WatchData(1, now - datetime.timedelta(seconds=100))]

        # Test 1 - 2 samples is normal
        data.append(WatchData(1, now - datetime.timedelta(seconds=150)))
        wr = watchrule.WatchRule(context=self.ctx,
                                 watch_name="testwatch",
                                 rule=rule,
                                 watch_data=data,
                                 stack_id=self.stack_id,
                                 last_evaluated=last)
        wr.now = now
        new_state = wr.get_alarm_state()
        self.assertEqual('NORMAL', new_state)

        # Test 2 - 3 samples is an alarm
        data.append(WatchData(1, now - datetime.timedelta(seconds=200)))
        wr = watchrule.WatchRule(context=self.ctx,
                                 watch_name="testwatch",
                                 rule=rule,
                                 watch_data=data,
                                 stack_id=self.stack_id,
                                 last_evaluated=last)
        wr.now = now
        new_state = wr.get_alarm_state()
        self.assertEqual('ALARM', new_state)

        # Test 3 - 3 samples (one old) is normal
        data.pop(0)
        data.append(WatchData(1, now - datetime.timedelta(seconds=400)))
        wr = watchrule.WatchRule(context=self.ctx,
                                 watch_name="testwatch",
                                 rule=rule,
                                 watch_data=data,
                                 stack_id=self.stack_id,
                                 last_evaluated=last)
        wr.now = now
        new_state = wr.get_alarm_state()
        self.assertEqual('NORMAL', new_state)
Ejemplo n.º 10
0
    def test_evaluate_suspend(self):
        rule = {
            'EvaluationPeriods': '1',
            'MetricName': 'test_metric',
            'Period': '300',
            'Statistic': 'Maximum',
            'ComparisonOperator': 'GreaterThanOrEqualToThreshold',
            'Threshold': '30'
        }

        now = timeutils.utcnow()
        self.m.StubOutWithMock(timeutils, 'utcnow')
        timeutils.utcnow().MultipleTimes().AndReturn(now)
        self.m.ReplayAll()

        # Now data breaches Threshold, but we're suspended
        last = now - datetime.timedelta(seconds=300)
        data = WatchData(35, now - datetime.timedelta(seconds=150))
        self.wr = watchrule.WatchRule(context=self.ctx,
                                      watch_name="testwatch",
                                      rule=rule,
                                      watch_data=[data],
                                      stack_id=self.stack_id,
                                      last_evaluated=last)

        self.wr.state_set(self.wr.SUSPENDED)

        actions = self.wr.evaluate()
        self.assertEqual(self.wr.SUSPENDED, self.wr.state)
        self.assertEqual([], actions)
Ejemplo n.º 11
0
    def test_rule_actions_alarm_normal(self):
        rule = {'EvaluationPeriods': '1',
                'MetricName': 'test_metric',
                'AlarmActions': ['DummyAction'],
                'Period': '300',
                'Statistic': 'Maximum',
                'ComparisonOperator': 'GreaterThanOrEqualToThreshold',
                'Threshold': '30'}

        now = timeutils.utcnow()
        self._action_set_stubs(now, action_expected=False)

        # Set data so rule evaluates to NORMAL state
        last = now - datetime.timedelta(seconds=300)
        data = WatchData(25, now - datetime.timedelta(seconds=150))
        watcher = watchrule.WatchRule(context=self.ctx,
                                      watch_name="testwatch",
                                      rule=rule,
                                      watch_data=[data],
                                      stack_id=self.stack_id,
                                      last_evaluated=last)

        actions = watcher.evaluate()
        self.assertEqual(watcher.state, 'NORMAL')
        self.assertEqual(actions, [])
        self.m.VerifyAll()
Ejemplo n.º 12
0
    def test_rule_actions_alarm_alarm(self):
        rule = {'EvaluationPeriods': '1',
                'MetricName': 'test_metric',
                'AlarmActions': ['DummyAction'],
                'Period': '300',
                'Statistic': 'Maximum',
                'ComparisonOperator': 'GreaterThanOrEqualToThreshold',
                'Threshold': '30'}

        now = timeutils.utcnow()
        self._action_set_stubs(now)

        # Set data so rule evaluates to ALARM state
        last = now - datetime.timedelta(seconds=300)
        data = WatchData(35, now - datetime.timedelta(seconds=150))
        watcher = watchrule.WatchRule(context=self.ctx,
                                      watch_name="testwatch",
                                      rule=rule,
                                      watch_data=[data],
                                      stack_id=self.stack_id,
                                      last_evaluated=last)

        actions = watcher.evaluate()
        self.assertEqual(watcher.state, 'ALARM')
        self.assertEqual(actions, ['DummyAction'])

        # re-set last_evaluated so the rule will be evaluated again,
        # but since we're already in ALARM state, we should not generate
        # any additional actions
        last = now - datetime.timedelta(seconds=300)
        watcher.last_evaluated = last
        actions = watcher.evaluate()
        self.assertEqual(watcher.state, 'ALARM')
        self.assertEqual(actions, [])
        self.m.VerifyAll()
Ejemplo n.º 13
0
    def test_create_watch_data(self):
        rule = {u'EvaluationPeriods': u'1',
                u'AlarmDescription': u'test alarm',
                u'Period': u'300',
                u'ComparisonOperator': u'GreaterThanThreshold',
                u'Statistic': u'SampleCount',
                u'Threshold': u'2',
                u'MetricName': u'CreateDataMetric'}
        wr = watchrule.WatchRule(context=self.ctx,
                                 watch_name='create_data_test',
                                 stack_id=self.stack_id, rule=rule)

        wr.store()

        data = {u'CreateDataMetric': {"Unit": "Counter",
                                      "Value": "1",
                                      "Dimensions": []}}
        wr.create_watch_data(data)

        dbwr = db_api.watch_rule_get_by_name(self.ctx, 'create_data_test')
        self.assertEqual(dbwr.watch_data[0].data, data)

        # Note, would be good to write another datapoint and check it
        # but sqlite seems to not interpret the backreference correctly
        # so dbwr.watch_data is always a list containing only the latest
        # datapoint.  In non-test use on mysql this is not the case, we
        # correctly get a list of all datapoints where watch_rule_id ==
        # watch_rule.id, so leave it as a single-datapoint test for now.

        # Cleanup
        db_api.watch_rule_delete(self.ctx, 'create_data_test')
Ejemplo n.º 14
0
    def test_evaluate_ceilometer_controlled(self):
        rule = {
            'EvaluationPeriods': '1',
            'MetricName': 'test_metric',
            'Period': '300',
            'Statistic': 'Maximum',
            'ComparisonOperator': 'GreaterThanOrEqualToThreshold',
            'Threshold': '30'
        }

        now = timeutils.utcnow()
        timeutils.set_time_override(now)
        self.addCleanup(timeutils.clear_time_override)

        # Now data breaches Threshold, but we're suspended
        last = now - datetime.timedelta(seconds=300)
        data = WatchData(35, now - datetime.timedelta(seconds=150))
        self.wr = watchrule.WatchRule(context=self.ctx,
                                      watch_name="testwatch",
                                      rule=rule,
                                      watch_data=[data],
                                      stack_id=self.stack_id,
                                      last_evaluated=last)

        self.wr.state_set(self.wr.CEILOMETER_CONTROLLED)

        actions = self.wr.evaluate()
        self.assertEqual(self.wr.CEILOMETER_CONTROLLED, self.wr.state)
        self.assertEqual([], actions)
Ejemplo n.º 15
0
    def test_set_watch_state_invalid(self):
        # Setup
        rule = {
            'EvaluationPeriods': '1',
            'MetricName': 'test_metric',
            'AlarmActions': ['DummyAction'],
            'Period': '300',
            'Statistic': 'Maximum',
            'ComparisonOperator': 'GreaterThanOrEqualToThreshold',
            'Threshold': '30'
        }

        now = timeutils.utcnow()

        last = now - datetime.timedelta(seconds=200)
        wr = watchrule.WatchRule(context=self.ctx,
                                 watch_name="testwatch",
                                 rule=rule,
                                 watch_data=[],
                                 stack_id=self.stack_id,
                                 last_evaluated=last)

        # Test
        self.assertRaises(ValueError, wr.set_watch_state, None)
        self.assertRaises(ValueError, wr.set_watch_state, "BADSTATE")
Ejemplo n.º 16
0
    def test_evaluate_suspend(self):
        # Setup
        rule = {
            'EvaluationPeriods': '1',
            'MetricName': 'test_metric',
            'Period': '300',
            'Statistic': 'Maximum',
            'ComparisonOperator': 'GreaterThanOrEqualToThreshold',
            'Threshold': '30'
        }

        now = timeutils.utcnow()
        timeutils.set_time_override(now)
        self.addCleanup(timeutils.clear_time_override)

        last = now - datetime.timedelta(seconds=300)
        data = WatchData(35, now - datetime.timedelta(seconds=150))
        wr = watchrule.WatchRule(context=self.ctx,
                                 watch_name="testwatch",
                                 rule=rule,
                                 watch_data=[data],
                                 stack_id=self.stack_id,
                                 last_evaluated=last)
        wr.state_set(wr.SUSPENDED)

        # Test
        actions = wr.evaluate()
        self.assertEqual(wr.SUSPENDED, wr.state)
        self.assertEqual([], actions)
Ejemplo n.º 17
0
    def test_store(self):
        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'
        }
        wr = watchrule.WatchRule(context=self.ctx,
                                 watch_name='storetest',
                                 stack_id=self.stack_id,
                                 rule=rule)
        wr.store()

        dbwr = db_api.watch_rule_get_by_name(self.ctx, 'storetest')
        self.assertNotEqual(dbwr, None)
        self.assertEqual(dbwr.name, 'storetest')
        self.assertEqual(dbwr.state, watchrule.WatchRule.NORMAL)
        self.assertEqual(dbwr.rule, rule)

        # Cleanup
        db_api.watch_rule_delete(self.ctx, 'storetest')
Ejemplo n.º 18
0
    def test_create_watch_data(self):
        rule = {
            u'EvaluationPeriods': u'1',
            u'AlarmDescription': u'test alarm',
            u'Period': u'300',
            u'ComparisonOperator': u'GreaterThanThreshold',
            u'Statistic': u'SampleCount',
            u'Threshold': u'2',
            u'MetricName': u'CreateDataMetric'
        }
        self.wr = watchrule.WatchRule(context=self.ctx,
                                      watch_name='create_data_test',
                                      stack_id=self.stack_id,
                                      rule=rule)

        self.wr.store()

        data = {
            u'CreateDataMetric': {
                "Unit": "Counter",
                "Value": "1",
                "Dimensions": []
            }
        }
        self.wr.create_watch_data(data)

        obj_wr = watch_rule.WatchRule.get_by_name(self.ctx, 'create_data_test')
        obj_wds = [wd for wd in obj_wr.watch_data]
        self.assertEqual(data, obj_wds[0].data)
Ejemplo n.º 19
0
    def test_create_watch_data_match(self):
        rule = {
            u'EvaluationPeriods': u'1',
            u'AlarmDescription': u'test alarm',
            u'Period': u'300',
            u'ComparisonOperator': u'GreaterThanThreshold',
            u'Statistic': u'SampleCount',
            u'Threshold': u'2',
            u'Dimensions': [{
                u'Name': 'AutoScalingGroupName',
                u'Value': 'group_x'
            }],
            u'MetricName': u'CreateDataMetric'
        }
        self.wr = watchrule.WatchRule(context=self.ctx,
                                      watch_name='create_data_test',
                                      stack_id=self.stack_id,
                                      rule=rule)
        self.wr.store()

        data = {
            u'CreateDataMetric': {
                "Unit": "Counter",
                "Value": "1",
                "Dimensions": [{
                    u'AutoScalingGroupName': u'group_x'
                }]
            }
        }
        self.assertTrue(watchrule.rule_can_use_sample(self.wr, data))
Ejemplo n.º 20
0
    def test_destroy(self):
        # Setup
        rule = {
            'EvaluationPeriods': '1',
            'MetricName': 'test_metric',
            'AlarmActions': ['DummyAction'],
            'Period': '300',
            'Statistic': 'Maximum',
            'ComparisonOperator': 'GreaterThanOrEqualToThreshold',
            'Threshold': '30'
        }

        last = timeutils.utcnow()
        wr = watchrule.WatchRule(context=self.ctx,
                                 watch_name='testwatch_destroy',
                                 rule=rule,
                                 watch_data=[],
                                 stack_id=self.stack_id,
                                 last_evaluated=last)

        wr.store()

        # Sanity Check
        check = watchrule.WatchRule.load(context=self.ctx,
                                         watch_name='testwatch_destroy')
        self.assertIsInstance(check, watchrule.WatchRule)

        # Test
        wr.destroy()
        ex = self.assertRaises(exception.EntityNotFound,
                               watchrule.WatchRule.load,
                               context=self.ctx,
                               watch_name='testwatch_destroy')
        self.assertEqual('Watch Rule', ex.kwargs.get('entity'))
Ejemplo n.º 21
0
    def test_rule_actions_alarm_normal(self, mock_get_resource):
        # Setup
        rule = {
            'EvaluationPeriods': '1',
            'MetricName': 'test_metric',
            'AlarmActions': ['DummyAction'],
            'Period': '300',
            'Statistic': 'Maximum',
            'ComparisonOperator': 'GreaterThanOrEqualToThreshold',
            'Threshold': '30'
        }

        now = timeutils.utcnow()
        self._setup_action_mocks(mock_get_resource, now, action_expected=False)

        # Set data so rule evaluates to NORMAL state
        last = now - datetime.timedelta(seconds=300)
        data = WatchData(25, now - datetime.timedelta(seconds=150))
        wr = watchrule.WatchRule(context=self.ctx,
                                 watch_name="testwatch",
                                 rule=rule,
                                 watch_data=[data],
                                 stack_id=self.stack_id,
                                 last_evaluated=last)

        # Test
        actions = wr.evaluate()
        self.assertEqual('NORMAL', wr.state)
        self.assertEqual([], actions)
        self.assertEqual(0, mock_get_resource.call_count)
Ejemplo n.º 22
0
    def test_rule_actions_alarm_two_actions(self):
        rule = {
            'EvaluationPeriods': '1',
            'MetricName': 'test_metric',
            'AlarmActions': ['DummyAction', 'AnotherDummyAction'],
            'Period': '300',
            'Statistic': 'Maximum',
            'ComparisonOperator': 'GreaterThanOrEqualToThreshold',
            'Threshold': '30'
        }

        now = timeutils.utcnow()
        self._action_set_stubs(now)

        # Set data so rule evaluates to ALARM state
        last = now - datetime.timedelta(seconds=300)
        data = WatchData(35, now - datetime.timedelta(seconds=150))
        self.wr = watchrule.WatchRule(context=self.ctx,
                                      watch_name="testwatch",
                                      rule=rule,
                                      watch_data=[data],
                                      stack_id=self.stack_id,
                                      last_evaluated=last)

        actions = self.wr.evaluate()
        self.assertEqual('ALARM', self.wr.state)
        self.assertEqual(['DummyAction', 'DummyAction'], actions)
        self.m.VerifyAll()
Ejemplo n.º 23
0
    def test_set_watch_state_badstate(self, mock_set):
        mock_set.side_effect = ValueError
        # Insert dummy watch rule into the DB
        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'
        }
        self.wr = watchrule.WatchRule(context=self.ctx,
                                      watch_name='OverrideAlarm2',
                                      rule=rule,
                                      watch_data=[],
                                      stack_id=self.stack.id,
                                      state='NORMAL')
        self.wr.store()

        for state in ["HGJHGJHG", "1234", "!\\*(&%"]:
            self.assertRaises(ValueError,
                              self.eng.set_watch_state,
                              self.ctx,
                              watch_name="OverrideAlarm2",
                              state=state)

        calls = [
            mock.call("HGJHGJHG"),
            mock.call("1234"),
            mock.call("!\\*(&%")
        ]
        mock_set.assert_has_calls(calls)
Ejemplo n.º 24
0
    def test_create_watch_data_suspended(self):
        rule = {
            u'EvaluationPeriods': u'1',
            u'AlarmDescription': u'test alarm',
            u'Period': u'300',
            u'ComparisonOperator': u'GreaterThanThreshold',
            u'Statistic': u'SampleCount',
            u'Threshold': u'2',
            u'MetricName': u'CreateDataMetric'
        }
        self.wr = watchrule.WatchRule(context=self.ctx,
                                      watch_name='create_data_test',
                                      stack_id=self.stack_id,
                                      rule=rule,
                                      state=watchrule.WatchRule.SUSPENDED)

        self.wr.store()

        data = {
            u'CreateDataMetric': {
                "Unit": "Counter",
                "Value": "1",
                "Dimensions": []
            }
        }
        self.wr.create_watch_data(data)

        dbwr = db_api.watch_rule_get_by_name(self.ctx, 'create_data_test')
        self.assertEqual([], dbwr.watch_data)
Ejemplo n.º 25
0
    def test_store(self):
        # Setup
        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'
        }

        # Test
        wr = watchrule.WatchRule(context=self.ctx,
                                 watch_name='storetest',
                                 stack_id=self.stack_id,
                                 rule=rule)
        wr.store()

        # Verify
        dbwr = watch_rule.WatchRule.get_by_name(self.ctx, 'storetest')
        self.assertIsNotNone(dbwr)
        self.assertEqual('storetest', dbwr.name)
        self.assertEqual(watchrule.WatchRule.NODATA, dbwr.state)
        self.assertEqual(rule, dbwr.rule)
Ejemplo n.º 26
0
    def test_set_watch_state(self):
        rule = {
            'EvaluationPeriods': '1',
            'MetricName': 'test_metric',
            'AlarmActions': ['DummyAction'],
            'Period': '300',
            'Statistic': 'Maximum',
            'ComparisonOperator': 'GreaterThanOrEqualToThreshold',
            'Threshold': '30'
        }

        now = timeutils.utcnow()
        self._action_set_stubs(now)

        # Set data so rule evaluates to ALARM state
        last = now - datetime.timedelta(seconds=200)
        self.wr = watchrule.WatchRule(context=self.ctx,
                                      watch_name="testwatch",
                                      rule=rule,
                                      watch_data=[],
                                      stack_id=self.stack_id,
                                      last_evaluated=last)

        actions = self.wr.set_watch_state(watchrule.WatchRule.NODATA)
        self.assertEqual([], actions)

        actions = self.wr.set_watch_state(watchrule.WatchRule.NORMAL)
        self.assertEqual([], actions)

        actions = self.wr.set_watch_state(watchrule.WatchRule.ALARM)
        self.assertEqual(['DummyAction'], actions)
        self.m.VerifyAll()
Ejemplo n.º 27
0
    def test_set_watch_state(self, mock_ref):
        self._create_periodic_tasks()
        # Insert dummy watch rule into the DB
        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'
        }
        self.wr = watchrule.WatchRule(context=self.ctx,
                                      watch_name='OverrideAlarm',
                                      rule=rule,
                                      watch_data=[],
                                      stack_id=self.stack.id,
                                      state='NORMAL')
        self.wr.store()

        class DummyAction(object):
            def signal(self):
                return "dummyfoo"

        dummy_action = DummyAction()
        mock_ref.return_value = dummy_action

        # Replace the real stack threadgroup with a dummy one, so we can
        # check the function returned on ALARM is correctly scheduled
        dtg = tools.DummyThreadGroup()
        self.eng.thread_group_mgr.groups[self.stack.id] = dtg

        state = watchrule.WatchRule.NODATA
        result = self.eng.set_watch_state(self.ctx,
                                          watch_name="OverrideAlarm",
                                          state=state)
        self.assertEqual(state, result[rpc_api.WATCH_STATE_VALUE])
        self.assertEqual(
            [], self.eng.thread_group_mgr.groups[self.stack.id].threads)

        state = watchrule.WatchRule.NORMAL
        result = self.eng.set_watch_state(self.ctx,
                                          watch_name="OverrideAlarm",
                                          state=state)
        self.assertEqual(state, result[rpc_api.WATCH_STATE_VALUE])
        self.assertEqual(
            [], self.eng.thread_group_mgr.groups[self.stack.id].threads)

        state = watchrule.WatchRule.ALARM
        result = self.eng.set_watch_state(self.ctx,
                                          watch_name="OverrideAlarm",
                                          state=state)
        self.assertEqual(state, result[rpc_api.WATCH_STATE_VALUE])
        self.assertEqual(
            [dummy_action.signal],
            self.eng.thread_group_mgr.groups[self.stack.id].threads)

        mock_ref.assert_called_once_with('WebServerRestartPolicy')
Ejemplo n.º 28
0
    def test_destroy(self):
        rule = {
            'EvaluationPeriods': '1',
            'MetricName': 'test_metric',
            'AlarmActions': ['DummyAction'],
            'Period': '300',
            'Statistic': 'Maximum',
            'ComparisonOperator': 'GreaterThanOrEqualToThreshold',
            'Threshold': '30'
        }

        last = timeutils.utcnow()
        self.wr = watchrule.WatchRule(context=self.ctx,
                                      watch_name="testwatch_destroy",
                                      rule=rule,
                                      watch_data=[],
                                      stack_id=self.stack_id,
                                      last_evaluated=last)

        self.wr.store()

        check = watchrule.WatchRule.load(context=self.ctx,
                                         watch_name="testwatch_destroy")
        self.assertIsInstance(check, watchrule.WatchRule)

        self.wr.destroy()
        self.assertRaises(exception.WatchRuleNotFound,
                          watchrule.WatchRule.load,
                          context=self.ctx,
                          watch_name="testwatch_destroy")
Ejemplo n.º 29
0
    def test_state_set(self):
        rule = {
            'EvaluationPeriods': '1',
            'MetricName': 'test_metric',
            'AlarmActions': ['DummyAction'],
            'Period': '300',
            'Statistic': 'Maximum',
            'ComparisonOperator': 'GreaterThanOrEqualToThreshold',
            'Threshold': '30'
        }

        last = timeutils.utcnow()
        watcher = watchrule.WatchRule(context=self.ctx,
                                      watch_name="testwatch_set_state",
                                      rule=rule,
                                      watch_data=[],
                                      stack_id=self.stack_id,
                                      last_evaluated=last)

        watcher.state_set(watcher.SUSPENDED)
        self.assertEqual(watcher.SUSPENDED, watcher.state)

        check = watchrule.WatchRule.load(context=self.ctx,
                                         watch_name="testwatch_set_state")
        self.assertEqual(watchrule.WatchRule.SUSPENDED, check.state)
Ejemplo n.º 30
0
    def test_load(self):
        # Insert two dummy watch rules into the DB
        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'
        }
        self.wr = []
        self.wr.append(
            watchrule.WatchRule(context=self.ctx,
                                watch_name='HttpFailureAlarm',
                                rule=rule,
                                watch_data=[],
                                stack_id=self.stack_id,
                                state='NORMAL'))
        self.wr[0].store()

        self.wr.append(
            watchrule.WatchRule(context=self.ctx,
                                watch_name='AnotherWatch',
                                rule=rule,
                                watch_data=[],
                                stack_id=self.stack_id,
                                state='NORMAL'))
        self.wr[1].store()

        # 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.assertIsInstance(wr, watchrule.WatchRule)
            self.assertEqual(wn, wr.name)
            self.assertEqual('NORMAL', wr.state)
            self.assertEqual(rule, wr.rule)
            self.assertEqual(datetime.timedelta(seconds=int(rule['Period'])),
                             wr.timeperiod)