blob: 1c63816d086853b9b5addf2d8809a581f019b879 [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
Miguel Lavallee0d03fc2024-08-07 19:27:25 -050015import base64
Bence Romsics61589652020-09-04 14:49:58 +020016import collections
Miguel Lavallee0d03fc2024-08-07 19:27:25 -050017import textwrap
18import time
Bence Romsics61589652020-09-04 14:49:58 +020019
20from neutron_lib import constants as nlib_const
21from oslo_log import log as logging
elajkat8bbd7432020-11-04 16:41:34 +010022from tempest.common import utils
Bence Romsics61589652020-09-04 14:49:58 +020023from tempest.lib.common.utils import data_utils
24from tempest.lib import decorators
Eduardo Olivaresf10618e2022-04-25 13:06:28 +020025from tempest.lib import exceptions
Bence Romsics61589652020-09-04 14:49:58 +020026import testtools
27
28from neutron_tempest_plugin.common import ssh
29from neutron_tempest_plugin import config
30from neutron_tempest_plugin.scenario import base
31
32LOG = logging.getLogger(__name__)
33CONF = config.CONF
34
35Server = collections.namedtuple(
36 'Server', ['floating_ip', 'server', 'ssh_client'])
37
Miguel Lavallee0d03fc2024-08-07 19:27:25 -050038QUERY_MSG = 'Queried the metadata service over IPv6'
39
Bence Romsics61589652020-09-04 14:49:58 +020040
41class MetadataTest(base.BaseTempestTestCase):
42
43 """Test metadata access over IPv6 tenant subnet.
44
45 Please note that there is metadata over IPv4 test coverage in tempest:
46
47 tempest.scenario.test_server_basic_ops\
48 .TestServerBasicOps.test_server_basic_ops
49 """
50
51 credentials = ['primary', 'admin']
52 force_tenant_isolation = False
53
54 @classmethod
elajkat8bbd7432020-11-04 16:41:34 +010055 def skip_checks(cls):
56 super(MetadataTest, cls).skip_checks()
57 if not utils.is_network_feature_enabled('ipv6_metadata'):
58 raise cls.skipException("Metadata over IPv6 is not enabled")
59
60 @classmethod
Bence Romsics61589652020-09-04 14:49:58 +020061 def resource_setup(cls):
62 super(MetadataTest, cls).resource_setup()
63 cls.rand_name = data_utils.rand_name(
64 cls.__name__.rsplit('.', 1)[-1])
yangjianfeng23e40c22020-11-22 08:42:18 +000065 cls.reserve_external_subnet_cidrs()
Bence Romsics61589652020-09-04 14:49:58 +020066 cls.network = cls.create_network(name=cls.rand_name)
67 cls.subnet_v4 = cls.create_subnet(
68 network=cls.network, name=cls.rand_name)
69 cls.subnet_v6 = cls.create_subnet(
70 network=cls.network, name=cls.rand_name, ip_version=6)
71 cls.router = cls.create_router_by_client()
72 cls.create_router_interface(cls.router['id'], cls.subnet_v4['id'])
73 cls.create_router_interface(cls.router['id'], cls.subnet_v6['id'])
74 cls.keypair = cls.create_keypair(name=cls.rand_name)
75 cls.security_group = cls.create_security_group(name=cls.rand_name)
76 cls.create_loginable_secgroup_rule(cls.security_group['id'])
77
78 def _create_server_with_network(self, network, use_advanced_image=False):
79 port = self._create_server_port(network=network)
80 floating_ip = self.create_floatingip(port=port)
81 ssh_client = self._create_ssh_client(
82 floating_ip=floating_ip, use_advanced_image=use_advanced_image)
83 server = self._create_server(port=port,
84 use_advanced_image=use_advanced_image)
85 return Server(
86 floating_ip=floating_ip, server=server, ssh_client=ssh_client)
87
88 def _create_server_port(self, network=None, **params):
89 network = network or self.network
90 return self.create_port(network=network, name=self.rand_name,
91 security_groups=[self.security_group['id']],
92 **params)
93
Miguel Lavallee0d03fc2024-08-07 19:27:25 -050094 def _create_server(self, port=None, network_id=None,
95 use_advanced_image=False, **params):
Bence Romsics61589652020-09-04 14:49:58 +020096 if use_advanced_image:
97 flavor_ref = CONF.neutron_plugin_options.advanced_image_flavor_ref
98 image_ref = CONF.neutron_plugin_options.advanced_image_ref
99 else:
100 flavor_ref = CONF.compute.flavor_ref
101 image_ref = CONF.compute.image_ref
Miguel Lavallee0d03fc2024-08-07 19:27:25 -0500102 if port:
103 networks = [{'port': port['id']}]
104 else:
105 networks = [{'uuid': network_id}]
Bence Romsics61589652020-09-04 14:49:58 +0200106 return self.create_server(flavor_ref=flavor_ref,
107 image_ref=image_ref,
108 key_name=self.keypair['name'],
Miguel Lavallee0d03fc2024-08-07 19:27:25 -0500109 networks=networks,
Bence Romsics61589652020-09-04 14:49:58 +0200110 **params)['server']
111
Miguel Lavallee0d03fc2024-08-07 19:27:25 -0500112 def _get_metadata_query_script(self):
113 sheebang_line = '\n#!/bin/bash'
114 curl_cmd = '\ncurl http://[%(address)s' % {'address':
115 nlib_const.METADATA_V6_IP}
116 ip_cmd = ("%$(ip -6 -br address show scope link up | head -1 | "
117 "cut -d ' ' -f1)]/openstack/")
118 echo_cmd = '\necho %s' % QUERY_MSG
119 script = '%s%s%s%s' % (sheebang_line, curl_cmd, ip_cmd, echo_cmd)
120 script_clean = textwrap.dedent(script).lstrip().encode('utf8')
121 script_b64 = base64.b64encode(script_clean)
122 return {'user_data': script_b64}
123
124 def _wait_for_metadata_query_msg(self, vm):
125 timeout = 300
126 start_time = int(time.time())
127 while int(time.time()) - start_time < timeout:
128 console_output = self.os_primary.servers_client.get_console_output(
129 vm['id'])['output']
130 pos = console_output.find(QUERY_MSG)
131 if pos > -1:
132 return console_output, pos
133 time.sleep(30)
134 self.fail('Failed to find metadata query message in console log %s' %
135 console_output)
136
Bence Romsics61589652020-09-04 14:49:58 +0200137 def _create_ssh_client(self, floating_ip, use_advanced_image=False):
138 if use_advanced_image:
139 username = CONF.neutron_plugin_options.advanced_image_ssh_user
140 else:
141 username = CONF.validation.image_ssh_user
142 return ssh.Client(host=floating_ip['floating_ip_address'],
143 username=username,
144 pkey=self.keypair['private_key'])
145
146 def _assert_has_ssh_connectivity(self, ssh_client):
147 ssh_client.exec_command('true')
148
149 def _get_primary_interface(self, ssh_client):
150 out = ssh_client.exec_command(
151 "ip -6 -br address show scope link up | head -1 | cut -d ' ' -f1")
152 interface = out.strip()
153 if not interface:
154 self.fail(
155 'Could not find a single interface '
156 'with an IPv6 link-local address.')
157 return interface
158
159 @testtools.skipUnless(
elajkat8bbd7432020-11-04 16:41:34 +0100160 CONF.neutron_plugin_options.advanced_image_ref or
161 CONF.neutron_plugin_options.default_image_is_advanced,
162 'Advanced image is required to run this test.')
Bence Romsics61589652020-09-04 14:49:58 +0200163 @decorators.idempotent_id('e680949a-f1cc-11ea-b49a-cba39bbbe5ad')
164 def test_metadata_routed(self):
165 use_advanced_image = (
166 not CONF.neutron_plugin_options.default_image_is_advanced)
167
168 vm = self._create_server_with_network(
169 self.network, use_advanced_image=use_advanced_image)
170 self.wait_for_server_active(server=vm.server)
Slawek Kaplonski2211eab2020-10-20 16:43:53 +0200171 self.wait_for_guest_os_ready(vm.server)
elajkat85472b62021-01-27 11:34:04 +0100172 self.check_connectivity(host=vm.floating_ip['floating_ip_address'],
173 ssh_client=vm.ssh_client)
Bence Romsics61589652020-09-04 14:49:58 +0200174 interface = self._get_primary_interface(vm.ssh_client)
175
elajkat85472b62021-01-27 11:34:04 +0100176 try:
177 out = vm.ssh_client.exec_command(
178 'curl http://[%(address)s%%25%(interface)s]/' % {
179 'address': nlib_const.METADATA_V6_IP,
180 'interface': interface})
181 self.assertIn('latest', out)
Bence Romsics61589652020-09-04 14:49:58 +0200182
elajkat85472b62021-01-27 11:34:04 +0100183 out = vm.ssh_client.exec_command(
184 'curl http://[%(address)s%%25%(interface)s]/openstack/' % {
185 'address': nlib_const.METADATA_V6_IP,
186 'interface': interface})
187 self.assertIn('latest', out)
188 except exceptions.SSHExecCommandFailed:
189 self._log_console_output()
190 self._log_local_network_status()
Miguel Lavallee0d03fc2024-08-07 19:27:25 -0500191
192 @testtools.skipUnless(
193 CONF.neutron_plugin_options.advanced_image_ref or
194 CONF.neutron_plugin_options.default_image_is_advanced,
195 'Advanced image is required to run this test.')
Miguel Lavallee0d03fc2024-08-07 19:27:25 -0500196 @decorators.idempotent_id('7542892a-d132-471c-addb-172dcf888ff6')
197 def test_metadata_ipv6_only_network(self):
198 ipv6_network = self.create_network()
Miguel Lavalle4a0b2342024-08-18 17:20:51 -0500199 ipv6_subnet = self.create_subnet(network=ipv6_network, ip_version=6,
200 ipv6_ra_mode="slaac",
201 ipv6_address_mode="slaac")
202 if not CONF.neutron_plugin_options.firewall_driver == 'ovn':
203 self.create_router_interface(self.router['id'], ipv6_subnet['id'])
Miguel Lavallee0d03fc2024-08-07 19:27:25 -0500204 use_advanced_image = (
205 not CONF.neutron_plugin_options.default_image_is_advanced)
206 params = self._get_metadata_query_script()
207 params['config_drive'] = True
208 vm = self._create_server(
209 network_id=ipv6_network['id'],
210 use_advanced_image=use_advanced_image, **params)
211 self.wait_for_server_active(server=vm)
212 self.wait_for_guest_os_ready(vm)
213 console_output, pos = self._wait_for_metadata_query_msg(vm)
214 self.assertIn('latest', console_output[pos - 100:])