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 | |
| 13 | import logging |
Steve Baker | a5bd912 | 2014-08-11 14:39:00 +1200 | [diff] [blame] | 14 | |
| 15 | from cinderclient import exceptions as cinder_exceptions |
Pavlo Shchelokovskyy | 60e0ecd | 2014-12-14 22:17:21 +0200 | [diff] [blame] | 16 | import six |
| 17 | from testtools import testcase |
Steve Baker | a5bd912 | 2014-08-11 14:39:00 +1200 | [diff] [blame] | 18 | |
Steve Baker | 50d3e8c | 2014-10-21 11:08:50 +1300 | [diff] [blame] | 19 | from heat_integrationtests.common import exceptions |
Steve Baker | a5bd912 | 2014-08-11 14:39:00 +1200 | [diff] [blame] | 20 | from heat_integrationtests.common import test |
| 21 | |
| 22 | LOG = logging.getLogger(__name__) |
| 23 | |
| 24 | |
| 25 | class VolumeBackupRestoreIntegrationTest(test.HeatIntegrationTest): |
| 26 | |
| 27 | def setUp(self): |
| 28 | super(VolumeBackupRestoreIntegrationTest, self).setUp() |
| 29 | self.client = self.orchestration_client |
| 30 | if self.conf.keypair_name: |
| 31 | self.keypair_name = self.conf.keypair_name |
| 32 | else: |
| 33 | self.keypair = self.create_keypair() |
| 34 | self.keypair_name = self.keypair.id |
| 35 | self.volume_description = 'A test volume description 123' |
| 36 | self.volume_size = self.conf.volume_size |
| 37 | |
| 38 | def _cinder_verify(self, volume_id, expected_status='available'): |
| 39 | self.assertIsNotNone(volume_id) |
| 40 | volume = self.volume_client.volumes.get(volume_id) |
| 41 | self.assertIsNotNone(volume) |
| 42 | self.assertEqual(expected_status, volume.status) |
| 43 | self.assertEqual(self.volume_size, volume.size) |
| 44 | self.assertEqual(self.volume_description, |
| 45 | volume.display_description) |
| 46 | |
| 47 | def _outputs_verify(self, stack, expected_status='available'): |
| 48 | self.assertEqual(expected_status, |
| 49 | self._stack_output(stack, 'status')) |
| 50 | self.assertEqual(six.text_type(self.volume_size), |
| 51 | self._stack_output(stack, 'size')) |
| 52 | self.assertEqual(self.volume_description, |
| 53 | self._stack_output(stack, 'display_description')) |
| 54 | |
| 55 | def _create_stack(self, template_name, add_parameters={}): |
| 56 | # TODO(shardy): refactor this into a generic base-class helper |
| 57 | net = self._get_default_network() |
| 58 | stack_name = self._stack_rand_name() |
| 59 | template = self._load_template(__file__, template_name) |
| 60 | parameters = {'key_name': self.keypair_name, |
| 61 | 'instance_type': self.conf.instance_type, |
| 62 | 'image_id': self.conf.minimal_image_ref, |
| 63 | 'volume_description': self.volume_description, |
| 64 | 'timeout': self.conf.build_timeout, |
| 65 | 'network': net['id']} |
| 66 | parameters.update(add_parameters) |
| 67 | ret_stack = self.client.stacks.create( |
| 68 | stack_name=stack_name, |
| 69 | template=template, |
| 70 | parameters=parameters) |
| 71 | stack_id = ret_stack['stack']['id'] |
| 72 | stack = self.client.stacks.get(stack_id) |
| 73 | self.assertIsNotNone(stack) |
| 74 | stack_identifier = '%s/%s' % (stack_name, stack.id) |
| 75 | |
| 76 | self.addCleanup(self._stack_delete, stack_identifier) |
| 77 | self._wait_for_stack_status(stack_identifier, 'CREATE_COMPLETE') |
| 78 | return stack, stack_identifier |
| 79 | |
Peter Razumovsky | 986ff14 | 2014-11-25 19:11:31 +0300 | [diff] [blame] | 80 | @testcase.skip('Skipped until failure rate ' |
| 81 | 'can be reduced ref bug #1382300') |
Steve Baker | a5bd912 | 2014-08-11 14:39:00 +1200 | [diff] [blame] | 82 | def test_cinder_volume_create_backup_restore(self): |
| 83 | """Ensure the 'Snapshot' deletion policy works. |
| 84 | |
| 85 | This requires a more complex test, but it tests several aspects |
| 86 | of the heat cinder resources: |
| 87 | 1. Create a volume, attach it to an instance, write some data to it |
| 88 | 2. Delete the stack, with 'Snapshot' specified, creates a backup |
| 89 | 3. Check the snapshot has created a volume backup |
| 90 | 4. Create a new stack, where the volume is created from the backup |
| 91 | 5. Verify the test data written in (1) is present in the new volume |
| 92 | """ |
| 93 | stack, stack_identifier = self._create_stack( |
| 94 | template_name='test_volumes_delete_snapshot.yaml', |
| 95 | add_parameters={'volume_size': self.volume_size}) |
| 96 | |
| 97 | # Verify with cinder that the volume exists, with matching details |
| 98 | volume_id = self._stack_output(stack, 'volume_id') |
| 99 | self._cinder_verify(volume_id, expected_status='in-use') |
| 100 | |
| 101 | # Verify the stack outputs are as expected |
| 102 | self._outputs_verify(stack, expected_status='in-use') |
| 103 | |
| 104 | # Delete the stack and ensure a backup is created for volume_id |
| 105 | # but the volume itself is gone |
| 106 | self.client.stacks.delete(stack_identifier) |
| 107 | self._wait_for_stack_status(stack_identifier, 'DELETE_COMPLETE') |
| 108 | self.assertRaises(cinder_exceptions.NotFound, |
| 109 | self.volume_client.volumes.get, |
| 110 | volume_id) |
| 111 | |
| 112 | backups = self.volume_client.backups.list() |
| 113 | self.assertIsNotNone(backups) |
| 114 | backups_filtered = [b for b in backups if b.volume_id == volume_id] |
| 115 | self.assertEqual(1, len(backups_filtered)) |
| 116 | backup = backups_filtered[0] |
| 117 | self.addCleanup(self.volume_client.backups.delete, backup.id) |
| 118 | |
| 119 | # Now, we create another stack where the volume is created from the |
| 120 | # backup created by the previous stack |
Steve Baker | 50d3e8c | 2014-10-21 11:08:50 +1300 | [diff] [blame] | 121 | try: |
| 122 | stack2, stack_identifier2 = self._create_stack( |
| 123 | template_name='test_volumes_create_from_backup.yaml', |
| 124 | add_parameters={'backup_id': backup.id}) |
| 125 | except exceptions.StackBuildErrorException as e: |
| 126 | LOG.error("Halting test due to bug: #1382300") |
| 127 | LOG.exception(e) |
| 128 | return |
Steve Baker | a5bd912 | 2014-08-11 14:39:00 +1200 | [diff] [blame] | 129 | |
| 130 | # Verify with cinder that the volume exists, with matching details |
| 131 | volume_id2 = self._stack_output(stack2, 'volume_id') |
| 132 | self._cinder_verify(volume_id2, expected_status='in-use') |
| 133 | |
| 134 | # Verify the stack outputs are as expected |
| 135 | self._outputs_verify(stack2, expected_status='in-use') |
| 136 | testfile_data = self._stack_output(stack2, 'testfile_data') |
| 137 | self.assertEqual('{"instance1": "Volume Data:ateststring"}', |
| 138 | testfile_data) |
| 139 | |
| 140 | # Delete the stack and ensure the volume is gone |
| 141 | self.client.stacks.delete(stack_identifier2) |
| 142 | self._wait_for_stack_status(stack_identifier2, 'DELETE_COMPLETE') |
| 143 | self.assertRaises(cinder_exceptions.NotFound, |
| 144 | self.volume_client.volumes.get, |
| 145 | volume_id2) |