Add response schema validation for volume messages

This is to add response schema validation for volume
messages, also to remove the fields check in testcases.

NOTE: For the message_level field, possible value is only
‘ERROR’ now, but may be extended later, so we do not restrictly
check the value here.

Change-Id: I1c404c942ef9c5c96aa898721487d891f8795eb4
partially-implements: blueprint volume-response-schema-validation
diff --git a/tempest/api/volume/admin/test_user_messages.py b/tempest/api/volume/admin/test_user_messages.py
index 9907497..8048017 100644
--- a/tempest/api/volume/admin/test_user_messages.py
+++ b/tempest/api/volume/admin/test_user_messages.py
@@ -20,18 +20,6 @@
 
 CONF = config.CONF
 
-MESSAGE_KEYS = [
-    'created_at',
-    'event_id',
-    'guaranteed_until',
-    'id',
-    'message_level',
-    'request_id',
-    'resource_type',
-    'resource_uuid',
-    'user_message',
-    'links']
-
 
 class UserMessagesTest(base.BaseVolumeAdminTest):
     _api_version = 3
@@ -66,18 +54,11 @@
         message_id = self._create_user_message()
         self.addCleanup(self.messages_client.delete_message, message_id)
 
-        # show message
-        message = self.messages_client.show_message(message_id)['message']
-        for key in MESSAGE_KEYS:
-            self.assertIn(key, message.keys(), 'Missing expected key %s' % key)
+        # show message, check response schema
+        self.messages_client.show_message(message_id)
 
-        # list messages
-        messages = self.messages_client.list_messages()['messages']
-        self.assertIsInstance(messages, list)
-        for message in messages:
-            for key in MESSAGE_KEYS:
-                self.assertIn(key, message.keys(),
-                              'Missing expected key %s' % key)
+        # list messages, check response schema
+        self.messages_client.list_messages()
 
     @decorators.idempotent_id('c6eb6901-cdcc-490f-b735-4fe251842aed')
     def test_delete_message(self):
diff --git a/tempest/lib/api_schema/response/volume/messages.py b/tempest/lib/api_schema/response/volume/messages.py
new file mode 100644
index 0000000..381f542
--- /dev/null
+++ b/tempest/lib/api_schema/response/volume/messages.py
@@ -0,0 +1,64 @@
+# Copyright 2018 ZTE 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.lib.api_schema.response.compute.v2_1 import parameter_types
+
+delete_message = {
+    'status_code': [204],
+}
+
+common_show_message = {
+    'type': 'object',
+    'properties': {
+        'request_id': {'type': 'string'},
+        'message_level': {'type': 'string'},
+        'links': parameter_types.links,
+        'event_id': {'type': 'string'},
+        'created_at': parameter_types.date_time,
+        'guaranteed_until': parameter_types.date_time,
+        'resource_uuid': {'type': 'string', 'format': 'uuid'},
+        'id': {'type': 'string', 'format': 'uuid'},
+        'resource_type': {'type': 'string'},
+        'user_message': {'type': 'string'}},
+    'additionalProperties': False,
+    'required': ['request_id', 'message_level', 'event_id', 'created_at',
+                 'id', 'user_message'],
+}
+
+show_message = {
+    'status_code': [200],
+    'response_body': {
+        'type': 'object',
+        'properties': {
+            'message': common_show_message
+        },
+        'additionalProperties': False,
+        'required': ['message']
+    }
+}
+
+list_messages = {
+    'status_code': [200],
+    'response_body': {
+        'type': 'object',
+        'properties': {
+            'messages': {
+                'type': 'array',
+                'items': common_show_message
+            },
+        },
+        'additionalProperties': False,
+        'required': ['messages']
+    }
+}
diff --git a/tempest/lib/services/volume/v3/messages_client.py b/tempest/lib/services/volume/v3/messages_client.py
index 47538cd..b770fac 100644
--- a/tempest/lib/services/volume/v3/messages_client.py
+++ b/tempest/lib/services/volume/v3/messages_client.py
@@ -15,6 +15,7 @@
 
 from oslo_serialization import jsonutils as json
 
+from tempest.lib.api_schema.response.volume import messages as schema
 from tempest.lib.common import rest_client
 from tempest.lib import exceptions as lib_exc
 from tempest.lib.services.volume import base_client
@@ -28,7 +29,7 @@
         url = 'messages/%s' % str(message_id)
         resp, body = self.get(url)
         body = json.loads(body)
-        self.expected_success(200, resp.status)
+        self.validate_response(schema.show_message, resp, body)
         return rest_client.ResponseBody(resp, body)
 
     def list_messages(self):
@@ -36,14 +37,14 @@
         url = 'messages'
         resp, body = self.get(url)
         body = json.loads(body)
-        self.expected_success(200, resp.status)
+        self.validate_response(schema.list_messages, resp, body)
         return rest_client.ResponseBody(resp, body)
 
     def delete_message(self, message_id):
         """Delete a single message."""
         url = 'messages/%s' % str(message_id)
         resp, body = self.delete(url)
-        self.expected_success(204, resp.status)
+        self.validate_response(schema.delete_message, resp, body)
         return rest_client.ResponseBody(resp, body)
 
     def is_resource_deleted(self, id):