예제 #1
0
 def test_duplicate_uuid(self):
     rules.create([], self.actions_json, uuid=self.uuid)
     self.assertRaisesRegex(utils.Error,
                            'already exists',
                            rules.create, [],
                            self.actions_json,
                            uuid=self.uuid)
예제 #2
0
    def test_apply_data_format_value_primitives(self, mock_ext_mgr):
        self.data.update({'outer': {'inner': False}})

        self.rule = rules.create(actions_json=[{
            'action': 'set-attribute',
            'path': '/driver_info/foo',
            'value': {
                42: {
                    True: [3.14, 'foo', '{data[outer][inner]}']
                }
            }
        }],
                                 conditions_json=self.conditions_json)
        mock_ext_mgr.return_value.__getitem__.return_value = self.ext_mock

        self.rule.apply_actions(self.node_info, data=self.data)

        self.act_mock.apply.assert_called_once_with(
            self.node_info,
            {
                # String-formatted values will be coerced to be strings.
                'value': {
                    42: {
                        True: [3.14, 'foo', 'False']
                    }
                },
                'path': '/driver_info/foo'
            })
예제 #3
0
    def test_apply_data_format_value_dict(self, mock_ext_mgr):
        self.data.update({
            'val_outer': {
                'val_inner': 17
            },
            'key_outer': {
                'key_inner': 'baz'
            }
        })

        self.rule = rules.create(actions_json=[{
            'action': 'set-attribute',
            'path': '/driver_info/foo',
            'value': {
                '{data[key_outer][key_inner]}': '{data[val_outer][val_inner]}'
            }
        }],
                                 conditions_json=self.conditions_json)
        mock_ext_mgr.return_value.__getitem__.return_value = self.ext_mock

        self.rule.apply_actions(self.node_info, data=self.data)

        self.act_mock.apply.assert_called_once_with(
            self.node_info,
            {
                # String-formatted values will be coerced to be strings.
                'value': {
                    'baz': '17'
                },
                'path': '/driver_info/foo'
            })
예제 #4
0
def api_rules():
    if flask.request.method == 'GET':
        res = [rule_repr(rule, short=True) for rule in rules.get_all()]
        return flask.jsonify(rules=res)
    elif flask.request.method == 'DELETE':
        rules.delete_all()
        return _generate_empty_response(204)
    else:
        body = flask.request.get_json(force=True)
        if body.get('uuid') and not uuidutils.is_uuid_like(body['uuid']):
            raise utils.Error(_('Invalid UUID value'), code=400)
        if body.get('scope') and len(body.get('scope')) > 255:
            raise utils.Error(_(
                "Invalid scope: the length of the scope should be within "
                "255 characters"),
                              code=400)

        rule = rules.create(conditions_json=body.get('conditions', []),
                            actions_json=body.get('actions', []),
                            uuid=body.get('uuid'),
                            description=body.get('description'),
                            scope=body.get('scope'))

        response_code = (200 if _get_version() < (1, 6) else 201)
        return flask.make_response(flask.jsonify(rule_repr(rule, short=False)),
                                   response_code)
예제 #5
0
    def setUp(self):
        super(TestCheckConditions, self).setUp()

        self.rule = rules.create(conditions_json=self.conditions_json,
                                 actions_json=self.actions_json)
        self.cond_mock = mock.Mock(spec=plugins_base.RuleConditionPlugin)
        self.cond_mock.ALLOW_NONE = False
        self.ext_mock = mock.Mock(spec=['obj'], obj=self.cond_mock)
예제 #6
0
    def setUp(self):
        super(TestApplyActions, self).setUp()
        self.actions_json.append({'action': 'example'})

        self.rule = rules.create(conditions_json=self.conditions_json,
                                 actions_json=self.actions_json)
        self.act_mock = mock.Mock(spec=plugins_base.RuleActionPlugin)
        self.ext_mock = mock.Mock(spec=['obj'], obj=self.act_mock)
예제 #7
0
    def setUp(self):
        super(TestCheckConditions, self).setUp()

        self.rule = rules.create(conditions_json=self.conditions_json,
                                 actions_json=self.actions_json)
        self.cond_mock = mock.Mock(spec=plugins_base.RuleConditionPlugin)
        self.cond_mock.ALLOW_NONE = False
        self.ext_mock = mock.Mock(spec=['obj'], obj=self.cond_mock)
예제 #8
0
    def setUp(self):
        super(TestApplyActions, self).setUp()
        self.actions_json.append({'action': 'example'})

        self.rule = rules.create(conditions_json=self.conditions_json,
                                 actions_json=self.actions_json)
        self.act_mock = mock.Mock(spec=plugins_base.RuleActionPlugin)
        self.ext_mock = mock.Mock(spec=['obj'], obj=self.act_mock)
예제 #9
0
    def test_with_conditions(self):
        rule = rules.create(self.conditions_json, self.actions_json)
        rule_json = rule.as_dict()

        self.assertTrue(rule_json.pop('uuid'))
        self.assertEqual({'description': None,
                          'conditions': self.conditions_json,
                          'actions': self.actions_json},
                         rule_json)
예제 #10
0
    def test_only_actions(self):
        rule = rules.create([], self.actions_json)
        rule_json = rule.as_dict()

        self.assertTrue(rule_json.pop('uuid'))
        self.assertEqual(
            {
                'description': None,
                'conditions': [],
                'actions': self.actions_json
            }, rule_json)
예제 #11
0
    def test_apply_data_non_format_value(self, mock_ext_mgr):
        self.rule = rules.create(actions_json=[{
            'action': 'set-attribute',
            'path': '/driver_info/ipmi_address',
            'value': 1
        }],
                                 conditions_json=self.conditions_json)
        mock_ext_mgr.return_value.__getitem__.return_value = self.ext_mock

        self.rule.apply_actions(self.node_info, data=self.data)

        self.assertEqual(1, self.act_mock.apply.call_count)
예제 #12
0
    def test_apply_data_format_value_fail(self, mock_ext_mgr):
        self.rule = rules.create(
            actions_json=[
                {'action': 'set-attribute',
                 'path': '/driver_info/ipmi_address',
                 'value': '{data[inventory][bmc_address]}'}],
            conditions_json=self.conditions_json
        )
        mock_ext_mgr.return_value.__getitem__.return_value = self.ext_mock

        self.assertRaises(utils.Error, self.rule.apply_actions,
                          self.node_info, data=self.data)
예제 #13
0
    def test_scope(self):
        rule = rules.create([], self.actions_json, scope=self.scope)
        rule_json = rule.as_dict()

        self.assertTrue(rule_json.pop('uuid'))
        self.assertEqual(
            {
                'description': None,
                'conditions': [],
                'actions': self.actions_json,
                'scope': self.scope
            }, rule_json)
예제 #14
0
    def test_apply_data_non_format_value(self, mock_ext_mgr):
        self.rule = rules.create(actions_json=[
            {'action': 'set-attribute',
             'path': '/driver_info/ipmi_address',
             'value': 1}],
            conditions_json=self.conditions_json
        )
        mock_ext_mgr.return_value.__getitem__.return_value = self.ext_mock

        self.rule.apply_actions(self.node_info, data=self.data)

        self.assertEqual(1, self.act_mock.apply.call_count)
        self.assertFalse(self.act_mock.rollback.called)
예제 #15
0
 def test_default(self):
     rule = rules.create(conditions_json=self.conditions_json,
                         actions_json=self.actions_json)
     data_set = [
         (['1.1.1.1', '1.2.3.4', '1.3.2.2'], True),
         (['1.2.3.4'], True),
         (['1.1.1.1', '1.3.2.2'], False),
         (['1.2.3.4', '1.3.2.2'], True),
     ]
     for ips, result in data_set:
         data = self._build_data(ips)
         self.assertIs(result, rule.check_conditions(self.node_info, data),
                       data)
예제 #16
0
 def test_default(self):
     rule = rules.create(conditions_json=self.conditions_json,
                         actions_json=self.actions_json)
     data_set = [
         (['1.1.1.1', '1.2.3.4', '1.3.2.2'], True),
         (['1.2.3.4'], True),
         (['1.1.1.1', '1.3.2.2'], False),
         (['1.2.3.4', '1.3.2.2'], True),
     ]
     for ips, result in data_set:
         data = self._build_data(ips)
         self.assertIs(result, rule.check_conditions(self.node_info, data),
                       data)
예제 #17
0
    def test_conditions_data_path(self):
        self.data_set = [
            ([{'op': 'eq', 'field': 'data://memory_mb', 'value': 1024}],
             True),
            ([{'op': 'gt', 'field': 'data://local_gb', 'value': 42}],
             False)
        ]

        for condition, res in self.data_set:
            rule = rules.create(conditions_json=condition,
                                actions_json=self.actions_json)
            self.assertIs(res,
                          rule.check_conditions(self.node_info, self.data),
                          self.data)
예제 #18
0
    def test_conditions_node_path(self):
        self.node_set = [
            ([{'op': 'eq', 'field': 'node://driver_info.ipmi_address',
               'value': self.bmc_address}],
             True),
            ([{'op': 'eq', 'field': 'node://driver', 'value': 'fake'}],
             False)
        ]

        for condition, res in self.node_set:
            rule = rules.create(conditions_json=condition,
                                actions_json=self.actions_json)
            self.assertIs(res,
                          rule.check_conditions(self.node_info, self.data))
예제 #19
0
    def test_apply_data_format_value_nested_fail(self, mock_ext_mgr):
        self.data.update({'outer': {'inner': 'baz'}})
        self.rule = rules.create(actions_json=[{
            'action':
            'set-attribute',
            'path':
            '/driver_info/foo',
            'value': ['basic', ['{data[outer][nonexistent]}']]
        }],
                                 conditions_json=self.conditions_json)
        mock_ext_mgr.return_value.__getitem__.return_value = self.ext_mock

        self.assertRaises(utils.Error,
                          self.rule.apply_actions,
                          self.node_info,
                          data=self.data)
예제 #20
0
    def test_invert(self, mock_ext_mgr):
        self.conditions_json = [
            {'op': 'eq', 'field': 'memory_mb', 'value': 42,
             'invert': True},
        ]
        self.rule = rules.create(conditions_json=self.conditions_json,
                                 actions_json=self.actions_json)

        mock_ext_mgr.return_value.__getitem__.return_value = self.ext_mock
        self.cond_mock.check.return_value = False

        res = self.rule.check_conditions(self.node_info, self.data)

        self.cond_mock.check.assert_called_once_with(self.node_info, 1024,
                                                     {'value': 42})
        self.assertTrue(res)
예제 #21
0
    def test_create_action_none_value(self):
        self.actions_json = [{
            'action': 'set-attribute',
            'path': '/properties/cpus',
            'value': None
        }]
        rule = rules.create([], self.actions_json)
        rule_json = rule.as_dict()

        self.assertTrue(rule_json.pop('uuid'))
        self.assertEqual(
            {
                'description': None,
                'conditions': [],
                'actions': self.actions_json
            }, rule_json)
예제 #22
0
    def test_apply_data_format_value_fail(self, mock_ext_mgr):
        self.rule = rules.create(actions_json=[{
            'action':
            'set-attribute',
            'path':
            '/driver_info/ipmi_address',
            'value':
            '{data[inventory][bmc_address]}'
        }],
                                 conditions_json=self.conditions_json)
        mock_ext_mgr.return_value.__getitem__.return_value = self.ext_mock

        self.assertRaises(utils.Error,
                          self.rule.apply_actions,
                          self.node_info,
                          data=self.data)
예제 #23
0
    def test_conditions_data_path(self):
        self.data_set = [([{
            'op': 'eq',
            'field': 'data://memory_mb',
            'value': 1024
        }], True),
                         ([{
                             'op': 'gt',
                             'field': 'data://local_gb',
                             'value': 42
                         }], False)]

        for condition, res in self.data_set:
            rule = rules.create(conditions_json=condition,
                                actions_json=self.actions_json)
            self.assertIs(res, rule.check_conditions(self.node_info,
                                                     self.data), self.data)
예제 #24
0
    def test_conditions_node_path(self):
        self.node_set = [([{
            'op': 'eq',
            'field': 'node://driver_info.ipmi_address',
            'value': self.bmc_address
        }], True),
                         ([{
                             'op': 'eq',
                             'field': 'node://driver',
                             'value': 'fake'
                         }], False)]

        for condition, res in self.node_set:
            rule = rules.create(conditions_json=condition,
                                actions_json=self.actions_json)
            self.assertIs(res, rule.check_conditions(self.node_info,
                                                     self.data))
예제 #25
0
def api_rules():
    utils.check_auth(flask.request)

    if flask.request.method == 'GET':
        res = [rule_repr(rule, short=True) for rule in rules.get_all()]
        return flask.jsonify(rules=res)
    elif flask.request.method == 'DELETE':
        rules.delete_all()
        return '', 204
    else:
        body = flask.request.get_json(force=True)
        if body.get('uuid') and not uuidutils.is_uuid_like(body['uuid']):
            raise utils.Error(_('Invalid UUID value'), code=400)

        rule = rules.create(conditions_json=body.get('conditions', []),
                            actions_json=body.get('actions', []),
                            uuid=body.get('uuid'),
                            description=body.get('description'))
        return flask.jsonify(rule_repr(rule, short=False))
예제 #26
0
    def test_apply_data_format_value_list(self, mock_ext_mgr):
        self.data.update({'outer': {'inner': 'baz'}})

        self.rule = rules.create(actions_json=[{
            'action':
            'set-attribute',
            'path':
            '/driver_info/foo',
            'value': ['basic', ['{data[outer][inner]}']]
        }],
                                 conditions_json=self.conditions_json)
        mock_ext_mgr.return_value.__getitem__.return_value = self.ext_mock

        self.rule.apply_actions(self.node_info, data=self.data)

        self.act_mock.apply.assert_called_once_with(self.node_info, {
            'value': ['basic', ['baz']],
            'path': '/driver_info/foo'
        })
예제 #27
0
def api_rules():
    utils.check_auth(flask.request)

    if flask.request.method == 'GET':
        res = [rule_repr(rule, short=True) for rule in rules.get_all()]
        return flask.jsonify(rules=res)
    elif flask.request.method == 'DELETE':
        rules.delete_all()
        return '', 204
    else:
        body = flask.request.get_json(force=True)
        if body.get('uuid') and not uuidutils.is_uuid_like(body['uuid']):
            raise utils.Error(_('Invalid UUID value'), code=400)

        rule = rules.create(conditions_json=body.get('conditions', []),
                            actions_json=body.get('actions', []),
                            uuid=body.get('uuid'),
                            description=body.get('description'))
        return flask.jsonify(rule_repr(rule, short=False))
예제 #28
0
    def test_with_conditions(self):
        self.conditions_json.extend([
            # multiple present&default, invert absent
            {
                'op': 'eq',
                'field': 'local_gb',
                'value': 60,
                'multiple': 'any'
            },
            # multiple absent, invert present&default
            {
                'op': 'eq',
                'field': 'local_gb',
                'value': 60,
                'invert': False
            },
            # multiple&invert present&non-default
            {
                'op': 'eq',
                'field': 'memory_mb',
                'value': 1024,
                'multiple': 'all',
                'invert': True
            },
        ])
        rule = rules.create(self.conditions_json, self.actions_json)
        rule_json = rule.as_dict()

        self.assertTrue(rule_json.pop('uuid'))
        self.assertEqual(
            {
                'description':
                None,
                'conditions': [
                    BaseTest.condition_defaults(cond)
                    for cond in self.conditions_json
                ],
                'actions':
                self.actions_json,
                'scope':
                None
            }, rule_json)
예제 #29
0
    def test_invert(self, mock_ext_mgr):
        self.conditions_json = [
            {
                'op': 'eq',
                'field': 'memory_mb',
                'value': 42,
                'invert': True
            },
        ]
        self.rule = rules.create(conditions_json=self.conditions_json,
                                 actions_json=self.actions_json)

        mock_ext_mgr.return_value.__getitem__.return_value = self.ext_mock
        self.cond_mock.check.return_value = False

        res = self.rule.check_conditions(self.node_info, self.data)

        self.cond_mock.check.assert_called_once_with(self.node_info, 1024,
                                                     {'value': 42})
        self.assertTrue(res)
예제 #30
0
 def test_get_all(self):
     rules.create(self.conditions_json, self.actions_json, uuid='uuid2')
     self.assertEqual([self.uuid, 'uuid2'],
                      [r.as_dict()['uuid'] for r in rules.get_all()])
예제 #31
0
 def test_get_all(self):
     uuid2 = uuidutils.generate_uuid()
     rules.create(self.conditions_json, self.actions_json, uuid=uuid2)
     self.assertEqual({self.uuid, uuid2},
                      {r.as_dict()['uuid'] for r in rules.get_all()})
예제 #32
0
 def setUp(self):
     super(TestDeleteRule, self).setUp()
     self.uuid2 = uuidutils.generate_uuid()
     rules.create(self.conditions_json, self.actions_json, uuid=self.uuid)
     rules.create(self.conditions_json, self.actions_json, uuid=self.uuid2)
예제 #33
0
 def setUp(self):
     super(TestGetRule, self).setUp()
     rules.create(self.conditions_json, self.actions_json, uuid=self.uuid)
예제 #34
0
 def test_get_all(self):
     rules.create(self.conditions_json, self.actions_json, uuid='uuid2')
     self.assertEqual([self.uuid, 'uuid2'],
                      [r.as_dict()['uuid'] for r in rules.get_all()])
예제 #35
0
 def setUp(self):
     super(TestDeleteRule, self).setUp()
     self.uuid2 = self.uuid + '-2'
     rules.create(self.conditions_json, self.actions_json, uuid=self.uuid)
     rules.create(self.conditions_json, self.actions_json, uuid=self.uuid2)
예제 #36
0
 def test_duplicate_uuid(self):
     rules.create([], self.actions_json, uuid=self.uuid)
     self.assertRaisesRegexp(utils.Error, 'already exists',
                             rules.create, [], self.actions_json,
                             uuid=self.uuid)
예제 #37
0
 def setUp(self):
     super(TestDeleteRule, self).setUp()
     self.uuid2 = self.uuid + '-2'
     rules.create(self.conditions_json, self.actions_json, uuid=self.uuid)
     rules.create(self.conditions_json, self.actions_json, uuid=self.uuid2)
예제 #38
0
 def setUp(self):
     super(TestGetRule, self).setUp()
     rules.create(self.conditions_json, self.actions_json, uuid=self.uuid)
예제 #39
0
 def test_get_all(self):
     uuid2 = uuidutils.generate_uuid()
     rules.create(self.conditions_json, self.actions_json, uuid=uuid2)
     self.assertEqual({self.uuid, uuid2},
                      {r.as_dict()['uuid']
                       for r in rules.get_all()})
예제 #40
0
 def setUp(self):
     super(TestDeleteRule, self).setUp()
     self.uuid2 = uuidutils.generate_uuid()
     rules.create(self.conditions_json, self.actions_json, uuid=self.uuid)
     rules.create(self.conditions_json, self.actions_json, uuid=self.uuid2)