blob: 7040454c18b8cf6ac8f20c6579e669095092e78f [file] [log] [blame]
Harrison Mutai9b099892023-01-13 11:34:36 +00001#!/usr/bin/env bash
2#
3# Copyright (c) 2019-2022, Arm Limited. All rights reserved.
4#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7
8gen_lava_job_def() {
9 local yaml_template_file="${yaml_template_file:?}"
10 local yaml_file="${yaml_file:?}"
11 local yaml_job_file="${yaml_job_file}"
12
13 # Bash doesn't have array values, we have to create references to the
14 # array of artefacts and their urls.
15 declare -n artefacts="$1"
16 declare -n artefact_urls="$2"
17
18 readarray -t boot_arguments < "${lava_model_params}"
19
20 # Source runtime environment variables now so that they are accessible from
21 # the LAVA job template.
22 local run_root="${archive}/run"
23 local run_env="${run_root}/env"
24
25 if [ -f "${run_env}" ]; then
26 source "${run_env}"
27 fi
28
29 # Generate the LAVA job definition, minus the test expectations
30 expand_template "${yaml_template_file}" > "${yaml_file}"
31
32 gen_yaml_expect >> "$yaml_file"
33
34 # create job.yaml
35 cp "$yaml_file" "$yaml_job_file"
36
37 # archive both yamls
38 archive_file "$yaml_file"
39 archive_file "$yaml_job_file"
40}
41
42gen_lava_model_params() {
43 local lava_model_params="${lava_model_params:?}"
44 declare -n macros="$1"
45
46 # Derive LAVA model parameters from the non-LAVA ones
47 cp "${archive}/model_params" "${lava_model_params}"
48
49 sed -i '/^[[:space:]]*$/d' $lava_model_params
50
51 if [[ $model =~ "qemu" ]]; then
52 # Strip the model parameters of parameters already specified in the deploy
53 # overlay and job context.
54 sed -i '/-M/d;/kernel/d;/initrd/d;/bios/d;/cpu/d;/^[[:space:]]*$/d' \
55 $lava_model_params
56 elif [[ ! $model =~ "qemu" ]]; then
57 # FIXME find a way to properly match FVP configurations.
58 # Ensure braces in the FVP model parameters are not accidentally
59 # interpreted as LAVA macros.
60 sed -i -e 's/{/{{/g' "${lava_model_params}"
61 sed -i -e 's/}/}}/g' "${lava_model_params}"
62 else
63 echo "Unsupported emulated platform $model."
64 fi
65
66 # LAVA expects binary paths as macros, i.e. `{X}` instead of `x.bin`, so
67 # replace the file paths in our pre-generated model parameters.
68 for regex in "${!macros[@]}"; do
69 sed -i -e "s!${regex}!${macros[${regex}]}!" "${lava_model_params}"
70 done
71}
72
73gen_yaml_template() {
74 local target="${target-fvp}"
75 local yaml_template_file="${yaml_template_file-$workspace/${target}_template.yaml}"
76
77 local payload_type="${payload_type:?}"
78
79 cp "${ci_root}/script/lava-templates/${target}-${payload_type:?}.yaml" \
80 "${yaml_template_file}"
81
82 archive_file "$yaml_template_file"
83}
84
85gen_yaml_expect() {
86 # Loop through all uarts expect files
87 for expect_file in $(find $run_root -name expect); do
88 local uart_number=$(basename "$(dirname ${expect_file})")
89
90 # Only handle the primary UART through LAVA. The remaining UARTs are
91 # validated after LAVA returns by the post-expect script.
92 if [ "${uart_number:?}" != "uart$(get_primary_uart "${archive}")" ]; then
93 continue
94 fi
95
96 # Array containing "interactive" or "monitor" expect strings and populated during run config execution.
97 # Interactive expect scripts are converted into LAVA Interactive Test Actions (see
98 # https://tf.validation.linaro.org/static/docs/v2/interactive.html#writing-tests-interactive) and
99 # monitor expect scripts are converted into LAVA Monitor Test Actions (see
100 # https://validation.linaro.org/static/docs/v2/actions-test.html#monitor)
101 #
102 # Interactive Expect strings have the format 'i;<prompt>;<succeses>;<failures>;<commands>'
103 # where multiple successes or failures or commands are separated by @
104 #
105 # Monitor Expect strings have the format 'm;<start>;<end>;<patterns>'
106 # where multiple patterns are separated by @
107 #
108 expect_string=()
109
110 # Get the real name of the expect file
111 expect_file=$(cat $expect_file)
112
113 # Source the run_config enviroment variables
114 env=$run_root/$uart_number/env
115 if [ -e $env ]; then
116 source $env
117 fi
118
119 # Get all expect strings
120 expect_dir="${ci_root}/expect-lava"
121 expect_file="${expect_dir}/${expect_file}"
122
123 # Allow the expectations to be provided directly in LAVA's job YAML
124 # format, rather than converting it from a pseudo-Expect Bash script in
125 # the block below.
126 if [ -f "${expect_file/.exp/.yaml}" ]; then
127 pushd "${expect_dir}"
128 expand_template "${expect_file/.exp/.yaml}"
129 popd
130
131 continue
132 else
133 source "${expect_file}"
134 fi
135
136 if [ ${#expect_string[@]} -gt 0 ]; then
137 # expect loop
138 for key in "${!expect_string[@]}"; do
139 # single raw expect string
140 es="${expect_string[${key}]}"
141
142 # action type: either m or i
143 action="$(echo "${es}" | awk -F ';' '{print $1}')"
144
145 if [ "${action}" = "m" ]; then
146 start="$(echo "${es}" | awk -F ';' '{print $2}')"
147 end="$(echo "${es}" | awk -F ';' '{print $3}')"
148 patterns="$(echo "${es}" | awk -F ';' '{print $4}')"
149
150 cat <<-EOF
151 - test:
152 monitors:
153 - name: tests
154 start: '${start}'
155 end: '${end}'
156 EOF
157
158 # Patterns are separated by '@'
159 OLD_IFS=$IFS; IFS=$'@'
160 for p in ${patterns}; do
161 cat <<-EOF
162 pattern: '$p'
163 EOF
164 done
165
166 IFS=$OLD_IFS
167 cat <<-EOF
168 fixupdict:
169 PASS: pass
170 FAIL: fail
171 EOF
172 fi # end of monitor action
173
174 if [ "${action}" = "i" ]; then
175 prompts="$(echo "${es}" | awk -F ';' '{print $2}')"
176 success="$(echo "${es}" | awk -F ';' '{print $3}')"
177 failure="$(echo "${es}" | awk -F ';' '{print $4}')"
178 commands="$(echo "${es}" | awk -F ';' '{print $5}')"
179
180 test_name="${uart_number}_${key}"
181
182 cat <<-EOF
183 - test:
184 interactive:
185 EOF
186 OLD_IFS=$IFS; IFS=$'@'
187
188 name="${test_name}" \
189 commands=$commands \
190 print_interactive_command
191
192 if [[ -n "${success}" ]]; then
193 message="$success" \
194 print_lava_result_msg
195 fi
196
197 if [[ -n "${failure}" ]]; then
198 res="failures" \
199 message="$failure" \
200 print_lava_result_msg
201 fi
202 IFS=$OLD_IFS
203 fi # end of interactive action
204
205 done # end of expect loop
206 fi
207 done # end of uart loop
208}
209
210
211print_interactive_command(){
212 local name="${name:?}"
213 local prompt=${prompts:-}
214 local commands=${commands:-}
215
216 cat <<-EOF
217 - name: interactive_${name}
218 prompts: ['${prompt}']
219 script:
220 - name: interactive_command_${name}
221 EOF
222
223 if [ -z "${commands}" ]; then
224 cat <<-EOF
225 command:
226 EOF
227 else
228 for c in ${commands}; do
229 cat <<-EOF
230 command: "$c"
231 EOF
232 done
233 fi
234}
235
236print_lava_result_msg() {
237 local res="${res:-successes}"
238 local message=${message:?}
239
240 cat <<-EOF
241 ${res}:
242 EOF
243
244 for m in ${message}; do
245 cat <<-EOF
246 - message: '$m'
247 EOF
248
249 if [ $res == "failures" ]; then
250 cat <<-EOF
251 exception: JobError
252 EOF
253 fi
254 done
255}