Merge "NONRTRIC - PMS Persistent storage of policies and type definitions"
[nonrtric.git] / test / common / cr_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 managemnt functions test functions for the Callback Reciver
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 __CR_imagesetup() {
29         __check_and_create_image_var CR "CR_IMAGE" "CR_IMAGE_BASE" "CR_IMAGE_TAG" LOCAL "$CR_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 __CR_imagepull() {
38         echo -e $RED" Image for app CR 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 __CR_imagebuild() {
45         cd ../cr
46         echo " Building CR - $CR_DISPLAY_NAME - image: $CR_IMAGE"
47         docker build  --build-arg NEXUS_PROXY_REPO=$NEXUS_PROXY_REPO -t $CR_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 __CR_image_data() {
62         echo -e "$CR_DISPLAY_NAME\t$(docker images --format $1 $CR_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 __CR_kube_scale_zero() {
69         __kube_scale_all_resources $KUBE_SIM_NAMESPACE autotest CR
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 __CR_kube_scale_zero_and_wait() {
75         echo -e $RED" CR 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 __CR_kube_delete_all() {
81         __kube_delete_all_resources $KUBE_SIM_NAMESPACE autotest CR
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 __CR_store_docker_logs() {
88         docker logs $CR_APP_NAME > $1$2_cr.log 2>&1
89 }
90
91 #######################################################
92
93
94 ## Access to Callback Receiver
95 # Host name may be changed if app started by kube
96 # Direct access from script
97 CR_HTTPX="http"
98 CR_HOST_NAME=$LOCALHOST_NAME
99 CR_PATH=$CR_HTTPX"://"$CR_HOST_NAME":"$CR_EXTERNAL_PORT
100 #Docker/Kube internal path
101 if [ $RUNMODE == "KUBE" ]; then
102         CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME"."$KUBE_SIM_NAMESPACE":"$CR_EXTERNAL_PORT$CR_APP_CALLBACK
103 else
104         CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME":"$CR_INTERNAL_PORT$CR_APP_CALLBACK
105 fi
106 # CR_ADAPTER used for switching between REST and DMAAP (only REST supported currently)
107 CR_ADAPTER_TYPE="REST"
108 CR_ADAPTER=$CR_PATH
109
110 ################
111 ### CR functions
112 ################
113
114 # Set http as the protocol to use for all communication to the Callback Receiver
115 # args: -
116 # (Function for test scripts)
117 use_cr_http() {
118         echo -e $BOLD"CR protocol setting"$EBOLD
119         echo -e " Using $BOLD http $EBOLD towards CR"
120
121         CR_HTTPX="http"
122         CR_PATH=$CR_HTTPX"://"$CR_HOST_NAME":"$CR_EXTERNAL_PORT
123
124         #Docker/Kube internal path
125         if [ $RUNMODE == "KUBE" ]; then
126                 CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME"."$KUBE_SIM_NAMESPACE":"$CR_EXTERNAL_PORT$CR_APP_CALLBACK
127         else
128                 CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME":"$CR_INTERNAL_PORT$CR_APP_CALLBACK
129         fi
130         CR_ADAPTER_TYPE="REST"
131         CR_ADAPTER=$CR_PATH
132         echo ""
133 }
134
135 # Set https as the protocol to use for all communication to the Callback Receiver
136 # args: -
137 # (Function for test scripts)
138 use_cr_https() {
139         echo -e $BOLD"CR protocol setting"$EBOLD
140         echo -e " Using $BOLD https $EBOLD towards CR"
141
142         CR_HTTPX="https"
143         CR_PATH=$CR_HTTPX"://"$CR_HOST_NAME":"$CR_EXTERNAL_SECURE_PORT
144
145         if [ $RUNMODE == "KUBE" ]; then
146                 CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME"."$KUBE_SIM_NAMESPACE":"$CR_EXTERNAL_SECURE_PORT$CR_APP_CALLBACK
147         else
148                 CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME":"$CR_INTERNAL_SECURE_PORT$CR_APP_CALLBACK
149         fi
150
151         CR_ADAPTER_TYPE="REST"
152         CR_ADAPTER=$CR_PATH
153         echo ""
154 }
155
156 # Start the Callback reciver in the simulator group
157 # args: -
158 # (Function for test scripts)
159 start_cr() {
160
161         echo -e $BOLD"Starting $CR_DISPLAY_NAME"$EBOLD
162
163         if [ $RUNMODE == "KUBE" ]; then
164
165                 # Check if app shall be fully managed by the test script
166                 __check_included_image "CR"
167                 retcode_i=$?
168
169                 # Check if app shall only be used by the testscipt
170                 __check_prestarted_image "CR"
171                 retcode_p=$?
172
173                 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
174                         echo -e $RED"The $CR_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
175                         echo -e $RED"The $CR_APP_NAME will not be started"$ERED
176                         exit
177                 fi
178                 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
179                         echo -e $RED"The $CR_APP_NAME app is included both as managed and prestarted in this test script"$ERED
180                         echo -e $RED"The $CR_APP_NAME will not be started"$ERED
181                         exit
182                 fi
183
184                 # Check if app shall be used - not managed - by the test script
185                 if [ $retcode_p -eq 0 ]; then
186                         echo -e " Using existing $CR_APP_NAME deployment and service"
187                         echo " Setting CR replicas=1"
188                         __kube_scale deployment $CR_APP_NAME $KUBE_SIM_NAMESPACE 1
189                 fi
190
191                 if [ $retcode_i -eq 0 ]; then
192                         echo -e " Creating $CR_APP_NAME deployment and service"
193                         export CR_APP_NAME
194                         export KUBE_SIM_NAMESPACE
195                         export CR_IMAGE
196                         export CR_INTERNAL_PORT
197                         export CR_INTERNAL_SECURE_PORT
198                         export CR_EXTERNAL_PORT
199                         export CR_EXTERNAL_SECURE_PORT
200
201                         __kube_create_namespace $KUBE_SIM_NAMESPACE
202
203                         # Create service
204                         input_yaml=$SIM_GROUP"/"$CR_COMPOSE_DIR"/"svc.yaml
205                         output_yaml=$PWD/tmp/cr_svc.yaml
206                         __kube_create_instance service $CR_APP_NAME $input_yaml $output_yaml
207
208                         # Create app
209                         input_yaml=$SIM_GROUP"/"$CR_COMPOSE_DIR"/"app.yaml
210                         output_yaml=$PWD/tmp/cr_app.yaml
211                         __kube_create_instance app $CR_APP_NAME $input_yaml $output_yaml
212
213                 fi
214
215                 echo " Retrieving host and ports for service..."
216                 CR_HOST_NAME=$(__kube_get_service_host $CR_APP_NAME $KUBE_SIM_NAMESPACE)
217
218                 CR_EXTERNAL_PORT=$(__kube_get_service_port $CR_APP_NAME $KUBE_SIM_NAMESPACE "http")
219                 CR_EXTERNAL_SECURE_PORT=$(__kube_get_service_port $CR_APP_NAME $KUBE_SIM_NAMESPACE "https")
220
221                 echo " Host IP, http port, https port: $CR_HOST_NAME $CR_EXTERNAL_PORT $CR_EXTERNAL_SECURE_PORT"
222                 if [ $CR_HTTPX == "http" ]; then
223                         CR_PATH=$CR_HTTPX"://"$CR_HOST_NAME":"$CR_EXTERNAL_PORT
224                         CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME"."$KUBE_SIM_NAMESPACE":"$CR_EXTERNAL_PORT$CR_APP_CALLBACK
225                 else
226                         CR_PATH=$CR_HTTPX"://"$CR_HOST_NAME":"$CR_EXTERNAL_SECURE_PORT
227                         CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME"."$KUBE_SIM_NAMESPACE":"$CR_EXTERNAL_SECURE_PORT$CR_APP_CALLBACK
228                 fi
229                 if [ $CR_ADAPTER_TYPE == "REST" ]; then
230                         CR_ADAPTER=$CR_PATH
231                 fi
232
233                 __check_service_start $CR_APP_NAME $CR_PATH$CR_ALIVE_URL
234
235                 echo -ne " Service $CR_APP_NAME - reset  "$SAMELINE
236                 result=$(__do_curl $CR_APP_NAME $CR_PATH/reset)
237                 if [ $? -ne 0 ]; then
238                         echo -e " Service $CR_APP_NAME - reset  $RED Failed $ERED - will continue"
239                 else
240                         echo -e " Service $CR_APP_NAME - reset  $GREEN OK $EGREEN"
241                 fi
242         else
243                 # Check if docker app shall be fully managed by the test script
244                 __check_included_image 'CR'
245                 if [ $? -eq 1 ]; then
246                         echo -e $RED"The Callback Receiver app is not included in this test script"$ERED
247                         echo -e $RED"The Callback Receiver will not be started"$ERED
248                         exit
249                 fi
250
251                 export CR_APP_NAME
252                 export CR_INTERNAL_PORT
253                 export CR_EXTERNAL_PORT
254                 export CR_INTERNAL_SECURE_PORT
255                 export CR_EXTERNAL_SECURE_PORT
256                 export DOCKER_SIM_NWNAME
257                 export CR_DISPLAY_NAME
258
259                 __start_container $CR_COMPOSE_DIR "" NODOCKERARGS 1 $CR_APP_NAME
260
261         __check_service_start $CR_APP_NAME $CR_PATH$CR_ALIVE_URL
262         fi
263         echo ""
264 }
265
266
267 # Tests if a variable value in the CR is equal to a target value and and optional timeout.
268 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
269 # equal to the target or not.
270 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
271 # before setting pass or fail depending on if the variable value becomes equal to the target
272 # value or not.
273 # (Function for test scripts)
274 cr_equal() {
275         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
276                 __var_test "CR" "$CR_PATH/counter/" $1 "=" $2 $3
277         else
278                 __print_err "Wrong args to cr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
279         fi
280 }
281
282 # CR API: Check the contents of all current ric sync events for one id from PMS
283 # <response-code> <id> [ EMPTY | ( <ric-id> )+ ]
284 # (Function for test scripts)
285 cr_api_check_all_sync_events() {
286         __log_test_start $@
287
288         if [ "$PMS_VERSION" != "V2" ]; then
289                 __log_test_fail_not_supported
290                 return 1
291         fi
292
293     if [ $# -lt 2 ]; then
294         __print_err "<response-code> <id> [ EMPTY | ( <ric-id> )+ ]" $@
295         return 1
296     fi
297
298         query="/get-all-events/"$2
299         res="$(__do_curl_to_api CR GET $query)"
300         status=${res:${#res}-3}
301
302         if [ $status -ne $1 ]; then
303                 __log_test_fail_status_code $1 $status
304                 return 1
305         fi
306
307         if [ $# -gt 2 ]; then
308                 body=${res:0:${#res}-3}
309                 if [ $# -eq 3 ] && [ $3 == "EMPTY" ]; then
310                         targetJson="["
311                 else
312                         targetJson="["
313                         arr=(${@:3})
314
315                         for ((i=0; i<$(($#-2)); i=i+1)); do
316
317                                 if [ "$targetJson" != "[" ]; then
318                                         targetJson=$targetJson","
319                                 fi
320                                 targetJson=$targetJson"{\"ric_id\":\"${arr[$i]}\",\"event_type\":\"AVAILABLE\"}"
321                         done
322                 fi
323
324                 targetJson=$targetJson"]"
325                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
326                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
327
328                 if [ $res -ne 0 ]; then
329                         __log_test_fail_body
330                         return 1
331                 fi
332         fi
333         __log_test_pass
334         return 0
335 }
336
337 # CR API: Check the contents of all current status events for one id from ECS
338 # <response-code> <id> [ EMPTY | ( <status> )+ ]
339 # (Function for test scripts)
340 cr_api_check_all_ecs_events() {
341         __log_test_start $@
342
343     if [ $# -lt 2 ]; then
344         __print_err "<response-code> <id> [ EMPTY | ( <status> )+ ]" $@
345         return 1
346     fi
347
348         query="/get-all-events/"$2
349         res="$(__do_curl_to_api CR GET $query)"
350         status=${res:${#res}-3}
351
352         if [ $status -ne $1 ]; then
353                 __log_test_fail_status_code $1 $status
354                 return 1
355         fi
356
357         if [ $# -gt 2 ]; then
358                 body=${res:0:${#res}-3}
359                 if [ $# -eq 3 ] && [ $3 == "EMPTY" ]; then
360                         targetJson="["
361                 else
362                         targetJson="["
363                         arr=(${@:3})
364
365                         for ((i=0; i<$(($#-2)); i=i+1)); do
366
367                                 if [ "$targetJson" != "[" ]; then
368                                         targetJson=$targetJson","
369                                 fi
370                                 targetJson=$targetJson"{\"eiJobStatus\":\"${arr[$i]}\"}"
371                         done
372                 fi
373
374                 targetJson=$targetJson"]"
375                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
376                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
377
378                 if [ $res -ne 0 ]; then
379                         __log_test_fail_body
380                         return 1
381                 fi
382         fi
383         __log_test_pass
384         return 0
385 }