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