Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

archive: do not crash when lzma is not around #5393

Merged
merged 1 commit into from Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/5393-archive.yml
@@ -0,0 +1,2 @@
bugfixes:
- "archive - avoid crash when ``lzma`` is not present and ``format`` is not ``xz`` (https://github.com/ansible-collections/community.general/pull/5393)."
7 changes: 6 additions & 1 deletion plugins/modules/files/archive.py
Expand Up @@ -575,6 +575,11 @@ def py26_filter(path):
self.file.add(path, archive_name, recursive=False, exclude=py26_filter)

def _get_checksums(self, path):
if HAS_LZMA:
LZMAError = lzma.LZMAError
else:
# Just picking another exception that's also listed below
LZMAError = tarfile.ReadError
try:
if self.format == 'xz':
with lzma.open(_to_native_ascii(path), 'r') as f:
Expand All @@ -585,7 +590,7 @@ def _get_checksums(self, path):
archive = tarfile.open(_to_native_ascii(path), 'r|' + self.format)
checksums = set((info.name, info.chksum) for info in archive.getmembers())
archive.close()
except (lzma.LZMAError, tarfile.ReadError, tarfile.CompressionError):
except (LZMAError, tarfile.ReadError, tarfile.CompressionError):
try:
# The python implementations of gzip, bz2, and lzma do not support restoring compressed files
# to their original names so only file checksum is returned
Expand Down
40 changes: 31 additions & 9 deletions tests/integration/targets/archive/tasks/main.yml
Expand Up @@ -12,6 +12,37 @@
# Make sure we start fresh

# Test setup
- name: prep our files
copy: src={{ item }} dest={{remote_tmp_dir}}/{{ item }}
with_items:
- foo.txt
- bar.txt
- empty.txt
- sub
- sub/subfile.txt

# Run twice without lzma backport installed, to make sure it does not crash
- name: Archive - pre-test - first run
archive:
path: "{{ remote_tmp_dir }}/*.txt"
dest: "{{ remote_tmp_dir }}/archive_pretest_1.tar"
format: "tar"
register: pretest_1

- name: Archive - pre-test - second run
archive:
path: "{{ remote_tmp_dir }}/*.txt"
dest: "{{ remote_tmp_dir }}/archive_pretest_1.tar"
format: "tar"
register: pretest_2

- name: Archive - validate pre-test
assert:
that:
- pretest_1 is changed
- pretest_2 is not changed

# Install dependencies
- name: Ensure zip is present to create test archive (yum)
yum: name=zip state=latest
when: ansible_facts.pkg_mgr == 'yum'
Expand Down Expand Up @@ -63,15 +94,6 @@
when: ansible_python_version.split('.')[0] == '2'
register: backports_lzma_pip

- name: prep our files
copy: src={{ item }} dest={{remote_tmp_dir}}/{{ item }}
with_items:
- foo.txt
- bar.txt
- empty.txt
- sub
- sub/subfile.txt

- name: Define formats to test
set_fact:
formats:
Expand Down