+echo ""
+
+echo -e $BOLD"Checking configured image setting for this test case"$EBOLD
+
+#Temp var to check for image variable name errors
+IMAGE_ERR=0
+#Create a file with image info for later printing as a table
+image_list_file="./tmp/.image-list"
+echo -e "Application\tApp short name\tImage\ttag\ttag-switch" > $image_list_file
+
+# Check if image env var is set and if so export the env var with image to use (used by docker compose files)
+# arg: <app-short-name> <target-variable-name> <image-variable-name> <image-tag-variable-name> <tag-suffix> <image name>
+__check_and_create_image_var() {
+
+ if [ $# -ne 6 ]; then
+ echo "Expected arg: <app-short-name> <target-variable-name> <image-variable-name> <image-tag-variable-name> <tag-suffix> <image name>"
+ ((IMAGE_ERR++))
+ return
+ fi
+
+ __check_included_image $1
+ if [ $? -ne 0 ]; then
+ echo -e "$6\t$1\t<image-excluded>\t<no-tag>" >> $image_list_file
+ # Image is excluded since the corresponding app is not used in this test
+ return
+ fi
+ tmp=${6}"\t"${1}"\t"
+ #Create var from the input var names
+ image="${!3}"
+ tmptag=$4"_"$5
+ tag="${!tmptag}"
+
+ optional_image_repo_target=""
+
+ if [ -z $image ]; then
+ __check_ignore_image $1
+ if [ $? -eq 0 ]; then
+ app_ds=$6
+ if [ -z "$6" ]; then
+ app_ds="<app ignored>"
+ fi
+ echo -e "$app_ds\t$1\t<image-ignored>\t<no-tag>" >> $image_list_file
+ # Image is ignored since the corresponding the images is not set in the env file
+ __remove_included_image $1 # Remove the image from the list of included images
+ return
+ fi
+ echo -e $RED"\$"$3" not set in $TEST_ENV_VAR_FILE"$ERED
+ ((IMAGE_ERR++))
+ echo ""
+ tmp=$tmp"<no-image>\t"
+ else
+
+ optional_image_repo_target=$image
+
+ #Add repo depending on image type
+ if [ "$5" == "REMOTE_RELEASE" ]; then
+ image=$NEXUS_RELEASE_REPO$image
+ fi
+ if [ "$5" == "REMOTE" ]; then
+ image=$NEXUS_STAGING_REPO$image
+ fi
+ if [ "$5" == "REMOTE_SNAPSHOT" ]; then
+ image=$NEXUS_SNAPSHOT_REPO$image
+ fi
+ if [ "$5" == "REMOTE_PROXY" ]; then
+ image=$NEXUS_PROXY_REPO$image
+ fi
+ if [ "$5" == "REMOTE_RELEASE_ONAP" ]; then
+ image=$NEXUS_RELEASE_REPO_ONAP$image
+ fi
+ if [ "$5" == "REMOTE_RELEASE_ORAN" ]; then
+ image=$NEXUS_RELEASE_REPO_ORAN$image
+ fi
+ #No nexus repo added for local images, tag: LOCAL
+ tmp=$tmp$image"\t"
+ fi
+ if [ -z $tag ]; then
+ echo -e $RED"\$"$tmptag" not set in $TEST_ENV_VAR_FILE"$ERED
+ ((IMAGE_ERR++))
+ echo ""
+ tmp=$tmp"<no-tag>\t"
+ else
+ tmp=$tmp$tag
+ fi
+ tmp=$tmp"\t"$5
+ echo -e "$tmp" >> $image_list_file
+ #Export the env var
+ export "${2}"=$image":"$tag #Note, this var may be set to the value of the target value below in __check_and_pull_image
+
+ remote_or_local_push=false
+ if [ ! -z "$IMAGE_REPO_ADR" ] && [[ $5 != *"PROXY"* ]]; then
+ if [ $5 == "LOCAL" ]; then
+ remote_or_local_push=true
+ fi
+ if [[ $5 == *"REMOTE"* ]]; then
+ if [ "$IMAGE_REPO_POLICY" == "remote" ]; then
+ remote_or_local_push=true
+ fi
+ fi
+ fi
+ if $remote_or_local_push; then # Only re-tag and push images according to policy, if repo is given
+ export "${2}_SOURCE"=$image":"$tag #Var to keep the actual source image
+ if [[ $optional_image_repo_target == *"/"* ]]; then # Replace all / with _ for images to push to external repo
+ optional_image_repo_target_tmp=${optional_image_repo_target//\//_}
+ optional_image_repo_target=$optional_image_repo_target_tmp
+ fi
+ export "${2}_TARGET"=$IMAGE_REPO_ADR"/"$optional_image_repo_target":"$tag #Create image + tag for optional image repo - pushed later if needed
+ else
+ export "${2}_SOURCE"=""
+ export "${2}_TARGET"=""
+ fi
+}
+
+# Check if app uses image included in this test run
+# Returns 0 if image is included, 1 if not
+__check_included_image() {
+ for im in $INCLUDED_IMAGES; do
+ if [ "$1" == "$im" ]; then
+ return 0
+ fi
+ done
+ return 1
+}
+
+# Check if app uses a project image
+# Returns 0 if image is included, 1 if not
+__check_project_image() {
+ for im in $PROJECT_IMAGES; do
+ if [ "$1" == "$im" ]; then
+ return 0
+ fi
+ done
+ return 1
+}
+
+# Check if app uses image built by the test script
+# Returns 0 if image is included, 1 if not
+__check_image_local_build() {
+ for im in $LOCAL_IMAGE_BUILD; do
+ if [ "$1" == "$im" ]; then
+ return 0
+ fi
+ done
+ return 1
+}
+
+# Check if app image is conditionally ignored in this test run
+# Returns 0 if image is conditionally ignored, 1 if not
+__check_ignore_image() {
+ for im in $CONDITIONALLY_IGNORED_IMAGES; do
+ if [ "$1" == "$im" ]; then
+ return 0
+ fi
+ done
+ return 1
+}
+
+# Removed image from included list of included images
+# Used when an image is marked as conditionally ignored
+__remove_included_image() {
+ tmp_img_rem_list=""
+ for im in $INCLUDED_IMAGES; do
+ if [ "$1" != "$im" ]; then
+ tmp_img_rem_list=$tmp_img_rem_list" "$im
+ fi
+ done
+ INCLUDED_IMAGES=$tmp_img_rem_list
+ return 0
+}
+
+# Check if app is included in the prestarted set of apps
+# Returns 0 if image is included, 1 if not
+__check_prestarted_image() {
+ for im in $KUBE_PRESTARTED_IMAGES; do
+ if [ "$1" == "$im" ]; then
+ return 0
+ fi
+ done
+ return 1
+}
+
+# Check if an app shall use a local image, based on the cmd parameters
+__check_image_local_override() {
+ for im in $USE_LOCAL_IMAGES; do
+ if [ "$1" == "$im" ]; then
+ return 1
+ fi
+ done
+ return 0
+}
+
+# Check if app uses image override
+# Returns the image/tag suffix LOCAL for local image or REMOTE/REMOTE_RELEASE/REMOTE_SNAPSHOT for staging/release/snapshot image
+__check_image_override() {
+
+ for im in $ORAN_IMAGES_APP_NAMES; do
+ if [ "$1" == "$im" ]; then
+ echo "REMOTE_RELEASE_ORAN"
+ return 0
+ fi
+ done
+
+ for im in $ONAP_IMAGES_APP_NAMES; do
+ if [ "$1" == "$im" ]; then
+ echo "REMOTE_RELEASE_ONAP"
+ return 0
+ fi
+ done
+
+ found=0
+ for im in $PROJECT_IMAGES_APP_NAMES; do
+ if [ "$1" == "$im" ]; then
+ found=1
+ fi
+ done
+
+ if [ $found -eq 0 ]; then
+ echo "REMOTE_PROXY"
+ return 0
+ fi
+
+ suffix=""
+ if [ $IMAGE_CATEGORY == "RELEASE" ]; then
+ suffix="REMOTE_RELEASE"
+ fi
+ if [ $IMAGE_CATEGORY == "DEV" ]; then
+ suffix="REMOTE"
+ fi
+ CTR=0
+ for im in $USE_STAGING_IMAGES; do
+ if [ "$1" == "$im" ]; then
+ suffix="REMOTE"
+ ((CTR++))
+ fi
+ done
+ for im in $USE_RELEASE_IMAGES; do
+ if [ "$1" == "$im" ]; then
+ suffix="REMOTE_RELEASE"
+ ((CTR++))
+ fi
+ done
+ for im in $USE_SNAPSHOT_IMAGES; do
+ if [ "$1" == "$im" ]; then
+ suffix="REMOTE_SNAPSHOT"
+ ((CTR++))
+ fi
+ done
+ for im in $USE_LOCAL_IMAGES; do
+ if [ "$1" == "$im" ]; then
+ suffix="LOCAL"
+ ((CTR++))
+ fi
+ done
+ echo $suffix
+ if [ $CTR -gt 1 ]; then
+ exit 1
+ fi
+ return 0
+}
+
+# Function to re-tag and image and push to another image repo
+__retag_and_push_image() {
+ if [ ! -z "$IMAGE_REPO_ADR" ]; then
+ source_image="${!1}"
+ trg_var_name=$1_"TARGET" # This var is created in func __check_and_create_image_var
+ target_image="${!trg_var_name}"
+
+ if [ -z $target_image ]; then
+ return 0 # Image with no target shall not be pushed
+ fi
+
+ echo -ne " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD}${SAMELINE}"
+ tmp=$(docker image tag $source_image ${target_image} )
+ if [ $? -ne 0 ]; then
+ docker stop $tmp &> ./tmp/.dockererr
+ ((IMAGE_ERR++))
+ echo ""
+ echo -e " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD} - ${RED}Failed${ERED}"
+ cat ./tmp/.dockererr
+ return 1
+ else
+ echo -e " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD} - ${GREEN}OK${EGREEN}"
+ fi
+ echo -ne " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD}${SAMELINE}"
+ tmp=$(docker push ${target_image} )
+ if [ $? -ne 0 ]; then
+ docker stop $tmp &> ./tmp/.dockererr
+ ((IMAGE_ERR++))
+ echo ""
+ echo -e " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD} - ${RED}Failed${ERED}"
+ cat ./tmp/.dockererr
+ return 1
+ else
+ echo -e " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD} - ${GREEN}OK${EGREEN}"
+ fi
+ export "${1}"=$target_image
+ fi
+ return 0
+}
+
+#Function to check if image exist and stop+remove the container+pull new images as needed
+#args <script-start-arg> <descriptive-image-name> <container-base-name> <image-with-tag-var-name>
+__check_and_pull_image() {
+
+ source_image="${!4}"
+
+ echo -e " Checking $BOLD$2$EBOLD container(s) with basename: $BOLD$3$EBOLD using image: $BOLD$source_image$EBOLD"
+ format_string="\"{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}\""
+ tmp_im=$(docker images --format $format_string $source_image)
+
+ if [ $1 == "local" ]; then
+ if [ -z "$tmp_im" ]; then
+ echo -e " "$2" (local image): \033[1m"$source_image"\033[0m $RED does not exist in local registry, need to be built (or manually pulled)"$ERED
+ ((IMAGE_ERR++))
+ return 1
+ else
+ echo -e " "$2" (local image): \033[1m"$source_image"\033[0m "$GREEN"OK"$EGREEN
+ fi
+ elif [ $1 == "remote" ] || [ $1 == "remote-remove" ]; then
+ if [ $1 == "remote-remove" ]; then
+ if [ $RUNMODE == "DOCKER" ]; then
+ echo -ne " Attempt to stop and remove container(s), if running - ${SAMELINE}"
+ tmp=$(docker ps -aq --filter name=${3} --filter network=${DOCKER_SIM_NWNAME})
+ if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
+ docker stop $tmp &> ./tmp/.dockererr
+ if [ $? -ne 0 ]; then
+ ((IMAGE_ERR++))
+ echo ""
+ echo -e $RED" Container(s) could not be stopped - try manual stopping the container(s)"$ERED
+ cat ./tmp/.dockererr
+ return 1
+ fi
+ fi
+ echo -ne " Attempt to stop and remove container(s), if running - "$GREEN"stopped"$EGREEN"${SAMELINE}"
+ tmp=$(docker ps -aq --filter name=${3} --filter network=${DOCKER_SIM_NWNAME}) &> /dev/null
+ if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
+ docker rm $tmp &> ./tmp/.dockererr
+ if [ $? -ne 0 ]; then
+ ((IMAGE_ERR++))
+ echo ""
+ echo -e $RED" Container(s) could not be removed - try manual removal of the container(s)"$ERED
+ cat ./tmp/.dockererr
+ return 1
+ fi
+ fi
+ echo -e " Attempt to stop and remove container(s), if running - "$GREEN"stopped removed"$EGREEN
+ tmp_im=""
+ else
+ tmp_im=""
+ fi
+ fi
+ if [ -z "$tmp_im" ]; then
+ echo -ne " Pulling image${SAMELINE}"
+ out=$(docker pull $source_image)
+ if [ $? -ne 0 ]; then
+ echo ""
+ echo -e " Pulling image -$RED could not be pulled"$ERED
+ ((IMAGE_ERR++))
+ echo $out > ./tmp/.dockererr
+ echo $out
+ return 1
+ fi
+ echo $out > ./tmp/.dockererr
+ if [[ $out == *"up to date"* ]]; then
+ echo -e " Pulling image -$GREEN Image is up to date $EGREEN"
+ elif [[ $out == *"Downloaded newer image"* ]]; then
+ echo -e " Pulling image -$GREEN Newer image pulled $EGREEN"
+ else
+ echo -e " Pulling image -$GREEN Pulled $EGREEN"
+ fi
+ else
+ echo -e " Pulling image -$GREEN OK $EGREEN(exists in local repository)"
+ fi
+ fi
+
+ __retag_and_push_image $4
+
+ return $?
+}
+
+setup_testenvironment() {
+ # Check that image env setting are available
+ echo ""
+
+ # Image var setup for all project images included in the test
+ for imagename in $APP_SHORT_NAMES; do
+ __check_included_image $imagename
+ incl=$?
+ __check_project_image $imagename
+ proj=$?
+ if [ $incl -eq 0 ]; then
+ if [ $proj -eq 0 ]; then
+ IMAGE_SUFFIX=$(__check_image_override $imagename)
+ if [ $? -ne 0 ]; then
+ echo -e $RED"Image setting from cmd line not consistent for $imagename."$ERED
+ ((IMAGE_ERR++))
+ fi
+ else
+ IMAGE_SUFFIX="none"
+ fi
+ # A function name is created from the app short name
+ # for example app short name 'ICS' -> produce the function
+ # name __ICS_imagesetup
+ # This function is called and is expected to exist in the imported
+ # file for the ics test functions
+ # The resulting function impl will call '__check_and_create_image_var' function
+ # with appropriate parameters
+ # If the image suffix is none, then the component decides the suffix
+ function_pointer="__"$imagename"_imagesetup"
+ $function_pointer $IMAGE_SUFFIX
+
+ function_pointer="__"$imagename"_test_requirements"
+ $function_pointer
+ fi
+ done
+
+ #Errors in image setting - exit
+ if [ $IMAGE_ERR -ne 0 ]; then
+ exit 1
+ fi
+
+ #Print a tables of the image settings
+ echo -e $BOLD"Images configured for start arg: "$START_ARG $EBOLD
+ column -t -s $'\t' $image_list_file | indent1
+
+ echo ""
+
+ #Set the SIM_GROUP var
+ echo -e $BOLD"Setting var to main dir of all container/simulator scripts"$EBOLD
+ if [ -z "$SIM_GROUP" ]; then
+ SIM_GROUP=$AUTOTEST_HOME/../simulator-group
+ if [ ! -d $SIM_GROUP ]; then
+ echo "Trying to set env var SIM_GROUP to dir 'simulator-group' in the nontrtric repo, but failed."
+ echo -e $RED"Please set the SIM_GROUP manually in the applicable $TEST_ENV_VAR_FILE"$ERED
+ exit 1
+ else
+ echo " SIM_GROUP auto set to: " $SIM_GROUP
+ fi
+ elif [ $SIM_GROUP = *simulator_group ]; then
+ echo -e $RED"Env var SIM_GROUP does not seem to point to dir 'simulator-group' in the repo, check $TEST_ENV_VAR_FILE"$ERED
+ exit 1
+ else
+ echo " SIM_GROUP env var already set to: " $SIM_GROUP
+ fi
+
+ echo ""
+
+ #Temp var to check for image pull errors
+ IMAGE_ERR=0
+
+ # Delete namespaces
+ echo -e $BOLD"Deleting namespaces"$EBOLD
+
+
+ if [ "$DELETE_KUBE_NAMESPACES" -eq 1 ]; then
+ test_env_namespaces=$(kubectl get ns --no-headers -o custom-columns=":metadata.name" -l autotest=engine) #Get list of ns created by the test env
+ if [ $? -ne 0 ]; then
+ echo " Cannot get list of namespaces...ignoring delete"
+ else
+ for test_env_ns in $test_env_namespaces; do
+ __kube_delete_namespace $test_env_ns
+ done
+ fi
+ else
+ echo " Namespace delete option not set"
+ fi
+ echo ""
+
+ # Delete containers
+ echo -e $BOLD"Deleting containers"$EBOLD
+
+ if [ "$DELETE_CONTAINERS" -eq 1 ]; then
+ echo " Stopping containers label 'nrttest_app'..."
+ docker stop $(docker ps -qa --filter "label=nrttest_app") 2> /dev/null
+ echo " Removing stopped containers..."
+ docker rm $(docker ps -qa --filter "label=nrttest_app") 2> /dev/null
+ else
+ echo " Contatiner delete option not set"
+ fi
+ echo ""
+
+ # The following sequence pull the configured images
+ echo -e $BOLD"Pulling configured images, if needed"$EBOLD
+ if [ ! -z "$IMAGE_REPO_ADR" ] && [ $IMAGE_REPO_POLICY == "local" ]; then
+ echo -e $YELLOW" Excluding all remote image check/pull when running with image repo: $IMAGE_REPO_ADR and image policy $IMAGE_REPO_POLICY"$EYELLOW
+ else
+ for imagename in $APP_SHORT_NAMES; do
+ __check_included_image $imagename
+ incl=$?
+ __check_project_image $imagename
+ proj=$?
+ if [ $incl -eq 0 ]; then
+ if [ $proj -eq 0 ]; then
+ START_ARG_MOD=$START_ARG
+ __check_image_local_override $imagename
+ if [ $? -eq 1 ]; then
+ START_ARG_MOD="local"
+ fi
+ else
+ START_ARG_MOD=$START_ARG
+ fi
+ __check_image_local_build $imagename
+ #No pull of images built locally
+ if [ $? -ne 0 ]; then
+ # A function name is created from the app short name
+ # for example app short name 'HTTPPROXY' -> produce the function
+ # name __HTTPPROXY_imagesetup
+ # This function is called and is expected to exist in the imported
+ # file for the httpproxy test functions
+ # The resulting function impl will call '__check_and_pull_image' function
+ # with appropriate parameters
+ function_pointer="__"$imagename"_imagepull"
+ $function_pointer $START_ARG_MOD $START_ARG
+ fi
+ else
+ echo -e $YELLOW" Excluding $imagename image from image check/pull"$EYELLOW
+ fi
+ done
+ fi
+
+ #Errors in image setting - exit
+ if [ $IMAGE_ERR -ne 0 ]; then
+ echo ""
+ echo "#################################################################################################"
+ echo -e $RED"One or more images could not be pulled or containers using the images could not be stopped/removed"$ERED
+ echo -e $RED"Or local image, overriding remote image, does not exist"$ERED
+ if [ $IMAGE_CATEGORY == "DEV" ]; then
+ echo ""
+ echo -e $RED"Note that SNAPSHOT and staging images may be purged from nexus after a certain period."$ERED
+ echo -e $RED"In addition, the image may not have been updated in the current release so no SNAPSHOT or staging image exists"$ERED
+ echo -e $RED"In these cases, switch to use a released image instead, use the flag '--use-release-image <App-short-name>'"$ERED
+ echo -e $RED"Use the 'App-short-name' for the applicable image from the above table: 'Images configured for start arg'."$ERED
+ fi
+ echo "#################################################################################################"
+ echo ""
+ exit 1
+ fi
+
+ echo ""
+
+ echo -e $BOLD"Building images needed for test"$EBOLD
+
+ for imagename in $APP_SHORT_NAMES; do
+ cd $AUTOTEST_HOME #Always reset to orig dir
+ __check_image_local_build $imagename
+ if [ $? -eq 0 ]; then
+ __check_included_image $imagename
+ if [ $? -eq 0 ]; then
+ # A function name is created from the app short name
+ # for example app short name 'MR' -> produce the function
+ # name __MR_imagebuild
+ # This function is called and is expected to exist in the imported
+ # file for the mr test functions
+ # The resulting function impl shall build the imagee
+ function_pointer="__"$imagename"_imagebuild"
+ $function_pointer
+
+ else
+ echo -e $YELLOW" Excluding image for app $imagename from image build"$EYELLOW
+ fi
+ fi
+ done
+
+ cd $AUTOTEST_HOME # Just to make sure...
+
+ echo ""
+
+ # Create a table of the images used in the script - from local repo
+ echo -e $BOLD"Local docker registry images used in this test script"$EBOLD
+
+ docker_tmp_file=./tmp/.docker-images-table
+ format_string="{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}\\t{{.CreatedAt}}"
+ echo -e "Application\tRepository\tTag\tCreated since\tSize\tCreated at" > $docker_tmp_file
+
+ for imagename in $APP_SHORT_NAMES; do
+ __check_included_image $imagename
+ if [ $? -eq 0 ]; then
+ # Only print image data if image repo is null, or if image repo is set and image is local
+ print_image_data=0
+ if [ -z "$IMAGE_REPO_ADR" ]; then
+ print_image_data=1
+ else
+ __check_image_local_build $imagename
+ if [ $? -eq 0 ]; then
+ print_image_data=1
+ fi
+ fi
+ if [ $print_image_data -eq 1 ]; then
+ # A function name is created from the app short name
+ # for example app short name 'MR' -> produce the function
+ # name __MR_imagebuild
+ # This function is called and is expected to exist in the imported
+ # file for the mr test functions
+ # The resulting function impl shall build the imagee
+ function_pointer="__"$imagename"_image_data"
+ $function_pointer "$format_string" $docker_tmp_file
+ fi
+ fi
+ done
+
+ column -t -s $'\t' $docker_tmp_file | indent1
+
+ echo ""
+
+ if [ ! -z "$IMAGE_REPO_ADR" ]; then
+
+ # Create a table of the images used in the script - from remote repo
+ echo -e $BOLD"Remote repo images used in this test script"$EBOLD
+ echo -e $YELLOW"-- Note: These image will be pulled when the container starts. Images not managed by the test engine --"$EYELLOW
+
+ docker_tmp_file=./tmp/.docker-images-table
+ format_string="{{.Repository}}\\t{{.Tag}}"
+ echo -e "Application\tRepository\tTag" > $docker_tmp_file
+
+ for imagename in $APP_SHORT_NAMES; do
+ __check_included_image $imagename
+ if [ $? -eq 0 ]; then
+ # Only print image data if image repo is null, or if image repo is set and image is local
+ __check_image_local_build $imagename
+ if [ $? -ne 0 ]; then
+ # A function name is created from the app short name
+ # for example app short name 'MR' -> produce the function
+ # name __MR_imagebuild
+ # This function is called and is expected to exist in the imported
+ # file for the mr test functions
+ # The resulting function impl shall build the imagee
+ function_pointer="__"$imagename"_image_data"
+ $function_pointer "$format_string" $docker_tmp_file
+ fi
+ fi
+ done
+
+ column -t -s $'\t' $docker_tmp_file | indent1
+
+ echo ""
+ fi