summaryrefslogtreecommitdiff
path: root/novaclient/tests/unit/v1_1/test_flavors.py
blob: 90f7eb1d9ae5ca59a8924c9aa361a7a61df5be55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# Copyright (c) 2013, OpenStack
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import mock

from novaclient import exceptions
from novaclient.tests.unit import utils
from novaclient.tests.unit.v1_1 import fakes
from novaclient.v1_1 import flavors


class FlavorsTest(utils.TestCase):
    def setUp(self):
        super(FlavorsTest, self).setUp()
        self.cs = self._get_fake_client()
        self.flavor_type = self._get_flavor_type()

    def _get_fake_client(self):
        return fakes.FakeClient()

    def _get_flavor_type(self):
        return flavors.Flavor

    def test_list_flavors(self):
        fl = self.cs.flavors.list()
        self.cs.assert_called('GET', '/flavors/detail')
        for flavor in fl:
            self.assertIsInstance(flavor, self.flavor_type)

    def test_list_flavors_undetailed(self):
        fl = self.cs.flavors.list(detailed=False)
        self.cs.assert_called('GET', '/flavors')
        for flavor in fl:
            self.assertIsInstance(flavor, self.flavor_type)

    def test_list_flavors_is_public_none(self):
        fl = self.cs.flavors.list(is_public=None)
        self.cs.assert_called('GET', '/flavors/detail?is_public=None')
        for flavor in fl:
            self.assertIsInstance(flavor, self.flavor_type)

    def test_list_flavors_is_public_false(self):
        fl = self.cs.flavors.list(is_public=False)
        self.cs.assert_called('GET', '/flavors/detail?is_public=False')
        for flavor in fl:
            self.assertIsInstance(flavor, self.flavor_type)

    def test_list_flavors_is_public_true(self):
        fl = self.cs.flavors.list(is_public=True)
        self.cs.assert_called('GET', '/flavors/detail')
        for flavor in fl:
            self.assertIsInstance(flavor, self.flavor_type)

    def test_get_flavor_details(self):
        f = self.cs.flavors.get(1)
        self.cs.assert_called('GET', '/flavors/1')
        self.assertIsInstance(f, self.flavor_type)
        self.assertEqual(256, f.ram)
        self.assertEqual(10, f.disk)
        self.assertEqual(10, f.ephemeral)
        self.assertEqual(True, f.is_public)

    def test_get_flavor_details_alphanum_id(self):
        f = self.cs.flavors.get('aa1')
        self.cs.assert_called('GET', '/flavors/aa1')
        self.assertIsInstance(f, self.flavor_type)
        self.assertEqual(128, f.ram)
        self.assertEqual(0, f.disk)
        self.assertEqual(0, f.ephemeral)
        self.assertEqual(True, f.is_public)

    def test_get_flavor_details_diablo(self):
        f = self.cs.flavors.get(3)
        self.cs.assert_called('GET', '/flavors/3')
        self.assertIsInstance(f, self.flavor_type)
        self.assertEqual(256, f.ram)
        self.assertEqual(10, f.disk)
        self.assertEqual('N/A', f.ephemeral)
        self.assertEqual('N/A', f.is_public)

    def test_find(self):
        f = self.cs.flavors.find(ram=256)
        self.cs.assert_called('GET', '/flavors/detail')
        self.assertEqual('256 MB Server', f.name)

        f = self.cs.flavors.find(disk=0)
        self.assertEqual('128 MB Server', f.name)

        self.assertRaises(exceptions.NotFound, self.cs.flavors.find,
                          disk=12345)

    def _create_body(self, name, ram, vcpus, disk, ephemeral, id, swap,
                     rxtx_factor, is_public):
        return {
            "flavor": {
                "name": name,
                "ram": ram,
                "vcpus": vcpus,
                "disk": disk,
                "OS-FLV-EXT-DATA:ephemeral": ephemeral,
                "id": id,
                "swap": swap,
                "rxtx_factor": rxtx_factor,
                "os-flavor-access:is_public": is_public,
            }
        }

    def test_create(self):
        f = self.cs.flavors.create("flavorcreate", 512, 1, 10, 1234,
                                   ephemeral=10, is_public=False)

        body = self._create_body("flavorcreate", 512, 1, 10, 10, 1234, 0, 1.0,
                                 False)

        self.cs.assert_called('POST', '/flavors', body)
        self.assertIsInstance(f, self.flavor_type)

    def test_create_with_id_as_string(self):
        flavor_id = 'foobar'
        f = self.cs.flavors.create("flavorcreate", 512,
                                   1, 10, flavor_id, ephemeral=10,
                                   is_public=False)

        body = self._create_body("flavorcreate", 512, 1, 10, 10, flavor_id, 0,
                                 1.0, False)

        self.cs.assert_called('POST', '/flavors', body)
        self.assertIsInstance(f, self.flavor_type)

    def test_create_ephemeral_ispublic_defaults(self):
        f = self.cs.flavors.create("flavorcreate", 512, 1, 10, 1234)

        body = self._create_body("flavorcreate", 512, 1, 10, 0, 1234, 0,
                                 1.0, True)

        self.cs.assert_called('POST', '/flavors', body)
        self.assertIsInstance(f, self.flavor_type)

    def test_invalid_parameters_create(self):
        self.assertRaises(exceptions.CommandError, self.cs.flavors.create,
                          "flavorcreate", "invalid", 1, 10, 1234, swap=0,
                          ephemeral=0, rxtx_factor=1.0, is_public=True)
        self.assertRaises(exceptions.CommandError, self.cs.flavors.create,
                          "flavorcreate", 512, "invalid", 10, 1234, swap=0,
                          ephemeral=0, rxtx_factor=1.0, is_public=True)
        self.assertRaises(exceptions.CommandError, self.cs.flavors.create,
                          "flavorcreate", 512, 1, "invalid", 1234, swap=0,
                          ephemeral=0, rxtx_factor=1.0, is_public=True)
        self.assertRaises(exceptions.CommandError, self.cs.flavors.create,
                          "flavorcreate", 512, 1, 10, 1234, swap="invalid",
                          ephemeral=0, rxtx_factor=1.0, is_public=True)
        self.assertRaises(exceptions.CommandError, self.cs.flavors.create,
                          "flavorcreate", 512, 1, 10, 1234, swap=0,
                          ephemeral="invalid", rxtx_factor=1.0, is_public=True)
        self.assertRaises(exceptions.CommandError, self.cs.flavors.create,
                          "flavorcreate", 512, 1, 10, 1234, swap=0,
                          ephemeral=0, rxtx_factor="invalid", is_public=True)
        self.assertRaises(exceptions.CommandError, self.cs.flavors.create,
                          "flavorcreate", 512, 1, 10, 1234, swap=0,
                          ephemeral=0, rxtx_factor=1.0, is_public='invalid')

    def test_delete(self):
        self.cs.flavors.delete("flavordelete")
        self.cs.assert_called('DELETE', '/flavors/flavordelete')

    def test_delete_with_flavor_instance(self):
        f = self.cs.flavors.get(2)
        self.cs.flavors.delete(f)
        self.cs.assert_called('DELETE', '/flavors/2')

    def test_delete_with_flavor_instance_method(self):
        f = self.cs.flavors.get(2)
        f.delete()
        self.cs.assert_called('DELETE', '/flavors/2')

    def test_set_keys(self):
        f = self.cs.flavors.get(1)
        f.set_keys({'k1': 'v1'})
        self.cs.assert_called('POST', '/flavors/1/os-extra_specs',
                              {"extra_specs": {'k1': 'v1'}})

    def test_set_with_valid_keys(self):
        valid_keys = ['key4', 'month.price', 'I-Am:AK-ey.44-',
                      'key with spaces and _']

        f = self.cs.flavors.get(4)
        for key in valid_keys:
            f.set_keys({key: 'v4'})
            self.cs.assert_called('POST', '/flavors/4/os-extra_specs',
                                  {"extra_specs": {key: 'v4'}})

    def test_set_with_invalid_keys(self):
        invalid_keys = ['/1', '?1', '%1', '<', '>']

        f = self.cs.flavors.get(1)
        for key in invalid_keys:
            self.assertRaises(exceptions.CommandError, f.set_keys, {key: 'v1'})

    @mock.patch.object(flavors.FlavorManager, '_delete')
    def test_unset_keys(self, mock_delete):
        f = self.cs.flavors.get(1)
        keys = ['k1', 'k2']
        f.unset_keys(keys)
        mock_delete.assert_has_calls([
            mock.call("/flavors/1/os-extra_specs/k1"),
            mock.call("/flavors/1/os-extra_specs/k2")
        ])