Debian Codegraph @

test/integration/targets/ansible-galaxy/runme.sh

raw
1 #!/usr/bin/env bash
2
3 set -eux -o pipefail
4
5 galaxy_testdir="${OUTPUT_DIR}/galaxy-test-dir"
6 role_testdir="${OUTPUT_DIR}/role-test-dir"
7 # Prep the local git repos with role and make a tar archive so we can test
8 # different things
9 galaxy_local_test_role="test-role"
10 galaxy_local_test_role_dir="${OUTPUT_DIR}/galaxy-role-test-root"
11 galaxy_local_test_role_git_repo="${galaxy_local_test_role_dir}/${galaxy_local_test_role}"
12 galaxy_local_test_role_tar="${galaxy_local_test_role_dir}/${galaxy_local_test_role}.tar"
13 galaxy_webserver_root="${OUTPUT_DIR}/ansible-galaxy-webserver"
14
15 mkdir -p "${galaxy_local_test_role_dir}"
16 mkdir -p "${role_testdir}"
17 mkdir -p "${galaxy_webserver_root}"
18
19 ansible-playbook setup.yml "$@"
20
21 trap 'ansible-playbook ${ANSIBLE_PLAYBOOK_DIR}/cleanup.yml' EXIT
22
23 # Very simple version test
24 ansible-galaxy --version
25
26 # Need a relative custom roles path for testing various scenarios of -p
27 galaxy_relative_rolespath="my/custom/roles/path"
28
29 # Status message function (f_ to designate that it's a function)
30 f_ansible_galaxy_status()
31 {
32 printf "\n\n\n### Testing ansible-galaxy: %s\n" "${@}"
33 }
34
35 # Use to initialize a repository. Must call the post function too.
36 f_ansible_galaxy_create_role_repo_pre()
37 {
38 repo_name=$1
39 repo_dir=$2
40
41 pushd "${repo_dir}"
42 ansible-galaxy init "${repo_name}"
43 pushd "${repo_name}"
44 git init .
45
46 # Prep git, because it doesn't work inside a docker container without it
47 git config user.email "[email protected]"
48 git config user.name "Ansible Tester"
49
50 # f_ansible_galaxy_create_role_repo_post
51 }
52
53 # Call after f_ansible_galaxy_create_repo_pre.
54 f_ansible_galaxy_create_role_repo_post()
55 {
56 repo_name=$1
57 repo_tar=$2
58
59 # f_ansible_galaxy_create_role_repo_pre
60
61 git add .
62 git commit -m "local testing ansible galaxy role"
63
64 # NOTE: `HEAD` is used because the newer Git versions create
65 # NOTE: `main` by default and the older ones differ. We
66 # NOTE: want to avoid hardcoding them.
67 git archive \
68 --format=tar \
69 --prefix="${repo_name}/" \
70 HEAD > "${repo_tar}"
71 # Configure basic (insecure) HTTPS-accessible repository
72 galaxy_local_test_role_http_repo="${galaxy_webserver_root}/${galaxy_local_test_role}.git"
73 if [[ ! -d "${galaxy_local_test_role_http_repo}" ]]; then
74 git clone --bare "${galaxy_local_test_role_git_repo}" "${galaxy_local_test_role_http_repo}"
75 pushd "${galaxy_local_test_role_http_repo}"
76 touch "git-daemon-export-ok"
77 git --bare update-server-info
78 mv "hooks/post-update.sample" "hooks/post-update"
79 popd # ${galaxy_local_test_role_http_repo}
80 fi
81 popd # "${repo_name}"
82 popd # "${repo_dir}"
83 }
84
85 f_ansible_galaxy_create_role_repo_pre "${galaxy_local_test_role}" "${galaxy_local_test_role_dir}"
86 f_ansible_galaxy_create_role_repo_post "${galaxy_local_test_role}" "${galaxy_local_test_role_tar}"
87
88 galaxy_local_parent_role="parent-role"
89 galaxy_local_parent_role_dir="${OUTPUT_DIR}/parent-role"
90 mkdir -p "${galaxy_local_parent_role_dir}"
91 galaxy_local_parent_role_git_repo="${galaxy_local_parent_role_dir}/${galaxy_local_parent_role}"
92 galaxy_local_parent_role_tar="${galaxy_local_parent_role_dir}/${galaxy_local_parent_role}.tar"
93
94 # Create parent-role repository
95 f_ansible_galaxy_create_role_repo_pre "${galaxy_local_parent_role}" "${galaxy_local_parent_role_dir}"
96
97 cat <<EOF > meta/requirements.yml
98 - src: git+file:///${galaxy_local_test_role_git_repo}
99 EOF
100 f_ansible_galaxy_create_role_repo_post "${galaxy_local_parent_role}" "${galaxy_local_parent_role_tar}"
101
102 # Galaxy install test case
103 #
104 # Install local git repo
105 f_ansible_galaxy_status "install of local git repo"
106 mkdir -p "${galaxy_testdir}"
107 pushd "${galaxy_testdir}"
108
109 # minimum verbosity is hardcoded to include calls to Galaxy
110 ansible-galaxy install git+file:///"${galaxy_local_test_role_git_repo}" "$@" -vvvv 2>&1 | tee out.txt
111
112 # Test no initial call is made to Galaxy
113 grep out.txt -e "https://galaxy.ansible.com" && cat out.txt && exit 1
114
115 # Test that the role was installed to the expected directory
116 [[ -d "${HOME}/.ansible/roles/${galaxy_local_test_role}" ]]
117 popd # ${galaxy_testdir}
118 rm -fr "${galaxy_testdir}"
119 rm -fr "${HOME}/.ansible/roles/${galaxy_local_test_role}"
120
121 # Galaxy install test case
122 #
123 # Install local git repo and ensure that if a role_path is passed, it is in fact used
124 f_ansible_galaxy_status "install of local git repo with -p \$role_path"
125 mkdir -p "${galaxy_testdir}"
126 pushd "${galaxy_testdir}"
127 mkdir -p "${galaxy_relative_rolespath}"
128
129 ansible-galaxy install git+file:///"${galaxy_local_test_role_git_repo}" -p "${galaxy_relative_rolespath}" "$@"
130
131 # Test that the role was installed to the expected directory
132 [[ -d "${galaxy_relative_rolespath}/${galaxy_local_test_role}" ]]
133 popd # ${galaxy_testdir}
134 rm -fr "${galaxy_testdir}"
135
136 # Galaxy install test case - skipping cert verification
137 #
138 # Install from remote git repo and ensure that cert validation is skipped
139 #
140 # Protect against regression (GitHub Issue #41077)
141 # https://github.com/ansible/ansible/issues/41077
142 f_ansible_galaxy_status "install of role from untrusted repository"
143 mkdir -p "${galaxy_testdir}"
144 pushd "${galaxy_testdir}"
145 mkdir -p "${galaxy_relative_rolespath}"
146
147 # Without --ignore-certs, installing a role from an untrusted repository should fail
148 set +e
149 ansible-galaxy install --verbose git+https://localhost:4443/"${galaxy_local_test_role}.git" -p "${galaxy_relative_rolespath}" "$@" 2>&1 | tee out.txt
150 ansible_exit_code="$?"
151 set -e
152 cat out.txt
153
154 if [[ "$ansible_exit_code" -ne 1 ]]; then echo "Exit code ($ansible_exit_code) is expected to be 1" && exit "$ansible_exit_code"; fi
155 [[ $(grep -c 'ERROR' out.txt) -eq 1 ]]
156 [[ ! -d "${galaxy_relative_rolespath}/${galaxy_local_test_role}" ]]
157
158 ansible-galaxy install --verbose --ignore-certs git+https://localhost:4443/"${galaxy_local_test_role}.git" -p "${galaxy_relative_rolespath}" "$@"
159
160 # Test that the role was installed to the expected directory
161 [[ -d "${galaxy_relative_rolespath}/${galaxy_local_test_role}" ]]
162 popd # ${galaxy_testdir}
163 rm -fr "${galaxy_testdir}"
164
165 # Galaxy install test case
166 #
167 # Install local git repo with a meta/requirements.yml
168 f_ansible_galaxy_status "install of local git repo with meta/requirements.yml"
169 mkdir -p "${galaxy_testdir}"
170 pushd "${galaxy_testdir}"
171
172 ansible-galaxy install git+file:///"${galaxy_local_parent_role_git_repo}" "$@"
173
174 # Test that the role was installed to the expected directory
175 [[ -d "${HOME}/.ansible/roles/${galaxy_local_parent_role}" ]]
176
177 # Test that the dependency was also installed
178 [[ -d "${HOME}/.ansible/roles/${galaxy_local_test_role}" ]]
179
180 popd # ${galaxy_testdir}
181 rm -fr "${galaxy_testdir}"
182 rm -fr "${HOME}/.ansible/roles/${galaxy_local_parent_role}"
183 rm -fr "${HOME}/.ansible/roles/${galaxy_local_test_role}"
184
185 # Galaxy install test case
186 #
187 # Install local git repo with a meta/requirements.yml + --no-deps argument
188 f_ansible_galaxy_status "install of local git repo with meta/requirements.yml + --no-deps argument"
189 mkdir -p "${galaxy_testdir}"
190 pushd "${galaxy_testdir}"
191
192 ansible-galaxy install git+file:///"${galaxy_local_parent_role_git_repo}" --no-deps "$@"
193
194 # Test that the role was installed to the expected directory
195 [[ -d "${HOME}/.ansible/roles/${galaxy_local_parent_role}" ]]
196
197 # Test that the dependency was not installed
198 [[ ! -d "${HOME}/.ansible/roles/${galaxy_local_test_role}" ]]
199
200 popd # ${galaxy_testdir}
201 rm -fr "${galaxy_testdir}"
202 rm -fr "${HOME}/.ansible/roles/${galaxy_local_test_role}"
203
204 # Galaxy install test case (expected failure)
205 #
206 # Install role with a meta/requirements.yml that is not a list of roles
207 mkdir -p "${role_testdir}"
208 pushd "${role_testdir}"
209
210 ansible-galaxy role init --init-path . unsupported_requirements_format
211 cat <<EOF > ./unsupported_requirements_format/meta/requirements.yml
212 roles:
213 - src: git+file:///${galaxy_local_test_role_git_repo}
214 EOF
215 tar czvf unsupported_requirements_format.tar.gz unsupported_requirements_format
216
217 set +e
218 ansible-galaxy role install -p ./roles unsupported_requirements_format.tar.gz 2>&1 | tee out.txt
219 rc="$?"
220 set -e
221
222 # Test that installing the role was an error
223 [[ ! "$rc" == 0 ]]
224 grep out.txt -qe 'Expected role dependencies to be a list.'
225
226 # Test that the role was not installed to the expected directory
227 [[ ! -d "${HOME}/.ansible/roles/unsupported_requirements_format" ]]
228
229 popd # ${role_testdir}
230 rm -rf "${role_testdir}"
231
232 # Galaxy install test case (expected failure)
233 #
234 # Install role with meta/main.yml dependencies that is not a list of roles
235 mkdir -p "${role_testdir}"
236 pushd "${role_testdir}"
237
238 ansible-galaxy role init --init-path . unsupported_requirements_format
239 cat <<EOF > ./unsupported_requirements_format/meta/main.yml
240 galaxy_info:
241 author: Ansible
242 description: test unknown dependency format (expected failure)
243 company: your company (optional)
244 license: license (GPL-2.0-or-later, MIT, etc)
245 min_ansible_version: 2.1
246 galaxy_tags: []
247 dependencies:
248 roles:
249 - src: git+file:///${galaxy_local_test_role_git_repo}
250 EOF
251 tar czvf unsupported_requirements_format.tar.gz unsupported_requirements_format
252
253 set +e
254 ansible-galaxy role install -p ./roles unsupported_requirements_format.tar.gz 2>&1 | tee out.txt
255 rc="$?"
256 set -e
257
258 # Test that installing the role was an error
259 [[ ! "$rc" == 0 ]]
260 grep out.txt -qe 'Expected role dependencies to be a list.'
261
262 # Test that the role was not installed to the expected directory
263 [[ ! -d "${HOME}/.ansible/roles/unsupported_requirements_format" ]]
264
265 popd # ${role_testdir}
266 rm -rf "${role_testdir}"
267
268 # Galaxy install test case
269 #
270 # Ensure that if both a role_file and role_path is provided, they are both
271 # honored
272 #
273 # Protect against regression (GitHub Issue #35217)
274 # https://github.com/ansible/ansible/issues/35217
275
276 f_ansible_galaxy_status \
277 "install of local git repo and local tarball with -p \$role_path and -r \$role_file" \
278 "Protect against regression (Issue #35217)"
279 mkdir -p "${galaxy_testdir}"
280 pushd "${galaxy_testdir}"
281
282 git clone "${galaxy_local_test_role_git_repo}" "${galaxy_local_test_role}"
283 ansible-galaxy init roles-path-bug "$@"
284 pushd roles-path-bug
285 cat <<EOF > ansible.cfg
286 [defaults]
287 roles_path = ../:../../:../roles:roles/
288 EOF
289 cat <<EOF > requirements.yml
290 ---
291 - src: ${galaxy_local_test_role_tar}
292 name: ${galaxy_local_test_role}
293 EOF
294
295 ansible-galaxy install -r requirements.yml -p roles/ "$@"
296 popd # roles-path-bug
297
298 # Test that the role was installed to the expected directory
299 [[ -d "${galaxy_testdir}/roles-path-bug/roles/${galaxy_local_test_role}" ]]
300
301 popd # ${galaxy_testdir}
302 rm -fr "${galaxy_testdir}"
303
304
305 # Galaxy role list tests
306 #
307 # Basic tests to ensure listing roles works
308
309 f_ansible_galaxy_status "role list"
310 mkdir -p "${galaxy_testdir}"
311 pushd "${galaxy_testdir}"
312 ansible-galaxy install git+file:///"${galaxy_local_test_role_git_repo}" "$@"
313
314 ansible-galaxy role list | tee out.txt
315 ansible-galaxy role list test-role | tee -a out.txt
316
317 [[ $(grep -c '^- test-role' out.txt ) -eq 2 ]]
318 popd # ${galaxy_testdir}
319
320 # Galaxy role test case
321 #
322 # Test listing a specific role that is not in the first path in ANSIBLE_ROLES_PATH.
323 # https://github.com/ansible/ansible/issues/60167#issuecomment-585460706
324
325 f_ansible_galaxy_status \
326 "list specific role not in the first path in ANSIBLE_ROLES_PATH"
327
328 mkdir -p "${role_testdir}"
329 pushd "${role_testdir}"
330
331 mkdir testroles
332 ansible-galaxy role init --init-path ./local-roles quark
333 ANSIBLE_ROLES_PATH=./local-roles:${HOME}/.ansible/roles ansible-galaxy role list quark | tee out.txt
334
335 [[ $(grep -c 'not found' out.txt) -eq 0 ]]
336
337 ANSIBLE_ROLES_PATH=${HOME}/.ansible/roles:./local-roles ansible-galaxy role list quark | tee out.txt
338
339 [[ $(grep -c 'not found' out.txt) -eq 0 ]]
340
341 popd # ${role_testdir}
342 rm -fr "${role_testdir}"
343
344
345 # Galaxy role info tests
346
347 # Get info about role that is not installed
348
349 f_ansible_galaxy_status "role info"
350 mkdir -p "${galaxy_testdir}"
351 pushd "${galaxy_testdir}"
352 ansible-galaxy role info samdoran.fish | tee out.txt
353
354 [[ $(grep -c 'not found' out.txt ) -eq 0 ]]
355 [[ $(grep -c 'Role:.*samdoran\.fish' out.txt ) -eq 1 ]]
356
357 popd # ${galaxy_testdir}
358
359 f_ansible_galaxy_status \
360 "role info non-existent role"
361
362 mkdir -p "${role_testdir}"
363 pushd "${role_testdir}"
364
365 ansible-galaxy role info notaroll | tee out.txt
366
367 grep -- '- the role notaroll was not found' out.txt
368
369 f_ansible_galaxy_status \
370 "role info description offline"
371
372 mkdir testroles
373 ansible-galaxy role init testdesc --init-path ./testroles
374
375 # Only galaxy_info['description'] exists in file
376 sed -i -e 's#[[:space:]]\{1,\}description:.*$# description: Description in galaxy_info#' ./testroles/testdesc/meta/main.yml
377 ansible-galaxy role info -p ./testroles --offline testdesc | tee out.txt
378 grep 'description: Description in galaxy_info' out.txt
379
380 # Both top level 'description' and galaxy_info['description'] exist in file
381 # Use shell-fu instead of sed to prepend a line to a file because BSD
382 # and macOS sed don't work the same as GNU sed.
383 echo 'description: Top level' | \
384 cat - ./testroles/testdesc/meta/main.yml > tmp.yml && \
385 mv tmp.yml ./testroles/testdesc/meta/main.yml
386 ansible-galaxy role info -p ./testroles --offline testdesc | tee out.txt
387 grep 'description: Top level' out.txt
388
389 # Only top level 'description' exists in file
390 sed -i.bak '/^[[:space:]]\{1,\}description: Description in galaxy_info/d' ./testroles/testdesc/meta/main.yml
391 ansible-galaxy role info -p ./testroles --offline testdesc | tee out.txt
392 grep 'description: Top level' out.txt
393
394 # test multiple role listing
395 ansible-galaxy role init otherrole --init-path ./testroles
396 ansible-galaxy role info -p ./testroles --offline testdesc otherrole | tee out.txt
397 grep 'Role: testdesc' out.txt
398 grep 'Role: otherrole' out.txt
399
400
401 popd # ${role_testdir}
402 rm -fr "${role_testdir}"
403
404 # Properly list roles when the role name is a subset of the path, or the role
405 # name is the same name as the parent directory of the role. Issue #67365
406 #
407 # ./parrot/parrot
408 # ./parrot/arr
409 # ./testing-roles/test
410
411 f_ansible_galaxy_status \
412 "list roles where the role name is the same or a subset of the role path (#67365)"
413
414 mkdir -p "${role_testdir}"
415 pushd "${role_testdir}"
416
417 mkdir parrot
418 ansible-galaxy role init --init-path ./parrot parrot
419 ansible-galaxy role init --init-path ./parrot parrot-ship
420 ansible-galaxy role init --init-path ./parrot arr
421
422 ansible-galaxy role list -p ./parrot | tee out.txt
423
424 [[ $(grep -Ec '\- (parrot|arr)' out.txt) -eq 3 ]]
425 ansible-galaxy role list test-role | tee -a out.txt
426
427 popd # ${role_testdir}
428 rm -rf "${role_testdir}"
429
430 f_ansible_galaxy_status \
431 "Test role with non-ascii characters"
432
433 mkdir -p "${role_testdir}"
434 pushd "${role_testdir}"
435
436 mkdir nonascii
437 ansible-galaxy role init --init-path ./nonascii nonascii
438 touch nonascii/ÅÑŚÌβŁÈ.txt
439 tar czvf nonascii.tar.gz nonascii
440 ansible-galaxy role install -p ./roles nonascii.tar.gz
441
442 popd # ${role_testdir}
443 rm -rf "${role_testdir}"
444
445 f_ansible_galaxy_status \
446 "Test if git hidden directories are skipped while using role skeleton (#71977)"
447
448 role_testdir=$(mktemp -d)
449 pushd "${role_testdir}"
450
451 ansible-galaxy role init sample-role-skeleton
452 git init ./sample-role-skeleton
453 ansible-galaxy role init --role-skeleton=sample-role-skeleton example
454
455 popd # ${role_testdir}
456 rm -rf "${role_testdir}"
457
458 #################################
459 # ansible-galaxy collection tests
460 #################################
461 # TODO: Move these to ansible-galaxy-collection
462
463 mkdir -p "${galaxy_testdir}"
464 pushd "${galaxy_testdir}"
465
466 ## ansible-galaxy collection list tests
467
468 # Create more collections and put them in various places
469 f_ansible_galaxy_status \
470 "setting up for collection list tests"
471
472 rm -rf ansible_test/* install/*
473
474 NAMES=(zoo museum airport)
475 for n in "${NAMES[@]}"; do
476 ansible-galaxy collection init "ansible_test.$n"
477 ansible-galaxy collection build "ansible_test/$n"
478 done
479
480 ansible-galaxy collection install ansible_test-zoo-1.0.0.tar.gz
481 ansible-galaxy collection install ansible_test-museum-1.0.0.tar.gz -p ./install
482 ansible-galaxy collection install ansible_test-airport-1.0.0.tar.gz -p ./local
483
484 # Change the collection version and install to another location
485 sed -i -e 's#^version:.*#version: 2.5.0#' ansible_test/zoo/galaxy.yml
486 ansible-galaxy collection build ansible_test/zoo
487 ansible-galaxy collection install ansible_test-zoo-2.5.0.tar.gz -p ./local
488
489 # Test listing a collection that contains a galaxy.yml
490 ansible-galaxy collection init "ansible_test.development"
491 mv ./ansible_test/development "${galaxy_testdir}/local/ansible_collections/ansible_test/"
492
493 export ANSIBLE_COLLECTIONS_PATH=~/.ansible/collections:${galaxy_testdir}/local
494
495 f_ansible_galaxy_status \
496 "collection list all collections"
497
498 ansible-galaxy collection list -p ./install | tee out.txt
499
500 [[ $(grep -c ansible_test out.txt) -eq 5 ]]
501
502 f_ansible_galaxy_status \
503 "collection list specific collection"
504
505 ansible-galaxy collection list -p ./install ansible_test.airport | tee out.txt
506
507 [[ $(grep -c 'ansible_test\.airport' out.txt) -eq 1 ]]
508
509 f_ansible_galaxy_status \
510 "collection list specific collection which contains galaxy.yml"
511
512 ansible-galaxy collection list -p ./install ansible_test.development 2>&1 | tee out.txt
513
514 [[ $(grep -c 'ansible_test\.development' out.txt) -eq 1 ]]
515 [[ $(grep -c 'WARNING' out.txt) -eq 0 ]]
516
517 f_ansible_galaxy_status \
518 "collection list specific collection found in multiple places"
519
520 ansible-galaxy collection list -p ./install ansible_test.zoo | tee out.txt
521
522 [[ $(grep -c 'ansible_test\.zoo' out.txt) -eq 2 ]]
523
524 f_ansible_galaxy_status \
525 "collection list all with duplicate paths"
526
527 ansible-galaxy collection list -p ~/.ansible/collections | tee out.txt
528
529 [[ $(grep -c "# ${HOME}/.ansible/collections/ansible_collections" out.txt) -eq 1 ]]
530
531 f_ansible_galaxy_status \
532 "collection list invalid collection name"
533
534 ansible-galaxy collection list -p ./install dirty.wraughten.name "$@" 2>&1 | tee out.txt || echo "expected failure"
535
536 grep '\[ERROR\]: Invalid collection name' out.txt
537
538 f_ansible_galaxy_status \
539 "collection list path not found"
540
541 ansible-galaxy collection list -p ./nope "$@" 2>&1 | tee out.txt || echo "expected failure"
542
543 grep '\[WARNING\]: - the configured path' out.txt
544
545 f_ansible_galaxy_status \
546 "collection list missing ansible_collections dir inside path"
547
548 mkdir emptydir
549
550 ansible-galaxy collection list -p ./emptydir "$@"
551
552 rmdir emptydir
553
554 unset ANSIBLE_COLLECTIONS_PATH
555
556 f_ansible_galaxy_status \
557 "collection list with collections installed from python package"
558
559 mkdir -p test-site-packages
560 ln -s "${galaxy_testdir}/local/ansible_collections" test-site-packages/ansible_collections
561 ansible-galaxy collection list
562 PYTHONPATH="./test-site-packages/:$PYTHONPATH" ansible-galaxy collection list | tee out.txt
563
564 grep ".ansible/collections/ansible_collections" out.txt
565 grep "test-site-packages/ansible_collections" out.txt
566
567 ## end ansible-galaxy collection list
568
569
570 popd # ${galaxy_testdir}
571
572 rm -fr "${galaxy_testdir}"
573
574 rm -fr "${galaxy_local_test_role_dir}"