Merge "Simplify xml-json inheritance in identity"
diff --git a/tempest/tests/compute/servers/test_console_output.py b/tempest/tests/compute/servers/test_console_output.py
deleted file mode 100644
index 4ddbc2f..0000000
--- a/tempest/tests/compute/servers/test_console_output.py
+++ /dev/null
@@ -1,96 +0,0 @@
-# vim: tabstop=4 shiftwidth=4 softtabstop=4
-
-# Copyright 2012 OpenStack, LLC
-# 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.
-
-import testtools
-
-from tempest.common.utils.data_utils import rand_name
-from tempest import exceptions
-from tempest.test import attr
-from tempest.tests.compute import base
-
-
-#TODO(afazekas): move these to the server actions test
-@attr(type='smoke')
-class ConsoleOutputTestJSON(base.BaseComputeTest):
-    _interface = 'json'
-
-    @classmethod
-    def setUpClass(cls):
-        super(ConsoleOutputTestJSON, cls).setUpClass()
-        cls.name = rand_name('server')
-        resp, server = cls.servers_client.create_server(cls.name,
-                                                        cls.image_ref,
-                                                        cls.flavor_ref)
-        cls.server_id = server['id']
-
-        cls.servers_client.wait_for_server_status(cls.server_id, 'ACTIVE')
-
-    @classmethod
-    def tearDownClass(cls):
-        cls.servers_client.delete_server(cls.server_id)
-        super(ConsoleOutputTestJSON, cls).tearDownClass()
-
-    @attr(type='positive')
-    def test_get_console_output(self):
-        # Positive test:Should be able to GET the console output
-        # for a given server_id and number of lines
-        def get_output():
-            resp, output = self.servers_client.get_console_output(
-                self.server_id, 10)
-            self.assertEqual(200, resp.status)
-            self.assertNotEqual(output, None)
-            lines = len(output.split('\n'))
-            self.assertEqual(lines, 10)
-        self.wait_for(get_output)
-
-    @attr(type='negative')
-    def test_get_console_output_invalid_server_id(self):
-        # Negative test: Should not be able to get the console output
-        # for an invalid server_id
-        try:
-            resp, output = self.servers_client.get_console_output(
-                '!@#$%^&*()', 10)
-        except exceptions.NotFound:
-            pass
-
-    @attr(type='positive')
-    @testtools.skip('Until tempest bug 1014683 is fixed.')
-    def test_get_console_output_server_id_in_reboot_status(self):
-        # Positive test:Should be able to GET the console output
-        # for a given server_id in reboot status
-        try:
-            resp, output = self.servers_client.reboot(self.server_id, 'SOFT')
-            self.servers_client.wait_for_server_status(self.server_id,
-                                                       'REBOOT')
-            resp, server = self.servers_client.get_server(self.server_id)
-            if (server['status'] == 'REBOOT'):
-                resp, output = self.servers_client.get_console_output(
-                    self.server_id, 10)
-                self.assertEqual(200, resp.status)
-                self.assertNotEqual(output, None)
-                lines = len(output.split('\n'))
-                self.assertEqual(lines, 10)
-            else:
-                self.fail("Could not capture instance in Reboot status")
-        finally:
-            self.servers_client.wait_for_server_status(self.server_id,
-                                                       'ACTIVE')
-
-
-@attr(type='smoke')
-class ConsoleOutputTestXML(ConsoleOutputTestJSON):
-    _interface = 'xml'
diff --git a/tempest/tests/compute/servers/test_server_actions.py b/tempest/tests/compute/servers/test_server_actions.py
index a9ada39..bc8e843 100644
--- a/tempest/tests/compute/servers/test_server_actions.py
+++ b/tempest/tests/compute/servers/test_server_actions.py
@@ -35,6 +35,8 @@
     run_ssh = tempest.config.TempestConfig().compute.run_ssh
 
     def setUp(self):
+        #NOTE(afazekas): Normally we use the same server with all test cases,
+        # but if it has an issue, we build a new one
         super(ServerActionsTestJSON, self).setUp()
         # Check if the server is in a clean state after test
         try:
@@ -208,18 +210,48 @@
         file_contents = 'Test server rebuild.'
         personality = [{'path': '/etc/rebuild.txt',
                         'contents': base64.b64encode(file_contents)}]
-        try:
-            resp, rebuilt_server = self.client.rebuild(999,
-                                                       self.image_ref_alt,
-                                                       name=new_name,
-                                                       meta=meta,
-                                                       personality=personality,
-                                                       adminPass='rebuild')
-        except exceptions.NotFound:
-            pass
-        else:
-            self.fail('The server rebuild for a non existing server should not'
-                      ' be allowed')
+        self.assertRaises(exceptions.NotFound,
+                          self.client.rebuild,
+                          999, self.image_ref_alt,
+                          name=new_name, meta=meta,
+                          personality=personality,
+                          adminPass='rebuild')
+
+    @attr(type='positive')
+    def test_get_console_output(self):
+        # Positive test:Should be able to GET the console output
+        # for a given server_id and number of lines
+        def get_output():
+            resp, output = self.servers_client.get_console_output(
+                self.server_id, 10)
+            self.assertEqual(200, resp.status)
+            self.assertNotEqual(output, None)
+            lines = len(output.split('\n'))
+            self.assertEqual(lines, 10)
+        self.wait_for(get_output)
+
+    @attr(type='negative')
+    def test_get_console_output_invalid_server_id(self):
+        # Negative test: Should not be able to get the console output
+        # for an invalid server_id
+        self.assertRaises(exceptions.NotFound,
+                          self.servers_client.get_console_output,
+                          '!@#$%^&*()', 10)
+
+    @attr(type='positive')
+    @testtools.skip('Until tempest bug 1014683 is fixed.')
+    def test_get_console_output_server_id_in_reboot_status(self):
+        # Positive test:Should be able to GET the console output
+        # for a given server_id in reboot status
+        resp, output = self.servers_client.reboot(self.server_id, 'SOFT')
+        self.servers_client.wait_for_server_status(self.server_id,
+                                                   'REBOOT')
+        resp, output = self.servers_client.get_console_output(self.server_id,
+                                                              10)
+        self.assertEqual(200, resp.status)
+        self.assertNotEqual(output, None)
+        lines = len(output.split('\n'))
+        self.assertEqual(lines, 10)
 
     @classmethod
     def rebuild_servers(cls):
diff --git a/tempest/tests/compute/servers/test_server_metadata.py b/tempest/tests/compute/servers/test_server_metadata.py
index 6958a7d..cdfe2f1 100644
--- a/tempest/tests/compute/servers/test_server_metadata.py
+++ b/tempest/tests/compute/servers/test_server_metadata.py
@@ -28,7 +28,11 @@
     def setUpClass(cls):
         super(ServerMetadataTest, 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]
         #Create a server to be used for all read only tests
         name = rand_name('server')
         resp, server = cls.client.create_server(name, cls.image_ref,
@@ -110,6 +114,15 @@
         expected = {'key1': 'alt1', 'key2': 'value2', 'key3': 'value3'}
         self.assertEqual(expected, resp_metadata)
 
+    def test_update_metadata_empty_body(self):
+        # The original metadata should not be lost if empty metadata body is
+        # passed
+        meta = {}
+        _, metadata = self.client.update_server_metadata(self.server_id, meta)
+        resp, resp_metadata = self.client.list_server_metadata(self.server_id)
+        expected = {'key1': 'value1', 'key2': 'value2'}
+        self.assertEqual(expected, resp_metadata)
+
     def test_get_server_metadata_item(self):
         # The value for a specic metadata key should be returned
         resp, meta = self.client.get_server_metadata_item(self.server_id,
@@ -143,63 +156,40 @@
     @attr(type='negative')
     def test_get_nonexistant_server_metadata_item(self):
         # Negative test: GET on nonexistant server should not succeed
-        try:
-            resp, meta = self.client.get_server_metadata_item(999, 'test2')
-        except Exception:
-            pass
-        else:
-            self.fail('GET on nonexistant server should not succeed')
+        self.assertRaises(exceptions.NotFound,
+                          self.client.get_server_metadata_item, 999, 'test2')
 
     @attr(type='negative')
     def test_list_nonexistant_server_metadata(self):
         # Negative test:List metadata on a non existant server should
         # not succeed
-        try:
-            resp, metadata = self.client.list_server_metadata(999)
-        except Exception:
-            pass
-        else:
-            self.fail('List metadata on a non existant server should'
-                      'not succeed')
+        self.assertRaises(exceptions.NotFound,
+                          self.client.list_server_metadata, 999)
 
     @attr(type='negative')
     def test_set_server_metadata_item_incorrect_uri_key(self):
-        #Raise BadRequest if key in uri does not match
-        #the key passed in body.
+        # Raise BadRequest if key in uri does not match
+        # the key passed in body.
 
         meta = {'testkey': 'testvalue'}
-        try:
-            resp, metadata = self.client.set_server_metadata_item(
-                                        self.server_id, 'key', meta)
-        except exceptions.BadRequest:
-            pass
-        else:
-            self.fail('Should raise BadRequest if URI key does not match key'
-                      'passed in the body')
+        self.assertRaises(exceptions.BadRequest,
+                          self.client.set_server_metadata_item,
+                          self.server_id, 'key', meta)
 
     @attr(type='negative')
     def test_set_nonexistant_server_metadata(self):
         # Negative test: Set metadata on a non existant server should not
         # succeed
         meta = {'meta1': 'data1'}
-        try:
-            resp, metadata = self.client.set_server_metadata(999, meta)
-        except Exception:
-            pass
-        else:
-            self.fail('Set metadata on a non existant server should'
-                      'not succeed')
+        self.assertRaises(exceptions.NotFound,
+                          self.client.set_server_metadata, 999, meta)
 
     @attr(type='negative')
     def test_update_nonexistant_server_metadata(self):
         # Negative test: An update should not happen for a nonexistant image
         meta = {'key1': 'value1', 'key2': 'value2'}
-        try:
-            resp, metadata = self.client.update_server_metadata(999, meta)
-        except Exception:
-            pass
-        else:
-            self.fail('An update should not happen for a nonexistant image')
+        self.assertRaises(exceptions.NotFound,
+                          self.client.update_server_metadata, 999, meta)
 
     @attr(type='negative')
     def test_update_metadata_key_error(self):
@@ -212,12 +202,43 @@
     @attr(type='negative')
     def test_delete_nonexistant_server_metadata_item(self):
         # Negative test: Should not be able to delete metadata item from a
-        # nonexistant server
+        #  nonexistant server
 
         #Delete the metadata item
-        try:
-            resp, metadata = self.client.delete_server_metadata_item(999, 'd')
-        except Exception:
-            pass
-        else:
-            self.fail('A delete should not happen for a nonexistant image')
+        self.assertRaises(exceptions.NotFound,
+                          self.client.delete_server_metadata_item, 999, 'd')
+
+    @attr(type='negative')
+    def test_set_server_metadata_too_long(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)
+
+    @attr(type='negative')
+    def test_update_server_metadata_too_long(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.update_server_metadata,
+                          self.server_id, req_metadata)
+
+    @attr(type='negative')
+    def test_update_all_metadata_field_error(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)