diff --git a/plugins/modules/quay_repository_prune.py b/plugins/modules/quay_repository_prune.py index 1f01856..61aacd1 100644 --- a/plugins/modules/quay_repository_prune.py +++ b/plugins/modules/quay_repository_prune.py @@ -251,6 +251,11 @@ def main(): if append and policy_details: module.exit_json(changed=False, id=policy_details.get("uuid")) + if not policies: + module.fail_json( + msg="The {repo} repository does not exist.".format(repo=full_repo_name) + ) + # Remove all the auto-pruning policies, except the one that the user # specifies if not append: diff --git a/tests/integration/targets/quay_registry_prune/meta/main.yml b/tests/integration/targets/quay_registry_prune/meta/main.yml new file mode 100644 index 0000000..6f61d07 --- /dev/null +++ b/tests/integration/targets/quay_registry_prune/meta/main.yml @@ -0,0 +1,4 @@ +--- +dependencies: + - setup_organization +... diff --git a/tests/integration/targets/quay_registry_prune/tasks/main.yml b/tests/integration/targets/quay_registry_prune/tasks/main.yml new file mode 100644 index 0000000..95989a5 --- /dev/null +++ b/tests/integration/targets/quay_registry_prune/tasks/main.yml @@ -0,0 +1,235 @@ +--- +# Supporting repository +- name: Ensure repository ansibletestrepo exists + infra.quay_configuration.quay_repository: + name: ansibletestorg/ansibletestrepo + visibility: private + state: present + quay_host: "{{ quay_url }}" + quay_token: "{{ quay_token }}" + validate_certs: false + +- name: ERROR EXPECTED Non-existing namespace + infra.quay_configuration.quay_repository_prune: + repository: nonexisting/ansibletestrepo + method: tags + value: 5 + tag_pattern: "unstable" + tag_pattern_matches: true + quay_host: "{{ quay_url }}" + quay_token: "{{ quay_token }}" + validate_certs: false + ignore_errors: true + register: result + +- name: Ensure that the task failed + ansible.builtin.assert: + that: result['failed'] + fail_msg: The preceding task should have failed (non-existing organization) + +- name: ERROR EXPECTED Wrong number of tags + infra.quay_configuration.quay_repository_prune: + repository: ansibletestorg/ansibletestrepo + method: tags + value: wrong + tag_pattern: "unstable" + tag_pattern_matches: true + quay_host: "{{ quay_url }}" + quay_token: "{{ quay_token }}" + validate_certs: false + ignore_errors: true + register: result + +- name: Ensure that the task failed + ansible.builtin.assert: + that: result['failed'] + fail_msg: The preceding task should have failed (wrong number of tags) + +- name: ERROR EXPECTED Wrong date + infra.quay_configuration.quay_repository_prune: + repository: ansibletestorg/ansibletestrepo + method: date + value: wrong + tag_pattern: "unstable" + tag_pattern_matches: true + quay_host: "{{ quay_url }}" + quay_token: "{{ quay_token }}" + validate_certs: false + ignore_errors: true + register: result + +- name: Ensure that the task failed + ansible.builtin.assert: + that: result['failed'] + fail_msg: The preceding task should have failed (wrong date) + +- name: Ensure a policy does not exist in a non-existing repository (no change) + infra.quay_configuration.quay_repository_prune: + repository: ansibletestorg/nonexisting + method: tags + value: 5 + tag_pattern: "unstable" + tag_pattern_matches: true + quay_host: "{{ quay_url }}" + quay_token: "{{ quay_token }}" + validate_certs: false + state: absent + register: result + +- name: Ensure that the task did not change anything + ansible.builtin.assert: + that: not result['changed'] + fail_msg: The preceding task should not have changed anything + +- name: Ensure a tag auto-pruning policy exists + infra.quay_configuration.quay_repository_prune: + repository: ansibletestorg/ansibletestrepo + method: tags + value: 5 + tag_pattern: "unstable" + tag_pattern_matches: false + quay_host: "{{ quay_url }}" + quay_token: "{{ quay_token }}" + validate_certs: false + +- name: Ensure a tag auto-pruning policy exists (no change) + infra.quay_configuration.quay_repository_prune: + repository: ansibletestorg/ansibletestrepo + method: tags + value: 5 + tag_pattern: "unstable" + tag_pattern_matches: false + quay_host: "{{ quay_url }}" + quay_token: "{{ quay_token }}" + validate_certs: false + register: result + +- name: Ensure that the task did not change anything + ansible.builtin.assert: + that: not result['changed'] + fail_msg: The preceding task should not have changed anything + +- name: Ensure a date auto-pruning policy exists 1 + infra.quay_configuration.quay_repository_prune: + repository: ansibletestorg/ansibletestrepo + method: date + value: 5d + quay_host: "{{ quay_url }}" + quay_token: "{{ quay_token }}" + validate_certs: false + +- name: Ensure a date auto-pruning policy exists 2 + infra.quay_configuration.quay_repository_prune: + repository: ansibletestorg/ansibletestrepo + method: date + value: 6w + tag_pattern: "nightly" + tag_pattern_matches: true + quay_host: "{{ quay_url }}" + quay_token: "{{ quay_token }}" + validate_certs: false + +- name: Ensure non-existing policy is removed (no change) + infra.quay_configuration.quay_repository_prune: + repository: ansibletestorg/ansibletestrepo + method: date + value: 42w + tag_pattern: "nightly" + tag_pattern_matches: false + state: absent + quay_host: "{{ quay_url }}" + quay_token: "{{ quay_token }}" + validate_certs: false + register: result + +- name: Ensure that the task did not change anything + ansible.builtin.assert: + that: not result['changed'] + fail_msg: The preceding task should not have changed anything + +- name: Ensure a tag auto-pruning policy is removed + infra.quay_configuration.quay_repository_prune: + repository: ansibletestorg/ansibletestrepo + method: tags + value: 5 + tag_pattern: "unstable" + tag_pattern_matches: false + state: absent + quay_host: "{{ quay_url }}" + quay_token: "{{ quay_token }}" + validate_certs: false + +- name: Ensure only one tag auto-pruning policy exists + infra.quay_configuration.quay_repository_prune: + repository: ansibletestorg/ansibletestrepo + method: tags + value: 50 + tag_pattern: "foobar" + tag_pattern_matches: true + append: false + quay_host: "{{ quay_url }}" + quay_token: "{{ quay_token }}" + validate_certs: false + +- name: Ensure only one tag auto-pruning policy exists (no change) + infra.quay_configuration.quay_repository_prune: + repository: ansibletestorg/ansibletestrepo + method: tags + value: 50 + tag_pattern: "foobar" + tag_pattern_matches: true + append: false + quay_host: "{{ quay_url }}" + quay_token: "{{ quay_token }}" + validate_certs: false + register: result + +- name: Ensure that the task did not change anything + ansible.builtin.assert: + that: not result['changed'] + fail_msg: The preceding task should not have changed anything + +- name: Ensure a date auto-pruning policy exists + infra.quay_configuration.quay_repository_prune: + repository: ansibletestorg/ansibletestrepo + method: date + value: 42w + tag_pattern: "nightly" + tag_pattern_matches: false + quay_host: "{{ quay_url }}" + quay_token: "{{ quay_token }}" + validate_certs: false + +- name: Ensure only one tag auto-pruning policy is set + infra.quay_configuration.quay_repository_prune: + repository: ansibletestorg/ansibletestrepo + method: tags + value: 50 + tag_pattern: "foobar" + tag_pattern_matches: true + append: false + quay_host: "{{ quay_url }}" + quay_token: "{{ quay_token }}" + validate_certs: false + +- name: Ensure the tag auto-pruning policy is removed + infra.quay_configuration.quay_repository_prune: + repository: ansibletestorg/ansibletestrepo + method: tags + value: 50 + tag_pattern: "foobar" + tag_pattern_matches: true + append: false + quay_host: "{{ quay_url }}" + quay_token: "{{ quay_token }}" + validate_certs: false + state: absent + +- name: Ensure the repository is removed + infra.quay_configuration.quay_repository: + name: ansibletestorg/ansibletestrepo + state: absent + quay_host: "{{ quay_url }}" + quay_token: "{{ quay_token }}" + validate_certs: false +...