ansible/test/units/modules/test_unarchive.py
Sam Doran 004c33d9c5
unarchive - do not fail in init when trying to find required binary (#74892)
Test for the required binaries in the can_handle_archive() method and fail there. This
prevents failures for missing binaries unrelated to the archive type.

* Update missing zip binary message to match tar message
* Update unit tests
* Add integration tests
* Define packages based on the system rather than ignoring failures
2021-06-07 12:59:06 -04:00

91 lines
2.3 KiB
Python

from __future__ import absolute_import, division, print_function
__metaclass__ = type
import pytest
from ansible.modules.unarchive import ZipArchive, TgzArchive
class AnsibleModuleExit(Exception):
def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
class ExitJson(AnsibleModuleExit):
pass
class FailJson(AnsibleModuleExit):
pass
@pytest.fixture
def fake_ansible_module():
return FakeAnsibleModule()
class FakeAnsibleModule:
def __init__(self):
self.params = {}
self.tmpdir = None
def exit_json(self, *args, **kwargs):
raise ExitJson(*args, **kwargs)
def fail_json(self, *args, **kwargs):
raise FailJson(*args, **kwargs)
class TestCaseZipArchive:
@pytest.mark.parametrize(
'side_effect, expected_reason', (
([ValueError, '/bin/zipinfo'], "Unable to find required 'unzip'"),
(ValueError, "Unable to find required 'unzip' or 'zipinfo'"),
)
)
def test_no_zip_zipinfo_binary(self, mocker, fake_ansible_module, side_effect, expected_reason):
mocker.patch("ansible.modules.unarchive.get_bin_path", side_effect=side_effect)
fake_ansible_module.params = {
"extra_opts": "",
"exclude": "",
"include": "",
}
z = ZipArchive(
src="",
b_dest="",
file_args="",
module=fake_ansible_module,
)
can_handle, reason = z.can_handle_archive()
assert can_handle is False
assert expected_reason in reason
assert z.cmd_path is None
class TestCaseTgzArchive:
def test_no_tar_binary(self, mocker, fake_ansible_module):
mocker.patch("ansible.modules.unarchive.get_bin_path", side_effect=ValueError)
fake_ansible_module.params = {
"extra_opts": "",
"exclude": "",
"include": "",
}
fake_ansible_module.check_mode = False
t = TgzArchive(
src="",
b_dest="",
file_args="",
module=fake_ansible_module,
)
can_handle, reason = t.can_handle_archive()
assert can_handle is False
assert 'Unable to find required' in reason
assert t.cmd_path is None
assert t.tar_type is None