blob: 343ac9fef7c1ef96091fb805587dbc6ce258277c [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. Villalovosf8b09fe2017-02-16 10:11:06 -080017import six
Dmitry Tantsur290c96b2016-07-01 14:22:51 +020018import tempest
Anton Arefiev44f678c2016-03-17 12:11:30 +020019from tempest import config
Dmitry Tantsur290c96b2016-07-01 14:22:51 +020020from tempest.lib.common.api_version_utils import LATEST_MICROVERSION
Ken'ichi Ohmichi853ec5e2017-02-09 10:04:02 -080021from tempest.lib.common.utils import test_utils
Anton Arefiev6b003562016-09-13 12:17:29 +030022from tempest.lib import exceptions as lib_exc
Anton Arefiev44f678c2016-03-17 12:11:30 +020023
24from ironic_inspector.test.inspector_tempest_plugin import exceptions
25from ironic_inspector.test.inspector_tempest_plugin.services import \
26 introspection_client
Dmitry Tantsur290c96b2016-07-01 14:22:51 +020027from ironic_tempest_plugin.tests.api.admin.api_microversion_fixture import \
28 APIMicroversionFixture as IronicMicroversionFixture
29from ironic_tempest_plugin.tests.scenario.baremetal_manager import \
30 BaremetalProvisionStates
Anton Arefiev44f678c2016-03-17 12:11:30 +020031from ironic_tempest_plugin.tests.scenario.baremetal_manager import \
32 BaremetalScenarioTest
33
34
35CONF = config.CONF
36
37
38class InspectorScenarioTest(BaremetalScenarioTest):
39 """Provide harness to do Inspector scenario tests."""
40
Dmitry Tantsur290c96b2016-07-01 14:22:51 +020041 wait_provisioning_state_interval = 15
42
Anton Arefiev44f678c2016-03-17 12:11:30 +020043 credentials = ['primary', 'admin']
44
Dmitry Tantsur290c96b2016-07-01 14:22:51 +020045 ironic_api_version = LATEST_MICROVERSION
46
Anton Arefiev44f678c2016-03-17 12:11:30 +020047 @classmethod
48 def setup_clients(cls):
49 super(InspectorScenarioTest, cls).setup_clients()
50 inspector_manager = introspection_client.Manager()
51 cls.introspection_client = inspector_manager.introspection_client
52
53 def setUp(self):
54 super(InspectorScenarioTest, self).setUp()
Dmitry Tantsur290c96b2016-07-01 14:22:51 +020055 # we rely on the 'available' provision_state; using latest
56 # microversion
57 self.useFixture(IronicMicroversionFixture(self.ironic_api_version))
Anton Arefiev44f678c2016-03-17 12:11:30 +020058 self.flavor = self.baremetal_flavor()
Dmitry Tantsur290c96b2016-07-01 14:22:51 +020059 self.node_ids = {node['uuid'] for node in
60 self.node_filter(filter=lambda node:
61 node['provision_state'] ==
62 BaremetalProvisionStates.AVAILABLE)}
63 self.rule_purge()
Anton Arefiev44f678c2016-03-17 12:11:30 +020064
65 def item_filter(self, list_method, show_method,
66 filter=lambda item: True, items=None):
67 if items is None:
68 items = [show_method(item['uuid']) for item in
69 list_method()]
70 return [item for item in items if filter(item)]
71
72 def node_list(self):
73 return self.baremetal_client.list_nodes()[1]['nodes']
74
Anton Arefiev6b003562016-09-13 12:17:29 +030075 def node_port_list(self, node_uuid):
76 return self.baremetal_client.list_node_ports(node_uuid)[1]['ports']
77
Anton Arefiev44f678c2016-03-17 12:11:30 +020078 def node_update(self, uuid, patch):
79 return self.baremetal_client.update_node(uuid, **patch)
80
81 def node_show(self, uuid):
82 return self.baremetal_client.show_node(uuid)[1]
83
Anton Arefiev6b003562016-09-13 12:17:29 +030084 def node_delete(self, uuid):
85 return self.baremetal_client.delete_node(uuid)
86
Anton Arefiev44f678c2016-03-17 12:11:30 +020087 def node_filter(self, filter=lambda node: True, nodes=None):
88 return self.item_filter(self.node_list, self.node_show,
89 filter=filter, items=nodes)
90
Anton Arefiev6b003562016-09-13 12:17:29 +030091 def node_set_power_state(self, uuid, state):
92 self.baremetal_client.set_node_power_state(uuid, state)
93
94 def node_set_provision_state(self, uuid, state):
95 self.baremetal_client.set_node_provision_state(self, uuid, state)
96
Anton Arefiev44f678c2016-03-17 12:11:30 +020097 def hypervisor_stats(self):
98 return (self.admin_manager.hypervisor_client.
99 show_hypervisor_statistics())
100
101 def server_show(self, uuid):
102 self.servers_client.show_server(uuid)
103
104 def rule_purge(self):
105 self.introspection_client.purge_rules()
106
107 def rule_import(self, rule_path):
Anton Arefiev6b003562016-09-13 12:17:29 +0300108 with open(rule_path, 'r') as fp:
109 rules = json.load(fp)
110 self.introspection_client.create_rules(rules)
111
112 def rule_import_from_dict(self, rules):
113 self.introspection_client.create_rules(rules)
Anton Arefiev44f678c2016-03-17 12:11:30 +0200114
115 def introspection_status(self, uuid):
116 return self.introspection_client.get_status(uuid)[1]
117
118 def introspection_data(self, uuid):
119 return self.introspection_client.get_data(uuid)[1]
120
Anton Arefiev6b003562016-09-13 12:17:29 +0300121 def introspection_start(self, uuid):
122 return self.introspection_client.start_introspection(uuid)
123
Sergii Nozhka1eb13cf2016-11-04 17:15:50 +0200124 def introspection_abort(self, uuid):
125 return self.introspection_client.abort_introspection(uuid)
126
Anton Arefiev44f678c2016-03-17 12:11:30 +0200127 def baremetal_flavor(self):
128 flavor_id = CONF.compute.flavor_ref
129 flavor = self.flavors_client.show_flavor(flavor_id)['flavor']
130 flavor['properties'] = self.flavors_client.list_flavor_extra_specs(
131 flavor_id)['extra_specs']
132 return flavor
133
134 def get_rule_path(self, rule_file):
135 base_path = os.path.split(
136 os.path.dirname(os.path.abspath(__file__)))[0]
137 base_path = os.path.split(base_path)[0]
138 return os.path.join(base_path, "inspector_tempest_plugin",
139 "rules", rule_file)
140
Anton Arefiev84a09c62016-07-19 19:35:18 +0300141 def boot_instance(self):
142 return super(InspectorScenarioTest, self).boot_instance()
143
144 def terminate_instance(self, instance):
145 return super(InspectorScenarioTest, self).terminate_instance(instance)
146
Anton Arefiev6b003562016-09-13 12:17:29 +0300147 def wait_for_node(self, node_name):
148 def check_node():
149 try:
150 self.node_show(node_name)
151 except lib_exc.NotFound:
152 return False
153 return True
154
Ken'ichi Ohmichi853ec5e2017-02-09 10:04:02 -0800155 if not test_utils.call_until_true(
Anton Arefiev6b003562016-09-13 12:17:29 +0300156 check_node,
157 duration=CONF.baremetal_introspection.discovery_timeout,
158 sleep_for=20):
159 msg = ("Timed out waiting for node %s " % node_name)
160 raise lib_exc.TimeoutException(msg)
161
162 inspected_node = self.node_show(self.node_info['name'])
163 self.wait_for_introspection_finished(inspected_node['uuid'])
164
Anton Arefiev44f678c2016-03-17 12:11:30 +0200165 # TODO(aarefiev): switch to call_until_true
166 def wait_for_introspection_finished(self, node_ids):
167 """Waits for introspection of baremetal nodes to finish.
168
169 """
Anton Arefiev6b003562016-09-13 12:17:29 +0300170 if isinstance(node_ids, six.text_type):
171 node_ids = [node_ids]
Anton Arefiev44f678c2016-03-17 12:11:30 +0200172 start = int(time.time())
173 not_introspected = {node_id for node_id in node_ids}
174
175 while not_introspected:
176 time.sleep(CONF.baremetal_introspection.introspection_sleep)
177 for node_id in node_ids:
178 status = self.introspection_status(node_id)
179 if status['finished']:
180 if status['error']:
181 message = ('Node %(node_id)s introspection failed '
182 'with %(error)s.' %
183 {'node_id': node_id,
184 'error': status['error']})
185 raise exceptions.IntrospectionFailed(message)
186 not_introspected = not_introspected - {node_id}
187
188 if (int(time.time()) - start >=
189 CONF.baremetal_introspection.introspection_timeout):
190 message = ('Introspection timed out for nodes: %s' %
191 not_introspected)
192 raise exceptions.IntrospectionTimeout(message)
193
194 def wait_for_nova_aware_of_bvms(self):
195 start = int(time.time())
196 while True:
197 time.sleep(CONF.baremetal_introspection.hypervisor_update_sleep)
198 stats = self.hypervisor_stats()
199 expected_cpus = self.baremetal_flavor()['vcpus']
200 if int(stats['hypervisor_statistics']['vcpus']) >= expected_cpus:
201 break
202
203 timeout = CONF.baremetal_introspection.hypervisor_update_timeout
204 if (int(time.time()) - start >= timeout):
205 message = (
206 'Timeout while waiting for nova hypervisor-stats: '
207 '%(stats)s required time (%(timeout)s s).' %
208 {'stats': stats,
209 'timeout': timeout})
210 raise exceptions.HypervisorUpdateTimeout(message)
Dmitry Tantsur290c96b2016-07-01 14:22:51 +0200211
212 def node_cleanup(self, node_id):
213 if (self.node_show(node_id)['provision_state'] ==
214 BaremetalProvisionStates.AVAILABLE):
215 return
Sergii Nozhka1eb13cf2016-11-04 17:15:50 +0200216 # in case when introspection failed we need set provision state
217 # to 'manage' to make it possible transit into 'provide' state
218 if self.node_show(node_id)['provision_state'] == 'inspect failed':
219 self.baremetal_client.set_node_provision_state(node_id, 'manage')
220
Dmitry Tantsur290c96b2016-07-01 14:22:51 +0200221 try:
222 self.baremetal_client.set_node_provision_state(node_id, 'provide')
223 except tempest.lib.exceptions.RestClientException:
224 # maybe node already cleaning or available
225 pass
226
227 self.wait_provisioning_state(
228 node_id, [BaremetalProvisionStates.AVAILABLE,
229 BaremetalProvisionStates.NOSTATE],
230 timeout=CONF.baremetal.unprovision_timeout,
231 interval=self.wait_provisioning_state_interval)
232
Sergii Nozhka1eb13cf2016-11-04 17:15:50 +0200233 def introspect_node(self, node_id, remove_props=True):
234 if remove_props:
235 # in case there are properties remove those
236 patch = {('properties/%s' % key): None for key in
237 self.node_show(node_id)['properties']}
238 # reset any previous rule result
239 patch['extra/rule_success'] = None
240 self.node_update(node_id, patch)
Dmitry Tantsur290c96b2016-07-01 14:22:51 +0200241
242 self.baremetal_client.set_node_provision_state(node_id, 'manage')
243 self.baremetal_client.set_node_provision_state(node_id, 'inspect')
244 self.addCleanup(self.node_cleanup, node_id)