mirror of
https://github.com/valitydev/salt.git
synced 2024-11-06 16:45:27 +00:00
Clean up filehandle usage in file state tests
This does a few things: 1. Change calls to open() to use salt.utils.fopen() 2. Use with blocks where applicable
This commit is contained in:
parent
f847bae5a2
commit
2939eaec15
@ -225,7 +225,7 @@ class FileTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):
|
|||||||
file.managed test interface
|
file.managed test interface
|
||||||
'''
|
'''
|
||||||
name = os.path.join(integration.TMP, 'grail_not_scene33')
|
name = os.path.join(integration.TMP, 'grail_not_scene33')
|
||||||
with open(name, 'wb') as fp_:
|
with salt.utils.fopen(name, 'wb') as fp_:
|
||||||
fp_.write('test_managed_show_diff_false\n')
|
fp_.write('test_managed_show_diff_false\n')
|
||||||
|
|
||||||
ret = self.run_state(
|
ret = self.run_state(
|
||||||
@ -444,10 +444,9 @@ class FileTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):
|
|||||||
template='jinja', defaults={'spam': _ts})
|
template='jinja', defaults={'spam': _ts})
|
||||||
try:
|
try:
|
||||||
self.assertSaltTrueReturn(ret)
|
self.assertSaltTrueReturn(ret)
|
||||||
self.assertIn(
|
with salt.utils.fopen(os.path.join(name, 'scene33'), 'r') as fp_:
|
||||||
_ts,
|
contents = fp_.read()
|
||||||
salt.utils.fopen(os.path.join(name, 'scene33'), 'r').read()
|
self.assertIn(_ts, contents)
|
||||||
)
|
|
||||||
finally:
|
finally:
|
||||||
shutil.rmtree(name, ignore_errors=True)
|
shutil.rmtree(name, ignore_errors=True)
|
||||||
|
|
||||||
@ -759,7 +758,8 @@ class FileTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):
|
|||||||
'state.sls', mods='testappend.issue-2227'
|
'state.sls', mods='testappend.issue-2227'
|
||||||
)
|
)
|
||||||
self.assertSaltTrueReturn(ret)
|
self.assertSaltTrueReturn(ret)
|
||||||
contents = salt.utils.fopen(tmp_file_append, 'r').read()
|
with salt.utils.fopen(tmp_file_append, 'r') as fp_:
|
||||||
|
contents_pre = fp_.read()
|
||||||
|
|
||||||
# It should not append text again
|
# It should not append text again
|
||||||
ret = self.run_function(
|
ret = self.run_function(
|
||||||
@ -767,10 +767,10 @@ class FileTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):
|
|||||||
)
|
)
|
||||||
self.assertSaltTrueReturn(ret)
|
self.assertSaltTrueReturn(ret)
|
||||||
|
|
||||||
self.assertEqual(
|
with salt.utils.fopen(tmp_file_append, 'r') as fp_:
|
||||||
contents, salt.utils.fopen(tmp_file_append, 'r').read()
|
contents_post = fp_.read()
|
||||||
)
|
|
||||||
|
|
||||||
|
self.assertEqual(contents_pre, contents_post)
|
||||||
except AssertionError:
|
except AssertionError:
|
||||||
shutil.copy(tmp_file_append, tmp_file_append + '.bak')
|
shutil.copy(tmp_file_append, tmp_file_append + '.bak')
|
||||||
raise
|
raise
|
||||||
@ -969,12 +969,15 @@ class FileTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):
|
|||||||
" - backup: '.bak2'",
|
" - backup: '.bak2'",
|
||||||
' - show_changes: True',
|
' - show_changes: True',
|
||||||
'']
|
'']
|
||||||
open(template_path, 'w').write(
|
with salt.utils.fopen(template_path, 'w') as fp_:
|
||||||
'\n'.join(sls_template).format(testcase_filedest))
|
fp_.write('\n'.join(sls_template).format(testcase_filedest))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ret = self.run_function('state.sls', mods='issue-8343')
|
ret = self.run_function('state.sls', mods='issue-8343')
|
||||||
for name, step in ret.items():
|
for name, step in ret.items():
|
||||||
self.assertSaltTrueReturn({name: step})
|
self.assertSaltTrueReturn({name: step})
|
||||||
|
with salt.utils.fopen(testcase_filedest) as fp_:
|
||||||
|
contents = fp_.read().split('\n')
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
['#-- start salt managed zonestart -- PLEASE, DO NOT EDIT',
|
['#-- start salt managed zonestart -- PLEASE, DO NOT EDIT',
|
||||||
'foo',
|
'foo',
|
||||||
@ -986,7 +989,7 @@ class FileTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):
|
|||||||
'',
|
'',
|
||||||
'#-- end salt managed zoneend --',
|
'#-- end salt managed zoneend --',
|
||||||
''],
|
''],
|
||||||
open(testcase_filedest).read().split('\n')
|
contents
|
||||||
)
|
)
|
||||||
finally:
|
finally:
|
||||||
if os.path.isdir(testcase_filedest):
|
if os.path.isdir(testcase_filedest):
|
||||||
@ -1033,12 +1036,15 @@ class FileTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):
|
|||||||
' - show_changes: True'
|
' - show_changes: True'
|
||||||
]
|
]
|
||||||
|
|
||||||
open(template_path, 'w').write(
|
with salt.utils.fopen(template_path, 'w') as fp_:
|
||||||
'\n'.join(sls_template).format(testcase_filedest))
|
fp_.write('\n'.join(sls_template).format(testcase_filedest))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ret = self.run_function('state.sls', mods='issue-11003')
|
ret = self.run_function('state.sls', mods='issue-11003')
|
||||||
for name, step in ret.items():
|
for name, step in ret.items():
|
||||||
self.assertSaltTrueReturn({name: step})
|
self.assertSaltTrueReturn({name: step})
|
||||||
|
with salt.utils.fopen(testcase_filedest) as fp_:
|
||||||
|
contents = fp_.read().split('\n')
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
['#',
|
['#',
|
||||||
'#-- start managed zone PLEASE, DO NOT EDIT',
|
'#-- start managed zone PLEASE, DO NOT EDIT',
|
||||||
@ -1048,7 +1054,7 @@ class FileTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):
|
|||||||
'',
|
'',
|
||||||
'#-- end managed zone',
|
'#-- end managed zone',
|
||||||
''],
|
''],
|
||||||
open(testcase_filedest).read().split('\n')
|
contents
|
||||||
)
|
)
|
||||||
finally:
|
finally:
|
||||||
if os.path.isdir(testcase_filedest):
|
if os.path.isdir(testcase_filedest):
|
||||||
@ -1120,8 +1126,8 @@ class FileTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):
|
|||||||
' - require:',
|
' - require:',
|
||||||
' - cmd: some-utf8-file-content-remove',
|
' - cmd: some-utf8-file-content-remove',
|
||||||
]
|
]
|
||||||
open(template_path, 'w').write(
|
with salt.utils.fopen(template_path, 'w') as fp_:
|
||||||
'\n'.join(template_lines))
|
fp_.write('\n'.join(template_lines))
|
||||||
try:
|
try:
|
||||||
ret = self.run_function('state.sls', mods='issue-8947')
|
ret = self.run_function('state.sls', mods='issue-8947')
|
||||||
if not isinstance(ret, dict):
|
if not isinstance(ret, dict):
|
||||||
@ -1301,19 +1307,22 @@ class FileTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):
|
|||||||
'''
|
'''
|
||||||
source = tempfile.mkstemp()[-1]
|
source = tempfile.mkstemp()[-1]
|
||||||
dest = tempfile.mkstemp()[-1]
|
dest = tempfile.mkstemp()[-1]
|
||||||
with open(source, 'w') as fp_:
|
with salt.utils.fopen(source, 'w') as fp_:
|
||||||
fp_.write('{{ foo }}\n')
|
fp_.write('{{ foo }}\n')
|
||||||
|
|
||||||
for prefix in ('file://', ''):
|
try:
|
||||||
self.assertSaltTrueReturn(
|
for prefix in ('file://', ''):
|
||||||
self.run_state(
|
ret = self.run_state(
|
||||||
'file.managed', name=dest, source=prefix + source,
|
'file.managed',
|
||||||
template='jinja', context={'foo': 'Hello world!'}
|
name=dest,
|
||||||
|
source=prefix + source,
|
||||||
|
template='jinja',
|
||||||
|
context={'foo': 'Hello world!'}
|
||||||
)
|
)
|
||||||
)
|
self.assertSaltTrueReturn(ret)
|
||||||
|
finally:
|
||||||
os.remove(source)
|
os.remove(source)
|
||||||
os.remove(dest)
|
os.remove(dest)
|
||||||
|
|
||||||
def test_template_local_file_noclobber(self):
|
def test_template_local_file_noclobber(self):
|
||||||
'''
|
'''
|
||||||
@ -1321,20 +1330,25 @@ class FileTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):
|
|||||||
and the source path is the same as the destination path.
|
and the source path is the same as the destination path.
|
||||||
'''
|
'''
|
||||||
source = tempfile.mkstemp()[-1]
|
source = tempfile.mkstemp()[-1]
|
||||||
with open(source, 'w') as fp_:
|
with salt.utils.fopen(source, 'w') as fp_:
|
||||||
fp_.write('{{ foo }}\n')
|
fp_.write('{{ foo }}\n')
|
||||||
|
|
||||||
ret = self.run_state(
|
try:
|
||||||
'file.managed', name=source, source=source, template='jinja',
|
ret = self.run_state(
|
||||||
context={'foo': 'Hello world!'}
|
'file.managed',
|
||||||
)
|
name=source,
|
||||||
self.assertSaltFalseReturn(ret)
|
source=source,
|
||||||
self.assertEqual(
|
template='jinja',
|
||||||
ret[next(iter(ret))]['comment'],
|
context={'foo': 'Hello world!'}
|
||||||
('Unable to manage file: Source file cannot be the same as '
|
)
|
||||||
'destination')
|
self.assertSaltFalseReturn(ret)
|
||||||
)
|
self.assertEqual(
|
||||||
os.remove(source)
|
ret[next(iter(ret))]['comment'],
|
||||||
|
('Unable to manage file: Source file cannot be the same as '
|
||||||
|
'destination')
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
os.remove(source)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
from integration import run_tests
|
from integration import run_tests
|
||||||
|
Loading…
Reference in New Issue
Block a user