Debian Codegraph @

blazar_tempest_plugin/tests/scenario/resource_reservation_scenario.py

raw
1 # Copyright 2014 Intel Corporation
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
17 from oslo_log import log
18 from tempest import clients as tempestclients
19 from tempest import config
20 from tempest import exceptions
21 from tempest.lib.common.utils import test_utils
22 from tempest.lib import exceptions as lib_exceptions
23
24 from blazar_tempest_plugin.services.reservation import (
25 reservation_client as clients)
26 from blazar_tempest_plugin.tests.scenario import manager_freeze as manager
27
28 CONF = config.CONF
29
30 LOG = log.getLogger(__name__)
31
32
33 class ResourceReservationScenarioTest(manager.ScenarioTest):
34 """Base class for resource reservation scenario tests."""
35
36 credentials = ['primary', 'admin']
37
38 @classmethod
39 def setup_clients(cls):
40 super(ResourceReservationScenarioTest, cls).setup_clients()
41 if not CONF.service_available.blazar:
42 raise cls.skipException("Resource reservation support is"
43 "required")
44
45 cred_provider = cls._get_credentials_provider()
46 creds = cred_provider.get_credentials('admin')
47 auth_prov = tempestclients.get_auth_provider(creds._credentials)
48 cls.os_admin.resource_reservation_client = (
49 clients.ResourceReservationV1Client(auth_prov,
50 'reservation',
51 CONF.identity.region))
52 cls.reservation_client = (
53 cls.os_admin.resource_reservation_client)
54
55 def get_lease_by_name(self, lease_name):
56 # the same as the blazarclient does it: ask for the entire list
57 lease_list = self.reservation_client.list_lease()
58 named_lease = []
59
60 # and then search by lease_name
61 named_lease = [lease for lease in lease_list
62 if lease['name'] == lease_name]
63
64 if named_lease:
65 return self.reservation_client.get_lease(
66 named_lease[0]['id'])
67 else:
68 message = "Unable to find lease with name '%s'" % lease_name
69 raise exceptions.NotFound(message)
70
71 def delete_lease(self, lease_id):
72 return self.reservation_client.delete_lease(lease_id)
73
74 def wait_for_lease_end(self, lease_id):
75
76 def check_lease_end():
77 try:
78 lease = self.reservation_client.get_lease(lease_id)['lease']
79 if lease:
80 events = lease['events']
81 return len([evt for evt in events
82 if evt['event_type'] == 'end_lease' and
83 evt['status'] == 'DONE']) > 0
84 else:
85 LOG.info("Lease with id %s is empty", lease_id)
86 except Exception as e:
87 LOG.info("Unable to find lease with id %(lease_id)s. "
88 "Exception: %(message)s",
89 {'lease_id': lease_id, 'message': str(e)})
90 return True
91
92 if not test_utils.call_until_true(
93 check_lease_end,
94 CONF.resource_reservation.lease_end_timeout,
95 CONF.resource_reservation.lease_interval):
96 message = "Timed out waiting for lease to change status to DONE"
97 raise lib_exceptions.TimeoutException(message)
98
99 def wait_for_lease_status(self, lease_id, status):
100
101 def check_lease_status():
102 try:
103 lease = self.reservation_client.get_lease(lease_id)['lease']
104 if lease and lease['status'] == status:
105 return True
106 else:
107 LOG.info("Lease with id %s is not %s, but %s",
108 lease_id, status, lease['status'])
109 except Exception as e:
110 LOG.info("Unable to find lease with id %(lease_id)s. "
111 "Exception: %(message)s",
112 {'lease_id': lease_id, 'message': str(e)})
113 return False
114
115 if not test_utils.call_until_true(
116 check_lease_status,
117 CONF.resource_reservation.lease_end_timeout,
118 CONF.resource_reservation.lease_interval):
119 message = ("Timed out waiting for lease to change status "
120 "to %s" % status)
121 raise lib_exceptions.TimeoutException(message)
122
123 def is_flavor_enough(self, flavor_id, image_id):
124 image = self.compute_images_client.show_image(image_id)['image']
125 flavor = self.flavors_client.show_flavor(flavor_id)['flavor']
126 return image['minDisk'] <= flavor['disk']
127
128 def fetch_one_compute_host(self):
129 """Returns the first host listed in nova-compute services."""
130 compute = next(iter(self.os_admin.services_client.
131 list_services(binary='nova-compute')['services']))
132 return compute
133
134 def _add_host_once(self):
135 host = self.fetch_one_compute_host()
136 hosts = self.reservation_client.list_host()['hosts']
137 try:
138 # TODO(masahito): Fix the check not to depend on '0' fixed check.
139 # When the scenario test covers the monitor resource feature, there
140 # are multiple compute hosts.
141 [h for h in hosts if h['hypervisor_hostname'] == host['host']][0]
142 except IndexError:
143 self.reservation_client.create_host({'name': host['host']})
144 return host