Take care of resource leakage on tests

This commit is contained in:
Pedro Algarvio 2017-04-04 13:11:54 +01:00
parent e6f43b46c0
commit 61003bca0a
No known key found for this signature in database
GPG key ID: BB36BF6584A298FF
29 changed files with 147 additions and 119 deletions

View file

@ -8,7 +8,7 @@
# run tests. It *will* install the build deps on the machine running the script.
#
# pylint: disable=file-perms
# pylint: disable=file-perms,resource-leakage
from __future__ import absolute_import, print_function
import errno

View file

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
# pylint: disable=resource-leakage
# Import Python libs
from __future__ import absolute_import, print_function

View file

@ -30,7 +30,8 @@ class MinionBlackoutTestCase(ModuleCase):
'''
setup minion blackout mode
'''
salt.utils.fopen(BLACKOUT_PILLAR, 'w').write(blackout_data)
with salt.utils.fopen(BLACKOUT_PILLAR, 'w') as wfh:
wfh.write(blackout_data)
self.run_function('saltutil.refresh_pillar')
sleep(5) # wait for minion to enter blackout mode

View file

@ -515,7 +515,7 @@ class CPModuleTest(ModuleCase):
def test_push(self):
log_to_xfer = os.path.join(paths.TMP, uuid.uuid4().hex)
open(log_to_xfer, 'w').close()
open(log_to_xfer, 'w').close() # pylint: disable=resource-leakage
try:
self.run_function('cp.push', [log_to_xfer])
tgt_cache_file = os.path.join(

View file

@ -91,7 +91,7 @@ class GitModuleTest(ModuleCase):
dir_path = os.path.join(self.repo, dirname)
_makedirs(dir_path)
for filename in self.files:
with open(os.path.join(dir_path, filename), 'w') as fp_:
with salt.utils.fopen(os.path.join(dir_path, filename), 'w') as fp_:
fp_.write('This is a test file named ' + filename + '.')
# Navigate to the root of the repo to init, stage, and commit
os.chdir(self.repo)
@ -124,7 +124,7 @@ class GitModuleTest(ModuleCase):
['git', 'checkout', '--quiet', '-b', self.branches[1]]
)
# Add a line to the file
with open(self.files[0], 'a') as fp_:
with salt.utils.fopen(self.files[0], 'a') as fp_:
fp_.write('Added a line\n')
# Commit the updated file
subprocess.check_call(
@ -152,7 +152,7 @@ class GitModuleTest(ModuleCase):
files = [os.path.join(newdir_path, x) for x in self.files]
files_relpath = [os.path.join(newdir, x) for x in self.files]
for path in files:
with open(path, 'w') as fp_:
with salt.utils.fopen(path, 'w') as fp_:
fp_.write(
'This is a test file with relative path {0}.\n'
.format(path)
@ -169,7 +169,7 @@ class GitModuleTest(ModuleCase):
'''
filename = 'quux'
file_path = os.path.join(self.repo, filename)
with open(file_path, 'w') as fp_:
with salt.utils.fopen(file_path, 'w') as fp_:
fp_.write('This is a test file named ' + filename + '.\n')
ret = self.run_function('git.add', [self.repo, filename])
self.assertEqual(ret, 'add \'{0}\''.format(filename))
@ -310,7 +310,7 @@ class GitModuleTest(ModuleCase):
filename = 'foo'
commit_re_prefix = r'^\[master [0-9a-f]+\] '
# Add a line
with open(os.path.join(self.repo, filename), 'a') as fp_:
with salt.utils.fopen(os.path.join(self.repo, filename), 'a') as fp_:
fp_.write('Added a line\n')
# Stage the file
self.run_function('git.add', [self.repo, filename])
@ -320,7 +320,7 @@ class GitModuleTest(ModuleCase):
# Make sure the expected line is in the output
self.assertTrue(bool(re.search(commit_re_prefix + commit_msg, ret)))
# Add another line
with open(os.path.join(self.repo, filename), 'a') as fp_:
with salt.utils.fopen(os.path.join(self.repo, filename), 'a') as fp_:
fp_.write('Added another line\n')
# Commit the second file without staging
commit_msg = 'Add another line to ' + filename
@ -345,7 +345,7 @@ class GitModuleTest(ModuleCase):
['git', 'config', '--global', '--remove-section', 'foo']
)
for cmd in cmds:
with open(os.devnull, 'w') as devnull:
with salt.utils.fopen(os.devnull, 'w') as devnull:
try:
subprocess.check_call(cmd, stderr=devnull)
except subprocess.CalledProcessError:
@ -704,7 +704,7 @@ class GitModuleTest(ModuleCase):
'''
# Make a change to a different file than the one modifed in setUp
file_path = os.path.join(self.repo, self.files[1])
with open(file_path, 'a') as fp_:
with salt.utils.fopen(file_path, 'a') as fp_:
fp_.write('Added a line\n')
# Commit the change
self.assertTrue(
@ -848,7 +848,7 @@ class GitModuleTest(ModuleCase):
# TODO: test more stash actions
'''
file_path = os.path.join(self.repo, self.files[0])
with open(file_path, 'a') as fp_:
with salt.utils.fopen(file_path, 'a') as fp_:
fp_.write('Temp change to be stashed')
self.assertTrue(
'ERROR' not in self.run_function('git.stash', [self.repo])
@ -887,10 +887,10 @@ class GitModuleTest(ModuleCase):
'untracked': ['thisisalsoanewfile']
}
for filename in changes['modified']:
with open(os.path.join(self.repo, filename), 'a') as fp_:
with salt.utils.fopen(os.path.join(self.repo, filename), 'a') as fp_:
fp_.write('Added a line\n')
for filename in changes['new']:
with open(os.path.join(self.repo, filename), 'w') as fp_:
with salt.utils.fopen(os.path.join(self.repo, filename), 'w') as fp_:
fp_.write('This is a new file named ' + filename + '.')
# Stage the new file so it shows up as a 'new' file
self.assertTrue(
@ -902,7 +902,7 @@ class GitModuleTest(ModuleCase):
for filename in changes['deleted']:
self.run_function('git.rm', [self.repo, filename])
for filename in changes['untracked']:
with open(os.path.join(self.repo, filename), 'w') as fp_:
with salt.utils.fopen(os.path.join(self.repo, filename), 'w') as fp_:
fp_.write('This is a new file named ' + filename + '.')
self.assertEqual(
self.run_function('git.status', [self.repo]),

View file

@ -1233,7 +1233,7 @@ class StateModuleTest(ModuleCase, SaltReturnAssertsMixin):
'''
testfile = os.path.join(TMP, 'retry_file')
time.sleep(30)
open(testfile, 'a').close()
open(testfile, 'a').close() # pylint: disable=resource-leakage
def test_retry_option_eventual_success(self):
'''

View file

@ -108,7 +108,7 @@ class SystemModuleTest(ModuleCase):
log.debug('Comparing hwclock to sys clock')
with os.fdopen(rpipeFd, "r") as rpipe:
with os.fdopen(wpipeFd, "w") as wpipe:
with open(os.devnull, "r") as nulFd:
with salt.utils.fopen(os.devnull, "r") as nulFd:
p = subprocess.Popen(args=['hwclock', '--compare'],
stdin=nulFd, stdout=wpipeFd, stderr=subprocess.PIPE)
p.communicate()

View file

@ -23,7 +23,8 @@ class EnvTestCase(ModuleCase, SaltReturnAssertsMixin):
self.state_name = 'test_sdb_env'
self.state_file_name = self.state_name + '.sls'
self.state_file_set_var = os.path.join(STATE_DIR, self.state_file_name)
salt.utils.fopen(self.state_file_set_var, 'w').write(textwrap.dedent('''\
with salt.utils.fopen(self.state_file_set_var, 'w') as wfh:
wfh.write(textwrap.dedent('''\
set some env var:
cmd.run:
- name: echo {{ salt['sdb.set']('sdb://osenv/foo', 'bar') }}

View file

@ -204,7 +204,7 @@ class MinionTest(ShellCase, testprogram.TestProgramCase, ShellCaseCommonTestsMix
default_dir = os.path.join(sysconf_dir, 'default')
if not os.path.exists(default_dir):
os.makedirs(default_dir)
with open(os.path.join(default_dir, 'salt'), 'w') as defaults:
with salt.utils.fopen(os.path.join(default_dir, 'salt'), 'w') as defaults:
# Test suites is quite slow - extend the timeout
defaults.write(
'TIMEOUT=60\n'

View file

@ -803,7 +803,7 @@ class FileTest(ModuleCase, SaltReturnAssertsMixin):
self.addCleanup(lambda: shutil.rmtree(directory))
wrong_file = os.path.join(directory, "wrong")
with open(wrong_file, "w") as fp:
with salt.utils.fopen(wrong_file, "w") as fp:
fp.write("foo")
good_file = os.path.join(directory, "bar")
@ -838,7 +838,7 @@ class FileTest(ModuleCase, SaltReturnAssertsMixin):
self.addCleanup(lambda: shutil.rmtree(directory))
wrong_file = os.path.join(directory, "wrong")
with open(wrong_file, "w") as fp:
with salt.utils.fopen(wrong_file, "w") as fp:
fp.write("foo")
good_file = os.path.join(directory, "bar")
@ -874,7 +874,7 @@ class FileTest(ModuleCase, SaltReturnAssertsMixin):
self.addCleanup(lambda: shutil.rmtree(directory))
wrong_file = os.path.join(directory, "wrong")
with open(wrong_file, "w") as fp:
with salt.utils.fopen(wrong_file, "w") as fp:
fp.write("foo")
good_file = os.path.join(directory, "bar")

View file

@ -241,7 +241,7 @@ class GitTest(ModuleCase, SaltReturnAssertsMixin):
# Make a change to the repo by editing the file in the admin copy
# of the repo and committing.
head_pre = _head(admin_dir)
with open(os.path.join(admin_dir, 'LICENSE'), 'a') as fp_:
with salt.utils.fopen(os.path.join(admin_dir, 'LICENSE'), 'a') as fp_:
fp_.write('Hello world!')
self.run_function(
'git.commit', [admin_dir, 'added a line'],

View file

@ -21,14 +21,14 @@ import time
import yaml
import salt.utils
import salt.utils.process
import salt.utils.psutil_compat as psutils
import salt.defaults.exitcodes as exitcodes
import salt.ext.six as six
from tests.support.unit import TestCase
import tests.integration as integration
from tests.support.paths import CODE_DIR
from tests.support.processes import terminate_process
log = logging.getLogger(__name__)
@ -209,7 +209,7 @@ class TestProgram(six.with_metaclass(TestProgramMeta, object)):
if not config:
return
cpath = self.abs_path(self.config_file_get(config))
with open(cpath, 'w') as cfo:
with salt.utils.fopen(cpath, 'w') as cfo:
cfg = self.config_stringify(config)
log.debug('Writing configuration for {0} to {1}:\n{2}'.format(self.name, cpath, cfg))
cfo.write(cfg)
@ -386,8 +386,8 @@ class TestProgram(six.with_metaclass(TestProgramMeta, object)):
if path not in env_pypath:
env_pypath.append(path)
# Always ensure that the test tree is searched first for python modules
if integration.CODE_DIR != env_pypath[0]:
env_pypath.insert(0, integration.CODE_DIR)
if CODE_DIR != env_pypath[0]:
env_pypath.insert(0, CODE_DIR)
env_delta['PYTHONPATH'] = ':'.join(env_pypath)
cmd_env = dict(os.environ)
@ -658,8 +658,8 @@ class TestSaltProgram(six.with_metaclass(TestSaltProgramMeta, TestProgram)):
def install_script(self):
'''Generate the script file that calls python objects and libraries.'''
lines = []
script_source = os.path.join(integration.CODE_DIR, 'scripts', self.script)
with open(script_source, 'r') as sso:
script_source = os.path.join(CODE_DIR, 'scripts', self.script)
with salt.utils.fopen(script_source, 'r') as sso:
lines.extend(sso.readlines())
if lines[0].startswith('#!'):
lines.pop(0)
@ -667,7 +667,7 @@ class TestSaltProgram(six.with_metaclass(TestSaltProgramMeta, TestProgram)):
script_path = self.abs_path(os.path.join(self.script_dir, self.script))
log.debug('Installing "{0}" to "{1}"'.format(script_source, script_path))
with open(script_path, 'w') as sdo:
with salt.utils.fopen(script_path, 'w') as sdo:
sdo.write(''.join(lines))
sdo.flush()

View file

@ -154,11 +154,10 @@ def echo_parseable_environment(options, parser):
'.github_token'
)
if os.path.isfile(github_access_token_path):
headers = {
'Authorization': 'token {0}'.format(
open(github_access_token_path).read().strip()
)
}
with salt.utils.fopen(github_access_token_path) as rfh:
headers = {
'Authorization': 'token {0}'.format(rfh.read().strip())
}
http_req = requests.get(url, headers=headers)
if http_req.status_code != 200:

View file

@ -5,7 +5,7 @@
The minionswarm script will start a group of salt minions with different ids
on a single system to test scale capabilities
'''
# pylint: disable=resource-leakage
# Import Python Libs
from __future__ import absolute_import, print_function
import os

View file

@ -2,7 +2,7 @@
'''
Simple script to dump the contents of msgpack files to the terminal
'''
# pylint: disable=resource-leakage
# Import python libs
from __future__ import absolute_import, print_function
import os

View file

@ -33,7 +33,7 @@ tcpdump "tcp[tcpflags] & tcp-syn != 0" and port 4505 and "tcp[tcpflags] & tcp-ac
For Port 4506
tcpdump "tcp[tcpflags] & tcp-syn != 0" and port 4506 and "tcp[tcpflags] & tcp-ack == 0"
'''
# pylint: disable=resource-leakage
# Import Python Libs
from __future__ import absolute_import, print_function
import socket

View file

@ -210,10 +210,12 @@ class RedirectStdStreams(object):
'''
def __init__(self, stdout=None, stderr=None):
# Late import
import salt.utils
if stdout is None:
stdout = open(os.devnull, 'w')
stdout = salt.utils.fopen(os.devnull, 'w') # pylint: disable=resource-leakage
if stderr is None:
stderr = open(os.devnull, 'w')
stderr = salt.utils.fopen(os.devnull, 'w') # pylint: disable=resource-leakage
self.__stdout = stdout
self.__stderr = stderr

View file

@ -25,7 +25,6 @@ import traceback
import subprocess
import warnings
from functools import partial
from contextlib import closing
from collections import namedtuple
from tests.support import helpers
@ -303,7 +302,7 @@ class SaltTestingParser(optparse.OptionParser):
def parse_args(self, args=None, values=None):
self.options, self.args = optparse.OptionParser.parse_args(self, args, values)
if self.options.names_file:
with open(self.options.names_file, 'rb') as fp_:
with open(self.options.names_file, 'rb') as fp_: # pylint: disable=resource-leakage
lines = []
for line in fp_.readlines():
if six.PY2:
@ -847,7 +846,7 @@ class SaltTestingParser(optparse.OptionParser):
try:
time.sleep(0.15)
if cid_printed is False:
with closing(open(cidfile)) as cidfile_fd:
with open(cidfile) as cidfile_fd: # pylint: disable=resource-leakage
cid = cidfile_fd.read()
if cid:
print(cid)

View file

@ -7,6 +7,7 @@ import shutil
import tempfile
# Salt libs
import salt.utils
from salt.beacons import inotify
# Salt testing libs
@ -46,7 +47,7 @@ class INotifyBeaconTestCase(TestCase, LoaderModuleMockMixin):
ret = inotify.beacon(config)
self.assertEqual(ret, [])
with open(path, 'r') as f:
with salt.utils.fopen(path, 'r') as f:
pass
ret = inotify.beacon(config)
self.assertEqual(len(ret), 1)
@ -58,13 +59,13 @@ class INotifyBeaconTestCase(TestCase, LoaderModuleMockMixin):
ret = inotify.beacon(config)
self.assertEqual(ret, [])
fp = os.path.join(self.tmpdir, 'tmpfile')
with open(fp, 'w') as f:
with salt.utils.fopen(fp, 'w') as f:
pass
ret = inotify.beacon(config)
self.assertEqual(len(ret), 1)
self.assertEqual(ret[0]['path'], fp)
self.assertEqual(ret[0]['change'], 'IN_CREATE')
with open(fp, 'r') as f:
with salt.utils.fopen(fp, 'r') as f:
pass
ret = inotify.beacon(config)
self.assertEqual(ret, [])
@ -74,7 +75,7 @@ class INotifyBeaconTestCase(TestCase, LoaderModuleMockMixin):
ret = inotify.beacon(config)
self.assertEqual(ret, [])
fp = os.path.join(self.tmpdir, 'tmpfile')
with open(fp, 'w') as f:
with salt.utils.fopen(fp, 'w') as f:
pass
ret = inotify.beacon(config)
self.assertEqual(len(ret), 2)
@ -82,7 +83,7 @@ class INotifyBeaconTestCase(TestCase, LoaderModuleMockMixin):
self.assertEqual(ret[0]['change'], 'IN_CREATE')
self.assertEqual(ret[1]['path'], fp)
self.assertEqual(ret[1]['change'], 'IN_OPEN')
with open(fp, 'r') as f:
with salt.utils.fopen(fp, 'r') as f:
pass
ret = inotify.beacon(config)
self.assertEqual(len(ret), 1)
@ -95,12 +96,12 @@ class INotifyBeaconTestCase(TestCase, LoaderModuleMockMixin):
dp2 = os.path.join(dp1, 'subdir2')
os.mkdir(dp2)
fp = os.path.join(dp2, 'tmpfile')
with open(fp, 'w') as f:
with salt.utils.fopen(fp, 'w') as f:
pass
config = {self.tmpdir: {'mask': ['open'], 'recurse': True}}
ret = inotify.beacon(config)
self.assertEqual(ret, [])
with open(fp) as f:
with salt.utils.fopen(fp) as f:
pass
ret = inotify.beacon(config)
self.assertEqual(len(ret), 3)
@ -126,7 +127,7 @@ class INotifyBeaconTestCase(TestCase, LoaderModuleMockMixin):
self.assertEqual(ret[0]['path'], dp2)
self.assertEqual(ret[0]['change'], 'IN_CREATE|IN_ISDIR')
fp = os.path.join(dp2, 'tmpfile')
with open(fp, 'w') as f:
with salt.utils.fopen(fp, 'w') as f:
pass
ret = inotify.beacon(config)
self.assertEqual(len(ret), 1)

View file

@ -79,10 +79,11 @@ class ConfigTestCase(TestCase, AdaptedConfigurationTestCaseMixin):
def test_sha256_is_default_for_master(self):
fpath = tempfile.mktemp()
try:
salt.utils.fopen(fpath, 'w').write(
"root_dir: /\n"
"key_logfile: key\n"
)
with salt.utils.fopen(fpath, 'w') as wfh:
wfh.write(
"root_dir: /\n"
"key_logfile: key\n"
)
config = sconfig.master_config(fpath)
self.assertEqual(config['hash_type'], 'sha256')
finally:
@ -92,10 +93,11 @@ class ConfigTestCase(TestCase, AdaptedConfigurationTestCaseMixin):
def test_sha256_is_default_for_minion(self):
fpath = tempfile.mktemp()
try:
salt.utils.fopen(fpath, 'w').write(
"root_dir: /\n"
"key_logfile: key\n"
)
with salt.utils.fopen(fpath, 'w') as wfh:
wfh.write(
"root_dir: /\n"
"key_logfile: key\n"
)
config = sconfig.minion_config(fpath)
self.assertEqual(config['hash_type'], 'sha256')
finally:
@ -359,13 +361,15 @@ class ConfigTestCase(TestCase, AdaptedConfigurationTestCaseMixin):
# Create some kown files.
for f in 'abc':
fpath = os.path.join(tempdir, f)
salt.utils.fopen(fpath, 'w').write(f)
with salt.utils.fopen(fpath, 'w') as wfh:
wfh.write(f)
salt.utils.fopen(fpath, 'w').write(
'file_roots:\n'
' base:\n'
' - {0}'.format(os.path.join(tempdir, '*'))
)
with salt.utils.fopen(fpath, 'w') as wfh:
wfh.write(
'file_roots:\n'
' base:\n'
' - {0}'.format(os.path.join(tempdir, '*'))
)
config = sconfig.master_config(fpath)
base = config['file_roots']['base']
self.assertEqual(set(base), set([
@ -387,13 +391,15 @@ class ConfigTestCase(TestCase, AdaptedConfigurationTestCaseMixin):
# Create some kown files.
for f in 'abc':
fpath = os.path.join(tempdir, f)
salt.utils.fopen(fpath, 'w').write(f)
with salt.utils.fopen(fpath, 'w') as wfh:
wfh.write(f)
salt.utils.fopen(fpath, 'w').write(
'pillar_roots:\n'
' base:\n'
' - {0}'.format(os.path.join(tempdir, '*'))
)
with salt.utils.fopen(fpath, 'w') as wfh:
wfh.write(
'pillar_roots:\n'
' base:\n'
' - {0}'.format(os.path.join(tempdir, '*'))
)
config = sconfig.master_config(fpath)
base = config['pillar_roots']['base']
self.assertEqual(set(base), set([
@ -415,13 +421,15 @@ class ConfigTestCase(TestCase, AdaptedConfigurationTestCaseMixin):
# Create some kown files.
for f in 'abc':
fpath = os.path.join(tempdir, f)
salt.utils.fopen(fpath, 'w').write(f)
with salt.utils.fopen(fpath, 'w') as wfh:
wfh.write(f)
salt.utils.fopen(fpath, 'w').write(
'file_roots:\n'
' base:\n'
' - {0}'.format(os.path.join(tempdir, '*'))
)
with salt.utils.fopen(fpath, 'w') as wfh:
wfh.write(
'file_roots:\n'
' base:\n'
' - {0}'.format(os.path.join(tempdir, '*'))
)
config = sconfig.minion_config(fpath)
base = config['file_roots']['base']
self.assertEqual(set(base), set([
@ -443,13 +451,15 @@ class ConfigTestCase(TestCase, AdaptedConfigurationTestCaseMixin):
# Create some kown files.
for f in 'abc':
fpath = os.path.join(tempdir, f)
salt.utils.fopen(fpath, 'w').write(f)
with salt.utils.fopen(fpath, 'w') as wfh:
wfh.write(f)
salt.utils.fopen(fpath, 'w').write(
'pillar_roots:\n'
' base:\n'
' - {0}'.format(os.path.join(tempdir, '*'))
)
with salt.utils.fopen(fpath, 'w') as wfh:
wfh.write(
'pillar_roots:\n'
' base:\n'
' - {0}'.format(os.path.join(tempdir, '*'))
)
config = sconfig.minion_config(fpath)
base = config['pillar_roots']['base']
self.assertEqual(set(base), set([

View file

@ -81,7 +81,7 @@ class LazyLoaderTest(TestCase):
self.module_dir = tempfile.mkdtemp(dir=TMP)
self.module_file = os.path.join(self.module_dir,
'{0}.py'.format(self.module_name))
with open(self.module_file, 'w') as fh:
with salt.utils.fopen(self.module_file, 'w') as fh:
fh.write(loader_template)
fh.flush()
os.fsync(fh.fileno())
@ -346,7 +346,7 @@ class LazyLoaderReloadingTest(TestCase):
def update_module(self):
self.count += 1
with open(self.module_path, 'wb') as fh:
with salt.utils.fopen(self.module_path, 'wb') as fh:
fh.write(
salt.utils.to_bytes(
module_template.format(count=self.count)
@ -484,7 +484,7 @@ class LazyLoaderVirtualAliasTest(TestCase):
del cls.opts
def update_module(self):
with open(self.module_path, 'wb') as fh:
with salt.utils.fopen(self.module_path, 'wb') as fh:
fh.write(salt.utils.to_bytes(virtual_alias_module_template))
fh.flush()
os.fsync(fh.fileno()) # flush to disk
@ -578,7 +578,7 @@ class LazyLoaderSubmodReloadingTest(TestCase):
def update_module(self):
self.count += 1
with open(self.module_path, 'wb') as fh:
with salt.utils.fopen(self.module_path, 'wb') as fh:
fh.write(
salt.utils.to_bytes(
submodule_template.format(self.module_name, count=self.count)
@ -602,7 +602,7 @@ class LazyLoaderSubmodReloadingTest(TestCase):
for modname in list(sys.modules):
if modname.startswith(self.module_name):
del sys.modules[modname]
with open(self.lib_path, 'wb') as fh:
with salt.utils.fopen(self.lib_path, 'wb') as fh:
fh.write(
salt.utils.to_bytes(
submodule_lib_template.format(count=self.lib_count)
@ -738,7 +738,7 @@ class LazyLoaderModulePackageTest(TestCase):
dirname = os.path.dirname(pyfile)
if not os.path.exists(dirname):
os.makedirs(dirname)
with open(pyfile, 'wb') as fh:
with salt.utils.fopen(pyfile, 'wb') as fh:
fh.write(salt.utils.to_bytes(contents))
fh.flush()
os.fsync(fh.fileno()) # flush to disk
@ -827,7 +827,7 @@ class LazyLoaderDeepSubmodReloadingTest(TestCase):
self.lib_count = collections.defaultdict(int) # mapping of path -> count
# bootstrap libs
with open(os.path.join(self.module_dir, '__init__.py'), 'w') as fh:
with salt.utils.fopen(os.path.join(self.module_dir, '__init__.py'), 'w') as fh:
# No .decode() needed here as deep_init_base is defined as str and
# not bytes.
fh.write(deep_init_base.format(self.module_name))
@ -881,7 +881,7 @@ class LazyLoaderDeepSubmodReloadingTest(TestCase):
del sys.modules[modname]
path = os.path.join(self.lib_paths[lib_name], '__init__.py')
self.lib_count[lib_name] += 1
with open(path, 'wb') as fh:
with salt.utils.fopen(path, 'wb') as fh:
fh.write(
salt.utils.to_bytes(
submodule_lib_template.format(count=self.lib_count[lib_name])

View file

@ -19,11 +19,11 @@ from tests.support.mock import (
)
# Import Salt Libs
import salt.utils
import salt.transport
import salt.modules.cp as cp
from salt.utils import templates
from salt.exceptions import CommandExecutionError
import salt.utils
import salt.transport
@skipIf(NO_MOCK, NO_MOCK_REASON)
@ -142,10 +142,10 @@ class CpTestCase(TestCase, LoaderModuleMockMixin):
'''
response = cp.push('/saltines/test.file')
self.assertEqual(response, True)
self.assertEqual(salt.utils.fopen().read.call_count, 2)
self.assertEqual(salt.utils.fopen().read.call_count, 2) # pylint: disable=resource-leakage
salt.transport.Channel.factory({}).send.assert_called_once_with(
dict(
loc=salt.utils.fopen().tell(),
loc=salt.utils.fopen().tell(), # pylint: disable=resource-leakage
cmd='_file_recv',
tok='token',
path=['saltines', 'test.file'],

View file

@ -16,6 +16,7 @@ from tests.support.unit import TestCase
from tests.support.helpers import skip_if_binaries_missing
# Import Salt libs
import salt.utils
import salt.modules.k8s as k8s
# Import 3rd-party libs
@ -85,7 +86,7 @@ class TestK8SSecrets(TestCase):
def test_get_one_secret(self):
name = self.name
filename = "/tmp/{0}.json".format(name)
with open(filename, 'w') as f:
with salt.utils.fopen(filename, 'w') as f:
json.dump(self.request, f)
create = Popen(["kubectl", "--namespace=default", "create", "-f", filename], stdout=PIPE)
@ -101,7 +102,7 @@ class TestK8SSecrets(TestCase):
def test_get_decoded_secret(self):
name = self.name
filename = "/tmp/{0}.json".format(name)
with open(filename, 'w') as f:
with salt.utils.fopen(filename, 'w') as f:
json.dump(self.request, f)
create = Popen(["kubectl", "--namespace=default", "create", "-f", filename], stdout=PIPE)
@ -117,7 +118,7 @@ class TestK8SSecrets(TestCase):
expected_data = {}
for i in range(2):
names.append("/tmp/{0}-{1}".format(name, i))
with open("/tmp/{0}-{1}".format(name, i), 'w') as f:
with salt.utils.fopen("/tmp/{0}-{1}".format(name, i), 'w') as f:
expected_data["{0}-{1}".format(name, i)] = base64.b64encode("{0}{1}".format(name, i))
f.write("{0}{1}".format(name, i))
res = k8s.create_secret("default", name, names, apiserver_url="http://127.0.0.1:8080")
@ -131,7 +132,7 @@ class TestK8SSecrets(TestCase):
def test_update_secret(self):
name = self.name
filename = "/tmp/{0}.json".format(name)
with open(filename, 'w') as f:
with salt.utils.fopen(filename, 'w') as f:
json.dump(self.request, f)
create = Popen(["kubectl", "--namespace=default", "create", "-f", filename], stdout=PIPE)
@ -141,7 +142,7 @@ class TestK8SSecrets(TestCase):
names = []
for i in range(3):
names.append("/tmp/{0}-{1}-updated".format(name, i))
with open("/tmp/{0}-{1}-updated".format(name, i), 'w') as f:
with salt.utils.fopen("/tmp/{0}-{1}-updated".format(name, i), 'w') as f:
expected_data["{0}-{1}-updated".format(name, i)] = base64.b64encode("{0}{1}-updated".format(name, i))
f.write("{0}{1}-updated".format(name, i))
@ -157,7 +158,7 @@ class TestK8SSecrets(TestCase):
def test_delete_secret(self):
name = self.name
filename = "/tmp/{0}.json".format(name)
with open(filename, 'w') as f:
with salt.utils.fopen(filename, 'w') as f:
json.dump(self.request, f)
create = Popen(["kubectl", "--namespace=default", "create", "-f", filename], stdout=PIPE)
@ -204,7 +205,7 @@ spec:
services: "5"
""".format(name)
filename = "/tmp/{0}.yaml".format(name)
with open(filename, 'w') as f:
with salt.utils.fopen(filename, 'w') as f:
f.write(request)
create = Popen(["kubectl", "--namespace={0}".format(namespace), "create", "-f", filename], stdout=PIPE)
@ -238,7 +239,7 @@ spec:
services: "5"
""".format(name)
filename = "/tmp/{0}.yaml".format(name)
with open(filename, 'w') as f:
with salt.utils.fopen(filename, 'w') as f:
f.write(request)
create = Popen(["kubectl", "--namespace={0}".format(namespace), "create", "-f", filename], stdout=PIPE)
@ -285,7 +286,7 @@ spec:
services: "5"
""".format(name)
filename = "/tmp/{0}.yaml".format(name)
with open(filename, 'w') as f:
with salt.utils.fopen(filename, 'w') as f:
f.write(request)
create = Popen(["kubectl", "--namespace={0}".format(namespace), "create", "-f", filename], stdout=PIPE)
@ -351,7 +352,7 @@ spec:
}
}
filename = "/tmp/{0}.yaml".format(name)
with open(filename, 'w') as f:
with salt.utils.fopen(filename, 'w') as f:
f.write(request)
create = Popen(["kubectl", "--namespace=default", "create", "-f", filename], stdout=PIPE)
@ -389,7 +390,7 @@ spec:
type: Container
""".format(name)
filename = "/tmp/{0}.yaml".format(name)
with open(filename, 'w') as f:
with salt.utils.fopen(filename, 'w') as f:
f.write(request)
create = Popen(["kubectl", "--namespace=default", "create", "-f", filename], stdout=PIPE)

View file

@ -18,6 +18,7 @@ from tests.support.mock import (
# Import Salt Libs
import salt.utils
import salt.utils.odict
import salt.modules.seed as seed
@ -37,7 +38,7 @@ class SeedTestCase(TestCase, LoaderModuleMockMixin):
ddd['b'] = 'b'
ddd['a'] = 'b'
data = seed.mkconfig(ddd, approve_key=False)
with open(data['config']) as fic:
with salt.utils.fopen(data['config']) as fic:
fdata = fic.read()
self.assertEqual(fdata, 'b: b\na: b\nmaster: foo\n')

View file

@ -20,7 +20,11 @@ from tests.support.mock import (
)
# Import Salt libs
import salt.utils
import salt.modules.zypper as zypper
from salt.exceptions import CommandExecutionError
# Import 3rd-party libs
from salt.ext.six.moves import configparser
import salt.ext.six as six
@ -40,11 +44,14 @@ def get_test_data(filename):
'''
Return static test data
'''
return open(os.path.join(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'zypp'), filename)).read()
with salt.utils.fopen(
os.path.join(
os.path.join(
os.path.dirname(os.path.abspath(__file__)), 'zypp'), filename)) as rfh:
return rfh.read()
# Import Salt Libs
import salt.modules.zypper as zypper
@skipIf(NO_MOCK, NO_MOCK_REASON)

View file

@ -34,6 +34,7 @@ FILE_DATA = {
}
# Import Salt Libs
import salt.utils
from salt.pillar import Pillar
import salt.pillar.git_pillar as git_pillar
@ -80,7 +81,7 @@ class GitPillarTestCase(TestCase, AdaptedConfigurationTestCaseMixin, LoaderModul
os.makedirs(repo)
subprocess.check_call(['git', 'init', repo])
for filename in FILE_DATA:
with open(os.path.join(repo, filename), 'w') as data_file:
with salt.utils.fopen(os.path.join(repo, filename), 'w') as data_file:
yaml.dump(FILE_DATA[filename], data_file)
subprocess.check_call(['git', 'add', '.'], cwd=repo)

View file

@ -15,18 +15,19 @@ from tests.integration import AdaptedConfigurationTestCaseMixin
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import TestCase, skipIf
from tests.support.mock import NO_MOCK, NO_MOCK_REASON
from tests.support.paths import TMP
import tests.integration as integration
COMMIT_USER_NAME = 'test_user'
# file contents
PILLAR_CONTENT = {'gna': 'hello'}
FILE_DATA = {
'top.sls': {'base': {'*': ['user']}},
'user.sls': PILLAR_CONTENT
'top.sls': {'base': {'*': ['user']}},
'user.sls': PILLAR_CONTENT
}
# Import Salt Libs
import salt.utils
import salt.pillar.hg_pillar as hg_pillar
HGLIB = hg_pillar.hglib
@ -38,7 +39,7 @@ class HgPillarTestCase(TestCase, AdaptedConfigurationTestCaseMixin, LoaderModule
maxDiff = None
def setup_loader_modules(self):
self.tmpdir = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR)
self.tmpdir = tempfile.mkdtemp(dir=TMP)
self.addCleanup(shutil.rmtree, self.tmpdir)
cachedir = os.path.join(self.tmpdir, 'cachedir')
os.makedirs(os.path.join(cachedir, 'hg_pillar'))
@ -65,7 +66,7 @@ class HgPillarTestCase(TestCase, AdaptedConfigurationTestCaseMixin, LoaderModule
os.makedirs(hg_repo)
subprocess.check_call(['hg', 'init', hg_repo])
for filename in FILE_DATA:
with open(os.path.join(hg_repo, filename), 'w') as data_file:
with salt.utils.fopen(os.path.join(hg_repo, filename), 'w') as data_file:
yaml.dump(FILE_DATA[filename], data_file)
subprocess.check_call(['hg', 'ci', '-A', '-R', hg_repo, '-m', 'first commit', '-u', COMMIT_USER_NAME])
return hg_repo

View file

@ -11,6 +11,7 @@ from tests.support.unit import TestCase
from tests.support.helpers import destructiveTest
import salt.config
import salt.utils
import salt.spm
_TMP_SPM = tempfile.mkdtemp()
@ -109,7 +110,7 @@ class SPMTest(TestCase):
dirname, _ = os.path.split(path)
if not os.path.exists(dirname):
os.makedirs(dirname)
with open(path, 'w') as f:
with salt.utils.fopen(path, 'w') as f:
f.write(contents)
return fdir
@ -125,7 +126,8 @@ class SPMTest(TestCase):
for path, contents in _F1['contents']:
path = os.path.join(__opts__['file_roots']['base'][0], _F1['definition']['name'], path)
assert os.path.exists(path)
assert open(path, 'r').read() == contents
with salt.utils.fopen(path, 'r') as rfh:
assert rfh.read() == contents
# Check database
self.client.run(['info', _F1['definition']['name']])
lines = self.ui._status[-1].split('\n')

View file

@ -8,6 +8,7 @@ import os
from contextlib import contextmanager
import salt.utils
from salt.utils.process import clean_proc
import salt.utils.reactor as reactor
@ -44,7 +45,7 @@ class TestReactor(TestCase, AdaptedConfigurationTestCaseMixin):
self.opts = self.get_temp_config('master')
self.tempdir = tempfile.mkdtemp(dir=TMP)
self.sls_name = os.path.join(self.tempdir, 'test.sls')
with open(self.sls_name, 'w') as fh:
with salt.utils.fopen(self.sls_name, 'w') as fh:
fh.write('''
update_fileserver:
runner.fileserver.update