mirror of https://github.com/ansible/ansible.git
[bp-2.11] unarchive - do not fail in init when trying to find required binary (#75363)
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
(cherry picked from commit 004c33d9c5
)
Co-authored-by: Sam Doran <sdoran@redhat.com>
pull/75418/head
parent
71a6954b51
commit
7f6415ee14
@ -0,0 +1,2 @@
|
||||
bugfixes:
|
||||
- unarchive - move failure for missing binary to ``can_handle_archive()`` rather than ``__init__()``
|
@ -0,0 +1,3 @@
|
||||
- name: restore packages
|
||||
package:
|
||||
name: "{{ unarchive_packages }}"
|
@ -0,0 +1,56 @@
|
||||
- name: Test missing binaries
|
||||
when: ansible_pkg_mgr in ('yum', 'dnf', 'apt', 'pkgng')
|
||||
block:
|
||||
- name: Remove zip binaries
|
||||
package:
|
||||
state: absent
|
||||
name:
|
||||
- zip
|
||||
- unzip
|
||||
notify: restore packages
|
||||
|
||||
- name: create unarchive destinations
|
||||
file:
|
||||
path: '{{ remote_tmp_dir }}/test-unarchive-{{ item }}'
|
||||
state: directory
|
||||
loop:
|
||||
- zip
|
||||
- tar
|
||||
|
||||
# With the zip binaries absent and tar still present, this task should work
|
||||
- name: unarchive a tar file
|
||||
unarchive:
|
||||
src: '{{remote_tmp_dir}}/test-unarchive.tar'
|
||||
dest: '{{remote_tmp_dir}}/test-unarchive-tar'
|
||||
remote_src: yes
|
||||
register: tar
|
||||
|
||||
- name: unarchive a zip file
|
||||
unarchive:
|
||||
src: '{{remote_tmp_dir}}/test-unarchive.zip'
|
||||
dest: '{{remote_tmp_dir}}/test-unarchive-zip'
|
||||
list_files: True
|
||||
remote_src: yes
|
||||
register: zip_fail
|
||||
ignore_errors: yes
|
||||
|
||||
- name: Ensure tasks worked as expected
|
||||
assert:
|
||||
that:
|
||||
- tar is success
|
||||
- zip_fail is failed
|
||||
- zip_fail.msg is search('Unable to find required')
|
||||
|
||||
- name: Remove unarchive destinations
|
||||
file:
|
||||
path: '{{ remote_tmp_dir }}/test-unarchive-{{ item }}'
|
||||
state: absent
|
||||
loop:
|
||||
- zip
|
||||
- tar
|
||||
|
||||
- name: Reinsntall zip binaries
|
||||
package:
|
||||
name:
|
||||
- zip
|
||||
- unzip
|
@ -0,0 +1 @@
|
||||
unarchive_packages: []
|
@ -0,0 +1,4 @@
|
||||
unarchive_packages:
|
||||
- unzip
|
||||
- zip
|
||||
- zstd
|
@ -0,0 +1,4 @@
|
||||
unarchive_packages:
|
||||
- tar
|
||||
- unzip
|
||||
- zip
|
@ -0,0 +1,91 @@
|
||||
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
|
Loading…
Reference in New Issue