From a8cfcb58e8cda3e36cba5569b051689ced612925 Mon Sep 17 00:00:00 2001 From: Enol Fernandez Date: Mon, 13 Feb 2017 07:09:55 +0000 Subject: [PATCH] Do not crash on volume attachments without device Sometimes OpenStack does not return any device in the attached volumes, this change avoids a KeyError when not defined. Change-Id: If96fa02a7d6fd93e31667b2481943c6294173f72 Closes-Bug: #1663491 --- ooi/api/compute.py | 3 ++- ooi/api/storage_link.py | 5 +++-- ooi/tests/fakes.py | 18 +++++++++++++++++- ooi/tests/unit/controllers/test_compute.py | 9 ++++++--- 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/ooi/api/compute.py b/ooi/api/compute.py index 61b28fd..04099d2 100644 --- a/ooi/api/compute.py +++ b/ooi/api/compute.py @@ -268,7 +268,8 @@ class Controller(ooi.api.base.Controller): for v in vols: st = storage.StorageResource(title="storage", id=v["volumeId"]) comp.add_link(storage_link.StorageLink(comp, st, - deviceid=v["device"])) + deviceid=v.get("device", + None))) # network links addresses = s.get("addresses", {}) diff --git a/ooi/api/storage_link.py b/ooi/api/storage_link.py index 8572b14..78e4e72 100644 --- a/ooi/api/storage_link.py +++ b/ooi/api/storage_link.py @@ -40,7 +40,8 @@ class Controller(base.Controller): id=attach["serverId"]) s = storage.StorageResource(title="Storage", id=v["id"]) l = storage_link.StorageLink(c, s, - deviceid=attach["device"]) + deviceid=attach.get("device", + None)) occi_link_resources.append(l) return collection.Collection(resources=occi_link_resources) @@ -61,7 +62,7 @@ class Controller(base.Controller): v = self._get_attachment_from_id(req, id) c = compute.ComputeResource(title="Compute", id=v["serverId"]) s = storage.StorageResource(title="Storage", id=v["volumeId"]) - return storage_link.StorageLink(c, s, deviceid=v["device"]) + return storage_link.StorageLink(c, s, deviceid=v.get("device", None)) def create(self, req, body): parser = req.get_parser()(req.headers, req.body) diff --git a/ooi/tests/fakes.py b/ooi/tests/fakes.py index 58205f4..953cf47 100644 --- a/ooi/tests/fakes.py +++ b/ooi/tests/fakes.py @@ -95,6 +95,12 @@ volumes = { "size": 5, "status": "in-use", }, + { + "id": uuid.uuid4().hex, + "displayName": "volume-nodev", + "size": 6, + "status": "in-use", + }, ], } @@ -213,7 +219,8 @@ servers = { "image": {"id": images["bar"]["id"]}, "status": "ACTIVE", "os-extended-volumes:volumes_attached": [ - {"id": volumes[tenants["baz"]["id"]][0]["id"]} + {"id": volumes[tenants["baz"]["id"]][0]["id"]}, + {"id": volumes[tenants["baz"]["id"]][1]["id"]} ], "addresses": { "private": [ @@ -249,6 +256,15 @@ volumes[tenants["baz"]["id"]][0]["attachments"] = [{ "id": volumes[tenants["baz"]["id"]][0]["id"], }] +volumes[tenants["baz"]["id"]][1]["attachments"] = [{ + "server_id": servers[tenants["baz"]["id"]][0]["id"], + "serverId": servers[tenants["baz"]["id"]][0]["id"], + "attachment_id": uuid.uuid4().hex, + "volumeId": volumes[tenants["baz"]["id"]][1]["id"], + "volume_id": volumes[tenants["baz"]["id"]][1]["id"], + "id": volumes[tenants["baz"]["id"]][0]["id"], +}] + def fake_query_results(): cats = [] diff --git a/ooi/tests/unit/controllers/test_compute.py b/ooi/tests/unit/controllers/test_compute.py index dba009a..ff7b413 100644 --- a/ooi/tests/unit/controllers/test_compute.py +++ b/ooi/tests/unit/controllers/test_compute.py @@ -186,8 +186,11 @@ class TestComputeController(base.TestController): flavor = fakes.flavors[server["flavor"]["id"]] image = fakes.images[server["image"]["id"]] volumes = fakes.volumes.get(tenant["id"], []) - if volumes: - volumes = volumes[0]["attachments"] + attachments = [] + for v in volumes: + for att in v["attachments"]: + if att["server_id"] == server["id"]: + attachments.append(att) net_id = fakes.networks.get(tenant["id"], []) if net_id: net_id = net_id[0]['id'] @@ -195,7 +198,7 @@ class TestComputeController(base.TestController): m_server.return_value = server m_flavor.return_value = flavor m_image.return_value = image - m_vol.return_value = volumes + m_vol.return_value = attachments ret = self.controller.show(None, server["id"]) # FIXME(aloga): Should we test the resource?