def test_create_link_invalid_pool(self): tenant = fakes.tenants["foo"] server_id = fakes.servers[tenant["id"]][0]["id"] server_url = utils.join_url(self.application_url + "/", "compute/%s" % server_id) net_url = utils.join_url(self.application_url + "/", "network/floating") app = self.get_app() headers = { 'Category': ('networkinterface;' 'scheme="http://schemas.ogf.org/occi/infrastructure#";' 'class="kind",' 'invalid;' 'scheme="http://schemas.openstack.org/network/' 'floatingippool#"; class="mixin"'), 'X-OCCI-Attribute': ('occi.core.source="%s", ' 'occi.core.target="%s"') % (server_url, net_url) } req = self._build_req("/networklink", tenant["id"], method="POST", headers=headers) resp = req.get_response(app) self.assertEqual(404, resp.status_code)
def test_create_link_no_pool(self): tenant = fakes.tenants["foo"] server_id = fakes.servers[tenant["id"]][0]["id"] server_url = utils.join_url(self.application_url + "/", "compute/%s" % server_id) net_url = utils.join_url(self.application_url + "/", "network/floating") app = self.get_app() headers = { 'Category': ('networkinterface;' 'scheme="http://schemas.ogf.org/occi/infrastructure#";' 'class="kind"'), 'X-OCCI-Attribute': ('occi.core.source="%s", ' 'occi.core.target="%s"') % (server_url, net_url) } req = self._build_req("/networklink", tenant["id"], method="POST", headers=headers) resp = req.get_response(app) link_id = '_'.join([server_id, fakes.allocated_ip]) expected = [("X-OCCI-Location", utils.join_url(self.application_url + "/", "networklink/%s" % link_id))] self.assertEqual(200, resp.status_code) self.assertExpectedResult(expected, resp) self.assertDefaults(resp)
def test_show_iface(self, m_show): tenant = fakes.tenants["foo"] m_show.return_value = fakes.fake_network_link_occi( fakes.network_links[tenant['id']] ) for ip in fakes.network_links[tenant["id"]]: if ip["instance_id"] is not None: link_id = '_'.join([ip["instance_id"], ip["network_id"], ip["ip"]] ) req = self._build_req("/networklink/%s" % link_id, tenant["id"], method="GET") resp = req.get_response(self.app) self.assertContentType(resp) # net_id = uuid.uuid4().hex source = utils.join_url(self.application_url + "/", "compute/%s" % ip["instance_id"]) target = utils.join_url(self.application_url + "/", "network/%s" % ip['network_id']) self.assertResultIncludesLinkAttr(link_id, source, target, resp) self.assertEqual(200, resp.status_code)
def test_create_vm_with_network(self): tenant = fakes.tenants["foo"] target = utils.join_url(self.application_url + "/", "network/%s" % uuid.uuid4().hex) app = self.get_app() headers = { 'Category': ( 'compute;' 'scheme="http://schemas.ogf.org/occi/infrastructure#";' 'class="kind",' 'foo;' 'scheme="http://schemas.openstack.org/template/resource#";' 'class="mixin",' 'bar;' 'scheme="http://schemas.openstack.org/template/os#";' 'class="mixin"'), 'Link': ( '</bar>;' 'rel="http://schemas.ogf.org/occi/infrastructure#network";' 'occi.core.target="%s"') % target } req = self._build_req("/compute", tenant["id"], method="POST", headers=headers) resp = req.get_response(app) expected = [("X-OCCI-Location", utils.join_url(self.application_url + "/", "compute/%s" % "foo"))] self.assertEqual(200, resp.status_code) self.assertExpectedResult(expected, resp) self.assertDefaults(resp)
def test_create_link_with_pool(self): tenant = fakes.tenants["foo"] server_id = fakes.servers[tenant["id"]][0]["id"] server_url = utils.join_url(self.application_url + "/", "compute/%s" % server_id) net_url = utils.join_url(self.application_url + "/", "network/floating") pool_name = fakes.pools[tenant["id"]][0]["name"] app = self.get_app() headers = { 'Category': ( 'networkinterface;' 'scheme="http://schemas.ogf.org/occi/infrastructure#";' 'class="kind",' '%s;' 'scheme="http://schemas.openstack.org/network/' 'floatingippool#"; class="mixin"') % pool_name, 'X-OCCI-Attribute': ( 'occi.core.source="%s", ' 'occi.core.target="%s"' ) % (server_url, net_url) } req = self._build_req("/networklink", tenant["id"], method="POST", headers=headers) resp = req.get_response(app) link_id = '_'.join([server_id, fakes.allocated_ip]) expected = [("X-OCCI-Location", utils.join_url(self.application_url + "/", "networklink/%s" % link_id))] self.assertEqual(200, resp.status_code) self.assertExpectedResult(expected, resp) self.assertDefaults(resp)
def test_list_ifaces(self): tenant = fakes.tenants["baz"] for url in ("/networklink/", "/networklink"): req = self._build_req(url, tenant["id"], method="GET") resp = req.get_response(self.app) self.assertEqual(200, resp.status_code) expected = [] float_list = {} for floating_ip in fakes.floating_ips[tenant["id"]]: if floating_ip["instance_id"]: float_list.update({floating_ip['fixed_ip']: floating_ip}) instance_vm = fakes.linked_vm_id for p in fakes.ports[tenant["id"]]: for ip in p["fixed_ips"]: link_id = '_'.join( [instance_vm, p["net_id"], ip["ip_address"]]) expected.append( ("X-OCCI-Location", utils.join_url(self.application_url + "/", "networklink/%s" % link_id))) float_ip = float_list.get(ip['ip_address'], None) if float_ip: link_id = '_'.join( [instance_vm, "PUBLIC", float_ip["ip"]]) expected.append( ("X-OCCI-Location", utils.join_url(self.application_url + "/", "networklink/%s" % link_id))) self.assertExpectedResult(expected, resp)
def test_create_link_invalid_pool(self): tenant = fakes.tenants["foo"] server_id = fakes.servers[tenant["id"]][0]["id"] server_url = utils.join_url(self.application_url + "/", "compute/%s" % server_id) net_url = utils.join_url(self.application_url + "/", "network/floating") app = self.get_app() headers = { 'Category': ( 'networkinterface;' 'scheme="http://schemas.ogf.org/occi/infrastructure#";' 'class="kind",' 'invalid;' 'scheme="http://schemas.openstack.org/network/' 'floatingippool#"; class="mixin"'), 'X-OCCI-Attribute': ( 'occi.core.source="%s", ' 'occi.core.target="%s"' ) % (server_url, net_url) } req = self._build_req("/networklink", tenant["id"], method="POST", headers=headers) resp = req.get_response(app) self.assertEqual(404, resp.status_code)
def test_create_link_with_device(self): tenant = fakes.tenants["foo"] server_id = fakes.servers[tenant["id"]][0]["id"] server_url = utils.join_url(self.application_url + "/", "compute/%s" % server_id) vol_id = fakes.volumes[tenant["id"]][0]["id"] vol_url = utils.join_url(self.application_url + "/", "storage/%s" % vol_id) app = self.get_app() headers = { 'Category': ( 'storagelink;' 'scheme="http://schemas.ogf.org/occi/infrastructure#";' 'class="kind"'), 'X-OCCI-Attribute': ( 'occi.storagelink.deviceid="/dev/vdc", ' 'occi.core.source="%s", ' 'occi.core.target="%s"' ) % (server_url, vol_url) } req = self._build_req("/storagelink", tenant["id"], method="POST", headers=headers) resp = req.get_response(app) link_id = '_'.join([server_id, vol_id]) expected = [("X-OCCI-Location", utils.join_url(self.application_url + "/", "storagelink/%s" % link_id))] self.assertEqual(200, resp.status_code) self.assertExpectedResult(expected, resp) self.assertDefaults(resp)
def test_create_link_with_pool(self): tenant = fakes.tenants["baz"] link_info = fakes.ports[tenant['id']][0] server_url = utils.join_url(self.application_url + "/", "compute/%s" % link_info['server_id']) net_url = utils.join_url(self.application_url + "/", "network/%s" % link_info['net_id']) pool_name = 'pool' headers = { 'Category': ('networkinterface;' 'scheme="http://schemas.ogf.org/occi/' 'infrastructure#";' 'class="kind",' '%s;' 'scheme="http://schemas.openstack.org/network/' 'floatingippool#"; class="mixin"') % pool_name, 'X-OCCI-Attribute': ('occi.core.source="%s", ' 'occi.core.target="%s"') % (server_url, net_url) } req = self._build_req("/networklink", tenant["id"], method="POST", headers=headers) resp = req.get_response(self.app) link_id = '_'.join( [link_info['server_id'], link_info['fixed_ips'][0]["ip_address"]]) expected = [("X-OCCI-Location", utils.join_url(self.application_url + "/", "networklink/%s" % link_id))] self.assertEqual(200, resp.status_code) self.assertExpectedResult(expected, resp) self.assertDefaults(resp)
def render_dict(self, env={}): r = super(LinkRenderer, self).render_dict(env) url = env.get("application_url", "") r["source"] = { "kind": self.obj.source.kind.type_id, "location": utils.join_url(url, self.obj.source.location), } r["target"] = { "kind": self.obj.target.kind.type_id, "location": utils.join_url(url, self.obj.target.location), } return r
def test_vm_links(self): tenant = fakes.tenants["baz"] app = self.get_app() for server in fakes.servers[tenant["id"]]: req = self._build_req("/compute/%s" % server["id"], tenant["id"], method="GET") resp = req.get_response(app) self.assertDefaults(resp) self.assertContentType(resp) self.assertEqual(200, resp.status_code) source = utils.join_url(self.application_url + "/", "compute/%s" % server["id"]) # volumes vols = server.get("os-extended-volumes:volumes_attached", []) for v in vols: vol_id = v["id"] link_id = '_'.join([server["id"], vol_id]) target = utils.join_url(self.application_url + "/", "storage/%s" % vol_id) self.assertResultIncludesLink(link_id, source, target, resp) # network addresses = server.get("addresses", {}) for addr_set in addresses.values(): for addr in addr_set: ip = addr["addr"] if addr["OS-EXT-IPS:type"] == "fixed": for p in fakes.ports[tenant["id"]]: if (p["mac_addr"] == addr["OS-EXT-IPS-MAC:mac_addr"]): net_id = p["net_id"] break target = utils.join_url(self.application_url + "/", "network/%s" % net_id) else: for floating_ip in fakes.floating_ips[tenant["id"]]: if floating_ip["ip"] == ip: net_id = floating_ip['id'] break target = utils.join_url(self.application_url + "/", "ipreservation/%s" % net_id) link_id = '_'.join([server["id"], ip]) self.assertResultIncludesLink(link_id, source, target, resp)
def test_create_with_context(self): tenant = fakes.tenants["foo"] app = self.get_app() headers = { 'Category': ('compute;' 'scheme="http://schemas.ogf.org/occi/infrastructure#";' 'class="kind",' 'foo;' 'scheme="http://schemas.openstack.org/template/resource#";' 'class="mixin",' 'bar;' 'scheme="http://schemas.openstack.org/template/os#";' 'class="mixin",' 'user_data;' 'scheme="http://schemas.openstack.org/compute/instance#";' 'class="mixin"'), 'X-OCCI-Attribute': ('org.openstack.compute.user_data="foo"') } req = self._build_req("/compute", tenant["id"], method="POST", headers=headers) resp = req.get_response(app) expected = [("X-OCCI-Location", utils.join_url(self.application_url + "/", "compute/%s" % "foo"))] self.assertEqual(200, resp.status_code) self.assertExpectedResult(expected, resp) self.assertDefaults(resp)
def test_list_ifaces(self, mock_index): tenant = fakes.tenants["foo"] mock_index.return_value = collection.Collection( fakes.fake_network_link_occi( fakes.network_links[tenant['id']] ) ) for url in ("/networklink/", "/networklink"): req = self._build_req(url, tenant["id"], method="GET") resp = req.get_response(self.app) self.assertEqual(200, resp.status_code) expected = [] for ip in fakes.network_links[tenant["id"]]: if ip["instance_id"]: # fixme(jorgesece): test in case of instance None link_id = '_'.join([ip["instance_id"], ip["network_id"], ip["ip"]]) expected.append( ("X-OCCI-Location", utils.join_url(self.application_url + "/", "networklink/%s" % link_id)) ) self.assertExpectedResult(expected, resp)
def test_create_with_context(self): tenant = fakes.tenants["foo"] app = self.get_app() headers = { 'Category': ( 'compute;' 'scheme="http://schemas.ogf.org/occi/infrastructure#";' 'class="kind",' 'foo;' 'scheme="http://schemas.openstack.org/template/resource#";' 'class="mixin",' 'bar;' 'scheme="http://schemas.openstack.org/template/os#";' 'class="mixin",' 'user_data;' 'scheme="http://schemas.openstack.org/compute/instance#";' 'class="mixin"' ), 'X-OCCI-Attribute': ( 'org.openstack.compute.user_data="foo"' ) } req = self._build_req("/compute", tenant["id"], method="POST", headers=headers) resp = req.get_response(app) expected = [("X-OCCI-Location", utils.join_url(self.application_url + "/", "compute/%s" % "foo"))] self.assertEqual(200, resp.status_code) self.assertExpectedResult(expected, resp) self.assertDefaults(resp)
def test_save_action_vm(self): tenant = fakes.tenants["foo"] app = self.get_app() headers = { 'Category': ( 'save ;' 'scheme="http://schemas.ogf.org/occi/infrastructure/' 'compute/action#";' 'class="action"'), # There is no easy way to test that this was taken into account, # but at least it should not fail if the attribute is there 'X-OCCI-Attribute': 'name="foobarimg"', } for server in fakes.servers[tenant["id"]]: req = self._build_req("/compute/%s?action=save" % server["id"], tenant["id"], method="POST", headers=headers) resp = req.get_response(app) self.assertDefaults(resp) expected = [("X-OCCI-Location", utils.join_url(self.application_url + "/", "os_tpl/%s" % fakes.action_loc_id))] self.assertEqual(200, resp.status_code) self.assertExpectedResult(expected, resp)
def test_create(self, m): tenant = fakes.tenants["foo"] net_out = fakes.create_fake_json_resp( {"network": fakes.networks[tenant['id']][0]}, 200) mock_net = mock.Mock(webob.Request) mock_net.get_response.return_value = net_out subnet_out = fakes.create_fake_json_resp( {"subnet": fakes.networks[tenant['id']][0]["subnet_info"]}, 200) mock_subnet = mock.Mock(webob.Request) mock_subnet.get_response.return_value = subnet_out public_out = fakes.create_fake_json_resp( {"networks": fakes.networks[tenant['id']]}, 200) mock_public = mock.Mock(webob.Request) mock_public.get_response.return_value = public_out router_out = fakes.create_fake_json_resp( {"router": {"id": uuid.uuid4().hex}}, 200) mock_router = mock.Mock(webob.Request) mock_router.get_response.return_value = router_out mock_iface = mock.Mock(webob.Request) mock_iface.get_response.return_value = fakes.create_fake_json_resp( {"foo": "foo"}, 200) m.side_effect = [mock_net, mock_subnet, mock_public, mock_router, mock_iface ] name = fakes.networks[tenant["id"]][0]["name"] net_id = fakes.networks[tenant["id"]][0]["id"] address = fakes.networks[tenant["id"]][0]["subnet_info"]["cidr"] headers = { 'Category': 'network;' ' scheme=' '"http://schemas.ogf.org/occi/infrastructure#";' 'class="kind",' 'ipnetwork;' ' scheme=' '"http://schemas.ogf.org/occi/infrastructure/' 'network#";' 'class="mixin",', 'X-OCCI-Attribute': '"occi.core.title"="%s",' '"occi.network.address"="%s"' % (name, address) } req = self._build_req(path="/network", tenant_id='X', method="POST", headers=headers) m.return_value = fakes.networks[tenant['id']][0] resp = req.get_response(self.app) self.assertEqual(200, resp.status_code) expected = [("X-OCCI-Location", utils.join_url(self.application_url + "/", "network/%s" % net_id))] self.assertExpectedResult(expected, resp)
def test_create(self, m): tenant = fakes.tenants["foo"] net_out = fakes.create_fake_json_resp( {"network": fakes.networks[tenant['id']][0]}, 200) mock_net = mock.Mock(webob.Request) mock_net.get_response.return_value = net_out subnet_out = fakes.create_fake_json_resp( {"subnet": fakes.networks[tenant['id']][0]["subnet_info"]}, 200) mock_subnet = mock.Mock(webob.Request) mock_subnet.get_response.return_value = subnet_out public_out = fakes.create_fake_json_resp( {"networks": fakes.networks[tenant['id']]}, 200) mock_public = mock.Mock(webob.Request) mock_public.get_response.return_value = public_out router_out = fakes.create_fake_json_resp( {"router": { "id": uuid.uuid4().hex }}, 200) mock_router = mock.Mock(webob.Request) mock_router.get_response.return_value = router_out mock_iface = mock.Mock(webob.Request) mock_iface.get_response.return_value = fakes.create_fake_json_resp( {"foo": "foo"}, 200) m.side_effect = [ mock_net, mock_subnet, mock_public, mock_router, mock_iface ] name = fakes.networks[tenant["id"]][0]["name"] net_id = fakes.networks[tenant["id"]][0]["id"] address = fakes.networks[tenant["id"]][0]["subnet_info"]["cidr"] headers = { 'Category': 'network;' ' scheme=' '"http://schemas.ogf.org/occi/infrastructure#";' 'class="kind",' 'ipnetwork;' ' scheme=' '"http://schemas.ogf.org/occi/infrastructure/' 'network#";' 'class="mixin",', 'X-OCCI-Attribute': '"occi.core.title"="%s",' '"occi.network.address"="%s"' % (name, address) } req = self._build_req(path="/network", tenant_id='X', method="POST", headers=headers) m.return_value = fakes.networks[tenant['id']][0] resp = req.get_response(self.app) self.assertEqual(200, resp.status_code) expected = [("X-OCCI-Location", utils.join_url(self.application_url + "/", "network/%s" % net_id))] self.assertExpectedResult(expected, resp)
def render(self, env={}): app_url = env.get("application_url", "") ret = [] for what in [self.obj.kinds, self.obj.mixins, self.obj.actions, self.obj.resources, self.obj.links]: for el in what: url = utils.join_url(app_url, el.location) ret.append(('X-OCCI-Location', '%s' % url)) return ret
def test_show_iface(self): tenant = fakes.tenants["baz"] app = self.get_app() for ip in fakes.floating_ips[tenant["id"]]: if ip["instance_id"] is not None: link_id = '_'.join([ip["instance_id"], ip["ip"]]) req = self._build_req("/networklink/%s" % link_id, tenant["id"], method="GET") resp = req.get_response(app) self.assertContentType(resp) source = utils.join_url(self.application_url + "/", "compute/%s" % ip["instance_id"]) target = utils.join_url(self.application_url + "/", "network/floating/%s" % ip["pool"]) self.assertResultIncludesLink(link_id, source, target, resp) self.assertEqual(200, resp.status_code)
def test_list_pools(self): tenant = fakes.tenants["foo"] app = self.get_app() for url in ("/network", "/network/"): req = self._build_req(url, tenant["id"], method="GET") resp = req.get_response(app) self.assertEqual(200, resp.status_code) expected = [("X-OCCI-Location", utils.join_url(self.application_url + "/", "network/fixed"))] if fakes.pools[tenant["id"]]: expected.append(("X-OCCI-Location", utils.join_url(self.application_url + "/", "network/floating"))) self.assertDefaults(resp) self.assertExpectedResult(expected, resp)
def test_show_iface(self): tenant = fakes.tenants["baz"] for p in fakes.ports[tenant["id"]]: for ip in p["fixed_ips"]: instance_vm = p["server_id"] link_id = '_'.join([instance_vm, ip["ip_address"]]) req = self._build_req("/networklink/%s" % link_id, tenant["id"], method="GET") resp = req.get_response(self.app) self.assertContentType(resp) source = utils.join_url(self.application_url + "/", "compute/%s" % instance_vm) target = utils.join_url(self.application_url + "/", "network/%s" % p["net_id"]) self.assertResultIncludesLinkAttr(link_id, source, target, resp) self.assertEqual(200, resp.status_code)
def test_show_link(self): tenant = fakes.tenants["baz"] app = self.get_app() for volume in fakes.volumes[tenant["id"]]: for a in volume["attachments"]: link_id = '_'.join([a["serverId"], volume["id"]]) req = self._build_req("/storagelink/%s" % link_id, tenant["id"], method="GET") resp = req.get_response(app) self.assertContentType(resp) link_id = '_'.join([a["serverId"], a["volumeId"]]) source = utils.join_url(self.application_url + "/", "compute/%s" % a["serverId"]) target = utils.join_url(self.application_url + "/", "storage/%s" % a["volumeId"]) self.assertResultIncludesLink(link_id, source, target, resp) self.assertEqual(200, resp.status_code)
def test_show_link(self): tenant = fakes.tenants["baz"] app = self.get_app() for volume in fakes.volumes[tenant["id"]]: for a in volume["attachments"]: link_id = '_'.join([a["serverId"], volume["id"]]) req = self._build_req("/storagelink/%s" % link_id, tenant["id"], method="GET") resp = req.get_response(app) self.assertContentType(resp) link_id = '_'.join([a["serverId"], a["volumeId"]]) source = utils.join_url(self.application_url + "/", "compute/%s" % a["serverId"]) target = utils.join_url(self.application_url + "/", "storage/%s" % a["volumeId"]) self.assertResultIncludesLinkAttr(link_id, source, target, resp) self.assertEqual(200, resp.status_code)
def render(self, ass_obj=None, env={}): # We have an associated object, render it as a link to that object if ass_obj is not None: url = env.get("application_url", "") term = ass_obj.kind.term + "/" url = utils.join_url(url, [term, ass_obj.id, self.obj.location]) d = {"location": url, "rel": self.obj.type_id} l = '<%(location)s>; rel="%(rel)s"' % d return [('Link', l)] else: # Otherwise, render as category return super(ActionRenderer, self).render(env=env)
def test_create_link_no_pool(self): tenant = fakes.tenants["foo"] net_id = fakes.ports[tenant['id']][0]['net_id'] occi_compute_id = utils.join_url( self.application_url + "/", "compute/%s" % fakes.linked_vm_id) occi_net_id = utils.join_url(self.application_url + "/", "network/%s" % net_id) headers = { 'Category': ( 'networkinterface;' 'scheme="http://schemas.ogf.org/occi/infrastructure#";' 'class="kind"'), 'X-OCCI-Attribute': ('occi.core.source="%s", ' 'occi.core.target="%s"' ) % (occi_compute_id, occi_net_id) } req = self._build_req("/networklink", tenant["id"], method="POST", headers=headers) resp = req.get_response(self.app) self.assertEqual(200, resp.status_code)
def test_show_iface(self): tenant = fakes.tenants["baz"] instance_vm = fakes.linked_vm_id for p in fakes.ports[tenant["id"]]: for ip in p["fixed_ips"]: link_id = '_'.join([instance_vm, p["net_id"], ip["ip_address"]] ) req = self._build_req("/networklink/%s" % link_id, tenant["id"], method="GET") resp = req.get_response(self.app) self.assertContentType(resp) source = utils.join_url(self.application_url + "/", "compute/%s" % instance_vm) target = utils.join_url(self.application_url + "/", "network/%s" % p["net_id"]) self.assertResultIncludesLinkAttr(link_id, source, target, resp) self.assertEqual(200, resp.status_code)
def test_create_link_with_pool(self, m_create): tenant = fakes.tenants["foo"] m_create.return_value = collection.Collection( [fakes.fake_network_link_occi( fakes.network_links[tenant['id']] )[0]]) link_info = fakes.network_links[tenant['id']][0] server_url = utils.join_url(self.application_url + "/", "compute/%s" % link_info['instance_id']) net_url = utils.join_url(self.application_url + "/", "network/%s" % link_info['network_id']) pool_name = 'pool' headers = { 'Category': ( 'networkinterface;' 'scheme="http://schemas.ogf.org/occi/infrastructure#";' 'class="kind",' '%s;' 'scheme="http://schemas.openstack.org/network/' 'floatingippool#"; class="mixin"') % pool_name, 'X-OCCI-Attribute': ( 'occi.core.source="%s", ' 'occi.core.target="%s"' ) % (server_url, net_url) } req = self._build_req("/networklink", tenant["id"], method="POST", headers=headers) resp = req.get_response(self.app) link_id = '_'.join([link_info['instance_id'], link_info['network_id'], link_info['ip']]) expected = [("X-OCCI-Location", utils.join_url(self.application_url + "/", "networklink/%s" % link_id))] self.assertEqual(200, resp.status_code) self.assertExpectedResult(expected, resp) self.assertDefaults(resp)
def test_list_pools(self): tenant = fakes.tenants["foo"] app = self.get_app() for url in ("/network", "/network/"): req = self._build_req(url, tenant["id"], method="GET") resp = req.get_response(app) self.assertEqual(200, resp.status_code) expected = [ ("X-OCCI-Location", utils.join_url(self.application_url + "/", "network/fixed")) ] if fakes.pools[tenant["id"]]: expected.append( ("X-OCCI-Location", utils.join_url(self.application_url + "/", "network/floating")) ) self.assertDefaults(resp) self.assertExpectedResult(expected, resp)
def test_create_link_ipreservation(self): tenant = fakes.tenants["baz"] net_id = fakes.floating_ips[tenant['id']][0]['id'] occi_compute_id = utils.join_url(self.application_url + "/", "compute/%s" % fakes.linked_vm_id) occi_net_id = utils.join_url(self.application_url + "/", "ipreservation/%s" % net_id) headers = { 'Category': ('networkinterface;' 'scheme="http://schemas.ogf.org/occi/infrastructure#";' 'class="kind"'), 'X-OCCI-Attribute': ('occi.core.source="%s", ' 'occi.core.target="%s"') % (occi_compute_id, occi_net_id) } req = self._build_req("/networklink", tenant["id"], method="POST", headers=headers) resp = req.get_response(self.app) self.assertEqual(200, resp.status_code)
def render_attr(self, env={}): value_str = '' if isinstance(self.obj.value, six.string_types): value_str = '"%s"' % self.obj.value elif isinstance(self.obj.value, bool): value_str = '"%s"' % str(self.obj.value).lower() elif isinstance(self.obj.value, entity.Entity): app_url = env.get("application_url", "") url = utils.join_url(app_url, self.obj.value.location) value_str = '"%s"' % url else: value_str = "%s" % self.obj.value return '%s=%s' % (self.obj.name, value_str)
def test_list_vms(self): tenant = fakes.tenants["foo"] app = self.get_app() for url in ("/compute/", "/compute"): req = self._build_req(url, tenant["id"], method="GET") resp = req.get_response(app) self.assertEqual(200, resp.status_code) expected = [] for s in fakes.servers[tenant["id"]]: expected.append(("X-OCCI-Location", utils.join_url(self.application_url + "/", "compute/%s" % s["id"]))) self.assertDefaults(resp) self.assertExpectedResult(expected, resp)
def test_list_ifaces(self): tenant = fakes.tenants["baz"] for url in ("/networklink/", "/networklink"): req = self._build_req(url, tenant["id"], method="GET") resp = req.get_response(self.app) self.assertEqual(200, resp.status_code) expected = [] float_list = {} for floating_ip in fakes.floating_ips[tenant["id"]]: if floating_ip["instance_id"]: float_list.update({floating_ip['fixed_ip']: floating_ip}) instance_vm = fakes.linked_vm_id for p in fakes.ports[tenant["id"]]: for ip in p["fixed_ips"]: link_id = '_'.join([instance_vm, p["net_id"], ip["ip_address"]]) expected.append( ("X-OCCI-Location", utils.join_url(self.application_url + "/", "networklink/%s" % link_id)) ) float_ip = float_list.get(ip['ip_address'], None) if float_ip: link_id = '_'.join([instance_vm, "PUBLIC", float_ip["ip"]]) expected.append( ("X-OCCI-Location", utils.join_url(self.application_url + "/", "networklink/%s" % link_id)) ) self.assertExpectedResult(expected, resp)
def test_list_networks(self, m): tenant = fakes.tenants["foo"] out = fakes.create_fake_json_resp( {"networks": fakes.networks[tenant['id']]}, 200) m.return_value.get_response.return_value = out req = self._build_req(path="/network", tenant_id='X', method="GET") resp = req.get_response(self.app) self.assertEqual(200, resp.status_code) expected = [] for s in fakes.networks[tenant["id"]]: expected.append(("X-OCCI-Location", utils.join_url(self.application_url + "/", "network/%s" % s["id"]))) self.assertDefaults(resp) self.assertExpectedResult(expected, resp)
def render_link(self, env={}): ret = [] url = env.get("application_url", "") url = utils.join_url(url, self.obj.location) d = {"location": url, "scheme": self.obj.target.kind.scheme, "term": self.obj.target.kind.term, "self": url} l = '<%(location)s>; rel="%(scheme)s%(term)s"; self="%(self)s"' % d ret.append(l) for a in self.obj.attributes: if self.obj.attributes[a].value is None: continue ret.append(AttributeRenderer( self.obj.attributes[a]).render_attr(env=env)) return [('Link', '; '.join(ret))]
def test_list(self): tenant = fakes.tenants["baz"] for url in ("/ipreservation/", "/ipreservation"): req = self._build_req(url, tenant["id"], method="GET") resp = req.get_response(self.app) self.assertEqual(200, resp.status_code) expected = [] for ip in fakes.floating_ips[tenant["id"]]: expected.append( ("X-OCCI-Location", utils.join_url(self.application_url + "/", "ipreservation/%s" % ip["id"]))) self.assertExpectedResult(expected, resp)
def test_list_vols(self): tenant = fakes.tenants["foo"] app = self.get_app() for url in ("/storage/", "/storage"): req = self._build_req(url, tenant["id"], method="GET") resp = req.get_response(app) self.assertEqual(200, resp.status_code) expected = [] for s in fakes.volumes[tenant["id"]]: expected.append(("X-OCCI-Location", utils.join_url(self.application_url + "/", "storage/%s" % s["id"]))) self.assertExpectedResult(expected, resp)
def test_create_link_invalid_network(self): app = self.get_app() server_id = utils.join_url(self.application_url + "/", "compute/foo") headers = { 'Category': ('networkinterface;' 'scheme="http://schemas.ogf.org/occi/infrastructure#";' 'class="kind"'), 'X-OCCI-Attribute': ('occi.core.source="%s", ' 'occi.core.target="bar"') % server_id } req = self._build_req("/networklink", None, method="POST", headers=headers) resp = req.get_response(app) self.assertEqual(400, resp.status_code)
def test_create_link_invalid_network(self): app = self.get_app() server_id = utils.join_url(self.application_url + "/", "compute/foo") headers = { 'Category': ( 'networkinterface;' 'scheme="http://schemas.ogf.org/occi/infrastructure#";' 'class="kind"'), 'X-OCCI-Attribute': ('occi.core.source="%s", ' 'occi.core.target="bar"' ) % server_id } req = self._build_req("/networklink", None, method="POST", headers=headers) resp = req.get_response(app) self.assertEqual(400, resp.status_code)
def test_list(self): tenant = fakes.tenants["baz"] for url in ("/ipreservation/", "/ipreservation"): req = self._build_req(url, tenant["id"], method="GET") resp = req.get_response(self.app) self.assertEqual(200, resp.status_code) expected = [] for ip in fakes.floating_ips[tenant["id"]]: expected.append( ("X-OCCI-Location", utils.join_url(self.application_url + "/", "ipreservation/%s" % ip["id"])) ) self.assertExpectedResult(expected, resp)
def render(self, env={}): ret = [] contents = (self.obj.kinds, self.obj.mixins, self.obj.actions, self.obj.resources, self.obj.links) # Render individual objects if there are more that one type of objects # otherwise render as X-OCCI-Location headers if len([x for x in contents if x]) > 1: for what in contents: for el in what: renderer = get_renderer(el) ret.extend(renderer.render(env=env)) else: app_url = env.get("application_url", "") for what in contents: for el in what: url = utils.join_url(app_url, el.location) ret.append(('X-OCCI-Location', '%s' % url)) return ret
def test_list_pools_empty(self): tenant = fakes.tenants["bar"] app = self.get_app() for url in ("/network", "/network/"): req = self._build_req(url, tenant["id"], method="GET") m = mock.MagicMock() m.user.project_id = tenant["id"] req.environ["keystone.token_auth"] = m resp = req.get_response(app) expected = [("X-OCCI-Location", utils.join_url(self.application_url + "/", "network/fixed"))] self.assertDefaults(resp) self.assertExpectedResult(expected, resp) self.assertEqual(200, resp.status_code)
def test_list_ifaces(self): tenant = fakes.tenants["baz"] app = self.get_app() for url in ("/networklink/", "/networklink"): req = self._build_req(url, tenant["id"], method="GET") resp = req.get_response(app) self.assertEqual(200, resp.status_code) expected = [] for ip in fakes.floating_ips[tenant["id"]]: if ip["instance_id"] is not None: link_id = '_'.join([ip["instance_id"], ip["ip"]]) expected.append( ("X-OCCI-Location", utils.join_url(self.application_url + "/", "networklink/%s" % link_id))) self.assertExpectedResult(expected, resp)
def test_create_link_invalid(self): tenant = fakes.tenants["foo"] net_id = fakes.ports[tenant['id']][0]['net_id'] occi_net_id = utils.join_url(self.application_url + "/", "network/%s" % net_id) headers = { 'Category': ('networkinterface;' 'scheme="http://schemas.ogf.org/occi/infrastructure#";' 'class="kind"'), 'X-OCCI-Attribute': ('occi.core.source="foo", ' 'occi.core.target="%s"') % occi_net_id } req = self._build_req("/networklink", None, method="POST", headers=headers) resp = req.get_response(self.app) self.assertEqual(400, resp.status_code)
def test_list_attachments(self): tenant = fakes.tenants["baz"] app = self.get_app() for url in ("/storagelink/", "/storagelink"): req = self._build_req(url, tenant["id"], method="GET") resp = req.get_response(app) self.assertEqual(200, resp.status_code) expected = [] for v in fakes.volumes[tenant["id"]]: for a in v["attachments"]: link_id = '_'.join([a["serverId"], v["id"]]) expected.append( ("X-OCCI-Location", utils.join_url(self.application_url + "/", "storagelink/%s" % link_id))) self.assertExpectedResult(expected, resp)
def test_list_networks(self, m): tenant = fakes.tenants["foo"] out = fakes.create_fake_json_resp( {"networks": fakes.networks[tenant['id']]}, 200) m.return_value.get_response.return_value = out req = self._build_req(path="/network", tenant_id='X', method="GET") resp = req.get_response(self.app) self.assertEqual(200, resp.status_code) expected = [] for s in fakes.networks[tenant["id"]]: expected.append( ("X-OCCI-Location", utils.join_url(self.application_url + "/", "network/%s" % s["id"])) ) self.assertDefaults(resp) self.assertExpectedResult(expected, resp)
def test_create(self): tenant = fakes.tenants["baz"] ip_id = fakes.allocated_ip["id"] headers = { 'Category': 'ipreservation;' ' scheme=' '"http://schemas.ogf.org/occi/infrastructure#";' 'class="kind",' } req = self._build_req("/ipreservation/", tenant["id"], method="POST", headers=headers) resp = req.get_response(self.app) expected = [("X-OCCI-Location", utils.join_url(self.application_url + "/", "ipreservation/%s" % ip_id))] self.assertEqual(200, resp.status_code) self.assertExpectedResult(expected, resp)
def test_create_link_invalid(self, m_create): tenant = fakes.tenants["foo"] m_create.side_effect = exception.Invalid net_id = fakes.network_links[tenant['id']][0]['network_id'] occi_net_id = utils.join_url(self.application_url + "/", "network/%s" % net_id) headers = { 'Category': ( 'networkinterface;' 'scheme="http://schemas.ogf.org/occi/infrastructure#";' 'class="kind"'), 'X-OCCI-Attribute': ( 'occi.core.source="foo", ' 'occi.core.target="%s"' ) % occi_net_id } req = self._build_req("/networklink", None, method="POST", headers=headers) resp = req.get_response(self.app) self.assertEqual(400, resp.status_code)
def test_list_attachments(self): tenant = fakes.tenants["baz"] app = self.get_app() for url in ("/storagelink/", "/storagelink"): req = self._build_req(url, tenant["id"], method="GET") resp = req.get_response(app) self.assertEqual(200, resp.status_code) expected = [] for v in fakes.volumes[tenant["id"]]: for a in v["attachments"]: link_id = '_'.join([a["serverId"], v["id"]]) expected.append( ("X-OCCI-Location", utils.join_url(self.application_url + "/", "storagelink/%s" % link_id)) ) self.assertExpectedResult(expected, resp)