blob: 04d27ea087797eff65592484bf05b469b998c307 [file] [log] [blame]
Giulio Fidente74b08ad2014-01-18 04:02:51 +01001# Copyright 2014 OpenStack Foundation
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
Sirushti Murugesan12dc9732016-07-13 22:49:17 +053016from oslo_serialization import base64
Gorka Eguileor9de20c12015-08-13 11:16:43 +020017from oslo_serialization import jsonutils as json
18
Masayuki Igawa1edf94f2014-03-04 18:34:16 +090019from tempest.api.volume import base
Fei Long Wangd39431f2015-05-14 11:30:48 +120020from tempest.common.utils import data_utils
Yaroslav Lobankoved3a35b2016-03-24 22:41:30 -050021from tempest.common import waiters
Giulio Fidente74b08ad2014-01-18 04:02:51 +010022from tempest import config
Ken'ichi Ohmichi6b279c72017-01-27 18:26:59 -080023from tempest.lib import decorators
Giulio Fidente74b08ad2014-01-18 04:02:51 +010024
25CONF = config.CONF
Giulio Fidente74b08ad2014-01-18 04:02:51 +010026
27
lkuchlan9dea88e2016-06-07 17:12:01 +030028class VolumesBackupsAdminV2Test(base.BaseVolumeAdminTest):
Giulio Fidente74b08ad2014-01-18 04:02:51 +010029
30 @classmethod
Rohan Kanade05749152015-01-30 17:15:18 +053031 def skip_checks(cls):
lkuchlan9dea88e2016-06-07 17:12:01 +030032 super(VolumesBackupsAdminV2Test, cls).skip_checks()
Giulio Fidente74b08ad2014-01-18 04:02:51 +010033 if not CONF.volume_feature_enabled.backup:
34 raise cls.skipException("Cinder backup feature disabled")
35
ravikumar-venkatesan18c1d0e2015-03-18 11:28:37 +000036 def _delete_backup(self, backup_id):
bkopilov62d21752016-06-08 10:16:11 +030037 self.admin_backups_client.delete_backup(backup_id)
Ken'ichi Ohmichi153b3d92016-08-03 14:35:46 -070038 self.admin_backups_client.wait_for_resource_deletion(backup_id)
ravikumar-venkatesan18c1d0e2015-03-18 11:28:37 +000039
Gorka Eguileor9de20c12015-08-13 11:16:43 +020040 def _decode_url(self, backup_url):
Sirushti Murugesan12dc9732016-07-13 22:49:17 +053041 return json.loads(base64.decode_as_text(backup_url))
Gorka Eguileor9de20c12015-08-13 11:16:43 +020042
43 def _encode_backup(self, backup):
44 retval = json.dumps(backup)
Sirushti Murugesan12dc9732016-07-13 22:49:17 +053045 return base64.encode_as_text(retval)
Gorka Eguileor9de20c12015-08-13 11:16:43 +020046
47 def _modify_backup_url(self, backup_url, changes):
48 backup = self._decode_url(backup_url)
49 backup.update(changes)
50 return self._encode_backup(backup)
51
Ken'ichi Ohmichi6b279c72017-01-27 18:26:59 -080052 @decorators.idempotent_id('a99c54a1-dd80-4724-8a13-13bf58d4068d')
ravikumar-venkatesan18c1d0e2015-03-18 11:28:37 +000053 def test_volume_backup_export_import(self):
Gorka Eguileor9de20c12015-08-13 11:16:43 +020054 """Test backup export import functionality.
55
56 Cinder allows exporting DB backup information through its API so it can
57 be imported back in case of a DB loss.
58 """
lkuchlana2beb492016-08-17 12:42:44 +030059 volume = self.create_volume()
ravikumar-venkatesan18c1d0e2015-03-18 11:28:37 +000060 # Create backup
zhuflc6ce5392016-08-17 14:34:37 +080061 backup_name = data_utils.rand_name(self.__class__.__name__ + '-Backup')
lkuchlana2beb492016-08-17 12:42:44 +030062 backup = (self.create_backup(backup_client=self.admin_backups_client,
63 volume_id=volume['id'],
64 name=backup_name))
ravikumar-venkatesan18c1d0e2015-03-18 11:28:37 +000065 self.assertEqual(backup_name, backup['name'])
ravikumar-venkatesan18c1d0e2015-03-18 11:28:37 +000066
67 # Export Backup
bkopilov62d21752016-06-08 10:16:11 +030068 export_backup = (self.admin_backups_client.export_backup(backup['id'])
John Warren6cadeee2015-08-13 17:00:56 +000069 ['backup-record'])
ravikumar-venkatesan18c1d0e2015-03-18 11:28:37 +000070 self.assertIn('backup_service', export_backup)
71 self.assertIn('backup_url', export_backup)
72 self.assertTrue(export_backup['backup_service'].startswith(
73 'cinder.backup.drivers'))
74 self.assertIsNotNone(export_backup['backup_url'])
75
Gorka Eguileor9de20c12015-08-13 11:16:43 +020076 # NOTE(geguileo): Backups are imported with the same backup id
77 # (important for incremental backups among other things), so we cannot
78 # import the exported backup information as it is, because that Backup
79 # ID already exists. So we'll fake the data by changing the backup id
80 # in the exported backup DB info we have retrieved before importing it
81 # back.
82 new_id = data_utils.rand_uuid()
83 new_url = self._modify_backup_url(
84 export_backup['backup_url'], {'id': new_id})
85
ravikumar-venkatesan18c1d0e2015-03-18 11:28:37 +000086 # Import Backup
bkopilov62d21752016-06-08 10:16:11 +030087 import_backup = self.admin_backups_client.import_backup(
ravikumar-venkatesan18c1d0e2015-03-18 11:28:37 +000088 backup_service=export_backup['backup_service'],
Gorka Eguileor9de20c12015-08-13 11:16:43 +020089 backup_url=new_url)['backup']
90
91 # NOTE(geguileo): We delete both backups, but only one of those
92 # deletions will delete data from the backup back-end because they
93 # were both pointing to the same backend data.
94 self.addCleanup(self._delete_backup, new_id)
ravikumar-venkatesan18c1d0e2015-03-18 11:28:37 +000095 self.assertIn("id", import_backup)
Gorka Eguileor9de20c12015-08-13 11:16:43 +020096 self.assertEqual(new_id, import_backup['id'])
lkuchlan8f321d82016-09-06 10:06:30 +030097 waiters.wait_for_backup_status(self.admin_backups_client,
98 import_backup['id'], 'available')
ravikumar-venkatesan18c1d0e2015-03-18 11:28:37 +000099
100 # Verify Import Backup
bkopilov62d21752016-06-08 10:16:11 +0300101 backups = self.admin_backups_client.list_backups(
102 detail=True)['backups']
Gorka Eguileor9de20c12015-08-13 11:16:43 +0200103 self.assertIn(new_id, [b['id'] for b in backups])
ravikumar-venkatesan18c1d0e2015-03-18 11:28:37 +0000104
105 # Restore backup
bkopilov62d21752016-06-08 10:16:11 +0300106 restore = self.admin_backups_client.restore_backup(
Gorka Eguileor9de20c12015-08-13 11:16:43 +0200107 backup['id'])['restore']
ravikumar-venkatesan18c1d0e2015-03-18 11:28:37 +0000108 self.addCleanup(self.admin_volume_client.delete_volume,
109 restore['volume_id'])
Gorka Eguileor9de20c12015-08-13 11:16:43 +0200110 self.assertEqual(backup['id'], restore['backup_id'])
Yaroslav Lobankoved3a35b2016-03-24 22:41:30 -0500111 waiters.wait_for_volume_status(self.admin_volume_client,
112 restore['volume_id'], 'available')
ravikumar-venkatesan18c1d0e2015-03-18 11:28:37 +0000113
114 # Verify if restored volume is there in volume list
John Warren6177c9e2015-08-19 20:00:17 +0000115 volumes = self.admin_volume_client.list_volumes()['volumes']
ravikumar-venkatesan18c1d0e2015-03-18 11:28:37 +0000116 self.assertIn(restore['volume_id'], [v['id'] for v in volumes])
lkuchlan8f321d82016-09-06 10:06:30 +0300117 waiters.wait_for_backup_status(self.admin_backups_client,
118 import_backup['id'], 'available')
ravikumar-venkatesan18c1d0e2015-03-18 11:28:37 +0000119
Ken'ichi Ohmichi6b279c72017-01-27 18:26:59 -0800120 @decorators.idempotent_id('47a35425-a891-4e13-961c-c45deea21e94')
bkopilov19fc5fd2016-07-06 12:02:18 +0300121 def test_volume_backup_reset_status(self):
lkuchlana2beb492016-08-17 12:42:44 +0300122 # Create a volume
123 volume = self.create_volume()
bkopilov19fc5fd2016-07-06 12:02:18 +0300124 # Create a backup
zhuflc6ce5392016-08-17 14:34:37 +0800125 backup_name = data_utils.rand_name(
126 self.__class__.__name__ + '-Backup')
lkuchlana2beb492016-08-17 12:42:44 +0300127 backup = self.create_backup(backup_client=self.admin_backups_client,
128 volume_id=volume['id'],
129 name=backup_name)
bkopilov19fc5fd2016-07-06 12:02:18 +0300130 self.assertEqual(backup_name, backup['name'])
bkopilov19fc5fd2016-07-06 12:02:18 +0300131 # Reset backup status to error
132 self.admin_backups_client.reset_backup_status(backup_id=backup['id'],
133 status="error")
lkuchlan8f321d82016-09-06 10:06:30 +0300134 waiters.wait_for_backup_status(self.admin_backups_client,
135 backup['id'], 'error')
bkopilov19fc5fd2016-07-06 12:02:18 +0300136
jun xieebc3da32014-11-18 14:34:56 +0800137
lkuchlan9dea88e2016-06-07 17:12:01 +0300138class VolumesBackupsAdminV1Test(VolumesBackupsAdminV2Test):
jun xieebc3da32014-11-18 14:34:56 +0800139 _api_version = 1