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