blob: 05f0f04106ac505c4fb9d9b2f51690f6763da32c [file] [log] [blame]
Bence Romsics61589652020-09-04 14:49:58 +02001# 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
15import collections
16
17from neutron_lib import constants as nlib_const
18from oslo_log import log as logging
elajkat8bbd7432020-11-04 16:41:34 +010019from tempest.common import utils
Bence Romsics61589652020-09-04 14:49:58 +020020from tempest.lib.common.utils import data_utils
21from tempest.lib import decorators
22import testtools
23
24from neutron_tempest_plugin.common import ssh
25from neutron_tempest_plugin import config
elajkat85472b62021-01-27 11:34:04 +010026from neutron_tempest_plugin import exceptions
Bence Romsics61589652020-09-04 14:49:58 +020027from neutron_tempest_plugin.scenario import base
28
29LOG = logging.getLogger(__name__)
30CONF = config.CONF
31
32Server = collections.namedtuple(
33 'Server', ['floating_ip', 'server', 'ssh_client'])
34
35
36class MetadataTest(base.BaseTempestTestCase):
37
38 """Test metadata access over IPv6 tenant subnet.
39
40 Please note that there is metadata over IPv4 test coverage in tempest:
41
42 tempest.scenario.test_server_basic_ops\
43 .TestServerBasicOps.test_server_basic_ops
44 """
45
46 credentials = ['primary', 'admin']
47 force_tenant_isolation = False
48
49 @classmethod
elajkat8bbd7432020-11-04 16:41:34 +010050 def skip_checks(cls):
51 super(MetadataTest, cls).skip_checks()
52 if not utils.is_network_feature_enabled('ipv6_metadata'):
53 raise cls.skipException("Metadata over IPv6 is not enabled")
54
55 @classmethod
Bence Romsics61589652020-09-04 14:49:58 +020056 def resource_setup(cls):
57 super(MetadataTest, cls).resource_setup()
58 cls.rand_name = data_utils.rand_name(
59 cls.__name__.rsplit('.', 1)[-1])
yangjianfeng23e40c22020-11-22 08:42:18 +000060 cls.reserve_external_subnet_cidrs()
Bence Romsics61589652020-09-04 14:49:58 +020061 cls.network = cls.create_network(name=cls.rand_name)
62 cls.subnet_v4 = cls.create_subnet(
63 network=cls.network, name=cls.rand_name)
64 cls.subnet_v6 = cls.create_subnet(
65 network=cls.network, name=cls.rand_name, ip_version=6)
66 cls.router = cls.create_router_by_client()
67 cls.create_router_interface(cls.router['id'], cls.subnet_v4['id'])
68 cls.create_router_interface(cls.router['id'], cls.subnet_v6['id'])
69 cls.keypair = cls.create_keypair(name=cls.rand_name)
70 cls.security_group = cls.create_security_group(name=cls.rand_name)
71 cls.create_loginable_secgroup_rule(cls.security_group['id'])
72
73 def _create_server_with_network(self, network, use_advanced_image=False):
74 port = self._create_server_port(network=network)
75 floating_ip = self.create_floatingip(port=port)
76 ssh_client = self._create_ssh_client(
77 floating_ip=floating_ip, use_advanced_image=use_advanced_image)
78 server = self._create_server(port=port,
79 use_advanced_image=use_advanced_image)
80 return Server(
81 floating_ip=floating_ip, server=server, ssh_client=ssh_client)
82
83 def _create_server_port(self, network=None, **params):
84 network = network or self.network
85 return self.create_port(network=network, name=self.rand_name,
86 security_groups=[self.security_group['id']],
87 **params)
88
89 def _create_server(self, port, use_advanced_image=False, **params):
90 if use_advanced_image:
91 flavor_ref = CONF.neutron_plugin_options.advanced_image_flavor_ref
92 image_ref = CONF.neutron_plugin_options.advanced_image_ref
93 else:
94 flavor_ref = CONF.compute.flavor_ref
95 image_ref = CONF.compute.image_ref
96 return self.create_server(flavor_ref=flavor_ref,
97 image_ref=image_ref,
98 key_name=self.keypair['name'],
99 networks=[{'port': port['id']}],
100 **params)['server']
101
102 def _create_ssh_client(self, floating_ip, use_advanced_image=False):
103 if use_advanced_image:
104 username = CONF.neutron_plugin_options.advanced_image_ssh_user
105 else:
106 username = CONF.validation.image_ssh_user
107 return ssh.Client(host=floating_ip['floating_ip_address'],
108 username=username,
109 pkey=self.keypair['private_key'])
110
111 def _assert_has_ssh_connectivity(self, ssh_client):
112 ssh_client.exec_command('true')
113
114 def _get_primary_interface(self, ssh_client):
115 out = ssh_client.exec_command(
116 "ip -6 -br address show scope link up | head -1 | cut -d ' ' -f1")
117 interface = out.strip()
118 if not interface:
119 self.fail(
120 'Could not find a single interface '
121 'with an IPv6 link-local address.')
122 return interface
123
124 @testtools.skipUnless(
elajkat8bbd7432020-11-04 16:41:34 +0100125 CONF.neutron_plugin_options.advanced_image_ref or
126 CONF.neutron_plugin_options.default_image_is_advanced,
127 'Advanced image is required to run this test.')
Bence Romsics61589652020-09-04 14:49:58 +0200128 @decorators.idempotent_id('e680949a-f1cc-11ea-b49a-cba39bbbe5ad')
129 def test_metadata_routed(self):
130 use_advanced_image = (
131 not CONF.neutron_plugin_options.default_image_is_advanced)
132
133 vm = self._create_server_with_network(
134 self.network, use_advanced_image=use_advanced_image)
135 self.wait_for_server_active(server=vm.server)
Slawek Kaplonski2211eab2020-10-20 16:43:53 +0200136 self.wait_for_guest_os_ready(vm.server)
elajkat85472b62021-01-27 11:34:04 +0100137 self.check_connectivity(host=vm.floating_ip['floating_ip_address'],
138 ssh_client=vm.ssh_client)
Bence Romsics61589652020-09-04 14:49:58 +0200139 interface = self._get_primary_interface(vm.ssh_client)
140
elajkat85472b62021-01-27 11:34:04 +0100141 try:
142 out = vm.ssh_client.exec_command(
143 'curl http://[%(address)s%%25%(interface)s]/' % {
144 'address': nlib_const.METADATA_V6_IP,
145 'interface': interface})
146 self.assertIn('latest', out)
Bence Romsics61589652020-09-04 14:49:58 +0200147
elajkat85472b62021-01-27 11:34:04 +0100148 out = vm.ssh_client.exec_command(
149 'curl http://[%(address)s%%25%(interface)s]/openstack/' % {
150 'address': nlib_const.METADATA_V6_IP,
151 'interface': interface})
152 self.assertIn('latest', out)
153 except exceptions.SSHExecCommandFailed:
154 self._log_console_output()
155 self._log_local_network_status()