Add response schema validation for volume backups

This is to add response schema validation for volume backups.

Change-Id: I2ab6ecfeb0c48c7ef4a0fc94e670d43dbc3f6650
partially-implements: blueprint volume-response-schema-validation
diff --git a/tempest/api/volume/admin/test_volumes_backup.py b/tempest/api/volume/admin/test_volumes_backup.py
index 45060d0..bd4b3fa 100644
--- a/tempest/api/volume/admin/test_volumes_backup.py
+++ b/tempest/api/volume/admin/test_volumes_backup.py
@@ -67,11 +67,8 @@
         # Export Backup
         export_backup = (self.admin_backups_client.export_backup(backup['id'])
                          ['backup-record'])
-        self.assertIn('backup_service', export_backup)
-        self.assertIn('backup_url', export_backup)
         self.assertTrue(export_backup['backup_service'].startswith(
                         'cinder.backup.drivers'))
-        self.assertIsNotNone(export_backup['backup_url'])
 
         # NOTE(geguileo): Backups are imported with the same backup id
         # (important for incremental backups among other things), so we cannot
@@ -92,7 +89,6 @@
         # deletions will delete data from the backup back-end because they
         # were both pointing to the same backend data.
         self.addCleanup(self._delete_backup, new_id)
-        self.assertIn("id", import_backup)
         self.assertEqual(new_id, import_backup['id'])
         waiters.wait_for_volume_resource_status(self.admin_backups_client,
                                                 import_backup['id'],
diff --git a/tempest/api/volume/test_volumes_backup.py b/tempest/api/volume/test_volumes_backup.py
index c178272..e3b8800 100644
--- a/tempest/api/volume/test_volumes_backup.py
+++ b/tempest/api/volume/test_volumes_backup.py
@@ -80,11 +80,7 @@
         self.assertEqual('container', backup['container'])
 
         # Get all backups with detail
-        backups = self.backups_client.list_backups(
-            detail=True)['backups']
-        for backup_info in backups:
-            self.assertIn('created_at', backup_info)
-            self.assertIn('links', backup_info)
+        backups = self.backups_client.list_backups(detail=True)['backups']
         self.assertIn((backup['name'], backup['id']),
                       [(m['name'], m['id']) for m in backups])
 
@@ -176,7 +172,6 @@
             backup['id'], **update_kwargs)['backup']
         self.assertEqual(backup['id'], update_backup['id'])
         self.assertEqual(update_kwargs['name'], update_backup['name'])
-        self.assertIn('links', update_backup)
 
         # Assert response body for show_backup method
         retrieved_backup = self.backups_client.show_backup(
diff --git a/tempest/lib/api_schema/response/volume/backups.py b/tempest/lib/api_schema/response/volume/backups.py
new file mode 100644
index 0000000..9e85f5f
--- /dev/null
+++ b/tempest/lib/api_schema/response/volume/backups.py
@@ -0,0 +1,229 @@
+# Copyright 2015 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.
+
+import copy
+
+from tempest.lib.api_schema.response.compute.v2_1 import parameter_types
+
+common_show_backup = {
+    'type': 'object',
+    'properties': {
+        'status': {'type': 'string'},
+        'object_count': {'type': 'integer'},
+        'container': {'type': ['string', 'null']},
+        'description': {'type': ['string', 'null']},
+        'links': parameter_types.links,
+        'availability_zone': {'type': ['string', 'null']},
+        'created_at': parameter_types.date_time,
+        'updated_at': parameter_types.date_time_or_null,
+        'name': {'type': ['string', 'null']},
+        'has_dependent_backups': {'type': 'boolean'},
+        'volume_id': {'type': 'string', 'format': 'uuid'},
+        'fail_reason': {'type': ['string', 'null']},
+        'size': {'type': 'integer'},
+        'id': {'type': 'string', 'format': 'uuid'},
+        'is_incremental': {'type': 'boolean'},
+        'data_timestamp': parameter_types.date_time_or_null,
+        'snapshot_id': {'type': ['string', 'null']},
+        # TODO(zhufl): os-backup-project-attr:project_id is added
+        # in 3.18, we should move it to the 3.18 schema file when
+        # microversion is supported in volume interfaces.
+        'os-backup-project-attr:project_id': {
+            'type': 'string', 'format': 'uuid'},
+        # TODO(zhufl): metadata is added in 3.43, we should move it
+        # to the 3.43 schema file when microversion is supported
+        # in volume interfaces.
+        'metadata': {'^.+$': {'type': 'string'}},
+        # TODO(zhufl): user_id is added in 3.56, we should move it
+        # to the 3.56 schema file when microversion is supported
+        # in volume interfaces.
+        'user_id': {'type': 'string'},
+    },
+    'additionalProperties': False,
+    'required': ['status', 'object_count', 'fail_reason', 'links',
+                 'created_at', 'updated_at', 'name', 'volume_id', 'size', 'id',
+                 'data_timestamp']
+}
+
+create_backup = {
+    'status_code': [202],
+    'response_body': {
+        'type': 'object',
+        'properties': {
+            'backup': {
+                'type': 'object',
+                'properties': {
+                    'id': {'type': 'string', 'format': 'uuid'},
+                    'links': parameter_types.links,
+                    'name': {'type': 'string'},
+                    # TODO(zhufl): metadata is added in 3.43, we should move it
+                    # to the 3.43 schema file when microversion is supported
+                    # in volume interfaces.
+                    'metadata': {'^.+$': {'type': 'string'}},
+                },
+                'additionalProperties': False,
+                'required': ['id', 'links', 'name']
+            }
+        },
+        'additionalProperties': False,
+        'required': ['backup']
+    }
+}
+
+update_backup = {
+    'status_code': [200],
+    'response_body': {
+        'type': 'object',
+        'properties': {
+            'backup': {
+                'type': 'object',
+                'properties': {
+                    'id': {'type': 'string', 'format': 'uuid'},
+                    'links': parameter_types.links,
+                    'name': {'type': 'string'},
+                    'metadata': {'^.+$': {'type': 'string'}}
+                },
+                'additionalProperties': False,
+                'required': ['id', 'links', 'name']
+            }
+        },
+        'additionalProperties': False,
+        'required': ['backup']
+    }
+}
+
+restore_backup = {
+    'status_code': [202],
+    'response_body': {
+        'type': 'object',
+        'properties': {
+            'restore': {
+                'type': 'object',
+                'properties': {
+                    'backup_id': {'type': 'string', 'format': 'uuid'},
+                    'volume_id': {'type': 'string', 'format': 'uuid'},
+                    'volume_name': {'type': 'string'},
+                },
+                'additionalProperties': False,
+                'required': ['backup_id', 'volume_id', 'volume_name']
+            }
+        },
+        'additionalProperties': False,
+        'required': ['restore']
+    }
+}
+
+delete_backup = {'status_code': [202]}
+
+show_backup = {
+    'status_code': [200],
+    'response_body': {
+        'type': 'object',
+        'properties': {
+            'backup': common_show_backup
+        },
+        'additionalProperties': False,
+        'required': ['backup']
+    }
+}
+
+list_backups_no_detail = {
+    'status_code': [200],
+    'response_body': {
+        'type': 'object',
+        'properties': {
+            'backups': {
+                'type': 'array',
+                'items': {
+                    'type': 'object',
+                    'properties': {
+                        'links': parameter_types.links,
+                        'id': {'type': 'string', 'format': 'uuid'},
+                        'name': {'type': ['string', 'null']},
+                        # TODO(zhufl): count is added in 3.45, we should move
+                        # it to the 3.45 schema file when microversion is
+                        # supported in volume interfaces
+                        'count': {'type': 'integer'}
+                    },
+                    'additionalProperties': False,
+                    'required': ['links', 'id', 'name']
+                }
+            }
+        },
+        'additionalProperties': False,
+        'required': ['backups'],
+    }
+}
+
+list_backups_detail = copy.deepcopy(common_show_backup)
+# TODO(zhufl): count is added in 3.45, we should move it to the 3.45 schema
+# file when microversion is supported in volume interfaces
+list_backups_detail['properties'].update({'count': {'type': 'integer'}})
+list_backups_with_detail = {
+    'status_code': [200],
+    'response_body': {
+        'type': 'object',
+        'properties': {
+            'backups': {
+                'type': 'array',
+                'items': list_backups_detail
+            }
+        },
+        'additionalProperties': False,
+        'required': ['backups'],
+    }
+}
+
+export_backup = {
+    'status_code': [200],
+    'response_body': {
+        'type': 'object',
+        'properties': {
+            'backup-record': {
+                'type': 'object',
+                'properties': {
+                    'backup_service': {'type': 'string'},
+                    'backup_url': {'type': 'string'}
+                },
+                'additionalProperties': False,
+                'required': ['backup_service', 'backup_url']
+            }
+        },
+        'additionalProperties': False,
+        'required': ['backup-record']
+    }
+}
+
+import_backup = {
+    'status_code': [201],
+    'response_body': {
+        'type': 'object',
+        'properties': {
+            'backup': {
+                'type': 'object',
+                'properties': {
+                    'id': {'type': 'string', 'format': 'uuid'},
+                    'links': parameter_types.links,
+                    'name': {'type': ['string', 'null']},
+                },
+                'additionalProperties': False,
+                'required': ['id', 'links', 'name']
+            }
+        },
+        'additionalProperties': False,
+        'required': ['backup']
+    }
+}
+
+reset_backup_status = {'status_code': [202]}
diff --git a/tempest/lib/services/volume/v3/backups_client.py b/tempest/lib/services/volume/v3/backups_client.py
index 970471e..1df45fa 100644
--- a/tempest/lib/services/volume/v3/backups_client.py
+++ b/tempest/lib/services/volume/v3/backups_client.py
@@ -16,6 +16,7 @@
 from oslo_serialization import jsonutils as json
 from six.moves.urllib import parse as urllib
 
+from tempest.lib.api_schema.response.volume import backups 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
@@ -34,7 +35,7 @@
         post_body = json.dumps({'backup': kwargs})
         resp, body = self.post('backups', post_body)
         body = json.loads(body)
-        self.expected_success(202, resp.status)
+        self.validate_response(schema.create_backup, resp, body)
         return rest_client.ResponseBody(resp, body)
 
     def update_backup(self, backup_id, **kwargs):
@@ -47,7 +48,7 @@
         put_body = json.dumps({'backup': kwargs})
         resp, body = self.put('backups/%s' % backup_id, put_body)
         body = json.loads(body)
-        self.expected_success(200, resp.status)
+        self.validate_response(schema.update_backup, resp, body)
         return rest_client.ResponseBody(resp, body)
 
     def restore_backup(self, backup_id, **kwargs):
@@ -60,13 +61,13 @@
         post_body = json.dumps({'restore': kwargs})
         resp, body = self.post('backups/%s/restore' % (backup_id), post_body)
         body = json.loads(body)
-        self.expected_success(202, resp.status)
+        self.validate_response(schema.restore_backup, resp, body)
         return rest_client.ResponseBody(resp, body)
 
     def delete_backup(self, backup_id):
         """Delete a backup of volume."""
         resp, body = self.delete('backups/%s' % backup_id)
-        self.expected_success(202, resp.status)
+        self.validate_response(schema.delete_backup, resp, body)
         return rest_client.ResponseBody(resp, body)
 
     def show_backup(self, backup_id):
@@ -74,7 +75,7 @@
         url = "backups/%s" % backup_id
         resp, body = self.get(url)
         body = json.loads(body)
-        self.expected_success(200, resp.status)
+        self.validate_response(schema.show_backup, resp, body)
         return rest_client.ResponseBody(resp, body)
 
     def list_backups(self, detail=False, **params):
@@ -86,13 +87,15 @@
         https://docs.openstack.org/api-ref/block-storage/v3/index.html#list-backups-with-detail
         """
         url = "backups"
+        list_backups_schema = schema.list_backups_no_detail
         if detail:
             url += "/detail"
+            list_backups_schema = schema.list_backups_with_detail
         if params:
             url += '?%s' % urllib.urlencode(params)
         resp, body = self.get(url)
         body = json.loads(body)
-        self.expected_success(200, resp.status)
+        self.validate_response(list_backups_schema, resp, body)
         return rest_client.ResponseBody(resp, body)
 
     def export_backup(self, backup_id):
@@ -100,7 +103,7 @@
         url = "backups/%s/export_record" % backup_id
         resp, body = self.get(url)
         body = json.loads(body)
-        self.expected_success(200, resp.status)
+        self.validate_response(schema.export_backup, resp, body)
         return rest_client.ResponseBody(resp, body)
 
     def import_backup(self, **kwargs):
@@ -113,14 +116,14 @@
         post_body = json.dumps({'backup-record': kwargs})
         resp, body = self.post("backups/import_record", post_body)
         body = json.loads(body)
-        self.expected_success(201, resp.status)
+        self.validate_response(schema.import_backup, resp, body)
         return rest_client.ResponseBody(resp, body)
 
     def reset_backup_status(self, backup_id, status):
         """Reset the specified backup's status."""
         post_body = json.dumps({'os-reset_status': {"status": status}})
         resp, body = self.post('backups/%s/action' % backup_id, post_body)
-        self.expected_success(202, resp.status)
+        self.validate_response(schema.reset_backup_status, resp, body)
         return rest_client.ResponseBody(resp, body)
 
     def is_resource_deleted(self, id):
diff --git a/tempest/tests/lib/services/volume/v3/test_backups_client.py b/tempest/tests/lib/services/volume/v3/test_backups_client.py
index 97e1132..ca7918a 100644
--- a/tempest/tests/lib/services/volume/v3/test_backups_client.py
+++ b/tempest/tests/lib/services/volume/v3/test_backups_client.py
@@ -45,6 +45,8 @@
                 "availability_zone": "az1",
                 "container": "volumebackups",
                 "created_at": "2013-04-02T10:35:27.000000",
+                "updated_at": "2013-04-02T10:39:27.000000",
+                "data_timestamp": "2013-04-02T10:35:27.000000",
                 "description": None,
                 "fail_reason": None,
                 "id": "2ef47aee-8844-490c-804d-2a8efe561c65",
@@ -64,7 +66,6 @@
                 "user_id": "515ba0dd59f84f25a6a084a45d8d93b2",
                 "size": 1,
                 "status": "available",
-                "updated_at": "2013-04-02T10:35:27.000000",
                 "volume_id": "e5185058-943a-4cb4-96d9-72c184c337d6",
                 "is_incremental": True,
                 "has_dependent_backups": False