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
This commit is contained in:
parent
acf09e56a2
commit
004c33d9c5
10 changed files with 140 additions and 54 deletions
2
changelogs/fragments/unarchive-fix-bin-checking.yml
Normal file
2
changelogs/fragments/unarchive-fix-bin-checking.yml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- unarchive - move failure for missing binary to ``can_handle_archive()`` rather than ``__init__()``
|
|
@ -290,14 +290,8 @@ class ZipArchive(object):
|
||||||
self.excludes = module.params['exclude']
|
self.excludes = module.params['exclude']
|
||||||
self.includes = []
|
self.includes = []
|
||||||
self.include_files = self.module.params['include']
|
self.include_files = self.module.params['include']
|
||||||
try:
|
self.cmd_path = None
|
||||||
self.cmd_path = get_bin_path('unzip')
|
self.zipinfo_cmd_path = None
|
||||||
except ValueError:
|
|
||||||
self.module.fail_json(msg="Unable to find required 'unzip' binary in the path")
|
|
||||||
try:
|
|
||||||
self.zipinfocmd_path = get_bin_path('zipinfo')
|
|
||||||
except ValueError:
|
|
||||||
self.module.fail_json(msg="Unable to find required 'zipinfo' binary in the path")
|
|
||||||
self._files_in_archive = []
|
self._files_in_archive = []
|
||||||
self._infodict = dict()
|
self._infodict = dict()
|
||||||
|
|
||||||
|
@ -387,7 +381,7 @@ class ZipArchive(object):
|
||||||
|
|
||||||
def is_unarchived(self):
|
def is_unarchived(self):
|
||||||
# BSD unzip doesn't support zipinfo listings with timestamp.
|
# BSD unzip doesn't support zipinfo listings with timestamp.
|
||||||
cmd = [self.zipinfocmd_path, '-T', '-s', self.src]
|
cmd = [self.zipinfo_cmd_path, '-T', '-s', self.src]
|
||||||
|
|
||||||
if self.excludes:
|
if self.excludes:
|
||||||
cmd.extend(['-x', ] + self.excludes)
|
cmd.extend(['-x', ] + self.excludes)
|
||||||
|
@ -708,8 +702,20 @@ class ZipArchive(object):
|
||||||
return dict(cmd=cmd, rc=rc, out=out, err=err)
|
return dict(cmd=cmd, rc=rc, out=out, err=err)
|
||||||
|
|
||||||
def can_handle_archive(self):
|
def can_handle_archive(self):
|
||||||
if not self.cmd_path:
|
binaries = (
|
||||||
return False, 'Command "unzip" not found.'
|
('unzip', 'cmd_path'),
|
||||||
|
('zipinfo', 'zipinfo_cmd_path'),
|
||||||
|
)
|
||||||
|
missing = []
|
||||||
|
for b in binaries:
|
||||||
|
try:
|
||||||
|
setattr(self, b[1], get_bin_path(b[0]))
|
||||||
|
except ValueError:
|
||||||
|
missing.append(b[0])
|
||||||
|
|
||||||
|
if missing:
|
||||||
|
return False, "Unable to find required '{missing}' binary in the path.".format(missing="' or '".join(missing))
|
||||||
|
|
||||||
cmd = [self.cmd_path, '-l', self.src]
|
cmd = [self.cmd_path, '-l', self.src]
|
||||||
rc, out, err = self.module.run_command(cmd)
|
rc, out, err = self.module.run_command(cmd)
|
||||||
if rc == 0:
|
if rc == 0:
|
||||||
|
@ -729,23 +735,11 @@ class TgzArchive(object):
|
||||||
self.module.exit_json(skipped=True, msg="remote module (%s) does not support check mode when using gtar" % self.module._name)
|
self.module.exit_json(skipped=True, msg="remote module (%s) does not support check mode when using gtar" % self.module._name)
|
||||||
self.excludes = [path.rstrip('/') for path in self.module.params['exclude']]
|
self.excludes = [path.rstrip('/') for path in self.module.params['exclude']]
|
||||||
self.include_files = self.module.params['include']
|
self.include_files = self.module.params['include']
|
||||||
# Prefer gtar (GNU tar) as it supports the compression options -z, -j and -J
|
self.cmd_path = None
|
||||||
try:
|
self.tar_type = None
|
||||||
self.cmd_path = get_bin_path('gtar')
|
|
||||||
except ValueError:
|
|
||||||
# Fallback to tar
|
|
||||||
try:
|
|
||||||
self.cmd_path = get_bin_path('tar')
|
|
||||||
except ValueError:
|
|
||||||
self.module.fail_json(msg="Unable to find required 'gtar' or 'tar' binary in the path")
|
|
||||||
self.zipflag = '-z'
|
self.zipflag = '-z'
|
||||||
self._files_in_archive = []
|
self._files_in_archive = []
|
||||||
|
|
||||||
if self.cmd_path:
|
|
||||||
self.tar_type = self._get_tar_type()
|
|
||||||
else:
|
|
||||||
self.tar_type = None
|
|
||||||
|
|
||||||
def _get_tar_type(self):
|
def _get_tar_type(self):
|
||||||
cmd = [self.cmd_path, '--version']
|
cmd = [self.cmd_path, '--version']
|
||||||
(rc, out, err) = self.module.run_command(cmd)
|
(rc, out, err) = self.module.run_command(cmd)
|
||||||
|
@ -873,6 +867,18 @@ class TgzArchive(object):
|
||||||
return dict(cmd=cmd, rc=rc, out=out, err=err)
|
return dict(cmd=cmd, rc=rc, out=out, err=err)
|
||||||
|
|
||||||
def can_handle_archive(self):
|
def can_handle_archive(self):
|
||||||
|
# Prefer gtar (GNU tar) as it supports the compression options -z, -j and -J
|
||||||
|
try:
|
||||||
|
self.cmd_path = get_bin_path('gtar')
|
||||||
|
except ValueError:
|
||||||
|
# Fallback to tar
|
||||||
|
try:
|
||||||
|
self.cmd_path = get_bin_path('tar')
|
||||||
|
except ValueError:
|
||||||
|
return False, "Unable to find required 'gtar' or 'tar' binary in the path"
|
||||||
|
|
||||||
|
self.tar_type = self._get_tar_type()
|
||||||
|
|
||||||
if self.tar_type != 'gnu':
|
if self.tar_type != 'gnu':
|
||||||
return False, 'Command "%s" detected as tar type %s. GNU tar required.' % (self.cmd_path, self.tar_type)
|
return False, 'Command "%s" detected as tar type %s. GNU tar required.' % (self.cmd_path, self.tar_type)
|
||||||
|
|
||||||
|
|
3
test/integration/targets/unarchive/handlers/main.yml
Normal file
3
test/integration/targets/unarchive/handlers/main.yml
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
- name: restore packages
|
||||||
|
package:
|
||||||
|
name: "{{ unarchive_packages }}"
|
|
@ -1,4 +1,5 @@
|
||||||
- import_tasks: prepare_tests.yml
|
- import_tasks: prepare_tests.yml
|
||||||
|
- import_tasks: test_missing_binaries.yml
|
||||||
- import_tasks: test_tar.yml
|
- import_tasks: test_tar.yml
|
||||||
- import_tasks: test_tar_gz.yml
|
- import_tasks: test_tar_gz.yml
|
||||||
- import_tasks: test_tar_gz_creates.yml
|
- import_tasks: test_tar_gz_creates.yml
|
||||||
|
|
|
@ -1,16 +1,10 @@
|
||||||
# Need unzip for unarchive module, and zip for archive creation.
|
- name: Include system specific variables
|
||||||
- name: Ensure zip & unzip are present
|
include_vars: "{{ ansible_facts.system }}.yml"
|
||||||
package:
|
|
||||||
name:
|
|
||||||
- zip
|
|
||||||
- unzip
|
|
||||||
when: ansible_pkg_mgr in ('yum', 'dnf', 'apt', 'pkgng')
|
|
||||||
|
|
||||||
- name: Ensure zstd is present, if available
|
# Need unzip for unarchive module, and zip for archive creation.
|
||||||
ignore_errors: true
|
- name: Ensure required binaries are present
|
||||||
package:
|
package:
|
||||||
name:
|
name: "{{ unarchive_packages }}"
|
||||||
- zstd
|
|
||||||
when: ansible_pkg_mgr in ('yum', 'dnf', 'apt', 'pkgng')
|
when: ansible_pkg_mgr in ('yum', 'dnf', 'apt', 'pkgng')
|
||||||
|
|
||||||
- name: prep our file
|
- name: prep our file
|
||||||
|
|
|
@ -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
|
1
test/integration/targets/unarchive/vars/Darwin.yml
Normal file
1
test/integration/targets/unarchive/vars/Darwin.yml
Normal file
|
@ -0,0 +1 @@
|
||||||
|
unarchive_packages: []
|
4
test/integration/targets/unarchive/vars/FreeBSD.yml
Normal file
4
test/integration/targets/unarchive/vars/FreeBSD.yml
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
unarchive_packages:
|
||||||
|
- unzip
|
||||||
|
- zip
|
||||||
|
- zstd
|
4
test/integration/targets/unarchive/vars/Linux.yml
Normal file
4
test/integration/targets/unarchive/vars/Linux.yml
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
unarchive_packages:
|
||||||
|
- tar
|
||||||
|
- unzip
|
||||||
|
- zip
|
|
@ -40,21 +40,31 @@ class FakeAnsibleModule:
|
||||||
|
|
||||||
|
|
||||||
class TestCaseZipArchive:
|
class TestCaseZipArchive:
|
||||||
def test_no_zip_binary(self, mocker, fake_ansible_module):
|
@pytest.mark.parametrize(
|
||||||
mocker.patch("ansible.modules.unarchive.get_bin_path", side_effect=ValueError)
|
'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 = {
|
fake_ansible_module.params = {
|
||||||
"extra_opts": "",
|
"extra_opts": "",
|
||||||
"exclude": "",
|
"exclude": "",
|
||||||
"include": "",
|
"include": "",
|
||||||
}
|
}
|
||||||
with pytest.raises(FailJson) as exec_info:
|
|
||||||
z = ZipArchive(
|
z = ZipArchive(
|
||||||
src="",
|
src="",
|
||||||
b_dest="",
|
b_dest="",
|
||||||
file_args="",
|
file_args="",
|
||||||
module=fake_ansible_module,
|
module=fake_ansible_module,
|
||||||
)
|
)
|
||||||
assert "Unable to find required" in exec_info.value.kwargs["msg"]
|
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:
|
class TestCaseTgzArchive:
|
||||||
|
@ -66,11 +76,16 @@ class TestCaseTgzArchive:
|
||||||
"include": "",
|
"include": "",
|
||||||
}
|
}
|
||||||
fake_ansible_module.check_mode = False
|
fake_ansible_module.check_mode = False
|
||||||
with pytest.raises(FailJson) as exec_info:
|
|
||||||
z = TgzArchive(
|
t = TgzArchive(
|
||||||
src="",
|
src="",
|
||||||
b_dest="",
|
b_dest="",
|
||||||
file_args="",
|
file_args="",
|
||||||
module=fake_ansible_module,
|
module=fake_ansible_module,
|
||||||
)
|
)
|
||||||
assert "Unable to find required" in exec_info.value.kwargs["msg"]
|
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 a new issue