Added tests and improvements
[nonrtric.git] / test / common / prodstub_api_functions.sh
1 #!/bin/bash
2
3 #  ============LICENSE_START===============================================
4 #  Copyright (C) 2020 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
9 #
10 #       http://www.apache.org/licenses/LICENSE-2.0
11 #
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=================================================
18 #
19
20 # This is a script that contains container/service management functions and test functions for Producer stub
21
22
23 ################ Test engine functions ################
24
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"
30 }
31
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
39 }
40
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() {
45         cd ../prodstub
46         echo " Building PRODSTUB - $PROD_STUB_DISPLAY_NAME - image: $PROD_STUB_IMAGE"
47         docker build  --build-arg NEXUS_PROXY_REPO=$NEXUS_PROXY_REPO -t $PROD_STUB_IMAGE . &> .dockererr
48         if [ $? -eq 0 ]; then
49                 echo -e  $GREEN"  Build Ok"$EGREEN
50                 __retag_and_push_image PROD_STUB_IMAGE
51                 if [ $? -ne 0 ]; then
52                         exit 1
53                 fi
54         else
55                 echo -e $RED"  Build Failed"$ERED
56                 ((RES_CONF_FAIL++))
57                 cat .dockererr
58                 echo -e $RED"Exiting...."$ERED
59                 exit 1
60         fi
61 }
62
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
70         fi
71 }
72
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
78 }
79
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 prestarted 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
84 }
85
86 # Delete all kube resouces 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
90 }
91
92 # Store docker logs
93 # This function is called for apps managed by the test script.
94 # args: <log-dir> <file-prexix>
95 __PRODSTUB_store_docker_logs() {
96         if [ $RUNMODE == "KUBE" ]; then
97                 kubectl  logs -l "autotest=PRODSTUB" -n $KUBE_SIM_NAMESPACE --tail=-1 > $1$2_prodstub.log 2>&1
98         else
99                 docker logs $PROD_STUB_APP_NAME > $1$2_prodstub.log 2>&1
100         fi
101 }
102
103 # Initial setup of protocol, host and ports
104 # This function is called for apps managed by the test script.
105 # args: -
106 __PRODSTUB_initial_setup() {
107         use_prod_stub_http
108 }
109
110 # Set app short-name, app name and namespace for logging runtime statistics of kubernets 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 prestarted apps.
113 # args: -
114 __PRODSTUB_statisics_setup() {
115         if [ $RUNMODE == "KUBE" ]; then
116                 echo "PRODSTUB $PROD_STUB_APP_NAME $KUBE_SIM_NAMESPACE"
117         else
118                 echo "PRODSTUB $PROD_STUB_APP_NAME"
119         fi
120 }
121
122 #######################################################
123
124 # Set http as the protocol to use for all communication to the Prod stub sim
125 # args: -
126 # (Function for test scripts)
127 use_prod_stub_http() {
128         __prod_stub_set_protocoll "http" $PROD_STUB_INTERNAL_PORT $PROD_STUB_EXTERNAL_PORT
129 }
130
131 # Set https as the protocol to use for all communication to the Prod stub sim
132 # args: -
133 # (Function for test scripts)
134 use_prod_stub_https() {
135         __prod_stub_set_protocoll "https" $PROD_STUB_INTERNAL_SECURE_PORT $PROD_STUB_EXTERNAL_SECURE_PORT
136 }
137
138 # Setup paths to svc/container for internal and external access
139 # args: <protocol> <internal-port> <external-port>
140 __prod_stub_set_protocoll() {
141         echo -e $BOLD"$PROD_STUB_DISPLAY_NAME protocol setting"$EBOLD
142         echo -e " Using $BOLD http $EBOLD towards $PROD_STUB_DISPLAY_NAME"
143
144         ## Access to Prod stub sim
145
146         PROD_STUB_SERVICE_PATH=$1"://"$PROD_STUB_APP_NAME":"$2  # docker access, container->container and script->container via proxy
147         if [ $RUNMODE == "KUBE" ]; then
148                 PROD_STUB_SERVICE_PATH=$1"://"$PROD_STUB_APP_NAME.$KUBE_SIM_NAMESPACE":"$3 # kube access, pod->svc and script->svc via proxy
149         fi
150
151         echo ""
152 }
153
154 ### Admin API functions producer stub
155
156 ###########################
157 ### Producer stub functions
158 ###########################
159
160 # Export env vars for config files, docker compose and kube resources
161 # args:
162 __prodstub_export_vars() {
163         export PROD_STUB_APP_NAME
164         export PROD_STUB_APP_NAME_ALIAS
165         export PROD_STUB_DISPLAY_NAME
166
167         export DOCKER_SIM_NWNAME
168         export KUBE_SIM_NAMESPACE
169
170         export PROD_STUB_IMAGE
171         export PROD_STUB_INTERNAL_PORT
172         export PROD_STUB_INTERNAL_SECURE_PORT
173         export PROD_STUB_EXTERNAL_PORT
174         export PROD_STUB_EXTERNAL_SECURE_PORT
175 }
176
177
178 # Start the Producer stub in the simulator group
179 # args: -
180 # (Function for test scripts)
181 start_prod_stub() {
182
183         echo -e $BOLD"Starting $PROD_STUB_DISPLAY_NAME"$EBOLD
184
185         if [ $RUNMODE == "KUBE" ]; then
186
187                 # Check if app shall be fully managed by the test script
188                 __check_included_image "PRODSTUB"
189                 retcode_i=$?
190
191                 # Check if app shall only be used by the testscipt
192                 __check_prestarted_image "PRODSTUB"
193                 retcode_p=$?
194
195                 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
196                         echo -e $RED"The $ECS_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
197                         echo -e $RED"The $ECS_APP_NAME will not be started"$ERED
198                         exit
199                 fi
200                 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
201                         echo -e $RED"The $ECS_APP_NAME app is included both as managed and prestarted in this test script"$ERED
202                         echo -e $RED"The $ECS_APP_NAME will not be started"$ERED
203                         exit
204                 fi
205
206                 if [ $retcode_p -eq 0 ]; then
207                         echo -e " Using existing $PROD_STUB_APP_NAME deployment and service"
208                         echo " Setting RC replicas=1"
209                         __kube_scale deployment $PROD_STUB_APP_NAME $KUBE_SIM_NAMESPACE 1
210                 fi
211
212                 if [ $retcode_i -eq 0 ]; then
213                         echo -e " Creating $PROD_STUB_APP_NAME deployment and service"
214
215             __kube_create_namespace $KUBE_SIM_NAMESPACE
216
217                         __prodstub_export_vars
218
219                         # Create service
220                         input_yaml=$SIM_GROUP"/"$PROD_STUB_COMPOSE_DIR"/"svc.yaml
221                         output_yaml=$PWD/tmp/prodstub_svc.yaml
222                         __kube_create_instance service $PROD_STUB_APP_NAME $input_yaml $output_yaml
223
224                         # Create app
225                         input_yaml=$SIM_GROUP"/"$PROD_STUB_COMPOSE_DIR"/"app.yaml
226                         output_yaml=$PWD/tmp/prodstub_app.yaml
227                         __kube_create_instance app $PROD_STUB_APP_NAME $input_yaml $output_yaml
228                 fi
229
230                 __check_service_start $PROD_STUB_APP_NAME $PROD_STUB_SERVICE_PATH$PROD_STUB_ALIVE_URL
231
232                 echo -ne " Service $PROD_STUB_APP_NAME - reset  "$SAMELINE
233                 result=$(__do_curl $PROD_STUB_SERVICE_PATH/reset)
234                 if [ $? -ne 0 ]; then
235                         echo -e " Service $PROD_STUB_APP_NAME - reset  $RED Failed $ERED - will continue"
236                 else
237                         echo -e " Service $PROD_STUB_APP_NAME - reset  $GREEN OK $EGREEN"
238                 fi
239         else
240
241                 # Check if docker app shall be fully managed by the test script
242                 __check_included_image 'PRODSTUB'
243                 if [ $? -eq 1 ]; then
244                         echo -e $RED"The Producer stub app is not included as managed in this test script"$ERED
245                         echo -e $RED"The Producer stub will not be started"$ERED
246                         exit
247                 fi
248
249                 __prodstub_export_vars
250
251                 __start_container $PROD_STUB_COMPOSE_DIR "" NODOCKERARGS 1 $PROD_STUB_APP_NAME
252
253         __check_service_start $PROD_STUB_APP_NAME $PROD_STUB_SERVICE_PATH$PROD_STUB_ALIVE_URL
254         fi
255     echo ""
256     return 0
257 }
258
259 # Excute a curl cmd towards the prodstub simulator and check the response code.
260 # args: TEST|CONF <expected-response-code> <curl-cmd-string> [<json-file-to-compare-output>]
261 __execute_curl_to_prodstub() {
262     TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
263     echo "(${BASH_LINENO[0]}) - ${TIMESTAMP}: ${FUNCNAME[0]}" $@ >> $HTTPLOG
264         proxyflag=""
265         if [ ! -z "$KUBE_PROXY_PATH" ]; then
266                 if [ $KUBE_PROXY_HTTPX == "http" ]; then
267                         proxyflag=" --proxy $KUBE_PROXY_PATH"
268                 else
269                         proxyflag=" --proxy-insecure --proxy $KUBE_PROXY_PATH"
270                 fi
271         fi
272         echo " CMD: $3 $proxyflag" >> $HTTPLOG
273         res="$($3 $proxyflag)"
274         echo " RESP: $res" >> $HTTPLOG
275         retcode=$?
276     if [ $retcode -ne 0 ]; then
277         __log_conf_fail_general " Fatal error when executing curl, response: "$retcode
278         return 1
279     fi
280     status=${res:${#res}-3}
281     if [ $status -eq $2 ]; then
282         if [ $# -eq 4 ]; then
283             body=${res:0:${#res}-3}
284             jobfile=$(cat $4)
285             echo " TARGET JSON: $jobfile" >> $HTTPLOG
286                     res=$(python3 ../common/compare_json.py "$jobfile" "$body")
287             if [ $res -ne 0 ]; then
288                 if [ $1 == "TEST" ]; then
289                     __log_test_fail_body
290                  else
291                     __log_conf_fail_body
292                 fi
293                 return 1
294             fi
295         fi
296         if [ $1 == "TEST" ]; then
297             __log_test_pass
298         else
299             __log_conf_ok
300         fi
301         return 0
302     fi
303     if [ $1 == "TEST" ]; then
304         __log_test_fail_status_code $2 $status
305         else
306         __log_conf_fail_status_code $2 $status
307     fi
308     return 1
309 }
310
311 # Prodstub API: Set (or reset) response code for producer supervision
312 # <response-code> <producer-id> [<forced_response_code>]
313 # (Function for test scripts)
314 prodstub_arm_producer() {
315         __log_conf_start $@
316         if [ $# -ne 2 ] && [ $# -ne 3 ]; then
317                 __print_err "<response-code> <producer-id> [<forced_response_code>]" $@
318                 return 1
319         fi
320
321     curlString="curl -X PUT -skw %{http_code} $PROD_STUB_SERVICE_PATH/arm/supervision/"$2
322         if [ $# -eq 3 ]; then
323                 curlString=$curlString"?response="$3
324         fi
325
326     __execute_curl_to_prodstub CONF $1 "$curlString"
327     return $?
328 }
329
330 # Prodstub API: Set (or reset) response code job create
331 # <response-code> <producer-id> <job-id> [<forced_response_code>]
332 # (Function for test scripts)
333 prodstub_arm_job_create() {
334         __log_conf_start $@
335         if [ $# -ne 3 ] && [ $# -ne 4 ]; then
336                 __print_err "<response-code> <producer-id> <job-id> [<forced_response_code>]" $@
337                 return 1
338         fi
339
340     curlString="curl -X PUT -skw %{http_code} $PROD_STUB_SERVICE_PATH/arm/create/$2/$3"
341         if [ $# -eq 4 ]; then
342                 curlString=$curlString"?response="$4
343         fi
344
345     __execute_curl_to_prodstub CONF $1 "$curlString"
346     return $?
347 }
348
349 # Prodstub API: Set (or reset) response code job delete
350 # <response-code> <producer-id> <job-id> [<forced_response_code>]
351 # (Function for test scripts)
352 prodstub_arm_job_delete() {
353         __log_conf_start $@
354         if [ $# -ne 3 ] && [ $# -ne 4 ]; then
355                 __print_err "<response-code> <producer-id> <job-id> [<forced_response_code>]" $@
356                 return 1
357         fi
358
359     curlString="curl -X PUT -skw %{http_code} $PROD_STUB_SERVICE_PATH/arm/delete/$2/$3"
360         if [ $# -eq 4 ]; then
361                 curlString=$curlString"?response="$4
362         fi
363
364     __execute_curl_to_prodstub CONF $1 "$curlString"
365     return $?
366 }
367
368 # Prodstub API: Arm a type of a producer
369 # <response-code> <producer-id> <type-id>
370 # (Function for test scripts)
371 prodstub_arm_type() {
372         __log_conf_start $@
373         if [ $# -ne 3 ]; then
374                 __print_err "<response-code> <producer-id> <type-id>" $@
375                 return 1
376         fi
377
378     curlString="curl -X PUT -skw %{http_code} $PROD_STUB_SERVICE_PATH/arm/type/$2/$3"
379
380     __execute_curl_to_prodstub CONF $1 "$curlString"
381     return $?
382 }
383
384 # Prodstub API: Disarm a type in a producer
385 # <response-code> <producer-id> <type-id>
386 # (Function for test scripts)
387 prodstub_disarm_type() {
388         __log_conf_start $@
389         if [ $# -ne 3 ]; then
390                 __print_err "<response-code> <producer-id> <type-id>" $@
391                 return 1
392         fi
393
394     curlString="curl -X DELETE -skw %{http_code} $PROD_STUB_SERVICE_PATH/arm/type/$2/$3"
395
396     __execute_curl_to_prodstub CONF $1 "$curlString"
397     return $?
398 }
399
400 # Prodstub API: Get job data for a job and compare with a target job json
401 # <response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>
402 # (Function for test scripts)
403 prodstub_check_jobdata() {
404         __log_test_start $@
405         if [ $# -ne 7 ]; then
406                 __print_err "<response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>" $@
407                 return 1
408         fi
409     if [ -f $7 ]; then
410         jobfile=$(cat $7)
411         jobfile=$(echo "$jobfile" | sed "s/XXXX/$3/g")
412     else
413         __log_test_fail_general "Template file "$7" for jobdata, does not exist"
414         return 1
415     fi
416     targetJson="{\"ei_job_identity\":\"$3\",\"ei_type_identity\":\"$4\",\"target_uri\":\"$5\",\"owner\":\"$6\", \"ei_job_data\":$jobfile}"
417     file="./tmp/.p.json"
418         echo "$targetJson" > $file
419
420     curlString="curl -X GET -skw %{http_code} $PROD_STUB_SERVICE_PATH/jobdata/$2/$3"
421
422     __execute_curl_to_prodstub TEST $1 "$curlString" $file
423     return $?
424 }
425
426 # Prodstub API: Get job data for a job and compare with a target job json
427 # <response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>
428 # (Function for test scripts)
429 prodstub_check_jobdata_2() {
430         __log_test_start $@
431         if [ $# -ne 7 ]; then
432                 __print_err "<response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>" $@
433                 return 1
434         fi
435     if [ -f $7 ]; then
436         jobfile=$(cat $7)
437         jobfile=$(echo "$jobfile" | sed "s/XXXX/$3/g")
438     else
439         __log_test_fail_general "Template file "$7" for jobdata, does not exist"
440         return 1
441     fi
442         if [[ "$ECS_FEATURE_LEVEL" == *"INFO-TYPES"* ]]; then
443                 targetJson="{\"info_job_identity\":\"$3\",\"info_type_identity\":\"$4\",\"target_uri\":\"$5\",\"owner\":\"$6\", \"info_job_data\":$jobfile,\"last_updated\":\"????\"}"
444         else
445                 targetJson="{\"ei_job_identity\":\"$3\",\"ei_type_identity\":\"$4\",\"target_uri\":\"$5\",\"owner\":\"$6\", \"ei_job_data\":$jobfile,\"last_updated\":\"????\"}"
446     fi
447         file="./tmp/.p.json"
448         echo "$targetJson" > $file
449
450     curlString="curl -X GET -skw %{http_code} $PROD_STUB_SERVICE_PATH/jobdata/$2/$3"
451
452     __execute_curl_to_prodstub TEST $1 "$curlString" $file
453     return $?
454 }
455
456 # Prodstub API: Get job data for a job and compare with a target job json (info-jobs)
457 # <response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>
458 # (Function for test scripts)
459 prodstub_check_jobdata_3() {
460         __log_test_start $@
461         if [ $# -ne 7 ]; then
462                 __print_err "<response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>" $@
463                 return 1
464         fi
465     if [ -f $7 ]; then
466         jobfile=$(cat $7)
467         jobfile=$(echo "$jobfile" | sed "s/XXXX/$3/g")
468     else
469         __log_test_fail_general "Template file "$7" for jobdata, does not exist"
470         return 1
471     fi
472     targetJson="{\"info_job_identity\":\"$3\",\"info_type_identity\":\"$4\",\"target_uri\":\"$5\",\"owner\":\"$6\", \"info_job_data\":$jobfile,\"last_updated\":\"????\"}"
473     file="./tmp/.p.json"
474         echo "$targetJson" > $file
475
476     curlString="curl -X GET -skw %{http_code} $PROD_STUB_SERVICE_PATH/jobdata/$2/$3"
477
478     __execute_curl_to_prodstub TEST $1 "$curlString" $file
479     return $?
480 }
481
482 # Prodstub API: Delete the job data
483 # <response-code> <producer-id> <job-id>
484 # (Function for test scripts)
485 prodstub_delete_jobdata() {
486         __log_conf_start
487         if [ $# -ne 3 ]; then
488                 __print_err "<response-code> <producer-id> <job-id> " $@
489                 return 1
490         fi
491     curlString="curl -X DELETE -skw %{http_code} $PROD_STUB_SERVICE_PATH/jobdata/$2/$3"
492
493     __execute_curl_to_prodstub CONF $1 "$curlString"
494     return $?
495 }
496
497 # Tests if a variable value in the prod stub is equal to a target value and and optional timeout.
498 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
499 # equal to the target or not.
500 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
501 # before setting pass or fail depending on if the variable value becomes equal to the target
502 # value or not.
503 # (Function for test scripts)
504 prodstub_equal() {
505         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
506                 __var_test "PRODSTUB" "$PROD_STUB_SERVICE_PATH/counter/" $1 "=" $2 $3
507         else
508                 __print_err "Wrong args to prodstub_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
509         fi
510 }