Anton Arefiev | 6b00356 | 2016-09-13 12:17:29 +0300 | [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 | import six |
| 14 | |
| 15 | from ironic_tempest_plugin.tests.scenario import baremetal_manager |
| 16 | from tempest import config |
| 17 | from tempest import test # noqa |
| 18 | |
| 19 | from ironic_inspector.test.inspector_tempest_plugin.tests import manager |
| 20 | |
| 21 | CONF = config.CONF |
| 22 | |
| 23 | ProvisionStates = baremetal_manager.BaremetalProvisionStates |
| 24 | |
| 25 | |
| 26 | class InspectorDiscoveryTest(manager.InspectorScenarioTest): |
| 27 | @classmethod |
| 28 | def skip_checks(cls): |
| 29 | super(InspectorDiscoveryTest, cls).skip_checks() |
| 30 | if not CONF.baremetal_introspection.auto_discovery_feature: |
| 31 | msg = ("Please, provide a value for node_not_found_hook in " |
| 32 | "processing section of inspector.conf for enable " |
| 33 | "auto-discovery feature.") |
| 34 | raise cls.skipException(msg) |
| 35 | |
| 36 | def setUp(self): |
| 37 | super(InspectorDiscoveryTest, self).setUp() |
| 38 | |
| 39 | discovered_node = self._get_discovery_node() |
| 40 | self.node_info = self._get_node_info(discovered_node) |
| 41 | |
| 42 | rule = self._generate_discovery_rule(self.node_info) |
| 43 | |
| 44 | self.rule_import_from_dict(rule) |
| 45 | self.addCleanup(self.rule_purge) |
| 46 | |
| 47 | def _get_node_info(self, node_uuid): |
| 48 | node = self.node_show(node_uuid) |
| 49 | ports = self.node_port_list(node_uuid) |
| 50 | node['port_macs'] = [port['address'] for port in ports] |
| 51 | return node |
| 52 | |
| 53 | def _get_discovery_node(self): |
| 54 | nodes = self.node_list() |
| 55 | |
| 56 | discovered_node = None |
| 57 | for node in nodes: |
| 58 | if (node['provision_state'] == ProvisionStates.AVAILABLE or |
| 59 | node['provision_state'] == ProvisionStates.ENROLL or |
| 60 | node['provision_state'] is ProvisionStates.NOSTATE): |
| 61 | discovered_node = node['uuid'] |
| 62 | break |
| 63 | |
| 64 | self.assertIsNotNone(discovered_node) |
| 65 | return discovered_node |
| 66 | |
| 67 | def _generate_discovery_rule(self, node): |
| 68 | rule = dict() |
| 69 | rule["description"] = "Node %s discovery rule" % node['name'] |
| 70 | rule["actions"] = [ |
| 71 | {"action": "set-attribute", "path": "/name", |
| 72 | "value": "%s" % node['name']}, |
| 73 | {"action": "set-attribute", "path": "/driver", |
| 74 | "value": "%s" % node['driver']}, |
| 75 | ] |
| 76 | |
| 77 | for key, value in node['driver_info'].items(): |
| 78 | rule["actions"].append( |
| 79 | {"action": "set-attribute", "path": "/driver_info/%s" % key, |
| 80 | "value": "%s" % value}) |
| 81 | rule["conditions"] = [ |
| 82 | {"op": "eq", "field": "data://auto_discovered", "value": True} |
| 83 | ] |
| 84 | return rule |
| 85 | |
| 86 | def verify_node_introspection_data(self, node): |
| 87 | data = self.introspection_data(node['uuid']) |
| 88 | self.assertEqual(data['cpu_arch'], |
| 89 | self.flavor['properties']['cpu_arch']) |
| 90 | self.assertEqual(int(data['memory_mb']), |
| 91 | int(self.flavor['ram'])) |
| 92 | self.assertEqual(int(data['cpus']), int(self.flavor['vcpus'])) |
| 93 | |
| 94 | def verify_node_flavor(self, node): |
| 95 | expected_cpus = self.flavor['vcpus'] |
| 96 | expected_memory_mb = self.flavor['ram'] |
| 97 | expected_cpu_arch = self.flavor['properties']['cpu_arch'] |
| 98 | disk_size = self.flavor['disk'] |
| 99 | ephemeral_size = self.flavor['OS-FLV-EXT-DATA:ephemeral'] |
| 100 | expected_local_gb = disk_size + ephemeral_size |
| 101 | |
| 102 | self.assertEqual(expected_cpus, |
| 103 | int(node['properties']['cpus'])) |
| 104 | self.assertEqual(expected_memory_mb, |
| 105 | int(node['properties']['memory_mb'])) |
| 106 | self.assertEqual(expected_local_gb, |
| 107 | int(node['properties']['local_gb'])) |
| 108 | self.assertEqual(expected_cpu_arch, |
| 109 | node['properties']['cpu_arch']) |
| 110 | |
| 111 | def verify_node_driver_info(self, node_info, inspected_node): |
| 112 | for key in node_info['driver_info']: |
| 113 | self.assertEqual(six.text_type(node_info['driver_info'][key]), |
| 114 | inspected_node['driver_info'].get(key)) |
| 115 | |
| 116 | @test.idempotent_id('dd3abe5e-0d23-488d-bb4e-344cdeff7dcb') |
| 117 | @test.services('baremetal', 'compute') |
| 118 | def test_berametal_auto_discovery(self): |
| 119 | """This test case follows this set of operations: |
| 120 | |
| 121 | * Choose appropriate node, based on provision state; |
| 122 | * Get node info; |
| 123 | * Generate discovery rule; |
| 124 | * Delete discovered node from ironic; |
| 125 | * Start baremetal vm via virsh; |
| 126 | * Wating for node introspection; |
| 127 | * Verify introspected node. |
| 128 | """ |
| 129 | # NOTE(aarefiev): workaround for infra, 'tempest' user doesn't |
| 130 | # have virsh privileges, so lets power on the node via ironic |
| 131 | # and then delete it. Because of node is blacklisted in inspector |
| 132 | # we can't just power on it, therefor start introspection is used |
| 133 | # to whitelist discovered node first. |
| 134 | self.baremetal_client.set_node_provision_state( |
| 135 | self.node_info['uuid'], 'manage') |
| 136 | self.introspection_start(self.node_info['uuid']) |
| 137 | self.wait_power_state( |
| 138 | self.node_info['uuid'], |
| 139 | baremetal_manager.BaremetalPowerStates.POWER_ON) |
| 140 | self.node_delete(self.node_info['uuid']) |
| 141 | |
| 142 | self.wait_for_node(self.node_info['name']) |
| 143 | |
| 144 | inspected_node = self.node_show(self.node_info['name']) |
| 145 | self.verify_node_flavor(inspected_node) |
| 146 | self.verify_node_introspection_data(inspected_node) |
| 147 | self.verify_node_driver_info(self.node_info, inspected_node) |