blob: e6eb3892271190027ec4c7381f9151fb6513a45a [file] [log] [blame]
Anton Arefiev44f678c2016-03-17 12:11:30 +02001# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
12
Anton Arefiev6b003562016-09-13 12:17:29 +030013import json
Anton Arefiev44f678c2016-03-17 12:11:30 +020014import os
15import time
16
John L. Villalovos2c22aad2017-10-12 14:56:24 -070017from ironic_tempest_plugin.tests.api.admin.api_microversion_fixture import \
18 APIMicroversionFixture as IronicMicroversionFixture
19from ironic_tempest_plugin.tests.scenario.baremetal_manager import \
20 BaremetalProvisionStates
21from ironic_tempest_plugin.tests.scenario.baremetal_manager import \
22 BaremetalScenarioTest
John L. Villalovosf8b09fe2017-02-16 10:11:06 -080023import six
Dmitry Tantsur290c96b2016-07-01 14:22:51 +020024import tempest
Anton Arefiev44f678c2016-03-17 12:11:30 +020025from tempest import config
Dmitry Tantsur290c96b2016-07-01 14:22:51 +020026from tempest.lib.common.api_version_utils import LATEST_MICROVERSION
Ken'ichi Ohmichi853ec5e2017-02-09 10:04:02 -080027from tempest.lib.common.utils import test_utils
Anton Arefiev6b003562016-09-13 12:17:29 +030028from tempest.lib import exceptions as lib_exc
Anton Arefiev44f678c2016-03-17 12:11:30 +020029
30from ironic_inspector.test.inspector_tempest_plugin import exceptions
31from ironic_inspector.test.inspector_tempest_plugin.services import \
32 introspection_client
Anton Arefiev44f678c2016-03-17 12:11:30 +020033
34CONF = config.CONF
35
36
37class InspectorScenarioTest(BaremetalScenarioTest):
38 """Provide harness to do Inspector scenario tests."""
39
Dmitry Tantsur290c96b2016-07-01 14:22:51 +020040 wait_provisioning_state_interval = 15
41
Anton Arefiev44f678c2016-03-17 12:11:30 +020042 credentials = ['primary', 'admin']
43
Dmitry Tantsur290c96b2016-07-01 14:22:51 +020044 ironic_api_version = LATEST_MICROVERSION
45
Anton Arefiev44f678c2016-03-17 12:11:30 +020046 @classmethod
47 def setup_clients(cls):
48 super(InspectorScenarioTest, cls).setup_clients()
49 inspector_manager = introspection_client.Manager()
50 cls.introspection_client = inspector_manager.introspection_client
51
52 def setUp(self):
53 super(InspectorScenarioTest, self).setUp()
Dmitry Tantsur290c96b2016-07-01 14:22:51 +020054 # we rely on the 'available' provision_state; using latest
55 # microversion
56 self.useFixture(IronicMicroversionFixture(self.ironic_api_version))
Anton Arefiev44f678c2016-03-17 12:11:30 +020057 self.flavor = self.baremetal_flavor()
Dmitry Tantsur290c96b2016-07-01 14:22:51 +020058 self.node_ids = {node['uuid'] for node in
59 self.node_filter(filter=lambda node:
60 node['provision_state'] ==
61 BaremetalProvisionStates.AVAILABLE)}
62 self.rule_purge()
Anton Arefiev44f678c2016-03-17 12:11:30 +020063
64 def item_filter(self, list_method, show_method,
65 filter=lambda item: True, items=None):
66 if items is None:
67 items = [show_method(item['uuid']) for item in
68 list_method()]
69 return [item for item in items if filter(item)]
70
71 def node_list(self):
72 return self.baremetal_client.list_nodes()[1]['nodes']
73
Anton Arefiev6b003562016-09-13 12:17:29 +030074 def node_port_list(self, node_uuid):
75 return self.baremetal_client.list_node_ports(node_uuid)[1]['ports']
76
Anton Arefiev44f678c2016-03-17 12:11:30 +020077 def node_update(self, uuid, patch):
78 return self.baremetal_client.update_node(uuid, **patch)
79
80 def node_show(self, uuid):
81 return self.baremetal_client.show_node(uuid)[1]
82
Anton Arefiev6b003562016-09-13 12:17:29 +030083 def node_delete(self, uuid):
84 return self.baremetal_client.delete_node(uuid)
85
Anton Arefiev44f678c2016-03-17 12:11:30 +020086 def node_filter(self, filter=lambda node: True, nodes=None):
87 return self.item_filter(self.node_list, self.node_show,
88 filter=filter, items=nodes)
89
Anton Arefiev6b003562016-09-13 12:17:29 +030090 def node_set_power_state(self, uuid, state):
91 self.baremetal_client.set_node_power_state(uuid, state)
92
93 def node_set_provision_state(self, uuid, state):
94 self.baremetal_client.set_node_provision_state(self, uuid, state)
95
Anton Arefiev44f678c2016-03-17 12:11:30 +020096 def hypervisor_stats(self):
Luong Anh Tuanbdc14292017-09-25 14:58:10 +070097 return (self.os_admin.hypervisor_client.
Anton Arefiev44f678c2016-03-17 12:11:30 +020098 show_hypervisor_statistics())
99
100 def server_show(self, uuid):
101 self.servers_client.show_server(uuid)
102
103 def rule_purge(self):
104 self.introspection_client.purge_rules()
105
106 def rule_import(self, rule_path):
Anton Arefiev6b003562016-09-13 12:17:29 +0300107 with open(rule_path, 'r') as fp:
108 rules = json.load(fp)
109 self.introspection_client.create_rules(rules)
110
111 def rule_import_from_dict(self, rules):
112 self.introspection_client.create_rules(rules)
Anton Arefiev44f678c2016-03-17 12:11:30 +0200113
114 def introspection_status(self, uuid):
115 return self.introspection_client.get_status(uuid)[1]
116
117 def introspection_data(self, uuid):
118 return self.introspection_client.get_data(uuid)[1]
119
Anton Arefiev6b003562016-09-13 12:17:29 +0300120 def introspection_start(self, uuid):
121 return self.introspection_client.start_introspection(uuid)
122
Sergii Nozhka1eb13cf2016-11-04 17:15:50 +0200123 def introspection_abort(self, uuid):
124 return self.introspection_client.abort_introspection(uuid)
125
Anton Arefiev44f678c2016-03-17 12:11:30 +0200126 def baremetal_flavor(self):
127 flavor_id = CONF.compute.flavor_ref
128 flavor = self.flavors_client.show_flavor(flavor_id)['flavor']
129 flavor['properties'] = self.flavors_client.list_flavor_extra_specs(
130 flavor_id)['extra_specs']
131 return flavor
132
133 def get_rule_path(self, rule_file):
134 base_path = os.path.split(
135 os.path.dirname(os.path.abspath(__file__)))[0]
136 base_path = os.path.split(base_path)[0]
137 return os.path.join(base_path, "inspector_tempest_plugin",
138 "rules", rule_file)
139
Anton Arefiev84a09c62016-07-19 19:35:18 +0300140 def boot_instance(self):
141 return super(InspectorScenarioTest, self).boot_instance()
142
143 def terminate_instance(self, instance):
144 return super(InspectorScenarioTest, self).terminate_instance(instance)
145
Anton Arefiev6b003562016-09-13 12:17:29 +0300146 def wait_for_node(self, node_name):
147 def check_node():
148 try:
149 self.node_show(node_name)
150 except lib_exc.NotFound:
151 return False
152 return True
153
Ken'ichi Ohmichi853ec5e2017-02-09 10:04:02 -0800154 if not test_utils.call_until_true(
Anton Arefiev6b003562016-09-13 12:17:29 +0300155 check_node,
156 duration=CONF.baremetal_introspection.discovery_timeout,
157 sleep_for=20):
158 msg = ("Timed out waiting for node %s " % node_name)
159 raise lib_exc.TimeoutException(msg)
160
161 inspected_node = self.node_show(self.node_info['name'])
162 self.wait_for_introspection_finished(inspected_node['uuid'])
163
Anton Arefiev44f678c2016-03-17 12:11:30 +0200164 # TODO(aarefiev): switch to call_until_true
165 def wait_for_introspection_finished(self, node_ids):
166 """Waits for introspection of baremetal nodes to finish.
167
168 """
Anton Arefiev6b003562016-09-13 12:17:29 +0300169 if isinstance(node_ids, six.text_type):
170 node_ids = [node_ids]
Anton Arefiev44f678c2016-03-17 12:11:30 +0200171 start = int(time.time())
172 not_introspected = {node_id for node_id in node_ids}
173
174 while not_introspected:
175 time.sleep(CONF.baremetal_introspection.introspection_sleep)
176 for node_id in node_ids:
177 status = self.introspection_status(node_id)
178 if status['finished']:
179 if status['error']:
180 message = ('Node %(node_id)s introspection failed '
181 'with %(error)s.' %
182 {'node_id': node_id,
183 'error': status['error']})
184 raise exceptions.IntrospectionFailed(message)
185 not_introspected = not_introspected - {node_id}
186
187 if (int(time.time()) - start >=
188 CONF.baremetal_introspection.introspection_timeout):
189 message = ('Introspection timed out for nodes: %s' %
190 not_introspected)
191 raise exceptions.IntrospectionTimeout(message)
192
193 def wait_for_nova_aware_of_bvms(self):
194 start = int(time.time())
195 while True:
196 time.sleep(CONF.baremetal_introspection.hypervisor_update_sleep)
197 stats = self.hypervisor_stats()
198 expected_cpus = self.baremetal_flavor()['vcpus']
199 if int(stats['hypervisor_statistics']['vcpus']) >= expected_cpus:
200 break
201
202 timeout = CONF.baremetal_introspection.hypervisor_update_timeout
203 if (int(time.time()) - start >= timeout):
204 message = (
205 'Timeout while waiting for nova hypervisor-stats: '
206 '%(stats)s required time (%(timeout)s s).' %
207 {'stats': stats,
208 'timeout': timeout})
209 raise exceptions.HypervisorUpdateTimeout(message)
Dmitry Tantsur290c96b2016-07-01 14:22:51 +0200210
211 def node_cleanup(self, node_id):
212 if (self.node_show(node_id)['provision_state'] ==
213 BaremetalProvisionStates.AVAILABLE):
214 return
Sergii Nozhka1eb13cf2016-11-04 17:15:50 +0200215 # in case when introspection failed we need set provision state
216 # to 'manage' to make it possible transit into 'provide' state
217 if self.node_show(node_id)['provision_state'] == 'inspect failed':
218 self.baremetal_client.set_node_provision_state(node_id, 'manage')
219
Dmitry Tantsur290c96b2016-07-01 14:22:51 +0200220 try:
221 self.baremetal_client.set_node_provision_state(node_id, 'provide')
222 except tempest.lib.exceptions.RestClientException:
223 # maybe node already cleaning or available
224 pass
225
226 self.wait_provisioning_state(
227 node_id, [BaremetalProvisionStates.AVAILABLE,
228 BaremetalProvisionStates.NOSTATE],
229 timeout=CONF.baremetal.unprovision_timeout,
230 interval=self.wait_provisioning_state_interval)
231
Sergii Nozhka1eb13cf2016-11-04 17:15:50 +0200232 def introspect_node(self, node_id, remove_props=True):
233 if remove_props:
234 # in case there are properties remove those
235 patch = {('properties/%s' % key): None for key in
236 self.node_show(node_id)['properties']}
237 # reset any previous rule result
238 patch['extra/rule_success'] = None
239 self.node_update(node_id, patch)
Dmitry Tantsur290c96b2016-07-01 14:22:51 +0200240
241 self.baremetal_client.set_node_provision_state(node_id, 'manage')
242 self.baremetal_client.set_node_provision_state(node_id, 'inspect')
243 self.addCleanup(self.node_cleanup, node_id)