qemu: add linux boot tests

Add some basic boot tests for QEMU and supporting scripts to automate
the generation of dependencies. Currently, there are no tests in CI that
utilise the platform. This makes it easier to do testing in CI without
the need for a complex hardware setup.

Change-Id: I98e43ecc56dc44a767590c73963ded7b2cee1cff
Signed-off-by: Harrison Mutai <harrison.mutai@arm.com>
diff --git a/utils.sh b/utils.sh
index 4716c804..0f7d82a 100644
--- a/utils.sh
+++ b/utils.sh
@@ -145,6 +145,119 @@
 	fi
 }
 
+fetch_and_archive() {
+	url=${url:?}
+	filename=${filename:-basename $url}
+
+	url="$url" saveas="$filename" fetch_file
+	archive_file "$filename"
+}
+
+filter_artefacts(){
+	local model_param_file="${model_param_file-$archive/model_params}"
+
+	# Bash doesn't have array values, we have to create references to the
+	# array of artefacts and the artefact filters.
+	declare -ga "$1"
+	declare -n artefacts="$1"
+	declare -n filters="$2"
+
+	for artefact in "${!filters[@]}"; do
+		if grep -E -q "${filters[${artefact}]}" "$model_param_file"; then
+			artefacts+=("${artefact}")
+		fi
+	done
+}
+
+gen_lava_job_def() {
+	local yaml_template_file="${yaml_template_file:?}"
+	local yaml_file="${yaml_file:?}"
+	local yaml_job_file="${yaml_job_file}"
+
+	# Bash doesn't have array values, we have to create references to the
+	# array of artefacts and their urls.
+	declare -n artefacts="$1"
+	declare -n artefact_urls="$2"
+
+	readarray -t boot_arguments < "${lava_model_params}"
+
+	# Generate the LAVA job definition, minus the test expectations
+	expand_template "${yaml_template_file}" > "${yaml_file}"
+
+	if [[ ! $model =~ "qemu" ]]; then
+		# Append expect commands into the job definition through
+		# test-interactive commands
+		gen_fvp_yaml_expect >> "$yaml_file"
+	fi
+
+	# create job.yaml
+	cp "$yaml_file" "$yaml_job_file"
+
+	# archive both yamls
+	archive_file "$yaml_file"
+	archive_file "$yaml_job_file"
+}
+
+gen_lava_model_params() {
+	local lava_model_params="${lava_model_params:?}"
+	declare -n macros="$1"
+
+	# Derive LAVA model parameters from the non-LAVA ones
+	cp "${archive}/model_params" "${lava_model_params}"
+
+	if [[ $model =~ "qemu" ]]; then
+		# Strip the model parameters of parameters already specified in the deploy
+		# overlay and job context.
+		sed -i '/-M/d;/kernel/d;/initrd/d;/bios/d;/cpu/d;/^[[:space:]]*$/d' \
+				$lava_model_params
+	elif [[ ! $model =~ "qemu" ]]; then
+		# FIXME find a way to properly match FVP configurations.
+		# Ensure braces in the FVP model parameters are not accidentally
+		# interpreted as LAVA macros.
+		sed -i -e 's/{/{{/g' "${lava_model_params}"
+		sed -i -e 's/}/}}/g' "${lava_model_params}"
+	else
+		echo "Unsupported emulated platform $model."
+	fi
+
+	# LAVA expects binary paths as macros, i.e. `{X}` instead of `x.bin`, so
+	# replace the file paths in our pre-generated model parameters.
+	for regex in "${!macros[@]}"; do
+		sed -i -e "s!${regex}!${macros[${regex}]}!" "${lava_model_params}"
+	done
+}
+
+gen_yaml_template() {
+	local target="${target-fvp}"
+	local yaml_template_file="${yaml_template_file-$workspace/${target}_template.yaml}"
+
+	local payload_type="${payload_type:?}"
+
+	cp "${ci_root}/script/lava-templates/${target}-${payload_type:?}.yaml" \
+		"${yaml_template_file}"
+
+	archive_file "$yaml_template_file"
+}
+
+# Generate link to an archived binary.
+gen_bin_url() {
+	local bin_mode="${bin_mode:?}"
+	local bin="${1:?}"
+
+	if upon "$jenkins_run"; then
+		echo "$jenkins_url/job/$JOB_NAME/$BUILD_NUMBER/artifact/artefacts/$bin_mode/$bin"
+	else
+		echo "file://$workspace/artefacts/$bin_mode/$bin"
+	fi
+}
+
+get_kernel() {
+	local kernel_type="${kernel_type:?}"
+	local url="${plat_kernel_list[$kernel_type]}"
+
+	url="${url:?}" filename="kernel.bin" fetch_and_archive
+}
+
 # Make a temporary directory/file insdie workspace, so that it doesn't need to
 # be cleaned up. Jenkins is setup to clean up workspace before a job runs.
 mktempdir() {