test: improving the handling of arguments within the script

so far all the arguments were being handled as separate getopts
flags introducing a lot of boilerplate code for every new flag.
The new approach improves the arguments handling by using env
variables.

Signed-off-by: Redouane Kachach <rkachach@redhat.com>
This commit is contained in:
Redouane Kachach committed 2024-01-12 11:47:37 +01:00
1 parent ba19596190
commit ddd852d767
1 file changed
+37 -59
+37 -59
View File
@@ -4,17 +4,19 @@ DEFAULT_NS="rook-ceph"
CLUSTER_FILES="common.yaml operator.yaml cluster-test.yaml cluster-on-pvc-minikube.yaml dashboard-external-http.yaml toolbox.yaml"
MONITORING_FILES="monitoring/prometheus.yaml monitoring/service-monitor.yaml monitoring/exporter-service-monitor.yaml monitoring/prometheus-service.yaml monitoring/rbac.yaml"
SCRIPT_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)
ROOK_EXAMPLES_DIR="${SCRIPT_ROOT}/../../deploy/examples/"
# Script arguments: new arguments must be added here (following the same format)
export ROOK_PROFILE_NAME="${ROOK_PROFILE_NAME:=rook}" ## Specify the minikube profile name
export ROOK_CLUSTER_NS="${ROOK_CLUSTER_NS:=$DEFAULT_NS}" ## CephCluster namespace
export ROOK_OPERATOR_NS="${ROOK_OPERATOR_NS:=$DEFAULT_NS}" ## Rook operator namespace (if different from CephCluster namespace)
export ROOK_EXAMPLES_DIR="${ROOK_EXAMPLES_DIR:=deploy/examples}" ## Path to Rook examples directory (i.e github.com/rook/rook/deploy/examples)
export ROOK_CLUSTER_SPEC_FILE="${ROOK_CLUSTER_SPEC_FILE:=cluster-test.yaml}" ## CephCluster manifest file
init_vars(){
ROOK_PROFILE_NAME=${1:-rook}
ROOK_CLUSTER_NS=${2:-$DEFAULT_NS}
ROOK_OPERATOR_NS=${3:-$DEFAULT_NS}
ROOK_CLUSTER_SPEC_FILE=${4:-cluster-test.yaml}
MINIKUBE="minikube --profile $ROOK_PROFILE_NAME"
KUBECTL="$MINIKUBE kubectl --"
echo "Using '$ROOK_EXAMPLES_DIR' as examples directory.."
echo "Using '$(realpath "$ROOK_EXAMPLES_DIR")' as examples directory.."
echo "Using '$ROOK_CLUSTER_SPEC_FILE' as cluster spec file.."
echo "Using '$ROOK_PROFILE_NAME' as minikube profile.."
echo "Using '$ROOK_CLUSTER_NS' as cluster namespace.."
@@ -22,17 +24,19 @@ init_vars(){
}
update_namespaces() {
for file in $CLUSTER_FILES $MONITORING_FILES; do
echo "Updating namespace on $file"
sed -i.bak \
-e "s/\(.*\):.*# namespace:operator/\1: $ROOK_OPERATOR_NS # namespace:operator/g" \
-e "s/\(.*\):.*# namespace:cluster/\1: $ROOK_CLUSTER_NS # namespace:cluster/g" \
-e "s/\(.*serviceaccount\):.*:\(.*\) # serviceaccount:namespace:operator/\1:$ROOK_OPERATOR_NS:\2 # serviceaccount:namespace:operator/g" \
-e "s/\(.*serviceaccount\):.*:\(.*\) # serviceaccount:namespace:cluster/\1:$ROOK_CLUSTER_NS:\2 # serviceaccount:namespace:cluster/g" \
-e "s/\(.*\): [-_A-Za-z0-9]*\.\(.*\) # driver:namespace:operator/\1: $ROOK_OPERATOR_NS.\2 # driver:namespace:operator/g" \
-e "s/\(.*\): [-_A-Za-z0-9]*\.\(.*\) # driver:namespace:cluster/\1: $ROOK_CLUSTER_NS.\2 # driver:namespace:cluster/g" \
"$file"
done
if [ "$ROOK_CLUSTER_NS" != "$DEFAULT_NS" ] || [ "$ROOK_OPERATOR_NS" != "$DEFAULT_NS" ]; then
for file in $CLUSTER_FILES $MONITORING_FILES; do
echo "Updating namespace on $file"
sed -i.bak \
-e "s/\(.*\):.*# namespace:operator/\1: $ROOK_OPERATOR_NS # namespace:operator/g" \
-e "s/\(.*\):.*# namespace:cluster/\1: $ROOK_CLUSTER_NS # namespace:cluster/g" \
-e "s/\(.*serviceaccount\):.*:\(.*\) # serviceaccount:namespace:operator/\1:$ROOK_OPERATOR_NS:\2 # serviceaccount:namespace:operator/g" \
-e "s/\(.*serviceaccount\):.*:\(.*\) # serviceaccount:namespace:cluster/\1:$ROOK_CLUSTER_NS:\2 # serviceaccount:namespace:cluster/g" \
-e "s/\(.*\): [-_A-Za-z0-9]*\.\(.*\) # driver:namespace:operator/\1: $ROOK_OPERATOR_NS.\2 # driver:namespace:operator/g" \
-e "s/\(.*\): [-_A-Za-z0-9]*\.\(.*\) # driver:namespace:cluster/\1: $ROOK_CLUSTER_NS.\2 # driver:namespace:cluster/g" \
"$file"
done
fi
}
wait_for_ceph_cluster() {
@@ -173,15 +177,13 @@ enable_monitoring() {
show_usage() {
echo ""
echo " Usage: $(basename "$0") [-r] [-m] [-p <profile-name>] [-d /path/to/rook-examples/dir]"
echo " -f Force cluster creation by deleting minikube profile"
echo " -r Enable rook orchestrator"
echo " -m Enable monitoring"
echo " -p <profile-name> Specify the minikube profile name"
echo " -d value Path to Rook examples directory (i.e github.com/rook/rook/deploy/examples)"
echo " -c <cluster-namespace>"
echo " -o <operator-namespace>"
echo " -i <cluster-spec-yaml> Specify the cluster file spec name"
echo "Usage: [ARG=VALUE]... $(basename "$0") [-f] [-r] [-m]"
echo " -f Force cluster creation by deleting minikube profile"
echo " -r Enable rook orchestrator"
echo " -m Enable monitoring"
echo " Args:"
sed -n -E "s/^export (.*)=\".*:=.*\" ## (.*)/ \1 (\\$\1): \2/p;" "$SCRIPT_ROOT"/"$(basename "$0")" | envsubst
echo ""
}
invocation_error() {
@@ -193,7 +195,7 @@ invocation_error() {
####################################################################
################# MAIN #############################################
while getopts ":hrmfd:p:i:c:o:" opt; do
while getopts ":hrmf" opt; do
case $opt in
h)
show_usage
@@ -208,21 +210,6 @@ while getopts ":hrmfd:p:i:c:o:" opt; do
f)
force_minikube=true
;;
d)
ROOK_EXAMPLES_DIR="$OPTARG"
;;
p)
minikube_profile_name="$OPTARG"
;;
i)
cluster_spec_file="$OPTARG"
;;
c)
rook_cluster_ns="$OPTARG"
;;
o)
rook_operator_ns="$OPTARG"
;;
\?)
invocation_error "Invalid option: -$OPTARG"
;;
@@ -232,30 +219,21 @@ while getopts ":hrmfd:p:i:c:o:" opt; do
esac
done
init_vars "$minikube_profile_name" "$rook_cluster_ns" "$rook_operator_ns" "$cluster_spec_file"
# initialization zone
init_vars
change_to_examples_dir
[ -z "$force_minikube" ] && check_minikube_exists
update_namespaces
if [ -z "$force_minikube" ]; then
check_minikube_exists
fi
if [ "$rook_cluster_ns" != "$DEFAULT_NS" ] || [ "$rook_operator_ns" != "$DEFAULT_NS" ]; then
update_namespaces
fi
# cluster creation zone
setup_minikube_env
create_rook_cluster
wait_for_rook_operator
wait_for_ceph_cluster
if [ "$enable_rook" = true ]; then
enable_rook_orchestrator
fi
if [ "$enable_monitoring" = true ]; then
enable_monitoring
fi
# final tweaks and ceph cluster tuning
[ "$enable_rook" = true ] && enable_rook_orchestrator
[ "$enable_monitoring" = true ] && enable_monitoring
show_info "$enable_monitoring"
####################################################################