Steve Baker | a5bd912 | 2014-08-11 14:39:00 +1200 | [diff] [blame] | 1 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | # not use this file except in compliance with the License. You may obtain |
| 3 | # a copy of the License at |
| 4 | # |
| 5 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | # |
| 7 | # Unless required by applicable law or agreed to in writing, software |
| 8 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | # License for the specific language governing permissions and limitations |
| 11 | # under the License. |
| 12 | |
Steve Baker | a5bd912 | 2014-08-11 14:39:00 +1200 | [diff] [blame] | 13 | |
| 14 | from cinderclient import exceptions as cinder_exceptions |
Steve Baker | 2464129 | 2015-03-13 10:47:50 +1300 | [diff] [blame] | 15 | from oslo_log import log as logging |
Pavlo Shchelokovskyy | 60e0ecd | 2014-12-14 22:17:21 +0200 | [diff] [blame] | 16 | import six |
Steve Baker | a5bd912 | 2014-08-11 14:39:00 +1200 | [diff] [blame] | 17 | |
Steve Baker | 50d3e8c | 2014-10-21 11:08:50 +1300 | [diff] [blame] | 18 | from heat_integrationtests.common import exceptions |
Anastasia Kuznetsova | e45bfff | 2015-02-25 12:50:34 +0400 | [diff] [blame] | 19 | from heat_integrationtests.scenario import scenario_base |
Steve Baker | a5bd912 | 2014-08-11 14:39:00 +1200 | [diff] [blame] | 20 | |
| 21 | LOG = logging.getLogger(__name__) |
| 22 | |
| 23 | |
Anastasia Kuznetsova | e45bfff | 2015-02-25 12:50:34 +0400 | [diff] [blame] | 24 | class VolumeBackupRestoreIntegrationTest(scenario_base.ScenarioTestsBase): |
Peter Razumovsky | f0ac958 | 2015-09-24 16:49:03 +0300 | [diff] [blame] | 25 | """Class is responsible for testing of volume backup.""" |
Steve Baker | a5bd912 | 2014-08-11 14:39:00 +1200 | [diff] [blame] | 26 | |
| 27 | def setUp(self): |
| 28 | super(VolumeBackupRestoreIntegrationTest, self).setUp() |
Steve Baker | a5bd912 | 2014-08-11 14:39:00 +1200 | [diff] [blame] | 29 | self.volume_description = 'A test volume description 123' |
| 30 | self.volume_size = self.conf.volume_size |
| 31 | |
| 32 | def _cinder_verify(self, volume_id, expected_status='available'): |
| 33 | self.assertIsNotNone(volume_id) |
| 34 | volume = self.volume_client.volumes.get(volume_id) |
| 35 | self.assertIsNotNone(volume) |
| 36 | self.assertEqual(expected_status, volume.status) |
| 37 | self.assertEqual(self.volume_size, volume.size) |
| 38 | self.assertEqual(self.volume_description, |
| 39 | volume.display_description) |
| 40 | |
| 41 | def _outputs_verify(self, stack, expected_status='available'): |
| 42 | self.assertEqual(expected_status, |
| 43 | self._stack_output(stack, 'status')) |
| 44 | self.assertEqual(six.text_type(self.volume_size), |
| 45 | self._stack_output(stack, 'size')) |
| 46 | self.assertEqual(self.volume_description, |
| 47 | self._stack_output(stack, 'display_description')) |
| 48 | |
Anastasia Kuznetsova | e45bfff | 2015-02-25 12:50:34 +0400 | [diff] [blame] | 49 | def check_stack(self, stack_id): |
| 50 | stack = self.client.stacks.get(stack_id) |
Sergey Kraynev | ef9d842 | 2015-02-13 03:41:46 -0500 | [diff] [blame] | 51 | |
Steve Baker | a5bd912 | 2014-08-11 14:39:00 +1200 | [diff] [blame] | 52 | # Verify with cinder that the volume exists, with matching details |
| 53 | volume_id = self._stack_output(stack, 'volume_id') |
| 54 | self._cinder_verify(volume_id, expected_status='in-use') |
| 55 | |
| 56 | # Verify the stack outputs are as expected |
| 57 | self._outputs_verify(stack, expected_status='in-use') |
| 58 | |
| 59 | # Delete the stack and ensure a backup is created for volume_id |
| 60 | # but the volume itself is gone |
Sergey Kraynev | bf67ce3 | 2015-04-17 10:54:20 -0400 | [diff] [blame] | 61 | self._stack_delete(stack_id) |
Steve Baker | a5bd912 | 2014-08-11 14:39:00 +1200 | [diff] [blame] | 62 | self.assertRaises(cinder_exceptions.NotFound, |
| 63 | self.volume_client.volumes.get, |
| 64 | volume_id) |
| 65 | |
| 66 | backups = self.volume_client.backups.list() |
| 67 | self.assertIsNotNone(backups) |
| 68 | backups_filtered = [b for b in backups if b.volume_id == volume_id] |
| 69 | self.assertEqual(1, len(backups_filtered)) |
| 70 | backup = backups_filtered[0] |
| 71 | self.addCleanup(self.volume_client.backups.delete, backup.id) |
| 72 | |
| 73 | # Now, we create another stack where the volume is created from the |
| 74 | # backup created by the previous stack |
Steve Baker | 50d3e8c | 2014-10-21 11:08:50 +1300 | [diff] [blame] | 75 | try: |
Sergey Kraynev | ef9d842 | 2015-02-13 03:41:46 -0500 | [diff] [blame] | 76 | stack_identifier2 = self.launch_stack( |
Steve Baker | 50d3e8c | 2014-10-21 11:08:50 +1300 | [diff] [blame] | 77 | template_name='test_volumes_create_from_backup.yaml', |
| 78 | add_parameters={'backup_id': backup.id}) |
Sergey Kraynev | ef9d842 | 2015-02-13 03:41:46 -0500 | [diff] [blame] | 79 | stack2 = self.client.stacks.get(stack_identifier2) |
Steve Baker | 50d3e8c | 2014-10-21 11:08:50 +1300 | [diff] [blame] | 80 | except exceptions.StackBuildErrorException as e: |
| 81 | LOG.error("Halting test due to bug: #1382300") |
| 82 | LOG.exception(e) |
| 83 | return |
Steve Baker | a5bd912 | 2014-08-11 14:39:00 +1200 | [diff] [blame] | 84 | |
| 85 | # Verify with cinder that the volume exists, with matching details |
| 86 | volume_id2 = self._stack_output(stack2, 'volume_id') |
| 87 | self._cinder_verify(volume_id2, expected_status='in-use') |
| 88 | |
| 89 | # Verify the stack outputs are as expected |
| 90 | self._outputs_verify(stack2, expected_status='in-use') |
| 91 | testfile_data = self._stack_output(stack2, 'testfile_data') |
| 92 | self.assertEqual('{"instance1": "Volume Data:ateststring"}', |
| 93 | testfile_data) |
| 94 | |
| 95 | # Delete the stack and ensure the volume is gone |
Sergey Kraynev | bf67ce3 | 2015-04-17 10:54:20 -0400 | [diff] [blame] | 96 | self._stack_delete(stack_identifier2) |
Steve Baker | a5bd912 | 2014-08-11 14:39:00 +1200 | [diff] [blame] | 97 | self.assertRaises(cinder_exceptions.NotFound, |
| 98 | self.volume_client.volumes.get, |
| 99 | volume_id2) |
Anastasia Kuznetsova | e45bfff | 2015-02-25 12:50:34 +0400 | [diff] [blame] | 100 | |
Anastasia Kuznetsova | e45bfff | 2015-02-25 12:50:34 +0400 | [diff] [blame] | 101 | def test_cinder_volume_create_backup_restore(self): |
Peter Razumovsky | f0ac958 | 2015-09-24 16:49:03 +0300 | [diff] [blame] | 102 | """Ensure the 'Snapshot' deletion policy works. |
Anastasia Kuznetsova | e45bfff | 2015-02-25 12:50:34 +0400 | [diff] [blame] | 103 | |
| 104 | This requires a more complex test, but it tests several aspects |
| 105 | of the heat cinder resources: |
| 106 | 1. Create a volume, attach it to an instance, write some data to it |
| 107 | 2. Delete the stack, with 'Snapshot' specified, creates a backup |
| 108 | 3. Check the snapshot has created a volume backup |
| 109 | 4. Create a new stack, where the volume is created from the backup |
| 110 | 5. Verify the test data written in (1) is present in the new volume |
| 111 | """ |
| 112 | parameters = { |
| 113 | 'key_name': self.keypair_name, |
Pavlo Shchelokovskyy | 46e5cb2 | 2015-03-23 12:01:25 +0000 | [diff] [blame] | 114 | 'instance_type': self.conf.minimal_instance_type, |
Anastasia Kuznetsova | e45bfff | 2015-02-25 12:50:34 +0400 | [diff] [blame] | 115 | 'image_id': self.conf.minimal_image_ref, |
| 116 | 'volume_description': self.volume_description, |
| 117 | 'timeout': self.conf.build_timeout, |
| 118 | 'network': self.net['id'] |
| 119 | } |
| 120 | |
| 121 | # Launch stack |
| 122 | stack_id = self.launch_stack( |
| 123 | template_name='test_volumes_delete_snapshot.yaml', |
| 124 | parameters=parameters, |
| 125 | add_parameters={'volume_size': self.volume_size} |
| 126 | ) |
| 127 | |
| 128 | # Check stack |
| 129 | self.check_stack(stack_id) |