Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 1 | # 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 | |
Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 16 | import time |
| 17 | |
Matthew Treinish | 2190551 | 2015-07-13 10:33:35 -0400 | [diff] [blame] | 18 | from oslo_serialization import jsonutils as json |
ravikumar-venkatesan | 18c1d0e | 2015-03-18 11:28:37 +0000 | [diff] [blame] | 19 | |
Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 20 | from tempest import exceptions |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 21 | from tempest.lib.common import rest_client |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 22 | from tempest.lib import exceptions as lib_exc |
Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 23 | |
| 24 | |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 25 | class BaseBackupsClient(rest_client.RestClient): |
Ken'ichi Ohmichi | b279084 | 2015-11-17 11:46:13 +0000 | [diff] [blame] | 26 | """Client class to send CRUD Volume backup API requests""" |
Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 27 | |
Ghanshyam | 4a9e303 | 2015-12-10 17:50:54 +0900 | [diff] [blame] | 28 | def create_backup(self, **kwargs): |
Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 29 | """Creates a backup of volume.""" |
Ghanshyam | 4a9e303 | 2015-12-10 17:50:54 +0900 | [diff] [blame] | 30 | post_body = json.dumps({'backup': kwargs}) |
Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 31 | resp, body = self.post('backups', post_body) |
| 32 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 33 | self.expected_success(202, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 34 | return rest_client.ResponseBody(resp, body) |
Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 35 | |
Ghanshyam | 4a9e303 | 2015-12-10 17:50:54 +0900 | [diff] [blame] | 36 | def restore_backup(self, backup_id, **kwargs): |
Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 37 | """Restore volume from backup.""" |
Ghanshyam | 4a9e303 | 2015-12-10 17:50:54 +0900 | [diff] [blame] | 38 | post_body = json.dumps({'restore': kwargs}) |
Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 39 | resp, body = self.post('backups/%s/restore' % (backup_id), post_body) |
| 40 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 41 | self.expected_success(202, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 42 | return rest_client.ResponseBody(resp, body) |
Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 43 | |
| 44 | def delete_backup(self, backup_id): |
| 45 | """Delete a backup of volume.""" |
| 46 | resp, body = self.delete('backups/%s' % (str(backup_id))) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 47 | self.expected_success(202, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 48 | return rest_client.ResponseBody(resp, body) |
Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 49 | |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 50 | def show_backup(self, backup_id): |
Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 51 | """Returns the details of a single backup.""" |
| 52 | url = "backups/%s" % str(backup_id) |
| 53 | resp, body = self.get(url) |
| 54 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 55 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 56 | return rest_client.ResponseBody(resp, body) |
Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 57 | |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 58 | def list_backups(self, detail=False): |
raiesmh08 | f04da34 | 2014-02-28 17:14:43 +0530 | [diff] [blame] | 59 | """Information for all the tenant's backups.""" |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 60 | url = "backups" |
| 61 | if detail: |
| 62 | url += "/detail" |
raiesmh08 | f04da34 | 2014-02-28 17:14:43 +0530 | [diff] [blame] | 63 | resp, body = self.get(url) |
| 64 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 65 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 66 | return rest_client.ResponseBody(resp, body) |
raiesmh08 | f04da34 | 2014-02-28 17:14:43 +0530 | [diff] [blame] | 67 | |
ravikumar-venkatesan | 18c1d0e | 2015-03-18 11:28:37 +0000 | [diff] [blame] | 68 | def export_backup(self, backup_id): |
| 69 | """Export backup metadata record.""" |
| 70 | url = "backups/%s/export_record" % backup_id |
| 71 | resp, body = self.get(url) |
| 72 | body = json.loads(body) |
| 73 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 74 | return rest_client.ResponseBody(resp, body) |
ravikumar-venkatesan | 18c1d0e | 2015-03-18 11:28:37 +0000 | [diff] [blame] | 75 | |
Ghanshyam | 4a9e303 | 2015-12-10 17:50:54 +0900 | [diff] [blame] | 76 | def import_backup(self, **kwargs): |
ravikumar-venkatesan | 18c1d0e | 2015-03-18 11:28:37 +0000 | [diff] [blame] | 77 | """Import backup metadata record.""" |
Ghanshyam | 4a9e303 | 2015-12-10 17:50:54 +0900 | [diff] [blame] | 78 | post_body = json.dumps({'backup-record': kwargs}) |
ravikumar-venkatesan | 18c1d0e | 2015-03-18 11:28:37 +0000 | [diff] [blame] | 79 | resp, body = self.post("backups/import_record", post_body) |
| 80 | body = json.loads(body) |
| 81 | self.expected_success(201, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 82 | return rest_client.ResponseBody(resp, body) |
ravikumar-venkatesan | 18c1d0e | 2015-03-18 11:28:37 +0000 | [diff] [blame] | 83 | |
Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 84 | def wait_for_backup_status(self, backup_id, status): |
| 85 | """Waits for a Backup to reach a given status.""" |
John Warren | 6cadeee | 2015-08-13 17:00:56 +0000 | [diff] [blame] | 86 | body = self.show_backup(backup_id)['backup'] |
Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 87 | backup_status = body['status'] |
| 88 | start = int(time.time()) |
| 89 | |
| 90 | while backup_status != status: |
| 91 | time.sleep(self.build_interval) |
John Warren | 6cadeee | 2015-08-13 17:00:56 +0000 | [diff] [blame] | 92 | body = self.show_backup(backup_id)['backup'] |
Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 93 | backup_status = body['status'] |
| 94 | if backup_status == 'error': |
| 95 | raise exceptions.VolumeBackupException(backup_id=backup_id) |
| 96 | |
| 97 | if int(time.time()) - start >= self.build_timeout: |
Martin Pavlasek | 1102c3a | 2014-10-20 17:17:55 +0200 | [diff] [blame] | 98 | message = ('Volume backup %s failed to reach %s status ' |
| 99 | '(current %s) within the required time (%s s).' % |
| 100 | (backup_id, status, backup_status, |
| 101 | self.build_timeout)) |
Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 102 | raise exceptions.TimeoutException(message) |
jun xie | ebc3da3 | 2014-11-18 14:34:56 +0800 | [diff] [blame] | 103 | |
ravikumar-venkatesan | 18c1d0e | 2015-03-18 11:28:37 +0000 | [diff] [blame] | 104 | def wait_for_backup_deletion(self, backup_id): |
| 105 | """Waits for backup deletion""" |
| 106 | start_time = int(time.time()) |
| 107 | while True: |
| 108 | try: |
| 109 | self.show_backup(backup_id) |
| 110 | except lib_exc.NotFound: |
| 111 | return |
| 112 | if int(time.time()) - start_time >= self.build_timeout: |
| 113 | raise exceptions.TimeoutException |
| 114 | time.sleep(self.build_interval) |