Merge "Update image tag"
[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 ## Access to Prod stub sim
23 # Direct access
24 PROD_STUB_HTTPX="http"
25 PROD_STUB_HOST_NAME=$LOCALHOST_NAME
26 PROD_STUB_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_HOST_NAME":"$PROD_STUB_EXTERNAL_PORT
27
28 #Docker/Kube internal path
29 if [ $RUNMODE == "KUBE" ]; then
30         PROD_STUB_SERVICE_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_APP_NAME"."$KUBE_SIM_NAMESPACE":"$PROD_STUB_EXTERNAL_PORT
31 else
32         PROD_STUB_SERVICE_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_APP_NAME":"$PROD_STUB_INTERNAL_PORT
33 fi
34
35 # Set http as the protocol to use for all communication to the Producer stub
36 # args: -
37 # (Function for test scripts)
38 use_prod_stub_http() {
39         echo -e $BOLD"Producer stub protocol setting"$EBOLD
40         echo -e " Using $BOLD http $EBOLD towards Producer stub"
41
42         PROD_STUB_HTTPX="http"
43     PROD_STUB_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_HOST_NAME":"$PROD_STUB_EXTERNAL_PORT
44
45         if [ $RUNMODE == "KUBE" ]; then
46                 PROD_STUB_SERVICE_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_APP_NAME"."$KUBE_SIM_NAMESPACE":"$PROD_STUB_EXTERNAL_PORT
47         else
48                 PROD_STUB_SERVICE_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_APP_NAME":"$PROD_STUB_INTERNAL_PORT
49         fi
50
51         echo ""
52 }
53
54 # Set https as the protocol to use for all communication to the Producer stub
55 # args: -
56 # (Function for test scripts)
57 use_prod_stub_https() {
58         echo -e $BOLD"Producer stub protocol setting"$EBOLD
59         echo -e " Using $BOLD https $EBOLD towards Producer stub"
60
61         PROD_STUB_HTTPX="https"
62     PROD_STUB_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_HOST_NAME":"$PROD_STUB_EXTERNAL_SECURE_PORT
63
64         if [ $RUNMODE == "KUBE" ]; then
65                 PROD_STUB_SERVICE_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_APP_NAME"."$KUBE_SIM_NAMESPACE":"$PROD_STUB_EXTERNAL_SECURE_PORT
66         else
67                 PROD_STUB_SERVICE_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_APP_NAME":"$PROD_STUB_INTERNAL_SECURE_PORT
68         fi
69         echo ""
70 }
71
72 ### Admin API functions producer stub
73
74 ###########################
75 ### Producer stub functions
76 ###########################
77
78 # Start the Producer stub in the simulator group
79 # args: -
80 # (Function for test scripts)
81 start_prod_stub() {
82
83         echo -e $BOLD"Starting $PROD_STUB_DISPLAY_NAME"$EBOLD
84
85         if [ $RUNMODE == "KUBE" ]; then
86
87                 # Check if app shall be fully managed by the test script
88                 __check_included_image "PRODSTUB"
89                 retcode_i=$?
90
91                 # Check if app shall only be used by the testscipt
92                 __check_prestarted_image "PRODSTUB"
93                 retcode_p=$?
94
95                 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
96                         echo -e $RED"The $ECS_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
97                         echo -e $RED"The $ECS_APP_NAME will not be started"$ERED
98                         exit
99                 fi
100                 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
101                         echo -e $RED"The $ECS_APP_NAME app is included both as managed and prestarted in this test script"$ERED
102                         echo -e $RED"The $ECS_APP_NAME will not be started"$ERED
103                         exit
104                 fi
105
106                 if [ $retcode_p -eq 0 ]; then
107                         echo -e " Using existing $PROD_STUB_APP_NAME deployment and service"
108                         echo " Setting RC replicas=1"
109                         __kube_scale deployment $PROD_STUB_APP_NAME $KUBE_SIM_NAMESPACE 1
110                 fi
111
112                 if [ $retcode_i -eq 0 ]; then
113                         echo -e " Creating $PROD_STUB_APP_NAME deployment and service"
114                         export PROD_STUB_APP_NAME
115                         export KUBE_SIM_NAMESPACE
116                         export PROD_STUB_IMAGE
117                         export PROD_STUB_INTERNAL_PORT
118                         export PROD_STUB_INTERNAL_SECURE_PORT
119                         export PROD_STUB_EXTERNAL_PORT
120                         export PROD_STUB_EXTERNAL_SECURE_PORT
121
122             __kube_create_namespace $KUBE_SIM_NAMESPACE
123
124                         # Create service
125                         input_yaml=$SIM_GROUP"/"$PROD_STUB_COMPOSE_DIR"/"svc.yaml
126                         output_yaml=$PWD/tmp/prodstub_svc.yaml
127                         __kube_create_instance service $PROD_STUB_APP_NAME $input_yaml $output_yaml
128
129                         # Create app
130                         input_yaml=$SIM_GROUP"/"$PROD_STUB_COMPOSE_DIR"/"app.yaml
131                         output_yaml=$PWD/tmp/prodstub_app.yaml
132                         __kube_create_instance app $PROD_STUB_APP_NAME $input_yaml $output_yaml
133                 fi
134
135                 PROD_STUB_HOST_NAME=$(__kube_get_service_host $PROD_STUB_APP_NAME $KUBE_SIM_NAMESPACE)
136
137                 PROD_STUB_EXTERNAL_PORT=$(__kube_get_service_port $PROD_STUB_APP_NAME $KUBE_SIM_NAMESPACE "http")
138                 PROD_STUB_EXTERNAL_SECURE_PORT=$(__kube_get_service_port $PROD_STUB_APP_NAME $KUBE_SIM_NAMESPACE "https")
139
140                 echo " Host IP, http port, https port: $PROD_STUB_HOST_NAME $PROD_STUB_EXTERNAL_PORT $PROD_STUB_EXTERNAL_SECURE_PORT"
141                 if [ $PROD_STUB_HTTPX == "http" ]; then
142             PROD_STUB_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_HOST_NAME":"$PROD_STUB_EXTERNAL_PORT
143                         PROD_STUB_SERVICE_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_APP_NAME"."$KUBE_SIM_NAMESPACE":"$PROD_STUB_EXTERNAL_PORT
144                 else
145             PROD_STUB_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_HOST_NAME":"$PROD_STUB_EXTERNAL_SECURE_PORT
146                         PROD_STUB_SERVICE_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_APP_NAME"."$KUBE_SIM_NAMESPACE":"$PROD_STUB_EXTERNAL_SECURE_PORT
147                 fi
148
149                 __check_service_start $PROD_STUB_APP_NAME $PROD_STUB_PATH$PROD_STUB_ALIVE_URL
150
151                 echo -ne " Service $PROD_STUB_APP_NAME - reset  "$SAMELINE
152                 result=$(__do_curl $PROD_STUB_PATH/reset)
153                 if [ $? -ne 0 ]; then
154                         echo -e " Service $PROD_STUB_APP_NAME - reset  $RED Failed $ERED - will continue"
155                 else
156                         echo -e " Service $PROD_STUB_APP_NAME - reset  $GREEN OK $EGREEN"
157                 fi
158         else
159
160                 # Check if docker app shall be fully managed by the test script
161                 __check_included_image 'PRODSTUB'
162                 if [ $? -eq 1 ]; then
163                         echo -e $RED"The Producer stub app is not included as managed in this test script"$ERED
164                         echo -e $RED"The Producer stub will not be started"$ERED
165                         exit
166                 fi
167
168         export PROD_STUB_APP_NAME
169         export PROD_STUB_APP_NAME_ALIAS
170         export PROD_STUB_INTERNAL_PORT
171         export PROD_STUB_EXTERNAL_PORT
172         export PROD_STUB_INTERNAL_SECURE_PORT
173         export PROD_STUB_EXTERNAL_SECURE_PORT
174         export DOCKER_SIM_NWNAME
175
176                 __start_container $PROD_STUB_COMPOSE_DIR NODOCKERARGS 1 $PROD_STUB_APP_NAME
177
178         __check_service_start $PROD_STUB_APP_NAME $PROD_STUB_PATH$PROD_STUB_ALIVE_URL
179         fi
180     echo ""
181     return 0
182 }
183
184 # Excute a curl cmd towards the prodstub simulator and check the response code.
185 # args: TEST|CONF <expected-response-code> <curl-cmd-string> [<json-file-to-compare-output>]
186 __execute_curl_to_prodstub() {
187     TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
188     echo "(${BASH_LINENO[0]}) - ${TIMESTAMP}: ${FUNCNAME[0]}" $@ >> $HTTPLOG
189         echo " CMD: $3" >> $HTTPLOG
190         res="$($3)"
191         echo " RESP: $res" >> $HTTPLOG
192         retcode=$?
193     if [ $retcode -ne 0 ]; then
194         __log_conf_fail_general " Fatal error when executing curl, response: "$retcode
195         return 1
196     fi
197     status=${res:${#res}-3}
198     if [ $status -eq $2 ]; then
199         if [ $# -eq 4 ]; then
200             body=${res:0:${#res}-3}
201             jobfile=$(cat $4)
202             echo " TARGET JSON: $jobfile" >> $HTTPLOG
203                     res=$(python3 ../common/compare_json.py "$jobfile" "$body")
204             if [ $res -ne 0 ]; then
205                 if [ $1 == "TEST" ]; then
206                     __log_test_fail_body
207                  else
208                     __log_conf_fail_body
209                 fi
210                 return 1
211             fi
212         fi
213         if [ $1 == "TEST" ]; then
214             __log_test_pass
215         else
216             __log_conf_ok
217         fi
218         return 0
219     fi
220     if [ $1 == "TEST" ]; then
221         __log_test_fail_status_code $2 $status
222         else
223         __log_conf_fail_status_code $2 $status
224     fi
225     return 1
226 }
227
228 # Prodstub API: Set (or reset) response code for producer supervision
229 # <response-code> <producer-id> [<forced_response_code>]
230 # (Function for test scripts)
231 prodstub_arm_producer() {
232         __log_conf_start $@
233         if [ $# -ne 2 ] && [ $# -ne 3 ]; then
234                 __print_err "<response-code> <producer-id> [<forced_response_code>]" $@
235                 return 1
236         fi
237
238     curlString="curl -X PUT -skw %{http_code} $PROD_STUB_PATH/arm/supervision/"$2
239         if [ $# -eq 3 ]; then
240                 curlString=$curlString"?response="$3
241         fi
242
243     __execute_curl_to_prodstub CONF $1 "$curlString"
244     return $?
245 }
246
247 # Prodstub API: Set (or reset) response code job create
248 # <response-code> <producer-id> <job-id> [<forced_response_code>]
249 # (Function for test scripts)
250 prodstub_arm_job_create() {
251         __log_conf_start $@
252         if [ $# -ne 3 ] && [ $# -ne 4 ]; then
253                 __print_err "<response-code> <producer-id> <job-id> [<forced_response_code>]" $@
254                 return 1
255         fi
256
257     curlString="curl -X PUT -skw %{http_code} $PROD_STUB_PATH/arm/create/$2/$3"
258         if [ $# -eq 4 ]; then
259                 curlString=$curlString"?response="$4
260         fi
261
262     __execute_curl_to_prodstub CONF $1 "$curlString"
263     return $?
264 }
265
266 # Prodstub API: Set (or reset) response code job delete
267 # <response-code> <producer-id> <job-id> [<forced_response_code>]
268 # (Function for test scripts)
269 prodstub_arm_job_delete() {
270         __log_conf_start $@
271         if [ $# -ne 3 ] && [ $# -ne 4 ]; then
272                 __print_err "<response-code> <producer-id> <job-id> [<forced_response_code>]" $@
273                 return 1
274         fi
275
276     curlString="curl -X PUT -skw %{http_code} $PROD_STUB_PATH/arm/delete/$2/$3"
277         if [ $# -eq 4 ]; then
278                 curlString=$curlString"?response="$4
279         fi
280
281     __execute_curl_to_prodstub CONF $1 "$curlString"
282     return $?
283 }
284
285 # Prodstub API: Arm a type of a producer
286 # <response-code> <producer-id> <type-id>
287 # (Function for test scripts)
288 prodstub_arm_type() {
289         __log_conf_start $@
290         if [ $# -ne 3 ]; then
291                 __print_err "<response-code> <producer-id> <type-id>" $@
292                 return 1
293         fi
294
295     curlString="curl -X PUT -skw %{http_code} $PROD_STUB_PATH/arm/type/$2/$3"
296
297     __execute_curl_to_prodstub CONF $1 "$curlString"
298     return $?
299 }
300
301 # Prodstub API: Disarm a type in a producer
302 # <response-code> <producer-id> <type-id>
303 # (Function for test scripts)
304 prodstub_disarm_type() {
305         __log_conf_start $@
306         if [ $# -ne 3 ]; then
307                 __print_err "<response-code> <producer-id> <type-id>" $@
308                 return 1
309         fi
310
311     curlString="curl -X DELETE -skw %{http_code} $PROD_STUB_PATH/arm/type/$2/$3"
312
313     __execute_curl_to_prodstub CONF $1 "$curlString"
314     return $?
315 }
316
317 # Prodstub API: Get job data for a job and compare with a target job json
318 # <response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>
319 # (Function for test scripts)
320 prodstub_check_jobdata() {
321         __log_test_start $@
322         if [ $# -ne 7 ]; then
323                 __print_err "<response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>" $@
324                 return 1
325         fi
326     if [ -f $7 ]; then
327         jobfile=$(cat $7)
328         jobfile=$(echo "$jobfile" | sed "s/XXXX/$3/g")
329     else
330         _log_test_fail_general "Template file "$7" for jobdata, does not exist"
331         return 1
332     fi
333     targetJson="{\"ei_job_identity\":\"$3\",\"ei_type_identity\":\"$4\",\"target_uri\":\"$5\",\"owner\":\"$6\", \"ei_job_data\":$jobfile}"
334     file="./tmp/.p.json"
335         echo "$targetJson" > $file
336
337     curlString="curl -X GET -skw %{http_code} $PROD_STUB_PATH/jobdata/$2/$3"
338
339     __execute_curl_to_prodstub TEST $1 "$curlString" $file
340     return $?
341 }
342
343 # Prodstub API: Delete the job data
344 # <response-code> <producer-id> <job-id>
345 # (Function for test scripts)
346 prodstub_delete_jobdata() {
347         __log_conf_start
348         if [ $# -ne 3 ]; then
349                 __print_err "<response-code> <producer-id> <job-id> " $@
350                 return 1
351         fi
352     curlString="curl -X DELETE -skw %{http_code} $PROD_STUB_PATH/jobdata/$2/$3"
353
354     __execute_curl_to_prodstub CONF $1 "$curlString"
355     return $?
356 }
357
358 # Tests if a variable value in the prod stub is equal to a target value and and optional timeout.
359 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
360 # equal to the target or not.
361 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
362 # before setting pass or fail depending on if the variable value becomes equal to the target
363 # value or not.
364 # (Function for test scripts)
365 prodstub_equal() {
366         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
367                 __var_test "PRODSTUB" "$PROD_STUB_PATH/counter/" $1 "=" $2 $3
368         else
369                 __print_err "Wrong args to prodstub_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
370         fi
371 }