blob: 1b82ea02e0c9915186d1f41a331eb12db0013b6d [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() {
BjornMagnussonXA663566c2021-11-08 10:25:07 +010096 if [ $RUNMODE == "KUBE" ]; then
BjornMagnussonXA79e37002021-11-22 13:36:04 +010097 for podname in $(kubectl get pods -n $KUBE_SIM_NAMESPACE -l "autotest=CR" -o custom-columns=":metadata.name"); do
98 kubectl logs -n $KUBE_SIM_NAMESPACE $podname --tail=-1 > $1$2_$podname.log 2>&1
99 done
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100100 else
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100101 crs=$(docker ps --filter "name=$CR_APP_NAME" --filter "network=$DOCKER_SIM_NWNAME" --filter "status=running" --format {{.Names}})
102 for crid in $crs; do
103 docker logs $crid > $1$2_$crid.log 2>&1
104 done
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100105 fi
106}
107
108# Initial setup of protocol, host and ports
109# This function is called for apps managed by the test script.
110# args: -
111__CR_initial_setup() {
112 use_cr_http
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100113}
114
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100115# Set app short-name, app name and namespace for logging runtime statistics of kubernets pods or docker containers
116# For docker, the namespace shall be excluded
117# This function is called for apps managed by the test script as well as for prestarted apps.
118# args: -
119__CR_statisics_setup() {
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100120 for ((CR_INSTANCE=MAX_CR_APP_COUNT; CR_INSTANCE>0; CR_INSTANCE-- )); do
121 if [ $RUNMODE == "KUBE" ]; then
122 CR_INSTANCE_KUBE=$(($CR_INSTANCE-1))
123 echo -n " CR-$CR_INSTANCE_KUBE $CR_APP_NAME-$CR_INSTANCE_KUBE $KUBE_SIM_NAMESPACE "
124 else
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100125 if [ $DOCKER_COMPOSE_VERION == "V1" ]; then
126 echo -n " CR_$CR_INSTANCE ${CR_APP_NAME}_cr_$CR_INSTANCE "
127 else
128 echo -n " CR_$CR_INSTANCE ${CR_APP_NAME}-cr-$CR_INSTANCE "
129 fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100130 fi
131 done
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100132}
133
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100134#######################################################
135
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100136################
137### CR functions
138################
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100139
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100140#Var to hold the current number of CR instances
141CR_APP_COUNT=1
142MAX_CR_APP_COUNT=10
143
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100144# Set http as the protocol to use for all communication to the Dmaap adapter
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100145# args: -
146# (Function for test scripts)
147use_cr_http() {
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100148 __cr_set_protocoll "http" $CR_INTERNAL_PORT $CR_EXTERNAL_PORT
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100149}
150
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100151# Set https as the protocol to use for all communication to the Dmaap adapter
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100152# args: -
153# (Function for test scripts)
154use_cr_https() {
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100155 __cr_set_protocoll "https" $CR_INTERNAL_SECURE_PORT $CR_EXTERNAL_SECURE_PORT
156}
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100157
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100158# Setup paths to svc/container for internal and external access
159# args: <protocol> <internal-port> <external-port>
160__cr_set_protocoll() {
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100161
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100162 echo -e $BOLD"$CR_DISPLAY_NAME protocol setting"$EBOLD
163 echo -e " Using $BOLD http $EBOLD towards $CR_DISPLAY_NAME"
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100164 ## Access to Dmaap adapter
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100165 for ((CR_INSTANCE=0; CR_INSTANCE<$MAX_CR_APP_COUNT; CR_INSTANCE++ )); do
166 CR_DOCKER_INSTANCE=$(($CR_INSTANCE+1))
167 # CR_SERVICE_PATH is the base path to cr
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100168 if [ $DOCKER_COMPOSE_VERION == "V1" ]; then
169 __CR_SERVICE_PATH=$1"://"$CR_APP_NAME"_cr_"${CR_DOCKER_INSTANCE}":"$2 # docker access, container->container and script->container via proxy
170 else
171 __CR_SERVICE_PATH=$1"://"$CR_APP_NAME"-cr-"${CR_DOCKER_INSTANCE}":"$2 # docker access, container->container and script->container via proxy
172 fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100173 if [ $RUNMODE == "KUBE" ]; then
174 __CR_SERVICE_PATH=$1"://"$CR_APP_NAME"-"$CR_INSTANCE.$CR_APP_NAME"."$KUBE_SIM_NAMESPACE":"$3 # kube access, pod->svc and script->svc via proxy
175 fi
176 export CR_SERVICE_PATH"_"${CR_INSTANCE}=$__CR_SERVICE_PATH
177 # Service paths are used in test script to provide callbacck urls to app
178 export CR_SERVICE_MR_PATH"_"${CR_INSTANCE}=$__CR_SERVICE_PATH$CR_APP_CALLBACK_MR #Only for messages from dmaap adapter/mediator
179 export CR_SERVICE_TEXT_PATH"_"${CR_INSTANCE}=$__CR_SERVICE_PATH$CR_APP_CALLBACK_TEXT #Callbacks for text payload
180 export CR_SERVICE_APP_PATH"_"${CR_INSTANCE}=$__CR_SERVICE_PATH$CR_APP_CALLBACK #For general callbacks from apps
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100181
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100182 if [ $CR_INSTANCE -eq 0 ]; then
183 # CR_ADAPTER used for switching between REST and DMAAP (only REST supported currently)
184 # CR_ADDAPTER need to be set before each call to CR....only set for instance 0 here
185 CR_ADAPTER_TYPE="REST"
186 CR_ADAPTER=$__CR_SERVICE_PATH
187 fi
188 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100189 echo ""
190}
191
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100192# Export env vars for config files, docker compose and kube resources
193# args: <proxy-flag>
194__cr_export_vars() {
195 export CR_APP_NAME
196 export CR_DISPLAY_NAME
197
198 export KUBE_SIM_NAMESPACE
199 export DOCKER_SIM_NWNAME
200
201 export CR_IMAGE
202
203 export CR_INTERNAL_PORT
204 export CR_INTERNAL_SECURE_PORT
205 export CR_EXTERNAL_PORT
206 export CR_EXTERNAL_SECURE_PORT
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100207
208 export CR_APP_COUNT
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100209}
210
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100211# Start the Callback reciver in the simulator group
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100212# args: <app-count>
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100213# (Function for test scripts)
214start_cr() {
215
216 echo -e $BOLD"Starting $CR_DISPLAY_NAME"$EBOLD
217
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100218 if [ $# -ne 1 ]; then
219 echo -e $RED" Number of CR instances missing, usage: start_cr <app-count>"$ERED
220 exit 1
221 fi
222 if [ $1 -lt 1 ] || [ $1 -gt 10 ]; then
223 echo -e $RED" Number of CR shall be 1...10, usage: start_cr <app-count>"$ERED
224 exit 1
225 fi
226 export CR_APP_COUNT=$1
227
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100228 if [ $RUNMODE == "KUBE" ]; then
229
230 # Check if app shall be fully managed by the test script
231 __check_included_image "CR"
232 retcode_i=$?
233
234 # Check if app shall only be used by the testscipt
235 __check_prestarted_image "CR"
236 retcode_p=$?
237
238 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
239 echo -e $RED"The $CR_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
240 echo -e $RED"The $CR_APP_NAME will not be started"$ERED
241 exit
242 fi
243 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
244 echo -e $RED"The $CR_APP_NAME app is included both as managed and prestarted in this test script"$ERED
245 echo -e $RED"The $CR_APP_NAME will not be started"$ERED
246 exit
247 fi
248
249 # Check if app shall be used - not managed - by the test script
250 if [ $retcode_p -eq 0 ]; then
251 echo -e " Using existing $CR_APP_NAME deployment and service"
252 echo " Setting CR replicas=1"
253 __kube_scale deployment $CR_APP_NAME $KUBE_SIM_NAMESPACE 1
254 fi
255
256 if [ $retcode_i -eq 0 ]; then
257 echo -e " Creating $CR_APP_NAME deployment and service"
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100258
259 __cr_export_vars
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100260
261 __kube_create_namespace $KUBE_SIM_NAMESPACE
262
263 # Create service
264 input_yaml=$SIM_GROUP"/"$CR_COMPOSE_DIR"/"svc.yaml
265 output_yaml=$PWD/tmp/cr_svc.yaml
266 __kube_create_instance service $CR_APP_NAME $input_yaml $output_yaml
267
268 # Create app
269 input_yaml=$SIM_GROUP"/"$CR_COMPOSE_DIR"/"app.yaml
270 output_yaml=$PWD/tmp/cr_app.yaml
271 __kube_create_instance app $CR_APP_NAME $input_yaml $output_yaml
272
273 fi
274
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100275 for ((CR_INSTANCE=0; CR_INSTANCE<$CR_APP_COUNT; CR_INSTANCE++ )); do
276 __dynvar="CR_SERVICE_PATH_"$CR_INSTANCE
277 __cr_app_name=$CR_APP_NAME"-"$CR_INSTANCE
278 __check_service_start $__cr_app_name ${!__dynvar}$CR_ALIVE_URL
279 result=$(__do_curl ${!__dynvar}/reset)
280 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100281
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100282 else
283 # Check if docker app shall be fully managed by the test script
284 __check_included_image 'CR'
285 if [ $? -eq 1 ]; then
286 echo -e $RED"The Callback Receiver app is not included in this test script"$ERED
287 echo -e $RED"The Callback Receiver will not be started"$ERED
288 exit
289 fi
290
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100291 __cr_export_vars
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100292
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100293 app_data=""
294 cntr=1
295 while [ $cntr -le $CR_APP_COUNT ]; do
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100296 if [ $DOCKER_COMPOSE_VERION == "V1" ]; then
297 app=$CR_APP_NAME"_cr_"$cntr
298 else
299 app=$CR_APP_NAME"-cr-"$cntr
300 fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100301 app_data="$app_data $app"
302 let cntr=cntr+1
303 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100304
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100305 echo "COMPOSE_PROJECT_NAME="$CR_APP_NAME > $SIM_GROUP/$CR_COMPOSE_DIR/.env
306
307 __start_container $CR_COMPOSE_DIR "" NODOCKERARGS $CR_APP_COUNT $app_data
308
309 cntr=1 #Counter for docker instance, starts on 1
310 cntr2=0 #Couter for env var name, starts with 0 to be compablible with kube
311 while [ $cntr -le $CR_APP_COUNT ]; do
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100312 if [ $DOCKER_COMPOSE_VERION == "V1" ]; then
313 app=$CR_APP_NAME"_cr_"$cntr
314 else
315 app=$CR_APP_NAME"-cr-"$cntr
316 fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100317 __dynvar="CR_SERVICE_PATH_"$cntr2
318 __check_service_start $app ${!__dynvar}$CR_ALIVE_URL
319 let cntr=cntr+1
320 let cntr2=cntr2+1
321 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100322 fi
323 echo ""
324}
325
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100326#Convert a cr path id to the value of the environment var holding the url
327# arg: <cr-path-id>
328# returns: <base-url-to-the-app>
329__cr_get_service_path(){
330 if [ $# -ne 1 ]; then
331 echo "DUMMY"
332 return 1
333 fi
334 if [ $1 -lt 0 ] || [ $1 -ge $MAX_CR_APP_COUNT ]; then
335 echo "DUMMY"
336 return 1
337 fi
338 __dynvar="CR_SERVICE_PATH_"$1
339 echo ${!__dynvar}
340 return 0
341}
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100342
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100343# Tests if a variable value in the CR is equal to a target value and and optional timeout.
344# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
345# equal to the target or not.
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100346# Arg: <cr-path-id> <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100347# before setting pass or fail depending on if the variable value becomes equal to the target
348# value or not.
349# (Function for test scripts)
350cr_equal() {
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100351 if [ $# -eq 3 ] || [ $# -eq 4 ]; then
352 CR_SERVICE_PATH=$(__cr_get_service_path $1)
353 CR_ADAPTER=$CR_SERVICE_PATH
354 if [ $? -ne 0 ]; then
355 __print_err "<cr-path-id> missing or incorrect" $@
356 return 1
357 fi
358 __var_test "CR" "$CR_SERVICE_PATH/counter/" $2 "=" $3 $4
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100359 else
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100360 __print_err "Wrong args to cr_equal, needs three or four args: <cr-path-id> <variable-name> <target-value> [ timeout ]" $@
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100361 fi
362}
363
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100364# Tests if a variable value in the CR contains the target string and and optional timeout
365# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable contains
366# the target or not.
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100367# Arg: <cr-path-id> <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100368# before setting pass or fail depending on if the variable value contains the target
369# value or not.
370# (Function for test scripts)
371cr_contains_str() {
372
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100373 if [ $# -eq 3 ] || [ $# -eq 4 ]; then
374 CR_SERVICE_PATH=$(__cr_get_service_path $1)
375 CR_ADAPTER=$CR_SERVICE_PATH
376 if [ $? -ne 0 ]; then
377 __print_err "<cr-path-id> missing or incorrect" $@
378 return 1
379 fi
380 __var_test "CR" "$CR_SERVICE_PATH/counter/" $2 "contain_str" $3 $4
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100381 return 0
382 else
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100383 __print_err "needs two or three args: <cr-path-id> <variable-name> <target-value> [ timeout ]"
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100384 return 1
385 fi
386}
387
388# Read a variable value from CR sim and send to stdout. Arg: <variable-name>
389cr_read() {
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100390 CR_SERVICE_PATH=$(__cr_get_service_path $1)
391 CR_ADAPTER=$CR_SERVICE_PATH
392 if [ $? -ne 0 ]; then
393 __print_err "<cr-path-id> missing or incorrect" $@
394 return 1
395 fi
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100396 echo "$(__do_curl $CR_SERVICE_PATH/counter/$1)"
397}
398
399# Function to configure write delay on callbacks
400# Delay given in seconds.
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100401# arg <response-code> <cr-path-id> <delay-in-sec>
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100402# (Function for test scripts)
403cr_delay_callback() {
404 __log_conf_start $@
405
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100406 if [ $# -ne 3 ]; then
407 __print_err "<response-code> <cr-path-id> <delay-in-sec>]" $@
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100408 return 1
409 fi
410
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100411 CR_SERVICE_PATH=$(__cr_get_service_path $2)
412 CR_ADAPTER=$CR_SERVICE_PATH
413 if [ $? -ne 0 ]; then
414 __print_err "<cr-path-id> missing or incorrect" $@
415 return 1
416 fi
417
418 res="$(__do_curl_to_api CR POST /forcedelay?delay=$3)"
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100419 status=${res:${#res}-3}
420
421 if [ $status -ne 200 ]; then
422 __log_conf_fail_status_code $1 $status
423 return 1
424 fi
425
426 __log_conf_ok
427 return 0
428}
429
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100430# CR API: Check the contents of all current ric sync events for one id from PMS
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100431# <response-code> <cr-path-id> <id> [ EMPTY | ( <ric-id> )+ ]
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100432# (Function for test scripts)
433cr_api_check_all_sync_events() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100434 __log_test_start $@
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100435
436 if [ "$PMS_VERSION" != "V2" ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100437 __log_test_fail_not_supported
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100438 return 1
439 fi
440
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100441 if [ $# -lt 3 ]; then
442 __print_err "<response-code> <cr-path-id> <id> [ EMPTY | ( <ric-id> )+ ]" $@
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100443 return 1
444 fi
445
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100446 CR_SERVICE_PATH=$(__cr_get_service_path $2)
447 CR_ADAPTER=$CR_SERVICE_PATH
448 if [ $? -ne 0 ]; then
449 __print_err "<cr-path-id> missing or incorrect" $@
450 return 1
451 fi
452
453 query="/get-all-events/"$3
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100454 res="$(__do_curl_to_api CR GET $query)"
455 status=${res:${#res}-3}
456
457 if [ $status -ne $1 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100458 __log_test_fail_status_code $1 $status
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100459 return 1
460 fi
461
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100462 if [ $# -gt 3 ]; then
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100463 body=${res:0:${#res}-3}
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100464 if [ $# -eq 4 ] && [ $4 == "EMPTY" ]; then
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100465 targetJson="["
466 else
467 targetJson="["
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100468 arr=(${@:4})
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100469
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100470 for ((i=0; i<$(($#-3)); i=i+1)); do
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100471
472 if [ "$targetJson" != "[" ]; then
473 targetJson=$targetJson","
474 fi
475 targetJson=$targetJson"{\"ric_id\":\"${arr[$i]}\",\"event_type\":\"AVAILABLE\"}"
476 done
477 fi
478
479 targetJson=$targetJson"]"
480 echo "TARGET JSON: $targetJson" >> $HTTPLOG
481 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
482
483 if [ $res -ne 0 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100484 __log_test_fail_body
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100485 return 1
486 fi
487 fi
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100488 __log_test_pass
489 return 0
490}
491
492# CR API: Check the contents of all current status events for one id from ECS
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100493# <response-code> <cr-path-id> <id> [ EMPTY | ( <status> )+ ]
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100494# (Function for test scripts)
495cr_api_check_all_ecs_events() {
496 __log_test_start $@
497
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100498 if [ $# -lt 3 ]; then
499 __print_err "<response-code> <cr-path-id> <id> [ EMPTY | ( <status> )+ ]" $@
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100500 return 1
501 fi
502
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100503 CR_SERVICE_PATH=$(__cr_get_service_path $2)
504 CR_ADAPTER=$CR_SERVICE_PATH
505 if [ $? -ne 0 ]; then
506 __print_err "<cr-path-id> missing or incorrect" $@
507 return 1
508 fi
509
510 query="/get-all-events/"$3
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100511 res="$(__do_curl_to_api CR GET $query)"
512 status=${res:${#res}-3}
513
514 if [ $status -ne $1 ]; then
515 __log_test_fail_status_code $1 $status
516 return 1
517 fi
518
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100519 if [ $# -gt 3 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100520 body=${res:0:${#res}-3}
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100521 if [ $# -eq 4 ] && [ $4 == "EMPTY" ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100522 targetJson="["
523 else
524 targetJson="["
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100525 arr=(${@:4})
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100526
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100527 for ((i=0; i<$(($#-3)); i=i+1)); do
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100528
529 if [ "$targetJson" != "[" ]; then
530 targetJson=$targetJson","
531 fi
532 targetJson=$targetJson"{\"eiJobStatus\":\"${arr[$i]}\"}"
533 done
534 fi
535
536 targetJson=$targetJson"]"
537 echo "TARGET JSON: $targetJson" >> $HTTPLOG
538 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
539
540 if [ $res -ne 0 ]; then
541 __log_test_fail_body
542 return 1
543 fi
544 fi
545 __log_test_pass
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100546 return 0
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200547}
548
549# CR API: Check the contents of all current type subscription events for one id from ECS
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100550# <response-code> <cr-path-id> <id> [ EMPTY | ( <type-id> <schema> <registration-status> )+ ]
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200551# (Function for test scripts)
552cr_api_check_all_ecs_subscription_events() {
553 __log_test_start $@
554
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100555 #Valid number of parameter 3,4,8,12
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200556 paramError=1
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100557 if [ $# -eq 3 ]; then
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200558 paramError=0
559 fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100560 if [ $# -eq 4 ] && [ "$4" == "EMPTY" ]; then
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200561 paramError=0
562 fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100563 variablecount=$(($#-3))
564 if [ $# -gt 4 ] && [ $(($variablecount%3)) -eq 0 ]; then
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200565 paramError=0
566 fi
567 if [ $paramError -eq 1 ]; then
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100568 __print_err "<response-code> <cr-path-id> <id> [ EMPTY | ( <type-id> <schema> <registration-status> )+ ]" $@
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200569 return 1
570 fi
571
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100572 CR_SERVICE_PATH=$(__cr_get_service_path $2)
573 CR_ADAPTER=$CR_SERVICE_PATH
574 if [ $? -ne 0 ]; then
575 __print_err "<cr-path-id> missing or incorrect" $@
576 return 1
577 fi
578
579 query="/get-all-events/"$3
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200580 res="$(__do_curl_to_api CR GET $query)"
581 status=${res:${#res}-3}
582
583 if [ $status -ne $1 ]; then
584 __log_test_fail_status_code $1 $status
585 return 1
586 fi
587
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100588 if [ $# -gt 3 ]; then
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200589 body=${res:0:${#res}-3}
590 targetJson="["
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100591 if [ $# -gt 4 ]; then
592 arr=(${@:4})
593 for ((i=0; i<$(($#-4)); i=i+3)); do
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200594 if [ "$targetJson" != "[" ]; then
595 targetJson=$targetJson","
596 fi
597 if [ -f ${arr[$i+1]} ]; then
598 jobfile=$(cat ${arr[$i+1]})
599 else
600 __log_test_fail_general "Job schema file "${arr[$i+1]}", does not exist"
601 return 1
602 fi
603 targetJson=$targetJson"{\"info_type_id\":\"${arr[$i]}\",\"job_data_schema\":$jobfile,\"status\":\"${arr[$i+2]}\"}"
604 done
605 fi
606 targetJson=$targetJson"]"
607
608 echo " TARGET JSON: $targetJson" >> $HTTPLOG
609 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
610
611 if [ $res -ne 0 ]; then
612 __log_test_fail_body
613 return 1
614 fi
615 fi
616
617 __log_test_pass
618 return 0
619}
620
621
622# CR API: Reset all events and counters
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100623# Arg: <cr-path-id>
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200624# (Function for test scripts)
625cr_api_reset() {
626 __log_conf_start $@
627
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100628 if [ $# -ne 0 ]; then
629 __print_err "<cr-path-id>" $@
630 return 1
631 fi
632
633 CR_SERVICE_PATH=$(__cr_get_service_path $1)
634 CR_ADAPTER=$CR_SERVICE_PATH
635 if [ $? -ne 0 ]; then
636 __print_err "<cr-path-id> missing or incorrect" $@
637 return 1
638 fi
639
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200640 res="$(__do_curl_to_api CR GET /reset)"
641 status=${res:${#res}-3}
642
643 if [ $status -ne 200 ]; then
644 __log_conf_fail_status_code $1 $status
645 return 1
646 fi
647
648 __log_conf_ok
649 return 0
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100650}
651
652
653# CR API: Check the contents of all json events for path
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100654# <response-code> <cr-path-id> <topic-url> (EMPTY | <json-msg>+ )
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100655# (Function for test scripts)
656cr_api_check_all_genric_json_events() {
657 __log_test_start $@
658
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100659 if [ $# -lt 4 ]; then
660 __print_err "<response-code> <cr-path-id> <topic-url> (EMPTY | <json-msg>+ )" $@
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100661 return 1
662 fi
663
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100664 CR_SERVICE_PATH=$(__cr_get_service_path $2)
665 CR_ADAPTER=$CR_SERVICE_PATH
666 if [ $? -ne 0 ]; then
667 __print_err "<cr-path-id> missing or incorrect" $@
668 return 1
669 fi
670
671 query="/get-all-events/"$3
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100672 res="$(__do_curl_to_api CR GET $query)"
673 status=${res:${#res}-3}
674
675 if [ $status -ne $1 ]; then
676 __log_test_fail_status_code $1 $status
677 return 1
678 fi
679 body=${res:0:${#res}-3}
680 targetJson="["
681
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100682 if [ $4 != "EMPTY" ]; then
683 shift
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100684 shift
685 shift
686 while [ $# -gt 0 ]; do
687 if [ "$targetJson" != "[" ]; then
688 targetJson=$targetJson","
689 fi
690 targetJson=$targetJson$1
691 shift
692 done
693 fi
694 targetJson=$targetJson"]"
695
696 echo " TARGET JSON: $targetJson" >> $HTTPLOG
697 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
698
699 if [ $res -ne 0 ]; then
700 __log_test_fail_body
701 return 1
702 fi
703
704 __log_test_pass
705 return 0
706}
707
708
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100709# CR API: Check a single (oldest) json event (or none if empty) for path
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100710# <response-code> <cr-path-id> <topic-url> (EMPTY | <json-msg> )
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100711# (Function for test scripts)
712cr_api_check_single_genric_json_event() {
713 __log_test_start $@
714
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100715 if [ $# -ne 4 ]; then
716 __print_err "<response-code> <cr-path-id> <topic-url> (EMPTY | <json-msg> )" $@
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100717 return 1
718 fi
719
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100720 CR_SERVICE_PATH=$(__cr_get_service_path $2)
721 CR_ADAPTER=$CR_SERVICE_PATH
722 if [ $? -ne 0 ]; then
723 __print_err "<cr-path-id> missing or incorrect" $@
724 return 1
725 fi
726
727 query="/get-event/"$3
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100728 res="$(__do_curl_to_api CR GET $query)"
729 status=${res:${#res}-3}
730
731 if [ $status -ne $1 ]; then
732 __log_test_fail_status_code $1 $status
733 return 1
734 fi
735 body=${res:0:${#res}-3}
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100736 targetJson=$4
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100737
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100738 if [ $targetJson == "EMPTY" ] && [ ${#body} -ne 0 ]; then
739 __log_test_fail_body
740 return 1
741 fi
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100742 echo " TARGET JSON: $targetJson" >> $HTTPLOG
743 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
744
745 if [ $res -ne 0 ]; then
746 __log_test_fail_body
747 return 1
748 fi
749
750 __log_test_pass
751 return 0
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100752}
753
754# CR API: Check a single (oldest) json in md5 format (or none if empty) for path.
755# Note that if a json message is given, it shall be compact, no ws except inside string.
756# The MD5 will generate different hash if ws is present or not in otherwise equivalent json
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100757# arg: <response-code> <cr-path-id> <topic-url> (EMPTY | <data-msg> )
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100758# (Function for test scripts)
759cr_api_check_single_genric_event_md5() {
760 __log_test_start $@
761
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100762 if [ $# -ne 4 ]; then
763 __print_err "<response-code> <cr-path-id> <topic-url> (EMPTY | <data-msg> )" $@
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100764 return 1
765 fi
766
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100767 CR_SERVICE_PATH=$(__cr_get_service_path $2)
768 CR_ADAPTER=$CR_SERVICE_PATH
769 if [ $? -ne 0 ]; then
770 __print_err "<cr-path-id> missing or incorrect" $@
771 return 1
772 fi
773
774 query="/get-event/"$3
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100775 res="$(__do_curl_to_api CR GET $query)"
776 status=${res:${#res}-3}
777
778 if [ $status -ne $1 ]; then
779 __log_test_fail_status_code $1 $status
780 return 1
781 fi
782 body=${res:0:${#res}-3}
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100783 if [ $4 == "EMPTY" ]; then
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100784 if [ ${#body} -ne 0 ]; then
785 __log_test_fail_body
786 return 1
787 else
788 __log_test_pass
789 return 0
790 fi
791 fi
792 command -v md5 > /dev/null # Mac
793 if [ $? -eq 0 ]; then
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100794 targetMd5=$(echo -n "$4" | md5)
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100795 else
796 command -v md5sum > /dev/null # Linux
797 if [ $? -eq 0 ]; then
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100798 targetMd5=$(echo -n "$4" | md5sum | cut -d' ' -f 1) # Need to cut additional info printed by cmd
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100799 else
800 __log_test_fail_general "Command md5 nor md5sum is available"
801 return 1
802 fi
803 fi
804 targetMd5="\""$targetMd5"\"" #Quotes needed
805
806 echo " TARGET MD5 hash: $targetMd5" >> $HTTPLOG
807
808 if [ "$body" != "$targetMd5" ]; then
809 __log_test_fail_body
810 return 1
811 fi
812
813 __log_test_pass
814 return 0
815}
816
817# CR API: Check a single (oldest) event in md5 format (or none if empty) for path.
818# Note that if a file with json message is given, the json shall be compact, no ws except inside string and not newlines.
819# The MD5 will generate different hash if ws/newlines is present or not in otherwise equivalent json
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100820# arg: <response-code> <cr-path-id> <topic-url> (EMPTY | <data-file> )
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100821# (Function for test scripts)
822cr_api_check_single_genric_event_md5_file() {
823 __log_test_start $@
824
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100825 if [ $# -ne 4 ]; then
826 __print_err "<response-code> <cr-path-id> <topic-url> (EMPTY | <data-file> )" $@
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100827 return 1
828 fi
829
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100830 CR_SERVICE_PATH=$(__cr_get_service_path $2)
831 CR_ADAPTER=$CR_SERVICE_PATH
832 if [ $? -ne 0 ]; then
833 __print_err "<cr-path-id> missing or incorrect" $@
834 return 1
835 fi
836
837 query="/get-event/"$3
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100838 res="$(__do_curl_to_api CR GET $query)"
839 status=${res:${#res}-3}
840
841 if [ $status -ne $1 ]; then
842 __log_test_fail_status_code $1 $status
843 return 1
844 fi
845 body=${res:0:${#res}-3}
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100846 if [ $4 == "EMPTY" ]; then
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100847 if [ ${#body} -ne 0 ]; then
848 __log_test_fail_body
849 return 1
850 else
851 __log_test_pass
852 return 0
853 fi
854 fi
855
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100856 if [ ! -f $4 ]; then
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100857 __log_test_fail_general "File $3 does not exist"
858 return 1
859 fi
860
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100861 filedata=$(cat $4)
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100862
863 command -v md5 > /dev/null # Mac
864 if [ $? -eq 0 ]; then
865 targetMd5=$(echo -n "$filedata" | md5)
866 else
867 command -v md5sum > /dev/null # Linux
868 if [ $? -eq 0 ]; then
869 targetMd5=$(echo -n "$filedata" | md5sum | cut -d' ' -f 1) # Need to cut additional info printed by cmd
870 else
871 __log_test_fail_general "Command md5 nor md5sum is available"
872 return 1
873 fi
874 fi
875 targetMd5="\""$targetMd5"\"" #Quotes needed
876
877 echo " TARGET MD5 hash: $targetMd5" >> $HTTPLOG
878
879 if [ "$body" != "$targetMd5" ]; then
880 __log_test_fail_body
881 return 1
882 fi
883
884 __log_test_pass
885 return 0
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100886}