#################################################################### # WARNING: These are designed specifically for Ansible tests # # and should not be used as examples of how to write Ansible roles # #################################################################### # Test code for the archive module. # (c) 2017, Abhijeet Kasurde # This file is part of Ansible # # Ansible is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Ansible is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . # Make sure we start fresh - name: Ensure zip is present to create test archive (yum) yum: name=zip state=latest when: ansible_facts.pkg_mgr == 'yum' - name: Ensure zip is present to create test archive (apt) apt: name=zip state=latest when: ansible_facts.pkg_mgr == 'apt' - 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: no become: yes 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_python_version.split('.')[0] == '2' - 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' 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: archive using gz archive: path: "{{ remote_tmp_dir }}/*.txt" dest: "{{ remote_tmp_dir }}/archive_01.gz" format: gz register: archive_gz_result_01 - debug: msg="{{ archive_gz_result_01 }}" - name: verify that the files archived file: path={{remote_tmp_dir}}/archive_01.gz state=file - name: check if gz file exists and includes all text files assert: that: - "{{ archive_gz_result_01.changed }}" - "{{ 'archived' in archive_gz_result_01 }}" - "{{ archive_gz_result_01['archived'] | length }} == 3" - name: archive using zip archive: path: "{{ remote_tmp_dir }}/*.txt" dest: "{{ remote_tmp_dir }}/archive_01.zip" format: zip register: archive_zip_result_01 - debug: msg="{{ archive_zip_result_01 }}" - name: verify that the files archived file: path={{remote_tmp_dir}}/archive_01.zip state=file - name: check if zip file exists assert: that: - "{{ archive_zip_result_01.changed }}" - "{{ 'archived' in archive_zip_result_01 }}" - "{{ archive_zip_result_01['archived'] | length }} == 3" - name: archive using bz2 archive: path: "{{ remote_tmp_dir }}/*.txt" dest: "{{ remote_tmp_dir }}/archive_01.bz2" format: bz2 register: archive_bz2_result_01 - debug: msg="{{ archive_bz2_result_01 }}" - name: verify that the files archived file: path={{remote_tmp_dir}}/archive_01.bz2 state=file - name: check if bzip file exists assert: that: - "{{ archive_bz2_result_01.changed }}" - "{{ 'archived' in archive_bz2_result_01 }}" - "{{ archive_bz2_result_01['archived'] | length }} == 3" - name: archive using xz archive: path: "{{ remote_tmp_dir }}/*.txt" dest: "{{ remote_tmp_dir }}/archive_01.xz" format: xz register: archive_xz_result_01 - debug: msg="{{ archive_xz_result_01 }}" - name: verify that the files archived file: path={{remote_tmp_dir}}/archive_01.xz state=file - name: check if xz file exists assert: that: - "{{ archive_xz_result_01.changed }}" - "{{ 'archived' in archive_xz_result_01 }}" - "{{ archive_xz_result_01['archived'] | length }} == 3" - name: archive and set mode to 0600 archive: path: "{{ remote_tmp_dir }}/*.txt" dest: "{{ remote_tmp_dir }}/archive_02.gz" format: gz mode: "u+rwX,g-rwx,o-rwx" register: archive_bz2_result_02 - name: Test that the file modes were changed stat: path: "{{ remote_tmp_dir }}/archive_02.gz" register: archive_02_gz_stat - debug: msg="{{ archive_02_gz_stat}}" - name: Test that the file modes were changed assert: that: - archive_02_gz_stat is not changed - "archive_02_gz_stat.stat.mode == '0600'" - "'archived' in archive_bz2_result_02" - "{{ archive_bz2_result_02['archived']| length}} == 3" - name: remove our gz file: path="{{ remote_tmp_dir }}/archive_02.gz" state=absent - name: archive and set mode to 0600 archive: path: "{{ remote_tmp_dir }}/*.txt" dest: "{{ remote_tmp_dir }}/archive_02.zip" format: zip mode: "u+rwX,g-rwx,o-rwx" register: archive_zip_result_02 - name: Test that the file modes were changed stat: path: "{{ remote_tmp_dir }}/archive_02.zip" register: archive_02_zip_stat - name: Test that the file modes were changed assert: that: - archive_02_zip_stat is not changed - "archive_02_zip_stat.stat.mode == '0600'" - "'archived' in archive_zip_result_02" - "{{ archive_zip_result_02['archived']| length}} == 3" - name: remove our zip file: path="{{ remote_tmp_dir }}/archive_02.zip" state=absent - name: archive and set mode to 0600 archive: path: "{{ remote_tmp_dir }}/*.txt" dest: "{{ remote_tmp_dir }}/archive_02.bz2" format: bz2 mode: "u+rwX,g-rwx,o-rwx" register: archive_bz2_result_02 - name: Test that the file modes were changed stat: path: "{{ remote_tmp_dir }}/archive_02.bz2" register: archive_02_bz2_stat - name: Test that the file modes were changed assert: that: - archive_02_bz2_stat is not changed - "archive_02_bz2_stat.stat.mode == '0600'" - "'archived' in archive_bz2_result_02" - "{{ archive_bz2_result_02['archived']| length}} == 3" - name: remove our bz2 file: path="{{ remote_tmp_dir }}/archive_02.bz2" state=absent - name: archive and set mode to 0600 archive: path: "{{ remote_tmp_dir }}/*.txt" dest: "{{ remote_tmp_dir }}/archive_02.xz" format: xz mode: "u+rwX,g-rwx,o-rwx" register: archive_xz_result_02 - name: Test that the file modes were changed stat: path: "{{ remote_tmp_dir }}/archive_02.xz" register: archive_02_xz_stat - name: Test that the file modes were changed assert: that: - archive_02_xz_stat is not changed - "archive_02_xz_stat.stat.mode == '0600'" - "'archived' in archive_xz_result_02" - "{{ archive_xz_result_02['archived']| length}} == 3" - name: remove our xz file: path="{{ remote_tmp_dir }}/archive_02.xz" state=absent - name: archive multiple files as list archive: path: - "{{ remote_tmp_dir }}/empty.txt" - "{{ remote_tmp_dir }}/foo.txt" - "{{ remote_tmp_dir }}/bar.txt" dest: "{{ remote_tmp_dir }}/archive_list.gz" format: gz register: archive_gz_list_result - name: verify that the files archived file: path={{remote_tmp_dir}}/archive_list.gz state=file - name: check if gz file exists and includes all text files assert: that: - "{{ archive_gz_list_result.changed }}" - "{{ 'archived' in archive_gz_list_result }}" - "{{ archive_gz_list_result['archived'] | length }} == 3" - name: remove our gz file: path="{{ remote_tmp_dir }}/archive_list.gz" state=absent - name: test that gz archive that contains non-ascii filenames archive: path: "{{ remote_tmp_dir }}/*.txt" dest: "{{ remote_tmp_dir }}/test-archive-nonascii-くらとみ.tar.gz" format: gz register: nonascii_result_0 - name: Check that file is really there stat: path: "{{ remote_tmp_dir }}/test-archive-nonascii-くらとみ.tar.gz" register: nonascii_stat0 - name: Assert that nonascii tests succeeded assert: that: - nonascii_result_0 is changed - "nonascii_stat0.stat.exists == true" - name: remove nonascii test file: path="{{ remote_tmp_dir }}/test-archive-nonascii-くらとみ.tar.gz" state=absent - name: test that bz2 archive that contains non-ascii filenames archive: path: "{{ remote_tmp_dir }}/*.txt" dest: "{{ remote_tmp_dir }}/test-archive-nonascii-くらとみ.bz2" format: bz2 register: nonascii_result_1 - name: Check that file is really there stat: path: "{{ remote_tmp_dir }}/test-archive-nonascii-くらとみ.bz2" register: nonascii_stat_1 - name: Assert that nonascii tests succeeded assert: that: - nonascii_result_1 is changed - "nonascii_stat_1.stat.exists == true" - name: remove nonascii test file: path="{{ remote_tmp_dir }}/test-archive-nonascii-くらとみ.bz2" state=absent - name: test that xz archive that contains non-ascii filenames archive: path: "{{ remote_tmp_dir }}/*.txt" dest: "{{ remote_tmp_dir }}/test-archive-nonascii-くらとみ.xz" format: xz register: nonascii_result_1 - name: Check that file is really there stat: path: "{{ remote_tmp_dir }}/test-archive-nonascii-くらとみ.xz" register: nonascii_stat_1 - name: Assert that nonascii tests succeeded assert: that: - nonascii_result_1 is changed - "nonascii_stat_1.stat.exists == true" - name: remove nonascii test file: path="{{ remote_tmp_dir }}/test-archive-nonascii-くらとみ.xz" state=absent - name: test that zip archive that contains non-ascii filenames archive: path: "{{ remote_tmp_dir }}/*.txt" dest: "{{ remote_tmp_dir }}/test-archive-nonascii-くらとみ.zip" format: zip register: nonascii_result_2 - name: Check that file is really there stat: path: "{{ remote_tmp_dir }}/test-archive-nonascii-くらとみ.zip" register: nonascii_stat_2 - name: Assert that nonascii tests succeeded assert: that: - nonascii_result_2 is changed - "nonascii_stat_2.stat.exists == true" - name: remove nonascii test file: path="{{ remote_tmp_dir }}/test-archive-nonascii-くらとみ.zip" state=absent - name: Test that excluded paths do not influence archive root archive: path: - "{{ remote_tmp_dir }}/sub/subfile.txt" - "{{ remote_tmp_dir }}" exclude_path: - "{{ remote_tmp_dir }}" dest: "{{ remote_tmp_dir }}/test-archive-root.tgz" register: archive_root_result - name: Assert that excluded paths do not influence archive root assert: that: - archive_root_result.arcroot != remote_tmp_dir - name: Remove archive root test file: path: "{{ remote_tmp_dir }}/test-archive-root.tgz" state: absent - name: Test Single Target with format={{ item }} archive: path: "{{ remote_tmp_dir }}/foo.txt" dest: "{{ remote_tmp_dir }}/test-single-target.{{ item }}" format: "{{ item }}" register: "single_target_test" loop: - zip - tar - gz - bz2 - xz # Dummy tests until ``dest_state`` result value can be implemented - name: Assert that single target tests are effective assert: that: - single_target_test.results[0] is changed - single_target_test.results[1] is changed - single_target_test.results[2] is changed - single_target_test.results[3] is changed - single_target_test.results[4] is changed - name: Retrieve contents of single target archives ansible.builtin.unarchive: src: "{{ remote_tmp_dir }}/test-single-target.zip" dest: . list_files: true check_mode: true ignore_errors: true register: single_target_test_contents - name: Assert that file names in single-file zip archives are preserved assert: that: - "'oo.txt' not in single_target_test_contents.files" - "'foo.txt' in single_target_test_contents.files" # ``unarchive`` fails for RHEL and FreeBSD on ansible 2.x when: single_target_test_contents is success and single_target_test_contents is not skipped - name: Remove single target test with format={{ item }} file: path: "{{ remote_tmp_dir }}/test-single-target.{{ item }}" state: absent loop: - zip - tar - gz - bz2 - xz - name: Test that missing files result in incomplete state archive: path: - "{{ remote_tmp_dir }}/*.txt" - "{{ remote_tmp_dir }}/dne.txt" exclude_path: "{{ remote_tmp_dir }}/foo.txt" dest: "{{ remote_tmp_dir }}/test-incomplete-archive.tgz" register: incomplete_archive_result - name: Assert that incomplete archive has incomplete state assert: that: - incomplete_archive_result is changed - "'{{ remote_tmp_dir }}/dne.txt' in incomplete_archive_result.missing" - "'{{ remote_tmp_dir }}/foo.txt' not in incomplete_archive_result.missing" - name: Remove incomplete archive file: path: "{{ remote_tmp_dir }}/test-incomplete-archive.tgz" state: absent - name: Remove backports.lzma if previously installed (pip) pip: name=backports.lzma state=absent when: backports_lzma_pip is changed - name: import remove tests import_tasks: remove.yml - name: import broken-link tests import_tasks: broken-link.yml