blob: 74d2625d547ef43449b3af6aa9813a61ed35a03b [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
Sean McGinniseed80742020-04-18 12:01:03 -050015from unittest import mock
Matt Riedemann2dac4662017-02-07 15:54:17 -050016
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))
zhufl66275c22018-03-28 15:32:14 +0800134 if compute_base.BaseV2ComputeTest.is_requested_microversion_compatible(
135 '2.35'):
136 status = 'ACTIVE'
137 else:
138 status = 'active'
Matt Riedemann13954352017-02-07 14:03:54 -0500139 wait_for_image_status.assert_called_once_with(
zhufl66275c22018-03-28 15:32:14 +0800140 compute_images_client, image_id, status)
Matt Riedemann13954352017-02-07 14:03:54 -0500141 servers_client.show_server.assert_called_once_with(
142 mock.sentinel.server_id)
143
144 def test_create_image_from_server_wait_until_active_not_found_no_fault(
145 self):
146 # Tests create_image_from_server with wait_until='active' kwarg and
147 # the a 404 is raised while waiting for the image status to change. In
148 # this test the server does not have a fault associated with it.
149 self._test_create_image_from_server_wait_until_active_not_found()
150
151 def test_create_image_from_server_wait_until_active_not_found_with_fault(
152 self):
153 # Tests create_image_from_server with wait_until='active' kwarg and
154 # the a 404 is raised while waiting for the image status to change. In
155 # this test the server has a fault associated with it.
156 self._test_create_image_from_server_wait_until_active_not_found(
157 fault='Lost connection to hypervisor!')
158
159 @mock.patch.multiple(compute_base.BaseV2ComputeTest,
160 compute_images_client=mock.DEFAULT,
161 images=[], create=True)
162 @mock.patch.object(waiters, 'wait_for_image_status',
163 side_effect=lib_exc.NotFound)
164 def test_create_image_from_server_wait_until_saving_not_found(
165 self, wait_for_image_status, compute_images_client):
166 # Tests create_image_from_server with wait_until='SAVING' kwarg and
167 # the a 404 is raised while waiting for the image status to change. In
168 # this case we do not get the server details and just re-raise the 404.
169 # setup mocks
170 image_id = uuidutils.generate_uuid()
171 fake_image = mock.Mock(response={'location': image_id})
172 compute_images_client.create_image.return_value = fake_image
173 # call the utility method
174 self.assertRaises(
175 lib_exc.NotFound,
176 compute_base.BaseV2ComputeTest.create_image_from_server,
177 mock.sentinel.server_id, wait_until='SAVING')
178 # make our assertions
179 wait_for_image_status.assert_called_once_with(
180 compute_images_client, image_id, 'SAVING')
ghanshyam66b9aed2018-03-30 08:11:10 +0000181
182 def _test_version_compatible(self, max_version, expected=True):
183 actual = (compute_base.BaseV2ComputeTest.
184 is_requested_microversion_compatible(max_version))
185 self.assertEqual(expected, actual)
186
187 def test_check_lower_version(self):
188 compute_base.BaseV2ComputeTest.request_microversion = '2.8'
189 self._test_version_compatible('2.40')
190
191 def test_check_euqal_version(self):
192 compute_base.BaseV2ComputeTest.request_microversion = '2.40'
193 self._test_version_compatible('2.40')
194
195 def test_check_higher_version(self):
196 compute_base.BaseV2ComputeTest.request_microversion = '2.41'
197 self._test_version_compatible('2.40', expected=False)