blob: a1da34393ed1db0965e1aa8a2d1ff57e9173bb6e [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
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,
51 images=[], create=True)
52 @mock.patch.object(waiters, 'wait_for_image_status')
53 def test_create_image_from_server_wait_until_active(self,
54 wait_for_image_status,
55 compute_images_client):
56 """Tests create_image_from_server with wait_until='ACTIVE' kwarg."""
57 # setup mocks
58 image_id = uuidutils.generate_uuid()
59 fake_image = mock.Mock(response={'location': image_id})
60 compute_images_client.create_image.return_value = fake_image
61 compute_images_client.show_image.return_value = (
62 {'image': fake_image})
63 # call the utility method
64 image = compute_base.BaseV2ComputeTest.create_image_from_server(
65 mock.sentinel.server_id, wait_until='ACTIVE')
66 self.assertEqual(fake_image, image)
67 # make our assertions
68 wait_for_image_status.assert_called_once_with(
69 compute_images_client, image_id, 'ACTIVE')
70 compute_images_client.show_image.assert_called_once_with(image_id)
Matt Riedemann13954352017-02-07 14:03:54 -050071
72 @mock.patch.multiple(compute_base.BaseV2ComputeTest,
73 compute_images_client=mock.DEFAULT,
74 servers_client=mock.DEFAULT,
75 images=[], create=True)
76 @mock.patch.object(waiters, 'wait_for_image_status',
77 side_effect=lib_exc.NotFound)
78 def _test_create_image_from_server_wait_until_active_not_found(
79 self, wait_for_image_status, compute_images_client,
80 servers_client, fault=None):
81 # setup mocks
82 image_id = uuidutils.generate_uuid()
83 fake_image = mock.Mock(response={'location': image_id})
84 compute_images_client.create_image.return_value = fake_image
85 fake_server = {'id': mock.sentinel.server_id}
86 if fault:
87 fake_server['fault'] = fault
88 servers_client.show_server.return_value = {'server': fake_server}
89 # call the utility method
90 ex = self.assertRaises(
91 exceptions.SnapshotNotFoundException,
92 compute_base.BaseV2ComputeTest.create_image_from_server,
93 mock.sentinel.server_id, wait_until='active')
94 # make our assertions
95 if fault:
96 self.assertIn(fault, six.text_type(ex))
97 else:
98 self.assertNotIn(fault, six.text_type(ex))
99 wait_for_image_status.assert_called_once_with(
100 compute_images_client, image_id, 'active')
101 servers_client.show_server.assert_called_once_with(
102 mock.sentinel.server_id)
103
104 def test_create_image_from_server_wait_until_active_not_found_no_fault(
105 self):
106 # Tests create_image_from_server with wait_until='active' kwarg and
107 # the a 404 is raised while waiting for the image status to change. In
108 # this test the server does not have a fault associated with it.
109 self._test_create_image_from_server_wait_until_active_not_found()
110
111 def test_create_image_from_server_wait_until_active_not_found_with_fault(
112 self):
113 # Tests create_image_from_server with wait_until='active' kwarg and
114 # the a 404 is raised while waiting for the image status to change. In
115 # this test the server has a fault associated with it.
116 self._test_create_image_from_server_wait_until_active_not_found(
117 fault='Lost connection to hypervisor!')
118
119 @mock.patch.multiple(compute_base.BaseV2ComputeTest,
120 compute_images_client=mock.DEFAULT,
121 images=[], create=True)
122 @mock.patch.object(waiters, 'wait_for_image_status',
123 side_effect=lib_exc.NotFound)
124 def test_create_image_from_server_wait_until_saving_not_found(
125 self, wait_for_image_status, compute_images_client):
126 # Tests create_image_from_server with wait_until='SAVING' kwarg and
127 # the a 404 is raised while waiting for the image status to change. In
128 # this case we do not get the server details and just re-raise the 404.
129 # setup mocks
130 image_id = uuidutils.generate_uuid()
131 fake_image = mock.Mock(response={'location': image_id})
132 compute_images_client.create_image.return_value = fake_image
133 # call the utility method
134 self.assertRaises(
135 lib_exc.NotFound,
136 compute_base.BaseV2ComputeTest.create_image_from_server,
137 mock.sentinel.server_id, wait_until='SAVING')
138 # make our assertions
139 wait_for_image_status.assert_called_once_with(
140 compute_images_client, image_id, 'SAVING')