mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Merge pull request #38288 from terminalmage/archive-extracted-local-source-2016.3
archive.extracted: don't try to cache local sources (2016.3 branch)
This commit is contained in:
commit
09d9cff992
2 changed files with 81 additions and 42 deletions
|
@ -291,6 +291,14 @@ def extracted(name,
|
|||
urlparsed_source = _urlparse(source_match)
|
||||
source_hash_name = urlparsed_source.path or urlparsed_source.netloc
|
||||
|
||||
source_is_local = urlparsed_source.scheme in ('', 'file')
|
||||
if source_is_local:
|
||||
# Get rid of "file://" from start of source_match
|
||||
source_match = urlparsed_source.path
|
||||
if not os.path.isfile(source_match):
|
||||
ret['comment'] = 'Source file \'{0}\' does not exist'.format(source_match)
|
||||
return ret
|
||||
|
||||
if if_missing is None:
|
||||
if_missing = name
|
||||
if source_hash and source_hash_update:
|
||||
|
@ -326,13 +334,16 @@ def extracted(name,
|
|||
return ret
|
||||
|
||||
log.debug('Input seem valid so far')
|
||||
filename = os.path.join(__opts__['cachedir'],
|
||||
'files',
|
||||
__env__,
|
||||
'{0}.{1}'.format(re.sub('[:/\\\\]', '_', if_missing),
|
||||
archive_format))
|
||||
if source_is_local:
|
||||
filename = source_match
|
||||
else:
|
||||
filename = os.path.join(
|
||||
__opts__['cachedir'],
|
||||
'files',
|
||||
__env__,
|
||||
'{0}.{1}'.format(re.sub('[:/\\\\]', '_', if_missing), archive_format))
|
||||
|
||||
if not os.path.exists(filename):
|
||||
if not source_is_local and not os.path.isfile(filename):
|
||||
if __opts__['test']:
|
||||
ret['result'] = None
|
||||
ret['comment'] = \
|
||||
|
@ -527,7 +538,7 @@ def extracted(name,
|
|||
ret['changes']['directories_created'] = [name]
|
||||
ret['changes']['extracted_files'] = files
|
||||
ret['comment'] = '{0} extracted to {1}'.format(source_match, name)
|
||||
if not keep:
|
||||
if not source_is_local and not keep:
|
||||
os.unlink(filename)
|
||||
if source_hash and source_hash_update:
|
||||
_update_checksum(hash_fname, name, hash[1])
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
# Import python libs
|
||||
from __future__ import absolute_import
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
# Import Salt Testing libs
|
||||
from salttesting import TestCase, skipIf
|
||||
|
@ -30,6 +29,23 @@ archive.__opts__ = {"cachedir": "/tmp", "test": False}
|
|||
archive.__env__ = 'test'
|
||||
|
||||
|
||||
def _isfile_side_effect(path):
|
||||
'''
|
||||
MagicMock side_effect for os.path.isfile(). We don't want to use dict.get
|
||||
here because we want the test to fail if there's a path we haven't
|
||||
accounted for, so that we can add it.
|
||||
|
||||
NOTE: This may fall over on some platforms if /usr/bin/tar does not exist.
|
||||
If so, just add an entry in the dictionary for the path being used for tar.
|
||||
'''
|
||||
return {
|
||||
'/tmp/foo.tar.gz': True,
|
||||
'/tmp/out': False,
|
||||
'/usr/bin/tar': True,
|
||||
'/tmp/test_extracted_tar': False,
|
||||
}[path]
|
||||
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
class ArchiveTestCase(TestCase):
|
||||
|
||||
|
@ -44,8 +60,8 @@ class ArchiveTestCase(TestCase):
|
|||
archive.extracted tar options
|
||||
'''
|
||||
|
||||
source = 'file.tar.gz'
|
||||
tmp_dir = os.path.join(tempfile.gettempdir(), 'test_archive', '')
|
||||
source = '/tmp/foo.tar.gz'
|
||||
tmp_dir = '/tmp/test_extracted_tar'
|
||||
test_tar_opts = [
|
||||
'--no-anchored foo',
|
||||
'v -p --opt',
|
||||
|
@ -65,7 +81,8 @@ class ArchiveTestCase(TestCase):
|
|||
mock_false = MagicMock(return_value=False)
|
||||
ret = {'stdout': ['saltines', 'cheese'], 'stderr': 'biscuits', 'retcode': '31337', 'pid': '1337'}
|
||||
mock_run = MagicMock(return_value=ret)
|
||||
mock_source_list = MagicMock(return_value=source)
|
||||
mock_source_list = MagicMock(return_value=(source, None))
|
||||
isfile_mock = MagicMock(side_effect=_isfile_side_effect)
|
||||
|
||||
with patch('os.path.exists', mock_true):
|
||||
with patch.dict(archive.__opts__, {'test': False,
|
||||
|
@ -75,59 +92,70 @@ class ArchiveTestCase(TestCase):
|
|||
'file.makedirs': mock_true,
|
||||
'cmd.run_all': mock_run,
|
||||
'file.source_list': mock_source_list}):
|
||||
filename = os.path.join(
|
||||
tmp_dir,
|
||||
'files/test/_tmp_test_archive_.tar'
|
||||
)
|
||||
for test_opts, ret_opts in zip(test_tar_opts, ret_tar_opts):
|
||||
ret = archive.extracted(tmp_dir,
|
||||
source,
|
||||
'tar',
|
||||
tar_options=test_opts)
|
||||
ret_opts.append(filename)
|
||||
mock_run.assert_called_with(ret_opts, cwd=tmp_dir, python_shell=False)
|
||||
with patch.object(os.path, 'isfile', isfile_mock):
|
||||
for test_opts, ret_opts in zip(test_tar_opts, ret_tar_opts):
|
||||
ret = archive.extracted(tmp_dir,
|
||||
source,
|
||||
archive_format='tar',
|
||||
tar_options=test_opts)
|
||||
ret_opts.append(source)
|
||||
mock_run.assert_called_with(ret_opts,
|
||||
cwd=tmp_dir + os.sep,
|
||||
python_shell=False)
|
||||
|
||||
def test_tar_gnutar(self):
|
||||
'''
|
||||
Tests the call of extraction with gnutar
|
||||
'''
|
||||
gnutar = MagicMock(return_value='tar (GNU tar)')
|
||||
source = 'GNU tar'
|
||||
missing = MagicMock(return_value=False)
|
||||
nop = MagicMock(return_value=True)
|
||||
source = '/tmp/foo.tar.gz'
|
||||
mock_false = MagicMock(return_value=False)
|
||||
mock_true = MagicMock(return_value=True)
|
||||
run_all = MagicMock(return_value={'retcode': 0, 'stdout': 'stdout', 'stderr': 'stderr'})
|
||||
mock_source_list = MagicMock(return_value=source)
|
||||
mock_source_list = MagicMock(return_value=(source, None))
|
||||
isfile_mock = MagicMock(side_effect=_isfile_side_effect)
|
||||
|
||||
with patch.dict(archive.__salt__, {'cmd.run': gnutar,
|
||||
'file.directory_exists': missing,
|
||||
'file.file_exists': missing,
|
||||
'state.single': nop,
|
||||
'file.makedirs': nop,
|
||||
'file.directory_exists': mock_false,
|
||||
'file.file_exists': mock_false,
|
||||
'state.single': mock_true,
|
||||
'file.makedirs': mock_true,
|
||||
'cmd.run_all': run_all,
|
||||
'file.source_list': mock_source_list}):
|
||||
ret = archive.extracted('/tmp/out', '/tmp/foo.tar.gz', 'tar', tar_options='xvzf', keep=True)
|
||||
self.assertEqual(ret['changes']['extracted_files'], 'stdout')
|
||||
with patch.object(os.path, 'isfile', isfile_mock):
|
||||
ret = archive.extracted('/tmp/out',
|
||||
'/tmp/foo.tar.gz',
|
||||
archive_format='tar',
|
||||
tar_options='xvzf',
|
||||
keep=True)
|
||||
self.assertEqual(ret['changes']['extracted_files'], 'stdout')
|
||||
|
||||
def test_tar_bsdtar(self):
|
||||
'''
|
||||
Tests the call of extraction with bsdtar
|
||||
'''
|
||||
bsdtar = MagicMock(return_value='tar (bsdtar)')
|
||||
source = 'bsdtar'
|
||||
missing = MagicMock(return_value=False)
|
||||
nop = MagicMock(return_value=True)
|
||||
source = '/tmp/foo.tar.gz'
|
||||
mock_false = MagicMock(return_value=False)
|
||||
mock_true = MagicMock(return_value=True)
|
||||
run_all = MagicMock(return_value={'retcode': 0, 'stdout': 'stdout', 'stderr': 'stderr'})
|
||||
mock_source_list = MagicMock(return_value=source)
|
||||
mock_source_list = MagicMock(return_value=(source, None))
|
||||
isfile_mock = MagicMock(side_effect=_isfile_side_effect)
|
||||
|
||||
with patch.dict(archive.__salt__, {'cmd.run': bsdtar,
|
||||
'file.directory_exists': missing,
|
||||
'file.file_exists': missing,
|
||||
'state.single': nop,
|
||||
'file.makedirs': nop,
|
||||
'file.directory_exists': mock_false,
|
||||
'file.file_exists': mock_false,
|
||||
'state.single': mock_true,
|
||||
'file.makedirs': mock_true,
|
||||
'cmd.run_all': run_all,
|
||||
'file.source_list': mock_source_list}):
|
||||
ret = archive.extracted('/tmp/out', '/tmp/foo.tar.gz', 'tar', tar_options='xvzf', keep=True)
|
||||
self.assertEqual(ret['changes']['extracted_files'], 'stderr')
|
||||
with patch.object(os.path, 'isfile', isfile_mock):
|
||||
ret = archive.extracted('/tmp/out',
|
||||
'/tmp/foo.tar.gz',
|
||||
archive_format='tar',
|
||||
tar_options='xvzf',
|
||||
keep=True)
|
||||
self.assertEqual(ret['changes']['extracted_files'], 'stderr')
|
||||
|
||||
if __name__ == '__main__':
|
||||
from integration import run_tests
|
||||
|
|
Loading…
Add table
Reference in a new issue