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