blob: 40ef7ea79be9b8aa1caebf5f5e59b955d197850d [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
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100134# Check application requirements, e.g. helm, the the test needs. Exit 1 if req not satisfied
135# args: -
136__CR_test_requirements() {
137 :
138}
139
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100140#######################################################
141
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100142################
143### CR functions
144################
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100145
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100146#Var to hold the current number of CR instances
147CR_APP_COUNT=1
148MAX_CR_APP_COUNT=10
149
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100150# Set http as the protocol to use for all communication to the Dmaap adapter
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100151# args: -
152# (Function for test scripts)
153use_cr_http() {
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100154 __cr_set_protocoll "http" $CR_INTERNAL_PORT $CR_EXTERNAL_PORT
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100155}
156
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100157# Set https as the protocol to use for all communication to the Dmaap adapter
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100158# args: -
159# (Function for test scripts)
160use_cr_https() {
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100161 __cr_set_protocoll "https" $CR_INTERNAL_SECURE_PORT $CR_EXTERNAL_SECURE_PORT
162}
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100163
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100164# Setup paths to svc/container for internal and external access
165# args: <protocol> <internal-port> <external-port>
166__cr_set_protocoll() {
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100167
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100168 echo -e $BOLD"$CR_DISPLAY_NAME protocol setting"$EBOLD
BjornMagnussonXA007b6452021-11-29 08:03:38 +0100169 echo -e " Using $BOLD $1 $EBOLD towards $CR_DISPLAY_NAME"
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100170 ## Access to Dmaap adapter
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100171 for ((CR_INSTANCE=0; CR_INSTANCE<$MAX_CR_APP_COUNT; CR_INSTANCE++ )); do
172 CR_DOCKER_INSTANCE=$(($CR_INSTANCE+1))
173 # CR_SERVICE_PATH is the base path to cr
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100174 if [ $DOCKER_COMPOSE_VERION == "V1" ]; then
175 __CR_SERVICE_PATH=$1"://"$CR_APP_NAME"_cr_"${CR_DOCKER_INSTANCE}":"$2 # docker access, container->container and script->container via proxy
176 else
177 __CR_SERVICE_PATH=$1"://"$CR_APP_NAME"-cr-"${CR_DOCKER_INSTANCE}":"$2 # docker access, container->container and script->container via proxy
178 fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100179 if [ $RUNMODE == "KUBE" ]; then
180 __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
181 fi
182 export CR_SERVICE_PATH"_"${CR_INSTANCE}=$__CR_SERVICE_PATH
183 # Service paths are used in test script to provide callbacck urls to app
184 export CR_SERVICE_MR_PATH"_"${CR_INSTANCE}=$__CR_SERVICE_PATH$CR_APP_CALLBACK_MR #Only for messages from dmaap adapter/mediator
185 export CR_SERVICE_TEXT_PATH"_"${CR_INSTANCE}=$__CR_SERVICE_PATH$CR_APP_CALLBACK_TEXT #Callbacks for text payload
186 export CR_SERVICE_APP_PATH"_"${CR_INSTANCE}=$__CR_SERVICE_PATH$CR_APP_CALLBACK #For general callbacks from apps
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100187
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100188 if [ $CR_INSTANCE -eq 0 ]; then
189 # CR_ADAPTER used for switching between REST and DMAAP (only REST supported currently)
190 # CR_ADDAPTER need to be set before each call to CR....only set for instance 0 here
191 CR_ADAPTER_TYPE="REST"
192 CR_ADAPTER=$__CR_SERVICE_PATH
193 fi
194 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100195 echo ""
196}
197
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100198# Export env vars for config files, docker compose and kube resources
199# args: <proxy-flag>
200__cr_export_vars() {
201 export CR_APP_NAME
202 export CR_DISPLAY_NAME
203
204 export KUBE_SIM_NAMESPACE
205 export DOCKER_SIM_NWNAME
206
207 export CR_IMAGE
208
209 export CR_INTERNAL_PORT
210 export CR_INTERNAL_SECURE_PORT
211 export CR_EXTERNAL_PORT
212 export CR_EXTERNAL_SECURE_PORT
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100213
214 export CR_APP_COUNT
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100215}
216
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100217# Start the Callback reciver in the simulator group
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100218# args: <app-count>
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100219# (Function for test scripts)
220start_cr() {
221
222 echo -e $BOLD"Starting $CR_DISPLAY_NAME"$EBOLD
223
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100224 if [ $# -ne 1 ]; then
225 echo -e $RED" Number of CR instances missing, usage: start_cr <app-count>"$ERED
226 exit 1
227 fi
228 if [ $1 -lt 1 ] || [ $1 -gt 10 ]; then
229 echo -e $RED" Number of CR shall be 1...10, usage: start_cr <app-count>"$ERED
230 exit 1
231 fi
232 export CR_APP_COUNT=$1
233
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100234 if [ $RUNMODE == "KUBE" ]; then
235
236 # Check if app shall be fully managed by the test script
237 __check_included_image "CR"
238 retcode_i=$?
239
240 # Check if app shall only be used by the testscipt
241 __check_prestarted_image "CR"
242 retcode_p=$?
243
244 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
245 echo -e $RED"The $CR_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
246 echo -e $RED"The $CR_APP_NAME will not be started"$ERED
247 exit
248 fi
249 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
250 echo -e $RED"The $CR_APP_NAME app is included both as managed and prestarted in this test script"$ERED
251 echo -e $RED"The $CR_APP_NAME will not be started"$ERED
252 exit
253 fi
254
255 # Check if app shall be used - not managed - by the test script
256 if [ $retcode_p -eq 0 ]; then
257 echo -e " Using existing $CR_APP_NAME deployment and service"
258 echo " Setting CR replicas=1"
259 __kube_scale deployment $CR_APP_NAME $KUBE_SIM_NAMESPACE 1
260 fi
261
262 if [ $retcode_i -eq 0 ]; then
263 echo -e " Creating $CR_APP_NAME deployment and service"
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100264
265 __cr_export_vars
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100266
267 __kube_create_namespace $KUBE_SIM_NAMESPACE
268
269 # Create service
270 input_yaml=$SIM_GROUP"/"$CR_COMPOSE_DIR"/"svc.yaml
271 output_yaml=$PWD/tmp/cr_svc.yaml
272 __kube_create_instance service $CR_APP_NAME $input_yaml $output_yaml
273
274 # Create app
275 input_yaml=$SIM_GROUP"/"$CR_COMPOSE_DIR"/"app.yaml
276 output_yaml=$PWD/tmp/cr_app.yaml
277 __kube_create_instance app $CR_APP_NAME $input_yaml $output_yaml
278
279 fi
280
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100281 for ((CR_INSTANCE=0; CR_INSTANCE<$CR_APP_COUNT; CR_INSTANCE++ )); do
282 __dynvar="CR_SERVICE_PATH_"$CR_INSTANCE
283 __cr_app_name=$CR_APP_NAME"-"$CR_INSTANCE
284 __check_service_start $__cr_app_name ${!__dynvar}$CR_ALIVE_URL
285 result=$(__do_curl ${!__dynvar}/reset)
286 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100287
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100288 else
289 # Check if docker app shall be fully managed by the test script
290 __check_included_image 'CR'
291 if [ $? -eq 1 ]; then
292 echo -e $RED"The Callback Receiver app is not included in this test script"$ERED
293 echo -e $RED"The Callback Receiver will not be started"$ERED
294 exit
295 fi
296
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100297 __cr_export_vars
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100298
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100299 app_data=""
300 cntr=1
301 while [ $cntr -le $CR_APP_COUNT ]; do
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100302 if [ $DOCKER_COMPOSE_VERION == "V1" ]; then
303 app=$CR_APP_NAME"_cr_"$cntr
304 else
305 app=$CR_APP_NAME"-cr-"$cntr
306 fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100307 app_data="$app_data $app"
308 let cntr=cntr+1
309 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100310
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100311 echo "COMPOSE_PROJECT_NAME="$CR_APP_NAME > $SIM_GROUP/$CR_COMPOSE_DIR/.env
312
313 __start_container $CR_COMPOSE_DIR "" NODOCKERARGS $CR_APP_COUNT $app_data
314
315 cntr=1 #Counter for docker instance, starts on 1
316 cntr2=0 #Couter for env var name, starts with 0 to be compablible with kube
317 while [ $cntr -le $CR_APP_COUNT ]; do
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100318 if [ $DOCKER_COMPOSE_VERION == "V1" ]; then
319 app=$CR_APP_NAME"_cr_"$cntr
320 else
321 app=$CR_APP_NAME"-cr-"$cntr
322 fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100323 __dynvar="CR_SERVICE_PATH_"$cntr2
324 __check_service_start $app ${!__dynvar}$CR_ALIVE_URL
325 let cntr=cntr+1
326 let cntr2=cntr2+1
327 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100328 fi
329 echo ""
330}
331
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100332#Convert a cr path id to the value of the environment var holding the url
333# arg: <cr-path-id>
334# returns: <base-url-to-the-app>
335__cr_get_service_path(){
336 if [ $# -ne 1 ]; then
337 echo "DUMMY"
338 return 1
339 fi
340 if [ $1 -lt 0 ] || [ $1 -ge $MAX_CR_APP_COUNT ]; then
341 echo "DUMMY"
342 return 1
343 fi
344 __dynvar="CR_SERVICE_PATH_"$1
345 echo ${!__dynvar}
346 return 0
347}
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100348
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100349# Tests if a variable value in the CR is equal to a target value and and optional timeout.
350# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
351# equal to the target or not.
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100352# 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 +0100353# before setting pass or fail depending on if the variable value becomes equal to the target
354# value or not.
355# (Function for test scripts)
356cr_equal() {
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100357 if [ $# -eq 3 ] || [ $# -eq 4 ]; then
358 CR_SERVICE_PATH=$(__cr_get_service_path $1)
359 CR_ADAPTER=$CR_SERVICE_PATH
360 if [ $? -ne 0 ]; then
361 __print_err "<cr-path-id> missing or incorrect" $@
362 return 1
363 fi
364 __var_test "CR" "$CR_SERVICE_PATH/counter/" $2 "=" $3 $4
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100365 else
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100366 __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 +0100367 fi
368}
369
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100370# Tests if a variable value in the CR contains the target string and and optional timeout
371# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable contains
372# the target or not.
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100373# 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 +0100374# before setting pass or fail depending on if the variable value contains the target
375# value or not.
376# (Function for test scripts)
377cr_contains_str() {
378
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100379 if [ $# -eq 3 ] || [ $# -eq 4 ]; then
380 CR_SERVICE_PATH=$(__cr_get_service_path $1)
381 CR_ADAPTER=$CR_SERVICE_PATH
382 if [ $? -ne 0 ]; then
383 __print_err "<cr-path-id> missing or incorrect" $@
384 return 1
385 fi
386 __var_test "CR" "$CR_SERVICE_PATH/counter/" $2 "contain_str" $3 $4
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100387 return 0
388 else
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100389 __print_err "needs two or three args: <cr-path-id> <variable-name> <target-value> [ timeout ]"
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100390 return 1
391 fi
392}
393
394# Read a variable value from CR sim and send to stdout. Arg: <variable-name>
395cr_read() {
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100396 CR_SERVICE_PATH=$(__cr_get_service_path $1)
397 CR_ADAPTER=$CR_SERVICE_PATH
398 if [ $? -ne 0 ]; then
399 __print_err "<cr-path-id> missing or incorrect" $@
400 return 1
401 fi
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100402 echo "$(__do_curl $CR_SERVICE_PATH/counter/$1)"
403}
404
405# Function to configure write delay on callbacks
406# Delay given in seconds.
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100407# arg <response-code> <cr-path-id> <delay-in-sec>
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100408# (Function for test scripts)
409cr_delay_callback() {
410 __log_conf_start $@
411
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100412 if [ $# -ne 3 ]; then
413 __print_err "<response-code> <cr-path-id> <delay-in-sec>]" $@
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100414 return 1
415 fi
416
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100417 CR_SERVICE_PATH=$(__cr_get_service_path $2)
418 CR_ADAPTER=$CR_SERVICE_PATH
419 if [ $? -ne 0 ]; then
420 __print_err "<cr-path-id> missing or incorrect" $@
421 return 1
422 fi
423
424 res="$(__do_curl_to_api CR POST /forcedelay?delay=$3)"
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100425 status=${res:${#res}-3}
426
427 if [ $status -ne 200 ]; then
428 __log_conf_fail_status_code $1 $status
429 return 1
430 fi
431
432 __log_conf_ok
433 return 0
434}
435
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100436# CR API: Check the contents of all current ric sync events for one id from PMS
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100437# <response-code> <cr-path-id> <id> [ EMPTY | ( <ric-id> )+ ]
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100438# (Function for test scripts)
439cr_api_check_all_sync_events() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100440 __log_test_start $@
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100441
442 if [ "$PMS_VERSION" != "V2" ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100443 __log_test_fail_not_supported
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100444 return 1
445 fi
446
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100447 if [ $# -lt 3 ]; then
448 __print_err "<response-code> <cr-path-id> <id> [ EMPTY | ( <ric-id> )+ ]" $@
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100449 return 1
450 fi
451
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100452 CR_SERVICE_PATH=$(__cr_get_service_path $2)
453 CR_ADAPTER=$CR_SERVICE_PATH
454 if [ $? -ne 0 ]; then
455 __print_err "<cr-path-id> missing or incorrect" $@
456 return 1
457 fi
458
459 query="/get-all-events/"$3
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100460 res="$(__do_curl_to_api CR GET $query)"
461 status=${res:${#res}-3}
462
463 if [ $status -ne $1 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100464 __log_test_fail_status_code $1 $status
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100465 return 1
466 fi
467
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100468 if [ $# -gt 3 ]; then
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100469 body=${res:0:${#res}-3}
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100470 if [ $# -eq 4 ] && [ $4 == "EMPTY" ]; then
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100471 targetJson="["
472 else
473 targetJson="["
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100474 arr=(${@:4})
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100475
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100476 for ((i=0; i<$(($#-3)); i=i+1)); do
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100477
478 if [ "$targetJson" != "[" ]; then
479 targetJson=$targetJson","
480 fi
481 targetJson=$targetJson"{\"ric_id\":\"${arr[$i]}\",\"event_type\":\"AVAILABLE\"}"
482 done
483 fi
484
485 targetJson=$targetJson"]"
486 echo "TARGET JSON: $targetJson" >> $HTTPLOG
487 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
488
489 if [ $res -ne 0 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100490 __log_test_fail_body
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100491 return 1
492 fi
493 fi
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100494 __log_test_pass
495 return 0
496}
497
BjornMagnussonXA007b6452021-11-29 08:03:38 +0100498# CR API: Check the contents of all current status events for one id from ICS
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100499# <response-code> <cr-path-id> <id> [ EMPTY | ( <status> )+ ]
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100500# (Function for test scripts)
BjornMagnussonXA007b6452021-11-29 08:03:38 +0100501cr_api_check_all_ics_events() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100502 __log_test_start $@
503
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100504 if [ $# -lt 3 ]; then
505 __print_err "<response-code> <cr-path-id> <id> [ EMPTY | ( <status> )+ ]" $@
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100506 return 1
507 fi
508
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100509 CR_SERVICE_PATH=$(__cr_get_service_path $2)
510 CR_ADAPTER=$CR_SERVICE_PATH
511 if [ $? -ne 0 ]; then
512 __print_err "<cr-path-id> missing or incorrect" $@
513 return 1
514 fi
515
516 query="/get-all-events/"$3
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100517 res="$(__do_curl_to_api CR GET $query)"
518 status=${res:${#res}-3}
519
520 if [ $status -ne $1 ]; then
521 __log_test_fail_status_code $1 $status
522 return 1
523 fi
524
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100525 if [ $# -gt 3 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100526 body=${res:0:${#res}-3}
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100527 if [ $# -eq 4 ] && [ $4 == "EMPTY" ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100528 targetJson="["
529 else
530 targetJson="["
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100531 arr=(${@:4})
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100532
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100533 for ((i=0; i<$(($#-3)); i=i+1)); do
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100534
535 if [ "$targetJson" != "[" ]; then
536 targetJson=$targetJson","
537 fi
538 targetJson=$targetJson"{\"eiJobStatus\":\"${arr[$i]}\"}"
539 done
540 fi
541
542 targetJson=$targetJson"]"
543 echo "TARGET JSON: $targetJson" >> $HTTPLOG
544 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
545
546 if [ $res -ne 0 ]; then
547 __log_test_fail_body
548 return 1
549 fi
550 fi
551 __log_test_pass
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100552 return 0
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200553}
554
BjornMagnussonXA007b6452021-11-29 08:03:38 +0100555# CR API: Check the contents of all current type subscription events for one id from ICS
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100556# <response-code> <cr-path-id> <id> [ EMPTY | ( <type-id> <schema> <registration-status> )+ ]
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200557# (Function for test scripts)
BjornMagnussonXA007b6452021-11-29 08:03:38 +0100558cr_api_check_all_ics_subscription_events() {
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200559 __log_test_start $@
560
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100561 #Valid number of parameter 3,4,8,12
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200562 paramError=1
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100563 if [ $# -eq 3 ]; then
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200564 paramError=0
565 fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100566 if [ $# -eq 4 ] && [ "$4" == "EMPTY" ]; then
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200567 paramError=0
568 fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100569 variablecount=$(($#-3))
570 if [ $# -gt 4 ] && [ $(($variablecount%3)) -eq 0 ]; then
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200571 paramError=0
572 fi
573 if [ $paramError -eq 1 ]; then
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100574 __print_err "<response-code> <cr-path-id> <id> [ EMPTY | ( <type-id> <schema> <registration-status> )+ ]" $@
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200575 return 1
576 fi
577
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100578 CR_SERVICE_PATH=$(__cr_get_service_path $2)
579 CR_ADAPTER=$CR_SERVICE_PATH
580 if [ $? -ne 0 ]; then
581 __print_err "<cr-path-id> missing or incorrect" $@
582 return 1
583 fi
584
585 query="/get-all-events/"$3
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200586 res="$(__do_curl_to_api CR GET $query)"
587 status=${res:${#res}-3}
588
589 if [ $status -ne $1 ]; then
590 __log_test_fail_status_code $1 $status
591 return 1
592 fi
593
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100594 if [ $# -gt 3 ]; then
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200595 body=${res:0:${#res}-3}
596 targetJson="["
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100597 if [ $# -gt 4 ]; then
598 arr=(${@:4})
599 for ((i=0; i<$(($#-4)); i=i+3)); do
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200600 if [ "$targetJson" != "[" ]; then
601 targetJson=$targetJson","
602 fi
603 if [ -f ${arr[$i+1]} ]; then
604 jobfile=$(cat ${arr[$i+1]})
605 else
606 __log_test_fail_general "Job schema file "${arr[$i+1]}", does not exist"
607 return 1
608 fi
609 targetJson=$targetJson"{\"info_type_id\":\"${arr[$i]}\",\"job_data_schema\":$jobfile,\"status\":\"${arr[$i+2]}\"}"
610 done
611 fi
612 targetJson=$targetJson"]"
613
614 echo " TARGET JSON: $targetJson" >> $HTTPLOG
615 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
616
617 if [ $res -ne 0 ]; then
618 __log_test_fail_body
619 return 1
620 fi
621 fi
622
623 __log_test_pass
624 return 0
625}
626
627
628# CR API: Reset all events and counters
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100629# Arg: <cr-path-id>
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200630# (Function for test scripts)
631cr_api_reset() {
632 __log_conf_start $@
633
BjornMagnussonXA007b6452021-11-29 08:03:38 +0100634 if [ $# -ne 1 ]; then
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100635 __print_err "<cr-path-id>" $@
636 return 1
637 fi
638
639 CR_SERVICE_PATH=$(__cr_get_service_path $1)
640 CR_ADAPTER=$CR_SERVICE_PATH
641 if [ $? -ne 0 ]; then
642 __print_err "<cr-path-id> missing or incorrect" $@
643 return 1
644 fi
645
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200646 res="$(__do_curl_to_api CR GET /reset)"
647 status=${res:${#res}-3}
648
649 if [ $status -ne 200 ]; then
650 __log_conf_fail_status_code $1 $status
651 return 1
652 fi
653
654 __log_conf_ok
655 return 0
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100656}
657
658
659# CR API: Check the contents of all json events for path
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100660# <response-code> <cr-path-id> <topic-url> (EMPTY | <json-msg>+ )
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100661# (Function for test scripts)
662cr_api_check_all_genric_json_events() {
663 __log_test_start $@
664
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100665 if [ $# -lt 4 ]; then
666 __print_err "<response-code> <cr-path-id> <topic-url> (EMPTY | <json-msg>+ )" $@
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100667 return 1
668 fi
669
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100670 CR_SERVICE_PATH=$(__cr_get_service_path $2)
671 CR_ADAPTER=$CR_SERVICE_PATH
672 if [ $? -ne 0 ]; then
673 __print_err "<cr-path-id> missing or incorrect" $@
674 return 1
675 fi
676
677 query="/get-all-events/"$3
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100678 res="$(__do_curl_to_api CR GET $query)"
679 status=${res:${#res}-3}
680
681 if [ $status -ne $1 ]; then
682 __log_test_fail_status_code $1 $status
683 return 1
684 fi
685 body=${res:0:${#res}-3}
686 targetJson="["
687
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100688 if [ $4 != "EMPTY" ]; then
689 shift
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100690 shift
691 shift
692 while [ $# -gt 0 ]; do
693 if [ "$targetJson" != "[" ]; then
694 targetJson=$targetJson","
695 fi
696 targetJson=$targetJson$1
697 shift
698 done
699 fi
700 targetJson=$targetJson"]"
701
702 echo " TARGET JSON: $targetJson" >> $HTTPLOG
703 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
704
705 if [ $res -ne 0 ]; then
706 __log_test_fail_body
707 return 1
708 fi
709
710 __log_test_pass
711 return 0
712}
713
714
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100715# CR API: Check a single (oldest) json event (or none if empty) for path
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100716# <response-code> <cr-path-id> <topic-url> (EMPTY | <json-msg> )
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100717# (Function for test scripts)
718cr_api_check_single_genric_json_event() {
719 __log_test_start $@
720
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100721 if [ $# -ne 4 ]; then
722 __print_err "<response-code> <cr-path-id> <topic-url> (EMPTY | <json-msg> )" $@
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100723 return 1
724 fi
725
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100726 CR_SERVICE_PATH=$(__cr_get_service_path $2)
727 CR_ADAPTER=$CR_SERVICE_PATH
728 if [ $? -ne 0 ]; then
729 __print_err "<cr-path-id> missing or incorrect" $@
730 return 1
731 fi
732
733 query="/get-event/"$3
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100734 res="$(__do_curl_to_api CR GET $query)"
735 status=${res:${#res}-3}
736
737 if [ $status -ne $1 ]; then
738 __log_test_fail_status_code $1 $status
739 return 1
740 fi
741 body=${res:0:${#res}-3}
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100742 targetJson=$4
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100743
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100744 if [ $targetJson == "EMPTY" ] && [ ${#body} -ne 0 ]; then
745 __log_test_fail_body
746 return 1
747 fi
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100748 echo " TARGET JSON: $targetJson" >> $HTTPLOG
749 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
750
751 if [ $res -ne 0 ]; then
752 __log_test_fail_body
753 return 1
754 fi
755
756 __log_test_pass
757 return 0
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100758}
759
760# CR API: Check a single (oldest) json in md5 format (or none if empty) for path.
761# Note that if a json message is given, it shall be compact, no ws except inside string.
762# The MD5 will generate different hash if ws is present or not in otherwise equivalent json
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100763# arg: <response-code> <cr-path-id> <topic-url> (EMPTY | <data-msg> )
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100764# (Function for test scripts)
765cr_api_check_single_genric_event_md5() {
766 __log_test_start $@
767
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100768 if [ $# -ne 4 ]; then
769 __print_err "<response-code> <cr-path-id> <topic-url> (EMPTY | <data-msg> )" $@
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100770 return 1
771 fi
772
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100773 CR_SERVICE_PATH=$(__cr_get_service_path $2)
774 CR_ADAPTER=$CR_SERVICE_PATH
775 if [ $? -ne 0 ]; then
776 __print_err "<cr-path-id> missing or incorrect" $@
777 return 1
778 fi
779
780 query="/get-event/"$3
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100781 res="$(__do_curl_to_api CR GET $query)"
782 status=${res:${#res}-3}
783
784 if [ $status -ne $1 ]; then
785 __log_test_fail_status_code $1 $status
786 return 1
787 fi
788 body=${res:0:${#res}-3}
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100789 if [ $4 == "EMPTY" ]; then
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100790 if [ ${#body} -ne 0 ]; then
791 __log_test_fail_body
792 return 1
793 else
794 __log_test_pass
795 return 0
796 fi
797 fi
798 command -v md5 > /dev/null # Mac
799 if [ $? -eq 0 ]; then
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100800 targetMd5=$(echo -n "$4" | md5)
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100801 else
802 command -v md5sum > /dev/null # Linux
803 if [ $? -eq 0 ]; then
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100804 targetMd5=$(echo -n "$4" | md5sum | cut -d' ' -f 1) # Need to cut additional info printed by cmd
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100805 else
806 __log_test_fail_general "Command md5 nor md5sum is available"
807 return 1
808 fi
809 fi
810 targetMd5="\""$targetMd5"\"" #Quotes needed
811
812 echo " TARGET MD5 hash: $targetMd5" >> $HTTPLOG
813
814 if [ "$body" != "$targetMd5" ]; then
815 __log_test_fail_body
816 return 1
817 fi
818
819 __log_test_pass
820 return 0
821}
822
823# CR API: Check a single (oldest) event in md5 format (or none if empty) for path.
824# Note that if a file with json message is given, the json shall be compact, no ws except inside string and not newlines.
825# The MD5 will generate different hash if ws/newlines is present or not in otherwise equivalent json
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100826# arg: <response-code> <cr-path-id> <topic-url> (EMPTY | <data-file> )
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100827# (Function for test scripts)
828cr_api_check_single_genric_event_md5_file() {
829 __log_test_start $@
830
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100831 if [ $# -ne 4 ]; then
832 __print_err "<response-code> <cr-path-id> <topic-url> (EMPTY | <data-file> )" $@
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100833 return 1
834 fi
835
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100836 CR_SERVICE_PATH=$(__cr_get_service_path $2)
837 CR_ADAPTER=$CR_SERVICE_PATH
838 if [ $? -ne 0 ]; then
839 __print_err "<cr-path-id> missing or incorrect" $@
840 return 1
841 fi
842
843 query="/get-event/"$3
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100844 res="$(__do_curl_to_api CR GET $query)"
845 status=${res:${#res}-3}
846
847 if [ $status -ne $1 ]; then
848 __log_test_fail_status_code $1 $status
849 return 1
850 fi
851 body=${res:0:${#res}-3}
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100852 if [ $4 == "EMPTY" ]; then
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100853 if [ ${#body} -ne 0 ]; then
854 __log_test_fail_body
855 return 1
856 else
857 __log_test_pass
858 return 0
859 fi
860 fi
861
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100862 if [ ! -f $4 ]; then
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100863 __log_test_fail_general "File $3 does not exist"
864 return 1
865 fi
866
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100867 filedata=$(cat $4)
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100868
869 command -v md5 > /dev/null # Mac
870 if [ $? -eq 0 ]; then
871 targetMd5=$(echo -n "$filedata" | md5)
872 else
873 command -v md5sum > /dev/null # Linux
874 if [ $? -eq 0 ]; then
875 targetMd5=$(echo -n "$filedata" | md5sum | cut -d' ' -f 1) # Need to cut additional info printed by cmd
876 else
877 __log_test_fail_general "Command md5 nor md5sum is available"
878 return 1
879 fi
880 fi
881 targetMd5="\""$targetMd5"\"" #Quotes needed
882
883 echo " TARGET MD5 hash: $targetMd5" >> $HTTPLOG
884
885 if [ "$body" != "$targetMd5" ]; then
886 __log_test_fail_body
887 return 1
888 fi
889
890 __log_test_pass
891 return 0
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100892}