Anton Arefiev | 44f678c | 2016-03-17 12:11:30 +0200 | [diff] [blame] | 1 | # 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 Arefiev | 6b00356 | 2016-09-13 12:17:29 +0300 | [diff] [blame] | 13 | import json |
Anton Arefiev | 44f678c | 2016-03-17 12:11:30 +0200 | [diff] [blame] | 14 | import os |
| 15 | import time |
| 16 | |
John L. Villalovos | 2c22aad | 2017-10-12 14:56:24 -0700 | [diff] [blame] | 17 | from ironic_tempest_plugin.tests.api.admin.api_microversion_fixture import \ |
| 18 | APIMicroversionFixture as IronicMicroversionFixture |
| 19 | from ironic_tempest_plugin.tests.scenario.baremetal_manager import \ |
| 20 | BaremetalProvisionStates |
| 21 | from ironic_tempest_plugin.tests.scenario.baremetal_manager import \ |
| 22 | BaremetalScenarioTest |
John L. Villalovos | f8b09fe | 2017-02-16 10:11:06 -0800 | [diff] [blame] | 23 | import six |
Dmitry Tantsur | 290c96b | 2016-07-01 14:22:51 +0200 | [diff] [blame] | 24 | import tempest |
Anton Arefiev | 44f678c | 2016-03-17 12:11:30 +0200 | [diff] [blame] | 25 | from tempest import config |
Dmitry Tantsur | 290c96b | 2016-07-01 14:22:51 +0200 | [diff] [blame] | 26 | from tempest.lib.common.api_version_utils import LATEST_MICROVERSION |
Ken'ichi Ohmichi | 853ec5e | 2017-02-09 10:04:02 -0800 | [diff] [blame] | 27 | from tempest.lib.common.utils import test_utils |
Anton Arefiev | 6b00356 | 2016-09-13 12:17:29 +0300 | [diff] [blame] | 28 | from tempest.lib import exceptions as lib_exc |
Anton Arefiev | 44f678c | 2016-03-17 12:11:30 +0200 | [diff] [blame] | 29 | |
| 30 | from ironic_inspector.test.inspector_tempest_plugin import exceptions |
| 31 | from ironic_inspector.test.inspector_tempest_plugin.services import \ |
| 32 | introspection_client |
Anton Arefiev | 44f678c | 2016-03-17 12:11:30 +0200 | [diff] [blame] | 33 | |
| 34 | CONF = config.CONF |
| 35 | |
| 36 | |
| 37 | class InspectorScenarioTest(BaremetalScenarioTest): |
| 38 | """Provide harness to do Inspector scenario tests.""" |
| 39 | |
Dmitry Tantsur | 290c96b | 2016-07-01 14:22:51 +0200 | [diff] [blame] | 40 | wait_provisioning_state_interval = 15 |
| 41 | |
Anton Arefiev | 44f678c | 2016-03-17 12:11:30 +0200 | [diff] [blame] | 42 | credentials = ['primary', 'admin'] |
| 43 | |
Dmitry Tantsur | 290c96b | 2016-07-01 14:22:51 +0200 | [diff] [blame] | 44 | ironic_api_version = LATEST_MICROVERSION |
| 45 | |
Anton Arefiev | 44f678c | 2016-03-17 12:11:30 +0200 | [diff] [blame] | 46 | @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 Tantsur | 290c96b | 2016-07-01 14:22:51 +0200 | [diff] [blame] | 54 | # we rely on the 'available' provision_state; using latest |
| 55 | # microversion |
| 56 | self.useFixture(IronicMicroversionFixture(self.ironic_api_version)) |
Anton Arefiev | 44f678c | 2016-03-17 12:11:30 +0200 | [diff] [blame] | 57 | self.flavor = self.baremetal_flavor() |
Dmitry Tantsur | 290c96b | 2016-07-01 14:22:51 +0200 | [diff] [blame] | 58 | 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 Arefiev | 44f678c | 2016-03-17 12:11:30 +0200 | [diff] [blame] | 63 | |
| 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 Arefiev | 6b00356 | 2016-09-13 12:17:29 +0300 | [diff] [blame] | 74 | def node_port_list(self, node_uuid): |
| 75 | return self.baremetal_client.list_node_ports(node_uuid)[1]['ports'] |
| 76 | |
Anton Arefiev | 44f678c | 2016-03-17 12:11:30 +0200 | [diff] [blame] | 77 | 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 Arefiev | 6b00356 | 2016-09-13 12:17:29 +0300 | [diff] [blame] | 83 | def node_delete(self, uuid): |
| 84 | return self.baremetal_client.delete_node(uuid) |
| 85 | |
Anton Arefiev | 44f678c | 2016-03-17 12:11:30 +0200 | [diff] [blame] | 86 | 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 Arefiev | 6b00356 | 2016-09-13 12:17:29 +0300 | [diff] [blame] | 90 | 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 Arefiev | 44f678c | 2016-03-17 12:11:30 +0200 | [diff] [blame] | 96 | def hypervisor_stats(self): |
Luong Anh Tuan | bdc1429 | 2017-09-25 14:58:10 +0700 | [diff] [blame] | 97 | return (self.os_admin.hypervisor_client. |
Anton Arefiev | 44f678c | 2016-03-17 12:11:30 +0200 | [diff] [blame] | 98 | 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 Arefiev | 6b00356 | 2016-09-13 12:17:29 +0300 | [diff] [blame] | 107 | 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 Arefiev | 44f678c | 2016-03-17 12:11:30 +0200 | [diff] [blame] | 113 | |
| 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 Arefiev | 6b00356 | 2016-09-13 12:17:29 +0300 | [diff] [blame] | 120 | def introspection_start(self, uuid): |
| 121 | return self.introspection_client.start_introspection(uuid) |
| 122 | |
Sergii Nozhka | 1eb13cf | 2016-11-04 17:15:50 +0200 | [diff] [blame] | 123 | def introspection_abort(self, uuid): |
| 124 | return self.introspection_client.abort_introspection(uuid) |
| 125 | |
Anton Arefiev | 44f678c | 2016-03-17 12:11:30 +0200 | [diff] [blame] | 126 | 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 Arefiev | 84a09c6 | 2016-07-19 19:35:18 +0300 | [diff] [blame] | 140 | 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 Arefiev | 6b00356 | 2016-09-13 12:17:29 +0300 | [diff] [blame] | 146 | 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 Ohmichi | 853ec5e | 2017-02-09 10:04:02 -0800 | [diff] [blame] | 154 | if not test_utils.call_until_true( |
Anton Arefiev | 6b00356 | 2016-09-13 12:17:29 +0300 | [diff] [blame] | 155 | 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 Arefiev | 44f678c | 2016-03-17 12:11:30 +0200 | [diff] [blame] | 164 | # 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 Arefiev | 6b00356 | 2016-09-13 12:17:29 +0300 | [diff] [blame] | 169 | if isinstance(node_ids, six.text_type): |
| 170 | node_ids = [node_ids] |
Anton Arefiev | 44f678c | 2016-03-17 12:11:30 +0200 | [diff] [blame] | 171 | 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 Tantsur | 290c96b | 2016-07-01 14:22:51 +0200 | [diff] [blame] | 210 | |
| 211 | def node_cleanup(self, node_id): |
| 212 | if (self.node_show(node_id)['provision_state'] == |
| 213 | BaremetalProvisionStates.AVAILABLE): |
| 214 | return |
Sergii Nozhka | 1eb13cf | 2016-11-04 17:15:50 +0200 | [diff] [blame] | 215 | # 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 Tantsur | 290c96b | 2016-07-01 14:22:51 +0200 | [diff] [blame] | 220 | 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 Nozhka | 1eb13cf | 2016-11-04 17:15:50 +0200 | [diff] [blame] | 232 | 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 Tantsur | 290c96b | 2016-07-01 14:22:51 +0200 | [diff] [blame] | 240 | |
| 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) |