blob: 6a802975ee2664f5693ace699aae1eddcd9cc974 [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
Steve Bakera5bd9122014-08-11 14:39:00 +120014
15from cinderclient import exceptions as cinder_exceptions
Pavlo Shchelokovskyy60e0ecd2014-12-14 22:17:21 +020016import six
17from testtools import testcase
Steve Bakera5bd9122014-08-11 14:39:00 +120018
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
Sergey Kraynevef9d8422015-02-13 03:41:46 -050055 def launch_stack(self, template_name, add_parameters={}):
Steve Bakera5bd9122014-08-11 14:39:00 +120056 net = self._get_default_network()
Sergey Kraynevd6fa5c02015-02-13 03:03:55 -050057 template = self._load_template(__file__, template_name, 'templates')
Steve Bakera5bd9122014-08-11 14:39:00 +120058 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)
Sergey Kraynevef9d8422015-02-13 03:41:46 -050065 return self.stack_create(template=template,
66 parameters=parameters)
Steve Bakera5bd9122014-08-11 14:39:00 +120067
Peter Razumovsky986ff142014-11-25 19:11:31 +030068 @testcase.skip('Skipped until failure rate '
69 'can be reduced ref bug #1382300')
Steve Bakera5bd9122014-08-11 14:39:00 +120070 def test_cinder_volume_create_backup_restore(self):
71 """Ensure the 'Snapshot' deletion policy works.
72
73 This requires a more complex test, but it tests several aspects
74 of the heat cinder resources:
75 1. Create a volume, attach it to an instance, write some data to it
76 2. Delete the stack, with 'Snapshot' specified, creates a backup
77 3. Check the snapshot has created a volume backup
78 4. Create a new stack, where the volume is created from the backup
79 5. Verify the test data written in (1) is present in the new volume
80 """
Sergey Kraynevef9d8422015-02-13 03:41:46 -050081 stack_identifier = self.launch_stack(
Steve Bakera5bd9122014-08-11 14:39:00 +120082 template_name='test_volumes_delete_snapshot.yaml',
83 add_parameters={'volume_size': self.volume_size})
84
Sergey Kraynevef9d8422015-02-13 03:41:46 -050085 stack = self.client.stacks.get(stack_identifier)
86
Steve Bakera5bd9122014-08-11 14:39:00 +120087 # Verify with cinder that the volume exists, with matching details
88 volume_id = self._stack_output(stack, 'volume_id')
89 self._cinder_verify(volume_id, expected_status='in-use')
90
91 # Verify the stack outputs are as expected
92 self._outputs_verify(stack, expected_status='in-use')
93
94 # Delete the stack and ensure a backup is created for volume_id
95 # but the volume itself is gone
96 self.client.stacks.delete(stack_identifier)
97 self._wait_for_stack_status(stack_identifier, 'DELETE_COMPLETE')
98 self.assertRaises(cinder_exceptions.NotFound,
99 self.volume_client.volumes.get,
100 volume_id)
101
102 backups = self.volume_client.backups.list()
103 self.assertIsNotNone(backups)
104 backups_filtered = [b for b in backups if b.volume_id == volume_id]
105 self.assertEqual(1, len(backups_filtered))
106 backup = backups_filtered[0]
107 self.addCleanup(self.volume_client.backups.delete, backup.id)
108
109 # Now, we create another stack where the volume is created from the
110 # backup created by the previous stack
Steve Baker50d3e8c2014-10-21 11:08:50 +1300111 try:
Sergey Kraynevef9d8422015-02-13 03:41:46 -0500112 stack_identifier2 = self.launch_stack(
Steve Baker50d3e8c2014-10-21 11:08:50 +1300113 template_name='test_volumes_create_from_backup.yaml',
114 add_parameters={'backup_id': backup.id})
Sergey Kraynevef9d8422015-02-13 03:41:46 -0500115 stack2 = self.client.stacks.get(stack_identifier2)
Steve Baker50d3e8c2014-10-21 11:08:50 +1300116 except exceptions.StackBuildErrorException as e:
117 LOG.error("Halting test due to bug: #1382300")
118 LOG.exception(e)
119 return
Steve Bakera5bd9122014-08-11 14:39:00 +1200120
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)