def test_create_for_failure(self): """ Test create for failure! """ source = mox.MockObject(core_model.Resource) source.attributes = {'occi.core.id': 'bar'} target = mox.MockObject(core_model.Resource) target.identifier = '/network/admin' link = core_model.Link('foo', None, [], source, target) self.mox.ReplayAll() self.assertRaises(AttributeError, self.backend.create, link, self.sec_obj) self.mox.VerifyAll() # should have pool name in attribute... target.identifier = '/network/public' link = core_model.Link('foo', None, [os_addon.OS_NET_LINK], source, target) self.mox.ReplayAll() self.assertRaises(AttributeError, self.backend.create, link, self.sec_obj) self.mox.VerifyAll()
def test_populate_dictionary_with_resource_containing_links(self): # Test entities - creating a resource with two links res_tar_1_id = "/an-id/res_tar_1" res_src_1_id = "/an-id/res_src_1" lnk_1_id = "/an-id/lnk_1_id" res_tar_2_id = "/an-id/res_tar_2" res_src_2_id = "/an-id/res_src_2" lnk_2_id = "/an-id/lnk_2_id" res_m_id = "/an-id/res_m" res_src_1 = core_model.Resource(res_src_1_id, occi_sla.AGREEMENT, None) res_tar_1 = core_model.Resource(res_tar_1_id, occi_sla.AGREEMENT, None) lnk_1 = core_model.Link(lnk_1_id, occi_sla.AGREEMENT_LINK, None, res_src_1, res_tar_1) res_src_2 = core_model.Resource(res_src_2_id, occi_sla.AGREEMENT, None) res_tar_2 = core_model.Resource(res_tar_2_id, occi_sla.AGREEMENT, None) lnk_2 = core_model.Link(lnk_2_id, occi_sla.AGREEMENT_LINK, None, res_src_2, res_tar_2) res_m = core_model.Resource(res_m_id, occi_sla.AGREEMENT, None, [lnk_1, lnk_2]) # Persist all to DB but not dict in memory entities = EntityDictionary(self.api.registry) entities._persist_link(lnk_1_id, lnk_1) entities._persist_link(lnk_2_id, lnk_2) entities._persist_resource(res_src_1_id, res_src_1) entities._persist_resource(res_tar_1_id, res_tar_1) entities._persist_resource(res_src_2_id, res_src_2) entities._persist_resource(res_tar_2_id, res_tar_2) entities._persist_resource(res_m_id, res_m) entities.populate_from_db() self.assertEqual(len(entities), 7) self.assertEqual(len(entities[res_m_id].links), 2) for link in entities[res_m_id].links: if link.identifier == lnk_1_id: # Removing assistive field templates for comparison temp_src = link.source.__dict__ temp_trgt = link.target.__dict__ temp_src.pop('templates') temp_trgt.pop('templates') self.assertEqual(temp_src, res_src_1.__dict__) self.assertEqual(temp_trgt, res_tar_1.__dict__) else: # Removing assistive field templates for comparison temp_src = link.source.__dict__ temp_trgt = link.target.__dict__ temp_src.pop('templates') temp_trgt.pop('templates') self.assertEqual(temp_src, res_src_2.__dict__) self.assertEqual(temp_trgt, res_tar_2.__dict__)
def _add_link(self, entity_record): """ Takes a dictionary representation of a link and generates a Link Object to the dictionary data structure. Will automatically add source and target objects and their dependencies to the collection if they are not already there. """ key = entity_record["_id"] del entity_record["_id"] entity = core_model.Link(None, None, None, None, None) entity.__dict__ = entity_record entity.kind = self.registry.get_category(entity.kind, None) entity.mixins = self._get_mixins(entity) if entity.source not in self: source_rec = self.entities.find_one(entity.source) self._add_resource(source_rec, False) entity.source = self.__getitem__(entity.source) if entity.target not in self: target_rec = self.entities.find_one(entity.target) if target_rec: self._add_resource(target_rec, False) entity.target = self.__getitem__(entity.target) else: entity.target = self.__getitem__(entity.target) entity.attributes = self._decode_attributes(entity.attributes) super(EntityDictionary, self).__setitem__(key, entity)
def __create_violation_link(self, agreement_id, violation, extras): """ Create an OCCI violation link with the proper attribute values """ LOG.debug( 'Violation instance created for agreement {}'.format(agreement_id)) now_iso = arrow.utcnow().isoformat() id = '/violation_link/' + str(uuid.uuid4()) myrulesengine = rulesengine.RulesEngine() agreement = myrulesengine._registry.get_resource(agreement_id, None) agreement.identifier = agreement_id res = core_model.Link(id, occi_violation.VIOLATION_LINK, [], agreement, violation) res.attributes = { 'occi.core.source': agreement_id, 'occi.core.target': violation.identifier } res.provider = extras["security"].items()[0][0] res.customer = extras["customer"] res.source = agreement res.target = violation.identifier # Updating agreement resource with new link agreement.links.append(res) myrulesengine._registry.resources.__setitem__(agreement_id, agreement) LOG.debug('Inserting violation link with ID: {}'.format(id)) myrulesengine._registry.resources.__setitem__(id, res) return res
def test_delete_for_sanity(self): """ Test deattachement. """ source = mox.MockObject(core_model.Resource) source.attributes = {'occi.core.id': 'foo'} target = mox.MockObject(core_model.Resource) target.attributes = {'occi.core.id': 'bar'} link = core_model.Link('foo', None, [], source, target) self.mox.StubOutWithMock(nova_glue.storage, 'get_storage') nova_glue.storage.get_storage(mox.IsA(object), mox.IsA(object)).\ AndReturn({'status': 'available', 'size': '1'}) self.mox.StubOutWithMock(nova_glue.vm, 'detach_volume') nova_glue.vm.detach_volume(mox.IsA(object), mox.IsA(object), mox.IsA(object)) self.mox.ReplayAll() self.backend.delete(link, self.sec_obj) self.mox.VerifyAll()
def test_create_for_sanity(self): """ Test attachement. """ source = mox.MockObject(core_model.Resource) source.attributes = {'occi.core.id': 'foo'} target = mox.MockObject(core_model.Resource) target.attributes = {'occi.core.id': 'bar'} link = core_model.Link('foo', None, [], source, target) link.attributes = {'occi.storagelink.deviceid': '/dev/sda'} self.mox.StubOutWithMock(nova_glue.vm, 'attach_volume') nova_glue.vm.attach_volume(mox.IsA(object), mox.IsA(object), mox.IsA(object), mox.IsA(object)).\ AndReturn({}) self.mox.ReplayAll() self.backend.create(link, self.sec_obj) # verify all attrs. self.assertEqual(link.attributes['occi.storagelink.deviceid'], '/dev/sda') self.assertIn('occi.storagelink.mountpoint', link.attributes) self.assertEqual(link.attributes['occi.storagelink.state'], 'active') self.mox.VerifyAll()
def setUp(self): self.dummy = dummies.DummyOpenShift2Adapter() self.cut = backends.ServiceLink(self.dummy) self.src = core_model.Resource('123', occi_ext.APP, []) self.trg = core_model.Resource('abc', occi_ext.COMPONENT, []) self.extras = {'auth_header': {'Authorization': 'Basic foobar'}} self.entity = core_model.Link('', occi_ext.APP, [], self.src, self.trg)
def test_populate_dictionary_with_basic_link_from_DB(self): # Create resources and Link src_id = "124816" tar_id = "112358" src_res = core_model.Resource(src_id, occi_sla.AGREEMENT, None) tar_res = core_model.Resource(tar_id, occi_sla.AGREEMENT, None) lnk_id = "/agreement/load-link-entity-from-DB" lnk = core_model.Link(lnk_id, None, None, src_res, tar_res) links = EntityDictionary(self.api.registry) links._persist_resource(src_id, src_res) links._persist_resource(tar_id, tar_res) links._persist_link(lnk_id, lnk) links.populate_from_db() self.assertEqual(len(links), 3) self.assertTrue(isinstance(links[lnk_id], core_model.Link)) self.assertEqual(links[lnk_id].identifier, lnk_id) # Removing assistive field templates for comparison temp_src = links[lnk_id].source.__dict__ temp_trgt = links[lnk_id].target.__dict__ temp_src.pop('templates') temp_trgt.pop('templates') self.assertEqual(temp_src, src_res.__dict__) self.assertEqual(temp_trgt, tar_res.__dict__)
def test_delete_multiple_entities_from_dictionary(self): """ Delete multiple entities and ensure others not deleted are persisted """ res_0 = core_model.Resource("res_0", None, None) res_1 = core_model.Resource("res_1", None, None) res_2 = core_model.Resource("res_2", None, None) res_3 = core_model.Resource("res_3", None, None) res_4 = core_model.Resource("res_4", None, None) res_5 = core_model.Resource("res_5", None, None) lnk_0 = core_model.Link("lnk_0", None, None, res_1, res_5) lnk_1 = core_model.Link("lnk_1", None, None, res_1, res_5) entities = EntityDictionary(None) entities[res_0.identifier] = res_0 entities[res_1.identifier] = res_1 entities[res_2.identifier] = res_2 entities[res_3.identifier] = res_3 entities[res_4.identifier] = res_4 entities[res_5.identifier] = res_5 entities[lnk_0.identifier] = lnk_0 entities[lnk_1.identifier] = lnk_1 del entities[res_1.identifier] del entities[res_5.identifier] del entities[lnk_1.identifier] self.assertIsNotNone(self.db.entities.find_one("res_0")) self.assertIsNotNone(self.db.entities.find_one("res_2")) self.assertIsNotNone(self.db.entities.find_one("res_3")) self.assertIsNotNone(self.db.entities.find_one("res_4")) self.assertIsNotNone(self.db.entities.find_one("lnk_0")) self.assertIsNone(self.db.entities.find_one("res_1")) self.assertIsNone(self.db.entities.find_one("res_5")) self.assertIsNone(self.db.entities.find_one("lnk_1")) self.assertTrue("res_0" in entities) self.assertTrue("res_2" in entities) self.assertTrue("res_3" in entities) self.assertTrue("res_4" in entities) self.assertTrue("lnk_0" in entities) self.assertFalse("res_1" in entities) self.assertFalse("res_5" in entities) self.assertFalse("lnk_1" in entities)
def test_create_for_sanity(self): """ Test create for sanity! """ source = mox.MockObject(core_model.Resource) source.attributes = {'occi.core.id': 'bar'} target = mox.MockObject(core_model.Resource) target.identifier = '/network/public' link = core_model.Link('foo', None, [os_addon.OS_NET_LINK], source, target) link.attributes = {'org.openstack.network.floating.pool': 'nova'} self.mox.StubOutWithMock(nova_glue.net, 'add_floating_ip') nova_glue.net.add_floating_ip(mox.IsA(str), mox.IsA(str), mox.IsA(object)).AndReturn('10.0.0.1') self.mox.ReplayAll() self.backend.create(link, self.sec_obj) # verify all attrs and mixins! self.assertIn('occi.networkinterface.interface', link.attributes) self.assertIn('occi.networkinterface.mac', link.attributes) self.assertIn('occi.networkinterface.state', link.attributes) self.assertIn('occi.networkinterface.address', link.attributes) self.assertIn('occi.networkinterface.gateway', link.attributes) self.assertIn('occi.networkinterface.allocation', link.attributes) # self.assertIn(infrastructure.IPNETWORKINTERFACE, link.mixins) # self.assertIn(infrastructure.NETWORKINTERFACE, link.mixins) # test without pool name... self.mox.UnsetStubs() link = core_model.Link('foo', None, [], source, target) self.mox.StubOutWithMock(nova_glue.net, 'add_floating_ip') nova_glue.net.add_floating_ip(mox.IsA(str), mox.IsA(None), mox.IsA(object)).AndReturn('10.0.0.2') self.mox.ReplayAll() self.backend.create(link, self.sec_obj) self.mox.VerifyAll()
def get_occi_link(kind, link_uuid, source, target): """ Get the OCCI Link :param kind: type of the link :param link_uuid: uuid of the link :param source: source entity :param target: target entity :return Link: OCCI Link """ iden = KIND_TYPE_MAPPING[kind].location + link_uuid link = core_model.Link(iden, KIND_TYPE_MAPPING[kind], [], source, target) source.links.append(link) return link
def test_delete_for_sanity(self): """ Test deattachement. """ source = mox.MockObject(core_model.Resource) target = mox.MockObject(core_model.Resource) target.attributes = {'occi.core.id': 'bar'} link = core_model.Link('foo', None, [], source, target) self.mox.StubOutWithMock(nova_glue.vm, 'detach_volume') nova_glue.vm.detach_volume(mox.IsA(object), mox.IsA(object)) self.mox.ReplayAll() self.backend.delete(link, self.sec_obj) self.mox.VerifyAll()
def test_policy_creation(self): """ Check that an active agreement has been detected and a record inserted into the database """ availability = core_model.Mixin('', 'availability', [occi_sla.AGREEMENT_TEMPLATE]) mixins = [availability] res = core_model.Resource(self.id, occi_sla.AGREEMENT, mixins) res.attributes = { 'occi.agreement.state': 'accepted', 'occi.agreement.effectiveFrom': '2014-11-02T02:20:26+00:00', 'occi.agreement.effectiveUntil': '2015-11-02T02:20:27+00:00' } comp_res = core_model.Resource(self.compute_id, infrastructure.COMPUTE, []) comp_res.attributes = {} link_res = core_model.Link(self.link_id, occi_sla.AGREEMENT_LINK, [], res, comp_res) link_res.attributes = {} res.links = [link_res] northbound_api = api.build() resources = EntityDictionary(northbound_api.registry) resources[self.id] = res resources[self.compute_id] = comp_res resources[self.link_id] = link_res resources.registry.populate_resources() myrulesengine = rulesengine.RulesEngine(resources.registry) myrulesengine.active_policies = {} myrulesengine.active_agreements = {} myrulesengine.start_engine(0) temp_policy_record = DB.policies.find({'agreement_id': self.id}, {'_id': 0}) self.assertEqual(temp_policy_record.count(), 1)
def _construct_network_link(self, net_desc, source, target, extras): """ Construct a network link and add to cache! """ link = core_model.Link( infrastructure.NETWORKINTERFACE.location + str(uuid.uuid4()), infrastructure.NETWORKINTERFACE, [infrastructure.IPNETWORKINTERFACE], source, target) link.attributes = { 'occi.networkinterface.interface': net_desc['interface'], 'occi.networkinterface.mac': net_desc['mac'], 'occi.networkinterface.state': net_desc['state'], 'occi.networkinterface.address': net_desc['address'], 'occi.networkinterface.gateway': net_desc['gateway'], 'occi.networkinterface.allocation': net_desc['allocation'] } link.extras = self.get_extras(extras) source.links.append(link) self.cache[(link.identifier, extras['nova_ctx'].user_id)] = link return link
def test_delete_for_sanity(self): """ Test create for sanity! """ source = mox.MockObject(core_model.Resource) source.attributes = {'occi.core.id': 'bar'} target = mox.MockObject(core_model.Resource) target.identifier = '/network/public' link = core_model.Link('foo', None, [], source, target) link.attributes = {'occi.networkinterface.address': '10.0.0.1'} self.mox.StubOutWithMock(nova_glue.net, 'remove_floating_ip') nova_glue.net.remove_floating_ip(mox.IsA(object), mox.IsA(object), mox.IsA(object)) self.mox.ReplayAll() self.backend.delete(link, self.sec_obj) self.mox.VerifyAll()
def get_resources(self, extras): # TODO fix for OpSv3 - no method implemented tmp = self.glue.list_apps(extras['auth_header']) for item in tmp['data']: uid = item['id'] if '/app/' + uid not in self.resources: # generic part entity = core_model.Resource('/app/' + uid, occi_ext.APP, []) entity.attributes['occi.core.id'] = uid res_temp = self.get_category('/' + item['gear_profile'] + '/', extras) # app_temp = self.get_category('/' + item['framework'] + # '/', extras) # links to services for component in item['embedded']: try: srv_link_key = '/componentlink/' + str(uuid.uuid4()) target = self.get_resource('/component/' + component, None) source = entity link = core_model.Link(srv_link_key, occi_ext.COMP_LINK, [], source, target) self.add_resource(srv_link_key, link, None) entity.links.append(link) except KeyError: LOG.debug('Skipping link to the component {}.' .format(component)) entity.mixins = [res_temp] # , app_temp] self.add_resource('', entity, extras) # TODO make this nicer available_res = self.resources.keys() for res_key in available_res: ids = [item['id'] for item in tmp['data']] if res_key.split('/')[2] not in ids and \ res_key.find('component') == -1 and \ res_key.find('public_key') == -1: self.delete_resource(res_key, extras) return super(Registry, self).get_resources(extras)
def test_correct_resource_links_collection_are_being_added_to_db(self): """ Test if the correct links are being added to the resource record in the DB. note: these are links within resources. Not links as resources """ # Create resources and Link src_res_id = "124816" tar_res_id = "112358" src_res = core_model.Resource(src_res_id, None, None) tar_res = core_model.Resource(tar_res_id, None, None) lnk_id = {"_id": "/agreement/add-link-entity"} lnk = core_model.Link(lnk_id["_id"], None, None, src_res, tar_res) # Load Link resources = EntityDictionary(None) resources[lnk_id["_id"]] = lnk result_dict = self.db.entities.find_one(lnk_id["_id"]) self.assertEqual(result_dict["source"], src_res_id) self.assertEqual(result_dict["target"], tar_res_id) LOG.info("Correct resource links added to the DB")
def _construct_occi_storage(self, identifier, extras): """ Construct a OCCI storage instance. First item in result list is entity self! Adds it to the cache too! """ result = [] context = extras['nova_ctx'] stor = storage.get_storage(identifier, context) # id, display_name, size, status iden = infrastructure.STORAGE.location + identifier entity = core_model.Resource(iden, infrastructure.STORAGE, []) result.append(entity) # create links on VM resources if stor['status'] == 'in-use': source = self.get_resource( infrastructure.COMPUTE.location + str(stor['instance_uuid']), extras) link = core_model.Link( infrastructure.STORAGELINK.location + str(uuid.uuid4()), infrastructure.STORAGELINK, [], source, entity) link.extras = self.get_extras(extras) source.links.append(link) result.append(link) self.cache[(link.identifier, context.user_id)] = link # core.id and cache it! entity.attributes['occi.core.id'] = identifier entity.extras = self.get_extras(extras) self.cache[(entity.identifier, context.user_id)] = entity return result