Debian Codegraph @

config_tempest/tests/test_flavors.py

raw
1 # Copyright 2018 Red Hat, Inc.
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
16 from unittest import mock
17
18 from fixtures import MonkeyPatch
19
20 from config_tempest import constants as C
21 from config_tempest.flavors import Flavors
22 from config_tempest.tests.base import BaseConfigTempestTest
23
24
25 class TestFlavors(BaseConfigTempestTest):
26 """Flavors test class
27
28 Tests for create_tempest_flavors and find_or_create_flavor methods.
29 """
30
31 CLIENT_MOCK = 'tempest.lib.services.compute.flavors_client.FlavorsClient'
32 FLAVORS_LIST = [
33 {"id": "Fakeid", "name": "Name"},
34 {"id": "MyFakeID", "name": "MyID"}
35 ]
36
37 def setUp(self):
38 super(TestFlavors, self).setUp()
39 self.conf = self._get_conf("v2.0", "v3")
40 self.client = self._get_clients(self.conf).flavors
41 return_value = {"flavors": [{"id": "MyFakeID", "name": "MyID"}]}
42 mock_function = mock.Mock(return_value=return_value)
43 self.useFixture(MonkeyPatch(self.CLIENT_MOCK + '.list_flavors',
44 mock_function))
45 self.Service = Flavors(self.client, True, self.conf, 128, 1)
46
47 def test_create_tempest_flavors(self):
48 self.Service.flavor_list = []
49 mock_function = mock.Mock(return_value="FakeID")
50 func2mock = 'config_tempest.flavors.Flavors.create_flavor'
51 self.useFixture(MonkeyPatch(func2mock, mock_function))
52 self.Service.create_tempest_flavors()
53 self.assertEqual(self.conf.get('compute', 'flavor_ref'), "FakeID")
54 self.assertEqual(self.conf.get('compute', 'flavor_ref_alt'), "FakeID")
55 calls = [mock.call('m1.nano', 128, 1, 1, no_rng=False),
56 mock.call('m1.micro', 192, 1, 1, no_rng=False)]
57 mock_function.assert_has_calls(calls, any_order=True)
58
59 def check_call_of_discover_smallest_flavor(self):
60 self.Service.flavor_list = [{'id': 'FAKE', 'name': 'Fake_flavor'},
61 {'id': 'FAKE_1', 'name': 'Fake_flavor_1'}]
62 self.Service.allow_creation = False
63 func2mock = 'config_tempest.flavors.Flavors.discover_smallest_flavor'
64 mock_function = mock.Mock()
65 self.useFixture(MonkeyPatch(func2mock, mock_function))
66 self.Service.create_flavor('nano', C.DEFAULT_FLAVOR_RAM,
67 C.DEFAULT_FLAVOR_VCPUS,
68 C.DEFAULT_FLAVOR_DISK)
69 calls = [mock.call('nano')]
70 mock_function.assert_has_calls(calls, any_order=True)
71
72 def test_create_tempest_flavors_overwrite_flavor_ref_not_exist(self):
73 self.conf.set('compute', 'flavor_ref', "FAKE_ID")
74 try:
75 self.Service.create_tempest_flavors()
76 except Exception:
77 return
78 # it should have ended in the except block above
79 self.assertTrue(False)
80
81 def test_create_tempest_flavors_overwrite_flavor_ref_alt_not_exist(self):
82 self.Service.flavor_list = [{'id': 'FAKE', 'name': 'Fake_flavor'}]
83 self.conf.set('compute', 'flavor_ref', 'FAKE')
84 self.conf.set('compute', 'flavor_ref_alt', 'FAKE_ID')
85 try:
86 self.Service.create_tempest_flavors()
87 except Exception:
88 self.assertEqual(self.conf.get('compute', 'flavor_ref'), 'FAKE')
89 return
90 # it should have ended in the except block above
91 self.assertTrue(False)
92
93 def test_create_flavor_not_allowed(self):
94 # mock list_flavors() to return empty list
95 self.Service.allow_creation = False
96 self.Service.flavor_list = []
97 try:
98 self.Service.create_flavor('name', C.DEFAULT_FLAVOR_RAM,
99 C.DEFAULT_FLAVOR_VCPUS,
100 C.DEFAULT_FLAVOR_DISK)
101 except Exception:
102 return
103 # it should have ended in the except block above
104 self.assertTrue(False)
105
106 # not enough flavors found
107 self.Service.flavor_list = [{'id': 'FAKE', 'name': 'fake_name'}]
108 try:
109 self.Service.create_flavor('name', C.DEFAULT_FLAVOR_RAM,
110 C.DEFAULT_FLAVOR_VCPUS,
111 C.DEFAULT_FLAVOR_DISK)
112 except Exception:
113 return
114 # it should have ended in the except block above
115 self.assertTrue(False)
116
117 def _test_create_flavor(self, no_rng=False):
118 return_value = {"flavor": {"id": "MyFakeID", "name": "MyID"}}
119 self.Service.flavor_list = []
120 client = self.CLIENT_MOCK
121 mock_function = mock.Mock(return_value=return_value)
122 self.useFixture(MonkeyPatch(client + '.create_flavor', mock_function))
123 mock_function = mock.Mock(return_value={})
124 self.useFixture(MonkeyPatch(client + '.set_flavor_extra_spec',
125 mock_function))
126 resp = self.Service.create_flavor("MyID", C.DEFAULT_FLAVOR_RAM,
127 C.DEFAULT_FLAVOR_VCPUS,
128 C.DEFAULT_FLAVOR_DISK,
129 no_rng=no_rng)
130 self.assertEqual(resp, return_value['flavor']['id'])
131 return mock_function
132
133 def test_create_flavor_rng(self):
134 mock_function = self._test_create_flavor()
135 calls = [mock.call(flavor_id='MyFakeID', **{'hw_rng:allowed': 'True'})]
136 mock_function.assert_has_calls(calls, any_order=True)
137
138 def test_create_flavor_no_rng(self):
139 self.Service.no_rng = True
140 mock_function = self._test_create_flavor(no_rng=True)
141 calls = [mock.call(flavor_id='MyFakeID')]
142 mock_function.assert_has_calls(calls, any_order=True)
143
144 def test_find_flavor_by_id(self):
145 return_value = {"flavors": self.FLAVORS_LIST}
146 mock_function = mock.Mock(return_value=return_value)
147 self.useFixture(MonkeyPatch(self.CLIENT_MOCK + '.list_flavors',
148 mock_function))
149 resp = self.Service.find_flavor_by_id("MyFakeID")
150 self.assertEqual(resp, "MyFakeID")
151 # test no flavor found case
152 resp = self.Service.find_flavor_by_id("NotExist")
153 self.assertEqual(resp, None)
154
155 def test_find_flavor_by_name(self):
156 return_value = {"flavors": self.FLAVORS_LIST}
157 mock_function = mock.Mock(return_value=return_value)
158 self.useFixture(MonkeyPatch(self.CLIENT_MOCK + '.list_flavors',
159 mock_function))
160 resp = self.Service.find_flavor_by_name("MyID")
161 self.assertEqual(resp, "MyFakeID")
162 # test no flavor found case
163 resp = self.Service.find_flavor_by_name("NotExist")
164 self.assertEqual(resp, None)