blob: adcdcadf6d5bb62217ab01925b1eccd78ade8a89 [file] [log] [blame]
Soren Hansenbc1d3a02011-09-08 13:33:17 +02001import json
2import os
Soren Hansena86180a2011-09-09 16:22:26 +02003import re
Soren Hansenbc1d3a02011-09-08 13:33:17 +02004
Soren Hansenbc1d3a02011-09-08 13:33:17 +02005from kong import openstack
Soren Hansen6adacc82011-09-09 13:34:35 +02006from kong import tests
Soren Hansenbc1d3a02011-09-08 13:33:17 +02007
8
Soren Hansen6adacc82011-09-09 13:34:35 +02009class FlavorsTest(tests.FunctionalTest):
Soren Hansenbc1d3a02011-09-08 13:33:17 +020010 def setUp(self):
Soren Hansen6adacc82011-09-09 13:34:35 +020011 super(FlavorsTest, self).setUp()
Soren Hansend6b047a2011-09-09 13:39:32 +020012 self.os = openstack.Manager(self.nova)
Soren Hansenbc1d3a02011-09-08 13:33:17 +020013
Soren Hansenbc1d3a02011-09-08 13:33:17 +020014 def _index_flavors(self):
15 url = '/flavors'
16 response, body = self.os.nova.request('GET', url)
17 self.assertEqual(response['status'], '200')
18 body_dict = json.loads(body)
19 self.assertEqual(body_dict.keys(), ['flavors'])
20 return body_dict['flavors']
21
22 def _show_flavor(self, flavor_id):
23 url = '/flavors/%s' % flavor_id
24 response, body = self.os.nova.request('GET', url)
25 self.assertEqual(response['status'], '200')
26 body_dict = json.loads(body)
27 self.assertEqual(body_dict.keys(), ['flavor'])
28 return body_dict['flavor']
29
30 def _assert_flavor_entity_basic(self, flavor):
31 actual_keys = set(flavor.keys())
32 expected_keys = set(('id', 'name', 'links'))
33 self.assertEqual(actual_keys, expected_keys)
34 self._assert_flavor_links(flavor)
35
36 def _assert_flavor_entity_detailed(self, flavor):
37 actual_keys = set(flavor.keys())
Brian Waldon86fd8bc2011-10-27 12:35:44 -040038 expected_keys = set(('id', 'name', 'ram', 'disk', 'vcpus', 'links'))
39 #NOTE(bcwaldon): We cannot expect an explicit list of keys since
40 # any extension can a new attribute to a flavor entity. However, we
41 # can expect a minimum set of keys defined in the spec.
42 self.assertTrue(actual_keys >= expected_keys)
Soren Hansenbc1d3a02011-09-08 13:33:17 +020043 self.assertEqual(type(flavor['ram']), int)
44 self.assertEqual(type(flavor['disk']), int)
45 self._assert_flavor_links(flavor)
46
47 def _assert_flavor_links(self, flavor):
48 actual_links = flavor['links']
49
50 flavor_id = str(flavor['id'])
Soren Hansen5f4ad832011-09-09 14:08:19 +020051 mgmt_url = self.os.nova.management_url
52 bmk_url = re.sub(r'v1.1\/', r'', mgmt_url)
Soren Hansenbc1d3a02011-09-08 13:33:17 +020053
Soren Hansen5f4ad832011-09-09 14:08:19 +020054 self_link = os.path.join(mgmt_url, 'flavors', flavor_id)
55 bookmark_link = os.path.join(bmk_url, 'flavors', flavor_id)
Soren Hansenbc1d3a02011-09-08 13:33:17 +020056
57 expected_links = [
58 {
59 'rel': 'self',
60 'href': self_link,
61 },
62 {
63 'rel': 'bookmark',
64 'href': bookmark_link,
65 },
66 ]
67
68 self.assertEqual(actual_links, expected_links)
69
70 def test_show_flavor(self):
71 """Retrieve a single flavor"""
72
73 flavors = self._index_flavors()
74
75 for flavor in flavors:
76 detailed_flavor = self._show_flavor(flavor['id'])
77 self._assert_flavor_entity_detailed(detailed_flavor)
Soren Hansenfce58c52011-09-09 16:07:13 +020078 test_show_flavor.tags = ['nova', 'glance']
Soren Hansenbc1d3a02011-09-08 13:33:17 +020079
80 def test_index_flavors_basic(self):
81 """List all flavors"""
82
83 flavors = self._index_flavors()
84
85 for flavor in flavors:
86 self._assert_flavor_entity_basic(flavor)
Soren Hansenfce58c52011-09-09 16:07:13 +020087 test_index_flavors_basic.tags = ['nova', 'glance']
Soren Hansenbc1d3a02011-09-08 13:33:17 +020088
89 def test_index_flavors_detailed(self):
90 """List all flavors in detail"""
91
92 url = '/flavors/detail'
93 response, body = self.os.nova.request('GET', url)
94 self.assertEqual(response['status'], '200')
95 body_dict = json.loads(body)
96 self.assertEqual(body_dict.keys(), ['flavors'])
97 flavors = body_dict['flavors']
98
99 for flavor in flavors:
100 self._assert_flavor_entity_detailed(flavor)
Soren Hansenfce58c52011-09-09 16:07:13 +0200101 test_index_flavors_detailed.tags = ['nova', 'glance']