blob: 47f4ad69270f0d9320696e8e8b909be0fc366e6d [file] [log] [blame]
Matt Riedemann2dac4662017-02-07 15:54:17 -05001# Copyright 2017 IBM Corp.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15import mock
16
17from oslo_utils import uuidutils
Matt Riedemann13954352017-02-07 14:03:54 -050018import six
Matt Riedemann2dac4662017-02-07 15:54:17 -050019
20from tempest.api.compute import base as compute_base
21from tempest.common import waiters
Matt Riedemann13954352017-02-07 14:03:54 -050022from tempest import exceptions
23from tempest.lib import exceptions as lib_exc
Matt Riedemann2dac4662017-02-07 15:54:17 -050024from tempest.tests import base
25
26
27class TestBaseV2ComputeTest(base.TestCase):
28 """Unit tests for utility functions in BaseV2ComputeTest."""
29
30 @mock.patch.multiple(compute_base.BaseV2ComputeTest,
31 compute_images_client=mock.DEFAULT,
32 images=[], create=True)
33 def test_create_image_from_server_no_wait(self, compute_images_client):
34 """Tests create_image_from_server without the wait_until kwarg."""
35 # setup mocks
36 image_id = uuidutils.generate_uuid()
37 fake_image = mock.Mock(response={'location': image_id})
38 compute_images_client.create_image.return_value = fake_image
39 # call the utility method
Andrea Frittolib17f7a32017-08-29 17:45:58 +010040 cleanup_path = 'tempest.test.BaseTestCase.addClassResourceCleanup'
41 with mock.patch(cleanup_path) as mock_cleanup:
42 image = compute_base.BaseV2ComputeTest.create_image_from_server(
43 mock.sentinel.server_id, name='fake-snapshot-name')
Matt Riedemann2dac4662017-02-07 15:54:17 -050044 self.assertEqual(fake_image, image)
45 # make our assertions
46 compute_images_client.create_image.assert_called_once_with(
47 mock.sentinel.server_id, name='fake-snapshot-name')
Andrea Frittolib17f7a32017-08-29 17:45:58 +010048 mock_cleanup.assert_called_once()
49 self.assertIn(image_id, mock_cleanup.call_args[0])
Matt Riedemann2dac4662017-02-07 15:54:17 -050050
51 @mock.patch.multiple(compute_base.BaseV2ComputeTest,
52 compute_images_client=mock.DEFAULT,
Bob Ball5fe62392017-02-20 09:51:00 +000053 servers_client=mock.DEFAULT,
Matt Riedemann2dac4662017-02-07 15:54:17 -050054 images=[], create=True)
55 @mock.patch.object(waiters, 'wait_for_image_status')
Bob Ball5fe62392017-02-20 09:51:00 +000056 @mock.patch.object(waiters, 'wait_for_server_status')
Matt Riedemann2dac4662017-02-07 15:54:17 -050057 def test_create_image_from_server_wait_until_active(self,
Bob Ball5fe62392017-02-20 09:51:00 +000058 wait_for_server_status,
Matt Riedemann2dac4662017-02-07 15:54:17 -050059 wait_for_image_status,
Bob Ball5fe62392017-02-20 09:51:00 +000060 servers_client,
Matt Riedemann2dac4662017-02-07 15:54:17 -050061 compute_images_client):
62 """Tests create_image_from_server with wait_until='ACTIVE' kwarg."""
63 # setup mocks
64 image_id = uuidutils.generate_uuid()
65 fake_image = mock.Mock(response={'location': image_id})
66 compute_images_client.create_image.return_value = fake_image
67 compute_images_client.show_image.return_value = (
68 {'image': fake_image})
69 # call the utility method
70 image = compute_base.BaseV2ComputeTest.create_image_from_server(
71 mock.sentinel.server_id, wait_until='ACTIVE')
72 self.assertEqual(fake_image, image)
73 # make our assertions
74 wait_for_image_status.assert_called_once_with(
75 compute_images_client, image_id, 'ACTIVE')
Bob Ball5fe62392017-02-20 09:51:00 +000076 wait_for_server_status.assert_called_once_with(
77 servers_client, mock.sentinel.server_id, 'ACTIVE')
78 compute_images_client.show_image.assert_called_once_with(image_id)
79
80 @mock.patch.multiple(compute_base.BaseV2ComputeTest,
81 compute_images_client=mock.DEFAULT,
82 servers_client=mock.DEFAULT,
83 images=[], create=True)
84 @mock.patch.object(waiters, 'wait_for_image_status')
85 @mock.patch.object(waiters, 'wait_for_server_status')
86 def test_create_image_from_server_wait_until_active_no_server_wait(
87 self, wait_for_server_status, wait_for_image_status,
88 servers_client, compute_images_client):
89 """Tests create_image_from_server with wait_until='ACTIVE' kwarg."""
90 # setup mocks
91 image_id = uuidutils.generate_uuid()
92 fake_image = mock.Mock(response={'location': image_id})
93 compute_images_client.create_image.return_value = fake_image
94 compute_images_client.show_image.return_value = (
95 {'image': fake_image})
96 # call the utility method
97 image = compute_base.BaseV2ComputeTest.create_image_from_server(
98 mock.sentinel.server_id, wait_until='ACTIVE',
99 wait_for_server=False)
100 self.assertEqual(fake_image, image)
101 # make our assertions
102 wait_for_image_status.assert_called_once_with(
103 compute_images_client, image_id, 'ACTIVE')
104 self.assertEqual(0, wait_for_server_status.call_count)
Matt Riedemann2dac4662017-02-07 15:54:17 -0500105 compute_images_client.show_image.assert_called_once_with(image_id)
Matt Riedemann13954352017-02-07 14:03:54 -0500106
107 @mock.patch.multiple(compute_base.BaseV2ComputeTest,
108 compute_images_client=mock.DEFAULT,
109 servers_client=mock.DEFAULT,
110 images=[], create=True)
111 @mock.patch.object(waiters, 'wait_for_image_status',
112 side_effect=lib_exc.NotFound)
113 def _test_create_image_from_server_wait_until_active_not_found(
114 self, wait_for_image_status, compute_images_client,
115 servers_client, fault=None):
116 # setup mocks
117 image_id = uuidutils.generate_uuid()
118 fake_image = mock.Mock(response={'location': image_id})
119 compute_images_client.create_image.return_value = fake_image
120 fake_server = {'id': mock.sentinel.server_id}
121 if fault:
122 fake_server['fault'] = fault
123 servers_client.show_server.return_value = {'server': fake_server}
124 # call the utility method
125 ex = self.assertRaises(
126 exceptions.SnapshotNotFoundException,
127 compute_base.BaseV2ComputeTest.create_image_from_server,
128 mock.sentinel.server_id, wait_until='active')
129 # make our assertions
130 if fault:
131 self.assertIn(fault, six.text_type(ex))
132 else:
133 self.assertNotIn(fault, six.text_type(ex))
134 wait_for_image_status.assert_called_once_with(
135 compute_images_client, image_id, 'active')
136 servers_client.show_server.assert_called_once_with(
137 mock.sentinel.server_id)
138
139 def test_create_image_from_server_wait_until_active_not_found_no_fault(
140 self):
141 # Tests create_image_from_server with wait_until='active' kwarg and
142 # the a 404 is raised while waiting for the image status to change. In
143 # this test the server does not have a fault associated with it.
144 self._test_create_image_from_server_wait_until_active_not_found()
145
146 def test_create_image_from_server_wait_until_active_not_found_with_fault(
147 self):
148 # Tests create_image_from_server with wait_until='active' kwarg and
149 # the a 404 is raised while waiting for the image status to change. In
150 # this test the server has a fault associated with it.
151 self._test_create_image_from_server_wait_until_active_not_found(
152 fault='Lost connection to hypervisor!')
153
154 @mock.patch.multiple(compute_base.BaseV2ComputeTest,
155 compute_images_client=mock.DEFAULT,
156 images=[], create=True)
157 @mock.patch.object(waiters, 'wait_for_image_status',
158 side_effect=lib_exc.NotFound)
159 def test_create_image_from_server_wait_until_saving_not_found(
160 self, wait_for_image_status, compute_images_client):
161 # Tests create_image_from_server with wait_until='SAVING' kwarg and
162 # the a 404 is raised while waiting for the image status to change. In
163 # this case we do not get the server details and just re-raise the 404.
164 # setup mocks
165 image_id = uuidutils.generate_uuid()
166 fake_image = mock.Mock(response={'location': image_id})
167 compute_images_client.create_image.return_value = fake_image
168 # call the utility method
169 self.assertRaises(
170 lib_exc.NotFound,
171 compute_base.BaseV2ComputeTest.create_image_from_server,
172 mock.sentinel.server_id, wait_until='SAVING')
173 # make our assertions
174 wait_for_image_status.assert_called_once_with(
175 compute_images_client, image_id, 'SAVING')
ghanshyam66b9aed2018-03-30 08:11:10 +0000176
177 def _test_version_compatible(self, max_version, expected=True):
178 actual = (compute_base.BaseV2ComputeTest.
179 is_requested_microversion_compatible(max_version))
180 self.assertEqual(expected, actual)
181
182 def test_check_lower_version(self):
183 compute_base.BaseV2ComputeTest.request_microversion = '2.8'
184 self._test_version_compatible('2.40')
185
186 def test_check_euqal_version(self):
187 compute_base.BaseV2ComputeTest.request_microversion = '2.40'
188 self._test_version_compatible('2.40')
189
190 def test_check_higher_version(self):
191 compute_base.BaseV2ComputeTest.request_microversion = '2.41'
192 self._test_version_compatible('2.40', expected=False)