Debian Codegraph @

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

raw
1 #!/usr/bin/env bash
2
3 # always set logical error behaviors, enable execution tracing later if sufficient verbosity requested
4 set -eu
5
6 verbosity=0
7
8 # default to silent output for naked grep; -vvv+ will adjust this
9 GREP_OPTS=(-q)
10
11 # shell tracing output is very large from this script; only enable if >= -vvv was passed
12 while getopts :v opt
13 do case "$opt" in
14 v) ((verbosity+=1)) ;;
15 *) ;;
16 esac
17 done
18
19 if (( verbosity >= 3 ));
20 then
21 set -x
22 GREP_OPTS=()
23 fi
24
25 echo "running playbook-backed docs tests"
26 ansible-playbook test.yml -i inventory "$@"
27
28 # test keyword docs
29 ansible-doc -t keyword -l | grep "${GREP_OPTS[@]}" 'vars_prompt: list of variables to prompt for.'
30 ansible-doc -t keyword vars_prompt | grep "${GREP_OPTS[@]}" 'description: list of variables to prompt for.'
31 ansible-doc -t keyword asldkfjaslidfhals 2>&1 | grep "${GREP_OPTS[@]}" 'Skipping invalid keyword'
32
33 echo "Check if deprecation collection name is printed"
34 ansible-doc --playbook-dir ./ testns.testcol.randommodule 2>&1 | grep "${GREP_OPTS[@]}" "Will be removed in: testns.testcol"
35
36 # collections testing
37 (
38 unset ANSIBLE_PLAYBOOK_DIR
39 export ANSIBLE_NOCOLOR=1
40
41 cd "$(dirname "$0")"
42
43
44 echo "test fakemodule docs from collection"
45 # we use sed to strip the module path from the first line
46 current_out="$(ansible-doc --playbook-dir ./ testns.testcol.fakemodule | sed '1 s/\(^> MODULE testns\.testcol\.fakemodule\).*(.*)$/\1/')"
47 expected_out="$(sed '1 s/\(^> MODULE testns\.testcol\.fakemodule\).*(.*)$/\1/' fakemodule.output)"
48 test "$current_out" == "$expected_out"
49
50 echo "test randommodule docs from collection"
51 # we use sed to strip the plugin path from the first line, and fix-urls.py to unbreak and replace URLs from stable-X branches
52 current_out="$(ansible-doc --playbook-dir ./ testns.testcol.randommodule | sed '1 s/\(^> MODULE testns\.testcol\.randommodule\).*(.*)$/\1/' | python fix-urls.py)"
53 expected_out="$(sed '1 s/\(^> MODULE testns\.testcol\.randommodule\).*(.*)$/\1/' randommodule-text.output)"
54 test "$current_out" == "$expected_out"
55
56 echo "test yolo filter docs from collection"
57 # we use sed to strip the plugin path from the first line, and fix-urls.py to unbreak and replace URLs from stable-X branches
58 current_out="$(ansible-doc --playbook-dir ./ testns.testcol.yolo --type test | sed '1 s/\(^> TEST testns\.testcol\.yolo\).*(.*)$/\1/' | python fix-urls.py)"
59 expected_out="$(sed '1 s/\(^> TEST testns\.testcol\.yolo\).*(.*)$/\1/' yolo-text.output)"
60 test "$current_out" == "$expected_out"
61
62 # PAGER is set to less by default, so we need to test with other pagers
63 for pager in more cat; do
64 echo "testing with pager $pager"
65 PAGER=$pager ansible-doc --list testns.testcol --playbook-dir ./ 2>&1 | grep "${GREP_OPTS[@]}" -v "Invalid collection name"
66 done
67
68 # set pager to empty string
69 echo "testing with pager empty"
70 PAGER="" ansible-doc --list testns.testcol --playbook-dir ./ 2>&1 | grep "${GREP_OPTS[@]}" -v "Invalid collection name"
71
72 # ensure we do work with valid collection name for list
73 ansible-doc --list testns.testcol --playbook-dir ./ 2>&1 | grep -v "Invalid collection name"
74
75 echo "ensure we do work with valid collection name for list"
76 ansible-doc --list testns.testcol --playbook-dir ./ 2>&1 | grep "${GREP_OPTS[@]}" -v "Invalid collection name"
77
78 echo "ensure we dont break on invalid collection name for list"
79 ansible-doc --list testns.testcol.fakemodule --playbook-dir ./ 2>&1 | grep "${GREP_OPTS[@]}" "Invalid collection name"
80
81 echo "filter list with more than one collection (1/2)"
82 output=$(ansible-doc --list testns.testcol3 testns.testcol4 --playbook-dir ./ 2>&1 | wc -l)
83 test "$output" -eq 2
84
85 echo "filter list with more than one collection (2/2)"
86 output=$(ansible-doc --list testns.testcol testns.testcol4 --playbook-dir ./ 2>&1 | wc -l)
87 test "$output" -eq 5
88
89 echo "testing ansible-doc output for various plugin types"
90 for ptype in cache inventory lookup vars filter module
91 do
92 # each plugin type adds 1 from collection
93 # FIXME pre=$(ansible-doc -l -t ${ptype}|wc -l)
94 # FIXME post=$(ansible-doc -l -t ${ptype} --playbook-dir ./|wc -l)
95 # FIXME test "$pre" -eq $((post - 1))
96 if [ "${ptype}" == "filter" ]; then
97 expected=5
98 expected_names=("b64decode" "filter_subdir.nested" "filter_subdir.noop" "noop" "ultimatequestion")
99 elif [ "${ptype}" == "module" ]; then
100 expected=4
101 expected_names=("fakemodule" "notrealmodule" "randommodule" "database.database_type.subdir_module")
102 else
103 expected=1
104 if [ "${ptype}" == "cache" ]; then expected_names=("notjsonfile");
105 elif [ "${ptype}" == "inventory" ]; then expected_names=("statichost");
106 elif [ "${ptype}" == "lookup" ]; then expected_names=("noop");
107 elif [ "${ptype}" == "vars" ]; then expected_names=("noop_vars_plugin"); fi
108 fi
109 echo "testing collection-filtered list for plugin ${ptype}"
110 justcol=$(ansible-doc -l -t ${ptype} --playbook-dir ./ testns.testcol|wc -l)
111 test "$justcol" -eq "$expected"
112
113 echo "validate collection plugin name display for plugin ${ptype}"
114 list_result=$(ansible-doc -l -t ${ptype} --playbook-dir ./ testns.testcol)
115 metadata_result=$(ansible-doc --metadata-dump --no-fail-on-errors -t ${ptype} --playbook-dir ./ testns.testcol)
116 for name in "${expected_names[@]}"; do
117 echo "${list_result}" | grep "${GREP_OPTS[@]}" "testns.testcol.${name}"
118 echo "${metadata_result}" | grep "${GREP_OPTS[@]}" "testns.testcol.${name}"
119 done
120
121 # ensure we get error if passing invalid collection, much less any plugins
122 ansible-doc -l -t ${ptype} bogus.boguscoll 2>&1 | grep "${GREP_OPTS[@]}" "unable to locate collection"
123
124 # TODO: do we want per namespace?
125 # ensure we get 1 plugins when restricting namespace
126 #justcol=$(ansible-doc -l -t ${ptype} --playbook-dir ./ testns|wc -l)
127 #test "$justcol" -eq 1
128 done
129
130 #### test role functionality
131
132 echo "testing role text output"
133 # we use sed to strip the role path from the first line
134 current_role_out="$(ansible-doc -t role -r ./roles test_role1 | sed '1 s/\(^> ROLE: \*test_role1\*\).*(.*)$/\1/' | python fix-urls.py)"
135 expected_role_out="$(sed '1 s/\(^> ROLE: \*test_role1\*\).*(.*)$/\1/' fakerole.output)"
136 test "$current_role_out" == "$expected_role_out"
137
138 echo "testing multiple role entrypoints"
139 # Two collection roles are defined, but only 1 has a role arg spec with 2 entry points
140 output=$(ansible-doc -t role -l --playbook-dir . testns.testcol | wc -l)
141 test "$output" -eq 4
142
143 echo "test listing roles with multiple collection filters"
144 # Two collection roles are defined, but only 1 has a role arg spec with 2 entry points
145 output=$(ansible-doc -t role -l --playbook-dir . testns.testcol2 testns.testcol | wc -l)
146 test "$output" -eq 4
147
148 echo "testing standalone roles"
149 # Include normal roles (no collection filter)
150 output=$(ansible-doc -t role -l --playbook-dir . | wc -l)
151 test "$output" -eq 11
152
153 echo "testing role precedence"
154 # Test that a role in the playbook dir with the same name as a role in the
155 # 'roles' subdir of the playbook dir does not appear (lower precedence).
156 output=$(ansible-doc -t role -l --playbook-dir . | grep -c "test_role1 from roles subdir")
157 test "$output" -eq 1
158 output=$(ansible-doc -t role -l --playbook-dir . | grep -c "test_role1 from playbook dir" || true)
159 test "$output" -eq 0
160
161 echo "testing role entrypoint filter"
162 current_role_out="$(ansible-doc -t role --playbook-dir . testns.testcol.testrole -e alternate| sed '1 s/\(^> ROLE: \*testns\.testcol\.testrole\*\).*(.*)$/\1/')"
163 expected_role_out="$(sed '1 s/\(^> ROLE: \*testns\.testcol\.testrole\*\).*(.*)$/\1/' fakecollrole.output)"
164 test "$current_role_out" == "$expected_role_out"
165
166 )
167
168 #### test add_collection_to_versions_and_dates()
169
170 echo "testing json output"
171 current_out="$(ansible-doc --json --playbook-dir ./ testns.testcol.randommodule | sed 's/ *$//' | sed 's/ *"filename": "[^"]*",$//')"
172 expected_out="$(sed 's/ *"filename": "[^"]*",$//' randommodule.output)"
173 test "$current_out" == "$expected_out"
174
175 echo "testing json output 2"
176 current_out="$(ansible-doc --json --playbook-dir ./ testns.testcol.yolo --type test | sed 's/ *$//' | sed 's/ *"filename": "[^"]*",$//')"
177 expected_out="$(sed 's/ *"filename": "[^"]*",$//' yolo.output)"
178 test "$current_out" == "$expected_out"
179
180 current_out="$(ansible-doc --json --playbook-dir ./ -t cache testns.testcol.notjsonfile | sed 's/ *$//' | sed 's/ *"filename": "[^"]*",$//')"
181 expected_out="$(sed 's/ *"filename": "[^"]*",$//' notjsonfile.output)"
182 test "$current_out" == "$expected_out"
183
184 current_out="$(ansible-doc --json --playbook-dir ./ -t lookup testns.testcol.noop | sed 's/ *$//' | sed 's/ *"filename": "[^"]*",$//')"
185 expected_out="$(sed 's/ *"filename": "[^"]*",$//' noop.output)"
186 test "$current_out" == "$expected_out"
187
188 current_out="$(ansible-doc --json --playbook-dir ./ -t vars testns.testcol.noop_vars_plugin | sed 's/ *$//' | sed 's/ *"filename": "[^"]*",$//')"
189 expected_out="$(sed 's/ *"filename": "[^"]*",$//' noop_vars_plugin.output)"
190 test "$current_out" == "$expected_out"
191
192 echo "testing metadata dump"
193 # just ensure it runs
194 ANSIBLE_LIBRARY='./nolibrary' ansible-doc --metadata-dump --playbook-dir /dev/null 1>/dev/null 2>&1
195
196 echo "test metadata dump on broken role metadata"
197 mkdir -p broken-docs/collections/ansible_collections/testns/testcol/roles/testrole/meta
198 cat <<EOF > broken-docs/collections/ansible_collections/testns/testcol/roles/testrole/meta/main.yml
199 ---
200 dependencies:
201 galaxy_info:
202
203 argument_specs:
204 main:
205 short_description: testns.testcol.testrole short description for main entry point
206 description:
207 - Longer description for testns.testcol.testrole main entry point.
208 author: Ansible Core (@ansible)
209 options:
210 opt1:
211 description: opt1 description
212 broken:
213 type: "str"
214 required: true
215 EOF
216
217 # ensure that --metadata-dump does not fail when --no-fail-on-errors is supplied
218 ANSIBLE_LIBRARY='./nolibrary' ansible-doc --metadata-dump --no-fail-on-errors --playbook-dir broken-docs testns.testcol 1>/dev/null 2>&1
219
220 # ensure that --metadata-dump does fail when --no-fail-on-errors is not supplied
221 output=$(ANSIBLE_LIBRARY='./nolibrary' ansible-doc --metadata-dump --playbook-dir broken-docs testns.testcol 2>&1 | grep -c 'ERROR' || true)
222 test "${output}" -eq 1
223
224 # ensure --metadata-dump does not crash if the ansible_collections is nested (https://github.com/ansible/ansible/issues/84909)
225 testdir="$(pwd)"
226 pbdir="collections/ansible_collections/testns/testcol/playbooks"
227 cd "$pbdir"
228 ANSIBLE_COLLECTIONS_PATH="$testdir/$pbdir/collections" ansible-doc -vvv --metadata-dump --no-fail-on-errors
229 cd "$testdir"
230
231 echo "test doc list on broken role metadata"
232 # ensure that role doc does not fail when --no-fail-on-errors is supplied
233 ANSIBLE_LIBRARY='./nolibrary' ansible-doc --no-fail-on-errors --playbook-dir broken-docs testns.testcol.testrole -t role 1>/dev/null 2>&1
234
235 # ensure that role doc does fail when --no-fail-on-errors is not supplied
236 output=$(ANSIBLE_LIBRARY='./nolibrary' ansible-doc --playbook-dir broken-docs testns.testcol.testrole -t role 2>&1 | grep -c 'ERROR' || true)
237 test "${output}" -eq 1
238
239 echo "testing legacy plugin listing"
240 [ "$(ansible-doc -M ./library -l ansible.legacy |wc -l)" -gt "0" ]
241
242 echo "testing legacy plugin list via --playbook-dir"
243 [ "$(ansible-doc -l ansible.legacy --playbook-dir ./|wc -l)" -gt "0" ]
244
245 echo "testing undocumented plugin output"
246 [ "$(ansible-doc -M ./library -l ansible.legacy |grep -c UNDOCUMENTED)" == "6" ]
247
248 echo "testing filtering does not include any 'test_' modules"
249 [ "$(ansible-doc -M ./library -l ansible.builtin |grep -c test_)" == 0 ]
250 [ "$(ansible-doc --playbook-dir ./ -l ansible.builtin |grep -c test_)" == 0 ]
251
252 echo "testing filtering still shows modules"
253 count=$(ANSIBLE_LIBRARY='./nolibrary' ansible-doc -l ansible.builtin |wc -l)
254 [ "${count}" -gt "0" ]
255 [ "$(ansible-doc -M ./library -l ansible.builtin |wc -l)" == "${count}" ]
256 [ "$(ansible-doc --playbook-dir ./ -l ansible.builtin |wc -l)" == "${count}" ]
257
258
259 echo "testing sidecar docs for jinja plugins"
260 [ "$(ansible-doc -t test --playbook-dir ./ testns.testcol.yolo| wc -l)" -gt "0" ]
261 [ "$(ansible-doc -t filter --playbook-dir ./ donothing| wc -l)" -gt "0" ]
262 [ "$(ansible-doc -t filter --playbook-dir ./ ansible.legacy.donothing| wc -l)" -gt "0" ]
263 [ "$(ansible-doc -t filter --playbook-dir ./ testns.testcol.filter_subdir.nested| wc -l)" -gt "0" ]
264
265 echo "testing no docs and no sidecar"
266 ansible-doc -t filter --playbook-dir ./ nodocs 2>&1| grep "${GREP_OPTS[@]}" -c 'missing documentation' || true
267
268 echo "testing sidecar docs for module"
269 [ "$(ansible-doc -M ./library test_win_module| wc -l)" -gt "0" ]
270 [ "$(ansible-doc --playbook-dir ./ test_win_module| wc -l)" -gt "0" ]
271
272 echo "testing duplicate DOCUMENTATION"
273 [ "$(ansible-doc --playbook-dir ./ double_doc| wc -l)" -gt "0" ]
274
275 echo "testing don't break on module dir"
276 ansible-doc --list --module-path ./modules > /dev/null
277
278 echo "testing dedupe by fqcn and not base name"
279 [ "$(ansible-doc -l -t filter --playbook-dir ./ |grep -c 'b64decode')" -eq "3" ]
280
281 echo "testing no duplicates for plugins that only exist in ansible.builtin when listing ansible.legacy plugins"
282 [ "$(ansible-doc -l -t filter --playbook-dir ./ |grep -c 'b64encode')" -eq "1" ]
283
284 echo "testing with playbook dir, legacy should override"
285 ansible-doc -t filter split --playbook-dir ./ -v|grep "${GREP_OPTS[@]}" hysterical
286
287 pyc_src="$(pwd)/filter_plugins/other.py"
288 pyc_1="$(pwd)/filter_plugins/split.pyc"
289 pyc_2="$(pwd)/library/notaplugin.pyc"
290 trap 'rm -rf "$pyc_1" "$pyc_2"' EXIT
291
292 echo "testing pyc files are not used as adjacent documentation"
293 python -c "import py_compile; py_compile.compile('$pyc_src', cfile='$pyc_1')"
294 ansible-doc -t filter split --playbook-dir ./ -v|grep "${GREP_OPTS[@]}" hysterical
295
296 echo "testing pyc files are not listed as plugins"
297 python -c "import py_compile; py_compile.compile('$pyc_src', cfile='$pyc_2')"
298 test "$(ansible-doc -l -t module --playbook-dir ./ 2>&1 1>/dev/null |grep -c "notaplugin")" == 0
299
300 echo "testing without playbook dir, builtin should return"
301 ansible-doc -t filter split -v 2>&1 |grep "${GREP_OPTS[@]}" -v hysterical
302
303 echo "test 'sidecar' for no extension module with .py doc"
304 [ "$(ansible-doc -M ./library -l ansible.legacy |grep -v 'UNDOCUMENTED' |grep -c bogus_facts)" == "1" ]
305
306 echo "test 'sidecar' for no extension module with .yml doc"
307 [ "$(ansible-doc -M ./library -l ansible.legacy |grep -v 'UNDOCUMENTED' |grep -c facts_one)" == "1" ]
308
309 echo "test j2 plugins get jinja2 instead of path"
310 ansible-doc -t filter map 2>&1 |grep "${GREP_OPTS[@]}" '(Jinja2)'
311
312 echo "test missing description in test_role4 argument spec"
313 ansible-doc -t role -r ./roles test_role4 2>&1 >/dev/null | grep -q 'Error extracting role docs from '\''test_role4'\'': All (sub-)options and return values must have a '\''description'\'' field'