summaryrefslogtreecommitdiff
path: root/novaclient/tests/unit/v2/test_flavors.py
diff options
context:
space:
mode:
Diffstat (limited to 'novaclient/tests/unit/v2/test_flavors.py')
-rw-r--r--novaclient/tests/unit/v2/test_flavors.py219
1 files changed, 219 insertions, 0 deletions
diff --git a/novaclient/tests/unit/v2/test_flavors.py b/novaclient/tests/unit/v2/test_flavors.py
new file mode 100644
index 0000000..27b77c0
--- /dev/null
+++ b/novaclient/tests/unit/v2/test_flavors.py
@@ -0,0 +1,219 @@
1# Copyright (c) 2013, OpenStack
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
16import mock
17
18from novaclient import exceptions
19from novaclient.tests.unit import utils
20from novaclient.tests.unit.v2 import fakes
21from novaclient.v2 import flavors
22
23
24class FlavorsTest(utils.TestCase):
25 def setUp(self):
26 super(FlavorsTest, self).setUp()
27 self.cs = self._get_fake_client()
28 self.flavor_type = self._get_flavor_type()
29
30 def _get_fake_client(self):
31 return fakes.FakeClient()
32
33 def _get_flavor_type(self):
34 return flavors.Flavor
35
36 def test_list_flavors(self):
37 fl = self.cs.flavors.list()
38 self.cs.assert_called('GET', '/flavors/detail')
39 for flavor in fl:
40 self.assertIsInstance(flavor, self.flavor_type)
41
42 def test_list_flavors_undetailed(self):
43 fl = self.cs.flavors.list(detailed=False)
44 self.cs.assert_called('GET', '/flavors')
45 for flavor in fl:
46 self.assertIsInstance(flavor, self.flavor_type)
47
48 def test_list_flavors_is_public_none(self):
49 fl = self.cs.flavors.list(is_public=None)
50 self.cs.assert_called('GET', '/flavors/detail?is_public=None')
51 for flavor in fl:
52 self.assertIsInstance(flavor, self.flavor_type)
53
54 def test_list_flavors_is_public_false(self):
55 fl = self.cs.flavors.list(is_public=False)
56 self.cs.assert_called('GET', '/flavors/detail?is_public=False')
57 for flavor in fl:
58 self.assertIsInstance(flavor, self.flavor_type)
59
60 def test_list_flavors_is_public_true(self):
61 fl = self.cs.flavors.list(is_public=True)
62 self.cs.assert_called('GET', '/flavors/detail')
63 for flavor in fl:
64 self.assertIsInstance(flavor, self.flavor_type)
65
66 def test_get_flavor_details(self):
67 f = self.cs.flavors.get(1)
68 self.cs.assert_called('GET', '/flavors/1')
69 self.assertIsInstance(f, self.flavor_type)
70 self.assertEqual(256, f.ram)
71 self.assertEqual(10, f.disk)
72 self.assertEqual(10, f.ephemeral)
73 self.assertEqual(True, f.is_public)
74
75 def test_get_flavor_details_alphanum_id(self):
76 f = self.cs.flavors.get('aa1')
77 self.cs.assert_called('GET', '/flavors/aa1')
78 self.assertIsInstance(f, self.flavor_type)
79 self.assertEqual(128, f.ram)
80 self.assertEqual(0, f.disk)
81 self.assertEqual(0, f.ephemeral)
82 self.assertEqual(True, f.is_public)
83
84 def test_get_flavor_details_diablo(self):
85 f = self.cs.flavors.get(3)
86 self.cs.assert_called('GET', '/flavors/3')
87 self.assertIsInstance(f, self.flavor_type)
88 self.assertEqual(256, f.ram)
89 self.assertEqual(10, f.disk)
90 self.assertEqual('N/A', f.ephemeral)
91 self.assertEqual('N/A', f.is_public)
92
93 def test_find(self):
94 f = self.cs.flavors.find(ram=256)
95 self.cs.assert_called('GET', '/flavors/detail')
96 self.assertEqual('256 MB Server', f.name)
97
98 f = self.cs.flavors.find(disk=0)
99 self.assertEqual('128 MB Server', f.name)
100
101 self.assertRaises(exceptions.NotFound, self.cs.flavors.find,
102 disk=12345)
103
104 def _create_body(self, name, ram, vcpus, disk, ephemeral, id, swap,
105 rxtx_factor, is_public):
106 return {
107 "flavor": {
108 "name": name,
109 "ram": ram,
110 "vcpus": vcpus,
111 "disk": disk,
112 "OS-FLV-EXT-DATA:ephemeral": ephemeral,
113 "id": id,
114 "swap": swap,
115 "rxtx_factor": rxtx_factor,
116 "os-flavor-access:is_public": is_public,
117 }
118 }
119
120 def test_create(self):
121 f = self.cs.flavors.create("flavorcreate", 512, 1, 10, 1234,
122 ephemeral=10, is_public=False)
123
124 body = self._create_body("flavorcreate", 512, 1, 10, 10, 1234, 0, 1.0,
125 False)
126
127 self.cs.assert_called('POST', '/flavors', body)
128 self.assertIsInstance(f, self.flavor_type)
129
130 def test_create_with_id_as_string(self):
131 flavor_id = 'foobar'
132 f = self.cs.flavors.create("flavorcreate", 512,
133 1, 10, flavor_id, ephemeral=10,
134 is_public=False)
135
136 body = self._create_body("flavorcreate", 512, 1, 10, 10, flavor_id, 0,
137 1.0, False)
138
139 self.cs.assert_called('POST', '/flavors', body)
140 self.assertIsInstance(f, self.flavor_type)
141
142 def test_create_ephemeral_ispublic_defaults(self):
143 f = self.cs.flavors.create("flavorcreate", 512, 1, 10, 1234)
144
145 body = self._create_body("flavorcreate", 512, 1, 10, 0, 1234, 0,
146 1.0, True)
147
148 self.cs.assert_called('POST', '/flavors', body)
149 self.assertIsInstance(f, self.flavor_type)
150
151 def test_invalid_parameters_create(self):
152 self.assertRaises(exceptions.CommandError, self.cs.flavors.create,
153 "flavorcreate", "invalid", 1, 10, 1234, swap=0,
154 ephemeral=0, rxtx_factor=1.0, is_public=True)
155 self.assertRaises(exceptions.CommandError, self.cs.flavors.create,
156 "flavorcreate", 512, "invalid", 10, 1234, swap=0,
157 ephemeral=0, rxtx_factor=1.0, is_public=True)
158 self.assertRaises(exceptions.CommandError, self.cs.flavors.create,
159 "flavorcreate", 512, 1, "invalid", 1234, swap=0,
160 ephemeral=0, rxtx_factor=1.0, is_public=True)
161 self.assertRaises(exceptions.CommandError, self.cs.flavors.create,
162 "flavorcreate", 512, 1, 10, 1234, swap="invalid",
163 ephemeral=0, rxtx_factor=1.0, is_public=True)
164 self.assertRaises(exceptions.CommandError, self.cs.flavors.create,
165 "flavorcreate", 512, 1, 10, 1234, swap=0,
166 ephemeral="invalid", rxtx_factor=1.0, is_public=True)
167 self.assertRaises(exceptions.CommandError, self.cs.flavors.create,
168 "flavorcreate", 512, 1, 10, 1234, swap=0,
169 ephemeral=0, rxtx_factor="invalid", is_public=True)
170 self.assertRaises(exceptions.CommandError, self.cs.flavors.create,
171 "flavorcreate", 512, 1, 10, 1234, swap=0,
172 ephemeral=0, rxtx_factor=1.0, is_public='invalid')
173
174 def test_delete(self):
175 self.cs.flavors.delete("flavordelete")
176 self.cs.assert_called('DELETE', '/flavors/flavordelete')
177
178 def test_delete_with_flavor_instance(self):
179 f = self.cs.flavors.get(2)
180 self.cs.flavors.delete(f)
181 self.cs.assert_called('DELETE', '/flavors/2')
182
183 def test_delete_with_flavor_instance_method(self):
184 f = self.cs.flavors.get(2)
185 f.delete()
186 self.cs.assert_called('DELETE', '/flavors/2')
187
188 def test_set_keys(self):
189 f = self.cs.flavors.get(1)
190 f.set_keys({'k1': 'v1'})
191 self.cs.assert_called('POST', '/flavors/1/os-extra_specs',
192 {"extra_specs": {'k1': 'v1'}})
193
194 def test_set_with_valid_keys(self):
195 valid_keys = ['key4', 'month.price', 'I-Am:AK-ey.44-',
196 'key with spaces and _']
197
198 f = self.cs.flavors.get(4)
199 for key in valid_keys:
200 f.set_keys({key: 'v4'})
201 self.cs.assert_called('POST', '/flavors/4/os-extra_specs',
202 {"extra_specs": {key: 'v4'}})
203
204 def test_set_with_invalid_keys(self):
205 invalid_keys = ['/1', '?1', '%1', '<', '>']
206
207 f = self.cs.flavors.get(1)
208 for key in invalid_keys:
209 self.assertRaises(exceptions.CommandError, f.set_keys, {key: 'v1'})
210
211 @mock.patch.object(flavors.FlavorManager, '_delete')
212 def test_unset_keys(self, mock_delete):
213 f = self.cs.flavors.get(1)
214 keys = ['k1', 'k2']
215 f.unset_keys(keys)
216 mock_delete.assert_has_calls([
217 mock.call("/flavors/1/os-extra_specs/k1"),
218 mock.call("/flavors/1/os-extra_specs/k2")
219 ])