Merge "Add negative tests for routers"
diff --git a/tempest/api/network/base_routers.py b/tempest/api/network/base_routers.py
new file mode 100644
index 0000000..9baef04
--- /dev/null
+++ b/tempest/api/network/base_routers.py
@@ -0,0 +1,50 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2013 OpenStack Foundation
+# 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 tempest.api.network import base
+
+
+class BaseRouterTest(base.BaseAdminNetworkTest):
+    # NOTE(salv-orlando): This class inherits from BaseAdminNetworkTest
+    # as some router operations, such as enabling or disabling SNAT
+    # require admin credentials by default
+
+    @classmethod
+    def setUpClass(cls):
+        super(BaseRouterTest, cls).setUpClass()
+
+    def _delete_router(self, router_id):
+        resp, _ = self.client.delete_router(router_id)
+        self.assertEqual(204, resp.status)
+        # Asserting that the router is not found in the list
+        # after deletion
+        resp, list_body = self.client.list_routers()
+        self.assertEqual('200', resp['status'])
+        routers_list = list()
+        for router in list_body['routers']:
+            routers_list.append(router['id'])
+        self.assertNotIn(router_id, routers_list)
+
+    def _remove_router_interface_with_subnet_id(self, router_id, subnet_id):
+        resp, _ = self.client.remove_router_interface_with_subnet_id(
+            router_id, subnet_id)
+        self.assertEqual('200', resp['status'])
+
+    def _remove_router_interface_with_port_id(self, router_id, port_id):
+        resp, _ = self.client.remove_router_interface_with_port_id(
+            router_id, port_id)
+        self.assertEqual('200', resp['status'])
diff --git a/tempest/api/network/test_routers.py b/tempest/api/network/test_routers.py
index 21934f2..b6022e8 100644
--- a/tempest/api/network/test_routers.py
+++ b/tempest/api/network/test_routers.py
@@ -17,43 +17,18 @@
 
 import netaddr
 
-from tempest.api.network import base
+from tempest.api.network import base_routers as base
 from tempest.common.utils import data_utils
 from tempest import test
 
 
-class RoutersTest(base.BaseAdminNetworkTest):
-    # NOTE(salv-orlando): This class inherits from BaseAdminNetworkTest
-    # as some router operations, such as enabling or disabling SNAT
-    # require admin credentials by default
+class RoutersTest(base.BaseRouterTest):
     _interface = 'json'
 
     @classmethod
     def setUpClass(cls):
         super(RoutersTest, cls).setUpClass()
 
-    def _delete_router(self, router_id):
-        resp, _ = self.client.delete_router(router_id)
-        self.assertEqual(204, resp.status)
-        # Asserting that the router is not found in the list
-        # after deletion
-        resp, list_body = self.client.list_routers()
-        self.assertEqual('200', resp['status'])
-        routers_list = list()
-        for router in list_body['routers']:
-            routers_list.append(router['id'])
-        self.assertNotIn(router_id, routers_list)
-
-    def _remove_router_interface_with_subnet_id(self, router_id, subnet_id):
-        resp, _ = self.client.remove_router_interface_with_subnet_id(
-            router_id, subnet_id)
-        self.assertEqual('200', resp['status'])
-
-    def _remove_router_interface_with_port_id(self, router_id, port_id):
-        resp, _ = self.client.remove_router_interface_with_port_id(
-            router_id, port_id)
-        self.assertEqual('200', resp['status'])
-
     @test.attr(type='smoke')
     def test_create_show_list_update_delete_router(self):
         # Create a router
diff --git a/tempest/api/network/test_routers_negative.py b/tempest/api/network/test_routers_negative.py
new file mode 100644
index 0000000..520a612
--- /dev/null
+++ b/tempest/api/network/test_routers_negative.py
@@ -0,0 +1,57 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2013 OpenStack Foundation
+# 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 tempest.api.network import base_routers as base
+from tempest.common.utils import data_utils
+from tempest import exceptions
+from tempest.test import attr
+
+
+class RoutersNegativeTest(base.BaseRouterTest):
+    _interface = 'json'
+
+    @classmethod
+    def setUpClass(cls):
+        super(RoutersNegativeTest, cls).setUpClass()
+        cls.router = cls.create_router(data_utils.rand_name('router-'))
+        cls.network = cls.create_network()
+        cls.subnet = cls.create_subnet(cls.network)
+
+    @attr(type=['negative', 'smoke'])
+    def test_router_add_gateway_invalid_network_returns_404(self):
+        self.assertRaises(exceptions.NotFound,
+                          self.client.update_router,
+                          self.router['id'],
+                          external_gateway_info={
+                              'network_id': self.router['id']})
+
+    @attr(type=['negative', 'smoke'])
+    def test_router_add_gateway_net_not_external_returns_400(self):
+        self.create_subnet(self.network)
+        self.assertRaises(exceptions.BadRequest,
+                          self.client.update_router,
+                          self.router['id'],
+                          external_gateway_info={
+                              'network_id': self.network['id']})
+
+    @attr(type=['negative', 'smoke'])
+    def test_router_remove_interface_in_use_returns_409(self):
+        self.client.add_router_interface_with_subnet_id(
+            self.router['id'], self.subnet['id'])
+        self.assertRaises(exceptions.Conflict,
+                          self.client.delete_router,
+                          self.router['id'])