Debian Codegraph @

config_tempest/tests/services/test_image.py

raw symbols SCIP
1 # Copyright 2018 Red Hat, Inc.
2 # All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 from unittest import mock
17
18 from fixtures import MonkeyPatch
19
20 from config_tempest import constants as C
21 from config_tempest.services.image import ImageService
22 from config_tempest.tempest_conf import TempestConf
23 from config_tempest.tests.base import BaseServiceTest
24
25
26 class TestImageService(BaseServiceTest):
27
28 CLIENT_MOCK = 'tempest.lib.services.image.v2.images_client.ImagesClient'
29 IMAGES_LIST = [
30 {"status": "active", "name": "ImageName"},
31 {"status": "default", "name": "MyImage"},
32 {"status": "active", "name": "MyImage"}
33 ]
34
35 def setUp(self):
36 super(TestImageService, self).setUp()
37 self.Service = ImageService("ServiceName",
38 "ServiceType",
39 self.FAKE_URL,
40 self.FAKE_TOKEN,
41 disable_ssl_validation=False)
42 self.Service.disk_format = ".format"
43 self.Service.non_admin = False
44 self.Service.convert = False
45 self.Service.client = self.FakeServiceClient()
46
47 C.DEFAULT_IMAGE_DIR = "/img/"
48 self.conf = TempestConf()
49 self.conf.set("image", "image_path", "my_image.qcow2")
50 self.conf.set("image", "http_image", "http_image.qcow2")
51
52 @mock.patch('config_tempest.services.image.ImageService._find_image')
53 @mock.patch('config_tempest.services.image.ImageService'
54 '.find_or_upload_image')
55 @mock.patch('os.makedirs')
56 def _test_create_tempest_images(self, mock_makedirs, mock_find_upload,
57 mock_find_image):
58 mock_find_upload.side_effect = ["id_c", "id_d"]
59 mock_find_image.return_value = {'name': 'my_image.qcow2'}
60 self.Service.create_tempest_images(conf=self.conf)
61 mock_makedirs.assert_called()
62 self.assertEqual(self.conf.get('compute', 'image_ref'), 'id_c')
63 self.assertEqual(self.conf.get('compute', 'image_ref_alt'), 'id_d')
64 self.assertEqual(self.conf.get('scenario', 'img_file'),
65 '/img/my_image.qcow2')
66
67 @mock.patch('config_tempest.services.image.ImageService._find_image')
68 @mock.patch('config_tempest.services.image.ImageService._download_file')
69 @mock.patch('config_tempest.services.image.ImageService._upload_image')
70 def _test_find_or_upload_image_not_found_creation_allowed_format(
71 self, mock_upload_image,
72 mock_download_file, mock_find_image, format):
73 mock_find_image.return_value = None
74 mock_upload_image.return_value = {"id": "my_fake_id"}
75 image_source = format + "://any_random_url"
76 image_dest = "my_dest"
77 image_name = "my_image"
78 image_id = self.Service.find_or_upload_image(
79 image_id=None, image_dest=image_dest,
80 image_name=image_name, image_source=image_source)
81 mock_download_file.assert_called_with(image_source, image_dest)
82 mock_upload_image.assert_called_with(image_name, image_dest)
83 self.assertEqual(image_id, "my_fake_id")
84
85 def _mock_list_images(self, return_value, image_name, expected_resp):
86
87 mock_function = mock.Mock(return_value=return_value)
88 func2mock = self.FAKE_CLIENT_MOCK + '.list_images'
89 self.useFixture(MonkeyPatch(func2mock, mock_function))
90 resp = self.Service._find_image(image_id=None,
91 image_name=image_name)
92 self.assertEqual(resp, expected_resp)
93
94 def test_set_get_versions(self):
95 exp_resp = ['v2.0', 'v2.1']
96 self._set_get_versions(self.Service, exp_resp, self.FAKE_VERSIONS)
97
98 @mock.patch('config_tempest.services.image.ImageService'
99 '.find_or_upload_image')
100 def test_create_tempest_images_exception(self, mock_find_upload):
101 mock_find_upload.side_effect = Exception
102 exc = Exception
103 self.assertRaises(exc,
104 self.Service.create_tempest_images,
105 conf=self.conf)
106
107 def test_create_tempest_images_ref_alt_ref(self):
108 self.conf.set('compute', 'image_ref', 'id_a')
109 self.conf.set('compute', 'image_ref_alt', 'id_b')
110 self._test_create_tempest_images()
111
112 def test_create_tempest_images_ref_no_alt_ref(self):
113 self.conf.set('compute', 'image_ref', 'id_a')
114 self._test_create_tempest_images()
115
116 def test_create_tempest_images_no_ref_alt_ref(self):
117 self.conf.set('compute', 'image_ref_alt', 'id_b')
118 self._test_create_tempest_images()
119
120 def test_create_tempest_images_no_ref_no_alt_ref(self):
121 self._test_create_tempest_images()
122
123 @mock.patch('config_tempest.services.image.ImageService._find_image')
124 def test_find_or_upload_image_not_found_creation_not_allowed(
125 self, mock_find_image):
126 mock_find_image.return_value = None
127 exc = Exception
128 self.assertRaises(exc, self.Service.find_or_upload_image,
129 image_id=None, image_name=None)
130
131 def test_find_or_upload_image_not_found_creation_allowed_http(self):
132 self._test_find_or_upload_image_not_found_creation_allowed_format(
133 format="http")
134
135 def test_find_or_upload_image_not_found_creation_allowed_https(self):
136 self._test_find_or_upload_image_not_found_creation_allowed_format(
137 format="https")
138
139 @mock.patch('shutil.copyfile')
140 @mock.patch('config_tempest.services.image.ImageService._find_image')
141 @mock.patch('config_tempest.services.image.ImageService._download_file')
142 @mock.patch('config_tempest.services.image.ImageService._upload_image')
143 def test_find_or_upload_image_not_found_creation_allowed_ftp_old(
144 self, mock_upload_image, mock_download_file, mock_find_image,
145 mock_copy):
146 mock_find_image.return_value = None
147 mock_upload_image.return_value = {"id": "my_fake_id"}
148 # image source does not start with http or https
149 image_source = "ftp://any_random_url"
150 image_dest = "place_on_disk"
151 image_name = "my_image"
152 image_id = self.Service.find_or_upload_image(
153 image_id=None, image_name=image_name,
154 image_source=image_source, image_dest=image_dest)
155 mock_copy.assert_called_with(image_source, image_dest)
156 mock_upload_image.assert_called_with(
157 image_name, image_dest)
158 self.assertEqual(image_id, "my_fake_id")
159
160 @mock.patch('os.path.isfile')
161 @mock.patch('config_tempest.services.image.ImageService._find_image')
162 def test_find_or_upload_image_found_downloaded(
163 self, mock_find_image, mock_isfile):
164 mock_find_image.return_value = \
165 {"status": "active", "name": "ImageName", "id": "my_fake_id"}
166 mock_isfile.return_value = True
167 image_id = self.Service.find_or_upload_image(
168 image_id=None, image_name=None)
169 self.assertEqual(image_id, "my_fake_id")
170
171 @mock.patch('config_tempest.services.image.ImageService._download_image')
172 @mock.patch('os.path.isfile')
173 @mock.patch('config_tempest.services.image.ImageService._find_image')
174 def test_find_or_upload_image_found_not_downloaded(
175 self, mock_find_image, mock_isfile, mock_download_image):
176 image_id = "my_fake_id"
177 mock_find_image.return_value = \
178 {"status": "active", "name": "ImageName", "id": image_id}
179 mock_isfile.return_value = False
180 image_id = self.Service.find_or_upload_image(
181 image_id=None, image_name=None)
182 mock_download_image.assert_called()
183 self.assertEqual(image_id, "my_fake_id")
184
185 def test_find_image_found(self):
186 expected_resp = {"status": "default", "name": "MyImage"}
187 self._mock_list_images(return_value={"images": self.IMAGES_LIST},
188 image_name="MyImage",
189 expected_resp=expected_resp)
190
191 def test_find_image_not_found(self):
192 self._mock_list_images(return_value={"images": self.IMAGES_LIST},
193 image_name="DoesNotExist",
194 expected_resp=None)
195
196 def test_find_image_by_id(self):
197 expected_resp = {"id": "001", "status": "active", "name": "ImageName"}
198 mock_function = mock.Mock(return_value=expected_resp)
199 func2mock = self.FAKE_CLIENT_MOCK + '.show_image'
200 self.useFixture(MonkeyPatch(func2mock, mock_function))
201 resp = self.Service._find_image(image_id="001",
202 image_name="cirros")
203 self.assertEqual(resp, expected_resp)
204
205 @mock.patch('subprocess.call')
206 @mock.patch('os.path.exists')
207 def test_convert_image_to_raw(self, mock_exists, mock_subcall):
208 path = '/path/of/my/image.qcow2'
209 raw_path = '/path/of/my/image.raw'
210 mock_exists.return_value = False
211 mock_subcall.return_value = 0
212 self.Service.convert_image_to_raw(path)
213 mock_subcall.assert_called_with(['qemu-img', 'convert',
214 path, raw_path])
215 self.assertEqual(self.Service.disk_format, 'raw')