Debian Codegraph @

blazar_tempest_plugin/tests/scenario/test_flavor_reservation.py

raw
1 # Copyright 2024 OpenStack Foundation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain 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,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import datetime
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
23 from blazar_tempest_plugin.tests.scenario import (
24 resource_reservation_scenario as rrs)
25
26 CONF = config.CONF
27
28 LOG = logging.getLogger(__name__)
29
30 # same as the one at blazar/manager/service
31 LEASE_DATE_FORMAT = "%Y-%m-%d %H:%M"
32
33
34 class TestFlavorReservationScenario(rrs.ResourceReservationScenarioTest):
35 """A scenario that checks the flavor-based instance reservation feature."""
36
37 def setUp(self):
38 super(TestFlavorReservationScenario, self).setUp()
39 # Setup image and flavor for the test instance
40 # Support both configured and injected values
41 if not hasattr(self, 'image_ref'):
42 self.image_ref = CONF.compute.image_ref
43 if not hasattr(self, 'flavor_ref'):
44 self.flavor_ref = CONF.compute.flavor_ref
45 if not self.is_flavor_enough(self.flavor_ref, self.image_ref):
46 raise self.skipException(
47 '{image} does not fit in {flavor}'.format(
48 image=self.image_ref, flavor=self.flavor_ref
49 )
50 )
51 self.host = self._add_host_once()
52
53 def tearDown(self):
54 super(TestFlavorReservationScenario, self).tearDown()
55
56 def get_lease_body(self, lease_name):
57 current_time = datetime.datetime.utcnow()
58 end_time = current_time + datetime.timedelta(hours=1)
59 body = {
60 "start_date": "now",
61 "end_date": end_time.strftime(LEASE_DATE_FORMAT),
62 "name": lease_name,
63 "events": [],
64 }
65 body["reservations"] = [
66 {
67 "resource_type": 'flavor:instance',
68 'flavor_id': self.flavor_ref,
69 'amount': 1,
70 'affinity': None,
71 'resource_properties': '',
72 }
73 ]
74 return body
75
76 @decorators.attr(type='smoke')
77 def test_flavor_instance_reservation(self):
78 body = self.get_lease_body('flavor-instance-scenario')
79 lease = self.reservation_client.create_lease(body)['lease']
80 reservation = next(iter(lease['reservations']))
81
82 self.wait_for_lease_status(lease['id'], 'ACTIVE')
83
84 # create an instance within the reservation
85 create_kwargs = {
86 'image_id': CONF.compute.image_ref,
87 'flavor': reservation['id'],
88 }
89 server1 = self.create_server(clients=self.os_admin,
90 **create_kwargs)
91
92 # create another instance within the reservation, which is expected to
93 # fail because we are over the reserved capacity
94 create_kwargs = {
95 'image_id': CONF.compute.image_ref,
96 'flavor': reservation['id'],
97 }
98 server2 = self.create_server(clients=self.os_admin,
99 wait_until=None,
100 **create_kwargs)
101 waiters.wait_for_server_status(self.os_admin.servers_client,
102 server2['id'], 'ERROR',
103 raise_on_error=False)
104
105 # create an instance without specifying a reservation, which is
106 # expected to fail
107 create_kwargs = {
108 'image_id': CONF.compute.image_ref,
109 'flavor': CONF.compute.flavor_ref,
110 }
111 server3 = self.create_server(clients=self.os_admin,
112 wait_until=None,
113 **create_kwargs)
114 waiters.wait_for_server_status(self.os_admin.servers_client,
115 server3['id'], 'ERROR',
116 raise_on_error=False)
117
118 # delete the lease, which should trigger termination of the instance
119 self.reservation_client.delete_lease(lease['id'])
120 waiters.wait_for_server_termination(self.os_admin.servers_client,
121 server1['id'])