Close resources correctly
Under python3 not closing file handles or network resources correctly results in warnings being shown to the end user. This change switches the resource usage to properly close when we're done. Change-Id: Iaf08f7d7b919b154797cf8f10663f349424cd6a2 Related-Bug: #1837393
This commit is contained in:
parent
7b365a586e
commit
6521585912
@ -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):
|
||||
|
||||
|
@ -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'], [])
|
||||
|
@ -372,6 +372,8 @@ class UploadOvercloudImage(command.Command):
|
||||
oc_vmlinuz_file = os.path.join(parsed_args.image_path,
|
||||
image_name +
|
||||
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,
|
||||
oc_vmlinuz_file,
|
||||
parsed_args) or
|
||||
@ -380,8 +382,7 @@ class UploadOvercloudImage(command.Command):
|
||||
is_public=True,
|
||||
disk_format='aki',
|
||||
properties=properties,
|
||||
data=self._read_image_file_pointer(
|
||||
parsed_args.image_path, oc_vmlinuz_file)
|
||||
data=data
|
||||
))
|
||||
|
||||
(oc_initrd_name,
|
||||
@ -390,6 +391,8 @@ class UploadOvercloudImage(command.Command):
|
||||
oc_initrd_file = os.path.join(parsed_args.image_path,
|
||||
image_name +
|
||||
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,
|
||||
oc_initrd_file,
|
||||
parsed_args) or
|
||||
@ -398,8 +401,7 @@ class UploadOvercloudImage(command.Command):
|
||||
is_public=True,
|
||||
disk_format='ari',
|
||||
properties=properties,
|
||||
data=self._read_image_file_pointer(
|
||||
parsed_args.image_path, oc_initrd_file)
|
||||
data=data
|
||||
))
|
||||
|
||||
(oc_name,
|
||||
@ -408,6 +410,8 @@ class UploadOvercloudImage(command.Command):
|
||||
oc_file = os.path.join(parsed_args.image_path,
|
||||
image_name +
|
||||
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,
|
||||
parsed_args) or
|
||||
glance_client_adaptor.upload_image(
|
||||
@ -419,8 +423,7 @@ class UploadOvercloudImage(command.Command):
|
||||
{'kernel_id': kernel.id,
|
||||
'ramdisk_id': ramdisk.id},
|
||||
**properties),
|
||||
data=self._read_image_file_pointer(
|
||||
parsed_args.image_path, oc_file)
|
||||
data=data
|
||||
))
|
||||
|
||||
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,
|
||||
image_name +
|
||||
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,
|
||||
parsed_args) or
|
||||
glance_client_adaptor.upload_image(
|
||||
@ -450,8 +455,7 @@ class UploadOvercloudImage(command.Command):
|
||||
disk_format='qcow2',
|
||||
container_format='bare',
|
||||
properties=properties,
|
||||
data=self._read_image_file_pointer(
|
||||
parsed_args.image_path, oc_file)
|
||||
data=data
|
||||
))
|
||||
|
||||
self.log.debug("uploading bm images to glance")
|
||||
|
@ -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,
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user