blob: 79d49314a67331c459f76925c7d180b78bce4edd [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
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
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040020from heat_integrationtests.scenario import scenario_base
Steve Bakera5bd9122014-08-11 14:39:00 +120021
22LOG = logging.getLogger(__name__)
23
24
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040025class VolumeBackupRestoreIntegrationTest(scenario_base.ScenarioTestsBase):
26 """
27 Class is responsible for testing of volume backup.
28 """
Steve Bakera5bd9122014-08-11 14:39:00 +120029
30 def setUp(self):
31 super(VolumeBackupRestoreIntegrationTest, self).setUp()
Steve Bakera5bd9122014-08-11 14:39:00 +120032 self.volume_description = 'A test volume description 123'
33 self.volume_size = self.conf.volume_size
34
35 def _cinder_verify(self, volume_id, expected_status='available'):
36 self.assertIsNotNone(volume_id)
37 volume = self.volume_client.volumes.get(volume_id)
38 self.assertIsNotNone(volume)
39 self.assertEqual(expected_status, volume.status)
40 self.assertEqual(self.volume_size, volume.size)
41 self.assertEqual(self.volume_description,
42 volume.display_description)
43
44 def _outputs_verify(self, stack, expected_status='available'):
45 self.assertEqual(expected_status,
46 self._stack_output(stack, 'status'))
47 self.assertEqual(six.text_type(self.volume_size),
48 self._stack_output(stack, 'size'))
49 self.assertEqual(self.volume_description,
50 self._stack_output(stack, 'display_description'))
51
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040052 def check_stack(self, stack_id):
53 stack = self.client.stacks.get(stack_id)
Sergey Kraynevef9d8422015-02-13 03:41:46 -050054
Steve Bakera5bd9122014-08-11 14:39:00 +120055 # Verify with cinder that the volume exists, with matching details
56 volume_id = self._stack_output(stack, 'volume_id')
57 self._cinder_verify(volume_id, expected_status='in-use')
58
59 # Verify the stack outputs are as expected
60 self._outputs_verify(stack, expected_status='in-use')
61
62 # Delete the stack and ensure a backup is created for volume_id
63 # but the volume itself is gone
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040064 self.client.stacks.delete(stack_id)
65 self._wait_for_stack_status(stack_id, 'DELETE_COMPLETE')
Steve Bakera5bd9122014-08-11 14:39:00 +120066 self.assertRaises(cinder_exceptions.NotFound,
67 self.volume_client.volumes.get,
68 volume_id)
69
70 backups = self.volume_client.backups.list()
71 self.assertIsNotNone(backups)
72 backups_filtered = [b for b in backups if b.volume_id == volume_id]
73 self.assertEqual(1, len(backups_filtered))
74 backup = backups_filtered[0]
75 self.addCleanup(self.volume_client.backups.delete, backup.id)
76
77 # Now, we create another stack where the volume is created from the
78 # backup created by the previous stack
Steve Baker50d3e8c2014-10-21 11:08:50 +130079 try:
Sergey Kraynevef9d8422015-02-13 03:41:46 -050080 stack_identifier2 = self.launch_stack(
Steve Baker50d3e8c2014-10-21 11:08:50 +130081 template_name='test_volumes_create_from_backup.yaml',
82 add_parameters={'backup_id': backup.id})
Sergey Kraynevef9d8422015-02-13 03:41:46 -050083 stack2 = self.client.stacks.get(stack_identifier2)
Steve Baker50d3e8c2014-10-21 11:08:50 +130084 except exceptions.StackBuildErrorException as e:
85 LOG.error("Halting test due to bug: #1382300")
86 LOG.exception(e)
87 return
Steve Bakera5bd9122014-08-11 14:39:00 +120088
89 # Verify with cinder that the volume exists, with matching details
90 volume_id2 = self._stack_output(stack2, 'volume_id')
91 self._cinder_verify(volume_id2, expected_status='in-use')
92
93 # Verify the stack outputs are as expected
94 self._outputs_verify(stack2, expected_status='in-use')
95 testfile_data = self._stack_output(stack2, 'testfile_data')
96 self.assertEqual('{"instance1": "Volume Data:ateststring"}',
97 testfile_data)
98
99 # Delete the stack and ensure the volume is gone
100 self.client.stacks.delete(stack_identifier2)
101 self._wait_for_stack_status(stack_identifier2, 'DELETE_COMPLETE')
102 self.assertRaises(cinder_exceptions.NotFound,
103 self.volume_client.volumes.get,
104 volume_id2)
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +0400105
106 @testcase.skip('Skipped until failure rate '
107 'can be reduced ref bug #1382300')
108 def test_cinder_volume_create_backup_restore(self):
109 """
110 Ensure the 'Snapshot' deletion policy works.
111
112 This requires a more complex test, but it tests several aspects
113 of the heat cinder resources:
114 1. Create a volume, attach it to an instance, write some data to it
115 2. Delete the stack, with 'Snapshot' specified, creates a backup
116 3. Check the snapshot has created a volume backup
117 4. Create a new stack, where the volume is created from the backup
118 5. Verify the test data written in (1) is present in the new volume
119 """
120 parameters = {
121 'key_name': self.keypair_name,
Pavlo Shchelokovskyy46e5cb22015-03-23 12:01:25 +0000122 'instance_type': self.conf.minimal_instance_type,
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +0400123 'image_id': self.conf.minimal_image_ref,
124 'volume_description': self.volume_description,
125 'timeout': self.conf.build_timeout,
126 'network': self.net['id']
127 }
128
129 # Launch stack
130 stack_id = self.launch_stack(
131 template_name='test_volumes_delete_snapshot.yaml',
132 parameters=parameters,
133 add_parameters={'volume_size': self.volume_size}
134 )
135
136 # Check stack
137 self.check_stack(stack_id)