Bence Romsics | 6158965 | 2020-09-04 14:49:58 +0200 | [diff] [blame] | 1 | # Copyright 2020 Ericsson Software Technology |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | import collections |
| 16 | |
| 17 | from neutron_lib import constants as nlib_const |
| 18 | from oslo_log import log as logging |
elajkat | 8bbd743 | 2020-11-04 16:41:34 +0100 | [diff] [blame] | 19 | from tempest.common import utils |
Bence Romsics | 6158965 | 2020-09-04 14:49:58 +0200 | [diff] [blame] | 20 | from tempest.lib.common.utils import data_utils |
| 21 | from tempest.lib import decorators |
| 22 | import testtools |
| 23 | |
| 24 | from neutron_tempest_plugin.common import ssh |
| 25 | from neutron_tempest_plugin import config |
| 26 | from neutron_tempest_plugin.scenario import base |
| 27 | |
| 28 | LOG = logging.getLogger(__name__) |
| 29 | CONF = config.CONF |
| 30 | |
| 31 | Server = collections.namedtuple( |
| 32 | 'Server', ['floating_ip', 'server', 'ssh_client']) |
| 33 | |
| 34 | |
| 35 | class MetadataTest(base.BaseTempestTestCase): |
| 36 | |
| 37 | """Test metadata access over IPv6 tenant subnet. |
| 38 | |
| 39 | Please note that there is metadata over IPv4 test coverage in tempest: |
| 40 | |
| 41 | tempest.scenario.test_server_basic_ops\ |
| 42 | .TestServerBasicOps.test_server_basic_ops |
| 43 | """ |
| 44 | |
| 45 | credentials = ['primary', 'admin'] |
| 46 | force_tenant_isolation = False |
| 47 | |
| 48 | @classmethod |
elajkat | 8bbd743 | 2020-11-04 16:41:34 +0100 | [diff] [blame] | 49 | def skip_checks(cls): |
| 50 | super(MetadataTest, cls).skip_checks() |
| 51 | if not utils.is_network_feature_enabled('ipv6_metadata'): |
| 52 | raise cls.skipException("Metadata over IPv6 is not enabled") |
| 53 | |
| 54 | @classmethod |
Bence Romsics | 6158965 | 2020-09-04 14:49:58 +0200 | [diff] [blame] | 55 | def resource_setup(cls): |
| 56 | super(MetadataTest, cls).resource_setup() |
| 57 | cls.rand_name = data_utils.rand_name( |
| 58 | cls.__name__.rsplit('.', 1)[-1]) |
yangjianfeng | 23e40c2 | 2020-11-22 08:42:18 +0000 | [diff] [blame] | 59 | cls.reserve_external_subnet_cidrs() |
Bence Romsics | 6158965 | 2020-09-04 14:49:58 +0200 | [diff] [blame] | 60 | cls.network = cls.create_network(name=cls.rand_name) |
| 61 | cls.subnet_v4 = cls.create_subnet( |
| 62 | network=cls.network, name=cls.rand_name) |
| 63 | cls.subnet_v6 = cls.create_subnet( |
| 64 | network=cls.network, name=cls.rand_name, ip_version=6) |
| 65 | cls.router = cls.create_router_by_client() |
| 66 | cls.create_router_interface(cls.router['id'], cls.subnet_v4['id']) |
| 67 | cls.create_router_interface(cls.router['id'], cls.subnet_v6['id']) |
| 68 | cls.keypair = cls.create_keypair(name=cls.rand_name) |
| 69 | cls.security_group = cls.create_security_group(name=cls.rand_name) |
| 70 | cls.create_loginable_secgroup_rule(cls.security_group['id']) |
| 71 | |
| 72 | def _create_server_with_network(self, network, use_advanced_image=False): |
| 73 | port = self._create_server_port(network=network) |
| 74 | floating_ip = self.create_floatingip(port=port) |
| 75 | ssh_client = self._create_ssh_client( |
| 76 | floating_ip=floating_ip, use_advanced_image=use_advanced_image) |
| 77 | server = self._create_server(port=port, |
| 78 | use_advanced_image=use_advanced_image) |
| 79 | return Server( |
| 80 | floating_ip=floating_ip, server=server, ssh_client=ssh_client) |
| 81 | |
| 82 | def _create_server_port(self, network=None, **params): |
| 83 | network = network or self.network |
| 84 | return self.create_port(network=network, name=self.rand_name, |
| 85 | security_groups=[self.security_group['id']], |
| 86 | **params) |
| 87 | |
| 88 | def _create_server(self, port, use_advanced_image=False, **params): |
| 89 | if use_advanced_image: |
| 90 | flavor_ref = CONF.neutron_plugin_options.advanced_image_flavor_ref |
| 91 | image_ref = CONF.neutron_plugin_options.advanced_image_ref |
| 92 | else: |
| 93 | flavor_ref = CONF.compute.flavor_ref |
| 94 | image_ref = CONF.compute.image_ref |
| 95 | return self.create_server(flavor_ref=flavor_ref, |
| 96 | image_ref=image_ref, |
| 97 | key_name=self.keypair['name'], |
| 98 | networks=[{'port': port['id']}], |
| 99 | **params)['server'] |
| 100 | |
| 101 | def _create_ssh_client(self, floating_ip, use_advanced_image=False): |
| 102 | if use_advanced_image: |
| 103 | username = CONF.neutron_plugin_options.advanced_image_ssh_user |
| 104 | else: |
| 105 | username = CONF.validation.image_ssh_user |
| 106 | return ssh.Client(host=floating_ip['floating_ip_address'], |
| 107 | username=username, |
| 108 | pkey=self.keypair['private_key']) |
| 109 | |
| 110 | def _assert_has_ssh_connectivity(self, ssh_client): |
| 111 | ssh_client.exec_command('true') |
| 112 | |
| 113 | def _get_primary_interface(self, ssh_client): |
| 114 | out = ssh_client.exec_command( |
| 115 | "ip -6 -br address show scope link up | head -1 | cut -d ' ' -f1") |
| 116 | interface = out.strip() |
| 117 | if not interface: |
| 118 | self.fail( |
| 119 | 'Could not find a single interface ' |
| 120 | 'with an IPv6 link-local address.') |
| 121 | return interface |
| 122 | |
| 123 | @testtools.skipUnless( |
elajkat | 8bbd743 | 2020-11-04 16:41:34 +0100 | [diff] [blame] | 124 | CONF.neutron_plugin_options.advanced_image_ref or |
| 125 | CONF.neutron_plugin_options.default_image_is_advanced, |
| 126 | 'Advanced image is required to run this test.') |
Bence Romsics | 6158965 | 2020-09-04 14:49:58 +0200 | [diff] [blame] | 127 | @decorators.idempotent_id('e680949a-f1cc-11ea-b49a-cba39bbbe5ad') |
| 128 | def test_metadata_routed(self): |
| 129 | use_advanced_image = ( |
| 130 | not CONF.neutron_plugin_options.default_image_is_advanced) |
| 131 | |
| 132 | vm = self._create_server_with_network( |
| 133 | self.network, use_advanced_image=use_advanced_image) |
| 134 | self.wait_for_server_active(server=vm.server) |
Slawek Kaplonski | 2211eab | 2020-10-20 16:43:53 +0200 | [diff] [blame] | 135 | self.wait_for_guest_os_ready(vm.server) |
Bence Romsics | 6158965 | 2020-09-04 14:49:58 +0200 | [diff] [blame] | 136 | self._assert_has_ssh_connectivity(vm.ssh_client) |
| 137 | interface = self._get_primary_interface(vm.ssh_client) |
| 138 | |
| 139 | out = vm.ssh_client.exec_command( |
| 140 | 'curl http://[%(address)s%%25%(interface)s]/' % { |
| 141 | 'address': nlib_const.METADATA_V6_IP, |
| 142 | 'interface': interface}) |
| 143 | self.assertIn('latest', out) |
| 144 | |
| 145 | out = vm.ssh_client.exec_command( |
| 146 | 'curl http://[%(address)s%%25%(interface)s]/openstack/' % { |
| 147 | 'address': nlib_const.METADATA_V6_IP, |
| 148 | 'interface': interface}) |
| 149 | self.assertIn('latest', out) |