blob: a654f861e1c10be4a2b6c0ef81947e6bb4e1f892 [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
13import logging
14import six
Steven Hardy6ef6c342014-11-18 16:36:47 +000015from testtools.testcase import skip
Steve Bakera5bd9122014-08-11 14:39:00 +120016
17from cinderclient import exceptions as cinder_exceptions
18
Steve Baker50d3e8c2014-10-21 11:08:50 +130019from heat_integrationtests.common import exceptions
Steve Bakera5bd9122014-08-11 14:39:00 +120020from heat_integrationtests.common import test
21
22LOG = logging.getLogger(__name__)
23
24
25class 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
Steven Hardy6ef6c342014-11-18 16:36:47 +000080 @skip('Skipped until failure rate can be reduced ref bug #1382300')
Steve Bakera5bd9122014-08-11 14:39:00 +120081 def test_cinder_volume_create_backup_restore(self):
82 """Ensure the 'Snapshot' deletion policy works.
83
84 This requires a more complex test, but it tests several aspects
85 of the heat cinder resources:
86 1. Create a volume, attach it to an instance, write some data to it
87 2. Delete the stack, with 'Snapshot' specified, creates a backup
88 3. Check the snapshot has created a volume backup
89 4. Create a new stack, where the volume is created from the backup
90 5. Verify the test data written in (1) is present in the new volume
91 """
92 stack, stack_identifier = self._create_stack(
93 template_name='test_volumes_delete_snapshot.yaml',
94 add_parameters={'volume_size': self.volume_size})
95
96 # Verify with cinder that the volume exists, with matching details
97 volume_id = self._stack_output(stack, 'volume_id')
98 self._cinder_verify(volume_id, expected_status='in-use')
99
100 # Verify the stack outputs are as expected
101 self._outputs_verify(stack, expected_status='in-use')
102
103 # Delete the stack and ensure a backup is created for volume_id
104 # but the volume itself is gone
105 self.client.stacks.delete(stack_identifier)
106 self._wait_for_stack_status(stack_identifier, 'DELETE_COMPLETE')
107 self.assertRaises(cinder_exceptions.NotFound,
108 self.volume_client.volumes.get,
109 volume_id)
110
111 backups = self.volume_client.backups.list()
112 self.assertIsNotNone(backups)
113 backups_filtered = [b for b in backups if b.volume_id == volume_id]
114 self.assertEqual(1, len(backups_filtered))
115 backup = backups_filtered[0]
116 self.addCleanup(self.volume_client.backups.delete, backup.id)
117
118 # Now, we create another stack where the volume is created from the
119 # backup created by the previous stack
Steve Baker50d3e8c2014-10-21 11:08:50 +1300120 try:
121 stack2, stack_identifier2 = self._create_stack(
122 template_name='test_volumes_create_from_backup.yaml',
123 add_parameters={'backup_id': backup.id})
124 except exceptions.StackBuildErrorException as e:
125 LOG.error("Halting test due to bug: #1382300")
126 LOG.exception(e)
127 return
Steve Bakera5bd9122014-08-11 14:39:00 +1200128
129 # Verify with cinder that the volume exists, with matching details
130 volume_id2 = self._stack_output(stack2, 'volume_id')
131 self._cinder_verify(volume_id2, expected_status='in-use')
132
133 # Verify the stack outputs are as expected
134 self._outputs_verify(stack2, expected_status='in-use')
135 testfile_data = self._stack_output(stack2, 'testfile_data')
136 self.assertEqual('{"instance1": "Volume Data:ateststring"}',
137 testfile_data)
138
139 # Delete the stack and ensure the volume is gone
140 self.client.stacks.delete(stack_identifier2)
141 self._wait_for_stack_status(stack_identifier2, 'DELETE_COMPLETE')
142 self.assertRaises(cinder_exceptions.NotFound,
143 self.volume_client.volumes.get,
144 volume_id2)