mirror of
https://github.com/valitydev/salt.git
synced 2024-11-08 01:18:58 +00:00
157 lines
5.3 KiB
Python
157 lines
5.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
'''
|
|
tests.unit.modules.archive_test
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
:codeauthor: :email:`Pedro Algarvio (pedro@algarvio.me)`
|
|
:copyright: © 2013 by the SaltStack Team, see AUTHORS for more details.
|
|
:license: Apache 2.0, see LICENSE for more details.
|
|
'''
|
|
|
|
# Import Salt Testing libs
|
|
from salttesting import skipIf, TestCase
|
|
from salttesting.helpers import ensure_in_syspath
|
|
ensure_in_syspath('../../')
|
|
|
|
# Import salt libs
|
|
from salt.modules import archive
|
|
from salt.exceptions import CommandNotFoundError
|
|
|
|
# Import 3rd party libs
|
|
try:
|
|
from mock import MagicMock, patch
|
|
HAS_MOCK = True
|
|
except ImportError:
|
|
HAS_MOCK = False
|
|
|
|
archive.__salt__ = {}
|
|
|
|
|
|
@skipIf(HAS_MOCK is False, 'mock python module is unavailable')
|
|
class ArchiveTestCase(TestCase):
|
|
|
|
@patch('salt.utils.which', lambda exe: exe)
|
|
def test_tar(self):
|
|
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'],
|
|
cwd=None, template=None
|
|
)
|
|
self.assertEqual(['salt'], ret)
|
|
mock.assert_called_once_with(
|
|
'tar -zcvf foo.tar /tmp/something-to-compress-1 '
|
|
'/tmp/something-to-compress-2',
|
|
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',
|
|
cwd=None, template=None
|
|
)
|
|
self.assertEqual(['salt'], ret)
|
|
mock.assert_called_once_with(
|
|
'tar -zcvf foo.tar /tmp/something-to-compress-1 '
|
|
'/tmp/something-to-compress-2',
|
|
cwd=None,
|
|
template=None
|
|
)
|
|
|
|
@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)
|
|
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
|
|
)
|
|
|
|
@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)
|
|
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
|
|
)
|
|
|
|
@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)
|
|
|
|
@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'
|
|
)
|
|
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)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
from integration import run_tests
|
|
run_tests(ArchiveTestCase, needs_daemon=False)
|