blob: 284040947fe422c07f4242def61824ded1a03f90 [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
26from neutron_tempest_plugin.scenario import base
27
28LOG = logging.getLogger(__name__)
29CONF = config.CONF
30
31Server = collections.namedtuple(
32 'Server', ['floating_ip', 'server', 'ssh_client'])
33
34
35class 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
elajkat8bbd7432020-11-04 16:41:34 +010049 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 Romsics61589652020-09-04 14:49:58 +020055 def resource_setup(cls):
56 super(MetadataTest, cls).resource_setup()
57 cls.rand_name = data_utils.rand_name(
58 cls.__name__.rsplit('.', 1)[-1])
59 cls.network = cls.create_network(name=cls.rand_name)
60 cls.subnet_v4 = cls.create_subnet(
61 network=cls.network, name=cls.rand_name)
62 cls.subnet_v6 = cls.create_subnet(
63 network=cls.network, name=cls.rand_name, ip_version=6)
64 cls.router = cls.create_router_by_client()
65 cls.create_router_interface(cls.router['id'], cls.subnet_v4['id'])
66 cls.create_router_interface(cls.router['id'], cls.subnet_v6['id'])
67 cls.keypair = cls.create_keypair(name=cls.rand_name)
68 cls.security_group = cls.create_security_group(name=cls.rand_name)
69 cls.create_loginable_secgroup_rule(cls.security_group['id'])
70
71 def _create_server_with_network(self, network, use_advanced_image=False):
72 port = self._create_server_port(network=network)
73 floating_ip = self.create_floatingip(port=port)
74 ssh_client = self._create_ssh_client(
75 floating_ip=floating_ip, use_advanced_image=use_advanced_image)
76 server = self._create_server(port=port,
77 use_advanced_image=use_advanced_image)
78 return Server(
79 floating_ip=floating_ip, server=server, ssh_client=ssh_client)
80
81 def _create_server_port(self, network=None, **params):
82 network = network or self.network
83 return self.create_port(network=network, name=self.rand_name,
84 security_groups=[self.security_group['id']],
85 **params)
86
87 def _create_server(self, port, use_advanced_image=False, **params):
88 if use_advanced_image:
89 flavor_ref = CONF.neutron_plugin_options.advanced_image_flavor_ref
90 image_ref = CONF.neutron_plugin_options.advanced_image_ref
91 else:
92 flavor_ref = CONF.compute.flavor_ref
93 image_ref = CONF.compute.image_ref
94 return self.create_server(flavor_ref=flavor_ref,
95 image_ref=image_ref,
96 key_name=self.keypair['name'],
97 networks=[{'port': port['id']}],
98 **params)['server']
99
100 def _create_ssh_client(self, floating_ip, use_advanced_image=False):
101 if use_advanced_image:
102 username = CONF.neutron_plugin_options.advanced_image_ssh_user
103 else:
104 username = CONF.validation.image_ssh_user
105 return ssh.Client(host=floating_ip['floating_ip_address'],
106 username=username,
107 pkey=self.keypair['private_key'])
108
109 def _assert_has_ssh_connectivity(self, ssh_client):
110 ssh_client.exec_command('true')
111
112 def _get_primary_interface(self, ssh_client):
113 out = ssh_client.exec_command(
114 "ip -6 -br address show scope link up | head -1 | cut -d ' ' -f1")
115 interface = out.strip()
116 if not interface:
117 self.fail(
118 'Could not find a single interface '
119 'with an IPv6 link-local address.')
120 return interface
121
122 @testtools.skipUnless(
elajkat8bbd7432020-11-04 16:41:34 +0100123 CONF.neutron_plugin_options.advanced_image_ref or
124 CONF.neutron_plugin_options.default_image_is_advanced,
125 'Advanced image is required to run this test.')
Bence Romsics61589652020-09-04 14:49:58 +0200126 @decorators.idempotent_id('e680949a-f1cc-11ea-b49a-cba39bbbe5ad')
127 def test_metadata_routed(self):
128 use_advanced_image = (
129 not CONF.neutron_plugin_options.default_image_is_advanced)
130
131 vm = self._create_server_with_network(
132 self.network, use_advanced_image=use_advanced_image)
133 self.wait_for_server_active(server=vm.server)
Slawek Kaplonski2211eab2020-10-20 16:43:53 +0200134 self.wait_for_guest_os_ready(vm.server)
Bence Romsics61589652020-09-04 14:49:58 +0200135 self._assert_has_ssh_connectivity(vm.ssh_client)
136 interface = self._get_primary_interface(vm.ssh_client)
137
138 out = vm.ssh_client.exec_command(
139 'curl http://[%(address)s%%25%(interface)s]/' % {
140 'address': nlib_const.METADATA_V6_IP,
141 'interface': interface})
142 self.assertIn('latest', out)
143
144 out = vm.ssh_client.exec_command(
145 'curl http://[%(address)s%%25%(interface)s]/openstack/' % {
146 'address': nlib_const.METADATA_V6_IP,
147 'interface': interface})
148 self.assertIn('latest', out)