4d88659b02d27e1fd805c316237c701acb799dea
[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 remove 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         else
51                 echo -e $RED" Build Failed"$ERED
52                 ((RES_CONF_FAIL++))
53                 cat .dockererr
54                 echo -e $RED"Exiting...."$ERED
55                 exit 1
56         fi
57 }
58
59 # Generate a string for each included image using the app display name and a docker images format string
60 # arg: <docker-images-format-string> <file-to-append>
61 __PRODSTUB_image_data() {
62         echo -e "$PROD_STUB_DISPLAY_NAME\t$(docker images --format $1 $PROD_STUB_IMAGE)" >>   $2
63 }
64
65 # Scale kubernetes resources to zero
66 # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
67 # This function is called for apps fully managed by the test script
68 __PRODSTUB_kube_scale_zero() {
69         __kube_scale_all_resources $KUBE_SIM_NAMESPACE autotest PRODSTUB
70 }
71
72 # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
73 # This function is called for prestarted apps not managed by the test script.
74 __PRODSTUB_kube_scale_zero_and_wait() {
75         echo -e $RED" PRODSTUB app is not scaled in this state"$ERED
76 }
77
78 # Delete all kube resouces for the app
79 # This function is called for apps managed by the test script.
80 __PRODSTUB_kube_delete_all() {
81         __kube_delete_all_resources $KUBE_SIM_NAMESPACE autotest PRODSTUB
82 }
83
84 # Store docker logs
85 # This function is called for apps managed by the test script.
86 # args: <log-dir> <file-prexix>
87 __PRODSTUB_store_docker_logs() {
88         docker logs $PROD_STUB_APP_NAME > $1$2_prodstub.log 2>&1
89 }
90 #######################################################
91
92
93 ## Access to Prod stub sim
94 # Direct access
95 PROD_STUB_HTTPX="http"
96 PROD_STUB_HOST_NAME=$LOCALHOST_NAME
97 PROD_STUB_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_HOST_NAME":"$PROD_STUB_EXTERNAL_PORT
98
99 #Docker/Kube internal path
100 if [ $RUNMODE == "KUBE" ]; then
101         PROD_STUB_SERVICE_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_APP_NAME"."$KUBE_SIM_NAMESPACE":"$PROD_STUB_EXTERNAL_PORT
102 else
103         PROD_STUB_SERVICE_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_APP_NAME":"$PROD_STUB_INTERNAL_PORT
104 fi
105
106 # Set http as the protocol to use for all communication to the Producer stub
107 # args: -
108 # (Function for test scripts)
109 use_prod_stub_http() {
110         echo -e $BOLD"Producer stub protocol setting"$EBOLD
111         echo -e " Using $BOLD http $EBOLD towards Producer stub"
112
113         PROD_STUB_HTTPX="http"
114     PROD_STUB_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_HOST_NAME":"$PROD_STUB_EXTERNAL_PORT
115
116         if [ $RUNMODE == "KUBE" ]; then
117                 PROD_STUB_SERVICE_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_APP_NAME"."$KUBE_SIM_NAMESPACE":"$PROD_STUB_EXTERNAL_PORT
118         else
119                 PROD_STUB_SERVICE_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_APP_NAME":"$PROD_STUB_INTERNAL_PORT
120         fi
121
122         echo ""
123 }
124
125 # Set https as the protocol to use for all communication to the Producer stub
126 # args: -
127 # (Function for test scripts)
128 use_prod_stub_https() {
129         echo -e $BOLD"Producer stub protocol setting"$EBOLD
130         echo -e " Using $BOLD https $EBOLD towards Producer stub"
131
132         PROD_STUB_HTTPX="https"
133     PROD_STUB_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_HOST_NAME":"$PROD_STUB_EXTERNAL_SECURE_PORT
134
135         if [ $RUNMODE == "KUBE" ]; then
136                 PROD_STUB_SERVICE_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_APP_NAME"."$KUBE_SIM_NAMESPACE":"$PROD_STUB_EXTERNAL_SECURE_PORT
137         else
138                 PROD_STUB_SERVICE_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_APP_NAME":"$PROD_STUB_INTERNAL_SECURE_PORT
139         fi
140         echo ""
141 }
142
143 ### Admin API functions producer stub
144
145 ###########################
146 ### Producer stub functions
147 ###########################
148
149 # Start the Producer stub in the simulator group
150 # args: -
151 # (Function for test scripts)
152 start_prod_stub() {
153
154         echo -e $BOLD"Starting $PROD_STUB_DISPLAY_NAME"$EBOLD
155
156         if [ $RUNMODE == "KUBE" ]; then
157
158                 # Check if app shall be fully managed by the test script
159                 __check_included_image "PRODSTUB"
160                 retcode_i=$?
161
162                 # Check if app shall only be used by the testscipt
163                 __check_prestarted_image "PRODSTUB"
164                 retcode_p=$?
165
166                 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
167                         echo -e $RED"The $ECS_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
168                         echo -e $RED"The $ECS_APP_NAME will not be started"$ERED
169                         exit
170                 fi
171                 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
172                         echo -e $RED"The $ECS_APP_NAME app is included both as managed and prestarted in this test script"$ERED
173                         echo -e $RED"The $ECS_APP_NAME will not be started"$ERED
174                         exit
175                 fi
176
177                 if [ $retcode_p -eq 0 ]; then
178                         echo -e " Using existing $PROD_STUB_APP_NAME deployment and service"
179                         echo " Setting RC replicas=1"
180                         __kube_scale deployment $PROD_STUB_APP_NAME $KUBE_SIM_NAMESPACE 1
181                 fi
182
183                 if [ $retcode_i -eq 0 ]; then
184                         echo -e " Creating $PROD_STUB_APP_NAME deployment and service"
185                         export PROD_STUB_APP_NAME
186                         export KUBE_SIM_NAMESPACE
187                         export PROD_STUB_IMAGE
188                         export PROD_STUB_INTERNAL_PORT
189                         export PROD_STUB_INTERNAL_SECURE_PORT
190                         export PROD_STUB_EXTERNAL_PORT
191                         export PROD_STUB_EXTERNAL_SECURE_PORT
192
193             __kube_create_namespace $KUBE_SIM_NAMESPACE
194
195                         # Create service
196                         input_yaml=$SIM_GROUP"/"$PROD_STUB_COMPOSE_DIR"/"svc.yaml
197                         output_yaml=$PWD/tmp/prodstub_svc.yaml
198                         __kube_create_instance service $PROD_STUB_APP_NAME $input_yaml $output_yaml
199
200                         # Create app
201                         input_yaml=$SIM_GROUP"/"$PROD_STUB_COMPOSE_DIR"/"app.yaml
202                         output_yaml=$PWD/tmp/prodstub_app.yaml
203                         __kube_create_instance app $PROD_STUB_APP_NAME $input_yaml $output_yaml
204                 fi
205
206                 PROD_STUB_HOST_NAME=$(__kube_get_service_host $PROD_STUB_APP_NAME $KUBE_SIM_NAMESPACE)
207
208                 PROD_STUB_EXTERNAL_PORT=$(__kube_get_service_port $PROD_STUB_APP_NAME $KUBE_SIM_NAMESPACE "http")
209                 PROD_STUB_EXTERNAL_SECURE_PORT=$(__kube_get_service_port $PROD_STUB_APP_NAME $KUBE_SIM_NAMESPACE "https")
210
211                 echo " Host IP, http port, https port: $PROD_STUB_HOST_NAME $PROD_STUB_EXTERNAL_PORT $PROD_STUB_EXTERNAL_SECURE_PORT"
212                 if [ $PROD_STUB_HTTPX == "http" ]; then
213             PROD_STUB_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_HOST_NAME":"$PROD_STUB_EXTERNAL_PORT
214                         PROD_STUB_SERVICE_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_APP_NAME"."$KUBE_SIM_NAMESPACE":"$PROD_STUB_EXTERNAL_PORT
215                 else
216             PROD_STUB_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_HOST_NAME":"$PROD_STUB_EXTERNAL_SECURE_PORT
217                         PROD_STUB_SERVICE_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_APP_NAME"."$KUBE_SIM_NAMESPACE":"$PROD_STUB_EXTERNAL_SECURE_PORT
218                 fi
219
220                 __check_service_start $PROD_STUB_APP_NAME $PROD_STUB_PATH$PROD_STUB_ALIVE_URL
221
222                 echo -ne " Service $PROD_STUB_APP_NAME - reset  "$SAMELINE
223                 result=$(__do_curl $PROD_STUB_PATH/reset)
224                 if [ $? -ne 0 ]; then
225                         echo -e " Service $PROD_STUB_APP_NAME - reset  $RED Failed $ERED - will continue"
226                 else
227                         echo -e " Service $PROD_STUB_APP_NAME - reset  $GREEN OK $EGREEN"
228                 fi
229         else
230
231                 # Check if docker app shall be fully managed by the test script
232                 __check_included_image 'PRODSTUB'
233                 if [ $? -eq 1 ]; then
234                         echo -e $RED"The Producer stub app is not included as managed in this test script"$ERED
235                         echo -e $RED"The Producer stub will not be started"$ERED
236                         exit
237                 fi
238
239         export PROD_STUB_APP_NAME
240         export PROD_STUB_APP_NAME_ALIAS
241         export PROD_STUB_INTERNAL_PORT
242         export PROD_STUB_EXTERNAL_PORT
243         export PROD_STUB_INTERNAL_SECURE_PORT
244         export PROD_STUB_EXTERNAL_SECURE_PORT
245         export DOCKER_SIM_NWNAME
246
247                 export PROD_STUB_DISPLAY_NAME
248
249                 __start_container $PROD_STUB_COMPOSE_DIR "" NODOCKERARGS 1 $PROD_STUB_APP_NAME
250
251         __check_service_start $PROD_STUB_APP_NAME $PROD_STUB_PATH$PROD_STUB_ALIVE_URL
252         fi
253     echo ""
254     return 0
255 }
256
257 # Excute a curl cmd towards the prodstub simulator and check the response code.
258 # args: TEST|CONF <expected-response-code> <curl-cmd-string> [<json-file-to-compare-output>]
259 __execute_curl_to_prodstub() {
260     TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
261     echo "(${BASH_LINENO[0]}) - ${TIMESTAMP}: ${FUNCNAME[0]}" $@ >> $HTTPLOG
262         proxyflag=""
263         if [ $RUNMODE == "KUBE" ]; then
264                 if [ ! -z "$CLUSTER_KUBE_PROXY_NODEPORT" ]; then
265                         proxyflag=" --proxy http://localhost:$CLUSTER_KUBE_PROXY_NODEPORT"
266                 fi
267         fi
268         echo " CMD: $3 $proxyflag" >> $HTTPLOG
269         res="$($3 $proxyflag)"
270         echo " RESP: $res" >> $HTTPLOG
271         retcode=$?
272     if [ $retcode -ne 0 ]; then
273         __log_conf_fail_general " Fatal error when executing curl, response: "$retcode
274         return 1
275     fi
276     status=${res:${#res}-3}
277     if [ $status -eq $2 ]; then
278         if [ $# -eq 4 ]; then
279             body=${res:0:${#res}-3}
280             jobfile=$(cat $4)
281             echo " TARGET JSON: $jobfile" >> $HTTPLOG
282                     res=$(python3 ../common/compare_json.py "$jobfile" "$body")
283             if [ $res -ne 0 ]; then
284                 if [ $1 == "TEST" ]; then
285                     __log_test_fail_body
286                  else
287                     __log_conf_fail_body
288                 fi
289                 return 1
290             fi
291         fi
292         if [ $1 == "TEST" ]; then
293             __log_test_pass
294         else
295             __log_conf_ok
296         fi
297         return 0
298     fi
299     if [ $1 == "TEST" ]; then
300         __log_test_fail_status_code $2 $status
301         else
302         __log_conf_fail_status_code $2 $status
303     fi
304     return 1
305 }
306
307 # Prodstub API: Set (or reset) response code for producer supervision
308 # <response-code> <producer-id> [<forced_response_code>]
309 # (Function for test scripts)
310 prodstub_arm_producer() {
311         __log_conf_start $@
312         if [ $# -ne 2 ] && [ $# -ne 3 ]; then
313                 __print_err "<response-code> <producer-id> [<forced_response_code>]" $@
314                 return 1
315         fi
316
317     curlString="curl -X PUT -skw %{http_code} $PROD_STUB_PATH/arm/supervision/"$2
318         if [ $# -eq 3 ]; then
319                 curlString=$curlString"?response="$3
320         fi
321
322     __execute_curl_to_prodstub CONF $1 "$curlString"
323     return $?
324 }
325
326 # Prodstub API: Set (or reset) response code job create
327 # <response-code> <producer-id> <job-id> [<forced_response_code>]
328 # (Function for test scripts)
329 prodstub_arm_job_create() {
330         __log_conf_start $@
331         if [ $# -ne 3 ] && [ $# -ne 4 ]; then
332                 __print_err "<response-code> <producer-id> <job-id> [<forced_response_code>]" $@
333                 return 1
334         fi
335
336     curlString="curl -X PUT -skw %{http_code} $PROD_STUB_PATH/arm/create/$2/$3"
337         if [ $# -eq 4 ]; then
338                 curlString=$curlString"?response="$4
339         fi
340
341     __execute_curl_to_prodstub CONF $1 "$curlString"
342     return $?
343 }
344
345 # Prodstub API: Set (or reset) response code job delete
346 # <response-code> <producer-id> <job-id> [<forced_response_code>]
347 # (Function for test scripts)
348 prodstub_arm_job_delete() {
349         __log_conf_start $@
350         if [ $# -ne 3 ] && [ $# -ne 4 ]; then
351                 __print_err "<response-code> <producer-id> <job-id> [<forced_response_code>]" $@
352                 return 1
353         fi
354
355     curlString="curl -X PUT -skw %{http_code} $PROD_STUB_PATH/arm/delete/$2/$3"
356         if [ $# -eq 4 ]; then
357                 curlString=$curlString"?response="$4
358         fi
359
360     __execute_curl_to_prodstub CONF $1 "$curlString"
361     return $?
362 }
363
364 # Prodstub API: Arm a type of a producer
365 # <response-code> <producer-id> <type-id>
366 # (Function for test scripts)
367 prodstub_arm_type() {
368         __log_conf_start $@
369         if [ $# -ne 3 ]; then
370                 __print_err "<response-code> <producer-id> <type-id>" $@
371                 return 1
372         fi
373
374     curlString="curl -X PUT -skw %{http_code} $PROD_STUB_PATH/arm/type/$2/$3"
375
376     __execute_curl_to_prodstub CONF $1 "$curlString"
377     return $?
378 }
379
380 # Prodstub API: Disarm a type in a producer
381 # <response-code> <producer-id> <type-id>
382 # (Function for test scripts)
383 prodstub_disarm_type() {
384         __log_conf_start $@
385         if [ $# -ne 3 ]; then
386                 __print_err "<response-code> <producer-id> <type-id>" $@
387                 return 1
388         fi
389
390     curlString="curl -X DELETE -skw %{http_code} $PROD_STUB_PATH/arm/type/$2/$3"
391
392     __execute_curl_to_prodstub CONF $1 "$curlString"
393     return $?
394 }
395
396 # Prodstub API: Get job data for a job and compare with a target job json
397 # <response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>
398 # (Function for test scripts)
399 prodstub_check_jobdata() {
400         __log_test_start $@
401         if [ $# -ne 7 ]; then
402                 __print_err "<response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>" $@
403                 return 1
404         fi
405     if [ -f $7 ]; then
406         jobfile=$(cat $7)
407         jobfile=$(echo "$jobfile" | sed "s/XXXX/$3/g")
408     else
409         _log_test_fail_general "Template file "$7" for jobdata, does not exist"
410         return 1
411     fi
412     targetJson="{\"ei_job_identity\":\"$3\",\"ei_type_identity\":\"$4\",\"target_uri\":\"$5\",\"owner\":\"$6\", \"ei_job_data\":$jobfile}"
413     file="./tmp/.p.json"
414         echo "$targetJson" > $file
415
416     curlString="curl -X GET -skw %{http_code} $PROD_STUB_PATH/jobdata/$2/$3"
417
418     __execute_curl_to_prodstub TEST $1 "$curlString" $file
419     return $?
420 }
421
422 # Prodstub API: Get job data for a job and compare with a target job json
423 # <response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>
424 # (Function for test scripts)
425 prodstub_check_jobdata_2() {
426         __log_test_start $@
427         if [ $# -ne 7 ]; then
428                 __print_err "<response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>" $@
429                 return 1
430         fi
431     if [ -f $7 ]; then
432         jobfile=$(cat $7)
433         jobfile=$(echo "$jobfile" | sed "s/XXXX/$3/g")
434     else
435         _log_test_fail_general "Template file "$7" for jobdata, does not exist"
436         return 1
437     fi
438     targetJson="{\"ei_job_identity\":\"$3\",\"ei_type_identity\":\"$4\",\"target_uri\":\"$5\",\"owner\":\"$6\", \"ei_job_data\":$jobfile,\"last_updated\":\"????\"}"
439     file="./tmp/.p.json"
440         echo "$targetJson" > $file
441
442     curlString="curl -X GET -skw %{http_code} $PROD_STUB_PATH/jobdata/$2/$3"
443
444     __execute_curl_to_prodstub TEST $1 "$curlString" $file
445     return $?
446 }
447
448 # Prodstub API: Delete the job data
449 # <response-code> <producer-id> <job-id>
450 # (Function for test scripts)
451 prodstub_delete_jobdata() {
452         __log_conf_start
453         if [ $# -ne 3 ]; then
454                 __print_err "<response-code> <producer-id> <job-id> " $@
455                 return 1
456         fi
457     curlString="curl -X DELETE -skw %{http_code} $PROD_STUB_PATH/jobdata/$2/$3"
458
459     __execute_curl_to_prodstub CONF $1 "$curlString"
460     return $?
461 }
462
463 # Tests if a variable value in the prod stub is equal to a target value and and optional timeout.
464 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
465 # equal to the target or not.
466 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
467 # before setting pass or fail depending on if the variable value becomes equal to the target
468 # value or not.
469 # (Function for test scripts)
470 prodstub_equal() {
471         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
472                 __var_test "PRODSTUB" "$PROD_STUB_PATH/counter/" $1 "=" $2 $3
473         else
474                 __print_err "Wrong args to prodstub_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
475         fi
476 }