blob: 445a15e0a09ff6289b37a9d446f8ac308815c399 [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
Anton Arefiev6b003562016-09-13 12:17:29 +030015import six
Anton Arefiev44f678c2016-03-17 12:11:30 +020016import time
17
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
Anton Arefiev6b003562016-09-13 12:17:29 +030021from tempest.lib import exceptions as lib_exc
22from tempest import test
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
Anton Arefiev44f678c2016-03-17 12:11:30 +0200124 def baremetal_flavor(self):
125 flavor_id = CONF.compute.flavor_ref
126 flavor = self.flavors_client.show_flavor(flavor_id)['flavor']
127 flavor['properties'] = self.flavors_client.list_flavor_extra_specs(
128 flavor_id)['extra_specs']
129 return flavor
130
131 def get_rule_path(self, rule_file):
132 base_path = os.path.split(
133 os.path.dirname(os.path.abspath(__file__)))[0]
134 base_path = os.path.split(base_path)[0]
135 return os.path.join(base_path, "inspector_tempest_plugin",
136 "rules", rule_file)
137
Anton Arefiev84a09c62016-07-19 19:35:18 +0300138 def boot_instance(self):
139 return super(InspectorScenarioTest, self).boot_instance()
140
141 def terminate_instance(self, instance):
142 return super(InspectorScenarioTest, self).terminate_instance(instance)
143
Anton Arefiev6b003562016-09-13 12:17:29 +0300144 def wait_for_node(self, node_name):
145 def check_node():
146 try:
147 self.node_show(node_name)
148 except lib_exc.NotFound:
149 return False
150 return True
151
152 if not test.call_until_true(
153 check_node,
154 duration=CONF.baremetal_introspection.discovery_timeout,
155 sleep_for=20):
156 msg = ("Timed out waiting for node %s " % node_name)
157 raise lib_exc.TimeoutException(msg)
158
159 inspected_node = self.node_show(self.node_info['name'])
160 self.wait_for_introspection_finished(inspected_node['uuid'])
161
Anton Arefiev44f678c2016-03-17 12:11:30 +0200162 # TODO(aarefiev): switch to call_until_true
163 def wait_for_introspection_finished(self, node_ids):
164 """Waits for introspection of baremetal nodes to finish.
165
166 """
Anton Arefiev6b003562016-09-13 12:17:29 +0300167 if isinstance(node_ids, six.text_type):
168 node_ids = [node_ids]
Anton Arefiev44f678c2016-03-17 12:11:30 +0200169 start = int(time.time())
170 not_introspected = {node_id for node_id in node_ids}
171
172 while not_introspected:
173 time.sleep(CONF.baremetal_introspection.introspection_sleep)
174 for node_id in node_ids:
175 status = self.introspection_status(node_id)
176 if status['finished']:
177 if status['error']:
178 message = ('Node %(node_id)s introspection failed '
179 'with %(error)s.' %
180 {'node_id': node_id,
181 'error': status['error']})
182 raise exceptions.IntrospectionFailed(message)
183 not_introspected = not_introspected - {node_id}
184
185 if (int(time.time()) - start >=
186 CONF.baremetal_introspection.introspection_timeout):
187 message = ('Introspection timed out for nodes: %s' %
188 not_introspected)
189 raise exceptions.IntrospectionTimeout(message)
190
191 def wait_for_nova_aware_of_bvms(self):
192 start = int(time.time())
193 while True:
194 time.sleep(CONF.baremetal_introspection.hypervisor_update_sleep)
195 stats = self.hypervisor_stats()
196 expected_cpus = self.baremetal_flavor()['vcpus']
197 if int(stats['hypervisor_statistics']['vcpus']) >= expected_cpus:
198 break
199
200 timeout = CONF.baremetal_introspection.hypervisor_update_timeout
201 if (int(time.time()) - start >= timeout):
202 message = (
203 'Timeout while waiting for nova hypervisor-stats: '
204 '%(stats)s required time (%(timeout)s s).' %
205 {'stats': stats,
206 'timeout': timeout})
207 raise exceptions.HypervisorUpdateTimeout(message)
Dmitry Tantsur290c96b2016-07-01 14:22:51 +0200208
209 def node_cleanup(self, node_id):
210 if (self.node_show(node_id)['provision_state'] ==
211 BaremetalProvisionStates.AVAILABLE):
212 return
213 try:
214 self.baremetal_client.set_node_provision_state(node_id, 'provide')
215 except tempest.lib.exceptions.RestClientException:
216 # maybe node already cleaning or available
217 pass
218
219 self.wait_provisioning_state(
220 node_id, [BaremetalProvisionStates.AVAILABLE,
221 BaremetalProvisionStates.NOSTATE],
222 timeout=CONF.baremetal.unprovision_timeout,
223 interval=self.wait_provisioning_state_interval)
224
225 def introspect_node(self, node_id):
226 # in case there are properties remove those
227 patch = {('properties/%s' % key): None for key in
228 self.node_show(node_id)['properties']}
229 # reset any previous rule result
230 patch['extra/rule_success'] = None
231 self.node_update(node_id, patch)
232
233 self.baremetal_client.set_node_provision_state(node_id, 'manage')
234 self.baremetal_client.set_node_provision_state(node_id, 'inspect')
235 self.addCleanup(self.node_cleanup, node_id)