github.com/openshift/installer@v1.4.17/hack/openstack/test-manifests.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  set -Eeuo pipefail
     4  
     5  [[ $(type -P yq) ]] || { >&2 echo "Required tool 'yq' not found in PATH" ; exit 1; }
     6  
     7  declare \
     8  	tests_dir='./scripts/openstack/manifest-tests' \
     9  	openshift_install='./bin/openshift-install' \
    10  	api_fip='' \
    11  	os_cloud='' \
    12  	external_network='' \
    13  	compute_flavor='' \
    14  	persist='NO' \
    15  	run='.*' \
    16  	test_counter=0
    17  
    18  # Let install-config describe a configuration that is incompatible with the
    19  # target CI infrastructure
    20  export OPENSHIFT_INSTALL_SKIP_PREFLIGHT_VALIDATIONS=1
    21  
    22  print_help() {
    23  	set +x
    24  
    25  	echo -e "Test the OpenStack manifest generation."
    26  	echo
    27  	echo -e "Required configuration:"
    28  	echo
    29  	echo -e "\\t-c\\tOS_CLOUD"
    30  	echo -e "\\t-e\\tExternal network"
    31  	echo -e "\\t-f\\tA valid flavor"
    32  	echo
    33  	echo -e "Use:"
    34  	echo -e "\\t${0} -c <cloud> -e <external network> -f <flavor> [-a <fip>] [-i <openshift-install>] [-t <test-dir>] [-p]"
    35  	echo
    36  	echo -e 'Additional arguments:'
    37  	echo -e "\\t-a\\t\\tapiFloatingIP"
    38  	echo -e "\\t-i\\t\\tpath to openshift-install (defaults to '${openshift_install}')"
    39  	echo -e "\\t-t\\t\\tpath to the tests to be run (defaults to '${tests_dir}')"
    40  	echo -e "\\t-p\\t\\tpersist artifacts: do not delete files generated by openshift-install"
    41  	echo -e "\\t-r <regexp>\\trun: only run test cases that match the given regular expression"
    42  	echo
    43  	echo -e 'Parsed environment variables:'
    44  	echo -e "\\tOS_CLOUD\\t\\ttarget OpenStack cloud (must match an entry in cloud.yaml)"
    45  	echo -e "\\tFEATURE_SET\\t\\tsets the cluster feature set. This is used to enable custom features such as tech preview features."
    46  	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."
    47  }
    48  
    49  fill_install_config() {
    50  	declare -r \
    51  		template="$1" \
    52  		pull_secret="'"'{"auths":{"registry.svc.ci.openshift.org":{"auth":"QW4gYWN0dWFsIHB1bGwgc2VjcmV0IGlzIG5vdCBuZWNlc3NhcnkK"}}}'"'"
    53  
    54  	if [[ -n "$FEATURE_SET" ]]; then
    55  		echo "featureSet: '${FEATURE_SET}'"
    56  	fi
    57  	if [[ -n "$FEATURE_GATES" ]]; then
    58  		echo 'featureGates:'
    59  		IFS=',' read -ra gates <<< "$FEATURE_GATES"
    60  		for gate in "${gates[@]}"; do
    61  			echo "- ${gate}"
    62  		done
    63  	fi
    64  
    65  	sed '
    66  		s|${\?OS_CLOUD}\?|'"${os_cloud}"'|;
    67  		s|${\?EXTERNAL_NETWORK}\?|'"${external_network}"'|;
    68  		s|${\?COMPUTE_FLAVOR}\?|'"${compute_flavor}"'|;
    69  		s|${\?API_FIP}\?|'"${api_fip}"'|;
    70  		s|${\?PULL_SECRET}\?|'"${pull_secret}"'|;
    71  		' "$template"
    72  }
    73  
    74  validate_configuration() {
    75  	declare -a required_values=("os_cloud" "external_network" "compute_flavor")
    76  	declare fail=false
    77  
    78  	for val in "${required_values[@]}"; do
    79  		declare required=${!val:-}
    80  		if [ -z "${required}" ]; then
    81  			>&2 echo "Missing required argument '${val}'."
    82  			fail=true
    83  		fi
    84  	done
    85  
    86  	if [ "$fail" = true ]; then
    87  		print_help
    88  		exit 1
    89  	fi
    90  }
    91  
    92  while getopts a:c:e:f:i:t:pr:h o; do
    93  	case "$o" in
    94  		a) api_fip="$OPTARG" ;;
    95  		c) os_cloud="$OPTARG" ;;
    96  		e) external_network="$OPTARG" ;;
    97  		f) compute_flavor="$OPTARG" ;;
    98  		i) openshift_install="$OPTARG" ;;
    99  		t) tests_dir="$OPTARG" ;;
   100  		p) persist='YES' ;;
   101  		r) run="$OPTARG" ;;
   102  		h) print_help; exit 0 ;;
   103  		*) print_help; exit 1 ;;
   104  	esac
   105  done
   106  readonly api_fip os_cloud external_network compute_flavor openshift_install tests_dir persist run
   107  
   108  declare python_venv
   109  if [[ -w './bin' ]]; then
   110  	python_venv="$(realpath './bin/venv')"
   111  else
   112  	python_venv="$(mktemp -d)"
   113  fi
   114  readonly python_venv
   115  
   116  declare -a temp_dirs
   117  cleanup() {
   118  	if [[ "$persist" == 'NO' ]]; then
   119  		for temp_dir in "${temp_dirs[@]}"; do
   120  			rm -rf "$temp_dir"
   121  		done
   122  	fi
   123  }
   124  trap cleanup EXIT
   125  
   126  validate_configuration
   127  
   128  python -m venv "$python_venv"
   129  # shellcheck source=/dev/null
   130  source "${python_venv}/bin/activate"
   131  pip install unittest-xml-reporting pyyaml
   132  
   133  >&2 echo "Running the tests from '${tests_dir}' against the Installer binary '${openshift_install}'."
   134  
   135  export JUNIT_DIR="${ARTIFACT_DIR:-.}/junit"
   136  mkdir -p "$JUNIT_DIR"
   137  
   138  declare result='PASS'
   139  for testcase in "${tests_dir}"/* ; do
   140  	if [ -d "$testcase" ] && [[ "$testcase" =~ $run ]]; then
   141  		echo
   142  		echo "*** TEST CASE: $(basename -- "$testcase")"
   143  		assets_dir="$(mktemp -d)"
   144  		if [[ "$persist" != 'NO' ]]; then
   145  			echo "Generated assets for this test can be found in ${assets_dir}"
   146  		fi
   147  		((test_counter+=1))
   148  		temp_dirs+=("$assets_dir")
   149  		fill_install_config "${testcase}/install-config.yaml" > "${assets_dir}/install-config.yaml"
   150  		"$openshift_install" --log-level warn create manifests --dir "$assets_dir"
   151  		for t in "${testcase}"/test_*; do
   152  			declare JUNIT_FILE test_name
   153  			test_name="$(basename -- "$t")"
   154  			test_name="${test_name#test_}"
   155  			test_name="${test_name%.*}"
   156  			JUNIT_FILE="${JUNIT_DIR}/$(basename -- "${testcase}")_${test_name}.xml"
   157  			export JUNIT_FILE
   158  			if $t "$assets_dir"; then
   159  				echo "PASS: '$t'"
   160  			else
   161  				result='FAIL'
   162  				echo "FAIL: '$t'"
   163  			fi
   164  		done
   165  	fi
   166  done
   167  
   168  echo "Ran ${test_counter} test cases."
   169  if [ "$result" != 'PASS' ]; then
   170  	echo "FAIL"
   171  	exit 1
   172  fi
   173  
   174  echo 'PASS'