2015-02-09 14:52:46 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
:codeauthor: :email:`Alexander Schwartz <alexander.schwartz@gmx.net>`
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Import python libs
|
|
|
|
from __future__ import absolute_import
|
2015-03-17 23:24:02 +00:00
|
|
|
import os
|
|
|
|
import tempfile
|
2015-02-09 14:52:46 +00:00
|
|
|
|
|
|
|
# Import Salt Testing libs
|
|
|
|
from salttesting import TestCase, skipIf
|
|
|
|
from salttesting.helpers import ensure_in_syspath
|
|
|
|
from salttesting.mock import NO_MOCK, NO_MOCK_REASON, MagicMock, patch
|
|
|
|
|
|
|
|
ensure_in_syspath('../../')
|
|
|
|
|
2015-03-21 02:09:19 +00:00
|
|
|
# Import Salt Libs
|
2015-02-11 19:57:14 +00:00
|
|
|
from salt.states import archive as archive
|
2015-02-09 14:52:46 +00:00
|
|
|
|
|
|
|
# Globals
|
|
|
|
archive.__salt__ = {}
|
2015-02-11 19:57:14 +00:00
|
|
|
archive.__opts__ = {"cachedir": "/tmp", "test": False}
|
2015-03-17 23:24:02 +00:00
|
|
|
archive.__env__ = 'test'
|
2015-02-09 14:52:46 +00:00
|
|
|
|
2015-02-11 19:57:14 +00:00
|
|
|
|
2015-02-09 14:52:46 +00:00
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
|
|
|
class ArchiveTestCase(TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(ArchiveTestCase, self).setUp()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
super(ArchiveTestCase, self).tearDown()
|
|
|
|
|
2015-03-17 23:24:02 +00:00
|
|
|
def test_extracted_tar(self):
|
|
|
|
'''
|
|
|
|
archive.extracted tar options
|
|
|
|
'''
|
|
|
|
|
|
|
|
source = 'file.tar.gz'
|
|
|
|
tmp_dir = os.path.join(tempfile.gettempdir(), 'test_archive')
|
|
|
|
test_tar_opts = [
|
|
|
|
'--no-anchored foo',
|
|
|
|
'v -p --opt',
|
|
|
|
'-v -p',
|
|
|
|
'--long-opt -z',
|
|
|
|
'z -v -weird-long-opt arg',
|
|
|
|
]
|
|
|
|
ret_tar_opts = [
|
|
|
|
['tar', 'x', '--no-anchored', 'foo', '-f'],
|
|
|
|
['tar', 'xv', '-p', '--opt', '-f'],
|
|
|
|
['tar', 'x', '-v', '-p', '-f'],
|
|
|
|
['tar', 'x', '--long-opt', '-z', '-f'],
|
|
|
|
['tar', 'xz', '-v', '-weird-long-opt', 'arg', '-f'],
|
|
|
|
]
|
|
|
|
|
|
|
|
mock_true = MagicMock(return_value=True)
|
|
|
|
mock_false = MagicMock(return_value=False)
|
|
|
|
ret = {'stdout': ['saltines', 'cheese'], 'stderr': 'biscuits', 'retcode': '31337', 'pid': '1337'}
|
|
|
|
mock_run = MagicMock(return_value=ret)
|
|
|
|
|
|
|
|
with patch('os.path.exists', mock_true):
|
|
|
|
with patch.dict(archive.__opts__, {'test': False,
|
|
|
|
'cachedir': tmp_dir}):
|
|
|
|
with patch.dict(archive.__salt__, {'file.directory_exists': mock_false,
|
|
|
|
'file.file_exists': mock_false,
|
|
|
|
'file.makedirs': mock_true,
|
|
|
|
'cmd.run_all': mock_run}):
|
|
|
|
for test_opts, ret_opts in zip(test_tar_opts, ret_tar_opts):
|
|
|
|
ret = archive.extracted(tmp_dir,
|
|
|
|
source,
|
|
|
|
'tar',
|
|
|
|
tar_options=test_opts)
|
2015-03-20 19:06:16 +00:00
|
|
|
ret_opts.append(os.path.join(tmp_dir, 'files/test/_tmp_test_archive_.tar'))
|
2015-03-20 17:55:45 +00:00
|
|
|
mock_run.assert_called_with(ret_opts, python_shell=False, cwd=os.path.join(tmp_dir, ''))
|
2015-03-17 23:24:02 +00:00
|
|
|
|
2015-02-09 14:52:46 +00:00
|
|
|
def test_tar_gnutar(self):
|
|
|
|
'''
|
|
|
|
Tests the call of extraction with gnutar
|
|
|
|
'''
|
2015-02-11 19:57:14 +00:00
|
|
|
gnutar = MagicMock(return_value='tar (GNU tar)')
|
|
|
|
missing = MagicMock(return_value=False)
|
|
|
|
nop = MagicMock(return_value=True)
|
|
|
|
run_all = MagicMock(return_value={'retcode': 0, 'stdout': 'stdout', 'stderr': 'stderr'})
|
2015-02-09 14:52:46 +00:00
|
|
|
with patch.dict(archive.__salt__, {'cmd.run': gnutar, 'file.directory_exists': missing, 'file.file_exists': missing, 'state.single': nop, 'file.makedirs': nop, 'cmd.run_all': run_all}):
|
|
|
|
ret = archive.extracted('/tmp/out', '/tmp/foo.tar.gz', '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
|
|
|
|
'''
|
2015-02-11 19:57:14 +00:00
|
|
|
bsdtar = MagicMock(return_value='tar (bsdtar)')
|
|
|
|
missing = MagicMock(return_value=False)
|
|
|
|
nop = MagicMock(return_value=True)
|
|
|
|
run_all = MagicMock(return_value={'retcode': 0, 'stdout': 'stdout', 'stderr': 'stderr'})
|
2015-02-09 14:52:46 +00:00
|
|
|
with patch.dict(archive.__salt__, {'cmd.run': bsdtar, 'file.directory_exists': missing, 'file.file_exists': missing, 'state.single': nop, 'file.makedirs': nop, 'cmd.run_all': run_all}):
|
|
|
|
ret = archive.extracted('/tmp/out', '/tmp/foo.tar.gz', 'tar', tar_options='xvzf', keep=True)
|
|
|
|
self.assertEqual(ret['changes']['extracted_files'], 'stderr')
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
from integration import run_tests
|
|
|
|
run_tests(ArchiveTestCase, needs_daemon=False)
|