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 | |
| 13 | |
| 14 | import os |
| 15 | import time |
| 16 | |
| 17 | from tempest import config |
| 18 | |
| 19 | from ironic_inspector.test.inspector_tempest_plugin import exceptions |
| 20 | from ironic_inspector.test.inspector_tempest_plugin.services import \ |
| 21 | introspection_client |
| 22 | from ironic_tempest_plugin.tests.scenario.baremetal_manager import \ |
| 23 | BaremetalScenarioTest |
| 24 | |
| 25 | |
| 26 | CONF = config.CONF |
| 27 | |
| 28 | |
| 29 | class InspectorScenarioTest(BaremetalScenarioTest): |
| 30 | """Provide harness to do Inspector scenario tests.""" |
| 31 | |
| 32 | credentials = ['primary', 'admin'] |
| 33 | |
| 34 | @classmethod |
| 35 | def setup_clients(cls): |
| 36 | super(InspectorScenarioTest, cls).setup_clients() |
| 37 | inspector_manager = introspection_client.Manager() |
| 38 | cls.introspection_client = inspector_manager.introspection_client |
| 39 | |
| 40 | def setUp(self): |
| 41 | super(InspectorScenarioTest, self).setUp() |
| 42 | self.flavor = self.baremetal_flavor() |
| 43 | |
| 44 | def item_filter(self, list_method, show_method, |
| 45 | filter=lambda item: True, items=None): |
| 46 | if items is None: |
| 47 | items = [show_method(item['uuid']) for item in |
| 48 | list_method()] |
| 49 | return [item for item in items if filter(item)] |
| 50 | |
| 51 | def node_list(self): |
| 52 | return self.baremetal_client.list_nodes()[1]['nodes'] |
| 53 | |
| 54 | def node_update(self, uuid, patch): |
| 55 | return self.baremetal_client.update_node(uuid, **patch) |
| 56 | |
| 57 | def node_show(self, uuid): |
| 58 | return self.baremetal_client.show_node(uuid)[1] |
| 59 | |
| 60 | def node_filter(self, filter=lambda node: True, nodes=None): |
| 61 | return self.item_filter(self.node_list, self.node_show, |
| 62 | filter=filter, items=nodes) |
| 63 | |
| 64 | def hypervisor_stats(self): |
| 65 | return (self.admin_manager.hypervisor_client. |
| 66 | show_hypervisor_statistics()) |
| 67 | |
| 68 | def server_show(self, uuid): |
| 69 | self.servers_client.show_server(uuid) |
| 70 | |
| 71 | def rule_purge(self): |
| 72 | self.introspection_client.purge_rules() |
| 73 | |
| 74 | def rule_import(self, rule_path): |
| 75 | self.introspection_client.import_rule(rule_path) |
| 76 | |
| 77 | def introspection_status(self, uuid): |
| 78 | return self.introspection_client.get_status(uuid)[1] |
| 79 | |
| 80 | def introspection_data(self, uuid): |
| 81 | return self.introspection_client.get_data(uuid)[1] |
| 82 | |
| 83 | def baremetal_flavor(self): |
| 84 | flavor_id = CONF.compute.flavor_ref |
| 85 | flavor = self.flavors_client.show_flavor(flavor_id)['flavor'] |
| 86 | flavor['properties'] = self.flavors_client.list_flavor_extra_specs( |
| 87 | flavor_id)['extra_specs'] |
| 88 | return flavor |
| 89 | |
| 90 | def get_rule_path(self, rule_file): |
| 91 | base_path = os.path.split( |
| 92 | os.path.dirname(os.path.abspath(__file__)))[0] |
| 93 | base_path = os.path.split(base_path)[0] |
| 94 | return os.path.join(base_path, "inspector_tempest_plugin", |
| 95 | "rules", rule_file) |
| 96 | |
| 97 | # TODO(aarefiev): switch to call_until_true |
| 98 | def wait_for_introspection_finished(self, node_ids): |
| 99 | """Waits for introspection of baremetal nodes to finish. |
| 100 | |
| 101 | """ |
| 102 | start = int(time.time()) |
| 103 | not_introspected = {node_id for node_id in node_ids} |
| 104 | |
| 105 | while not_introspected: |
| 106 | time.sleep(CONF.baremetal_introspection.introspection_sleep) |
| 107 | for node_id in node_ids: |
| 108 | status = self.introspection_status(node_id) |
| 109 | if status['finished']: |
| 110 | if status['error']: |
| 111 | message = ('Node %(node_id)s introspection failed ' |
| 112 | 'with %(error)s.' % |
| 113 | {'node_id': node_id, |
| 114 | 'error': status['error']}) |
| 115 | raise exceptions.IntrospectionFailed(message) |
| 116 | not_introspected = not_introspected - {node_id} |
| 117 | |
| 118 | if (int(time.time()) - start >= |
| 119 | CONF.baremetal_introspection.introspection_timeout): |
| 120 | message = ('Introspection timed out for nodes: %s' % |
| 121 | not_introspected) |
| 122 | raise exceptions.IntrospectionTimeout(message) |
| 123 | |
| 124 | def wait_for_nova_aware_of_bvms(self): |
| 125 | start = int(time.time()) |
| 126 | while True: |
| 127 | time.sleep(CONF.baremetal_introspection.hypervisor_update_sleep) |
| 128 | stats = self.hypervisor_stats() |
| 129 | expected_cpus = self.baremetal_flavor()['vcpus'] |
| 130 | if int(stats['hypervisor_statistics']['vcpus']) >= expected_cpus: |
| 131 | break |
| 132 | |
| 133 | timeout = CONF.baremetal_introspection.hypervisor_update_timeout |
| 134 | if (int(time.time()) - start >= timeout): |
| 135 | message = ( |
| 136 | 'Timeout while waiting for nova hypervisor-stats: ' |
| 137 | '%(stats)s required time (%(timeout)s s).' % |
| 138 | {'stats': stats, |
| 139 | 'timeout': timeout}) |
| 140 | raise exceptions.HypervisorUpdateTimeout(message) |