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

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

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

@ -372,6 +372,8 @@ class UploadOvercloudImage(command.Command):
oc_vmlinuz_file = os.path.join(parsed_args.image_path, oc_vmlinuz_file = os.path.join(parsed_args.image_path,
image_name + image_name +
oc_vmlinuz_extension) oc_vmlinuz_extension)
with self._read_image_file_pointer(
parsed_args.image_path, oc_vmlinuz_file) as data:
kernel = (self._image_try_update(oc_vmlinuz_name, kernel = (self._image_try_update(oc_vmlinuz_name,
oc_vmlinuz_file, oc_vmlinuz_file,
parsed_args) or parsed_args) or
@ -380,8 +382,7 @@ class UploadOvercloudImage(command.Command):
is_public=True, is_public=True,
disk_format='aki', disk_format='aki',
properties=properties, properties=properties,
data=self._read_image_file_pointer( data=data
parsed_args.image_path, oc_vmlinuz_file)
)) ))
(oc_initrd_name, (oc_initrd_name,
@ -390,6 +391,8 @@ class UploadOvercloudImage(command.Command):
oc_initrd_file = os.path.join(parsed_args.image_path, oc_initrd_file = os.path.join(parsed_args.image_path,
image_name + image_name +
oc_initrd_extension) oc_initrd_extension)
with self._read_image_file_pointer(
parsed_args.image_path, oc_initrd_file) as data:
ramdisk = (self._image_try_update(oc_initrd_name, ramdisk = (self._image_try_update(oc_initrd_name,
oc_initrd_file, oc_initrd_file,
parsed_args) or parsed_args) or
@ -398,8 +401,7 @@ class UploadOvercloudImage(command.Command):
is_public=True, is_public=True,
disk_format='ari', disk_format='ari',
properties=properties, properties=properties,
data=self._read_image_file_pointer( data=data
parsed_args.image_path, oc_initrd_file)
)) ))
(oc_name, (oc_name,
@ -408,6 +410,8 @@ class UploadOvercloudImage(command.Command):
oc_file = os.path.join(parsed_args.image_path, oc_file = os.path.join(parsed_args.image_path,
image_name + image_name +
oc_extension) oc_extension)
with self._read_image_file_pointer(
parsed_args.image_path, oc_file) as data:
overcloud_image = (self._image_try_update(oc_name, oc_file, overcloud_image = (self._image_try_update(oc_name, oc_file,
parsed_args) or parsed_args) or
glance_client_adaptor.upload_image( glance_client_adaptor.upload_image(
@ -419,8 +423,7 @@ class UploadOvercloudImage(command.Command):
{'kernel_id': kernel.id, {'kernel_id': kernel.id,
'ramdisk_id': ramdisk.id}, 'ramdisk_id': ramdisk.id},
**properties), **properties),
data=self._read_image_file_pointer( data=data
parsed_args.image_path, oc_file)
)) ))
img_kernel_id = glance_client_adaptor.get_image_property( img_kernel_id = glance_client_adaptor.get_image_property(
@ -442,6 +445,8 @@ class UploadOvercloudImage(command.Command):
oc_file = os.path.join(parsed_args.image_path, oc_file = os.path.join(parsed_args.image_path,
image_name + image_name +
oc_extension) oc_extension)
with self._read_image_file_pointer(
parsed_args.image_path, oc_file) as data:
overcloud_image = (self._image_try_update(oc_name, oc_file, overcloud_image = (self._image_try_update(oc_name, oc_file,
parsed_args) or parsed_args) or
glance_client_adaptor.upload_image( glance_client_adaptor.upload_image(
@ -450,8 +455,7 @@ class UploadOvercloudImage(command.Command):
disk_format='qcow2', disk_format='qcow2',
container_format='bare', container_format='bare',
properties=properties, properties=properties,
data=self._read_image_file_pointer( data=data
parsed_args.image_path, oc_file)
)) ))
self.log.debug("uploading bm images to glance") self.log.debug("uploading bm images to glance")

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

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