mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 21:00:22 -07:00
Add decompress module (#9175)
* adds simple implementation of `decompress` module * adds simple test, fixes src and dest arg types * minor refactoring * adds support for common file operations adds integration test for gz decompressing * makes tests parametrized to test all supported compression formats * checks that target file exists * writes to decompressed file now uses atomic_move * adds idempotency for decompression * refactoring, removed classes * adds support for check mode * adds check for destination file. If it exists and it is a directory, the module returns error * refactoring, moves code to a class. Also, simplifies tests (now only tests related to the module core functionality run as parametrized, tests for idempotency and check mode run only for one format) * adds 'remove' parameter that deletes original compressed file after decompression * adds documentation * fixes bug with 'remove' parameter in check mode * makes dest argument not required. Dest filename now can be produced from the src filename * adds dest to output * updates the documentation, adds "RETURN" block * fixes test * adds support for python2 * removes some of the test files that can be generated during testing. Adds copyright header to test files * adds maintainer * apply minor suggestions from code review Co-authored-by: Felix Fontein <felix@fontein.de> * fixes code review comments (idempotency issue with non existing src, existing dest and remove=true; fixes the issue and adds test) * refactors the module to use ModuleHelper * refactors lzma dependency manual check to use 'deps.validate' * minor fix * removes registered handlers check * minor refactoring * adds aliases * changes setup for tests * tests: ignores macos and fixes tests for FreeBSD * tests: reverts ignore for macos and fixes issue with centos7 * tests: adds liblzma dependency for python2 * tests: adds backports.lzma * fixes bz2 decompression for python2 * tests: install xz for osx * tests: install xz for osx (2) * fixes code review comments --------- Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
parent
f2dbe08d0e
commit
41b6a281e1
12 changed files with 529 additions and 0 deletions
7
tests/integration/targets/decompress/aliases
Normal file
7
tests/integration/targets/decompress/aliases
Normal file
|
@ -0,0 +1,7 @@
|
|||
# Copyright (c) Ansible Project
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
azp/posix/2
|
||||
needs/root
|
||||
destructive
|
5
tests/integration/targets/decompress/files/file.txt
Normal file
5
tests/integration/targets/decompress/files/file.txt
Normal file
|
@ -0,0 +1,5 @@
|
|||
# Copyright (c) Ansible Project
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
This is sample file
|
|
@ -0,0 +1,5 @@
|
|||
# Copyright (c) Ansible Project
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
Content of this file must differ from the 'file.txt'
|
9
tests/integration/targets/decompress/handlers/main.yml
Normal file
9
tests/integration/targets/decompress/handlers/main.yml
Normal file
|
@ -0,0 +1,9 @@
|
|||
---
|
||||
# Copyright (c) Ansible Project
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
- name: delete backports.lzma
|
||||
pip:
|
||||
name: backports.lzma
|
||||
state: absent
|
7
tests/integration/targets/decompress/meta/main.yml
Normal file
7
tests/integration/targets/decompress/meta/main.yml
Normal file
|
@ -0,0 +1,7 @@
|
|||
---
|
||||
# Copyright (c) Ansible Project
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
dependencies:
|
||||
- setup_remote_tmp_dir
|
12
tests/integration/targets/decompress/tasks/cleanup.yml
Normal file
12
tests/integration/targets/decompress/tasks/cleanup.yml
Normal file
|
@ -0,0 +1,12 @@
|
|||
---
|
||||
# Copyright (c) Ansible Project
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
- name: Delete decompressed files
|
||||
file:
|
||||
path: "{{ remote_tmp_dir }}/file_from_{{ format }}.txt"
|
||||
state: absent
|
||||
loop: "{{ formats }}"
|
||||
loop_control:
|
||||
loop_var: format
|
29
tests/integration/targets/decompress/tasks/core.yml
Normal file
29
tests/integration/targets/decompress/tasks/core.yml
Normal file
|
@ -0,0 +1,29 @@
|
|||
---
|
||||
# Copyright (c) Ansible Project
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
- name: Set mode for decompressed file ({{ format }} test)
|
||||
set_fact:
|
||||
decompressed_mode: "0640"
|
||||
|
||||
- name: Simple decompress ({{ format }} test)
|
||||
decompress:
|
||||
src: "{{ remote_tmp_dir }}/file.txt.{{ format }}"
|
||||
dest: "{{ remote_tmp_dir }}/file_from_{{ format }}.txt"
|
||||
format: "{{ format }}"
|
||||
mode: "{{ decompressed_mode }}"
|
||||
register: first_decompression
|
||||
|
||||
- name: Stat decompressed file ({{ format }} test)
|
||||
stat:
|
||||
path: "{{ remote_tmp_dir }}/file_from_{{ format }}.txt"
|
||||
register: decompressed_file_stat
|
||||
|
||||
- name: Check that file was decompressed correctly ({{ format }} test)
|
||||
assert:
|
||||
that:
|
||||
- first_decompression.changed
|
||||
- decompressed_file_stat.stat.exists
|
||||
- decompressed_file_stat.stat.mode == decompressed_mode
|
||||
- orig_file_stat.stat.checksum == decompressed_file_stat.stat.checksum
|
51
tests/integration/targets/decompress/tasks/dest.yml
Normal file
51
tests/integration/targets/decompress/tasks/dest.yml
Normal file
|
@ -0,0 +1,51 @@
|
|||
---
|
||||
# Copyright (c) Ansible Project
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
- name: Copy a compressed file
|
||||
copy:
|
||||
src: "{{ item.orig }}"
|
||||
dest: "{{ item.new }}"
|
||||
remote_src: true
|
||||
loop:
|
||||
- { orig: "{{ remote_tmp_dir }}/file.txt.gz", new: "{{ remote_tmp_dir }}/dest.txt.gz" }
|
||||
- { orig: "{{ remote_tmp_dir }}/file.txt.gz", new: "{{ remote_tmp_dir }}/dest" }
|
||||
|
||||
- name: Decompress a file without specifying destination
|
||||
decompress:
|
||||
src: "{{ remote_tmp_dir }}/dest.txt.gz"
|
||||
remove: true
|
||||
|
||||
- name: Decompress a file which lacks extension without specifying destination
|
||||
decompress:
|
||||
src: "{{ remote_tmp_dir }}/dest"
|
||||
remove: true
|
||||
|
||||
- name: Stat result files
|
||||
stat:
|
||||
path: "{{ remote_tmp_dir }}/{{ filename }}"
|
||||
loop:
|
||||
- dest.txt
|
||||
- dest_decompressed
|
||||
loop_control:
|
||||
loop_var: filename
|
||||
register: result_files_stat
|
||||
|
||||
- name: Test that file exists
|
||||
assert:
|
||||
that: "{{ item.stat.exists }}"
|
||||
quiet: true
|
||||
loop: "{{ result_files_stat.results }}"
|
||||
loop_control:
|
||||
label: "{{ item.stat.path }}"
|
||||
|
||||
- name: Delete test files
|
||||
file:
|
||||
path: "{{ filename }}"
|
||||
state: absent
|
||||
loop:
|
||||
- "dest.txt"
|
||||
- "dest_decompressed"
|
||||
loop_control:
|
||||
loop_var: filename
|
115
tests/integration/targets/decompress/tasks/main.yml
Normal file
115
tests/integration/targets/decompress/tasks/main.yml
Normal file
|
@ -0,0 +1,115 @@
|
|||
---
|
||||
# Copyright (c) Ansible Project
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
- name: Copy test files
|
||||
copy:
|
||||
src: "files/"
|
||||
dest: "{{ remote_tmp_dir }}"
|
||||
|
||||
- name: Get original file stat
|
||||
stat:
|
||||
path: "{{ remote_tmp_dir }}/file.txt"
|
||||
register: orig_file_stat
|
||||
|
||||
- name: Set supported formats
|
||||
set_fact:
|
||||
formats:
|
||||
- bz2
|
||||
- gz
|
||||
- xz
|
||||
|
||||
- name: Ensure xz is present to create compressed files (not Debian)
|
||||
package:
|
||||
name:
|
||||
- xz
|
||||
- bzip2
|
||||
state: latest
|
||||
when:
|
||||
- ansible_system != 'FreeBSD'
|
||||
- ansible_os_family != 'Darwin'
|
||||
- ansible_os_family != 'Debian'
|
||||
|
||||
- name: Ensure xz is present to create compressed files (Debian)
|
||||
package:
|
||||
name: xz-utils
|
||||
state: latest
|
||||
when: ansible_os_family == 'Debian'
|
||||
|
||||
- name: Install prerequisites for backports.lzma when using python2 (non OSX)
|
||||
block:
|
||||
- name: Set liblzma package name depending on the OS
|
||||
set_fact:
|
||||
liblzma_dev_package:
|
||||
Debian: liblzma-dev
|
||||
RedHat: xz-devel
|
||||
Suse: xz-devel
|
||||
- name: Ensure liblzma-dev is present to install backports-lzma
|
||||
package:
|
||||
name: "{{ liblzma_dev_package[ansible_os_family] }}"
|
||||
state: latest
|
||||
when: ansible_os_family in liblzma_dev_package.keys()
|
||||
when:
|
||||
- ansible_python_version.split('.')[0] == '2'
|
||||
- ansible_os_family != 'Darwin'
|
||||
|
||||
- name: Install prerequisites for backports.lzma when using python2 (OSX)
|
||||
block:
|
||||
- name: Find brew binary
|
||||
command: which brew
|
||||
register: brew_which
|
||||
- name: Get owner of brew binary
|
||||
stat:
|
||||
path: "{{ brew_which.stdout }}"
|
||||
register: brew_stat
|
||||
- name: "Install package"
|
||||
homebrew:
|
||||
name: xz
|
||||
state: present
|
||||
update_homebrew: false
|
||||
become: true
|
||||
become_user: "{{ brew_stat.stat.pw_name }}"
|
||||
# Newer versions of brew want to compile a package which takes a long time. Do not upgrade homebrew until a
|
||||
# proper solution can be found
|
||||
environment:
|
||||
HOMEBREW_NO_AUTO_UPDATE: "True"
|
||||
when:
|
||||
- ansible_os_family == 'Darwin'
|
||||
|
||||
- name: Ensure backports.lzma is present to create test archive (pip)
|
||||
pip:
|
||||
name: backports.lzma
|
||||
state: latest
|
||||
when: ansible_python_version.split('.')[0] == '2'
|
||||
notify:
|
||||
- delete backports.lzma
|
||||
|
||||
- name: Generate compressed files
|
||||
shell: |
|
||||
gzip < {{ item }} > {{ item }}.gz
|
||||
bzip2 < {{ item }} > {{ item }}.bz2
|
||||
xz < {{ item }} > {{ item }}.xz
|
||||
loop:
|
||||
- "{{ remote_tmp_dir }}/file.txt"
|
||||
- "{{ remote_tmp_dir }}/second_file.txt"
|
||||
|
||||
# Run tests
|
||||
- name: Run core tests
|
||||
block:
|
||||
- include_tasks: core.yml
|
||||
loop: "{{ formats }}"
|
||||
loop_control:
|
||||
loop_var: format
|
||||
- import_tasks: cleanup.yml
|
||||
|
||||
|
||||
- name: Run idempotency and check mode tests
|
||||
block:
|
||||
- import_tasks: misc.yml
|
||||
- import_tasks: cleanup.yml
|
||||
|
||||
- name: Run tests for destination file
|
||||
block:
|
||||
- import_tasks: dest.yml
|
||||
- import_tasks: cleanup.yml
|
74
tests/integration/targets/decompress/tasks/misc.yml
Normal file
74
tests/integration/targets/decompress/tasks/misc.yml
Normal file
|
@ -0,0 +1,74 @@
|
|||
---
|
||||
# Copyright (c) Ansible Project
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
- name: Decompress with check mode enabled
|
||||
decompress:
|
||||
src: "{{ remote_tmp_dir }}/file.txt.gz"
|
||||
dest: "{{ remote_tmp_dir }}/file_from_gz.txt"
|
||||
format: gz
|
||||
check_mode: true
|
||||
register: decompressed_check_mode
|
||||
|
||||
- name: Decompress second time with check mode enabled
|
||||
decompress:
|
||||
src: "{{ remote_tmp_dir }}/file.txt.gz"
|
||||
dest: "{{ remote_tmp_dir }}/file_from_gz.txt"
|
||||
format: gz
|
||||
remove: true
|
||||
check_mode: true
|
||||
register: decompressed_check_mode_2
|
||||
|
||||
- name: Stat original compressed file
|
||||
stat:
|
||||
path: "{{ remote_tmp_dir }}/file.txt.gz"
|
||||
register: original_file
|
||||
|
||||
- name: Stat non-existing file
|
||||
stat:
|
||||
path: "{{ remote_tmp_dir }}/file_from_gz.txt"
|
||||
register: nonexisting_stat
|
||||
|
||||
- name: Check mode test
|
||||
assert:
|
||||
that:
|
||||
- decompressed_check_mode.changed
|
||||
- decompressed_check_mode_2.changed
|
||||
- original_file.stat.exists
|
||||
- not nonexisting_stat.stat.exists
|
||||
|
||||
- name: Copy compressed file
|
||||
copy:
|
||||
src: "{{ remote_tmp_dir }}/file.txt.gz"
|
||||
dest: "{{ remote_tmp_dir }}/file_copied.txt.gz"
|
||||
remote_src: true
|
||||
|
||||
- name: Decompress, deleting original file
|
||||
decompress:
|
||||
src: "{{ remote_tmp_dir }}/file_copied.txt.gz"
|
||||
dest: "{{ remote_tmp_dir }}/file_copied.txt"
|
||||
remove: true
|
||||
|
||||
- name: Decompress non existing src
|
||||
decompress:
|
||||
src: "{{ remote_tmp_dir }}/file_copied.txt.gz"
|
||||
dest: "{{ remote_tmp_dir }}/file_copied.txt"
|
||||
remove: true
|
||||
register: decompress_non_existing_src
|
||||
|
||||
- name: Stat compressed file
|
||||
stat:
|
||||
path: "{{ remote_tmp_dir }}/file_copied.txt.gz"
|
||||
register: compressed_stat
|
||||
|
||||
- name: Run tests
|
||||
assert:
|
||||
that:
|
||||
- not compressed_stat.stat.exists
|
||||
- not decompress_non_existing_src.changed
|
||||
|
||||
- name: Delete decompressed file
|
||||
file:
|
||||
path: "{{ remote_tmp_dir }}/file_copied.txt"
|
||||
state: absent
|
Loading…
Add table
Add a link
Reference in a new issue