#!/bin/bash # # This script should be run on the hypervisor to manage the VMs needed # for a scenario. set -euo pipefail SCENARIO_MERGE_OUTPUT_STREAMS=${SCENARIO_MERGE_OUTPUT_STREAMS:-false} if "${SCENARIO_MERGE_OUTPUT_STREAMS}"; then exec 2>&1 fi SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" # shellcheck source=test/bin/common.sh source "${SCRIPTDIR}/common.sh" # shellcheck source=test/bin/common_versions.sh source "${SCRIPTDIR}/common_versions.sh" LVM_SYSROOT_SIZE="15360" PULL_SECRET="${PULL_SECRET:-${HOME}/.pull-secret.json}" XTRACE_ENABLED=false case "$-" in *x*) XTRACE_ENABLED=true ;; esac set +x PULL_SECRET_CONTENT="$(jq -c . "${PULL_SECRET}")" if "${XTRACE_ENABLED}"; then set -x fi VM_BOOT_TIMEOUT=1200 # Overall total boot times are around 15m VM_GREENBOOT_TIMEOUT=1800 # Greenboot readiness may take up to 15-30m depending on the load SKIP_SOS=${SKIP_SOS:-false} # may be overridden in global settings file SKIP_PCP=${SKIP_PCP:-false} # may be overridden in global settings file SKIP_GREENBOOT=${SKIP_GREENBOOT:-false} # may be overridden in scenario file GREENBOOT_TIMEOUT=${GREENBOOT_TIMEOUT:-600} # may be overridden in scenario file # Container image signature verification should be disabled by default in the # main branch because not all the images are signed IMAGE_SIGSTORE_ENABLED=false # may be overridden in scenario file VNC_CONSOLE=${VNC_CONSOLE:-false} # may be overridden in global settings file TEST_RANDOMIZATION="all" # may be overridden in scenario file TEST_EXCLUDES="none" # may be overridden in scenario file TEST_EXECUTION_TIMEOUT="${TEST_EXECUTION_TIMEOUT:-30m}" # may be overriden in scenario file or CI config SUBSCRIPTION_MANAGER_PLUGIN="${SUBSCRIPTION_MANAGER_PLUGIN:-${SCRIPTDIR}/subscription_manager_register.sh}" # may be overridden in global settings file RUN_HOST_OVERRIDE="" # target any given VM for running scenarios declare -i TESTCASES=0 declare -i FAILURES=0 declare -i SKIPPED=0 TIMESTAMP="$(date --iso-8601=ns)" full_vm_name() { local -r base="${1}" local -r type="$(get_scenario_type_from_path "${SCENARIO_SCRIPT}")" # Add a type suffix to the name to allow running scenarios from different # build types on the same hypervisor echo "${SCENARIO//@/-}-${type}-${base}" } # hostname validation # based on https://github.com/rhinstaller/anaconda/blob/c95142f76735a2e9ae6d845f8569d46632ddd619/pyanaconda/network.py#L96-L120 validate_vm_hostname() { local vm_name="$1" if [ -z "${vm_name}" ]; then error "VM hostname cannot be empty string" record_junit "${vm_name}" "vm_hostname_validation" "FAILED" exit 1 fi if [ ${#vm_name} -gt 64 ]; then error "VM hostname is too long" record_junit "${vm_name}" "vm_hostname_validation" "FAILED" exit 1 fi if ! echo "${vm_name}" | grep -E '^([a-zA-Z0-9]+-*[a-zA-Z0-9]+)+$|^[a-zA-Z0-9]+$' > /dev/null; then error "VM hostname is invalid" record_junit "${vm_name}" "vm_hostname_validation" "FAILED" exit 1 fi record_junit "${vm_name}" "vm_hostname_validation" "OK" } vm_property_filename() { local -r vmname="$1" local -r property="$2" echo "${SCENARIO_INFO_DIR}/${SCENARIO}/vms/${vmname}/${property}" } get_vm_property() { local -r vmname="$1" local -r property="$2" local -r property_file="$(vm_property_filename "${vmname}" "${property}")" cat "${property_file}" } set_vm_property() { local -r vmname="$1" local -r property="$2" local -r value="$3" local -r property_file="$(vm_property_filename "${vmname}" "${property}")" mkdir -p "$(dirname "${property_file}")" echo "${value}" > "${property_file}" } run_command_on_vm() { local -r vmname="$1" shift local -r command="$*" local -r ip=$(get_vm_property "${vmname}" ip) local -r ssh_port=$(get_vm_property "${vmname}" ssh_port) local term_opt="" if [ -t 0 ] ; then # Allocate pseudo-terminal for SSH commands when stdin is a terminal # Necessary in devenv for entering input i.e. system registration, etc. term_opt="-t" fi # Must return normally so local variables are cleaned up on command failure, # otherwise stale values and readonly attributes break SOS collection local rc=0 ssh "redhat@${ip}" -p "${ssh_port}" ${term_opt} "${command}" || rc=$? return "${rc}" } copy_file_to_vm() { local -r vmname="$1" local -r local_filename="$2" local -r remote_filename="$3" local ip ip=$(get_vm_property "${vmname}" ip) if [ "${ip}" != "${ip#*:[0-9a-fA-F]}" ]; then ip="[${ip}]" fi local -r ssh_port=$(get_vm_property "${vmname}" ssh_port) # Must return normally so local variables are cleaned up on command failure, # otherwise stale values and readonly attributes break SOS collection local rc=0 scp -P "${ssh_port}" "${local_filename}" "redhat@${ip}:${remote_filename}" || rc=$? return "${rc}" } copy_file_from_vm() { local -r vmname="$1" local -r remote_filename="$2" local -r local_filename="$3" local ip ip=$(get_vm_property "${vmname}" ip) if [ "${ip}" != "${ip#*:[0-9a-fA-F]}" ]; then ip="[${ip}]" fi local -r ssh_port=$(get_vm_property "${vmname}" ssh_port) # Must return normally so local variables are cleaned up on command failure, # otherwise stale values and readonly attributes break SOS collection local rc=0 scp -P "${ssh_port}" "redhat@${ip}:${remote_filename}" "${local_filename}" || rc=$? return "${rc}" } sos_report() { local -r junit="${1:-false}" if "${SKIP_SOS}"; then echo "Skipping sos reports" if "${junit}"; then record_junit "post_setup" "sos-report" "SKIP" fi return fi echo "Creating sos reports" local vmname local ip local scenario_result=0 for vmdir in "${SCENARIO_INFO_DIR}"/"${SCENARIO}"/vms/*; do if [ ! -d "${vmdir}" ]; then # skip log files, etc. continue fi vmname=$(basename "${vmdir}") ip=$(get_vm_property "${vmname}" ip) sos_func="sos_report_for_vm" if [ -z "${ip}" ]; then echo "Creating sos reports offline" sos_func="sos_report_for_vm_offline" fi if ! "${sos_func}" "${vmdir}" "${vmname}"; then scenario_result=1 if "${junit}"; then record_junit "${vmname}" "sos-report" "FAILED" fi else if "${junit}"; then record_junit "${vmname}" "sos-report" "OK" fi fi done return "${scenario_result}" } sos_report_for_vm() { local -r vmdir="${1}" local -r vmname="${2}" local -r vm_ip=$(get_vm_property "${vmname}" ip) wait_for_ssh "${vm_ip}" 120 || { echo "WARNING: SSH not available, skipping SOS collection for ${vmname}" return } # Some scenarios do not start with MicroShift installed, so we # can't rely on the wrapper being there or working if it # is. Copy the script to the host, just in case, along with a # wrapper that knows how to execute it or the installed version. copy_file_to_vm "${vmname}" "${ROOTDIR}/test/assets/sos-wrapper.sh" "/tmp/sos-wrapper.sh" copy_file_to_vm "${vmname}" "${ROOTDIR}/scripts/microshift-sos-report.sh" "/tmp/microshift-sos-report.sh" run_command_on_vm "${vmname}" "sudo bash -x /tmp/sos-wrapper.sh" mkdir -p "${vmdir}/sos" copy_file_from_vm "${vmname}" "/tmp/sosreport-*" "${vmdir}/sos/" || { echo "WARNING: Ignoring an error when copying sos report files" } run_command_on_vm "${vmname}" "sudo journalctl > /tmp/journal_$(date +'%Y-%m-%d_%H:%M:%S').log" copy_file_from_vm "${vmname}" "/tmp/journal*.log" "${vmdir}/sos" || { echo "WARNING: Ignoring an error when copying journal" } # Also copy the logs from the /var/log/anaconda directory to # collect information about potentially failed installations. # Note: we cannot use `anaconda` sos report plugin because # it also includes the kickstart files that may expose the # OpenShift Pull Secret and SSH keys. run_command_on_vm "${vmname}" \ "sudo mkdir -p /tmp/var-log-anaconda && \ sudo cp /var/log/anaconda/*.log /tmp/var-log-anaconda/ && \ sudo chmod +r /tmp/var-log-anaconda/*.log" mkdir -p "${vmdir}/anaconda" copy_file_from_vm "${vmname}" "/tmp/var-log-anaconda/*.log" "${vmdir}/anaconda" || { echo "WARNING: Ignoring an error when copying anaconda logs" } } invoke_qemu_script() { timeout --verbose --foreground 2m \ "${ROOTDIR}/_output/robotenv/bin/python" "${ROOTDIR}/test/resources/qemu-guest-agent.py" "$@" } sos_report_for_vm_offline() { local -r vmdir="${1}" local -r vmname="${2}" local -r full_vmname="$(full_vm_name "${vmname}")" "${ROOTDIR}/scripts/fetch_tools.sh" "robotframework" invoke_qemu_script "wait" \ "--vm" "${full_vmname}" invoke_qemu_script "upload" \ "--vm" "${full_vmname}" \ "--src" "${ROOTDIR}/test/assets/sos-wrapper.sh" \ "--dst" "/tmp/sos-wrapper.sh" invoke_qemu_script "upload" \ "--vm" "${full_vmname}" \ "--src" "${ROOTDIR}/scripts/microshift-sos-report.sh" \ "--dst" "/tmp/microshift-sos-report.sh" invoke_qemu_script "bash" \ "--vm" "${full_vmname}" \ "--args" "sudo bash -x /tmp/sos-wrapper.sh" mkdir -p "${vmdir}/sos" invoke_qemu_script "download" \ "--vm" "${full_vmname}" \ "--src_dir" "/tmp/" \ "--dst_dir" "${vmdir}/sos/" \ "--filename" "sosreport-*" invoke_qemu_script "bash" \ "--vm" "${full_vmname}" \ "--args" "sudo journalctl > /tmp/journal_$(date +'%Y-%m-%d_%H:%M:%S').log" invoke_qemu_script "download" \ "--vm" "${full_vmname}" \ "--src_dir" "/tmp/" \ "--dst_dir" "${vmdir}/sos/" \ "--filename" "journal*.log" # Also copy the logs from the /var/log/anaconda directory invoke_qemu_script "bash" \ "--vm" "${full_vmname}" \ "--args" "sudo mkdir -p /tmp/var-log-anaconda" invoke_qemu_script "bash" \ "--vm" "${full_vmname}" \ "--args" 'sudo cp /var/log/anaconda/*.log /tmp/var-log-anaconda/' invoke_qemu_script "bash" \ "--vm" "${full_vmname}" \ "--args" "sudo chmod +r /tmp/var-log-anaconda/*.log" mkdir -p "${vmdir}/anaconda" invoke_qemu_script "download" \ "--vm" "${full_vmname}" \ "--src_dir" "/tmp/var-log-anaconda/" \ "--dst_dir" "${vmdir}/anaconda/" \ "--filename" "*.log" } collect_pcp_reports_for_vm() { local -r vmdir="${1}" local -r vmname="${2}" echo "Collecting PCP data from ${vmname}" run_command_on_vm "${vmname}" "sudo systemctl stop --now pmlogger" || true if ! run_command_on_vm "${vmname}" "test -d /var/log/pcp/pmlogger" ; then echo "WARNING: No PCP data directory on ${vmname}, skipping collection" return 0 fi run_command_on_vm "${vmname}" \ "sudo tar czf /tmp/pcp-archives.tar.gz -C /var/log/pcp/pmlogger ." || true mkdir -p "${vmdir}/pcp" copy_file_from_vm "${vmname}" "/tmp/pcp-archives.tar.gz" "${vmdir}/pcp/" || { echo "WARNING: Failed to collect PCP data from ${vmname}" } } collect_pcp_reports_for_vm_offline() { local -r vmdir="${1}" local -r vmname="${2}" local -r full_vmname="$(full_vm_name "${vmname}")" echo "Collecting PCP data offline from ${vmname}" "${ROOTDIR}/scripts/fetch_tools.sh" "robotframework" invoke_qemu_script "wait" \ "--vm" "${full_vmname}" invoke_qemu_script "bash" \ "--vm" "${full_vmname}" \ "--args" "sudo systemctl stop --now pmlogger" invoke_qemu_script "bash" \ "--vm" "${full_vmname}" \ "--args" "sudo tar czf /tmp/pcp-archives.tar.gz -C /var/log/pcp/pmlogger ." mkdir -p "${vmdir}/pcp" invoke_qemu_script "download" \ "--vm" "${full_vmname}" \ "--src_dir" "/tmp/" \ "--dst_dir" "${vmdir}/pcp/" \ "--filename" "pcp-archives.tar.gz" } collect_pcp_reports() { if "${SKIP_PCP}"; then echo "Skipping PCP collection" return 0 fi echo "Collecting PCP reports" for vmdir in "${SCENARIO_INFO_DIR}"/"${SCENARIO}"/vms/*; do if [ ! -d "${vmdir}" ]; then continue fi local vmname vmname=$(basename "${vmdir}") local ip ip=$(cat "$(vm_property_filename "${vmname}" "ip")" 2>/dev/null) || true local pcp_func="collect_pcp_reports_for_vm" if [ -z "${ip}" ]; then echo "Collecting PCP reports offline" pcp_func="collect_pcp_reports_for_vm_offline" fi "${pcp_func}" "${vmdir}" "${vmname}" || { echo "WARNING: Failed to collect PCP data from ${vmname}" } done } get_lrel_release_image_url() { local -r brew_lrel_release_version="$1" local -r rhel_version="${2:-9}" local image_url="" # Strip the rpm release suffix and convert tilde to dash. # "4.19.7-202501010000.p0.gc62e92f.assembly.4.19.7.el9" -> "4.19.7" # "4.20.0~rc.3-..." -> "4.20.0-rc.3" local release_version="" release_version="$(echo "${brew_lrel_release_version}" \ | sed -E 's/(.*)-.*/\1/' \ | sed -E 's/(.*)~(.*)/\1-\2/')" # EC and RC releases have their bootc pullspec published on the mirror. local mirror_path="" if [[ "${release_version}" == *"ec"* ]]; then mirror_path="ocp-dev-preview" elif [[ "${release_version}" == *"rc"* ]]; then mirror_path="ocp" fi if [ -n "${mirror_path}" ]; then if ! image_url="$(curl -fsS --retry 3 \ "https://mirror.openshift.com/pub/openshift-v4/${UNAME_M}/microshift/${mirror_path}/${release_version}/el${rhel_version}/bootc-pullspec.txt")"; then image_url="" fi echo "${image_url}" return fi # GA releases: resolve the arch-specific image digest from the registry. local arch="" if [[ "${UNAME_M}" =~ x86 ]]; then arch="amd64" elif [[ "${UNAME_M}" =~ aarch ]]; then arch="arm64" fi # Resolve the arch-specific digest from both registries local -r image_path="openshift${release_version%%.*}/microshift-bootc-rhel${rhel_version}" local -r image_tag="v${release_version}" local -r prod_registry="registry.redhat.io" local -r stage_registry="registry.stage.redhat.io" local prod_sha="" local stage_sha="" for registry in "${prod_registry}" "${stage_registry}"; do local sha_id="" if sha_id=$(skopeo inspect --raw --authfile "${PULL_SECRET}" \ "docker://${registry}/${image_path}:${image_tag}" 2>/dev/null | \ jq -r ".manifests[] | select(.platform.architecture==\"${arch}\") | .digest" 2>/dev/null); then if [[ "${sha_id}" =~ ^sha256:[0-9a-f]{64}$ ]]; then case "${registry}" in "${prod_registry}") prod_sha="${sha_id}" ;; "${stage_registry}") stage_sha="${sha_id}" ;; esac fi fi done # Select registry with the following priority: # 1. use stage if it is available and prod is unavailable (e.g., pre-GA release) # 2. use stage if it is available and has a newer digest than prod (e.g., new z-stream not yet in prod) # 3. use prod if it is available and stage is available and has same digest as prod (e.g., post GA) # 4. use prod if it is available and stage is unavailable local selected_registry="" if [[ -n "${stage_sha}" && -z "${prod_sha}" ]]; then selected_registry="${stage_registry}" elif [[ -n "${stage_sha}" && -n "${prod_sha}" && "${stage_sha}" != "${prod_sha}" ]]; then selected_registry="${stage_registry}" elif [[ -n "${stage_sha}" && -n "${prod_sha}" && "${stage_sha}" == "${prod_sha}" ]]; then selected_registry="${prod_registry}" elif [[ -z "${stage_sha}" && -n "${prod_sha}" ]]; then selected_registry="${prod_registry}" fi if [[ -n "${selected_registry}" ]]; then local selected_sha case "${selected_registry}" in "${prod_registry}") selected_sha="${prod_sha}" ;; "${stage_registry}") selected_sha="${stage_sha}" ;; esac image_url="${selected_registry}/${image_path}@${selected_sha}" fi echo "${image_url}" } # Public function to render a unique kickstart from a template for a # VM in a scenario. # # Arguments: # vmname -- The short name of the VM (e.g., "host1") # template -- The path to the kickstart template file, relative to # the scenario directory. # boot_commit_ref -- The reference to the image that should be booted # first on the host. This usually matches an image # blueprint name. # fips_enabled -- Enable FIPS mode (true or false). # ipv6_only -- Only use IPv6 single stack configuration by explicitly # disabling IPv4 (true or false) prepare_kickstart() { local vmname="$1" local template="$2" local boot_commit_ref="$3" local fips_enabled=${4:-false} local ipv6_only=${5:-false} local -r full_vmname="$(full_vm_name "${vmname}")" local -r output_dir="${SCENARIO_INFO_DIR}/${SCENARIO}/vms/${vmname}" local -r vm_hostname="${full_vmname/./-}" local -r hostname=$(hostname) local ipv6_opt="" if ${ipv6_only} ; then ipv6_opt="--noipv4 --ipv6 auto" fi validate_vm_hostname "${vm_hostname}" echo "Preparing kickstart file ${template} at ${output_dir}" if [ ! -f "${KICKSTART_TEMPLATE_DIR}/${template}" ]; then error "No ${template} in ${KICKSTART_TEMPLATE_DIR}" record_junit "${vmname}" "prepare_kickstart" "no-template" exit 1 fi # For bootc kickstart templates, make sure that commit references are # fully qualified. Unqualified references are assumed to be served from # the local mirror registry. if [[ "${template}" == *bootc* ]] ; then if [ "$(dirname "${boot_commit_ref}")" == "." ] ; then boot_commit_ref="${MIRROR_REGISTRY_URL}/${boot_commit_ref}" fi fi mkdir -p "${output_dir}" for ifile in "${KICKSTART_TEMPLATE_DIR}/${template}" "${KICKSTART_TEMPLATE_DIR}"/includes/*.cfg ; do local output_file if [[ ${ifile} == *.cfg ]] ; then output_file="${output_dir}/$(basename "${ifile}")" else # The main kickstart file name is hardcoded to kickstart.ks output_file="${output_dir}/kickstart.ks" fi sed -e "s|REPLACE_LVM_SYSROOT_SIZE|${LVM_SYSROOT_SIZE}|g" \ -e "s|REPLACE_OSTREE_SERVER_URL|${WEB_SERVER_URL}/repo|g" \ -e "s|REPLACE_BOOTC_REGISTRY_URL|${MIRROR_REGISTRY_URL}|g" \ -e "s|REPLACE_RPM_SERVER_URL|${WEB_SERVER_URL}/rpm-repos|g" \ -e "s|REPLACE_MAJOR_VERSION|${MAJOR_VERSION}|g" \ -e "s|REPLACE_MINOR_VERSION|${MINOR_VERSION}|g" \ -e "s|REPLACE_BOOT_COMMIT_REF|${boot_commit_ref}|g" \ -e "s|REPLACE_HOST_NAME|${vm_hostname}|g" \ -e "s|REPLACE_IPV6_ONLY|${ipv6_opt}|g" \ -e "s|REPLACE_REDHAT_AUTHORIZED_KEYS|${REDHAT_AUTHORIZED_KEYS}|g" \ -e "s|REPLACE_FIPS_ENABLED|${fips_enabled}|g" \ -e "s|REPLACE_MIRROR_HOSTNAME|${hostname}|g" \ -e "s|REPLACE_MIRROR_PORT|${MIRROR_REGISTRY_PORT}|g" \ -e "s|REPLACE_VM_BRIDGE_IP|${VM_BRIDGE_IP}|g" \ -e "s|REPLACE_IMAGE_SIGSTORE_ENABLED|${IMAGE_SIGSTORE_ENABLED}|g" \ -e "s|REPLACE_GREENBOOT_TIMEOUT|${GREENBOOT_TIMEOUT}|g" \ "${ifile}" > "${output_file}" set +x sed -i \ -e "s|REPLACE_PULL_SECRET|${PULL_SECRET_CONTENT}|g" \ "${output_file}" if "${XTRACE_ENABLED}"; then set -x fi done record_junit "${vmname}" "prepare_kickstart" "OK" } # Checks if provided commit exists in local ostree repository. # Returns 0 when the ref exists or 1 otherwise. does_commit_exist() { local -r commit="${1}" if ostree refs --repo "${IMAGEDIR}/repo" | grep -q "${commit}"; then return 0 else return 1 fi } # Checks if provided image ref exists in the mirror registry. # Returns 0 when the ref exists or 1 otherwise. does_image_exist() { local -r image="${1}" if skopeo inspect "docker://${MIRROR_REGISTRY_URL}/${image}" &>/dev/null ; then return 0 else return 1 fi } # Exit the script if the commit is not found in the ostree repository. exit_if_commit_not_found() { local -r commit="${1}" if ! does_commit_exist "${commit}"; then echo "Commit '${commit}' not found in ostree repo - VM can't be created" record_junit "${commit}" "build_vm_commit_not_found" "SKIPPED" exit 0 fi } # Exit the script if the image is not found in the mirror registry. exit_if_image_not_found() { local -r image="${1}" if ! does_image_exist "${image}"; then echo "Image '${image}' not found in mirror registry - VM can't be created" record_junit "${image}" "build_vm_image_not_found" "SKIPPED" exit 0 fi } # Exit the script if Brew RPMs are not found. exit_if_brew_rpms_not_found() { if [[ -z "${BREW_LREL_RELEASE_VERSION:-}" ]]; then echo "Brew RPM release version is not set - VM can't be created" record_junit "BREW_LREL_RELEASE_VERSION is not set" "brew_rpms_not_found" "SKIPPED" exit 0 fi } # Exit the script if the latest release image is not set. exit_if_image_not_set() { local -r release_image_url="${1}" if [[ "${release_image_url}" == "" ]] ; then echo "Release image URL is not defined - VM can't be created" record_junit "release image URL is not defined" "exit_if_image_not_set" "SKIPPED" exit 0 fi } # Exit the script if microshift previous Z-stream version does not exist in rhocp repository exit_if_zprel_not_exist() { local -r rhocp_repo="rhocp-${MAJOR_VERSION}.${MINOR_VERSION}-for-rhel-9-${UNAME_M}-rpms" local microshift_zprel if ! microshift_zprel="$(dnf repoquery -q --repo "${rhocp_repo}" --latest-limit 1 --nvr microshift 2>&1)"; then record_junit "repo ${rhocp_repo} does not exist or is not available: ${microshift_zprel}" "exit_if_zprel_not_exist" "SKIPPED" exit 0 fi # check if there are any microshift packages found if [[ -z ${microshift_zprel} ]]; then record_junit "no microshift packages found in ${rhocp_repo} repo" "exit_if_zprel_not_exist" "SKIPPED" exit 0 fi # check if the microshift package is the same as the target version if [[ "${microshift_zprel}" == "microshift-${BREW_LREL_RELEASE_VERSION}" ]]; then record_junit "microshift packages found in ${rhocp_repo} repo ${microshift_zprel} is the same as the target version: ${BREW_LREL_RELEASE_VERSION}" "exit_if_zprel_not_exist" "SKIPPED" exit 0 fi } # Show the IP address of the VM function get_vm_ip { local -r vmname="${1}" local -r start=$(date +%s) local ip if [[ "${vmname}" =~ ([0-9]{1,3}\.){3}[0-9]{1,3} ]]; then ip="${BASH_REMATCH[0]}" else ip=$("${ROOTDIR}/scripts/devenv-builder/manage-vm.sh" ip -n "${vmname}" | head -1) while true; do now=$(date +%s) if [ $(( now - start )) -ge ${VM_BOOT_TIMEOUT} ]; then echo "Timed out while waiting for IP retrieval" >&2 return 1 fi sleep 1 # Try pinging the IP address to avoid stale DHCP leases that would falsely # return as the current IP for the VM. ip=$("${ROOTDIR}/scripts/devenv-builder/manage-vm.sh" ip -n "${vmname}" | head -1) if ping -c 1 -W 1 "${ip}" &> /dev/null; then break fi done fi echo "${ip}" } # Try to login to the host via ssh until the connection is accepted wait_for_ssh() { local -r ip="${1}" local -r timeout="${2:-${VM_BOOT_TIMEOUT}}" echo "Waiting ${timeout} for ssh access to ${ip}" local -r start_time=$(date +%s) while [ $(( $(date +%s) - start_time )) -lt "${timeout}" ] ; do if ssh -oConnectTimeout=10 -oBatchMode=yes -oStrictHostKeyChecking=accept-new "redhat@${ip}" "echo host is up" ; then return 0 fi date sleep 5 done # Return an error if non of the ssh attempts succeeded return 1 } wait_for_microshift_to_be_ready() { local vmname="${1}" shift # Handle RUN_HOST_OVERRIDE vmname=$(apply_host_override "${vmname}") if [ -z "${vmname}" ]; then record_junit "${vmname}" "apply_host_override" "FAILED" exit 1 fi # Set up kubeconfig for tests local -r vm_ip=$(get_vm_property "${vmname}" "ip") local -r full_vmname="$(full_vm_name "${vmname}")" # Wait for MicroShift to be ready if ! wait_for_greenboot "${full_vmname}" "${vm_ip}"; then record_junit "${vmname}" "pre_test_greenboot_check" "FAILED" exit 1 fi record_junit "${vmname}" "pre_test_greenboot_check" "OK" } # Wait for greenboot health check to complete, without checking the results wait_for_greenboot() { local -r vmname="${1}" local -r ip="${2}" if "${SKIP_GREENBOOT}"; then echo "Skipping greenboot check" record_junit "${vmname}" "greenboot-check" "SKIPPED" return 0 fi echo "Waiting ${VM_GREENBOOT_TIMEOUT} for greenboot on ${vmname} to complete" local -r start_time=$(date +%s) local -r ssh_cmd="ssh -oConnectTimeout=10 -oBatchMode=yes -oStrictHostKeyChecking=accept-new redhat@${ip}" while [ $(( $(date +%s) - start_time )) -lt "${VM_GREENBOOT_TIMEOUT}" ] ; do local svc_state svc_state="$(${ssh_cmd} systemctl show --property=SubState --value greenboot-healthcheck || true)" if [ "${svc_state}" = "exited" ] ; then record_junit "${vmname}" "greenboot-check" "OK" return 0 fi # Print the last log and check for terminal failure ${ssh_cmd} "sudo journalctl -n 10 -u greenboot-healthcheck" || true if [ "${svc_state}" = "failed" ] ; then echo "The greenboot service reported a failed state, no need to wait any longer" break fi date sleep 10 done # Return an error if none of the ssh attempts succeeded record_junit "${vmname}" "greenboot-check" "FAILED" return 1 } start_junit() { mkdir -p "$(dirname "${JUNIT_OUTPUT_FILE}")" echo "Creating ${JUNIT_OUTPUT_FILE}" cat - >"${JUNIT_OUTPUT_FILE}" < EOF } close_junit() { echo '' >> "${JUNIT_OUTPUT_FILE}" local line="" sed -i "2c${line}" "${JUNIT_OUTPUT_FILE}" } record_junit() { local vmname="$1" local step="$2" local results="$3" TESTCASES=$((TESTCASES+1)) cat - >>"${JUNIT_OUTPUT_FILE}" < EOF case "${results}" in OK) ;; SKIP*) SKIPPED=$((SKIPPED+1)); cat - >>"${JUNIT_OUTPUT_FILE}" < EOF ;; *) FAILURES=$((FAILURES+1)); cat - >>"${JUNIT_OUTPUT_FILE}" < EOF esac cat - >>"${JUNIT_OUTPUT_FILE}" < EOF } # Public function to start a VM. # # Creates a new VM using the scenario name and the vmname given to # create a unique name. Uses the boot_blueprint and network # arguments to select the ISO and networks from which to boot. # If no network is specified, uses the "default" network. # # Usage: launch_vm \ # \ # [--vmname ] \ # [--network [,...]] \ # [--network_mtu ] \ # [--vm_vcpus ] \ # [--vm_memory ] \ # [--vm_disksize ] \ # [--fips] \ # [--no_network] # # Arguments: # : Mandatory. The name of the image blueprint # used to create the ISO that should be used # to boot the VM. This is _not_ necessarily the # image to be installed (see prepare_kickstart). # [--vmname ]: The short name of the VM in the scenario # (e.g., "host1"). # [--network [,...]]: A comma-separated list for the networks used # when creating the VM. Each network entry will # create a NIC and they are repeatable. # [--network_mtu ]: Sets the guest-visible MTU on every NIC via # virt-install's mtu.size sub-option. Required # in addition to a libvirt network's own # element — QEMU only negotiates a larger MTU # with the guest's virtio-net driver when the # domain's own XML requests it; # the network-level MTU alone only affects the # host-side bridge and tap devices. # [--no_network]: Do not configure any network attachments (and # therefore no NICs) for the VM. # [--vm_vcpus ]: Number of vCPUs for the VM. # [--vm_memory ]: Size of RAM in MB for the VM. # [--vm_disksize ]: Size of disk in GB for the VM. # [--fips]: Enable FIPS mode for the VM. launch_vm() { # Set default values for the optional arguments local vmname="host1" local network="default" local network_mtu="" local vm_memory=4096 local vm_vcpus=2 local vm_disksize=20 local fips_mode=0 local kernel_location="images/pxeboot" # Parse the mandatory arguments local -r boot_blueprint="${1:-}" if [ -z "${boot_blueprint}" ]; then error "Boot blueprint is not set" record_junit "${vmname}" "vm-launch-args" "FAILED" exit 1 fi shift # Parse the optional arguments while [ $# -gt 0 ]; do case "$1" in --vmname|--vm_vcpus|--vm_memory|--vm_disksize|--network_mtu) var="${1/--/}" if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then declare "${var}=$2" shift 2 else error "Failed parsing arguments: ${var} value not set" record_junit "${vmname}" "vm-launch-args" "FAILED" exit 1 fi ;; --network) if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then network="${2//,/ }" shift 2 else error "Failed parsing network argument: value not set" record_junit "${vmname}" "vm-launch-args" "FAILED" exit 1 fi ;; --no_network) network="" shift ;; --fips) fips_mode=1 shift ;; *) error "Invalid argument: ${1}" record_junit "${vmname}" "vm-launch-args" "FAILED" exit 1 ;; esac done record_junit "${vmname}" "vm-launch-args" "OK" local -r full_vmname="$(full_vm_name "${vmname}")" local -r kickstart_url="${WEB_SERVER_URL}/scenario-info/${SCENARIO}/vms/${vmname}/kickstart.ks" local -r vm_pool_name="${VM_POOL_BASENAME}-${full_vmname}" local -r vm_pool_dir="${VM_DISK_BASEDIR}/${vm_pool_name}" # See if the VM already exists if sudo virsh dominfo "${full_vmname}" 2>/dev/null; then echo "${full_vmname} already exists" record_junit "${vmname}" "install_vm" "SKIP" return 0 fi echo "Creating ${full_vmname}" # Create the pool if it does not exist if [ ! -d "${vm_pool_dir}" ] ; then mkdir -p "${vm_pool_dir}" fi if ! sudo virsh pool-info "${vm_pool_name}" &>/dev/null; then sudo virsh pool-define-as "${vm_pool_name}" dir --target "${vm_pool_dir}" sudo virsh pool-build "${vm_pool_name}" sudo virsh pool-start "${vm_pool_name}" sudo virsh pool-autostart "${vm_pool_name}" fi # Prepare network and extra arguments for the VM creation # depending on the number of requested NICs and other parameters local vm_loc_args local vm_network_args local vm_extra_args local vm_initrd_inject vm_loc_args="--location ${VM_DISK_BASEDIR}/${boot_blueprint}.iso,initrd=${kernel_location}/initrd.img,kernel=${kernel_location}/vmlinuz" vm_network_args="" vm_extra_args="fips=${fips_mode}" vm_initrd_inject="" # Specify the right console device per each platform. The baud rate # setting boost may result in slightly improved speed. # The device name is a mandatory option on x86_64 to get the console # output from virt-install, but optional on aarch64 platform. case "${UNAME_M}" in x86_64) vm_extra_args+=" console=ttyS0,115200n8 inst.notmux" ;; aarch64) vm_extra_args+=" console=ttyAMA0,115200n8 inst.notmux" ;; esac # Attach the graphical console if specified in the scenario settings local graphics_args graphics_args="none" if "${VNC_CONSOLE}"; then graphics_args="vnc,listen=0.0.0.0" else # The inst.cmdline mode does not allow any interaction and it ensures # that %onerror kickstart handlers are executed on failure vm_extra_args+=" inst.cmdline" fi for n in ${network}; do # For simplicity we assume that network filters are named the same as the networks # If there is a filter with the same name as the network, attach it to the NIC if [ "${n}" = "sriov" ] ; then vm_network_args+="--network network=default,model=igb" else vm_network_args+="--network network=${n},model=virtio" fi if sudo virsh nwfilter-list | awk '{print $2}' | grep -qx "${n}"; then vm_network_args+=",filterref=${n}" fi # Propagate MTU to the domain interface (see --network_mtu doc above). if [ -n "${network_mtu}" ]; then vm_network_args+=",mtu.size=${network_mtu}" fi vm_network_args+=" " done if [ -z "${vm_network_args}" ] ; then vm_network_args="--network none" fi # Inject the kickstart file and all its includes into the image local -r kickstart_file=$(mktemp /tmp/kickstart.XXXXXXXX.ks) local -r kickstart_idir=$(mktemp -d /tmp/kickstart-includes.XXXXXXXX) # Download and inject the kickstart main file local -r http_code=$(curl -o "${kickstart_file}" -s -w "%{http_code}" "${kickstart_url}") if [ "${http_code}" -ne 200 ] ; then error "Failed to load kickstart file from ${kickstart_url}" exit 1 fi vm_extra_args+=" inst.ks=file:/$(basename "${kickstart_file}")" vm_initrd_inject+=" --initrd-inject ${kickstart_file}" # Download and inject all the kickstart include files wget -r -q -nd -A "*.cfg" -P "${kickstart_idir}" "$(dirname "${kickstart_url}")/" for cfg_file in "${kickstart_idir}"/*.cfg ; do vm_initrd_inject+=" --initrd-inject ${cfg_file}" done # Implement retries on VM creation that can time out when pulling # ostree commits or any other installation error local vm_created=false local attempt=1 local max_attempts=2 while true ; do # Make sure the virt-install command times out after a predefined period. # The 'timeout' command sends the HUP signal and, if the process does not # exit after 1m, it sends the KILL signal to terminate the process. # Note: Using the '--wait