Merge "Add negative API tests that try to remove the resources in use."
diff --git a/neutron/tests/tempest/api/test_networks_negative.py b/neutron/tests/tempest/api/test_networks_negative.py
new file mode 100644
index 0000000..87da1b8
--- /dev/null
+++ b/neutron/tests/tempest/api/test_networks_negative.py
@@ -0,0 +1,36 @@
+#    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 tempest.lib import exceptions as lib_exc
+from tempest import test
+import testtools
+
+from neutron.tests.tempest.api import base
+
+
+class NetworksNegativeTest(base.BaseNetworkTest):
+
+    @classmethod
+    def resource_setup(cls):
+        super(NetworksNegativeTest, cls).resource_setup()
+        cls.network = cls.create_network()
+        cls.subnet = cls.create_subnet(cls.network)
+
+    @test.attr(type='negative')
+    @test.idempotent_id('9f80f25b-5d1b-4f26-9f6b-774b9b270819')
+    def test_delete_network_in_use(self):
+        port = self.client.create_port(network_id=self.network['id'])
+        self.addCleanup(self.client.delete_port, port['port']['id'])
+        with testtools.ExpectedException(lib_exc.Conflict):
+            self.client.delete_subnet(self.subnet['id'])
+        with testtools.ExpectedException(lib_exc.Conflict):
+            self.client.delete_network(self.network['id'])
diff --git a/neutron/tests/tempest/api/test_routers_negative.py b/neutron/tests/tempest/api/test_routers_negative.py
index 2ccb34a..6a028db 100644
--- a/neutron/tests/tempest/api/test_routers_negative.py
+++ b/neutron/tests/tempest/api/test_routers_negative.py
@@ -21,20 +21,41 @@
 from neutron.tests.tempest.api import base_routers as base
 
 
-class DvrRoutersNegativeTest(base.BaseRouterTest):
+class RoutersNegativeTestBase(base.BaseRouterTest):
+
+    @classmethod
+    def resource_setup(cls):
+        super(RoutersNegativeTestBase, cls).resource_setup()
+        cls.router = cls.create_router(data_utils.rand_name('router'))
+        cls.network = cls.create_network()
+        cls.subnet = cls.create_subnet(cls.network)
+
+
+class RoutersNegativeTest(RoutersNegativeTestBase):
+
+    @classmethod
+    @test.requires_ext(extension="router", service="network")
+    def skip_checks(cls):
+        super(RoutersNegativeTest, cls).skip_checks()
+
+    @test.attr(type='negative')
+    @test.idempotent_id('e3e751af-15a2-49cc-b214-a7154579e94f')
+    def test_delete_router_in_use(self):
+        # This port is deleted after a test by remove_router_interface.
+        port = self.client.create_port(network_id=self.network['id'])
+        self.client.add_router_interface_with_port_id(
+            self.router['id'], port['port']['id'])
+        with testtools.ExpectedException(lib_exc.Conflict):
+            self.client.delete_router(self.router['id'])
+
+
+class DvrRoutersNegativeTest(RoutersNegativeTestBase):
 
     @classmethod
     @test.requires_ext(extension="dvr", service="network")
     def skip_checks(cls):
         super(DvrRoutersNegativeTest, cls).skip_checks()
 
-    @classmethod
-    def resource_setup(cls):
-        super(DvrRoutersNegativeTest, cls).resource_setup()
-        cls.router = cls.create_router(data_utils.rand_name('router'))
-        cls.network = cls.create_network()
-        cls.subnet = cls.create_subnet(cls.network)
-
     @test.attr(type='negative')
     @test.idempotent_id('4990b055-8fc7-48ab-bba7-aa28beaad0b9')
     def test_router_create_tenant_distributed_returns_forbidden(self):