blob: b62473ca8825e2e3a2477dd3d05e24da16396ca3 [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
13
14import os
15import time
16
17from tempest import config
18
19from ironic_inspector.test.inspector_tempest_plugin import exceptions
20from ironic_inspector.test.inspector_tempest_plugin.services import \
21 introspection_client
22from ironic_tempest_plugin.tests.scenario.baremetal_manager import \
23 BaremetalScenarioTest
24
25
26CONF = config.CONF
27
28
29class 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
Anton Arefiev84a09c62016-07-19 19:35:18 +030097 def boot_instance(self):
98 return super(InspectorScenarioTest, self).boot_instance()
99
100 def terminate_instance(self, instance):
101 return super(InspectorScenarioTest, self).terminate_instance(instance)
102
Anton Arefiev44f678c2016-03-17 12:11:30 +0200103 # TODO(aarefiev): switch to call_until_true
104 def wait_for_introspection_finished(self, node_ids):
105 """Waits for introspection of baremetal nodes to finish.
106
107 """
108 start = int(time.time())
109 not_introspected = {node_id for node_id in node_ids}
110
111 while not_introspected:
112 time.sleep(CONF.baremetal_introspection.introspection_sleep)
113 for node_id in node_ids:
114 status = self.introspection_status(node_id)
115 if status['finished']:
116 if status['error']:
117 message = ('Node %(node_id)s introspection failed '
118 'with %(error)s.' %
119 {'node_id': node_id,
120 'error': status['error']})
121 raise exceptions.IntrospectionFailed(message)
122 not_introspected = not_introspected - {node_id}
123
124 if (int(time.time()) - start >=
125 CONF.baremetal_introspection.introspection_timeout):
126 message = ('Introspection timed out for nodes: %s' %
127 not_introspected)
128 raise exceptions.IntrospectionTimeout(message)
129
130 def wait_for_nova_aware_of_bvms(self):
131 start = int(time.time())
132 while True:
133 time.sleep(CONF.baremetal_introspection.hypervisor_update_sleep)
134 stats = self.hypervisor_stats()
135 expected_cpus = self.baremetal_flavor()['vcpus']
136 if int(stats['hypervisor_statistics']['vcpus']) >= expected_cpus:
137 break
138
139 timeout = CONF.baremetal_introspection.hypervisor_update_timeout
140 if (int(time.time()) - start >= timeout):
141 message = (
142 'Timeout while waiting for nova hypervisor-stats: '
143 '%(stats)s required time (%(timeout)s s).' %
144 {'stats': stats,
145 'timeout': timeout})
146 raise exceptions.HypervisorUpdateTimeout(message)