diff options
author | Yikun Jiang <yikunkero@gmail.com> | 2018-06-29 21:10:11 +0800 |
---|---|---|
committer | Yikun Jiang <yikunkero@gmail.com> | 2018-07-12 09:30:41 +0800 |
commit | 8fa70e5dfc73e52d52c94ec2781ff185fdcca481 (patch) | |
tree | 914bc7776061d2d9f24497e4de836a845c93b50b /nova/tests/unit/objects/test_instance_group.py | |
parent | afc7650e64753ab7687ae2c4f2714d4bb78a4e5a (diff) | |
download | nova-8fa70e5dfc73e52d52c94ec2781ff185fdcca481.tar.gz |
Add policy to InstanceGroup object
The change items in this patch as below:
1. Add policy and rules to InstanceGroup, deprecate
the policies field.
2. Also, make _from_db_object and _create_in_db method to
support new "policy" field.
The internal usage of InstanceGroup.policies is converted to
use the new model in the REST API and RequestSpec compat code.
Related to blueprint complex-anti-affinity-policies
Change-Id: Ib33719a4b9599d86848c618a6e142c71ece79ca5
Diffstat (limited to 'nova/tests/unit/objects/test_instance_group.py')
-rw-r--r-- | nova/tests/unit/objects/test_instance_group.py | 64 |
1 files changed, 54 insertions, 10 deletions
diff --git a/nova/tests/unit/objects/test_instance_group.py b/nova/tests/unit/objects/test_instance_group.py index f8fa7f4901..41d0b9792a 100644 --- a/nova/tests/unit/objects/test_instance_group.py +++ b/nova/tests/unit/objects/test_instance_group.py @@ -15,6 +15,7 @@ import copy import mock +from oslo_serialization import jsonutils from oslo_utils import timeutils from oslo_versionedobjects import exception as ovo_exc @@ -30,6 +31,10 @@ _TS_NOW = timeutils.utcnow(with_timezone=True) # in process we lose microsecond resolution. _TS_NOW = _TS_NOW.replace(microsecond=0) _DB_UUID = uuids.fake +_INST_GROUP_POLICY_DB = { + 'policy': 'policy1', + 'rules': jsonutils.dumps({'max_server_per_host': '2'}), +} _INST_GROUP_DB = { 'id': 1, 'uuid': _DB_UUID, @@ -37,11 +42,18 @@ _INST_GROUP_DB = { 'project_id': 'fake_project', 'name': 'fake_name', # a group can only have 1 policy associated with it - 'policies': ['policy1'], + 'policy': _INST_GROUP_POLICY_DB, + '_policies': [_INST_GROUP_POLICY_DB], 'members': ['instance_id1', 'instance_id2'], 'created_at': _TS_NOW, - 'updated_at': _TS_NOW + 'updated_at': _TS_NOW, } +_INST_GROUP_OBJ_VALS = dict( + {k: v for k, v in + _INST_GROUP_DB.items() + if k not in ('policy', '_policies')}, + policy=_INST_GROUP_POLICY_DB['policy'], + rules=jsonutils.loads(_INST_GROUP_POLICY_DB['rules'])) class _TestInstanceGroupObject(object): @@ -53,11 +65,21 @@ class _TestInstanceGroupObject(object): _DB_UUID) mock_api_get.assert_called_once_with(self.context, _DB_UUID) self.assertEqual(_INST_GROUP_DB['members'], obj.members) - self.assertEqual(_INST_GROUP_DB['policies'], obj.policies) + self.assertEqual([_INST_GROUP_POLICY_DB['policy']], obj.policies) self.assertEqual(_DB_UUID, obj.uuid) self.assertEqual(_INST_GROUP_DB['project_id'], obj.project_id) self.assertEqual(_INST_GROUP_DB['user_id'], obj.user_id) self.assertEqual(_INST_GROUP_DB['name'], obj.name) + self.assertEqual(_INST_GROUP_POLICY_DB['policy'], obj.policy) + self.assertEqual({'max_server_per_host': 2}, obj.rules) + + def test_rules_helper(self): + obj = objects.InstanceGroup() + self.assertEqual({}, obj.rules) + self.assertNotIn('_rules', obj) + obj._rules = {} + self.assertEqual({}, obj.rules) + self.assertIn('_rules', obj) @mock.patch('nova.objects.InstanceGroup._get_from_db_by_instance', return_value=_INST_GROUP_DB) @@ -87,9 +109,10 @@ class _TestInstanceGroupObject(object): changed_group['name'] = 'new_name' db_group = copy.deepcopy(_INST_GROUP_DB) mock_db_get.return_value = db_group - obj = objects.InstanceGroup(self.context, **_INST_GROUP_DB) + obj = objects.InstanceGroup(self.context, **_INST_GROUP_OBJ_VALS) self.assertEqual(obj.name, 'fake_name') obj.obj_reset_changes() + self.assertEqual(set([]), obj.obj_what_changed()) obj.name = 'new_name' obj.members = ['instance_id1'] # Remove member 2 obj.save() @@ -105,7 +128,7 @@ class _TestInstanceGroupObject(object): @mock.patch('nova.objects.InstanceGroup._get_from_db_by_uuid') def test_save_without_hosts(self, mock_db_get, mock_notify): mock_db_get.return_value = _INST_GROUP_DB - obj = objects.InstanceGroup(self.context, **_INST_GROUP_DB) + obj = objects.InstanceGroup(self.context, **_INST_GROUP_OBJ_VALS) obj.obj_reset_changes() obj.hosts = ['fake-host1'] self.assertRaises(exception.InstanceGroupSaveException, @@ -139,7 +162,7 @@ class _TestInstanceGroupObject(object): obj.user_id = _INST_GROUP_DB['user_id'] obj.project_id = _INST_GROUP_DB['project_id'] obj.members = _INST_GROUP_DB['members'] - obj.policies = _INST_GROUP_DB['policies'] + obj.policies = [_INST_GROUP_DB['policy']['policy']] obj.updated_at = _TS_NOW obj.created_at = _TS_NOW obj.create() @@ -153,7 +176,9 @@ class _TestInstanceGroupObject(object): 'updated_at': _TS_NOW, }, members=_INST_GROUP_DB['members'], - policies=_INST_GROUP_DB['policies']) + policies=[_INST_GROUP_DB['policy']['policy']], + policy=None, + rules=None) mock_notify.assert_called_once_with( self.context, "create", {'uuid': _DB_UUID, @@ -163,7 +188,7 @@ class _TestInstanceGroupObject(object): 'created_at': _TS_NOW, 'updated_at': _TS_NOW, 'members': _INST_GROUP_DB['members'], - 'policies': _INST_GROUP_DB['policies'], + 'policies': [_INST_GROUP_DB['policy']['policy']], 'server_group_id': _DB_UUID}) def _group_matcher(group): @@ -175,7 +200,7 @@ class _TestInstanceGroupObject(object): group.created_at == _TS_NOW and group.updated_at == _TS_NOW and group.members == _INST_GROUP_DB['members'] and - group.policies == _INST_GROUP_DB['policies'] and + group.policies == [_INST_GROUP_DB['policy']['policy']] and group.id == 1) group_matcher = test_utils.CustomMockCallMatcher(_group_matcher) @@ -271,12 +296,31 @@ class _TestInstanceGroupObject(object): filters=expected_filters) def test_obj_make_compatible(self): - obj = objects.InstanceGroup(self.context, **_INST_GROUP_DB) + obj = objects.InstanceGroup(self.context, **_INST_GROUP_OBJ_VALS) obj_primitive = obj.obj_to_primitive() self.assertNotIn('metadetails', obj_primitive) obj.obj_make_compatible(obj_primitive, '1.6') self.assertEqual({}, obj_primitive['metadetails']) + def test_obj_make_compatible_pre_1_11(self): + none_policy_group = copy.deepcopy(_INST_GROUP_DB) + none_policy_group['policy'] = None + dbs = [_INST_GROUP_DB, none_policy_group] + data = lambda x: x['nova_object.data'] + for db in dbs: + ig = objects.InstanceGroup() + obj = ig._from_db_object(self.context, ig, db) + # Latest version obj has policy and policies + obj_primitive = obj.obj_to_primitive() + self.assertIn('policy', data(obj_primitive)) + self.assertIn('policies', data(obj_primitive)) + # Before 1.10, only has polices which is the list of policy name + obj_primitive = obj.obj_to_primitive('1.10') + self.assertNotIn('policy', data(obj_primitive)) + self.assertIn('policies', data(obj_primitive)) + self.assertEqual([db['policy']['policy']] if db['policy'] else [], + data(obj_primitive)['policies']) + @mock.patch.object(objects.InstanceList, 'get_by_filters') def test_load_hosts(self, mock_get_by_filt): mock_get_by_filt.return_value = [objects.Instance(host='host1'), |