| 1 |
--- |
| 2 |
- name: create a dangling symbolic link inside collection directory |
| 3 |
ansible.builtin.file: |
| 4 |
src: '/non-existent-path/README.md' |
| 5 |
dest: '{{ galaxy_dir }}/scratch/ansible_test/my_collection/docs/README.md' |
| 6 |
state: link |
| 7 |
force: yes |
| 8 |
|
| 9 |
- name: build basic collection based on current directory with dangling symlink |
| 10 |
command: ansible-galaxy collection build {{ galaxy_verbosity }} |
| 11 |
args: |
| 12 |
chdir: '{{ galaxy_dir }}/scratch/ansible_test/my_collection' |
| 13 |
register: fail_symlink_build |
| 14 |
ignore_errors: yes |
| 15 |
|
| 16 |
- name: assert that build fails due to dangling symlink |
| 17 |
assert: |
| 18 |
that: |
| 19 |
- fail_symlink_build.failed |
| 20 |
- '"Failed to find the target path" in fail_symlink_build.stderr' |
| 21 |
|
| 22 |
- name: remove dangling symbolic link |
| 23 |
ansible.builtin.file: |
| 24 |
path: '{{ galaxy_dir }}/scratch/ansible_test/my_collection/docs/README.md' |
| 25 |
state: absent |
| 26 |
|
| 27 |
- name: build basic collection based on current directory |
| 28 |
command: ansible-galaxy collection build {{ galaxy_verbosity }} |
| 29 |
args: |
| 30 |
chdir: '{{ galaxy_dir }}/scratch/ansible_test/my_collection' |
| 31 |
register: build_current_dir |
| 32 |
|
| 33 |
- name: get result of build basic collection on current directory |
| 34 |
stat: |
| 35 |
path: '{{ galaxy_dir }}/scratch/ansible_test/my_collection/ansible_test-my_collection-1.0.0.tar.gz' |
| 36 |
register: build_current_dir_actual |
| 37 |
|
| 38 |
- name: assert build basic collection based on current directory |
| 39 |
assert: |
| 40 |
that: |
| 41 |
- '"Created collection for ansible_test.my_collection" in build_current_dir.stdout' |
| 42 |
- build_current_dir_actual.stat.exists |
| 43 |
|
| 44 |
- name: build basic collection based on relative dir |
| 45 |
command: ansible-galaxy collection build scratch/ansible_test/my_collection {{ galaxy_verbosity }} |
| 46 |
args: |
| 47 |
chdir: '{{ galaxy_dir }}' |
| 48 |
register: build_relative_dir |
| 49 |
|
| 50 |
- name: get result of build basic collection based on relative dir |
| 51 |
stat: |
| 52 |
path: '{{ galaxy_dir }}/ansible_test-my_collection-1.0.0.tar.gz' |
| 53 |
register: build_relative_dir_actual |
| 54 |
|
| 55 |
- name: assert build basic collection based on relative dir |
| 56 |
assert: |
| 57 |
that: |
| 58 |
- '"Created collection for ansible_test.my_collection" in build_relative_dir.stdout' |
| 59 |
- build_relative_dir_actual.stat.exists |
| 60 |
|
| 61 |
- name: fail to build existing collection without force |
| 62 |
command: ansible-galaxy collection build scratch/ansible_test/my_collection {{ galaxy_verbosity }} |
| 63 |
args: |
| 64 |
chdir: '{{ galaxy_dir }}' |
| 65 |
ignore_errors: yes |
| 66 |
register: build_existing_no_force |
| 67 |
|
| 68 |
- name: build existing collection with force |
| 69 |
command: ansible-galaxy collection build scratch/ansible_test/my_collection --force {{ galaxy_verbosity }} |
| 70 |
args: |
| 71 |
chdir: '{{ galaxy_dir }}' |
| 72 |
register: |
| 73 |
build_existing_force: _task.result |
| 74 |
artifact_path: build_existing_force.stdout_lines | select('search', 'Created collection for ansible_test.my_collection at ') | map('regex_replace', '.* at (.*)$', '\\1') | first |
| 75 |
|
| 76 |
- name: assert build existing collection |
| 77 |
assert: |
| 78 |
that: |
| 79 |
- '"use --force to re-create the collection artifact" in build_existing_no_force.stderr' |
| 80 |
- '"Created collection for ansible_test.my_collection" in build_existing_force.stdout' |
| 81 |
|
| 82 |
- name: Define extraction path |
| 83 |
set_fact: |
| 84 |
extraction_path: "{{ remote_tmp_dir }}/new_ansible_collections_extraction" |
| 85 |
|
| 86 |
- name: Ensure extraction directory exists |
| 87 |
file: |
| 88 |
path: "{{ extraction_path }}" |
| 89 |
state: directory |
| 90 |
mode: '0755' |
| 91 |
|
| 92 |
- name: Extract collection artifact |
| 93 |
unarchive: |
| 94 |
src: "{{ artifact_path }}" |
| 95 |
dest: "{{ extraction_path }}" |
| 96 |
remote_src: yes |
| 97 |
|
| 98 |
- name: Slurp FILES.json content |
| 99 |
slurp: |
| 100 |
src: "{{ extraction_path }}/FILES.json" |
| 101 |
register: |
| 102 |
decoded_files_json: _task.result.content | b64decode | from_json |
| 103 |
files_names: decoded_files_json.files | map(attribute='name') | list |
| 104 |
|
| 105 |
- name: Assert that files are sorted in the predefined ASCII order |
| 106 |
assert: |
| 107 |
that: |
| 108 |
- files_names == expected_files_order |
| 109 |
vars: |
| 110 |
expected_files_order: [ |
| 111 |
".", |
| 112 |
"README.md", |
| 113 |
"docs", |
| 114 |
"meta", |
| 115 |
"meta/runtime.yml", |
| 116 |
"plugins", |
| 117 |
"plugins/README.md", |
| 118 |
"roles" |
| 119 |
] |
| 120 |
|
| 121 |
- name: build collection containing ignored files |
| 122 |
command: ansible-galaxy collection build |
| 123 |
args: |
| 124 |
chdir: '{{ galaxy_dir }}/scratch/ansible_test/ignore' |
| 125 |
|
| 126 |
- name: list the created tar contents |
| 127 |
command: tar -tf {{ galaxy_dir }}/scratch/ansible_test/ignore/ansible_test-ignore-1.0.0.tar.gz |
| 128 |
register: tar_output |
| 129 |
|
| 130 |
- name: assert ignored files are not present in the archive |
| 131 |
assert: |
| 132 |
that: |
| 133 |
- item not in tar_output.stdout_lines |
| 134 |
loop: '{{ collection_ignored_files }}' |
| 135 |
|
| 136 |
- name: assert ignored directories are not present in the archive |
| 137 |
assert: |
| 138 |
that: |
| 139 |
- item not in tar_output.stdout_lines |
| 140 |
loop: '{{ collection_ignored_directories }}' |
| 141 |
|
| 142 |
# Additional comprehensive tests for FILES.json sorting robustness |
| 143 |
- name: Clean up previous extraction for new tests |
| 144 |
file: |
| 145 |
path: "{{ extraction_path }}" |
| 146 |
state: absent |
| 147 |
|
| 148 |
- name: Test multiple builds produce identical FILES.json - Build 1 |
| 149 |
command: ansible-galaxy collection build scratch/ansible_test/my_collection --force {{ galaxy_verbosity }} |
| 150 |
args: |
| 151 |
chdir: '{{ galaxy_dir }}' |
| 152 |
register: |
| 153 |
first_artifact: _task.result.stdout_lines | select('search', 'Created collection for ansible_test.my_collection at ') | map('regex_replace', '.* at (.*)$', '\\1') | first |
| 154 |
|
| 155 |
- name: Create directory for first extraction |
| 156 |
file: |
| 157 |
path: "{{ extraction_path }}_1" |
| 158 |
state: directory |
| 159 |
mode: '0755' |
| 160 |
|
| 161 |
- name: Extract first build |
| 162 |
unarchive: |
| 163 |
src: "{{ first_artifact }}" |
| 164 |
dest: "{{ extraction_path }}_1" |
| 165 |
remote_src: yes |
| 166 |
|
| 167 |
- name: Read FILES.json from first build |
| 168 |
slurp: |
| 169 |
src: "{{ extraction_path }}_1/FILES.json" |
| 170 |
register: |
| 171 |
first_files_content: _task.result.content | b64decode | from_json |
| 172 |
first_order: first_files_content.files | map(attribute='name') | list |
| 173 |
|
| 174 |
- name: Remove first build artifact to ensure clean build |
| 175 |
file: |
| 176 |
path: "{{ first_artifact }}" |
| 177 |
state: absent |
| 178 |
|
| 179 |
- name: Test multiple builds produce identical FILES.json - Build 2 |
| 180 |
command: ansible-galaxy collection build scratch/ansible_test/my_collection --force {{ galaxy_verbosity }} |
| 181 |
args: |
| 182 |
chdir: '{{ galaxy_dir }}' |
| 183 |
register: |
| 184 |
second_artifact: _task.result.stdout_lines | select('search', 'Created collection for ansible_test.my_collection at ') | map('regex_replace', '.* at (.*)$', '\\1') | first |
| 185 |
|
| 186 |
- name: Create directory for second extraction |
| 187 |
file: |
| 188 |
path: "{{ extraction_path }}_2" |
| 189 |
state: directory |
| 190 |
mode: '0755' |
| 191 |
|
| 192 |
- name: Extract second build |
| 193 |
unarchive: |
| 194 |
src: "{{ second_artifact }}" |
| 195 |
dest: "{{ extraction_path }}_2" |
| 196 |
remote_src: yes |
| 197 |
|
| 198 |
- name: Read FILES.json from second build |
| 199 |
slurp: |
| 200 |
src: "{{ extraction_path }}_2/FILES.json" |
| 201 |
register: |
| 202 |
second_files_content: _task.result.content | b64decode | from_json |
| 203 |
second_order: second_files_content.files | map(attribute='name') | list |
| 204 |
|
| 205 |
- name: Verify FILES.json is reproducible across builds |
| 206 |
assert: |
| 207 |
that: |
| 208 |
- first_files_content == second_files_content |
| 209 |
- first_order == second_order |
| 210 |
|
| 211 |
- debug: var=first_order |
| 212 |
- debug: |
| 213 |
var: "{{ item }}" |
| 214 |
loop: |
| 215 |
- first_order |
| 216 |
- first_order | sort(case_sensitive=True) |
| 217 |
|
| 218 |
- name: Verify all files in order are ASCII sorted |
| 219 |
assert: |
| 220 |
that: |
| 221 |
- first_order == (first_order | sort(case_sensitive=True)) |
| 222 |
- second_order == (second_order | sort(case_sensitive=True)) |
| 223 |
|
| 224 |
- name: Validate FILES.json format and keys |
| 225 |
assert: |
| 226 |
that: |
| 227 |
- first_files_content['format'] == 1 |
| 228 |
- second_files_content['format'] == 1 |
| 229 |
- first_files_content['files'] | length > 0 |
| 230 |
- second_files_content['files'] | length > 0 |
| 231 |
|
| 232 |
- name: Verify each file entry has required fields |
| 233 |
assert: |
| 234 |
that: |
| 235 |
- item | dict2items | map(attribute='key') | list | sort == [ |
| 236 |
'chksum_sha256', |
| 237 |
'chksum_type', |
| 238 |
'ftype', |
| 239 |
'format', |
| 240 |
'name'] | sort |
| 241 |
loop: "{{ first_files_content['files'][1:] }}" # Skip root directory entry |
| 242 |
loop_control: |
| 243 |
label: "{{ item.name }}" |
| 244 |
|
| 245 |
- name: Clean up extraction directories |
| 246 |
file: |
| 247 |
path: "{{ item }}" |
| 248 |
state: absent |
| 249 |
loop: |
| 250 |
- "{{ extraction_path }}_1" |
| 251 |
- "{{ extraction_path }}_2" |