2013-07-27 18:24:49 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
:codeauthor: :email:`Pedro Algarvio (pedro@algarvio.me)`
|
2013-09-16 16:24:00 +00:00
|
|
|
:copyright: © 2013 by the SaltStack Team, see AUTHORS for more details
|
2013-07-27 18:24:49 +00:00
|
|
|
:license: Apache 2.0, see LICENSE for more details.
|
2013-09-16 16:24:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
tests.unit.modules.archive_test
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2013-07-27 18:24:49 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
# Import Salt Testing libs
|
|
|
|
from salttesting import skipIf, TestCase
|
|
|
|
from salttesting.helpers import ensure_in_syspath
|
2013-08-26 09:45:22 +00:00
|
|
|
from salttesting.mock import NO_MOCK, NO_MOCK_REASON, MagicMock, patch
|
2013-07-27 18:24:49 +00:00
|
|
|
ensure_in_syspath('../../')
|
|
|
|
|
|
|
|
# Import salt libs
|
|
|
|
from salt.modules import archive
|
2013-07-29 22:29:58 +00:00
|
|
|
from salt.exceptions import CommandNotFoundError
|
2013-07-27 18:24:49 +00:00
|
|
|
|
|
|
|
archive.__salt__ = {}
|
|
|
|
|
|
|
|
|
2013-08-26 09:45:22 +00:00
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2013-07-27 18:24:49 +00:00
|
|
|
class ArchiveTestCase(TestCase):
|
2013-07-29 22:29:58 +00:00
|
|
|
|
|
|
|
@patch('salt.utils.which', lambda exe: exe)
|
2013-07-27 18:24:49 +00:00
|
|
|
def test_tar(self):
|
|
|
|
mock = MagicMock(return_value='salt')
|
|
|
|
with patch.dict(archive.__salt__, {'cmd.run': mock}):
|
2013-07-28 21:55:27 +00:00
|
|
|
ret = archive.tar(
|
|
|
|
'zcvf', 'foo.tar',
|
2013-07-29 05:57:27 +00:00
|
|
|
['/tmp/something-to-compress-1',
|
|
|
|
'/tmp/something-to-compress-2'],
|
|
|
|
cwd=None, template=None
|
|
|
|
)
|
|
|
|
self.assertEqual(['salt'], ret)
|
|
|
|
mock.assert_called_once_with(
|
|
|
|
'tar -zcvf foo.tar /tmp/something-to-compress-1 '
|
2013-07-28 21:55:27 +00:00
|
|
|
'/tmp/something-to-compress-2',
|
2013-07-29 05:57:27 +00:00
|
|
|
cwd=None,
|
|
|
|
template=None
|
|
|
|
)
|
|
|
|
|
|
|
|
mock = MagicMock(return_value='salt')
|
|
|
|
with patch.dict(archive.__salt__, {'cmd.run': mock}):
|
|
|
|
ret = archive.tar(
|
|
|
|
'zcvf', 'foo.tar',
|
|
|
|
'/tmp/something-to-compress-1,/tmp/something-to-compress-2',
|
2013-07-28 21:55:27 +00:00
|
|
|
cwd=None, template=None
|
|
|
|
)
|
2013-07-27 18:24:49 +00:00
|
|
|
self.assertEqual(['salt'], ret)
|
|
|
|
mock.assert_called_once_with(
|
2013-07-29 05:57:27 +00:00
|
|
|
'tar -zcvf foo.tar /tmp/something-to-compress-1 '
|
|
|
|
'/tmp/something-to-compress-2',
|
|
|
|
cwd=None,
|
2013-07-27 18:24:49 +00:00
|
|
|
template=None
|
|
|
|
)
|
|
|
|
|
2013-07-29 22:29:58 +00:00
|
|
|
@patch('salt.utils.which', lambda exe: None)
|
|
|
|
def test_tar_raises_exception_if_not_found(self):
|
|
|
|
mock = MagicMock(return_value='salt')
|
|
|
|
with patch.dict(archive.__salt__, {'cmd.run': mock}):
|
|
|
|
self.assertRaises(
|
|
|
|
CommandNotFoundError,
|
|
|
|
archive.tar,
|
|
|
|
'zxvf',
|
|
|
|
'foo.tar',
|
|
|
|
'/tmp/something-to-compress'
|
|
|
|
)
|
|
|
|
self.assertFalse(mock.called)
|
|
|
|
|
|
|
|
@patch('salt.utils.which', lambda exe: exe)
|
2013-07-28 11:50:05 +00:00
|
|
|
def test_gzip(self):
|
|
|
|
mock = MagicMock(return_value='salt')
|
|
|
|
with patch.dict(archive.__salt__, {'cmd.run': mock}):
|
|
|
|
ret = archive.gzip('/tmp/something-to-compress')
|
|
|
|
self.assertEqual(['salt'], ret)
|
|
|
|
mock.assert_called_once_with(
|
|
|
|
'gzip /tmp/something-to-compress',
|
|
|
|
template=None
|
|
|
|
)
|
|
|
|
|
2013-07-29 22:29:58 +00:00
|
|
|
@patch('salt.utils.which', lambda exe: None)
|
|
|
|
def test_gzip_raises_exception_if_not_found(self):
|
|
|
|
mock = MagicMock(return_value='salt')
|
|
|
|
with patch.dict(archive.__salt__, {'cmd.run': mock}):
|
|
|
|
self.assertRaises(
|
|
|
|
CommandNotFoundError,
|
|
|
|
archive.gzip, '/tmp/something-to-compress'
|
|
|
|
)
|
|
|
|
self.assertFalse(mock.called)
|
|
|
|
|
|
|
|
@patch('salt.utils.which', lambda exe: exe)
|
2013-07-28 11:51:13 +00:00
|
|
|
def test_gunzip(self):
|
|
|
|
mock = MagicMock(return_value='salt')
|
|
|
|
with patch.dict(archive.__salt__, {'cmd.run': mock}):
|
|
|
|
ret = archive.gunzip('/tmp/something-to-decompress.tar.gz')
|
|
|
|
self.assertEqual(['salt'], ret)
|
|
|
|
mock.assert_called_once_with(
|
|
|
|
'gunzip /tmp/something-to-decompress.tar.gz',
|
|
|
|
template=None
|
|
|
|
)
|
|
|
|
|
2013-07-29 22:29:58 +00:00
|
|
|
@patch('salt.utils.which', lambda exe: None)
|
|
|
|
def test_gunzip_raises_exception_if_not_found(self):
|
|
|
|
mock = MagicMock(return_value='salt')
|
|
|
|
with patch.dict(archive.__salt__, {'cmd.run': mock}):
|
|
|
|
self.assertRaises(
|
|
|
|
CommandNotFoundError,
|
|
|
|
archive.gunzip,
|
|
|
|
'/tmp/something-to-decompress.tar.gz'
|
|
|
|
)
|
|
|
|
self.assertFalse(mock.called)
|
|
|
|
|
2013-07-30 03:22:02 +00:00
|
|
|
@patch('salt.utils.which', lambda exe: exe)
|
|
|
|
def test_zip(self):
|
|
|
|
mock = MagicMock(return_value='salt')
|
|
|
|
with patch.dict(archive.__salt__, {'cmd.run': mock}):
|
|
|
|
ret = archive.zip_(
|
|
|
|
'/tmp/salt.{{grains.id}}.zip',
|
|
|
|
'/tmp/tmpePe8yO,/tmp/tmpLeSw1A',
|
|
|
|
template='jinja'
|
|
|
|
)
|
2013-07-30 04:01:39 +00:00
|
|
|
self.assertEqual(['salt'], ret)
|
|
|
|
mock.assert_called_once_with(
|
|
|
|
'zip /tmp/salt.{{grains.id}}.zip '
|
|
|
|
'/tmp/tmpePe8yO /tmp/tmpLeSw1A',
|
|
|
|
template='jinja'
|
|
|
|
)
|
|
|
|
|
|
|
|
mock = MagicMock(return_value='salt')
|
|
|
|
with patch.dict(archive.__salt__, {'cmd.run': mock}):
|
|
|
|
ret = archive.zip_(
|
|
|
|
'/tmp/salt.{{grains.id}}.zip',
|
|
|
|
['/tmp/tmpePe8yO', '/tmp/tmpLeSw1A'],
|
|
|
|
template='jinja'
|
|
|
|
)
|
2013-07-30 03:22:02 +00:00
|
|
|
self.assertEqual(['salt'], ret)
|
|
|
|
mock.assert_called_once_with(
|
|
|
|
'zip /tmp/salt.{{grains.id}}.zip '
|
|
|
|
'/tmp/tmpePe8yO /tmp/tmpLeSw1A',
|
|
|
|
template='jinja'
|
|
|
|
)
|
|
|
|
|
|
|
|
@patch('salt.utils.which', lambda exe: None)
|
|
|
|
def test_zip_raises_exception_if_not_found(self):
|
|
|
|
mock = MagicMock(return_value='salt')
|
|
|
|
with patch.dict(archive.__salt__, {'cmd.run': mock}):
|
|
|
|
self.assertRaises(
|
|
|
|
CommandNotFoundError,
|
|
|
|
archive.zip_,
|
|
|
|
'/tmp/salt.{{grains.id}}.zip',
|
|
|
|
'/tmp/tmpePe8yO,/tmp/tmpLeSw1A',
|
|
|
|
template='jinja',
|
|
|
|
)
|
|
|
|
self.assertFalse(mock.called)
|
|
|
|
|
2013-07-30 03:52:31 +00:00
|
|
|
@patch('salt.utils.which', lambda exe: exe)
|
|
|
|
def test_unzip(self):
|
|
|
|
mock = MagicMock(return_value='salt')
|
|
|
|
with patch.dict(archive.__salt__, {'cmd.run': mock}):
|
|
|
|
ret = archive.unzip(
|
|
|
|
'/tmp/salt.{{grains.id}}.zip',
|
|
|
|
'/tmp/dest',
|
|
|
|
excludes='/tmp/tmpePe8yO,/tmp/tmpLeSw1A',
|
|
|
|
template='jinja'
|
|
|
|
)
|
|
|
|
self.assertEqual(['salt'], ret)
|
|
|
|
mock.assert_called_once_with(
|
|
|
|
'unzip /tmp/salt.{{grains.id}}.zip -d /tmp/dest '
|
|
|
|
'-x /tmp/tmpePe8yO /tmp/tmpLeSw1A',
|
|
|
|
template='jinja'
|
|
|
|
)
|
|
|
|
|
2013-07-30 04:00:58 +00:00
|
|
|
mock = MagicMock(return_value='salt')
|
|
|
|
with patch.dict(archive.__salt__, {'cmd.run': mock}):
|
|
|
|
ret = archive.unzip(
|
|
|
|
'/tmp/salt.{{grains.id}}.zip',
|
|
|
|
'/tmp/dest',
|
|
|
|
excludes=['/tmp/tmpePe8yO', '/tmp/tmpLeSw1A'],
|
|
|
|
template='jinja'
|
|
|
|
)
|
|
|
|
self.assertEqual(['salt'], ret)
|
|
|
|
mock.assert_called_once_with(
|
|
|
|
'unzip /tmp/salt.{{grains.id}}.zip -d /tmp/dest '
|
|
|
|
'-x /tmp/tmpePe8yO /tmp/tmpLeSw1A',
|
|
|
|
template='jinja'
|
|
|
|
)
|
|
|
|
|
2014-01-28 08:41:45 +00:00
|
|
|
mock = MagicMock(return_value='salt')
|
|
|
|
with patch.dict(archive.__salt__, {'cmd.run': mock}):
|
|
|
|
ret = archive.unzip(
|
|
|
|
'/tmp/salt.{{grains.id}}.zip',
|
|
|
|
'/tmp/dest',
|
|
|
|
excludes='/tmp/tmpePe8yO,/tmp/tmpLeSw1A',
|
|
|
|
template='jinja',
|
|
|
|
options='fo'
|
|
|
|
)
|
|
|
|
self.assertEqual(['salt'], ret)
|
|
|
|
mock.assert_called_once_with(
|
|
|
|
'unzip -fo /tmp/salt.{{grains.id}}.zip -d /tmp/dest '
|
|
|
|
'-x /tmp/tmpePe8yO /tmp/tmpLeSw1A',
|
|
|
|
template='jinja',
|
|
|
|
)
|
|
|
|
|
|
|
|
mock = MagicMock(return_value='salt')
|
|
|
|
with patch.dict(archive.__salt__, {'cmd.run': mock}):
|
|
|
|
ret = archive.unzip(
|
|
|
|
'/tmp/salt.{{grains.id}}.zip',
|
|
|
|
'/tmp/dest',
|
|
|
|
excludes=['/tmp/tmpePe8yO', '/tmp/tmpLeSw1A'],
|
|
|
|
template='jinja',
|
|
|
|
options='fo'
|
|
|
|
)
|
|
|
|
self.assertEqual(['salt'], ret)
|
|
|
|
mock.assert_called_once_with(
|
|
|
|
'unzip -fo /tmp/salt.{{grains.id}}.zip -d /tmp/dest '
|
|
|
|
'-x /tmp/tmpePe8yO /tmp/tmpLeSw1A',
|
|
|
|
template='jinja'
|
|
|
|
)
|
|
|
|
|
2013-07-30 03:52:31 +00:00
|
|
|
@patch('salt.utils.which', lambda exe: None)
|
|
|
|
def test_unzip_raises_exception_if_not_found(self):
|
|
|
|
mock = MagicMock(return_value='salt')
|
|
|
|
with patch.dict(archive.__salt__, {'cmd.run': mock}):
|
|
|
|
self.assertRaises(
|
|
|
|
CommandNotFoundError,
|
|
|
|
archive.unzip,
|
|
|
|
'/tmp/salt.{{grains.id}}.zip',
|
|
|
|
'/tmp/dest',
|
|
|
|
excludes='/tmp/tmpePe8yO,/tmp/tmpLeSw1A',
|
|
|
|
template='jinja',
|
|
|
|
)
|
|
|
|
self.assertFalse(mock.called)
|
|
|
|
|
2013-07-30 03:59:28 +00:00
|
|
|
@patch('salt.utils.which', lambda exe: exe)
|
|
|
|
def test_rar(self):
|
|
|
|
mock = MagicMock(return_value='salt')
|
|
|
|
with patch.dict(archive.__salt__, {'cmd.run': mock}):
|
|
|
|
ret = archive.rar(
|
|
|
|
'/tmp/rarfile.rar',
|
|
|
|
'/tmp/sourcefile1,/tmp/sourcefile2'
|
|
|
|
)
|
|
|
|
self.assertEqual(['salt'], ret)
|
|
|
|
mock.assert_called_once_with(
|
|
|
|
'rar a -idp /tmp/rarfile.rar '
|
|
|
|
'/tmp/sourcefile1 /tmp/sourcefile2',
|
|
|
|
template=None
|
|
|
|
)
|
|
|
|
|
|
|
|
mock = MagicMock(return_value='salt')
|
|
|
|
with patch.dict(archive.__salt__, {'cmd.run': mock}):
|
|
|
|
ret = archive.rar(
|
|
|
|
'/tmp/rarfile.rar',
|
|
|
|
['/tmp/sourcefile1', '/tmp/sourcefile2']
|
|
|
|
)
|
|
|
|
self.assertEqual(['salt'], ret)
|
|
|
|
mock.assert_called_once_with(
|
|
|
|
'rar a -idp /tmp/rarfile.rar '
|
|
|
|
'/tmp/sourcefile1 /tmp/sourcefile2',
|
|
|
|
template=None
|
|
|
|
)
|
|
|
|
|
|
|
|
@patch('salt.utils.which', lambda exe: None)
|
|
|
|
def test_rar_raises_exception_if_not_found(self):
|
|
|
|
mock = MagicMock(return_value='salt')
|
|
|
|
with patch.dict(archive.__salt__, {'cmd.run': mock}):
|
|
|
|
self.assertRaises(
|
|
|
|
CommandNotFoundError,
|
|
|
|
archive.rar,
|
|
|
|
'/tmp/rarfile.rar',
|
|
|
|
'/tmp/sourcefile1,/tmp/sourcefile2'
|
|
|
|
)
|
|
|
|
self.assertFalse(mock.called)
|
|
|
|
|
2013-07-30 05:14:13 +00:00
|
|
|
@patch('salt.utils.which', lambda exe: exe)
|
2013-07-30 04:36:19 +00:00
|
|
|
@patch('salt.utils.which_bin', lambda exe: exe)
|
|
|
|
def test_unrar(self):
|
|
|
|
mock = MagicMock(return_value='salt')
|
|
|
|
with patch.dict(archive.__salt__, {'cmd.run': mock}):
|
|
|
|
ret = archive.unrar(
|
|
|
|
'/tmp/rarfile.rar',
|
|
|
|
'/home/strongbad/',
|
|
|
|
excludes='file_1,file_2'
|
|
|
|
)
|
|
|
|
self.assertEqual(['salt'], ret)
|
|
|
|
mock.assert_called_once_with(
|
2013-07-30 05:14:13 +00:00
|
|
|
'unrar x -idp /tmp/rarfile.rar '
|
|
|
|
'-x file_1 -x file_2 /home/strongbad/',
|
2013-07-30 04:36:19 +00:00
|
|
|
template=None
|
|
|
|
)
|
|
|
|
|
|
|
|
mock = MagicMock(return_value='salt')
|
|
|
|
with patch.dict(archive.__salt__, {'cmd.run': mock}):
|
|
|
|
ret = archive.unrar(
|
|
|
|
'/tmp/rarfile.rar',
|
|
|
|
'/home/strongbad/',
|
|
|
|
excludes=['file_1', 'file_2']
|
|
|
|
)
|
|
|
|
self.assertEqual(['salt'], ret)
|
|
|
|
mock.assert_called_once_with(
|
2013-07-30 05:14:13 +00:00
|
|
|
'unrar x -idp /tmp/rarfile.rar '
|
|
|
|
'-x file_1 -x file_2 /home/strongbad/',
|
2013-07-30 04:36:19 +00:00
|
|
|
template=None
|
|
|
|
)
|
|
|
|
|
|
|
|
@patch('salt.utils.which_bin', lambda exe: None)
|
|
|
|
def test_unrar_raises_exception_if_not_found(self):
|
|
|
|
mock = MagicMock(return_value='salt')
|
|
|
|
with patch.dict(archive.__salt__, {'cmd.run': mock}):
|
|
|
|
self.assertRaises(
|
|
|
|
CommandNotFoundError,
|
|
|
|
archive.unrar,
|
|
|
|
'/tmp/rarfile.rar',
|
|
|
|
'/home/strongbad/',
|
|
|
|
)
|
|
|
|
self.assertFalse(mock.called)
|
|
|
|
|
2013-07-27 18:24:49 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
from integration import run_tests
|
|
|
|
run_tests(ArchiveTestCase, needs_daemon=False)
|