#!/usr/bin/env bash set -Eeuo pipefail [[ $(type -P yq) ]] || { >&2 echo "Required tool 'yq' not found in PATH" ; exit 1; } declare \ tests_dir='./scripts/openstack/manifest-tests' \ openshift_install='./bin/openshift-install' \ api_fip='' \ os_cloud='' \ external_network='' \ compute_flavor='' \ persist='NO' \ run='.*' \ test_counter=0 # Let install-config describe a configuration that is incompatible with the # target CI infrastructure export OPENSHIFT_INSTALL_SKIP_PREFLIGHT_VALIDATIONS=1 print_help() { set +x echo -e "Test the OpenStack manifest generation." echo echo -e "Required configuration:" echo echo -e "\\t-c\\tOS_CLOUD" echo -e "\\t-e\\tExternal network" echo -e "\\t-f\\tA valid flavor" echo echo -e "Use:" echo -e "\\t${0} -c -e -f [-a ] [-i ] [-t ] [-p]" echo echo -e 'Additional arguments:' echo -e "\\t-a\\t\\tapiFloatingIP" echo -e "\\t-i\\t\\tpath to openshift-install (defaults to '${openshift_install}')" echo -e "\\t-t\\t\\tpath to the tests to be run (defaults to '${tests_dir}')" echo -e "\\t-p\\t\\tpersist artifacts: do not delete files generated by openshift-install" echo -e "\\t-r \\trun: only run test cases that match the given regular expression" echo echo -e 'Parsed environment variables:' echo -e "\\tOS_CLOUD\\t\\ttarget OpenStack cloud (must match an entry in cloud.yaml)" echo -e "\\tFEATURE_SET\\t\\tsets the cluster feature set. This is used to enable custom features such as tech preview features." echo -e "\\tFEATURE_GATES\\t\\tcomma-separated list of cluster feature gates. This is used to enable custom features. Only to be used in conjunction with FEATURE_SET=CustomNoUpgrade." } fill_install_config() { declare -r \ template="$1" \ pull_secret="'"'{"auths":{"registry.svc.ci.openshift.org":{"auth":"QW4gYWN0dWFsIHB1bGwgc2VjcmV0IGlzIG5vdCBuZWNlc3NhcnkK"}}}'"'" if [[ -n "$FEATURE_SET" ]]; then echo "featureSet: '${FEATURE_SET}'" fi if [[ -n "$FEATURE_GATES" ]]; then echo 'featureGates:' IFS=',' read -ra gates <<< "$FEATURE_GATES" for gate in "${gates[@]}"; do echo "- ${gate}" done fi sed ' s|${\?OS_CLOUD}\?|'"${os_cloud}"'|; s|${\?EXTERNAL_NETWORK}\?|'"${external_network}"'|; s|${\?COMPUTE_FLAVOR}\?|'"${compute_flavor}"'|; s|${\?API_FIP}\?|'"${api_fip}"'|; s|${\?PULL_SECRET}\?|'"${pull_secret}"'|; ' "$template" } validate_configuration() { declare -a required_values=("os_cloud" "external_network" "compute_flavor") declare fail=false for val in "${required_values[@]}"; do declare required=${!val:-} if [ -z "${required}" ]; then >&2 echo "Missing required argument '${val}'." fail=true fi done if [ "$fail" = true ]; then print_help exit 1 fi } while getopts a:c:e:f:i:t:pr:h o; do case "$o" in a) api_fip="$OPTARG" ;; c) os_cloud="$OPTARG" ;; e) external_network="$OPTARG" ;; f) compute_flavor="$OPTARG" ;; i) openshift_install="$OPTARG" ;; t) tests_dir="$OPTARG" ;; p) persist='YES' ;; r) run="$OPTARG" ;; h) print_help; exit 0 ;; *) print_help; exit 1 ;; esac done readonly api_fip os_cloud external_network compute_flavor openshift_install tests_dir persist run declare python_venv if [[ -w './bin' ]]; then python_venv="$(realpath './bin/venv')" else python_venv="$(mktemp -d)" fi readonly python_venv declare -a temp_dirs cleanup() { if [[ "$persist" == 'NO' ]]; then for temp_dir in "${temp_dirs[@]}"; do rm -rf "$temp_dir" done fi } trap cleanup EXIT validate_configuration python -m venv "$python_venv" # shellcheck source=/dev/null source "${python_venv}/bin/activate" pip install unittest-xml-reporting pyyaml >&2 echo "Running the tests from '${tests_dir}' against the Installer binary '${openshift_install}'." export JUNIT_DIR="${ARTIFACT_DIR:-.}/junit" mkdir -p "$JUNIT_DIR" declare result='PASS' for testcase in "${tests_dir}"/* ; do if [ -d "$testcase" ] && [[ "$testcase" =~ $run ]]; then echo echo "*** TEST CASE: $(basename -- "$testcase")" assets_dir="$(mktemp -d)" if [[ "$persist" != 'NO' ]]; then echo "Generated assets for this test can be found in ${assets_dir}" fi ((test_counter+=1)) temp_dirs+=("$assets_dir") fill_install_config "${testcase}/install-config.yaml" > "${assets_dir}/install-config.yaml" "$openshift_install" --log-level warn create manifests --dir "$assets_dir" for t in "${testcase}"/test_*; do declare JUNIT_FILE test_name test_name="$(basename -- "$t")" test_name="${test_name#test_}" test_name="${test_name%.*}" JUNIT_FILE="${JUNIT_DIR}/$(basename -- "${testcase}")_${test_name}.xml" export JUNIT_FILE if $t "$assets_dir"; then echo "PASS: '$t'" else result='FAIL' echo "FAIL: '$t'" fi done fi done echo "Ran ${test_counter} test cases." if [ "$result" != 'PASS' ]; then echo "FAIL" exit 1 fi echo 'PASS'