Merge "Move negative tests for v3 test_server_metadata"
diff --git a/tempest/api/compute/v3/servers/test_server_metadata.py b/tempest/api/compute/v3/servers/test_server_metadata.py
index 1758b0b..00549eb 100644
--- a/tempest/api/compute/v3/servers/test_server_metadata.py
+++ b/tempest/api/compute/v3/servers/test_server_metadata.py
@@ -14,8 +14,7 @@
 #    under the License.
 
 from tempest.api.compute import base
-from tempest import exceptions
-from tempest.test import attr
+from tempest import test
 
 
 class ServerMetadataV3TestJSON(base.BaseV3ComputeTest):
@@ -40,7 +39,7 @@
         resp, _ = self.client.set_server_metadata(self.server_id, meta)
         self.assertEqual(resp.status, 200)
 
-    @attr(type='gate')
+    @test.attr(type='gate')
     def test_list_server_metadata(self):
         # All metadata key/value pairs for a server should be returned
         resp, resp_metadata = self.client.list_server_metadata(self.server_id)
@@ -50,7 +49,7 @@
         expected = {'key1': 'value1', 'key2': 'value2'}
         self.assertEqual(expected, resp_metadata)
 
-    @attr(type='gate')
+    @test.attr(type='gate')
     def test_set_server_metadata(self):
         # The server's metadata should be replaced with the provided values
         # Create a new set of metadata for the server
@@ -64,22 +63,7 @@
         resp, resp_metadata = self.client.list_server_metadata(self.server_id)
         self.assertEqual(resp_metadata, req_metadata)
 
-    @attr(type='gate')
-    def test_server_create_metadata_key_too_long(self):
-        # Attempt to start a server with a meta-data key that is > 255
-        # characters
-
-        # Try a few values
-        for sz in [256, 257, 511, 1023]:
-            key = "k" * sz
-            meta = {key: 'data1'}
-            self.assertRaises(exceptions.OverLimit,
-                              self.create_test_server,
-                              meta=meta)
-
-        # no teardown - all creates should fail
-
-    @attr(type='gate')
+    @test.attr(type='gate')
     def test_update_server_metadata(self):
         # The server's metadata values should be updated to the
         # provided values
@@ -93,7 +77,7 @@
         expected = {'key1': 'alt1', 'key2': 'value2', 'key3': 'value3'}
         self.assertEqual(expected, resp_metadata)
 
-    @attr(type='gate')
+    @test.attr(type='gate')
     def test_update_metadata_empty_body(self):
         # The original metadata should not be lost if empty metadata body is
         # passed
@@ -103,14 +87,14 @@
         expected = {'key1': 'value1', 'key2': 'value2'}
         self.assertEqual(expected, resp_metadata)
 
-    @attr(type='gate')
+    @test.attr(type='gate')
     def test_get_server_metadata_item(self):
         # The value for a specific metadata key should be returned
         resp, meta = self.client.get_server_metadata_item(self.server_id,
                                                           'key2')
         self.assertEqual('value2', meta['key2'])
 
-    @attr(type='gate')
+    @test.attr(type='gate')
     def test_set_server_metadata_item(self):
         # The item's value should be updated to the provided value
         # Update the metadata value
@@ -124,7 +108,7 @@
         expected = {'key1': 'value1', 'key2': 'value2', 'nova': 'alt'}
         self.assertEqual(expected, resp_metadata)
 
-    @attr(type='gate')
+    @test.attr(type='gate')
     def test_delete_server_metadata_item(self):
         # The metadata value/key pair should be deleted from the server
         resp, meta = self.client.delete_server_metadata_item(self.server_id,
@@ -135,77 +119,3 @@
         resp, resp_metadata = self.client.list_server_metadata(self.server_id)
         expected = {'key2': 'value2'}
         self.assertEqual(expected, resp_metadata)
-
-    @attr(type=['negative', 'gate'])
-    def test_server_metadata_negative(self):
-        # Blank key should trigger an error.
-        meta = {'': 'data1'}
-        self.assertRaises(exceptions.BadRequest,
-                          self.create_test_server,
-                          meta=meta)
-
-        # GET on a non-existent server should not succeed
-        self.assertRaises(exceptions.NotFound,
-                          self.client.get_server_metadata_item, 999, 'test2')
-
-        # List metadata on a non-existent server should not succeed
-        self.assertRaises(exceptions.NotFound,
-                          self.client.list_server_metadata, 999)
-
-        # Raise BadRequest if key in uri does not match
-        # the key passed in body.
-        meta = {'testkey': 'testvalue'}
-        self.assertRaises(exceptions.BadRequest,
-                          self.client.set_server_metadata_item,
-                          self.server_id, 'key', meta)
-
-        # Set metadata on a non-existent server should not succeed
-        meta = {'meta1': 'data1'}
-        self.assertRaises(exceptions.NotFound,
-                          self.client.set_server_metadata, 999, meta)
-
-        # An update should not happen for a non-existent image
-        meta = {'key1': 'value1', 'key2': 'value2'}
-        self.assertRaises(exceptions.NotFound,
-                          self.client.update_server_metadata, 999, meta)
-
-        # Blank key should trigger an error
-        meta = {'': 'data1'}
-        self.assertRaises(exceptions.BadRequest,
-                          self.client.update_server_metadata,
-                          self.server_id, meta=meta)
-
-        # Should not be able to delete metadata item from a non-existent server
-        self.assertRaises(exceptions.NotFound,
-                          self.client.delete_server_metadata_item, 999, 'd')
-
-        # Raise a 413 OverLimit exception while exceeding metadata items limit
-        # for tenant.
-        _, quota_set = self.quotas.get_quota_set(self.tenant_id)
-        quota_metadata = quota_set['metadata_items']
-        req_metadata = {}
-        for num in range(1, quota_metadata + 2):
-            req_metadata['key' + str(num)] = 'val' + str(num)
-        self.assertRaises(exceptions.OverLimit,
-                          self.client.set_server_metadata,
-                          self.server_id, req_metadata)
-
-        # Raise a 413 OverLimit exception while exceeding metadata items limit
-        # for tenant (update).
-        self.assertRaises(exceptions.OverLimit,
-                          self.client.update_server_metadata,
-                          self.server_id, req_metadata)
-
-        # Raise a bad request error for blank key.
-        # set_server_metadata will replace all metadata with new value
-        meta = {'': 'data1'}
-        self.assertRaises(exceptions.BadRequest,
-                          self.client.set_server_metadata,
-                          self.server_id, meta=meta)
-
-        # Raise a bad request error for a missing metadata field
-        # set_server_metadata will replace all metadata with new value
-        meta = {'meta1': 'data1'}
-        self.assertRaises(exceptions.BadRequest,
-                          self.client.set_server_metadata,
-                          self.server_id, meta=meta, no_metadata_field=True)
diff --git a/tempest/api/compute/v3/servers/test_server_metadata_negative.py b/tempest/api/compute/v3/servers/test_server_metadata_negative.py
new file mode 100644
index 0000000..4062696
--- /dev/null
+++ b/tempest/api/compute/v3/servers/test_server_metadata_negative.py
@@ -0,0 +1,158 @@
+# Copyright 2014 NEC Corporation.  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.compute import base
+from tempest.common.utils import data_utils
+from tempest import exceptions
+from tempest import test
+
+
+class ServerMetadataV3NegativeTestJSON(base.BaseV3ComputeTest):
+    _interface = 'json'
+
+    @classmethod
+    def setUpClass(cls):
+        super(ServerMetadataV3NegativeTestJSON, cls).setUpClass()
+        cls.client = cls.servers_client
+        cls.quotas = cls.quotas_client
+        cls.admin_client = cls._get_identity_admin_client()
+        resp, tenants = cls.admin_client.list_tenants()
+        cls.tenant_id = [tnt['id'] for tnt in tenants if tnt['name'] ==
+                         cls.client.tenant_name][0]
+        resp, server = cls.create_test_server(meta={}, wait_until='ACTIVE')
+
+        cls.server_id = server['id']
+
+    @test.attr(type=['gate', 'negative'])
+    def test_server_create_metadata_key_too_long(self):
+        # Attempt to start a server with a meta-data key that is > 255
+        # characters
+
+        # Tryset_server_metadata_item a few values
+        for sz in [256, 257, 511, 1023]:
+            key = "k" * sz
+            meta = {key: 'data1'}
+            self.assertRaises(exceptions.OverLimit,
+                              self.create_test_server,
+                              meta=meta)
+
+        # no teardown - all creates should fail
+
+    @test.attr(type=['negative', 'gate'])
+    def test_create_server_metadata_blank_key(self):
+        # Blank key should trigger an error.
+        meta = {'': 'data1'}
+        self.assertRaises(exceptions.BadRequest,
+                          self.create_test_server,
+                          meta=meta)
+
+    @test.attr(type=['negative', 'gate'])
+    def test_server_metadata_non_existent_server(self):
+        # GET on a non-existent server should not succeed
+        non_existent_server_id = data_utils.rand_uuid()
+        self.assertRaises(exceptions.NotFound,
+                          self.client.get_server_metadata_item,
+                          non_existent_server_id,
+                          'test2')
+
+    @test.attr(type=['negative', 'gate'])
+    def test_list_server_metadata_non_existent_server(self):
+        # List metadata on a non-existent server should not succeed
+        non_existent_server_id = data_utils.rand_uuid()
+        self.assertRaises(exceptions.NotFound,
+                          self.client.list_server_metadata,
+                          non_existent_server_id)
+
+    @test.attr(type=['negative', 'gate'])
+    def test_wrong_key_passed_in_body(self):
+        # Raise BadRequest if key in uri does not match
+        # the key passed in body.
+        meta = {'testkey': 'testvalue'}
+        self.assertRaises(exceptions.BadRequest,
+                          self.client.set_server_metadata_item,
+                          self.server_id, 'key', meta)
+
+    @test.attr(type=['negative', 'gate'])
+    def test_set_metadata_non_existent_server(self):
+        # Set metadata on a non-existent server should not succeed
+        non_existent_server_id = data_utils.rand_uuid()
+        meta = {'meta1': 'data1'}
+        self.assertRaises(exceptions.NotFound,
+                          self.client.set_server_metadata,
+                          non_existent_server_id,
+                          meta)
+
+    @test.attr(type=['negative', 'gate'])
+    def test_update_metadata_non_existent_server(self):
+        # An update should not happen for a non-existent server
+        non_existent_server_id = data_utils.rand_uuid()
+        meta = {'key1': 'value1', 'key2': 'value2'}
+        self.assertRaises(exceptions.NotFound,
+                          self.client.update_server_metadata,
+                          non_existent_server_id,
+                          meta)
+
+    @test.attr(type=['negative', 'gate'])
+    def test_update_metadata_with_blank_key(self):
+        # Blank key should trigger an error
+        meta = {'': 'data1'}
+        self.assertRaises(exceptions.BadRequest,
+                          self.client.update_server_metadata,
+                          self.server_id, meta=meta)
+
+    @test.attr(type=['negative', 'gate'])
+    def test_delete_metadata_non_existent_server(self):
+        # Should not be able to delete metadata item from a non-existent server
+        non_existent_server_id = data_utils.rand_uuid()
+        self.assertRaises(exceptions.NotFound,
+                          self.client.delete_server_metadata_item,
+                          non_existent_server_id,
+                          'd')
+
+    @test.attr(type=['negative', 'gate'])
+    def test_metadata_items_limit(self):
+        # Raise a 413 OverLimit exception while exceeding metadata items limit
+        # for tenant.
+        _, quota_set = self.quotas.get_quota_set(self.tenant_id)
+        quota_metadata = quota_set['metadata_items']
+        req_metadata = {}
+        for num in range(1, quota_metadata + 2):
+            req_metadata['key' + str(num)] = 'val' + str(num)
+        self.assertRaises(exceptions.OverLimit,
+                          self.client.set_server_metadata,
+                          self.server_id, req_metadata)
+
+        # Raise a 413 OverLimit exception while exceeding metadata items limit
+        # for tenant (update).
+        self.assertRaises(exceptions.OverLimit,
+                          self.client.update_server_metadata,
+                          self.server_id, req_metadata)
+
+    @test.attr(type=['negative', 'gate'])
+    def test_set_server_metadata_blank_key(self):
+        # Raise a bad request error for blank key.
+        # set_server_metadata will replace all metadata with new value
+        meta = {'': 'data1'}
+        self.assertRaises(exceptions.BadRequest,
+                          self.client.set_server_metadata,
+                          self.server_id, meta=meta)
+
+    @test.attr(type=['negative', 'gate'])
+    def test_set_server_metadata_missing_metadata(self):
+        # Raise a bad request error for a missing metadata field
+        # set_server_metadata will replace all metadata with new value
+        meta = {'meta1': 'data1'}
+        self.assertRaises(exceptions.BadRequest,
+                          self.client.set_server_metadata,
+                          self.server_id, meta=meta, no_metadata_field=True)