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
This commit is contained in:
zhufl 2018-11-08 14:35:49 +08:00
parent ad25f2577b
commit 93a047f42c
5 changed files with 244 additions and 20 deletions
tempest
api/volume
lib
api_schema/response/volume
services/volume/v3
tests/lib/services/volume/v3

@ -67,11 +67,8 @@ class VolumesBackupsAdminTest(base.BaseVolumeAdminTest):
# 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 @@ class VolumesBackupsAdminTest(base.BaseVolumeAdminTest):
# 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'],

@ -80,11 +80,7 @@ class VolumesBackupsTest(base.BaseVolumeTest):
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 @@ class VolumesBackupsV39Test(base.BaseVolumeTest):
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(

@ -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]}

@ -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 @@ class BackupsClient(base_client.BaseClient):
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 @@ class BackupsClient(base_client.BaseClient):
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 @@ class BackupsClient(base_client.BaseClient):
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 @@ class BackupsClient(base_client.BaseClient):
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 @@ class BackupsClient(base_client.BaseClient):
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 @@ class BackupsClient(base_client.BaseClient):
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 @@ class BackupsClient(base_client.BaseClient):
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):

@ -45,6 +45,8 @@ class TestBackupsClient(base.BaseServiceTest):
"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 @@ class TestBackupsClient(base.BaseServiceTest):
"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