Add tests for server metadata

* Raise exception while passing too many metadata items during
set server metadata
* Raise exception while passing too many metadata items during
update server metadata
* Passing an empty body during update server metadata preserves
original metadata items
* Make exception asserts consistent in all test cases

Partially implements bp missing-tempest-tests

Change-Id: I66d5102ddda10072943c454578eb445a3a480b1c
diff --git a/tempest/tests/compute/servers/test_server_metadata.py b/tempest/tests/compute/servers/test_server_metadata.py
index 6958a7d..7896466 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,34 @@
     @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)