Skip to content

Commit

Permalink
Merge pull request #49164 from terminalmage/issue49154
Browse files Browse the repository at this point in the history
Fix bug in keep_source for non-templated salt:// file sources
  • Loading branch information
Mike Place committed Aug 18, 2018
2 parents b510441 + c2aba7a commit 0157eac
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
22 changes: 18 additions & 4 deletions salt/states/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2596,8 +2596,15 @@ def managed(name,
ret['changes'] = {}
log.debug(traceback.format_exc())
salt.utils.files.remove(tmp_filename)
if not keep_source and sfn:
salt.utils.files.remove(sfn)
if not keep_source:
if not sfn \
and source \
and _urlparse(source).scheme == 'salt':
# The file would not have been cached until manage_file was
# run, so check again here for a cached copy.
sfn = __salt__['cp.is_cached'](source, __env__)
if sfn:
salt.utils.files.remove(sfn)
return _error(ret, 'Unable to check_cmd file: {0}'.format(exc))

# file being updated to verify using check_cmd
Expand Down Expand Up @@ -2667,8 +2674,15 @@ def managed(name,
finally:
if tmp_filename:
salt.utils.files.remove(tmp_filename)
if not keep_source and sfn:
salt.utils.files.remove(sfn)
if not keep_source:
if not sfn \
and source \
and _urlparse(source).scheme == 'salt':
# The file would not have been cached until manage_file was
# run, so check again here for a cached copy.
sfn = __salt__['cp.is_cached'](source, __env__)
if sfn:
salt.utils.files.remove(sfn)


_RECURSE_TYPES = ['user', 'group', 'mode', 'ignore_files', 'ignore_dirs']
Expand Down
50 changes: 47 additions & 3 deletions tests/integration/states/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,29 @@ def test_managed_source_hash_indifferent_case(self):
if os.path.exists(name):
os.remove(name)

@with_tempfile
def test_managed_keep_source_false_salt(self, name):
'''
This test ensures that we properly clean the cached file if keep_source
is set to False, for source files using a salt:// URL
'''
source = 'salt://grail/scene33'
saltenv = 'base'

# Run the state
ret = self.run_state(
'file.managed',
name=name,
source=source,
saltenv=saltenv,
keep_source=False)
ret = ret[next(iter(ret))]
assert ret['result'] is True

# Now make sure that the file is not cached
result = self.run_function('cp.is_cached', [source, saltenv])
assert result == '', 'File is still cached at {0}'.format(result)

def test_directory(self):
'''
file.directory
Expand Down Expand Up @@ -3860,6 +3883,11 @@ def tearDown(self):
if exc.errno != errno.ENOENT:
raise exc

def run_state(self, *args, **kwargs):
ret = super(RemoteFileTest, self).run_state(*args, **kwargs)
log.debug('ret = %s', ret)
return ret

def test_file_managed_http_source_no_hash(self):
'''
Test a remote file with no hash
Expand All @@ -3868,7 +3896,6 @@ def test_file_managed_http_source_no_hash(self):
name=self.name,
source=self.source,
skip_verify=False)
log.debug('ret = %s', ret)
# This should fail because no hash was provided
self.assertSaltFalseReturn(ret)

Expand All @@ -3881,7 +3908,6 @@ def test_file_managed_http_source(self):
source=self.source,
source_hash=self.source_hash,
skip_verify=False)
log.debug('ret = %s', ret)
self.assertSaltTrueReturn(ret)

def test_file_managed_http_source_skip_verify(self):
Expand All @@ -3892,9 +3918,27 @@ def test_file_managed_http_source_skip_verify(self):
name=self.name,
source=self.source,
skip_verify=True)
log.debug('ret = %s', ret)
self.assertSaltTrueReturn(ret)

def test_file_managed_keep_source_false_http(self):
'''
This test ensures that we properly clean the cached file if keep_source
is set to False, for source files using an http:// URL
'''
# Run the state
ret = self.run_state('file.managed',
name=self.name,
source=self.source,
source_hash=self.source_hash,
keep_source=False)
ret = ret[next(iter(ret))]
assert ret['result'] is True

# Now make sure that the file is not cached
result = self.run_function('cp.is_cached', [self.source])
assert result == '', 'File is still cached at {0}'.format(result)


WIN_TEST_FILE = 'c:/testfile'


Expand Down

0 comments on commit 0157eac

Please sign in to comment.