| 1 |
--- |
| 2 |
# Copyright (c) Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
#################################################################### |
| 7 |
# WARNING: These are designed specifically for Ansible tests # |
| 8 |
# and should not be used as examples of how to write Ansible roles # |
| 9 |
#################################################################### |
| 10 |
|
| 11 |
- name: Create random names |
| 12 |
ansible.builtin.set_fact: |
| 13 |
cname: "{{ 'ansible-container-%0x' % ((2**32) | random) }}" |
| 14 |
nname: "{{ 'ansible-network-%0x' % ((2**32) | random) }}" |
| 15 |
vname: "{{ 'ansible-volume-%0x' % ((2**32) | random) }}" |
| 16 |
|
| 17 |
- block: |
| 18 |
# Create objects to be pruned |
| 19 |
- name: Create container (without volume) |
| 20 |
community.docker.docker_container: |
| 21 |
name: "{{ cname }}" |
| 22 |
image: "{{ docker_test_image_hello_world }}" |
| 23 |
state: present |
| 24 |
register: container |
| 25 |
- name: Create network |
| 26 |
community.docker.docker_network: |
| 27 |
name: "{{ nname }}" |
| 28 |
state: present |
| 29 |
register: network |
| 30 |
- name: Create named volume |
| 31 |
community.docker.docker_volume: |
| 32 |
name: "{{ vname }}" |
| 33 |
state: present |
| 34 |
register: volume |
| 35 |
- name: Create anonymous volume |
| 36 |
ansible.builtin.command: docker volume create |
| 37 |
register: volume_anon |
| 38 |
|
| 39 |
- name: List volumes |
| 40 |
ansible.builtin.command: docker volume list |
| 41 |
|
| 42 |
# Prune objects |
| 43 |
- name: Prune everything |
| 44 |
community.docker.docker_prune: |
| 45 |
containers: true |
| 46 |
images: true |
| 47 |
networks: true |
| 48 |
volumes: true |
| 49 |
builder_cache: true |
| 50 |
register: result |
| 51 |
|
| 52 |
# Analyze result |
| 53 |
- name: Show results |
| 54 |
ansible.builtin.debug: |
| 55 |
var: result |
| 56 |
- name: General checks |
| 57 |
ansible.builtin.assert: |
| 58 |
that: |
| 59 |
- result is changed |
| 60 |
# containers |
| 61 |
- container.container.Id in result.containers |
| 62 |
- "'containers_space_reclaimed' in result" |
| 63 |
# images |
| 64 |
- "'images_space_reclaimed' in result" |
| 65 |
# networks |
| 66 |
- network.network.Name in result.networks |
| 67 |
# volumes |
| 68 |
- volume_anon.stdout in result.volumes |
| 69 |
- "'volumes_space_reclaimed' in result" |
| 70 |
# builder_cache |
| 71 |
- "'builder_cache_space_reclaimed' in result" |
| 72 |
- name: API-version specific volumes check (API version before 1.42) |
| 73 |
ansible.builtin.assert: |
| 74 |
that: |
| 75 |
# For API version 1.41 and before, pruning always considers all volumes |
| 76 |
- volume.volume.Name in result.volumes |
| 77 |
when: docker_api_version is version('1.42', '<') |
| 78 |
- name: API-version specific volumes check (API version 1.42+) |
| 79 |
ansible.builtin.assert: |
| 80 |
that: |
| 81 |
# For API version 1.41 and before, pruning considers only anonymous volumes, |
| 82 |
# so our named container is not removed |
| 83 |
- volume.volume.Name not in result.volumes |
| 84 |
when: docker_api_version is version('1.42', '>=') |
| 85 |
|
| 86 |
# Prune objects again |
| 87 |
- name: Prune everything again (should have no change) |
| 88 |
community.docker.docker_prune: |
| 89 |
containers: true |
| 90 |
images: true |
| 91 |
networks: true |
| 92 |
volumes: true |
| 93 |
builder_cache: true |
| 94 |
register: result |
| 95 |
|
| 96 |
# Analyze result |
| 97 |
- name: Show results |
| 98 |
ansible.builtin.debug: |
| 99 |
var: result |
| 100 |
- name: General checks |
| 101 |
ansible.builtin.assert: |
| 102 |
that: |
| 103 |
- result is not changed |
| 104 |
# containers |
| 105 |
- result.containers == [] |
| 106 |
- result.containers_space_reclaimed == 0 |
| 107 |
# images |
| 108 |
- result.images == [] |
| 109 |
- result.images_space_reclaimed == 0 |
| 110 |
# networks |
| 111 |
- result.networks == [] |
| 112 |
# volumes |
| 113 |
- result.volumes == [] |
| 114 |
# builder_cache |
| 115 |
- result.builder_cache_space_reclaimed == 0 |
| 116 |
|
| 117 |
# Test with filters |
| 118 |
- name: Prune with filters |
| 119 |
community.docker.docker_prune: |
| 120 |
images: true |
| 121 |
images_filters: |
| 122 |
dangling: true |
| 123 |
register: result |
| 124 |
|
| 125 |
- name: Show results |
| 126 |
ansible.builtin.debug: |
| 127 |
var: result |
| 128 |
|
| 129 |
- name: Prune build cache (API version 1.39+) |
| 130 |
when: docker_api_version is version('1.39', '>=') |
| 131 |
block: |
| 132 |
- name: Prune build cache with option |
| 133 |
community.docker.docker_prune: |
| 134 |
builder_cache: true |
| 135 |
builder_cache_all: true |
| 136 |
builder_cache_filters: |
| 137 |
until: 10m |
| 138 |
builder_cache_keep_storage: 1MB |
| 139 |
register: result |
| 140 |
|
| 141 |
- name: Show results |
| 142 |
ansible.builtin.debug: |
| 143 |
var: result |
| 144 |
|
| 145 |
- name: Check results |
| 146 |
ansible.builtin.assert: |
| 147 |
that: |
| 148 |
- "'builder_cache_space_reclaimed' in result" |
| 149 |
- "'builder_cache_caches_deleted' in result" |
| 150 |
|
| 151 |
- name: Prune volumes with all filter (API version 1.42+) |
| 152 |
when: docker_api_version is version('1.42', '>=') |
| 153 |
block: |
| 154 |
- name: Prune with filters |
| 155 |
community.docker.docker_prune: |
| 156 |
volumes: true |
| 157 |
volumes_filters: |
| 158 |
all: true |
| 159 |
register: result |
| 160 |
|
| 161 |
- name: Show results |
| 162 |
ansible.builtin.debug: |
| 163 |
var: result |
| 164 |
|
| 165 |
- name: Check results |
| 166 |
ansible.builtin.assert: |
| 167 |
that: |
| 168 |
- result is changed |
| 169 |
- volume.volume.Name in result.volumes |
| 170 |
- "'volumes_space_reclaimed' in result" |
| 171 |
|
| 172 |
when: docker_api_version is version('1.25', '>=') |
| 173 |
|
| 174 |
- ansible.builtin.fail: msg="Too old docker / docker-py version to run docker_prune tests!" |
| 175 |
when: not(docker_api_version is version('1.25', '>=')) and (ansible_facts.distribution != 'CentOS' or ansible_facts.distribution_major_version|int > 6) |