Fix many ResourceWarning messages in tox

This update is to fix `ResourceWarning` messages while running tox
tests because file descriptors are not closed explicitly after each
of tests. Although these tests are completed as `succeeded`, such nouse
warnings make hard to inspect log messages.

Closes-Bug: #1881416

Change-Id: Icff5315969a1303fcbf71ecddfaa7ea0c99a3e5b
Signed-off-by: Yasufumi Ogawa <yasufum.o@gmail.com>
This commit is contained in:
Yasufumi Ogawa 2020-06-01 16:07:33 +00:00
parent ba185d6f28
commit a3649b6811
5 changed files with 17 additions and 17 deletions

View File

@ -16,7 +16,6 @@
import datetime
import functools
import inspect
import io
import os
import shutil
import sys
@ -338,7 +337,8 @@ class Conductor(manager.Manager):
for file in imported_yamls:
file_path = os.path.join(
csar_path, dir_of_parent_definition_file, file)
file_data = yaml.safe_load(io.open(file_path))
with open(file_path) as f:
file_data = yaml.safe_load(f)
dest_file_path = os.path.abspath(file_path).split(
csar_path + '/')[-1]
file_path_and_data[dest_file_path] = yaml.dump(file_data)
@ -356,8 +356,9 @@ class Conductor(manager.Manager):
# This is CSAR containing a TOSCA-Metadata directory, which
# includes the TOSCA.meta metadata file providing an entry
# information for processing a CSAR file.
tosca_meta_data = yaml.safe_load(io.open(os.path.join(
csar_path, 'TOSCA-Metadata', 'TOSCA.meta')))
with open(os.path.join(
csar_path, 'TOSCA-Metadata', 'TOSCA.meta')) as f:
tosca_meta_data = yaml.safe_load(f)
file_path_and_data['TOSCA-Metadata/TOSCA.meta'] = yaml.dump(
tosca_meta_data)
entry_defination_file = tosca_meta_data['Entry-Definitions']
@ -371,7 +372,8 @@ class Conductor(manager.Manager):
os.listdir(csar_path),
key=lambda item: item.endswith(('yaml', '.yml')))[-1]
src_path = os.path.join(csar_path, root_yaml_file)
file_data = yaml.safe_load(io.open(src_path))
with open(src_path) as f:
file_data = yaml.safe_load(f)
file_path_and_data[root_yaml_file] = yaml.dump(file_data)
return file_path_and_data

View File

@ -13,7 +13,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import io
import os
from oslo_config import cfg
import shutil
@ -105,9 +104,9 @@ def get_expected_vnfd_data(zip_file=None):
'Definitions/etsi_nfv_sol001_common_types.yaml']
file_path_and_data = {}
for file_name in file_names:
with io.open(os.path.join(csar_temp_dir, file_name)) as f:
file_path_and_data.update({file_name: yaml.dump(yaml.safe_load(
f))})
with open(os.path.join(csar_temp_dir, file_name)) as f:
file_path_and_data.update({file_name: yaml.dump(
yaml.safe_load(f))})
shutil.rmtree(csar_temp_dir)
return file_path_and_data

View File

@ -51,8 +51,8 @@ def dummy_get_vim(*args, **kwargs):
def _get_template(name):
filename = os.path.abspath(os.path.join(os.path.dirname(__file__),
'../../etc/samples/' + str(name)))
f = codecs.open(filename, encoding='utf-8', errors='strict')
return f.read()
with codecs.open(filename, encoding='utf-8', errors='strict') as f:
return f.read()
class FakeDriverManager(mock.Mock):

View File

@ -27,8 +27,8 @@ def _get_template(name):
filename = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"../infra_drivers/openstack/data/", name)
f = codecs.open(filename, encoding='utf-8', errors='strict')
return f.read()
with codecs.open(filename, encoding='utf-8', errors='strict') as f:
return f.read()
class TestToscaUtils(testtools.TestCase):

View File

@ -16,7 +16,6 @@
from copy import deepcopy
import datetime
import io
import iso8601
import os
import shutil
@ -249,9 +248,9 @@ def return_vnfd_data(csar_without_tosca_meta=False):
'Definitions/etsi_nfv_sol001_vnfd_types.yaml']
file_path_and_data = {}
for file_name in file_names:
with io.open(os.path.join(csar_temp_dir, file_name)) as f:
file_path_and_data.update({file_name: yaml.dump(yaml.safe_load(
f))})
with open(os.path.join(csar_temp_dir, file_name)) as f:
file_path_and_data.update({file_name: yaml.dump(
yaml.safe_load(f))})
shutil.rmtree(csar_temp_dir)
return file_path_and_data