3 # ============LICENSE_START===============================================
4 # Copyright (C) 2020-2023 Nordix Foundation. All rights reserved.
5 # ========================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 # ============LICENSE_END=================================================
20 # This is a script that contains container/service management functions and test functions for Producer stub
23 ################ Test engine functions ################
25 # Create the image var used during the test
26 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
27 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
28 __PRODSTUB_imagesetup() {
29 __check_and_create_image_var PRODSTUB "PROD_STUB_IMAGE" "PROD_STUB_IMAGE_BASE" "PROD_STUB_IMAGE_TAG" LOCAL "$PROD_STUB_DISPLAY_NAME" $IMAGE_TARGET_PLATFORM_IMG_TAG
32 # Pull image from remote repo or use locally built image
33 # arg: <pull-policy-override> <pull-policy-original>
34 # <pull-policy-override> Shall be used for images allowing overriding. For example use a local image when test is started to use released images
35 # <pull-policy-original> Shall be used for images that does not allow overriding
36 # Both var may contain: 'remote', 'remote-remove' or 'local'
37 __PRODSTUB_imagepull() {
38 echo -e $RED"Image for app PRODSTUB shall never be pulled from remote repo"$ERED
41 # Build image (only for simulator or interfaces stubs owned by the test environment)
42 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
43 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
44 __PRODSTUB_imagebuild() {
46 echo " Building PRODSTUB - $PROD_STUB_DISPLAY_NAME - image: $PROD_STUB_IMAGE"
47 docker build $IMAGE_TARGET_PLATFORM_CMD_PARAM --build-arg NEXUS_PROXY_REPO=$NEXUS_PROXY_REPO -t $PROD_STUB_IMAGE . &> .dockererr
49 echo -e $GREEN" Build Ok"$EGREEN
50 __retag_and_push_image PROD_STUB_IMAGE
55 echo -e $RED" Build Failed"$ERED
58 echo -e $RED"Exiting...."$ERED
63 # Generate a string for each included image using the app display name and a docker images format string
64 # If a custom image repo is used then also the source image from the local repo is listed
65 # arg: <docker-images-format-string> <file-to-append>
66 __PRODSTUB_image_data() {
67 echo -e "$PROD_STUB_DISPLAY_NAME\t$(docker images --format $1 $PROD_STUB_IMAGE)" >> $2
68 if [ ! -z "$PROD_STUB_IMAGE_SOURCE" ]; then
69 echo -e "-- source image --\t$(docker images --format $1 $PROD_STUB_IMAGE_SOURCE)" >> $2
73 # Scale kubernetes resources to zero
74 # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
75 # This function is called for apps fully managed by the test script
76 __PRODSTUB_kube_scale_zero() {
77 __kube_scale_all_resources $KUBE_SIM_NAMESPACE autotest PRODSTUB
80 # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
81 # This function is called for pre-started apps not managed by the test script.
82 __PRODSTUB_kube_scale_zero_and_wait() {
83 echo -e $RED" PRODSTUB app is not scaled in this state"$ERED
86 # Delete all kube resources for the app
87 # This function is called for apps managed by the test script.
88 __PRODSTUB_kube_delete_all() {
89 __kube_delete_all_resources $KUBE_SIM_NAMESPACE autotest PRODSTUB
93 # This function is called for apps managed by the test script.
94 # args: <log-dir> <file-prefix>
95 __PRODSTUB_store_docker_logs() {
96 if [ $RUNMODE == "KUBE" ]; then
97 kubectl $KUBECONF logs -l "autotest=PRODSTUB" -n $KUBE_SIM_NAMESPACE --tail=-1 > $1$2_prodstub.log 2>&1
99 docker logs $PROD_STUB_APP_NAME > $1$2_prodstub.log 2>&1
103 # Initial setup of protocol, host and ports
104 # This function is called for apps managed by the test script.
106 __PRODSTUB_initial_setup() {
110 # Set app short-name, app name and namespace for logging runtime statistics of kubernetes pods or docker containers
111 # For docker, the namespace shall be excluded
112 # This function is called for apps managed by the test script as well as for pre-started apps.
114 __PRODSTUB_statistics_setup() {
115 if [ $RUNMODE == "KUBE" ]; then
116 echo "PRODSTUB $PROD_STUB_APP_NAME $KUBE_SIM_NAMESPACE"
118 echo "PRODSTUB $PROD_STUB_APP_NAME"
122 # Check application requirements, e.g. helm, the the test needs. Exit 1 if req not satisfied
124 __PRODSTUB_test_requirements() {
128 #######################################################
130 # Set http as the protocol to use for all communication to the Prod stub sim
132 # (Function for test scripts)
133 use_prod_stub_http() {
134 __prod_stub_set_protocoll "http" $PROD_STUB_INTERNAL_PORT $PROD_STUB_EXTERNAL_PORT
137 # Set https as the protocol to use for all communication to the Prod stub sim
139 # (Function for test scripts)
140 use_prod_stub_https() {
141 __prod_stub_set_protocoll "https" $PROD_STUB_INTERNAL_SECURE_PORT $PROD_STUB_EXTERNAL_SECURE_PORT
144 # Setup paths to svc/container for internal and external access
145 # args: <protocol> <internal-port> <external-port>
146 __prod_stub_set_protocoll() {
147 echo -e $BOLD"$PROD_STUB_DISPLAY_NAME protocol setting"$EBOLD
148 echo -e " Using $BOLD $1 $EBOLD towards $PROD_STUB_DISPLAY_NAME"
150 ## Access to Prod stub sim
152 PROD_STUB_SERVICE_PATH=$1"://"$PROD_STUB_APP_NAME":"$2 # docker access, container->container and script->container via proxy
153 if [ $RUNMODE == "KUBE" ]; then
154 PROD_STUB_SERVICE_PATH=$1"://"$PROD_STUB_APP_NAME.$KUBE_SIM_NAMESPACE":"$3 # kube access, pod->svc and script->svc via proxy
160 ### Admin API functions producer stub
162 ###########################
163 ### Producer stub functions
164 ###########################
166 # Export env vars for config files, docker compose and kube resources
168 __prodstub_export_vars() {
169 export PROD_STUB_APP_NAME
170 export PROD_STUB_DISPLAY_NAME
172 export DOCKER_SIM_NWNAME
173 export KUBE_SIM_NAMESPACE
175 export PROD_STUB_IMAGE
176 export PROD_STUB_INTERNAL_PORT
177 export PROD_STUB_INTERNAL_SECURE_PORT
178 export PROD_STUB_EXTERNAL_PORT
179 export PROD_STUB_EXTERNAL_SECURE_PORT
183 # Start the Producer stub in the simulator group
185 # (Function for test scripts)
188 echo -e $BOLD"Starting $PROD_STUB_DISPLAY_NAME"$EBOLD
190 if [ $RUNMODE == "KUBE" ]; then
192 # Check if app shall be fully managed by the test script
193 __check_included_image "PRODSTUB"
196 # Check if app shall only be used by the test script
197 __check_prestarted_image "PRODSTUB"
200 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
201 echo -e $RED"The $PROD_STUB_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
202 echo -e $RED"The $PROD_STUB_APP_NAME will not be started"$ERED
205 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
206 echo -e $RED"The $PROD_STUB_APP_NAME app is included both as managed and prestarted in this test script"$ERED
207 echo -e $RED"The $PROD_STUB_APP_NAME will not be started"$ERED
211 if [ $retcode_p -eq 0 ]; then
212 echo -e " Using existing $PROD_STUB_APP_NAME deployment and service"
213 echo " Setting RC replicas=1"
214 __kube_scale deployment $PROD_STUB_APP_NAME $KUBE_SIM_NAMESPACE 1
217 if [ $retcode_i -eq 0 ]; then
218 echo -e " Creating $PROD_STUB_APP_NAME deployment and service"
220 __kube_create_namespace $KUBE_SIM_NAMESPACE
222 __prodstub_export_vars
225 input_yaml=$SIM_GROUP"/"$PROD_STUB_COMPOSE_DIR"/"svc.yaml
226 output_yaml=$PWD/tmp/prodstub_svc.yaml
227 __kube_create_instance service $PROD_STUB_APP_NAME $input_yaml $output_yaml
230 input_yaml=$SIM_GROUP"/"$PROD_STUB_COMPOSE_DIR"/"app.yaml
231 output_yaml=$PWD/tmp/prodstub_app.yaml
232 __kube_create_instance app $PROD_STUB_APP_NAME $input_yaml $output_yaml
235 __check_service_start $PROD_STUB_APP_NAME $PROD_STUB_SERVICE_PATH$PROD_STUB_ALIVE_URL
237 echo -ne " Service $PROD_STUB_APP_NAME - reset "$SAMELINE
238 result=$(__do_curl $PROD_STUB_SERVICE_PATH/reset)
239 if [ $? -ne 0 ]; then
240 echo -e " Service $PROD_STUB_APP_NAME - reset $RED Failed $ERED - will continue"
242 echo -e " Service $PROD_STUB_APP_NAME - reset $GREEN OK $EGREEN"
246 # Check if docker app shall be fully managed by the test script
247 __check_included_image 'PRODSTUB'
248 if [ $? -eq 1 ]; then
249 echo -e $RED"The Producer stub app is not included as managed in this test script"$ERED
250 echo -e $RED"The Producer stub will not be started"$ERED
254 __prodstub_export_vars
256 __start_container $PROD_STUB_COMPOSE_DIR "" NODOCKERARGS 1 $PROD_STUB_APP_NAME
258 __check_service_start $PROD_STUB_APP_NAME $PROD_STUB_SERVICE_PATH$PROD_STUB_ALIVE_URL
264 # Execute a curl cmd towards the prodstub simulator and check the response code.
265 # args: TEST|CONF <expected-response-code> <curl-cmd-string> [<json-file-to-compare-output>]
266 __execute_curl_to_prodstub() {
267 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
268 echo "(${BASH_LINENO[0]}) - ${TIMESTAMP}: ${FUNCNAME[0]}" $@ >> $HTTPLOG
270 if [ ! -z "$KUBE_PROXY_PATH" ]; then
271 if [ $KUBE_PROXY_HTTPX == "http" ]; then
272 proxyflag=" --proxy $KUBE_PROXY_PATH"
274 proxyflag=" --proxy-insecure --proxy $KUBE_PROXY_PATH"
277 if [ ! -z "$KUBE_PROXY_CURL_JWT" ]; then
278 jwt="-H "\""Authorization: Bearer $KUBE_PROXY_CURL_JWT"\"
279 echo " CMD: $3 $proxyflag $jwt" >> $HTTPLOG
280 res=$($3 $proxyflag -H "Authorization: Bearer $KUBE_PROXY_CURL_JWT")
282 echo " CMD: $3 $proxyflag" >> $HTTPLOG
283 res="$($3 $proxyflag)"
285 echo " RESP: $res" >> $HTTPLOG
287 if [ $retcode -ne 0 ]; then
288 __log_conf_fail_general " Fatal error when executing curl, response: "$retcode
291 status=${res:${#res}-3}
292 if [ $status -eq $2 ]; then
293 if [ $# -eq 4 ]; then
294 body=${res:0:${#res}-3}
296 echo " TARGET JSON: $jobfile" >> $HTTPLOG
297 res=$(python3 ../common/compare_json.py "$jobfile" "$body")
298 if [ $res -ne 0 ]; then
299 if [ $1 == "TEST" ]; then
307 if [ $1 == "TEST" ]; then
314 if [ $1 == "TEST" ]; then
315 __log_test_fail_status_code $2 $status
317 __log_conf_fail_status_code $2 $status
322 # Prodstub API: Set (or reset) response code for producer supervision
323 # <response-code> <producer-id> [<forced_response_code>]
324 # (Function for test scripts)
325 prodstub_arm_producer() {
327 if [ $# -ne 2 ] && [ $# -ne 3 ]; then
328 __print_err "<response-code> <producer-id> [<forced_response_code>]" $@
332 curlString="curl -X PUT -skw %{http_code} $PROD_STUB_SERVICE_PATH/arm/supervision/"$2
333 if [ $# -eq 3 ]; then
334 curlString=$curlString"?response="$3
337 __execute_curl_to_prodstub CONF $1 "$curlString"
341 # Prodstub API: Set (or reset) response code job create
342 # <response-code> <producer-id> <job-id> [<forced_response_code>]
343 # (Function for test scripts)
344 prodstub_arm_job_create() {
346 if [ $# -ne 3 ] && [ $# -ne 4 ]; then
347 __print_err "<response-code> <producer-id> <job-id> [<forced_response_code>]" $@
351 curlString="curl -X PUT -skw %{http_code} $PROD_STUB_SERVICE_PATH/arm/create/$2/$3"
352 if [ $# -eq 4 ]; then
353 curlString=$curlString"?response="$4
356 __execute_curl_to_prodstub CONF $1 "$curlString"
360 # Prodstub API: Set (or reset) response code job delete
361 # <response-code> <producer-id> <job-id> [<forced_response_code>]
362 # (Function for test scripts)
363 prodstub_arm_job_delete() {
365 if [ $# -ne 3 ] && [ $# -ne 4 ]; then
366 __print_err "<response-code> <producer-id> <job-id> [<forced_response_code>]" $@
370 curlString="curl -X PUT -skw %{http_code} $PROD_STUB_SERVICE_PATH/arm/delete/$2/$3"
371 if [ $# -eq 4 ]; then
372 curlString=$curlString"?response="$4
375 __execute_curl_to_prodstub CONF $1 "$curlString"
379 # Prodstub API: Arm a type of a producer
380 # <response-code> <producer-id> <type-id>
381 # (Function for test scripts)
382 prodstub_arm_type() {
384 if [ $# -ne 3 ]; then
385 __print_err "<response-code> <producer-id> <type-id>" $@
389 curlString="curl -X PUT -skw %{http_code} $PROD_STUB_SERVICE_PATH/arm/type/$2/$3"
391 __execute_curl_to_prodstub CONF $1 "$curlString"
395 # Prodstub API: Disarm a type in a producer
396 # <response-code> <producer-id> <type-id>
397 # (Function for test scripts)
398 prodstub_disarm_type() {
400 if [ $# -ne 3 ]; then
401 __print_err "<response-code> <producer-id> <type-id>" $@
405 curlString="curl -X DELETE -skw %{http_code} $PROD_STUB_SERVICE_PATH/arm/type/$2/$3"
407 __execute_curl_to_prodstub CONF $1 "$curlString"
411 # Prodstub API: Get job data for a job and compare with a target job json
412 # <response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>
413 # (Function for test scripts)
414 prodstub_check_jobdata() {
416 if [ $# -ne 7 ]; then
417 __print_err "<response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>" $@
422 jobfile=$(echo "$jobfile" | sed "s/XXXX/$3/g")
424 __log_test_fail_general "Template file "$7" for jobdata, does not exist"
427 targetJson="{\"ei_job_identity\":\"$3\",\"ei_type_identity\":\"$4\",\"target_uri\":\"$5\",\"owner\":\"$6\", \"ei_job_data\":$jobfile}"
429 echo "$targetJson" > $file
431 curlString="curl -X GET -skw %{http_code} $PROD_STUB_SERVICE_PATH/jobdata/$2/$3"
433 __execute_curl_to_prodstub TEST $1 "$curlString" $file
437 # Prodstub API: Get job data for a job and compare with a target job json
438 # <response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>
439 # (Function for test scripts)
440 prodstub_check_jobdata_2() {
442 if [ $# -ne 7 ]; then
443 __print_err "<response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>" $@
448 jobfile=$(echo "$jobfile" | sed "s/XXXX/$3/g")
450 __log_test_fail_general "Template file "$7" for jobdata, does not exist"
453 targetJson="{\"ei_job_identity\":\"$3\",\"ei_type_identity\":\"$4\",\"target_uri\":\"$5\",\"owner\":\"$6\", \"ei_job_data\":$jobfile,\"last_updated\":\"????\"}"
456 echo "$targetJson" > $file
458 curlString="curl -X GET -skw %{http_code} $PROD_STUB_SERVICE_PATH/jobdata/$2/$3"
460 __execute_curl_to_prodstub TEST $1 "$curlString" $file
464 # Prodstub API: Get job data for a job and compare with a target job json (info-jobs)
465 # <response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>
466 # (Function for test scripts)
467 prodstub_check_jobdata_3() {
469 if [ $# -ne 7 ]; then
470 __print_err "<response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>" $@
475 jobfile=$(echo "$jobfile" | sed "s/XXXX/$3/g")
477 __log_test_fail_general "Template file "$7" for jobdata, does not exist"
480 targetJson="{\"info_job_identity\":\"$3\",\"info_type_identity\":\"$4\",\"target_uri\":\"$5\",\"owner\":\"$6\", \"info_job_data\":$jobfile,\"last_updated\":\"????\"}"
482 echo "$targetJson" > $file
484 curlString="curl -X GET -skw %{http_code} $PROD_STUB_SERVICE_PATH/jobdata/$2/$3"
486 __execute_curl_to_prodstub TEST $1 "$curlString" $file
490 # Prodstub API: Delete the job data
491 # <response-code> <producer-id> <job-id>
492 # (Function for test scripts)
493 prodstub_delete_jobdata() {
495 if [ $# -ne 3 ]; then
496 __print_err "<response-code> <producer-id> <job-id> " $@
499 curlString="curl -X DELETE -skw %{http_code} $PROD_STUB_SERVICE_PATH/jobdata/$2/$3"
501 __execute_curl_to_prodstub CONF $1 "$curlString"
505 # Tests if a variable value in the prod stub is equal to a target value and and optional timeout.
506 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
507 # equal to the target or not.
508 # Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
509 # before setting pass or fail depending on if the variable value becomes equal to the target
511 # (Function for test scripts)
513 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
514 __var_test "PRODSTUB" "$PROD_STUB_SERVICE_PATH/counter/" $1 "=" $2 $3
516 __print_err "Wrong args to prodstub_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@