Change the image download to download to tmpdir as a default

This is to prevent (as far as possible) putting the image into the
module under test.
This commit is contained in:
Alex Kavanagh
2018-09-19 14:24:51 +02:00
parent 6d9724bdcf
commit 4bf09fc45a
2 changed files with 32 additions and 4 deletions

View File

@@ -434,16 +434,38 @@ class TestOpenStackUtils(ut_utils.BaseTestCase):
expected_status='active',
msg='Image status wait')
def test_create_image(self):
def test_create_image_use_tempdir(self):
glance_mock = mock.MagicMock()
self.patch_object(openstack_utils.os.path, "exists")
self.patch_object(openstack_utils, "download_image")
self.patch_object(openstack_utils, "upload_image_to_glance")
self.patch_object(openstack_utils.tempfile, "gettempdir")
self.gettempdir.return_value = "wibbly"
openstack_utils.create_image(
glance_mock,
'http://cirros/c.img',
'bob')
self.exists.return_value = False
self.download_image.assert_called_once_with(
'http://cirros/c.img',
'wibbly/c.img')
self.upload_image_to_glance.assert_called_once_with(
glance_mock,
'wibbly/c.img',
'bob')
def test_create_image_pass_directory(self):
glance_mock = mock.MagicMock()
self.patch_object(openstack_utils.os.path, "exists")
self.patch_object(openstack_utils, "download_image")
self.patch_object(openstack_utils, "upload_image_to_glance")
self.patch_object(openstack_utils.tempfile, "gettempdir")
openstack_utils.create_image(
glance_mock,
'http://cirros/c.img',
'bob',
'tests')
self.exists.return_value = False
self.download_image.assert_called_once_with(
'http://cirros/c.img',
'tests/c.img')
@@ -451,6 +473,7 @@ class TestOpenStackUtils(ut_utils.BaseTestCase):
glance_mock,
'tests/c.img',
'bob')
self.gettempdir.assert_not_called()
def test_create_ssh_key(self):
nova_mock = mock.MagicMock()

View File

@@ -46,6 +46,7 @@ import re
import six
import subprocess
import sys
import tempfile
import tenacity
import urllib
@@ -1536,7 +1537,7 @@ def upload_image_to_glance(glance, local_path, image_name, disk_format='qcow2',
return image
def create_image(glance, image_url, image_name, image_cache_dir='tests'):
def create_image(glance, image_url, image_name, image_cache_dir=None):
"""Download the image and upload it to glance.
Download an image from image_url and upload it to glance labelling
@@ -1548,11 +1549,15 @@ def create_image(glance, image_url, image_name, image_cache_dir='tests'):
:type image_url: str
:param image_name: display name for new image
:type image_name: str
:param image_cache_dir: Directory to store image in before uploading
:type image_cache_dir: str
:param image_cache_dir: Directory to store image in before uploading. If it
is not passed, or is None, then a tmp directory is used.
:type image_cache_dir: Option[str, None]
:returns: glance image pointer
:rtype: glanceclient.common.utils.RequestIdProxy
"""
if image_cache_dir is None:
image_cache_dir = tempfile.gettempdir()
logging.debug('Creating glance cirros image '
'({})...'.format(image_name))