Merge "Fix ML2 revision_number handling in port updates"
diff --git a/neutron/tests/tempest/api/admin/test_networks.py b/neutron/tests/tempest/api/admin/test_networks.py
new file mode 100644
index 0000000..1068c0b
--- /dev/null
+++ b/neutron/tests/tempest/api/admin/test_networks.py
@@ -0,0 +1,70 @@
+#    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.
+
+from oslo_utils import uuidutils
+from tempest.lib import exceptions as lib_exc
+from tempest import test
+
+from neutron.tests.tempest.api import base
+
+
+class NetworksTestAdmin(base.BaseAdminNetworkTest):
+
+    @test.idempotent_id('d3c76044-d067-4cb0-ae47-8cdd875c7f67')
+    @test.requires_ext(extension="project-id", service="network")
+    def test_admin_create_network_keystone_v3(self):
+        project_id = self.client.tenant_id  # non-admin
+
+        name = 'admin-created-with-project_id'
+        new_net = self.create_network_keystone_v3(name, project_id,
+            client=self.admin_client)
+        self.assertEqual(name, new_net['name'])
+        self.assertEqual(project_id, new_net['project_id'])
+        self.assertEqual(project_id, new_net['tenant_id'])
+
+        body = self.client.list_networks(id=new_net['id'])
+        lookup_net = body['networks'][0]
+        self.assertEqual(name, lookup_net['name'])
+        self.assertEqual(project_id, lookup_net['project_id'])
+        self.assertEqual(project_id, lookup_net['tenant_id'])
+
+    @test.idempotent_id('8d21aaca-4364-4eb9-8b79-44b4fff6373b')
+    @test.requires_ext(extension="project-id", service="network")
+    def test_admin_create_network_keystone_v3_and_tenant(self):
+        project_id = self.client.tenant_id  # non-admin
+
+        name = 'created-with-project-and-tenant'
+        new_net = self.create_network_keystone_v3(
+            name, project_id, tenant_id=project_id, client=self.admin_client)
+        self.assertEqual(name, new_net['name'])
+        self.assertEqual(project_id, new_net['project_id'])
+        self.assertEqual(project_id, new_net['tenant_id'])
+
+        body = self.client.list_networks(id=new_net['id'])
+        lookup_net = body['networks'][0]
+        self.assertEqual(name, lookup_net['name'])
+        self.assertEqual(project_id, lookup_net['project_id'])
+        self.assertEqual(project_id, lookup_net['tenant_id'])
+
+    @test.idempotent_id('08b92179-669d-45ee-8233-ef6611190809')
+    @test.requires_ext(extension="project-id", service="network")
+    def test_admin_create_network_keystone_v3_and_other_tenant(self):
+        project_id = self.client.tenant_id  # non-admin
+        other_tenant = uuidutils.generate_uuid()
+
+        name = 'created-with-project-and-other-tenant'
+        e = self.assertRaises(lib_exc.BadRequest,
+                              self.create_network_keystone_v3, name,
+                              project_id, tenant_id=other_tenant,
+                              client=self.admin_client)
+        expected_message = "'project_id' and 'tenant_id' do not match"
+        self.assertEqual(expected_message, e.resp_body['message'])
diff --git a/neutron/tests/tempest/api/base.py b/neutron/tests/tempest/api/base.py
index ffb2dfb..39c714c 100644
--- a/neutron/tests/tempest/api/base.py
+++ b/neutron/tests/tempest/api/base.py
@@ -93,7 +93,7 @@
         super(BaseNetworkTest, cls).resource_setup()
 
         cls.networks = []
-        cls.shared_networks = []
+        cls.admin_networks = []
         cls.subnets = []
         cls.ports = []
         cls.routers = []
@@ -162,8 +162,8 @@
                 cls._try_delete_resource(cls.client.delete_network,
                                          network['id'])
 
-            # Clean up shared networks
-            for network in cls.shared_networks:
+            # Clean up admin networks
+            for network in cls.admin_networks:
                 cls._try_delete_resource(cls.admin_client.delete_network,
                                          network['id'])
 
@@ -222,7 +222,24 @@
         post_body.update({'name': network_name, 'shared': True})
         body = cls.admin_client.create_network(**post_body)
         network = body['network']
-        cls.shared_networks.append(network)
+        cls.admin_networks.append(network)
+        return network
+
+    @classmethod
+    def create_network_keystone_v3(cls, network_name=None, project_id=None,
+                                   tenant_id=None, client=None):
+        """Wrapper utility that creates a test network with project_id."""
+        client = client or cls.client
+        network_name = network_name or data_utils.rand_name(
+            'test-network-with-project_id')
+        project_id = cls.client.tenant_id
+        body = client.create_network_keystone_v3(network_name, project_id,
+            tenant_id)
+        network = body['network']
+        if client is cls.client:
+            cls.networks.append(network)
+        else:
+            cls.admin_networks.append(network)
         return network
 
     @classmethod
diff --git a/neutron/tests/tempest/api/test_networks.py b/neutron/tests/tempest/api/test_networks.py
index 439d7a9..279964a 100644
--- a/neutron/tests/tempest/api/test_networks.py
+++ b/neutron/tests/tempest/api/test_networks.py
@@ -106,8 +106,7 @@
         project_id = self.client.tenant_id
 
         name = 'created-with-project_id'
-        body = self.client.create_network_keystone_v3(name, project_id)
-        new_net = body['network']
+        new_net = self.create_network_keystone_v3(name, project_id)
         self.assertEqual(name, new_net['name'])
         self.assertEqual(project_id, new_net['project_id'])
         self.assertEqual(project_id, new_net['tenant_id'])
diff --git a/neutron/tests/tempest/api/test_trunk.py b/neutron/tests/tempest/api/test_trunk.py
index d5aa7db..3dc8f0d 100644
--- a/neutron/tests/tempest/api/test_trunk.py
+++ b/neutron/tests/tempest/api/test_trunk.py
@@ -12,11 +12,13 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
+from tempest.lib.common.utils import data_utils
 from tempest.lib.common.utils import test_utils
 from tempest.lib import exceptions as lib_exc
 from tempest import test
 
 from neutron.tests.tempest.api import base
+from neutron.tests.tempest import config
 
 
 def trunks_cleanup(client, trunks):
@@ -206,6 +208,85 @@
         self.assertEqual(1, len(observed_subports))
 
 
+class TrunkTestMtusJSONBase(TrunkTestJSONBase):
+
+    required_extensions = ['provider', 'trunk']
+
+    @classmethod
+    def skip_checks(cls):
+        super(TrunkTestMtusJSONBase, cls).skip_checks()
+        for ext in cls.required_extensions:
+            if not test.is_extension_enabled(ext, 'network'):
+                msg = "%s extension not enabled." % ext
+                raise cls.skipException(msg)
+
+        if any(t
+               not in config.CONF.neutron_plugin_options.available_type_drivers
+               for t in ['gre', 'vxlan']):
+            msg = "Either vxlan or gre type driver not enabled."
+            raise cls.skipException(msg)
+
+    def setUp(self):
+        super(TrunkTestMtusJSONBase, self).setUp()
+
+        # VXLAN autocomputed MTU (1450) is smaller than that of GRE (1458)
+        vxlan_kwargs = {'network_name': data_utils.rand_name('vxlan-net-'),
+                        'provider:network_type': 'vxlan'}
+        self.smaller_mtu_net = self.create_shared_network(**vxlan_kwargs)
+
+        gre_kwargs = {'network_name': data_utils.rand_name('gre-net-'),
+                      'provider:network_type': 'gre'}
+        self.larger_mtu_net = self.create_shared_network(**gre_kwargs)
+
+        self.smaller_mtu_port = self.create_port(self.smaller_mtu_net)
+        self.smaller_mtu_port_2 = self.create_port(self.smaller_mtu_net)
+        self.larger_mtu_port = self.create_port(self.larger_mtu_net)
+
+
+class TrunkTestMtusJSON(TrunkTestMtusJSONBase):
+
+    @test.idempotent_id('0f05d98e-41f5-4629-ac29-9aee269c9602')
+    def test_create_trunk_with_mtu_greater_than_subport(self):
+        subports = [{'port_id': self.smaller_mtu_port['id'],
+                     'segmentation_type': 'vlan',
+                     'segmentation_id': 2}]
+
+        trunk = self.client.create_trunk(self.larger_mtu_port['id'], subports)
+        self.trunks.append(trunk['trunk'])
+
+    @test.idempotent_id('2004c5c6-e557-4c43-8100-c820ad4953e8')
+    def test_add_subport_with_mtu_smaller_than_trunk(self):
+        subports = [{'port_id': self.smaller_mtu_port['id'],
+                     'segmentation_type': 'vlan',
+                     'segmentation_id': 2}]
+
+        trunk = self.client.create_trunk(self.larger_mtu_port['id'], None)
+        self.trunks.append(trunk['trunk'])
+
+        self.client.add_subports(trunk['trunk']['id'], subports)
+
+    @test.idempotent_id('22725101-f4bc-4e00-84ec-4e02cd7e0500')
+    def test_create_trunk_with_mtu_equal_to_subport(self):
+        subports = [{'port_id': self.smaller_mtu_port['id'],
+                     'segmentation_type': 'vlan',
+                     'segmentation_id': 2}]
+
+        trunk = self.client.create_trunk(self.smaller_mtu_port_2['id'],
+                                         subports)
+        self.trunks.append(trunk['trunk'])
+
+    @test.idempotent_id('175b05ae-66ad-44c7-857a-a12d16f1058f')
+    def test_add_subport_with_mtu_equal_to_trunk(self):
+        subports = [{'port_id': self.smaller_mtu_port['id'],
+                     'segmentation_type': 'vlan',
+                     'segmentation_id': 2}]
+
+        trunk = self.client.create_trunk(self.smaller_mtu_port_2['id'], None)
+        self.trunks.append(trunk['trunk'])
+
+        self.client.add_subports(trunk['trunk']['id'], subports)
+
+
 class TrunksSearchCriteriaTest(base.BaseSearchCriteriaTest):
 
     resource = 'trunk'
diff --git a/neutron/tests/tempest/api/test_trunk_negative.py b/neutron/tests/tempest/api/test_trunk_negative.py
index 654b497..a5ed4d5 100644
--- a/neutron/tests/tempest/api/test_trunk_negative.py
+++ b/neutron/tests/tempest/api/test_trunk_negative.py
@@ -12,6 +12,8 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
+import testtools
+
 from oslo_utils import uuidutils
 from tempest.lib import exceptions as lib_exc
 from tempest import test
@@ -235,3 +237,35 @@
         self._create_trunk_with_network_and_parent(subports)
         self.assertRaises(lib_exc.Conflict, self.client.delete_port,
                           port['id'])
+
+
+class TrunkTestMtusJSON(test_trunk.TrunkTestMtusJSONBase):
+
+    required_extensions = (
+        ['net-mtu'] + test_trunk.TrunkTestMtusJSONBase.required_extensions)
+
+    @test.attr(type='negative')
+    @test.idempotent_id('228380ef-1b7a-495e-b759-5b1f08e3e858')
+    def test_create_trunk_with_mtu_smaller_than_subport(self):
+        subports = [{'port_id': self.larger_mtu_port['id'],
+                     'segmentation_type': 'vlan',
+                     'segmentation_id': 2}]
+
+        with testtools.ExpectedException(lib_exc.Conflict):
+            trunk = self.client.create_trunk(self.smaller_mtu_port['id'],
+                                             subports)
+            self.trunks.append(trunk['trunk'])
+
+    @test.attr(type='negative')
+    @test.idempotent_id('3b32bf77-8002-403e-ad01-6f4cf018daa5')
+    def test_add_subport_with_mtu_greater_than_trunk(self):
+        subports = [{'port_id': self.larger_mtu_port['id'],
+                     'segmentation_type': 'vlan',
+                     'segmentation_id': 2}]
+
+        trunk = self.client.create_trunk(self.smaller_mtu_port['id'], None)
+        self.trunks.append(trunk['trunk'])
+
+        self.assertRaises(lib_exc.Conflict,
+                          self.client.add_subports,
+                          trunk['trunk']['id'], subports)
diff --git a/neutron/tests/tempest/config.py b/neutron/tests/tempest/config.py
index f50fa39..479d909 100644
--- a/neutron/tests/tempest/config.py
+++ b/neutron/tests/tempest/config.py
@@ -22,7 +22,12 @@
     cfg.BoolOpt('specify_floating_ip_address_available',
                 default=True,
                 help='Allow passing an IP Address of the floating ip when '
-                     'creating the floating ip')]
+                     'creating the floating ip'),
+    cfg.ListOpt('available_type_drivers',
+                default=[],
+                help='List of network types available to neutron, '
+                     'e.g. vxlan,vlan,gre.'),
+]
 
 # TODO(amuller): Redo configuration options registration as part of the planned
 # transition to the Tempest plugin architecture
diff --git a/neutron/tests/tempest/scenario/test_trunk.py b/neutron/tests/tempest/scenario/test_trunk.py
new file mode 100644
index 0000000..30d6022
--- /dev/null
+++ b/neutron/tests/tempest/scenario/test_trunk.py
@@ -0,0 +1,160 @@
+# All Rights Reserved.
+#
+#    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.
+
+from oslo_log import log as logging
+from tempest.common import waiters
+from tempest import test
+
+from neutron.common import utils
+from neutron.tests.tempest import config
+from neutron.tests.tempest.scenario import base
+from neutron.tests.tempest.scenario import constants
+
+CONF = config.CONF
+LOG = logging.getLogger(__name__)
+
+
+class TrunkTest(base.BaseTempestTestCase):
+    credentials = ['primary']
+    force_tenant_isolation = False
+
+    @classmethod
+    @test.requires_ext(extension="trunk", service="network")
+    def resource_setup(cls):
+        super(TrunkTest, cls).resource_setup()
+        # setup basic topology for servers we can log into
+        cls.network = cls.create_network()
+        cls.subnet = cls.create_subnet(cls.network)
+        cls.create_router_and_interface(cls.subnet['id'])
+        cls.keypair = cls.create_keypair()
+        cls.create_loginable_secgroup_rule()
+
+    def _create_server_with_trunk_port(self):
+        port = self.create_port(self.network)
+        trunk = self.client.create_trunk(port['id'], subports=[])['trunk']
+        fip = self.create_and_associate_floatingip(port['id'])
+        server = self.create_server(
+            flavor_ref=CONF.compute.flavor_ref,
+            image_ref=CONF.compute.image_ref,
+            key_name=self.keypair['name'],
+            networks=[{'port': port['id']}])['server']
+        self.addCleanup(self._detach_and_delete_trunk, server, trunk)
+        return {'port': port, 'trunk': trunk, 'fip': fip,
+                'server': server}
+
+    def _detach_and_delete_trunk(self, server, trunk):
+        # we have to detach the interface from the server before
+        # the trunk can be deleted.
+        self.manager.compute.InterfacesClient().delete_interface(
+            server['id'], trunk['port_id'])
+
+        def is_port_detached():
+            p = self.client.show_port(trunk['port_id'])['port']
+            return p['device_id'] == ''
+        utils.wait_until_true(is_port_detached)
+        self.client.delete_trunk(trunk['id'])
+
+    def _is_port_down(self, port_id):
+        p = self.client.show_port(port_id)['port']
+        return p['status'] == 'DOWN'
+
+    def _is_port_active(self, port_id):
+        p = self.client.show_port(port_id)['port']
+        return p['status'] == 'ACTIVE'
+
+    def _is_trunk_active(self, trunk_id):
+        t = self.client.show_trunk(trunk_id)['trunk']
+        return t['status'] == 'ACTIVE'
+
+    @test.idempotent_id('bb13fe28-f152-4000-8131-37890a40c79e')
+    def test_trunk_subport_lifecycle(self):
+        """Test trunk creation and subport transition to ACTIVE status.
+
+        This is a basic test for the trunk extension to ensure that we
+        can create a trunk, attach it to a server, add/remove subports,
+        while ensuring the status transitions as appropriate.
+
+        This test does not assert any dataplane behavior for the subports.
+        It's just a high-level check to ensure the agents claim to have
+        wired the port correctly and that the trunk port itself maintains
+        connectivity.
+        """
+        server1 = self._create_server_with_trunk_port()
+        server2 = self._create_server_with_trunk_port()
+        for server in (server1, server2):
+            waiters.wait_for_server_status(self.manager.servers_client,
+                                           server['server']['id'],
+                                           constants.SERVER_STATUS_ACTIVE)
+            self.check_connectivity(server['fip']['floating_ip_address'],
+                                    CONF.validation.image_ssh_user,
+                                    self.keypair['private_key'])
+        trunk1_id, trunk2_id = server1['trunk']['id'], server2['trunk']['id']
+        # trunks should transition to ACTIVE without any subports
+        utils.wait_until_true(
+            lambda: self._is_trunk_active(trunk1_id),
+            exception=RuntimeError("Timed out waiting for trunk %s to "
+                                   "transition to ACTIVE." % trunk1_id))
+        utils.wait_until_true(
+            lambda: self._is_trunk_active(trunk2_id),
+            exception=RuntimeError("Timed out waiting for trunk %s to "
+                                   "transition to ACTIVE." % trunk2_id))
+        # create a few more networks and ports for subports
+        subports = [{'port_id': self.create_port(self.create_network())['id'],
+                     'segmentation_type': 'vlan', 'segmentation_id': seg_id}
+                    for seg_id in range(3, 7)]
+        # add all subports to server1
+        self.client.add_subports(trunk1_id, subports)
+        # ensure trunk transitions to ACTIVE
+        utils.wait_until_true(
+            lambda: self._is_trunk_active(trunk1_id),
+            exception=RuntimeError("Timed out waiting for trunk %s to "
+                                   "transition to ACTIVE." % trunk1_id))
+        # ensure all underlying subports transitioned to ACTIVE
+        for s in subports:
+            utils.wait_until_true(lambda: self._is_port_active(s['port_id']))
+        # ensure main dataplane wasn't interrupted
+        self.check_connectivity(server1['fip']['floating_ip_address'],
+                                CONF.validation.image_ssh_user,
+                                self.keypair['private_key'])
+        # move subports over to other server
+        self.client.remove_subports(trunk1_id, subports)
+        # ensure all subports go down
+        for s in subports:
+            utils.wait_until_true(
+                lambda: self._is_port_down(s['port_id']),
+                exception=RuntimeError("Timed out waiting for subport %s to "
+                                       "transition to DOWN." % s['port_id']))
+        self.client.add_subports(trunk2_id, subports)
+        # wait for both trunks to go back to ACTIVE
+        utils.wait_until_true(
+            lambda: self._is_trunk_active(trunk1_id),
+            exception=RuntimeError("Timed out waiting for trunk %s to "
+                                   "transition to ACTIVE." % trunk1_id))
+        utils.wait_until_true(
+            lambda: self._is_trunk_active(trunk2_id),
+            exception=RuntimeError("Timed out waiting for trunk %s to "
+                                   "transition to ACTIVE." % trunk2_id))
+        # ensure subports come up on other trunk
+        for s in subports:
+            utils.wait_until_true(
+                lambda: self._is_port_active(s['port_id']),
+                exception=RuntimeError("Timed out waiting for subport %s to "
+                                       "transition to ACTIVE." % s['port_id']))
+        # final connectivity check
+        self.check_connectivity(server1['fip']['floating_ip_address'],
+                                CONF.validation.image_ssh_user,
+                                self.keypair['private_key'])
+        self.check_connectivity(server2['fip']['floating_ip_address'],
+                                CONF.validation.image_ssh_user,
+                                self.keypair['private_key'])
diff --git a/neutron/tests/tempest/services/network/json/network_client.py b/neutron/tests/tempest/services/network/json/network_client.py
index 4828059..3b1a9a8 100644
--- a/neutron/tests/tempest/services/network/json/network_client.py
+++ b/neutron/tests/tempest/services/network/json/network_client.py
@@ -872,7 +872,7 @@
         body = jsonutils.loads(body)
         return service_client.ResponseBody(resp, body)
 
-    def create_network_keystone_v3(self, name, project_id):
+    def create_network_keystone_v3(self, name, project_id, tenant_id=None):
         uri = '%s/networks' % self.uri_prefix
         post_data = {
             'network': {
@@ -880,6 +880,8 @@
                 'project_id': project_id
             }
         }
+        if tenant_id is not None:
+            post_data['network']['tenant_id'] = tenant_id
         resp, body = self.post(uri, self.serialize(post_data))
         body = self.deserialize_single(body)
         self.expected_success(201, resp.status)