diff options
Diffstat (limited to 'test/integration/targets/apt_repository/tasks')
5 files changed, 427 insertions, 0 deletions
diff --git a/test/integration/targets/apt_repository/tasks/apt.yml b/test/integration/targets/apt_repository/tasks/apt.yml new file mode 100644 index 0000000..0dc25af --- /dev/null +++ b/test/integration/targets/apt_repository/tasks/apt.yml @@ -0,0 +1,243 @@ +--- + +- set_fact: + test_ppa_name: 'ppa:git-core/ppa' + test_ppa_filename: 'git-core' + test_ppa_spec: 'deb http://ppa.launchpad.net/git-core/ppa/ubuntu {{ansible_distribution_release}} main' + test_ppa_key: 'E1DF1F24' # http://keyserver.ubuntu.com:11371/pks/lookup?search=0xD06AAF4C11DAB86DF421421EFE6B20ECA7AD98A1&op=index + +- name: show python version + debug: var=ansible_python_version + +- name: use python-apt + set_fact: + python_apt: python-apt + when: ansible_python_version is version('3', '<') + +- name: use python3-apt + set_fact: + python_apt: python3-apt + when: ansible_python_version is version('3', '>=') + +# UNINSTALL 'python-apt' +# The `apt_repository` module has the smarts to auto-install `python-apt`. To +# test, we will first uninstall `python-apt`. +- name: check {{ python_apt }} with dpkg + shell: dpkg -s {{ python_apt }} + register: dpkg_result + ignore_errors: true + +- name: uninstall {{ python_apt }} with apt + apt: pkg={{ python_apt }} state=absent purge=yes + register: apt_result + when: dpkg_result is successful + +# +# TEST: apt_repository: repo=<name> +# +- import_tasks: 'cleanup.yml' + +- name: 'record apt cache mtime' + stat: path='/var/cache/apt/pkgcache.bin' + register: cache_before + +- name: 'name=<name> (expect: pass)' + apt_repository: repo='{{test_ppa_name}}' state=present + register: result + +- name: 'assert the apt cache did *NOT* change' + assert: + that: + - 'result.changed' + - 'result.state == "present"' + - 'result.repo == "{{test_ppa_name}}"' + +- name: 'examine apt cache mtime' + stat: path='/var/cache/apt/pkgcache.bin' + register: cache_after + +- name: 'assert the apt cache did change' + assert: + that: + - 'cache_before.stat.mtime != cache_after.stat.mtime' + +- name: 'ensure ppa key is installed (expect: pass)' + apt_key: id='{{test_ppa_key}}' state=present + +# +# TEST: apt_repository: repo=<name> update_cache=no +# +- import_tasks: 'cleanup.yml' + +- name: 'record apt cache mtime' + stat: path='/var/cache/apt/pkgcache.bin' + register: cache_before + +- name: 'name=<name> update_cache=no (expect: pass)' + apt_repository: repo='{{test_ppa_name}}' state=present update_cache=no + register: result + +- assert: + that: + - 'result.changed' + - 'result.state == "present"' + - 'result.repo == "{{test_ppa_name}}"' + +- name: 'examine apt cache mtime' + stat: path='/var/cache/apt/pkgcache.bin' + register: cache_after + +- name: 'assert the apt cache did *NOT* change' + assert: + that: + - 'cache_before.stat.mtime == cache_after.stat.mtime' + +- name: 'ensure ppa key is installed (expect: pass)' + apt_key: id='{{test_ppa_key}}' state=present + +# +# TEST: apt_repository: repo=<name> update_cache=yes +# +- import_tasks: 'cleanup.yml' + +- name: 'record apt cache mtime' + stat: path='/var/cache/apt/pkgcache.bin' + register: cache_before + +- name: 'name=<name> update_cache=yes (expect: pass)' + apt_repository: repo='{{test_ppa_name}}' state=present update_cache=yes + register: result + +- assert: + that: + - 'result.changed' + - 'result.state == "present"' + - 'result.repo == "{{test_ppa_name}}"' + +- name: 'examine apt cache mtime' + stat: path='/var/cache/apt/pkgcache.bin' + register: cache_after + +- name: 'assert the apt cache did change' + assert: + that: + - 'cache_before.stat.mtime != cache_after.stat.mtime' + +- name: 'ensure ppa key is installed (expect: pass)' + apt_key: id='{{test_ppa_key}}' state=present + +# +# TEST: apt_repository: repo=<spec> +# +- import_tasks: 'cleanup.yml' + +- name: 'record apt cache mtime' + stat: path='/var/cache/apt/pkgcache.bin' + register: cache_before + +- name: ensure ppa key is present before adding repo that requires authentication + apt_key: keyserver=keyserver.ubuntu.com id='{{test_ppa_key}}' state=present + +- name: 'name=<spec> (expect: pass)' + apt_repository: repo='{{test_ppa_spec}}' state=present + register: result + +- name: update the cache + apt: + update_cache: true + register: result_cache + +- assert: + that: + - 'result.changed' + - 'result.state == "present"' + - 'result.repo == "{{test_ppa_spec}}"' + - result_cache is not changed + +- name: 'examine apt cache mtime' + stat: path='/var/cache/apt/pkgcache.bin' + register: cache_after + +- name: 'assert the apt cache did change' + assert: + that: + - 'cache_before.stat.mtime != cache_after.stat.mtime' + +- name: remove repo by spec + apt_repository: repo='{{test_ppa_spec}}' state=absent + register: result + +# When installing a repo with the spec, the key is *NOT* added +- name: 'ensure ppa key is absent (expect: pass)' + apt_key: id='{{test_ppa_key}}' state=absent + +# +# TEST: apt_repository: repo=<spec> filename=<filename> +# +- import_tasks: 'cleanup.yml' + +- name: 'record apt cache mtime' + stat: path='/var/cache/apt/pkgcache.bin' + register: cache_before + +- name: ensure ppa key is present before adding repo that requires authentication + apt_key: keyserver=keyserver.ubuntu.com id='{{test_ppa_key}}' state=present + +- name: 'name=<spec> filename=<filename> (expect: pass)' + apt_repository: repo='{{test_ppa_spec}}' filename='{{test_ppa_filename}}' state=present + register: result + +- assert: + that: + - 'result.changed' + - 'result.state == "present"' + - 'result.repo == "{{test_ppa_spec}}"' + +- name: 'examine source file' + stat: path='/etc/apt/sources.list.d/{{test_ppa_filename}}.list' + register: source_file + +- name: 'assert source file exists' + assert: + that: + - 'source_file.stat.exists == True' + +- name: 'examine apt cache mtime' + stat: path='/var/cache/apt/pkgcache.bin' + register: cache_after + +- name: 'assert the apt cache did change' + assert: + that: + - 'cache_before.stat.mtime != cache_after.stat.mtime' + +# When installing a repo with the spec, the key is *NOT* added +- name: 'ensure ppa key is absent (expect: pass)' + apt_key: id='{{test_ppa_key}}' state=absent + +- name: Test apt_repository with a null value for repo + apt_repository: + repo: + register: result + ignore_errors: yes + +- assert: + that: + - result is failed + - result.msg == 'Please set argument \'repo\' to a non-empty value' + +- name: Test apt_repository with an empty value for repo + apt_repository: + repo: "" + register: result + ignore_errors: yes + +- assert: + that: + - result is failed + - result.msg == 'Please set argument \'repo\' to a non-empty value' + +# +# TEARDOWN +# +- import_tasks: 'cleanup.yml' diff --git a/test/integration/targets/apt_repository/tasks/cleanup.yml b/test/integration/targets/apt_repository/tasks/cleanup.yml new file mode 100644 index 0000000..92280ce --- /dev/null +++ b/test/integration/targets/apt_repository/tasks/cleanup.yml @@ -0,0 +1,17 @@ +--- +# tasks to cleanup a repo and assert it is gone + +- name: remove existing ppa + apt_repository: repo={{test_ppa_name}} state=absent + ignore_errors: true + +- name: test that ppa does not exist (expect pass) + shell: cat /etc/apt/sources.list /etc/apt/sources.list.d/* | grep "{{test_ppa_spec}}" + register: command + failed_when: command.rc == 0 + changed_when: false + +# Should this use apt-key, maybe? +- name: remove ppa key + apt_key: id={{test_ppa_key}} state=absent + ignore_errors: true diff --git a/test/integration/targets/apt_repository/tasks/main.yml b/test/integration/targets/apt_repository/tasks/main.yml new file mode 100644 index 0000000..5d72f6f --- /dev/null +++ b/test/integration/targets/apt_repository/tasks/main.yml @@ -0,0 +1,25 @@ +# test code for the apt_repository module +# (c) 2014, James Laska <jlaska@ansible.com> + +# 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 <http://www.gnu.org/licenses/>. + +- import_tasks: 'apt.yml' + when: ansible_distribution in ('Ubuntu') + +- import_tasks: mode.yaml + when: ansible_distribution in ('Ubuntu') + tags: + - test_apt_repository_mode diff --git a/test/integration/targets/apt_repository/tasks/mode.yaml b/test/integration/targets/apt_repository/tasks/mode.yaml new file mode 100644 index 0000000..4b4fabf --- /dev/null +++ b/test/integration/targets/apt_repository/tasks/mode.yaml @@ -0,0 +1,135 @@ +--- + +# These tests are likely slower than they should be, since each +# invocation of apt_repository seems to end up querying for +# lots (all?) configured repos. + +- set_fact: + test_repo_spec: "deb http://apt.postgresql.org/pub/repos/apt/ {{ ansible_distribution_release }}-pgdg main" + test_repo_path: /etc/apt/sources.list.d/apt_postgresql_org_pub_repos_apt.list + +- import_tasks: mode_cleanup.yaml + +- name: Add GPG key to verify signatures + apt_key: + id: 7FCC7D46ACCC4CF8 + keyserver: keyserver.ubuntu.com + +- name: Mode specified as yaml literal 0600 + apt_repository: + repo: "{{ test_repo_spec }}" + state: present + mode: 0600 + update_cache: false + register: mode_given_results + +- name: Gather mode_given_as_literal_yaml stat + stat: + path: "{{ test_repo_path }}" + register: mode_given_yaml_literal_0600 + +- name: Show mode_given_yaml_literal_0600 + debug: + var: mode_given_yaml_literal_0600 + +- import_tasks: mode_cleanup.yaml + +- name: Assert mode_given_yaml_literal_0600 is correct + assert: + that: "mode_given_yaml_literal_0600.stat.mode == '0600'" + +- name: No mode specified + apt_repository: + repo: "{{ test_repo_spec }}" + state: present + update_cache: false + register: no_mode_results + +- name: Gather no mode stat + stat: + path: "{{ test_repo_path }}" + register: no_mode_stat + +- name: Show no mode stat + debug: + var: no_mode_stat + +- import_tasks: mode_cleanup.yaml + +- name: Assert no_mode_stat is correct + assert: + that: "no_mode_stat.stat.mode == '0644'" + +- name: Mode specified as string 0600 + apt_repository: + repo: "{{ test_repo_spec }}" + state: present + mode: "0600" + update_cache: false + register: mode_given_string_results + +- name: Gather mode_given_string stat + stat: + path: "{{ test_repo_path }}" + register: mode_given_string_stat + +- name: Show mode_given_string_stat + debug: + var: mode_given_string_stat + +- import_tasks: mode_cleanup.yaml + +- name: Mode specified as string 600 + apt_repository: + repo: "{{ test_repo_spec }}" + state: present + mode: "600" + update_cache: false + register: mode_given_string_600_results + +- name: Gather mode_given_600_string stat + stat: + path: "{{ test_repo_path }}" + register: mode_given_string_600_stat + +- name: Show mode_given_string_stat + debug: + var: mode_given_string_600_stat + +- import_tasks: mode_cleanup.yaml + +- name: Assert mode is correct + assert: + that: "mode_given_string_600_stat.stat.mode == '0600'" + +- name: Mode specified as yaml literal 600 + apt_repository: + repo: "{{ test_repo_spec }}" + state: present + mode: 600 + update_cache: false + register: mode_given_short_results + +- name: Gather mode_given_yaml_literal_600 stat + stat: + path: "{{ test_repo_path }}" + register: mode_given_yaml_literal_600 + +- name: Show mode_given_yaml_literal_600 + debug: + var: mode_given_yaml_literal_600 + +- import_tasks: mode_cleanup.yaml + +# a literal 600 as the mode will fail currently, in the sense that it +# doesn't guess and consider 600 and 0600 to be the same, and will instead +# intepret literal 600 as the decimal 600 (and thereby octal 1130). +# The literal 0600 can be interpreted as octal correctly. Note that +# a decimal 644 is octal 420. The default perm is 0644 so a mis intrpretation +# of 644 was previously resulting in a default file mode of 0420. +# 'mode: 600' is likely not what a user meant but there isnt enough info +# to determine that. Note that a string arg of '600' will be intrepeted as 0600. +# See https://github.com/ansible/ansible/issues/16370 +- name: Assert mode_given_yaml_literal_600 is correct + assert: + that: "mode_given_yaml_literal_600.stat.mode == '1130'" diff --git a/test/integration/targets/apt_repository/tasks/mode_cleanup.yaml b/test/integration/targets/apt_repository/tasks/mode_cleanup.yaml new file mode 100644 index 0000000..726de11 --- /dev/null +++ b/test/integration/targets/apt_repository/tasks/mode_cleanup.yaml @@ -0,0 +1,7 @@ +--- +# tasks to cleanup after creating a repo file, specifically for testing the 'mode' arg + +- name: Delete existing repo + file: + path: "{{ test_repo_path }}" + state: absent
\ No newline at end of file |