def test_create_with_single_tag(self): domain_def = policy.DomainDef('eukarya') group_def = policy.GroupDef('eukarya', 'dogs', conditions=policy.Condition('spaniel')) self.policy_api.create_with_parent(domain_def, group_def) data = domain_def.get_obj_dict() data['groups'] = [group_def.get_obj_dict()] # validate body structure and defaults expected_condition = { 'value': 'spaniel', 'operator': 'EQUALS', 'member_type': 'LogicalPort', 'resource_type': 'Condition', 'key': 'Tag' } expected_group = { 'id': 'dogs', 'display_name': None, 'description': None, 'expression': [expected_condition] } expected_data = { 'id': 'eukarya', 'display_name': None, 'description': None, 'groups': [expected_group] } self.assert_json_call('PATCH', self.client, 'infra/domains/eukarya', data=expected_data)
def create_or_overwrite( self, name, domain_id, group_id=None, description=None, cond_val=None, cond_key=policy_constants.CONDITION_KEY_TAG, cond_op=policy_constants.CONDITION_OP_EQUALS, cond_member_type=policy_constants.CONDITION_MEMBER_PORT, tenant=policy_constants.POLICY_INFRA_TENANT): """Create a group with/without a condition. Empty condition value will result a group with no condition. """ group_id = self._init_obj_uuid(group_id) # Prepare the condition if cond_val is not None: condition = policy_defs.Condition(value=cond_val, key=cond_key, operator=cond_op, member_type=cond_member_type) conditions = [condition] else: conditions = [] group_def = policy_defs.GroupDef(domain_id=domain_id, group_id=group_id, name=name, description=description, conditions=conditions, tenant=tenant) return self.policy_api.create_or_update(group_def)
def test_create(self): group_def = policy.GroupDef('eukarya', 'cats', 'felis catus') self.policy_api.create_or_update(group_def) self.assert_json_call('PATCH', self.client, 'infra/domains/eukarya/groups/cats', data=group_def.get_obj_dict())
def test_remove_condition(self): domain_id = '111' id = '222' old_cond = { 'resource_type': 'Condition', 'member_type': policy_constants.CONDITION_MEMBER_PORT, 'key': policy_constants.CONDITION_KEY_TAG, 'value': 'abc', 'operator': policy_constants.CONDITION_OP_EQUALS } with mock.patch.object( self.policy_api, "get", return_value={'expression': [old_cond]}) as get_call,\ mock.patch.object( self.policy_api, "create_or_update") as update_call: self.resourceApi.update_condition(domain_id, id, cond_val=None, tenant=TEST_TENANT) expected_def = policy_defs.GroupDef(domain_id=domain_id, group_id=id, tenant=TEST_TENANT) expected_dict = {'expression': []} self.assert_called_with_def(get_call, expected_def) self.assert_called_with_def_and_dict(update_call, expected_def, expected_dict)
def get_realized_state(self, domain_id, group_id, ep_id, tenant=policy_constants.POLICY_INFRA_TENANT): group_def = policy_defs.GroupDef(domain_id=domain_id, group_id=group_id, tenant=tenant) path = group_def.get_realized_state_path(ep_id) return self._get_realized_state(path)
def test_create_with_condition(self): domain_id = '111' name = 'g1' description = 'desc' cond_val = '123' cond_op = policy_constants.CONDITION_OP_EQUALS cond_member_type = policy_constants.CONDITION_MEMBER_VM cond_key = policy_constants.CONDITION_KEY_TAG with mock.patch.object(self.policy_api, "create_or_update") as api_call: self.resourceApi.create_or_overwrite( name, domain_id, description=description, cond_val=cond_val, cond_op=cond_op, cond_member_type=cond_member_type, cond_key=cond_key, tenant=TEST_TENANT) exp_cond = policy_defs.Condition(value=cond_val, key=cond_key, operator=cond_op, member_type=cond_member_type) expected_def = policy_defs.GroupDef(domain_id=domain_id, group_id=mock.ANY, name=name, description=description, conditions=[exp_cond], tenant=TEST_TENANT) self.assert_called_with_def(api_call, expected_def)
def update_condition( self, domain_id, group_id, cond_val=None, cond_key=policy_constants.CONDITION_KEY_TAG, cond_op=policy_constants.CONDITION_OP_EQUALS, cond_member_type=policy_constants.CONDITION_MEMBER_PORT, tenant=policy_constants.POLICY_INFRA_TENANT): """Update/Remove the condition of a group. Empty condition value will result a group with no condition. """ group_def = policy_defs.GroupDef(domain_id=domain_id, group_id=group_id, tenant=tenant) # Prepare the condition if cond_val is not None: condition = policy_defs.Condition(value=cond_val, key=cond_key, operator=cond_op, member_type=cond_member_type) conditions = [condition] else: conditions = [] # Get the current data, and update it with the new values # We need to do that here because of the conditions data group = self.get(domain_id, group_id, tenant=tenant) group_def.update_attributes_in_body(body=group, conditions=conditions) # update the backend return self.policy_api.create_or_update(group_def)
def test_list(self): domain_id = '111' with mock.patch.object(self.policy_api, "list") as api_call: self.resourceApi.list(domain_id, tenant=TEST_TENANT) expected_def = policy_defs.GroupDef(domain_id=domain_id, tenant=TEST_TENANT) self.assert_called_with_def(api_call, expected_def)
def test_delete(self): domain_id = '111' id = '222' with mock.patch.object(self.policy_api, "delete") as api_call: self.resourceApi.delete(domain_id, id, tenant=TEST_TENANT) expected_def = policy_defs.GroupDef(domain_id=domain_id, group_id=id, tenant=TEST_TENANT) self.assert_called_with_def(api_call, expected_def)
def test_create_with_domain(self): domain_def = policy.DomainDef('eukarya', 'eukarya', 'dude with cell membranes') group_def = policy.GroupDef('eukarya', 'cats', 'Ailuropoda melanoleuca') self.policy_api.create_with_parent(domain_def, group_def) data = domain_def.get_obj_dict() data['groups'] = [group_def.get_obj_dict()] self.assert_json_call('PATCH', self.client, 'infra/domains/eukarya', data=data)
def update(self, domain_id, group_id, name=None, description=None, tenant=policy_constants.POLICY_INFRA_TENANT): """Update the general data of the group. Without changing the conditions """ group_def = policy_defs.GroupDef(domain_id=domain_id, group_id=group_id, tenant=tenant) group_def.update_attributes_in_body(name=name, description=description) # update the backend return self.policy_api.create_or_update(group_def)
def test_get_by_name(self): domain_id = '111' name = 'g1' with mock.patch.object( self.policy_api, "list", return_value={'results': [{ 'display_name': name }]}) as api_call: obj = self.resourceApi.get_by_name(domain_id, name, tenant=TEST_TENANT) self.assertIsNotNone(obj) expected_def = policy_defs.GroupDef(domain_id, tenant=TEST_TENANT) self.assert_called_with_def(api_call, expected_def)
def test_create_with_multi_tag(self): domain_def = policy.DomainDef('eukarya') pines = policy.Condition( 'pine', operator=policy_constants.CONDITION_OP_CONTAINS) maples = policy.Condition( 'maple', operator=policy_constants.CONDITION_OP_STARTS_WITH) group_def = policy.GroupDef('eukarya', 'trees', conditions=[pines, maples]) self.policy_api.create_with_parent(domain_def, group_def) data = domain_def.get_obj_dict() data['groups'] = [group_def.get_obj_dict()] self.assert_json_call('PATCH', self.client, 'infra/domains/eukarya', data=data)
def test_create_without_id(self): domain_id = '111' name = 'g1' description = 'desc' with mock.patch.object(self.policy_api, "create_or_update") as api_call: self.resourceApi.create_or_overwrite(name, domain_id, description=description, tenant=TEST_TENANT) expected_def = policy_defs.GroupDef(domain_id=domain_id, group_id=mock.ANY, name=name, description=description, conditions=[], tenant=TEST_TENANT) self.assert_called_with_def(api_call, expected_def)
def test_update(self): domain_id = '111' id = '222' name = 'new name' description = 'new desc' with mock.patch.object(self.policy_api, "create_or_update") as update_call: self.resourceApi.update(domain_id, id, name=name, description=description, tenant=TEST_TENANT) expected_def = policy_defs.GroupDef(domain_id=domain_id, group_id=id, tenant=TEST_TENANT) expected_dict = {'display_name': name, 'description': description} self.assert_called_with_def_and_dict(update_call, expected_def, expected_dict)
def get(self, domain_id, group_id, tenant=policy_constants.POLICY_INFRA_TENANT): group_def = policy_defs.GroupDef(domain_id=domain_id, group_id=group_id, tenant=tenant) return self.policy_api.get(group_def)
def delete(self, domain_id, group_id, tenant=policy_constants.POLICY_INFRA_TENANT): group_def = policy_defs.GroupDef(domain_id=domain_id, group_id=group_id, tenant=tenant) self.policy_api.delete(group_def)
def test_delete(self): group_def = policy.GroupDef(domain_id='eukarya', group_id='giraffe') self.policy_api.delete(group_def) self.assert_json_call('DELETE', self.client, 'infra/domains/eukarya/groups/giraffe')
def list(self, domain_id, tenant=policy_constants.POLICY_INFRA_TENANT): """List all the groups of a specific domain.""" group_def = policy_defs.GroupDef(domain_id=domain_id, tenant=tenant) return self.policy_api.list(group_def)['results']