Skip to content

Commit

Permalink
Added compute mixins.
Browse files Browse the repository at this point in the history
Fix tests.

Use a dictionary for the responses of the fake app.

Moved function to openstack.helpers.
  • Loading branch information
enolfc committed Mar 17, 2015
1 parent 1a99f34 commit b7a66ad
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 32 deletions.
41 changes: 29 additions & 12 deletions ooi/api/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,8 @@
from ooi.api import base
from ooi.occi.core import collection
from ooi.occi.infrastructure import compute

# TODO(enolfc): move this function elsewhere? Check the correct names
# of nova states
def map_occi_state(nova_status):
if nova_status in ["ACTIVE"]:
return "active"
elif nova_status in ["PAUSED", "SUSPENDED", "STOPPED"]:
return "suspended"
else:
return "inactive"
from ooi.openstack import helpers
from ooi.openstack import templates


class Controller(base.Controller):
Expand All @@ -46,11 +38,36 @@ def index(self, req):

def show(self, id, req):
tenant_id = req.environ["keystone.token_auth"].user.project_id

# get info from server
req = self._get_req(req, path="/%s/servers/%s" % (tenant_id, id))
response = req.get_response(self.app)
s = response.json_body.get("server", {})

# get info from flavor
req = self._get_req(req, path="/%s/flavors/%s" % (tenant_id,
s["flavor"]["id"]))
response = req.get_response(self.app)
flavor = response.json_body.get("flavor", {})
res_tpl = templates.OpenStackResourceTemplate(flavor["name"],
flavor["vcpus"],
flavor["ram"],
flavor["disk"])

# get info from image
req = self._get_req(req, path="/%s/images/%s" % (tenant_id,
s["image"]["id"]))
response = req.get_response(self.app)
image = response.json_body.get("image", {})
os_tpl = templates.OpenStackOSTemplate(image["id"],
image["name"])

s = response.json_body.get("server", [])
# build the compute object
# TODO(enolfc): link to network + storage
comp = compute.ComputeResource(title=s["name"], id=s["id"],
cores=flavor["vcpus"],
hostname=s["name"],
state=map_occi_state(s["status"]))
memory=flavor["ram"],
state=helpers.occi_state(s["status"]),
mixins=[os_tpl, res_tpl])
return [comp]
4 changes: 1 addition & 3 deletions ooi/occi/infrastructure/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ class ComputeResource(resource.Resource):

def __init__(self, title, summary=None, id=None, architecture=None,
cores=None, hostname=None, speed=None, memory=None,
state=None):

mixins = []
state=None, mixins=[]):

super(ComputeResource, self).__init__(title, mixins, summary=summary,
id=id)
Expand Down
10 changes: 10 additions & 0 deletions ooi/openstack/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@

def build_scheme(category):
return helpers.build_scheme(category, prefix=_PREFIX)


# TODO(enolfc): Check the correct names of nova states
def occi_state(nova_status):
if nova_status in ["ACTIVE"]:
return "active"
elif nova_status in ["PAUSED", "SUSPENDED", "STOPPED"]:
return "suspended"
else:
return "inactive"
43 changes: 26 additions & 17 deletions ooi/tests/test_compute_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
def fake_app(resp):
@webob.dec.wsgify
def app(req):
return resp
return resp[req.path_info]
return app


Expand All @@ -41,24 +41,25 @@ def create_fake_json_resp(data):
return r


# TODO(enolfc): split tests? i.e. one test to check that the correct
# PATH_INFO, other for correct output (not text, but objects)
# TODO(enolfc): this should check the resulting obects, not the text.
class TestComputeMiddleware(base.TestCase):
def test_list_vms_empty(self):
tenant = uuid.uuid4().hex
d = {"servers": []}
fake_resp = create_fake_json_resp(d)
fake_resp = {
'/%s/servers' % tenant: create_fake_json_resp(d),
}

app = wsgi.OCCIMiddleware(fake_app(fake_resp))
req = webob.Request.blank("/compute", method="GET")

m = mock.MagicMock()
m.user.project_id = "3dd7b3f6-c19d-11e4-8dfc-aa07a5b093db"
m.user.project_id = tenant
req.environ["keystone.token_auth"] = m

resp = req.get_response(app)

self.assertEqual("/3dd7b3f6-c19d-11e4-8dfc-aa07a5b093db/servers",
req.environ["PATH_INFO"])
self.assertEqual("/%s/servers" % tenant, req.environ["PATH_INFO"])

self.assertEqual(200, resp.status_code)
self.assertEqual("", resp.text)
Expand All @@ -70,7 +71,9 @@ def test_list_vms_one_vm(self):
{"id": uuid.uuid4().hex, "name": "bar"},
{"id": uuid.uuid4().hex, "name": "baz"}]}

fake_resp = create_fake_json_resp(d)
fake_resp = {
'/%s/servers' % tenant: create_fake_json_resp(d),
}

app = wsgi.OCCIMiddleware(fake_app(fake_resp))
req = webob.Request.blank("/compute", method="GET")
Expand All @@ -90,16 +93,25 @@ def test_list_vms_one_vm(self):

def test_show_vm(self):
tenant = uuid.uuid4().hex

server_id = uuid.uuid4().hex
d = {"server": {"id": server_id,
s = {"server": {"id": server_id,
"name": "foo",
"flavor": {"id": "1"},
"image": {"id": uuid.uuid4().hex},
"image": {"id": "2"},
"status": "ACTIVE"}}

fake_resp = create_fake_json_resp(d)

f = {"flavor": {"id": 1,
"name": "foo",
"vcpus": 2,
"ram": 256,
"disk": 10}}
i = {"image": {"id": 2,
"name": "bar"}}

fake_resp = {
'/%s/servers/%s' % (tenant, server_id): create_fake_json_resp(s),
'/%s/flavors/1' % tenant: create_fake_json_resp(f),
'/%s/images/2' % tenant: create_fake_json_resp(i),
}
app = wsgi.OCCIMiddleware(fake_app(fake_resp))
req = webob.Request.blank("/compute/%s" % server_id, method="GET")

Expand All @@ -109,9 +121,6 @@ def test_show_vm(self):

resp = req.get_response(app)

self.assertEqual("/%s/servers/%s" % (tenant, server_id),
req.environ["PATH_INFO"])

self.assertEqual(200, resp.status_code)
expected = 'X-OCCI-Attribute: occi.core.id="%s"' % server_id
self.assertIn(expected, resp.text)
15 changes: 15 additions & 0 deletions ooi/tests/test_occi.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ def set_val():

self.assertRaises(AttributeError, set_val)

def test_as_str(self):
attr = attribute.MutableAttribute("occi.foo.bar", "bar")
self.assertEqual('"bar"', attr._as_str())
attr.value(True)
self.assertEqual('"true"', attr._as_str())
attr.value(False)
self.assertEqual('"false"', attr._as_str())
attr.value(4.5)
self.assertEqual("4.5", attr._as_str())


class TestAttributeCollection(base.TestCase):
def test_collection(self):
Expand Down Expand Up @@ -109,6 +119,11 @@ def test_obj(self):
for i in self.args:
self.assertEqual(i, getattr(cat, i))

def test_str(self):
cat = self.obj(*self.args)

self.assertRaises(ValueError, cat.__str__)


class TestCoreOCCIKind(TestCoreOCCICategory):
obj = kind.Kind
Expand Down
9 changes: 9 additions & 0 deletions ooi/tests/test_openstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,12 @@ def test_resource_template(self):
self.assertEqual(disk, tpl.disk)
self.assertEqual(swap, tpl.swap)
self.assertEqual(ephemeral, tpl.ephemeral)


class TestHelpers(base.TestCase):
def test_occi_state(self):
self.assertEqual("active", helpers.occi_state("ACTIVE"))
self.assertEqual("suspended", helpers.occi_state("PAUSED"))
self.assertEqual("suspended", helpers.occi_state("SUSPENDED"))
self.assertEqual("suspended", helpers.occi_state("STOPPED"))
self.assertEqual("inactive", helpers.occi_state("BUILDING"))

0 comments on commit b7a66ad

Please sign in to comment.