Debian Codegraph @

test/integration/targets/callback_default/runme.sh

raw
1 #!/usr/bin/env bash
2
3 # DTFIX-FUTURE: convert this to the new output validation test format, which will ignore things like host path changes
4
5 # This test compares "known good" output with various settings against output
6 # with the current code. It's brittle by nature, but this is probably the
7 # "best" approach possible.
8 #
9 # Notes:
10 # * options passed to this script (such as -v) are ignored, as they would change
11 # the output and break the test
12 # * the number of asterisks after a "banner" differs depending on the number of
13 # columns on the TTY, so we must adjust the columns for the current session
14 # for consistency
15
16 set -eux
17
18 export ANSIBLE_DISPLAY_TRACEBACK=never # override anything ansible-test set, it will screw up the test diffs
19
20 umask 0022
21
22 run_test() {
23 local testname=$1
24 local playbook=$2
25
26 # output was recorded w/o cowsay, ensure we reproduce the same
27 export ANSIBLE_NOCOWS=1
28
29 # The shenanigans with redirection and 'tee' are to capture STDOUT and
30 # STDERR separately while still displaying both to the console
31 { ansible-playbook -i inventory "$playbook" "${@:3}" \
32 > >(set +x; tee "${OUTFILE}.${testname}.stdout"); } \
33 2> >(set +x; tee "${OUTFILE}.${testname}.stderr" >&2)
34 sed -i -e 's/included: .*\/test\/integration/included: ...\/test\/integration/g' "${OUTFILE}.${testname}.stdout"
35 sed -i -e 's/@@ -1,1 +1,1 @@/@@ -1 +1 @@/g' "${OUTFILE}.${testname}.stdout"
36 sed -i -e 's/: .*\/test_diff\.txt/: ...\/test_diff.txt/g' "${OUTFILE}.${testname}.stdout"
37 sed -i -e "s#${ANSIBLE_PLAYBOOK_DIR}#TEST_PATH#g" "${OUTFILE}.${testname}.stdout" "${OUTFILE}.${testname}.stderr"
38 sed -i -e 's/^Using .*//g' "${OUTFILE}.${testname}.stdout"
39 sed -i -e 's/[0-9]:[0-9]\{2\}:[0-9]\{2\}\.[0-9]\{6\}/0:00:00.000000/g' "${OUTFILE}.${testname}.stdout"
40 sed -i -e 's/[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\} [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}\.[0-9]\{6\}/0000-00-00 00:00:00.000000/g' "${OUTFILE}.${testname}.stdout"
41 sed -i -e 's#: .*/\.source\.txt$#: .../.source.txt#g' "${OUTFILE}.${testname}.stdout"
42 sed -i -e '/secontext:/d' "${OUTFILE}.${testname}.stdout"
43
44 # normalize gid/group/owner/uid/homedir so tests can run as non-root user
45 ESC_HOME=$(echo "${HOME}" | sed -e 's/\//\\\//g')
46 sed -i -e "s/${ESC_HOME}/\/<<HOMEDIR>>/g" "${OUTFILE}.${testname}.stdout"
47 sed -i -e "s/${ESC_HOME}/\/<<HOMEDIR>>/g" "${OUTFILE}.${testname}.stderr"
48 sed -i -e "s/gid: $(id -g)/gid: <<GID>>/g" "${OUTFILE}.${testname}.stdout"
49 sed -i -e "s/group: $(id -gn)/group: <<GROUP>>/g" "${OUTFILE}.${testname}.stdout"
50 sed -i -e "s/owner: $(id -un)/owner: <<OWNER>>/g" "${OUTFILE}.${testname}.stdout"
51 sed -i -e "s/uid: $(id -u)/uid: <<UID>>/g" "${OUTFILE}.${testname}.stdout"
52
53 diff -u "${ORIGFILE}.${testname}.stdout" "${OUTFILE}.${testname}.stdout" || diff_failure
54 diff -u "${ORIGFILE}.${testname}.stderr" "${OUTFILE}.${testname}.stderr" || diff_failure
55 }
56
57 run_test_dryrun() {
58 local testname=$1
59 # optional, pass --check to run a dry run
60 local chk=${2:-}
61
62 # output was recorded w/o cowsay, ensure we reproduce the same
63 export ANSIBLE_NOCOWS=1
64
65 # This needed to satisfy shellcheck that can not accept unquoted variable
66 cmd="ansible-playbook -i inventory ${chk} test_dryrun.yml"
67
68 # The shenanigans with redirection and 'tee' are to capture STDOUT and
69 # STDERR separately while still displaying both to the console
70 { $cmd \
71 > >(set +x; tee "${OUTFILE}.${testname}.stdout"); } \
72 2> >(set +x; tee "${OUTFILE}.${testname}.stderr" >&2)
73
74 diff -u "${ORIGFILE}.${testname}.stdout" "${OUTFILE}.${testname}.stdout" || diff_failure
75 diff -u "${ORIGFILE}.${testname}.stderr" "${OUTFILE}.${testname}.stderr" || diff_failure
76 }
77
78 diff_failure() {
79 if [[ $INIT = 0 ]]; then
80 echo "FAILURE...diff mismatch!"
81 exit 1
82 fi
83 }
84
85 cleanup() {
86 if [[ $INIT = 0 ]]; then
87 rm -rf "${OUTFILE}.*"
88 fi
89
90 if [[ -f "${BASEFILE}.unreachable.stdout" ]]; then
91 rm -rf "${BASEFILE}.unreachable.stdout"
92 fi
93
94 if [[ -f "${BASEFILE}.unreachable.stderr" ]]; then
95 rm -rf "${BASEFILE}.unreachable.stderr"
96 fi
97
98 # Restore TTY cols
99 if [[ -n ${TTY_COLS:-} ]]; then
100 stty cols "${TTY_COLS}"
101 fi
102 }
103
104 adjust_tty_cols() {
105 if [[ -t 1 ]]; then
106 # Preserve existing TTY cols
107 TTY_COLS=$( stty -a | grep -Eo '; columns [0-9]+;' | cut -d';' -f2 | cut -d' ' -f3 )
108 # Override TTY cols to make comparing ansible-playbook output easier
109 # This value matches the default in the code when there is no TTY
110 stty cols 79
111 fi
112 }
113
114 BASEFILE=callback_default.out
115
116 ORIGFILE="${BASEFILE}"
117 OUTFILE="${BASEFILE}.new"
118
119 trap 'cleanup' EXIT
120
121 # The --init flag will (re)generate the "good" output files used by the tests
122 INIT=0
123 if [[ ${1:-} == "--init" ]]; then
124 shift
125 OUTFILE=$ORIGFILE
126 INIT=1
127 fi
128
129 adjust_tty_cols
130
131 # Force the 'default' callback plugin, since that's what we're testing
132 export ANSIBLE_STDOUT_CALLBACK=default
133 # Disable color in output for consistency
134 export ANSIBLE_FORCE_COLOR=0
135 export ANSIBLE_NOCOLOR=1
136
137 # Default settings
138 export ANSIBLE_DISPLAY_SKIPPED_HOSTS=1
139 export ANSIBLE_DISPLAY_OK_HOSTS=1
140 export ANSIBLE_DISPLAY_FAILED_STDERR=0
141 export ANSIBLE_CHECK_MODE_MARKERS=0
142
143 run_test default test.yml
144
145 set +e
146 ANSIBLE_CALLBACKS_ENABLED=default run_test include_role_fails test_include_role_fails.yml
147 set -e
148
149 # Check for async output
150 # NOTE: regex to match 1 or more digits works for both BSD and GNU grep
151 ansible-playbook -i inventory test_async.yml 2>&1 | tee async_test.out
152 grep "ASYNC OK .* jid=j[0-9]\{1,\}" async_test.out
153 grep "ASYNC FAILED .* jid=j[0-9]\{1,\}" async_test.out
154 rm -f async_test.out
155
156 # Hide skipped
157 export ANSIBLE_DISPLAY_SKIPPED_HOSTS=0
158
159 run_test hide_skipped test.yml
160
161 # Hide skipped/ok
162 export ANSIBLE_DISPLAY_SKIPPED_HOSTS=0
163 export ANSIBLE_DISPLAY_OK_HOSTS=0
164
165 run_test hide_skipped_ok test.yml
166
167 # Hide ok
168 export ANSIBLE_DISPLAY_SKIPPED_HOSTS=1
169 export ANSIBLE_DISPLAY_OK_HOSTS=0
170
171 run_test hide_ok test.yml
172
173 # Hide include
174 export ANSIBLE_DISPLAY_SKIPPED_HOSTS=1
175 export ANSIBLE_DISPLAY_OK_HOSTS=1
176 export ANSIBLE_DISPLAY_INCLUDED_HOSTS=0
177
178 run_test hide_included test.yml
179
180 # Hide skipped/ok/included
181 export ANSIBLE_DISPLAY_SKIPPED_HOSTS=0
182 export ANSIBLE_DISPLAY_OK_HOSTS=0
183 export ANSIBLE_DISPLAY_INCLUDED_HOSTS=0
184
185 run_test hide_skipped_ok_included test.yml
186
187 # Hide ok
188 export ANSIBLE_DISPLAY_SKIPPED_HOSTS=1
189 export ANSIBLE_DISPLAY_OK_HOSTS=0
190 export ANSIBLE_DISPLAY_INCLUDED_HOSTS=1
191
192 run_test hide_ok test.yml
193
194 # Failed to stderr
195 export ANSIBLE_DISPLAY_SKIPPED_HOSTS=1
196 export ANSIBLE_DISPLAY_OK_HOSTS=1
197 export ANSIBLE_DISPLAY_FAILED_STDERR=1
198
199 run_test failed_to_stderr test.yml
200 export ANSIBLE_DISPLAY_FAILED_STDERR=0
201
202
203 # Test displaying task path on failure
204 export ANSIBLE_SHOW_TASK_PATH_ON_FAILURE=1
205 run_test display_path_on_failure test.yml
206 export ANSIBLE_SHOW_TASK_PATH_ON_FAILURE=0
207
208
209 # Default settings with unreachable tasks
210 export ANSIBLE_DISPLAY_SKIPPED_HOSTS=1
211 export ANSIBLE_DISPLAY_OK_HOSTS=1
212 export ANSIBLE_DISPLAY_FAILED_STDERR=1
213 export ANSIBLE_TIMEOUT=1
214
215 # Check if UNREACHABLE is available in stderr
216 set +e
217 ansible-playbook -i inventory test_2.yml > >(set +x; tee "${BASEFILE}.unreachable.stdout";) 2> >(set +x; tee "${BASEFILE}.unreachable.stderr" >&2) || true
218 set -e
219 if test "$(grep -c 'UNREACHABLE' "${BASEFILE}.unreachable.stderr")" -ne 1; then
220 echo "Test failed"
221 exit 1
222 fi
223 export ANSIBLE_DISPLAY_FAILED_STDERR=0
224
225 export ANSIBLE_CALLBACK_RESULT_FORMAT=yaml
226 run_test result_format_yaml test.yml
227 export ANSIBLE_CALLBACK_RESULT_INDENTATION=2
228 run_test result_format_yaml_indent_2 test.yml
229 export ANSIBLE_CALLBACK_RESULT_FORMAT=json
230 unset ANSIBLE_CALLBACK_RESULT_INDENTATION
231
232 export ANSIBLE_CALLBACK_RESULT_FORMAT=yaml
233 export ANSIBLE_CALLBACK_YAML_LINE_WIDTH=default
234 run_test result_format_yaml_default_break test_long_line.yml
235 export ANSIBLE_CALLBACK_YAML_LINE_WIDTH=no-break
236 run_test result_format_yaml_no_break test_long_line.yml
237 export ANSIBLE_CALLBACK_RESULT_FORMAT=json
238 unset ANSIBLE_CALLBACK_YAML_LINE_WIDTH
239
240 export ANSIBLE_CALLBACK_RESULT_FORMAT=yaml
241 export ANSIBLE_CALLBACK_FORMAT_PRETTY=1
242 run_test result_format_yaml_lossy_verbose test.yml -v
243 run_test yaml_result_format_yaml_verbose test_yaml.yml -v
244 export ANSIBLE_CALLBACK_RESULT_FORMAT=json
245 unset ANSIBLE_CALLBACK_FORMAT_PRETTY
246
247 export ANSIBLE_CALLBACK_RESULT_FORMAT=yaml
248 export ANSIBLE_CALLBACK_FORMAT_PRETTY=0
249 run_test result_format_yaml_verbose test.yml -v
250 export ANSIBLE_CALLBACK_RESULT_FORMAT=json
251 unset ANSIBLE_CALLBACK_FORMAT_PRETTY
252
253
254 ## DRY RUN tests
255 #
256 # Default settings with dry run tasks
257 export ANSIBLE_DISPLAY_SKIPPED_HOSTS=1
258 export ANSIBLE_DISPLAY_OK_HOSTS=1
259 export ANSIBLE_DISPLAY_FAILED_STDERR=1
260 # Enable Check mode markers
261 export ANSIBLE_CHECK_MODE_MARKERS=1
262
263 # Test the wet run with check markers
264 run_test_dryrun check_markers_wet
265
266 # Test the dry run with check markers
267 run_test_dryrun check_markers_dry --check
268
269 # Disable Check mode markers
270 export ANSIBLE_CHECK_MODE_MARKERS=0
271
272 # Test the wet run without check markers
273 run_test_dryrun check_nomarkers_wet
274
275 # Test the dry run without check markers
276 run_test_dryrun check_nomarkers_dry --check
277
278 # Make sure implicit meta tasks are not printed
279 ansible-playbook -i host1,host2 no_implicit_meta_banners.yml > meta_test.out
280 cat meta_test.out
281 [ "$(grep -c 'TASK \[meta\]' meta_test.out)" -eq 0 ]
282 rm -f meta_test.out
283
284 # Ensure free/host_pinned non-lockstep strategies display correctly
285 for strategy in free ansible.builtin.free host_pinned; do
286 ANSIBLE_STRATEGY=$strategy ansible-playbook -i inventory test_non_lockstep.yml 2>/dev/null | tee non_lockstep_test.out
287 # Verify callback output structure by normalizing hostnames
288 diff -u callback_default.out.non_lockstep.stdout <(sed 's/testhost1[0-9]/testhostXX/g' non_lockstep_test.out)
289 # Verify each host completed the expected number of tasks
290 for host in testhost10 testhost11 testhost12; do
291 test "$(grep -c "changed: \[$host\]" non_lockstep_test.out)" -eq 3
292 done
293 done
294 rm -f non_lockstep_test.out