blob: 0fb3258011e54c50c95fd373256cca6eb90d3bcd [file] [log] [blame]
Soren Hansenbc1d3a02011-09-08 13:33:17 +02001import json
2import os
3
Soren Hansenbc1d3a02011-09-08 13:33:17 +02004from kong import openstack
Soren Hansen6adacc82011-09-09 13:34:35 +02005from kong import tests
Soren Hansenbc1d3a02011-09-08 13:33:17 +02006
7
Soren Hansen6adacc82011-09-09 13:34:35 +02008class FlavorsTest(tests.FunctionalTest):
Soren Hansenbc1d3a02011-09-08 13:33:17 +02009 def setUp(self):
Soren Hansen6adacc82011-09-09 13:34:35 +020010 super(FlavorsTest, self).setUp()
Soren Hansend6b047a2011-09-09 13:39:32 +020011 self.os = openstack.Manager(self.nova)
Soren Hansenbc1d3a02011-09-08 13:33:17 +020012
Soren Hansenbc1d3a02011-09-08 13:33:17 +020013 def _index_flavors(self):
14 url = '/flavors'
15 response, body = self.os.nova.request('GET', url)
16 self.assertEqual(response['status'], '200')
17 body_dict = json.loads(body)
18 self.assertEqual(body_dict.keys(), ['flavors'])
19 return body_dict['flavors']
20
21 def _show_flavor(self, flavor_id):
22 url = '/flavors/%s' % flavor_id
23 response, body = self.os.nova.request('GET', url)
24 self.assertEqual(response['status'], '200')
25 body_dict = json.loads(body)
26 self.assertEqual(body_dict.keys(), ['flavor'])
27 return body_dict['flavor']
28
29 def _assert_flavor_entity_basic(self, flavor):
30 actual_keys = set(flavor.keys())
31 expected_keys = set(('id', 'name', 'links'))
32 self.assertEqual(actual_keys, expected_keys)
33 self._assert_flavor_links(flavor)
34
35 def _assert_flavor_entity_detailed(self, flavor):
36 actual_keys = set(flavor.keys())
37 expected_keys = set(('id', 'name', 'ram', 'disk', 'links'))
38 self.assertEqual(actual_keys, expected_keys)
39 self.assertEqual(type(flavor['ram']), int)
40 self.assertEqual(type(flavor['disk']), int)
41 self._assert_flavor_links(flavor)
42
43 def _assert_flavor_links(self, flavor):
44 actual_links = flavor['links']
45
46 flavor_id = str(flavor['id'])
47 host = self.os.config.nova.host
48 port = self.os.config.nova.port
49 api_url = '%s:%s' % (host, port)
50 base_url = os.path.join(api_url, self.os.config.nova.base_url,
51 self.os.config.nova.project_id)
52 api_url = os.path.join(api_url, self.os.config.nova.project_id)
53
54 self_link = 'http://' + os.path.join(base_url, 'flavors', flavor_id)
55 bookmark_link = 'http://' + os.path.join(api_url, 'flavors', flavor_id)
56
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)
78
79 def test_index_flavors_basic(self):
80 """List all flavors"""
81
82 flavors = self._index_flavors()
83
84 for flavor in flavors:
85 self._assert_flavor_entity_basic(flavor)
86
87 def test_index_flavors_detailed(self):
88 """List all flavors in detail"""
89
90 url = '/flavors/detail'
91 response, body = self.os.nova.request('GET', url)
92 self.assertEqual(response['status'], '200')
93 body_dict = json.loads(body)
94 self.assertEqual(body_dict.keys(), ['flavors'])
95 flavors = body_dict['flavors']
96
97 for flavor in flavors:
98 self._assert_flavor_entity_detailed(flavor)