Merge "Wait for the router port activation before deletion"
diff --git a/tempest/api/network/base.py b/tempest/api/network/base.py
index 99742cc..5bbd50e 100644
--- a/tempest/api/network/base.py
+++ b/tempest/api/network/base.py
@@ -15,6 +15,8 @@
 
 import netaddr
 
+from tempest.common import utils as common_utils
+from tempest.common import waiters
 from tempest import config
 from tempest import exceptions
 from tempest.lib.common.utils import data_utils
@@ -226,6 +228,18 @@
                 subnet_id=i['fixed_ips'][0]['subnet_id'])
         cls.routers_client.delete_router(router['id'])
 
+    def remove_router_interface(self, router_id, port_id, subnet_id=None):
+        # NOTE: with DVR and without a VM port, it is not possible to know
+        # what agent will host the router interface thus won't be bound.
+        if not common_utils.is_extension_enabled('dvr', 'network'):
+            waiters.wait_for_port_status(client=self.ports_client,
+                                         port_id=port_id, status='ACTIVE')
+        if subnet_id:
+            kwargs = {'subnet_id': subnet_id}
+        else:
+            kwargs = {'port_id': port_id}
+        self.routers_client.remove_router_interface(router_id, **kwargs)
+
 
 class BaseAdminNetworkTest(BaseNetworkTest):
 
diff --git a/tempest/api/network/test_routers.py b/tempest/api/network/test_routers.py
index aaedba2..fedf2f4 100644
--- a/tempest/api/network/test_routers.py
+++ b/tempest/api/network/test_routers.py
@@ -18,7 +18,6 @@
 
 from tempest.api.network import base
 from tempest.common import utils
-from tempest.common import waiters
 from tempest import config
 from tempest.lib.common.utils import data_utils
 from tempest.lib.common.utils import test_utils
@@ -33,22 +32,11 @@
     def _add_router_interface_with_subnet_id(self, router_id, subnet_id):
         interface = self.routers_client.add_router_interface(
             router_id, subnet_id=subnet_id)
-        self.addCleanup(self._remove_router_interface_with_subnet_id,
-                        router_id, subnet_id, interface['port_id'])
+        self.addCleanup(self.remove_router_interface,
+                        router_id, interface['port_id'], subnet_id=subnet_id)
         self.assertEqual(subnet_id, interface['subnet_id'])
         return interface
 
-    def _remove_router_interface_with_subnet_id(self, router_id, subnet_id,
-                                                port_id):
-        # NOTE: with DVR and without a VM port, it is not possible to know
-        # what agent will host the router interface thus won't be bound.
-        if not utils.is_extension_enabled('dvr', 'network'):
-            waiters.wait_for_port_status(client=self.ports_client,
-                                         port_id=port_id, status='ACTIVE')
-        body = self.routers_client.remove_router_interface(router_id,
-                                                           subnet_id=subnet_id)
-        self.assertEqual(subnet_id, body['subnet_id'])
-
     @classmethod
     def skip_checks(cls):
         super(RoutersTest, cls).skip_checks()
@@ -113,8 +101,9 @@
         # Add router interface with subnet id
         interface = self.routers_client.add_router_interface(
             router['id'], subnet_id=subnet['id'])
-        self.addCleanup(self._remove_router_interface_with_subnet_id,
-                        router['id'], subnet['id'], interface['port_id'])
+        self.addCleanup(self.remove_router_interface,
+                        router['id'], interface['port_id'],
+                        subnet_id=subnet['id'])
         self.assertIn('subnet_id', interface.keys())
         self.assertIn('port_id', interface.keys())
         # Verify router id is equal to device id in port details
@@ -192,8 +181,9 @@
             # Add router interface with subnet id
             interface = self.create_router_interface(router['id'],
                                                      subnet['id'])
-            self.addCleanup(self._remove_router_interface_with_subnet_id,
-                            router['id'], subnet['id'], interface['port_id'])
+            self.addCleanup(self.remove_router_interface,
+                            router['id'], interface['port_id'],
+                            subnet_id=subnet['id'])
             cidr = netaddr.IPNetwork(subnet['cidr'])
             next_hop = str(cidr[2])
             destination = str(subnet['cidr'])
diff --git a/tempest/api/network/test_routers_negative.py b/tempest/api/network/test_routers_negative.py
index 50ba977..299e0e9 100644
--- a/tempest/api/network/test_routers_negative.py
+++ b/tempest/api/network/test_routers_negative.py
@@ -77,8 +77,9 @@
         subnet02 = self.create_subnet(network02)
         interface = self.routers_client.add_router_interface(
             self.router['id'], subnet_id=subnet01['id'])
-        self.addCleanup(self.routers_client.remove_router_interface,
-                        self.router['id'], subnet_id=subnet01['id'])
+        self.addCleanup(self.remove_router_interface,
+                        self.router['id'], interface['port_id'],
+                        subnet_id=subnet01['id'])
         self.assertEqual(subnet01['id'], interface['subnet_id'])
         self.assertRaises(lib_exc.BadRequest,
                           self.routers_client.add_router_interface,
@@ -89,10 +90,11 @@
     @decorators.idempotent_id('04df80f9-224d-47f5-837a-bf23e33d1c20')
     def test_router_remove_interface_in_use_returns_409(self):
         """Test removing in-use interface from router"""
-        self.routers_client.add_router_interface(self.router['id'],
-                                                 subnet_id=self.subnet['id'])
-        self.addCleanup(self.routers_client.remove_router_interface,
-                        self.router['id'], subnet_id=self.subnet['id'])
+        interface = self.routers_client.add_router_interface(
+            self.router['id'], subnet_id=self.subnet['id'])
+        self.addCleanup(self.remove_router_interface,
+                        self.router['id'], interface['port_id'],
+                        subnet_id=self.subnet['id'])
         self.assertRaises(lib_exc.Conflict,
                           self.routers_client.delete_router,
                           self.router['id'])