Matt Riedemann | 2dac466 | 2017-02-07 15:54:17 -0500 | [diff] [blame] | 1 | # 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 | |
| 15 | import mock |
| 16 | |
| 17 | from oslo_utils import uuidutils |
Matt Riedemann | 1395435 | 2017-02-07 14:03:54 -0500 | [diff] [blame] | 18 | import six |
Matt Riedemann | 2dac466 | 2017-02-07 15:54:17 -0500 | [diff] [blame] | 19 | |
| 20 | from tempest.api.compute import base as compute_base |
| 21 | from tempest.common import waiters |
Matt Riedemann | 1395435 | 2017-02-07 14:03:54 -0500 | [diff] [blame] | 22 | from tempest import exceptions |
| 23 | from tempest.lib import exceptions as lib_exc |
Matt Riedemann | 2dac466 | 2017-02-07 15:54:17 -0500 | [diff] [blame] | 24 | from tempest.tests import base |
| 25 | |
| 26 | |
| 27 | class 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 |
| 40 | image = compute_base.BaseV2ComputeTest.create_image_from_server( |
| 41 | mock.sentinel.server_id, name='fake-snapshot-name') |
| 42 | self.assertEqual(fake_image, image) |
| 43 | # make our assertions |
| 44 | compute_images_client.create_image.assert_called_once_with( |
| 45 | mock.sentinel.server_id, name='fake-snapshot-name') |
| 46 | self.assertEqual(1, len(compute_base.BaseV2ComputeTest.images)) |
| 47 | self.assertEqual(image_id, compute_base.BaseV2ComputeTest.images[0]) |
| 48 | |
| 49 | @mock.patch.multiple(compute_base.BaseV2ComputeTest, |
| 50 | compute_images_client=mock.DEFAULT, |
Bob Ball | 5fe6239 | 2017-02-20 09:51:00 +0000 | [diff] [blame] | 51 | servers_client=mock.DEFAULT, |
Matt Riedemann | 2dac466 | 2017-02-07 15:54:17 -0500 | [diff] [blame] | 52 | images=[], create=True) |
| 53 | @mock.patch.object(waiters, 'wait_for_image_status') |
Bob Ball | 5fe6239 | 2017-02-20 09:51:00 +0000 | [diff] [blame] | 54 | @mock.patch.object(waiters, 'wait_for_server_status') |
Matt Riedemann | 2dac466 | 2017-02-07 15:54:17 -0500 | [diff] [blame] | 55 | def test_create_image_from_server_wait_until_active(self, |
Bob Ball | 5fe6239 | 2017-02-20 09:51:00 +0000 | [diff] [blame] | 56 | wait_for_server_status, |
Matt Riedemann | 2dac466 | 2017-02-07 15:54:17 -0500 | [diff] [blame] | 57 | wait_for_image_status, |
Bob Ball | 5fe6239 | 2017-02-20 09:51:00 +0000 | [diff] [blame] | 58 | servers_client, |
Matt Riedemann | 2dac466 | 2017-02-07 15:54:17 -0500 | [diff] [blame] | 59 | compute_images_client): |
| 60 | """Tests create_image_from_server with wait_until='ACTIVE' kwarg.""" |
| 61 | # setup mocks |
| 62 | image_id = uuidutils.generate_uuid() |
| 63 | fake_image = mock.Mock(response={'location': image_id}) |
| 64 | compute_images_client.create_image.return_value = fake_image |
| 65 | compute_images_client.show_image.return_value = ( |
| 66 | {'image': fake_image}) |
| 67 | # call the utility method |
| 68 | image = compute_base.BaseV2ComputeTest.create_image_from_server( |
| 69 | mock.sentinel.server_id, wait_until='ACTIVE') |
| 70 | self.assertEqual(fake_image, image) |
| 71 | # make our assertions |
| 72 | wait_for_image_status.assert_called_once_with( |
| 73 | compute_images_client, image_id, 'ACTIVE') |
Bob Ball | 5fe6239 | 2017-02-20 09:51:00 +0000 | [diff] [blame] | 74 | wait_for_server_status.assert_called_once_with( |
| 75 | servers_client, mock.sentinel.server_id, 'ACTIVE') |
| 76 | compute_images_client.show_image.assert_called_once_with(image_id) |
| 77 | |
| 78 | @mock.patch.multiple(compute_base.BaseV2ComputeTest, |
| 79 | compute_images_client=mock.DEFAULT, |
| 80 | servers_client=mock.DEFAULT, |
| 81 | images=[], create=True) |
| 82 | @mock.patch.object(waiters, 'wait_for_image_status') |
| 83 | @mock.patch.object(waiters, 'wait_for_server_status') |
| 84 | def test_create_image_from_server_wait_until_active_no_server_wait( |
| 85 | self, wait_for_server_status, wait_for_image_status, |
| 86 | servers_client, compute_images_client): |
| 87 | """Tests create_image_from_server with wait_until='ACTIVE' kwarg.""" |
| 88 | # setup mocks |
| 89 | image_id = uuidutils.generate_uuid() |
| 90 | fake_image = mock.Mock(response={'location': image_id}) |
| 91 | compute_images_client.create_image.return_value = fake_image |
| 92 | compute_images_client.show_image.return_value = ( |
| 93 | {'image': fake_image}) |
| 94 | # call the utility method |
| 95 | image = compute_base.BaseV2ComputeTest.create_image_from_server( |
| 96 | mock.sentinel.server_id, wait_until='ACTIVE', |
| 97 | wait_for_server=False) |
| 98 | self.assertEqual(fake_image, image) |
| 99 | # make our assertions |
| 100 | wait_for_image_status.assert_called_once_with( |
| 101 | compute_images_client, image_id, 'ACTIVE') |
| 102 | self.assertEqual(0, wait_for_server_status.call_count) |
Matt Riedemann | 2dac466 | 2017-02-07 15:54:17 -0500 | [diff] [blame] | 103 | compute_images_client.show_image.assert_called_once_with(image_id) |
Matt Riedemann | 1395435 | 2017-02-07 14:03:54 -0500 | [diff] [blame] | 104 | |
| 105 | @mock.patch.multiple(compute_base.BaseV2ComputeTest, |
| 106 | compute_images_client=mock.DEFAULT, |
| 107 | servers_client=mock.DEFAULT, |
| 108 | images=[], create=True) |
| 109 | @mock.patch.object(waiters, 'wait_for_image_status', |
| 110 | side_effect=lib_exc.NotFound) |
| 111 | def _test_create_image_from_server_wait_until_active_not_found( |
| 112 | self, wait_for_image_status, compute_images_client, |
| 113 | servers_client, fault=None): |
| 114 | # setup mocks |
| 115 | image_id = uuidutils.generate_uuid() |
| 116 | fake_image = mock.Mock(response={'location': image_id}) |
| 117 | compute_images_client.create_image.return_value = fake_image |
| 118 | fake_server = {'id': mock.sentinel.server_id} |
| 119 | if fault: |
| 120 | fake_server['fault'] = fault |
| 121 | servers_client.show_server.return_value = {'server': fake_server} |
| 122 | # call the utility method |
| 123 | ex = self.assertRaises( |
| 124 | exceptions.SnapshotNotFoundException, |
| 125 | compute_base.BaseV2ComputeTest.create_image_from_server, |
| 126 | mock.sentinel.server_id, wait_until='active') |
| 127 | # make our assertions |
| 128 | if fault: |
| 129 | self.assertIn(fault, six.text_type(ex)) |
| 130 | else: |
| 131 | self.assertNotIn(fault, six.text_type(ex)) |
| 132 | wait_for_image_status.assert_called_once_with( |
| 133 | compute_images_client, image_id, 'active') |
| 134 | servers_client.show_server.assert_called_once_with( |
| 135 | mock.sentinel.server_id) |
| 136 | |
| 137 | def test_create_image_from_server_wait_until_active_not_found_no_fault( |
| 138 | self): |
| 139 | # Tests create_image_from_server with wait_until='active' kwarg and |
| 140 | # the a 404 is raised while waiting for the image status to change. In |
| 141 | # this test the server does not have a fault associated with it. |
| 142 | self._test_create_image_from_server_wait_until_active_not_found() |
| 143 | |
| 144 | def test_create_image_from_server_wait_until_active_not_found_with_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 has a fault associated with it. |
| 149 | self._test_create_image_from_server_wait_until_active_not_found( |
| 150 | fault='Lost connection to hypervisor!') |
| 151 | |
| 152 | @mock.patch.multiple(compute_base.BaseV2ComputeTest, |
| 153 | compute_images_client=mock.DEFAULT, |
| 154 | images=[], create=True) |
| 155 | @mock.patch.object(waiters, 'wait_for_image_status', |
| 156 | side_effect=lib_exc.NotFound) |
| 157 | def test_create_image_from_server_wait_until_saving_not_found( |
| 158 | self, wait_for_image_status, compute_images_client): |
| 159 | # Tests create_image_from_server with wait_until='SAVING' kwarg and |
| 160 | # the a 404 is raised while waiting for the image status to change. In |
| 161 | # this case we do not get the server details and just re-raise the 404. |
| 162 | # setup mocks |
| 163 | image_id = uuidutils.generate_uuid() |
| 164 | fake_image = mock.Mock(response={'location': image_id}) |
| 165 | compute_images_client.create_image.return_value = fake_image |
| 166 | # call the utility method |
| 167 | self.assertRaises( |
| 168 | lib_exc.NotFound, |
| 169 | compute_base.BaseV2ComputeTest.create_image_from_server, |
| 170 | mock.sentinel.server_id, wait_until='SAVING') |
| 171 | # make our assertions |
| 172 | wait_for_image_status.assert_called_once_with( |
| 173 | compute_images_client, image_id, 'SAVING') |