Merge "Test metadata over IPv6"
diff --git a/neutron_tempest_plugin/config.py b/neutron_tempest_plugin/config.py
index 2290d0f..c0e21c1 100644
--- a/neutron_tempest_plugin/config.py
+++ b/neutron_tempest_plugin/config.py
@@ -120,6 +120,18 @@
                     'This is required if advanced image has to be used in '
                     'tests.'),
 
+    # Enable/disable metadata over IPv6 tests. This feature naturally
+    # does not have an API extension, but at the time of first implementation
+    # it works only on victoria+ deployments with dhcp- and/or l3-agents
+    # (which in the gate is the same as non-ovn jobs).
+    cfg.BoolOpt('ipv6_metadata',
+                default=True,
+                help='Enable metadata over IPv6 tests where the feature is '
+                     'implemented, disable where it is not. Use this instead '
+                     'of network-feature-enabled.api_extensions, since API '
+                     'extensions do not make sense for a feature not '
+                     'exposed on the API.'),
+
     # Option for creating QoS policies configures as "shared".
     # The default is false in order to prevent undesired usage
     # while testing in parallel.
diff --git a/neutron_tempest_plugin/scenario/test_metadata.py b/neutron_tempest_plugin/scenario/test_metadata.py
new file mode 100644
index 0000000..c78ff69
--- /dev/null
+++ b/neutron_tempest_plugin/scenario/test_metadata.py
@@ -0,0 +1,142 @@
+# Copyright 2020 Ericsson Software Technology
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+import collections
+
+from neutron_lib import constants as nlib_const
+from oslo_log import log as logging
+from tempest.lib.common.utils import data_utils
+from tempest.lib import decorators
+import testtools
+
+from neutron_tempest_plugin.common import ssh
+from neutron_tempest_plugin import config
+from neutron_tempest_plugin.scenario import base
+
+LOG = logging.getLogger(__name__)
+CONF = config.CONF
+
+Server = collections.namedtuple(
+    'Server', ['floating_ip', 'server', 'ssh_client'])
+
+
+class MetadataTest(base.BaseTempestTestCase):
+
+    """Test metadata access over IPv6 tenant subnet.
+
+    Please note that there is metadata over IPv4 test coverage in tempest:
+
+    tempest.scenario.test_server_basic_ops\
+        .TestServerBasicOps.test_server_basic_ops
+    """
+
+    credentials = ['primary', 'admin']
+    force_tenant_isolation = False
+
+    @classmethod
+    def resource_setup(cls):
+        super(MetadataTest, cls).resource_setup()
+        cls.rand_name = data_utils.rand_name(
+            cls.__name__.rsplit('.', 1)[-1])
+        cls.network = cls.create_network(name=cls.rand_name)
+        cls.subnet_v4 = cls.create_subnet(
+            network=cls.network, name=cls.rand_name)
+        cls.subnet_v6 = cls.create_subnet(
+            network=cls.network, name=cls.rand_name, ip_version=6)
+        cls.router = cls.create_router_by_client()
+        cls.create_router_interface(cls.router['id'], cls.subnet_v4['id'])
+        cls.create_router_interface(cls.router['id'], cls.subnet_v6['id'])
+        cls.keypair = cls.create_keypair(name=cls.rand_name)
+        cls.security_group = cls.create_security_group(name=cls.rand_name)
+        cls.create_loginable_secgroup_rule(cls.security_group['id'])
+
+    def _create_server_with_network(self, network, use_advanced_image=False):
+        port = self._create_server_port(network=network)
+        floating_ip = self.create_floatingip(port=port)
+        ssh_client = self._create_ssh_client(
+            floating_ip=floating_ip, use_advanced_image=use_advanced_image)
+        server = self._create_server(port=port,
+                                     use_advanced_image=use_advanced_image)
+        return Server(
+            floating_ip=floating_ip, server=server, ssh_client=ssh_client)
+
+    def _create_server_port(self, network=None, **params):
+        network = network or self.network
+        return self.create_port(network=network, name=self.rand_name,
+                                security_groups=[self.security_group['id']],
+                                **params)
+
+    def _create_server(self, port, use_advanced_image=False, **params):
+        if use_advanced_image:
+            flavor_ref = CONF.neutron_plugin_options.advanced_image_flavor_ref
+            image_ref = CONF.neutron_plugin_options.advanced_image_ref
+        else:
+            flavor_ref = CONF.compute.flavor_ref
+            image_ref = CONF.compute.image_ref
+        return self.create_server(flavor_ref=flavor_ref,
+                                  image_ref=image_ref,
+                                  key_name=self.keypair['name'],
+                                  networks=[{'port': port['id']}],
+                                  **params)['server']
+
+    def _create_ssh_client(self, floating_ip, use_advanced_image=False):
+        if use_advanced_image:
+            username = CONF.neutron_plugin_options.advanced_image_ssh_user
+        else:
+            username = CONF.validation.image_ssh_user
+        return ssh.Client(host=floating_ip['floating_ip_address'],
+                          username=username,
+                          pkey=self.keypair['private_key'])
+
+    def _assert_has_ssh_connectivity(self, ssh_client):
+        ssh_client.exec_command('true')
+
+    def _get_primary_interface(self, ssh_client):
+        out = ssh_client.exec_command(
+            "ip -6 -br address show scope link up | head -1 | cut -d ' ' -f1")
+        interface = out.strip()
+        if not interface:
+            self.fail(
+                'Could not find a single interface '
+                'with an IPv6 link-local address.')
+        return interface
+
+    @testtools.skipUnless(
+        (CONF.neutron_plugin_options.ipv6_metadata and
+         (CONF.neutron_plugin_options.advanced_image_ref or
+          CONF.neutron_plugin_options.default_image_is_advanced)),
+        'Advanced image and neutron_plugin_options.ipv6_metadata=True '
+        'is required to run this test.')
+    @decorators.idempotent_id('e680949a-f1cc-11ea-b49a-cba39bbbe5ad')
+    def test_metadata_routed(self):
+        use_advanced_image = (
+            not CONF.neutron_plugin_options.default_image_is_advanced)
+
+        vm = self._create_server_with_network(
+            self.network, use_advanced_image=use_advanced_image)
+        self.wait_for_server_active(server=vm.server)
+        self._assert_has_ssh_connectivity(vm.ssh_client)
+        interface = self._get_primary_interface(vm.ssh_client)
+
+        out = vm.ssh_client.exec_command(
+            'curl http://[%(address)s%%25%(interface)s]/' % {
+                'address': nlib_const.METADATA_V6_IP,
+                'interface': interface})
+        self.assertIn('latest', out)
+
+        out = vm.ssh_client.exec_command(
+            'curl http://[%(address)s%%25%(interface)s]/openstack/' % {
+                'address': nlib_const.METADATA_V6_IP,
+                'interface': interface})
+        self.assertIn('latest', out)
diff --git a/zuul.d/master_jobs.yaml b/zuul.d/master_jobs.yaml
index 2c4fc02..5607274 100644
--- a/zuul.d/master_jobs.yaml
+++ b/zuul.d/master_jobs.yaml
@@ -239,6 +239,7 @@
           $TEMPEST_CONFIG:
             neutron_plugin_options:
               available_type_drivers: local,flat,vlan,geneve
+              ipv6_metadata: False
               is_igmp_snooping_enabled: True
 
 - job:
diff --git a/zuul.d/stein_jobs.yaml b/zuul.d/stein_jobs.yaml
index b132cfc..ff6ed38 100644
--- a/zuul.d/stein_jobs.yaml
+++ b/zuul.d/stein_jobs.yaml
@@ -86,6 +86,11 @@
       network_api_extensions: *api_extensions
       devstack_localrc:
         NETWORK_API_EXTENSIONS: "{{ network_api_extensions | join(',') }}"
+      devstack_local_conf:
+        test-config:
+          $TEMPEST_CONFIG:
+            neutron_plugin_options:
+              ipv6_metadata: False
 
 - job:
     name: neutron-tempest-plugin-scenario-openvswitch-iptables_hybrid-stein
@@ -97,6 +102,11 @@
       network_api_extensions: *api_extensions
       devstack_localrc:
         NETWORK_API_EXTENSIONS: "{{ network_api_extensions | join(',') }}"
+      devstack_local_conf:
+        test-config:
+          $TEMPEST_CONFIG:
+            neutron_plugin_options:
+              ipv6_metadata: False
 
 - job:
     name: neutron-tempest-plugin-scenario-linuxbridge-stein
@@ -108,6 +118,11 @@
       network_api_extensions: *api_extensions
       devstack_localrc:
         NETWORK_API_EXTENSIONS: "{{ network_api_extensions | join(',') }}"
+      devstack_local_conf:
+        test-config:
+          $TEMPEST_CONFIG:
+            neutron_plugin_options:
+              ipv6_metadata: False
 
 - job:
     name: neutron-tempest-plugin-dvr-multinode-scenario-stein
diff --git a/zuul.d/train_jobs.yaml b/zuul.d/train_jobs.yaml
index ab560e0..a9cc5be 100644
--- a/zuul.d/train_jobs.yaml
+++ b/zuul.d/train_jobs.yaml
@@ -91,6 +91,11 @@
       network_api_extensions: *api_extensions
       devstack_localrc:
         NETWORK_API_EXTENSIONS: "{{ network_api_extensions | join(',') }}"
+      devstack_local_conf:
+        test-config:
+          $TEMPEST_CONFIG:
+            neutron_plugin_options:
+              ipv6_metadata: False
 
 - job:
     name: neutron-tempest-plugin-scenario-openvswitch-iptables_hybrid-train
@@ -102,6 +107,11 @@
       network_api_extensions: *api_extensions
       devstack_localrc:
         NETWORK_API_EXTENSIONS: "{{ network_api_extensions | join(',') }}"
+      devstack_local_conf:
+        test-config:
+          $TEMPEST_CONFIG:
+            neutron_plugin_options:
+              ipv6_metadata: False
 
 - job:
     name: neutron-tempest-plugin-scenario-linuxbridge-train
@@ -113,6 +123,11 @@
       network_api_extensions: *api_extensions
       devstack_localrc:
         NETWORK_API_EXTENSIONS: "{{ network_api_extensions | join(',') }}"
+      devstack_local_conf:
+        test-config:
+          $TEMPEST_CONFIG:
+            neutron_plugin_options:
+              ipv6_metadata: False
 
 - job:
     name: neutron-tempest-plugin-dvr-multinode-scenario-train
diff --git a/zuul.d/ussuri_jobs.yaml b/zuul.d/ussuri_jobs.yaml
index e15cf55..135d9f5 100644
--- a/zuul.d/ussuri_jobs.yaml
+++ b/zuul.d/ussuri_jobs.yaml
@@ -95,6 +95,11 @@
       network_api_extensions: *api_extensions
       devstack_localrc:
         NETWORK_API_EXTENSIONS: "{{ network_api_extensions | join(',') }}"
+      devstack_local_conf:
+        test-config:
+          $TEMPEST_CONFIG:
+            neutron_plugin_options:
+              ipv6_metadata: False
 
 - job:
     name: neutron-tempest-plugin-scenario-openvswitch-iptables_hybrid-ussuri
@@ -106,6 +111,11 @@
       network_api_extensions: *api_extensions
       devstack_localrc:
         NETWORK_API_EXTENSIONS: "{{ network_api_extensions | join(',') }}"
+      devstack_local_conf:
+        test-config:
+          $TEMPEST_CONFIG:
+            neutron_plugin_options:
+              ipv6_metadata: False
 
 - job:
     name: neutron-tempest-plugin-scenario-linuxbridge-ussuri
@@ -117,6 +127,11 @@
       network_api_extensions: *api_extensions
       devstack_localrc:
         NETWORK_API_EXTENSIONS: "{{ network_api_extensions | join(',') }}"
+      devstack_local_conf:
+        test-config:
+          $TEMPEST_CONFIG:
+            neutron_plugin_options:
+              ipv6_metadata: False
 
 - job:
     name: neutron-tempest-plugin-scenario-ovn-ussuri