Debian Codegraph @

blazar_tempest_plugin/tests/scenario/test_host_reservation.py

raw
1 # Copyright 2017 NTT Corporation. All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 import datetime
16 import time
17
18 from oslo_log import log as logging
19 from tempest.common import waiters
20 from tempest import config
21 from tempest.lib import decorators
22 from tempest.lib import exceptions
23
24 from blazar_tempest_plugin.tests.scenario import (
25 resource_reservation_scenario as rrs)
26
27 CONF = config.CONF
28
29 LOG = logging.getLogger(__name__)
30
31
32 class TestHostReservationScenario(rrs.ResourceReservationScenarioTest):
33 """A Scenario test class that checks host reservation."""
34
35 MAX_RETRY = 20
36 WAIT_TIME = 2
37
38 def setUp(self):
39 super(TestHostReservationScenario, self).setUp()
40 self.aggr_client = self.os_admin.aggregates_client
41
42 def tearDown(self):
43 super(TestHostReservationScenario, self).tearDown()
44
45 def get_lease_body(self, lease_name, host_name):
46 current_time = datetime.datetime.utcnow()
47 end_time = current_time + datetime.timedelta(hours=1)
48 body = {
49 "start_date": "now",
50 "end_date": end_time.strftime('%Y-%m-%d %H:%M'),
51 "name": lease_name,
52 "events": [],
53 }
54 body["reservations"] = [
55 {
56 "hypervisor_properties": ('["==", "$hypervisor_hostname", "'
57 '%s"]' % host_name),
58 "max": 1,
59 "min": 1,
60 "resource_type": 'physical:host',
61 "resource_properties": ''
62 }
63 ]
64
65 return body
66
67 def get_lease_body_missing_param(self, lease_name, host_name):
68 current_time = datetime.datetime.utcnow()
69 end_time = current_time + datetime.timedelta(hours=1)
70 body = {
71 "start_date": "now",
72 "end_date": end_time.strftime('%Y-%m-%d %H:%M'),
73 "name": lease_name,
74 "events": [],
75 }
76 body["reservations"] = [
77 {
78 "hypervisor_properties": ('["==", "$hypervisor_hostname", "'
79 '%s"]' % host_name),
80 "min": '1',
81 "resource_type": 'physical:host',
82 "resource_properties": ''
83 }
84 ]
85
86 return body
87
88 def get_invalid_lease_body(self, lease_name, host_name):
89 current_time = datetime.datetime.utcnow()
90 end_time = current_time + datetime.timedelta(hours=1)
91 body = {
92 "start_date": "now",
93 "end_date": end_time.strftime('%Y-%m-%d %H:%M'),
94 "name": lease_name,
95 "events": [],
96 }
97 body["reservations"] = [
98 {
99 "hypervisor_properties": ('["==", "$hypervisor_hostname", "'
100 '%s"]' % host_name),
101 "max": 'foo',
102 "min": 'bar',
103 "resource_type": 'physical:host',
104 "resource_properties": ''
105 }
106 ]
107
108 return body
109
110 def get_expiration_lease_body(self, lease_name, host_name):
111 current_time = datetime.datetime.utcnow()
112 end_time = current_time + datetime.timedelta(seconds=90)
113 body = {
114 'start_date': "now",
115 'end_date': end_time.strftime('%Y-%m-%d %H:%M'),
116 'name': lease_name,
117 'events': [],
118 }
119 body['reservations'] = [
120 {
121 'hypervisor_properties': ('["==", "$hypervisor_hostname", "'
122 '%s"]' % host_name),
123 'max': 1,
124 'min': 1,
125 'resource_type': 'physical:host',
126 'resource_properties': ''
127 }
128 ]
129
130 return body
131
132 def fetch_aggregate_by_name(self, name):
133 aggregates = self.aggr_client.list_aggregates()['aggregates']
134 try:
135 aggr = [aggr for aggr in aggregates if aggr['name'] == name][0]
136 except IndexError:
137 err_msg = "aggregate with name %s doesn't exist." % name
138 raise exceptions.NotFound(err_msg)
139 return aggr
140
141 def wait_until_aggregated(self, aggregate_name, host_name):
142 for i in range(self.MAX_RETRY):
143 try:
144 aggr = self.fetch_aggregate_by_name(aggregate_name)
145 self.assertIn(host_name, aggr['hosts'])
146 return
147 except Exception:
148 pass
149 time.sleep(self.WAIT_TIME)
150 err_msg = ("hostname %s doesn't exist in aggregate %s."
151 % (host_name, aggregate_name))
152 raise exceptions.NotFound(err_msg)
153
154 @decorators.attr(type='smoke')
155 def test_host_reservation(self):
156
157 # Create the host if it doesn't exists
158 host = self._add_host_once()
159
160 # check the host is in freepool
161 freepool = self.fetch_aggregate_by_name('freepool')
162 self.assertIn(host['host'], freepool['hosts'])
163
164 # try creating a new lease with a missing parameter
165 body = self.get_lease_body_missing_param('scenario-1-missing-param',
166 host['host'])
167 self.assertRaises(exceptions.BadRequest,
168 self.reservation_client.create_lease, body)
169
170 # try creating a new lease with an invalid request
171 body = self.get_invalid_lease_body('scenario-1-invalid', host['host'])
172 self.assertRaises(exceptions.BadRequest,
173 self.reservation_client.create_lease, body)
174
175 # create new lease and start reservation immediately
176 body = self.get_lease_body('scenario-1', host['host'])
177 lease = self.reservation_client.create_lease(body)['lease']
178
179 # check host added to the reservation
180 reservation_id = next(iter(lease['reservations']))['id']
181 self.wait_until_aggregated(reservation_id, host['host'])
182
183 # create an instance with reservation id
184 create_kwargs = {
185 'scheduler_hints': {
186 "reservation": reservation_id,
187 },
188 'image_id': CONF.compute.image_ref,
189 'flavor': CONF.compute.flavor_ref,
190 }
191 server = self.create_server(clients=self.os_admin,
192 **create_kwargs)
193 # ensure server is located on the requested host
194 self.assertEqual(host['host'], server['OS-EXT-SRV-ATTR:host'])
195
196 # delete the lease, which should trigger termination of the instance
197 self.reservation_client.delete_lease(lease['id'])
198 waiters.wait_for_server_termination(self.os_admin.servers_client,
199 server['id'])
200
201 # create an instance without reservation id, which is expected to fail
202 create_kwargs = {
203 'image_id': CONF.compute.image_ref,
204 'flavor': CONF.compute.flavor_ref,
205 }
206 server = self.create_server(clients=self.os_admin,
207 wait_until=None,
208 **create_kwargs)
209 waiters.wait_for_server_status(self.os_admin.servers_client,
210 server['id'], 'ERROR',
211 raise_on_error=False)
212
213 @decorators.attr(type='smoke')
214 def test_lease_expiration(self):
215
216 # create the host if it doesn't exist
217 host = self._add_host_once()
218
219 # create new lease and start reservation immediately
220 body = self.get_expiration_lease_body('scenario-2-expiration',
221 host['host'])
222 lease = self.reservation_client.create_lease(body)['lease']
223 lease_id = lease['id']
224
225 # check host added to the reservation
226 reservation_id = next(iter(lease['reservations']))['id']
227 self.wait_until_aggregated(reservation_id, host['host'])
228
229 create_kwargs = {
230 'scheduler_hints': {
231 'reservation': reservation_id,
232 },
233 'image_id': CONF.compute.image_ref,
234 'flavor': CONF.compute.flavor_ref,
235 }
236 server = self.create_server(clients=self.os_admin,
237 **create_kwargs)
238
239 # wait for lease end
240 self.wait_for_lease_end(lease_id)
241
242 # check if the lease has been correctly terminated and
243 # the instance is removed
244 waiters.wait_for_server_termination(self.os_admin.servers_client,
245 server['id'])
246
247 # check that the host aggregate was deleted
248 self.assertRaises(exceptions.NotFound,
249 self.fetch_aggregate_by_name, reservation_id)
250
251 # check that the host is back in the freepool
252 freepool = self.fetch_aggregate_by_name('freepool')
253 self.assertIn(host['host'], freepool['hosts'])
254
255 # check the reservation status
256 lease = self.reservation_client.get_lease(lease_id)['lease']
257 self.assertIn('deleted',
258 next(iter(lease['reservations']))['status'])
259
260 @decorators.attr(type='smoke')
261 def test_update_host_reservation(self):
262
263 # create the host if it doesn't exist
264 host = self._add_host_once()
265
266 # create new lease and start reservation immediately
267 body = self.get_lease_body('scenario-3-update', host['host'])
268 lease = self.reservation_client.create_lease(body)['lease']
269 lease_id = lease['id']
270
271 # check host added to the reservation
272 reservation_id = next(iter(lease['reservations']))['id']
273 self.wait_until_aggregated(reservation_id, host['host'])
274
275 # check the host aggregate for blazar
276 self.fetch_aggregate_by_name(reservation_id)
277
278 # create an instance with reservation id
279 create_kwargs = {
280 'scheduler_hints': {
281 'reservation': reservation_id,
282 },
283 'image_id': CONF.compute.image_ref,
284 'flavor': CONF.compute.flavor_ref,
285 }
286 server = self.create_server(clients=self.os_admin,
287 wait_until=None,
288 **create_kwargs)
289 waiters.wait_for_server_status(self.os_admin.servers_client,
290 server['id'], 'ACTIVE')
291
292 # wait enough time for the update API to succeed
293 time.sleep(75)
294
295 # update the lease end_time
296 end_time = datetime.datetime.utcnow() + datetime.timedelta(seconds=30)
297 body = {
298 'end_date': end_time.strftime('%Y-%m-%d %H:%M')
299 }
300 self.reservation_client.update_lease(lease_id,
301 body)['lease']
302
303 # check if the lease has been correctly terminated and
304 # the instance is removed
305 waiters.wait_for_server_termination(self.os_admin.servers_client,
306 server['id'])
307
308 # wait long enough for the lease deletion to be complete
309 time.sleep(30)
310
311 # check that the host aggregate was deleted
312 self.assertRaises(exceptions.NotFound,
313 self.fetch_aggregate_by_name, reservation_id)
314
315 # check that the host is back in the freepool
316 freepool = self.fetch_aggregate_by_name('freepool')
317 self.assertIn(host['host'], freepool['hosts'])
318
319 # check the reservation status
320 lease = self.reservation_client.get_lease(lease_id)['lease']
321 self.assertTrue('deleted' in
322 next(iter(lease['reservations']))['status'])