Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 1 | import json |
| 2 | import os |
Soren Hansen | a86180a | 2011-09-09 16:22:26 +0200 | [diff] [blame] | 3 | import re |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 4 | |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 5 | from kong import openstack |
Soren Hansen | 6adacc8 | 2011-09-09 13:34:35 +0200 | [diff] [blame] | 6 | from kong import tests |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 7 | |
| 8 | |
Soren Hansen | 6adacc8 | 2011-09-09 13:34:35 +0200 | [diff] [blame] | 9 | class FlavorsTest(tests.FunctionalTest): |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 10 | def setUp(self): |
Soren Hansen | 6adacc8 | 2011-09-09 13:34:35 +0200 | [diff] [blame] | 11 | super(FlavorsTest, self).setUp() |
Soren Hansen | d6b047a | 2011-09-09 13:39:32 +0200 | [diff] [blame] | 12 | self.os = openstack.Manager(self.nova) |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 13 | |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 14 | 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 Waldon | 86fd8bc | 2011-10-27 12:35:44 -0400 | [diff] [blame] | 38 | 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 Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 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']) |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 51 | mgmt_url = self.os.nova.management_url |
| 52 | bmk_url = re.sub(r'v1.1\/', r'', mgmt_url) |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 53 | |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 54 | self_link = os.path.join(mgmt_url, 'flavors', flavor_id) |
| 55 | bookmark_link = os.path.join(bmk_url, 'flavors', flavor_id) |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 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) |
Soren Hansen | fce58c5 | 2011-09-09 16:07:13 +0200 | [diff] [blame] | 78 | test_show_flavor.tags = ['nova', 'glance'] |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 79 | |
| 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 Hansen | fce58c5 | 2011-09-09 16:07:13 +0200 | [diff] [blame] | 87 | test_index_flavors_basic.tags = ['nova', 'glance'] |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 88 | |
| 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 Hansen | fce58c5 | 2011-09-09 16:07:13 +0200 | [diff] [blame] | 101 | test_index_flavors_detailed.tags = ['nova', 'glance'] |