blob: d4b1aa864aca7e2f046e9493bf8f5e1224de1e13 [file] [log] [blame]
Steve Bakera5bd9122014-08-11 14:39:00 +12001# 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 Bakera5bd9122014-08-11 14:39:00 +120013
14from cinderclient import exceptions as cinder_exceptions
Steve Baker24641292015-03-13 10:47:50 +130015from oslo_log import log as logging
Pavlo Shchelokovskyy60e0ecd2014-12-14 22:17:21 +020016import six
Steve Bakera5bd9122014-08-11 14:39:00 +120017
Steve Baker50d3e8c2014-10-21 11:08:50 +130018from heat_integrationtests.common import exceptions
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040019from heat_integrationtests.scenario import scenario_base
Steve Bakera5bd9122014-08-11 14:39:00 +120020
21LOG = logging.getLogger(__name__)
22
23
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040024class VolumeBackupRestoreIntegrationTest(scenario_base.ScenarioTestsBase):
25 """
26 Class is responsible for testing of volume backup.
27 """
Steve Bakera5bd9122014-08-11 14:39:00 +120028
29 def setUp(self):
30 super(VolumeBackupRestoreIntegrationTest, self).setUp()
Steve Bakera5bd9122014-08-11 14:39:00 +120031 self.volume_description = 'A test volume description 123'
32 self.volume_size = self.conf.volume_size
33
34 def _cinder_verify(self, volume_id, expected_status='available'):
35 self.assertIsNotNone(volume_id)
36 volume = self.volume_client.volumes.get(volume_id)
37 self.assertIsNotNone(volume)
38 self.assertEqual(expected_status, volume.status)
39 self.assertEqual(self.volume_size, volume.size)
40 self.assertEqual(self.volume_description,
41 volume.display_description)
42
43 def _outputs_verify(self, stack, expected_status='available'):
44 self.assertEqual(expected_status,
45 self._stack_output(stack, 'status'))
46 self.assertEqual(six.text_type(self.volume_size),
47 self._stack_output(stack, 'size'))
48 self.assertEqual(self.volume_description,
49 self._stack_output(stack, 'display_description'))
50
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040051 def check_stack(self, stack_id):
52 stack = self.client.stacks.get(stack_id)
Sergey Kraynevef9d8422015-02-13 03:41:46 -050053
Steve Bakera5bd9122014-08-11 14:39:00 +120054 # Verify with cinder that the volume exists, with matching details
55 volume_id = self._stack_output(stack, 'volume_id')
56 self._cinder_verify(volume_id, expected_status='in-use')
57
58 # Verify the stack outputs are as expected
59 self._outputs_verify(stack, expected_status='in-use')
60
61 # Delete the stack and ensure a backup is created for volume_id
62 # but the volume itself is gone
Sergey Kraynevbf67ce32015-04-17 10:54:20 -040063 self._stack_delete(stack_id)
Steve Bakera5bd9122014-08-11 14:39:00 +120064 self.assertRaises(cinder_exceptions.NotFound,
65 self.volume_client.volumes.get,
66 volume_id)
67
68 backups = self.volume_client.backups.list()
69 self.assertIsNotNone(backups)
70 backups_filtered = [b for b in backups if b.volume_id == volume_id]
71 self.assertEqual(1, len(backups_filtered))
72 backup = backups_filtered[0]
73 self.addCleanup(self.volume_client.backups.delete, backup.id)
74
75 # Now, we create another stack where the volume is created from the
76 # backup created by the previous stack
Steve Baker50d3e8c2014-10-21 11:08:50 +130077 try:
Sergey Kraynevef9d8422015-02-13 03:41:46 -050078 stack_identifier2 = self.launch_stack(
Steve Baker50d3e8c2014-10-21 11:08:50 +130079 template_name='test_volumes_create_from_backup.yaml',
80 add_parameters={'backup_id': backup.id})
Sergey Kraynevef9d8422015-02-13 03:41:46 -050081 stack2 = self.client.stacks.get(stack_identifier2)
Steve Baker50d3e8c2014-10-21 11:08:50 +130082 except exceptions.StackBuildErrorException as e:
83 LOG.error("Halting test due to bug: #1382300")
84 LOG.exception(e)
85 return
Steve Bakera5bd9122014-08-11 14:39:00 +120086
87 # Verify with cinder that the volume exists, with matching details
88 volume_id2 = self._stack_output(stack2, 'volume_id')
89 self._cinder_verify(volume_id2, expected_status='in-use')
90
91 # Verify the stack outputs are as expected
92 self._outputs_verify(stack2, expected_status='in-use')
93 testfile_data = self._stack_output(stack2, 'testfile_data')
94 self.assertEqual('{"instance1": "Volume Data:ateststring"}',
95 testfile_data)
96
97 # Delete the stack and ensure the volume is gone
Sergey Kraynevbf67ce32015-04-17 10:54:20 -040098 self._stack_delete(stack_identifier2)
Steve Bakera5bd9122014-08-11 14:39:00 +120099 self.assertRaises(cinder_exceptions.NotFound,
100 self.volume_client.volumes.get,
101 volume_id2)
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +0400102
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +0400103 def test_cinder_volume_create_backup_restore(self):
104 """
105 Ensure the 'Snapshot' deletion policy works.
106
107 This requires a more complex test, but it tests several aspects
108 of the heat cinder resources:
109 1. Create a volume, attach it to an instance, write some data to it
110 2. Delete the stack, with 'Snapshot' specified, creates a backup
111 3. Check the snapshot has created a volume backup
112 4. Create a new stack, where the volume is created from the backup
113 5. Verify the test data written in (1) is present in the new volume
114 """
115 parameters = {
116 'key_name': self.keypair_name,
Pavlo Shchelokovskyy46e5cb22015-03-23 12:01:25 +0000117 'instance_type': self.conf.minimal_instance_type,
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +0400118 'image_id': self.conf.minimal_image_ref,
119 'volume_description': self.volume_description,
120 'timeout': self.conf.build_timeout,
121 'network': self.net['id']
122 }
123
124 # Launch stack
125 stack_id = self.launch_stack(
126 template_name='test_volumes_delete_snapshot.yaml',
127 parameters=parameters,
128 add_parameters={'volume_size': self.volume_size}
129 )
130
131 # Check stack
132 self.check_stack(stack_id)