blob: 134f50c30793ce5ff50db1796505fb8afb401d56 [file] [log] [blame]
BjornMagnussonXA2791e082020-11-12 00:52:08 +01001#!/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
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010020# This is a script that contains container/service managemnt functions test functions for the Callback Reciver
BjornMagnussonXA2791e082020-11-12 00:52:08 +010021
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010022
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() {
BjornMagnussonXAa69cd902021-04-22 23:46:10 +020038 echo -e $RED" Image for app CR shall never be pulled from remote repo"$ERED
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010039}
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
BjornMagnussonXA483ee332021-04-08 01:35:24 +020049 echo -e $GREEN" Build Ok"$EGREEN
50 __retag_and_push_image CR_IMAGE
51 if [ $? -ne 0 ]; then
52 exit 1
53 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010054 else
BjornMagnussonXA483ee332021-04-08 01:35:24 +020055 echo -e $RED" Build Failed"$ERED
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010056 ((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
BjornMagnussonXA483ee332021-04-08 01:35:24 +020064# If a custom image repo is used then also the source image from the local repo is listed
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010065# arg: <docker-images-format-string> <file-to-append>
66__CR_image_data() {
67 echo -e "$CR_DISPLAY_NAME\t$(docker images --format $1 $CR_IMAGE)" >> $2
BjornMagnussonXA483ee332021-04-08 01:35:24 +020068 if [ ! -z "$CR_IMAGE_SOURCE" ]; then
69 echo -e "-- source image --\t$(docker images --format $1 $CR_IMAGE_SOURCE)" >> $2
70 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010071}
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__CR_kube_scale_zero() {
77 __kube_scale_all_resources $KUBE_SIM_NAMESPACE autotest CR
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__CR_kube_scale_zero_and_wait() {
83 echo -e $RED" CR 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__CR_kube_delete_all() {
89 __kube_delete_all_resources $KUBE_SIM_NAMESPACE autotest CR
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__CR_store_docker_logs() {
96 docker logs $CR_APP_NAME > $1$2_cr.log 2>&1
97}
98
99#######################################################
100
101
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100102## Access to Callback Receiver
103# Host name may be changed if app started by kube
104# Direct access from script
105CR_HTTPX="http"
106CR_HOST_NAME=$LOCALHOST_NAME
107CR_PATH=$CR_HTTPX"://"$CR_HOST_NAME":"$CR_EXTERNAL_PORT
108#Docker/Kube internal path
109if [ $RUNMODE == "KUBE" ]; then
110 CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME"."$KUBE_SIM_NAMESPACE":"$CR_EXTERNAL_PORT$CR_APP_CALLBACK
111else
112 CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME":"$CR_INTERNAL_PORT$CR_APP_CALLBACK
113fi
114# CR_ADAPTER used for switching between REST and DMAAP (only REST supported currently)
115CR_ADAPTER_TYPE="REST"
116CR_ADAPTER=$CR_PATH
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100117
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100118################
119### CR functions
120################
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100121
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100122# Set http as the protocol to use for all communication to the Callback Receiver
123# args: -
124# (Function for test scripts)
125use_cr_http() {
126 echo -e $BOLD"CR protocol setting"$EBOLD
127 echo -e " Using $BOLD http $EBOLD towards CR"
128
129 CR_HTTPX="http"
130 CR_PATH=$CR_HTTPX"://"$CR_HOST_NAME":"$CR_EXTERNAL_PORT
131
132 #Docker/Kube internal path
133 if [ $RUNMODE == "KUBE" ]; then
134 CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME"."$KUBE_SIM_NAMESPACE":"$CR_EXTERNAL_PORT$CR_APP_CALLBACK
135 else
136 CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME":"$CR_INTERNAL_PORT$CR_APP_CALLBACK
137 fi
138 CR_ADAPTER_TYPE="REST"
139 CR_ADAPTER=$CR_PATH
140 echo ""
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100141}
142
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100143# Set https as the protocol to use for all communication to the Callback Receiver
144# args: -
145# (Function for test scripts)
146use_cr_https() {
147 echo -e $BOLD"CR protocol setting"$EBOLD
148 echo -e " Using $BOLD https $EBOLD towards CR"
149
150 CR_HTTPX="https"
151 CR_PATH=$CR_HTTPX"://"$CR_HOST_NAME":"$CR_EXTERNAL_SECURE_PORT
152
153 if [ $RUNMODE == "KUBE" ]; then
154 CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME"."$KUBE_SIM_NAMESPACE":"$CR_EXTERNAL_SECURE_PORT$CR_APP_CALLBACK
155 else
156 CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME":"$CR_INTERNAL_SECURE_PORT$CR_APP_CALLBACK
157 fi
158
159 CR_ADAPTER_TYPE="REST"
160 CR_ADAPTER=$CR_PATH
161 echo ""
162}
163
164# Start the Callback reciver in the simulator group
165# args: -
166# (Function for test scripts)
167start_cr() {
168
169 echo -e $BOLD"Starting $CR_DISPLAY_NAME"$EBOLD
170
171 if [ $RUNMODE == "KUBE" ]; then
172
173 # Check if app shall be fully managed by the test script
174 __check_included_image "CR"
175 retcode_i=$?
176
177 # Check if app shall only be used by the testscipt
178 __check_prestarted_image "CR"
179 retcode_p=$?
180
181 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
182 echo -e $RED"The $CR_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
183 echo -e $RED"The $CR_APP_NAME will not be started"$ERED
184 exit
185 fi
186 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
187 echo -e $RED"The $CR_APP_NAME app is included both as managed and prestarted in this test script"$ERED
188 echo -e $RED"The $CR_APP_NAME will not be started"$ERED
189 exit
190 fi
191
192 # Check if app shall be used - not managed - by the test script
193 if [ $retcode_p -eq 0 ]; then
194 echo -e " Using existing $CR_APP_NAME deployment and service"
195 echo " Setting CR replicas=1"
196 __kube_scale deployment $CR_APP_NAME $KUBE_SIM_NAMESPACE 1
197 fi
198
199 if [ $retcode_i -eq 0 ]; then
200 echo -e " Creating $CR_APP_NAME deployment and service"
201 export CR_APP_NAME
202 export KUBE_SIM_NAMESPACE
203 export CR_IMAGE
204 export CR_INTERNAL_PORT
205 export CR_INTERNAL_SECURE_PORT
206 export CR_EXTERNAL_PORT
207 export CR_EXTERNAL_SECURE_PORT
208
209 __kube_create_namespace $KUBE_SIM_NAMESPACE
210
211 # Create service
212 input_yaml=$SIM_GROUP"/"$CR_COMPOSE_DIR"/"svc.yaml
213 output_yaml=$PWD/tmp/cr_svc.yaml
214 __kube_create_instance service $CR_APP_NAME $input_yaml $output_yaml
215
216 # Create app
217 input_yaml=$SIM_GROUP"/"$CR_COMPOSE_DIR"/"app.yaml
218 output_yaml=$PWD/tmp/cr_app.yaml
219 __kube_create_instance app $CR_APP_NAME $input_yaml $output_yaml
220
221 fi
222
223 echo " Retrieving host and ports for service..."
224 CR_HOST_NAME=$(__kube_get_service_host $CR_APP_NAME $KUBE_SIM_NAMESPACE)
225
226 CR_EXTERNAL_PORT=$(__kube_get_service_port $CR_APP_NAME $KUBE_SIM_NAMESPACE "http")
227 CR_EXTERNAL_SECURE_PORT=$(__kube_get_service_port $CR_APP_NAME $KUBE_SIM_NAMESPACE "https")
228
229 echo " Host IP, http port, https port: $CR_HOST_NAME $CR_EXTERNAL_PORT $CR_EXTERNAL_SECURE_PORT"
230 if [ $CR_HTTPX == "http" ]; then
231 CR_PATH=$CR_HTTPX"://"$CR_HOST_NAME":"$CR_EXTERNAL_PORT
232 CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME"."$KUBE_SIM_NAMESPACE":"$CR_EXTERNAL_PORT$CR_APP_CALLBACK
233 else
234 CR_PATH=$CR_HTTPX"://"$CR_HOST_NAME":"$CR_EXTERNAL_SECURE_PORT
235 CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME"."$KUBE_SIM_NAMESPACE":"$CR_EXTERNAL_SECURE_PORT$CR_APP_CALLBACK
236 fi
237 if [ $CR_ADAPTER_TYPE == "REST" ]; then
238 CR_ADAPTER=$CR_PATH
239 fi
240
241 __check_service_start $CR_APP_NAME $CR_PATH$CR_ALIVE_URL
242
243 echo -ne " Service $CR_APP_NAME - reset "$SAMELINE
244 result=$(__do_curl $CR_APP_NAME $CR_PATH/reset)
245 if [ $? -ne 0 ]; then
246 echo -e " Service $CR_APP_NAME - reset $RED Failed $ERED - will continue"
247 else
248 echo -e " Service $CR_APP_NAME - reset $GREEN OK $EGREEN"
249 fi
250 else
251 # Check if docker app shall be fully managed by the test script
252 __check_included_image 'CR'
253 if [ $? -eq 1 ]; then
254 echo -e $RED"The Callback Receiver app is not included in this test script"$ERED
255 echo -e $RED"The Callback Receiver will not be started"$ERED
256 exit
257 fi
258
259 export CR_APP_NAME
260 export CR_INTERNAL_PORT
261 export CR_EXTERNAL_PORT
262 export CR_INTERNAL_SECURE_PORT
263 export CR_EXTERNAL_SECURE_PORT
264 export DOCKER_SIM_NWNAME
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100265 export CR_DISPLAY_NAME
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100266
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100267 __start_container $CR_COMPOSE_DIR "" NODOCKERARGS 1 $CR_APP_NAME
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100268
269 __check_service_start $CR_APP_NAME $CR_PATH$CR_ALIVE_URL
270 fi
271 echo ""
272}
273
274
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100275# Tests if a variable value in the CR is equal to a target value and and optional timeout.
276# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
277# equal to the target or not.
278# Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
279# before setting pass or fail depending on if the variable value becomes equal to the target
280# value or not.
281# (Function for test scripts)
282cr_equal() {
283 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100284 __var_test "CR" "$CR_PATH/counter/" $1 "=" $2 $3
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100285 else
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100286 __print_err "Wrong args to cr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
287 fi
288}
289
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100290# CR API: Check the contents of all current ric sync events for one id from PMS
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100291# <response-code> <id> [ EMPTY | ( <ric-id> )+ ]
292# (Function for test scripts)
293cr_api_check_all_sync_events() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100294 __log_test_start $@
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100295
296 if [ "$PMS_VERSION" != "V2" ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100297 __log_test_fail_not_supported
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100298 return 1
299 fi
300
301 if [ $# -lt 2 ]; then
302 __print_err "<response-code> <id> [ EMPTY | ( <ric-id> )+ ]" $@
303 return 1
304 fi
305
306 query="/get-all-events/"$2
307 res="$(__do_curl_to_api CR GET $query)"
308 status=${res:${#res}-3}
309
310 if [ $status -ne $1 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100311 __log_test_fail_status_code $1 $status
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100312 return 1
313 fi
314
315 if [ $# -gt 2 ]; then
316 body=${res:0:${#res}-3}
317 if [ $# -eq 3 ] && [ $3 == "EMPTY" ]; then
318 targetJson="["
319 else
320 targetJson="["
321 arr=(${@:3})
322
323 for ((i=0; i<$(($#-2)); i=i+1)); do
324
325 if [ "$targetJson" != "[" ]; then
326 targetJson=$targetJson","
327 fi
328 targetJson=$targetJson"{\"ric_id\":\"${arr[$i]}\",\"event_type\":\"AVAILABLE\"}"
329 done
330 fi
331
332 targetJson=$targetJson"]"
333 echo "TARGET JSON: $targetJson" >> $HTTPLOG
334 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
335
336 if [ $res -ne 0 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100337 __log_test_fail_body
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100338 return 1
339 fi
340 fi
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100341 __log_test_pass
342 return 0
343}
344
345# CR API: Check the contents of all current status events for one id from ECS
346# <response-code> <id> [ EMPTY | ( <status> )+ ]
347# (Function for test scripts)
348cr_api_check_all_ecs_events() {
349 __log_test_start $@
350
351 if [ $# -lt 2 ]; then
352 __print_err "<response-code> <id> [ EMPTY | ( <status> )+ ]" $@
353 return 1
354 fi
355
356 query="/get-all-events/"$2
357 res="$(__do_curl_to_api CR GET $query)"
358 status=${res:${#res}-3}
359
360 if [ $status -ne $1 ]; then
361 __log_test_fail_status_code $1 $status
362 return 1
363 fi
364
365 if [ $# -gt 2 ]; then
366 body=${res:0:${#res}-3}
367 if [ $# -eq 3 ] && [ $3 == "EMPTY" ]; then
368 targetJson="["
369 else
370 targetJson="["
371 arr=(${@:3})
372
373 for ((i=0; i<$(($#-2)); i=i+1)); do
374
375 if [ "$targetJson" != "[" ]; then
376 targetJson=$targetJson","
377 fi
378 targetJson=$targetJson"{\"eiJobStatus\":\"${arr[$i]}\"}"
379 done
380 fi
381
382 targetJson=$targetJson"]"
383 echo "TARGET JSON: $targetJson" >> $HTTPLOG
384 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
385
386 if [ $res -ne 0 ]; then
387 __log_test_fail_body
388 return 1
389 fi
390 fi
391 __log_test_pass
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100392 return 0
393}