Merge "Close resources correctly"

This commit is contained in:
Zuul 2019-08-02 15:14:12 +00:00 committed by Gerrit Code Review
commit c57dcce036
5 changed files with 81 additions and 68 deletions

View File

@ -43,7 +43,15 @@ class FakeClientManager(object):
self.workflow_engine = mock.Mock()
class FakeWebSocket(object):
class FakeHandle(object):
def __enter__(self):
return self
def __exit__(self, *args):
return
class FakeWebSocket(FakeHandle):
def wait_for_messages(self, timeout=None):
yield {
@ -51,12 +59,6 @@ class FakeWebSocket(object):
'status': 'SUCCESS',
}
def __enter__(self):
return self
def __exit__(self, *args):
return
class FakeClientWrapper(object):

View File

@ -18,6 +18,7 @@ import os
from osc_lib import exceptions
import tripleo_common.arch
from tripleoclient.tests.fakes import FakeHandle
from tripleoclient.tests.v1.test_plugin import TestPluginV1
from tripleoclient.v1 import overcloud_image
@ -127,7 +128,9 @@ class TestUploadOvercloudImage(TestPluginV1):
properties={'kernel_id': 10, 'ramdisk_id': 10,
'hw_architecture': self._arch},
created_at='2015-07-31T14:37:22.000000'))
self.cmd._read_image_file_pointer = mock.Mock(return_value=b'IMGDATA')
self._file_handle = FakeHandle()
self.cmd._read_image_file_pointer = mock.Mock(
return_value=self._file_handle)
self.cmd._check_file_exists = mock.Mock(return_value=True)
@mock.patch('osc_lib.utils.find_resource')
@ -282,19 +285,19 @@ class TestUploadOvercloudImage(TestPluginV1):
)
self.app.client_manager.image.images.create.assert_has_calls([
mock.call(properties={'hw_architecture': self._arch},
data=b'IMGDATA',
data=self._file_handle,
name='overcloud-full-vmlinuz',
disk_format='aki',
is_public=True),
mock.call(properties={'hw_architecture': self._arch},
data=b'IMGDATA',
data=self._file_handle,
name='overcloud-full-initrd',
disk_format='ari',
is_public=True),
mock.call(properties={'kernel_id': 10, 'ramdisk_id': 10,
'hw_architecture': self._arch},
name='overcloud-full',
data=b'IMGDATA',
data=self._file_handle,
container_format='bare',
disk_format='qcow2',
is_public=True),
@ -322,19 +325,19 @@ class TestUploadOvercloudImage(TestPluginV1):
)
self.app.client_manager.image.images.create.assert_has_calls([
mock.call(properties={'hw_architecture': 'x86_64'},
data=b'IMGDATA',
data=self._file_handle,
name='x86_64-overcloud-full-vmlinuz',
disk_format='aki',
is_public=True),
mock.call(properties={'hw_architecture': 'x86_64'},
data=b'IMGDATA',
data=self._file_handle,
name='x86_64-overcloud-full-initrd',
disk_format='ari',
is_public=True),
mock.call(properties={'hw_architecture': 'x86_64',
'kernel_id': 10, 'ramdisk_id': 10},
name='x86_64-overcloud-full',
data=b'IMGDATA',
data=self._file_handle,
container_format='bare',
disk_format='qcow2',
is_public=True),
@ -452,7 +455,9 @@ class TestUploadOvercloudImageFull(TestPluginV1):
mock.Mock(id=10, name='imgname',
properties={'hw_architecture': self._arch},
created_at='2015-07-31T14:37:22.000000'))
self.cmd._read_image_file_pointer = mock.Mock(return_value=b'IMGDATA')
self._file_handle = FakeHandle()
self.cmd._read_image_file_pointer = mock.Mock(
return_value=self._file_handle)
self.cmd._check_file_exists = mock.Mock(return_value=True)
@mock.patch('os.path.isfile', autospec=True)
@ -623,7 +628,9 @@ class TestUploadOvercloudImageFullMultiArch(TestPluginV1):
# call so this way we always create() and get() the same mocked "image"
self.app.client_manager.image.images.create.side_effect = self.images
self.app.client_manager.image.images.get.side_effect = self.images
self.cmd._read_image_file_pointer = mock.Mock(return_value=b'IMGDATA')
self._file_handle = FakeHandle()
self.cmd._read_image_file_pointer = mock.Mock(
return_value=self._file_handle)
self.cmd._check_file_exists = mock.Mock(return_value=True)
@mock.patch('tripleo_common.arch.kernel_arch', autospec=True,
@ -773,7 +780,9 @@ class TestUploadOnlyExisting(TestPluginV1):
mock.Mock(id=10, name='imgname', properties={},
created_at='2015-07-31T14:37:22.000000'))
self.cmd._check_file_exists = mock.Mock()
self.cmd._read_image_file_pointer = mock.Mock(return_value=b'IMGDATA')
self._file_handle = FakeHandle()
self.cmd._read_image_file_pointer = mock.Mock(
return_value=self._file_handle)
@mock.patch('subprocess.check_call', autospec=True)
@mock.patch('os.path.isfile', autospec=True)
@ -781,7 +790,6 @@ class TestUploadOnlyExisting(TestPluginV1):
self, mock_isfile_call, mock_subprocess_call):
self.cmd._image_changed = mock.Mock(return_value=True)
self.cmd._image_try_update = mock.Mock(return_value=None)
self.cmd._read_image_file_pointer = mock.Mock(return_value=b'IMGDATA')
parsed_args = self.check_parser(
self.cmd, ['--whole-disk', '--image-type=ironic-python-agent'], [])
@ -802,7 +810,6 @@ class TestUploadOnlyExisting(TestPluginV1):
self, mock_isfile_call, mock_subprocess_call):
self.cmd._image_changed = mock.Mock(return_value=True)
self.cmd._image_try_update = mock.Mock(return_value=None)
self.cmd._read_image_file_pointer = mock.Mock(return_value=b'IMGDATA')
parsed_args = self.check_parser(
self.cmd, ['--whole-disk', '--image-type=os'], [])
@ -826,7 +833,6 @@ class TestUploadOnlyExisting(TestPluginV1):
self, mock_isfile_call, mock_subprocess_call):
self.cmd._image_changed = mock.Mock(return_value=True)
self.cmd._image_try_update = mock.Mock(return_value=None)
self.cmd._read_image_file_pointer = mock.Mock(return_value=b'IMGDATA')
parsed_args = self.check_parser(
self.cmd, ['--image-type=ironic-python-agent'], [])
@ -847,7 +853,6 @@ class TestUploadOnlyExisting(TestPluginV1):
self, mock_isfile_call, mock_subprocess_call):
self.cmd._image_changed = mock.Mock(return_value=True)
self.cmd._image_try_update = mock.Mock(return_value=None)
self.cmd._read_image_file_pointer = mock.Mock(return_value=b'IMGDATA')
parsed_args = self.check_parser(
self.cmd, ['--image-type=os'], [])

View File

@ -372,17 +372,18 @@ class UploadOvercloudImage(command.Command):
oc_vmlinuz_file = os.path.join(parsed_args.image_path,
image_name +
oc_vmlinuz_extension)
kernel = (self._image_try_update(oc_vmlinuz_name,
oc_vmlinuz_file,
parsed_args) or
glance_client_adaptor.upload_image(
name=oc_vmlinuz_name,
is_public=True,
disk_format='aki',
properties=properties,
data=self._read_image_file_pointer(
parsed_args.image_path, oc_vmlinuz_file)
))
with self._read_image_file_pointer(
parsed_args.image_path, oc_vmlinuz_file) as data:
kernel = (self._image_try_update(oc_vmlinuz_name,
oc_vmlinuz_file,
parsed_args) or
glance_client_adaptor.upload_image(
name=oc_vmlinuz_name,
is_public=True,
disk_format='aki',
properties=properties,
data=data
))
(oc_initrd_name,
oc_initrd_extension) = plugin_utils.overcloud_ramdisk(
@ -390,17 +391,18 @@ class UploadOvercloudImage(command.Command):
oc_initrd_file = os.path.join(parsed_args.image_path,
image_name +
oc_initrd_extension)
ramdisk = (self._image_try_update(oc_initrd_name,
oc_initrd_file,
parsed_args) or
glance_client_adaptor.upload_image(
name=oc_initrd_name,
is_public=True,
disk_format='ari',
properties=properties,
data=self._read_image_file_pointer(
parsed_args.image_path, oc_initrd_file)
))
with self._read_image_file_pointer(
parsed_args.image_path, oc_initrd_file) as data:
ramdisk = (self._image_try_update(oc_initrd_name,
oc_initrd_file,
parsed_args) or
glance_client_adaptor.upload_image(
name=oc_initrd_name,
is_public=True,
disk_format='ari',
properties=properties,
data=data
))
(oc_name,
oc_extension) = plugin_utils.overcloud_image(
@ -408,20 +410,21 @@ class UploadOvercloudImage(command.Command):
oc_file = os.path.join(parsed_args.image_path,
image_name +
oc_extension)
overcloud_image = (self._image_try_update(oc_name, oc_file,
parsed_args) or
glance_client_adaptor.upload_image(
name=oc_name,
is_public=True,
disk_format='qcow2',
container_format='bare',
properties=dict(
{'kernel_id': kernel.id,
'ramdisk_id': ramdisk.id},
**properties),
data=self._read_image_file_pointer(
parsed_args.image_path, oc_file)
))
with self._read_image_file_pointer(
parsed_args.image_path, oc_file) as data:
overcloud_image = (self._image_try_update(oc_name, oc_file,
parsed_args) or
glance_client_adaptor.upload_image(
name=oc_name,
is_public=True,
disk_format='qcow2',
container_format='bare',
properties=dict(
{'kernel_id': kernel.id,
'ramdisk_id': ramdisk.id},
**properties),
data=data
))
img_kernel_id = glance_client_adaptor.get_image_property(
overcloud_image, 'kernel_id')
@ -442,17 +445,18 @@ class UploadOvercloudImage(command.Command):
oc_file = os.path.join(parsed_args.image_path,
image_name +
oc_extension)
overcloud_image = (self._image_try_update(oc_name, oc_file,
parsed_args) or
glance_client_adaptor.upload_image(
name=oc_name,
is_public=True,
disk_format='qcow2',
container_format='bare',
properties=properties,
data=self._read_image_file_pointer(
parsed_args.image_path, oc_file)
))
with self._read_image_file_pointer(
parsed_args.image_path, oc_file) as data:
overcloud_image = (self._image_try_update(oc_name, oc_file,
parsed_args) or
glance_client_adaptor.upload_image(
name=oc_name,
is_public=True,
disk_format='qcow2',
container_format='bare',
properties=properties,
data=data
))
self.log.debug("uploading bm images to glance")

View File

@ -292,6 +292,7 @@ class ImportNode(command.Command):
self.log.debug("take_action(%s)" % parsed_args)
nodes_config = oooutils.parse_env_file(parsed_args.env_file)
parsed_args.env_file.close()
if parsed_args.validate_only:
return baremetal.validate_nodes(self.app.client_manager,

View File

@ -202,6 +202,7 @@ class ExportPlan(command.Command):
)
f = request.urlopen(tempurl)
tarball_contents = f.read()
f.close()
with open(outfile, 'wb') as f:
f.write(tarball_contents)