blob: c7175302947c8caf43a83cc3cea4de539f6ce39c [file] [log] [blame]
Soren Hansenbc1d3a02011-09-08 13:33:17 +02001import json
2import os
3
4import unittest2 as unittest
5
6from kong import openstack
7
8
9class FlavorsTest(unittest.TestCase):
10
11 def setUp(self):
12 self.os = openstack.Manager()
13
14 def tearDown(self):
15 pass
16
17 def _index_flavors(self):
18 url = '/flavors'
19 response, body = self.os.nova.request('GET', url)
20 self.assertEqual(response['status'], '200')
21 body_dict = json.loads(body)
22 self.assertEqual(body_dict.keys(), ['flavors'])
23 return body_dict['flavors']
24
25 def _show_flavor(self, flavor_id):
26 url = '/flavors/%s' % flavor_id
27 response, body = self.os.nova.request('GET', url)
28 self.assertEqual(response['status'], '200')
29 body_dict = json.loads(body)
30 self.assertEqual(body_dict.keys(), ['flavor'])
31 return body_dict['flavor']
32
33 def _assert_flavor_entity_basic(self, flavor):
34 actual_keys = set(flavor.keys())
35 expected_keys = set(('id', 'name', 'links'))
36 self.assertEqual(actual_keys, expected_keys)
37 self._assert_flavor_links(flavor)
38
39 def _assert_flavor_entity_detailed(self, flavor):
40 actual_keys = set(flavor.keys())
41 expected_keys = set(('id', 'name', 'ram', 'disk', 'links'))
42 self.assertEqual(actual_keys, expected_keys)
43 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'])
51 host = self.os.config.nova.host
52 port = self.os.config.nova.port
53 api_url = '%s:%s' % (host, port)
54 base_url = os.path.join(api_url, self.os.config.nova.base_url,
55 self.os.config.nova.project_id)
56 api_url = os.path.join(api_url, self.os.config.nova.project_id)
57
58 self_link = 'http://' + os.path.join(base_url, 'flavors', flavor_id)
59 bookmark_link = 'http://' + os.path.join(api_url, 'flavors', flavor_id)
60
61 expected_links = [
62 {
63 'rel': 'self',
64 'href': self_link,
65 },
66 {
67 'rel': 'bookmark',
68 'href': bookmark_link,
69 },
70 ]
71
72 self.assertEqual(actual_links, expected_links)
73
74 def test_show_flavor(self):
75 """Retrieve a single flavor"""
76
77 flavors = self._index_flavors()
78
79 for flavor in flavors:
80 detailed_flavor = self._show_flavor(flavor['id'])
81 self._assert_flavor_entity_detailed(detailed_flavor)
82
83 def test_index_flavors_basic(self):
84 """List all flavors"""
85
86 flavors = self._index_flavors()
87
88 for flavor in flavors:
89 self._assert_flavor_entity_basic(flavor)
90
91 def test_index_flavors_detailed(self):
92 """List all flavors in detail"""
93
94 url = '/flavors/detail'
95 response, body = self.os.nova.request('GET', url)
96 self.assertEqual(response['status'], '200')
97 body_dict = json.loads(body)
98 self.assertEqual(body_dict.keys(), ['flavors'])
99 flavors = body_dict['flavors']
100
101 for flavor in flavors:
102 self._assert_flavor_entity_detailed(flavor)