blob: a9acd6e5d12f61e6e71f74342b886cb0687fbb68 [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
125 echo -n " CR_$CR_INSTANCE ${CR_APP_NAME}_cr_$CR_INSTANCE "
126 fi
127 done
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100128}
129
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100130#######################################################
131
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100132################
133### CR functions
134################
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100135
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100136#Var to hold the current number of CR instances
137CR_APP_COUNT=1
138MAX_CR_APP_COUNT=10
139
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100140# Set http as the protocol to use for all communication to the Dmaap adapter
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100141# args: -
142# (Function for test scripts)
143use_cr_http() {
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100144 __cr_set_protocoll "http" $CR_INTERNAL_PORT $CR_EXTERNAL_PORT
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100145}
146
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100147# Set https as the protocol to use for all communication to the Dmaap adapter
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100148# args: -
149# (Function for test scripts)
150use_cr_https() {
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100151 __cr_set_protocoll "https" $CR_INTERNAL_SECURE_PORT $CR_EXTERNAL_SECURE_PORT
152}
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100153
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100154# Setup paths to svc/container for internal and external access
155# args: <protocol> <internal-port> <external-port>
156__cr_set_protocoll() {
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100157
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100158 echo -e $BOLD"$CR_DISPLAY_NAME protocol setting"$EBOLD
159 echo -e " Using $BOLD http $EBOLD towards $CR_DISPLAY_NAME"
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100160 ## Access to Dmaap adapter
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100161 for ((CR_INSTANCE=0; CR_INSTANCE<$MAX_CR_APP_COUNT; CR_INSTANCE++ )); do
162 CR_DOCKER_INSTANCE=$(($CR_INSTANCE+1))
163 # CR_SERVICE_PATH is the base path to cr
164 __CR_SERVICE_PATH=$1"://"$CR_APP_NAME"_cr_"${CR_DOCKER_INSTANCE}":"$2 # docker access, container->container and script->container via proxy
165 if [ $RUNMODE == "KUBE" ]; then
166 __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
167 fi
168 export CR_SERVICE_PATH"_"${CR_INSTANCE}=$__CR_SERVICE_PATH
169 # Service paths are used in test script to provide callbacck urls to app
170 export CR_SERVICE_MR_PATH"_"${CR_INSTANCE}=$__CR_SERVICE_PATH$CR_APP_CALLBACK_MR #Only for messages from dmaap adapter/mediator
171 export CR_SERVICE_TEXT_PATH"_"${CR_INSTANCE}=$__CR_SERVICE_PATH$CR_APP_CALLBACK_TEXT #Callbacks for text payload
172 export CR_SERVICE_APP_PATH"_"${CR_INSTANCE}=$__CR_SERVICE_PATH$CR_APP_CALLBACK #For general callbacks from apps
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100173
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100174 if [ $CR_INSTANCE -eq 0 ]; then
175 # CR_ADAPTER used for switching between REST and DMAAP (only REST supported currently)
176 # CR_ADDAPTER need to be set before each call to CR....only set for instance 0 here
177 CR_ADAPTER_TYPE="REST"
178 CR_ADAPTER=$__CR_SERVICE_PATH
179 fi
180 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100181 echo ""
182}
183
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100184# Export env vars for config files, docker compose and kube resources
185# args: <proxy-flag>
186__cr_export_vars() {
187 export CR_APP_NAME
188 export CR_DISPLAY_NAME
189
190 export KUBE_SIM_NAMESPACE
191 export DOCKER_SIM_NWNAME
192
193 export CR_IMAGE
194
195 export CR_INTERNAL_PORT
196 export CR_INTERNAL_SECURE_PORT
197 export CR_EXTERNAL_PORT
198 export CR_EXTERNAL_SECURE_PORT
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100199
200 export CR_APP_COUNT
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100201}
202
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100203# Start the Callback reciver in the simulator group
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100204# args: <app-count>
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100205# (Function for test scripts)
206start_cr() {
207
208 echo -e $BOLD"Starting $CR_DISPLAY_NAME"$EBOLD
209
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100210 if [ $# -ne 1 ]; then
211 echo -e $RED" Number of CR instances missing, usage: start_cr <app-count>"$ERED
212 exit 1
213 fi
214 if [ $1 -lt 1 ] || [ $1 -gt 10 ]; then
215 echo -e $RED" Number of CR shall be 1...10, usage: start_cr <app-count>"$ERED
216 exit 1
217 fi
218 export CR_APP_COUNT=$1
219
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100220 if [ $RUNMODE == "KUBE" ]; then
221
222 # Check if app shall be fully managed by the test script
223 __check_included_image "CR"
224 retcode_i=$?
225
226 # Check if app shall only be used by the testscipt
227 __check_prestarted_image "CR"
228 retcode_p=$?
229
230 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
231 echo -e $RED"The $CR_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
232 echo -e $RED"The $CR_APP_NAME will not be started"$ERED
233 exit
234 fi
235 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
236 echo -e $RED"The $CR_APP_NAME app is included both as managed and prestarted in this test script"$ERED
237 echo -e $RED"The $CR_APP_NAME will not be started"$ERED
238 exit
239 fi
240
241 # Check if app shall be used - not managed - by the test script
242 if [ $retcode_p -eq 0 ]; then
243 echo -e " Using existing $CR_APP_NAME deployment and service"
244 echo " Setting CR replicas=1"
245 __kube_scale deployment $CR_APP_NAME $KUBE_SIM_NAMESPACE 1
246 fi
247
248 if [ $retcode_i -eq 0 ]; then
249 echo -e " Creating $CR_APP_NAME deployment and service"
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100250
251 __cr_export_vars
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100252
253 __kube_create_namespace $KUBE_SIM_NAMESPACE
254
255 # Create service
256 input_yaml=$SIM_GROUP"/"$CR_COMPOSE_DIR"/"svc.yaml
257 output_yaml=$PWD/tmp/cr_svc.yaml
258 __kube_create_instance service $CR_APP_NAME $input_yaml $output_yaml
259
260 # Create app
261 input_yaml=$SIM_GROUP"/"$CR_COMPOSE_DIR"/"app.yaml
262 output_yaml=$PWD/tmp/cr_app.yaml
263 __kube_create_instance app $CR_APP_NAME $input_yaml $output_yaml
264
265 fi
266
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100267 for ((CR_INSTANCE=0; CR_INSTANCE<$CR_APP_COUNT; CR_INSTANCE++ )); do
268 __dynvar="CR_SERVICE_PATH_"$CR_INSTANCE
269 __cr_app_name=$CR_APP_NAME"-"$CR_INSTANCE
270 __check_service_start $__cr_app_name ${!__dynvar}$CR_ALIVE_URL
271 result=$(__do_curl ${!__dynvar}/reset)
272 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100273
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100274 else
275 # Check if docker app shall be fully managed by the test script
276 __check_included_image 'CR'
277 if [ $? -eq 1 ]; then
278 echo -e $RED"The Callback Receiver app is not included in this test script"$ERED
279 echo -e $RED"The Callback Receiver will not be started"$ERED
280 exit
281 fi
282
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100283 __cr_export_vars
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100284
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100285 app_data=""
286 cntr=1
287 while [ $cntr -le $CR_APP_COUNT ]; do
288 app=$CR_APP_NAME"_cr_"$cntr
289 app_data="$app_data $app"
290 let cntr=cntr+1
291 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100292
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100293 echo "COMPOSE_PROJECT_NAME="$CR_APP_NAME > $SIM_GROUP/$CR_COMPOSE_DIR/.env
294
295 __start_container $CR_COMPOSE_DIR "" NODOCKERARGS $CR_APP_COUNT $app_data
296
297 cntr=1 #Counter for docker instance, starts on 1
298 cntr2=0 #Couter for env var name, starts with 0 to be compablible with kube
299 while [ $cntr -le $CR_APP_COUNT ]; do
300 app=$CR_APP_NAME"_cr_"$cntr
301 __dynvar="CR_SERVICE_PATH_"$cntr2
302 __check_service_start $app ${!__dynvar}$CR_ALIVE_URL
303 let cntr=cntr+1
304 let cntr2=cntr2+1
305 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100306 fi
307 echo ""
308}
309
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100310#Convert a cr path id to the value of the environment var holding the url
311# arg: <cr-path-id>
312# returns: <base-url-to-the-app>
313__cr_get_service_path(){
314 if [ $# -ne 1 ]; then
315 echo "DUMMY"
316 return 1
317 fi
318 if [ $1 -lt 0 ] || [ $1 -ge $MAX_CR_APP_COUNT ]; then
319 echo "DUMMY"
320 return 1
321 fi
322 __dynvar="CR_SERVICE_PATH_"$1
323 echo ${!__dynvar}
324 return 0
325}
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100326
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100327# Tests if a variable value in the CR is equal to a target value and and optional timeout.
328# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
329# equal to the target or not.
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100330# 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 +0100331# before setting pass or fail depending on if the variable value becomes equal to the target
332# value or not.
333# (Function for test scripts)
334cr_equal() {
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100335 if [ $# -eq 3 ] || [ $# -eq 4 ]; then
336 CR_SERVICE_PATH=$(__cr_get_service_path $1)
337 CR_ADAPTER=$CR_SERVICE_PATH
338 if [ $? -ne 0 ]; then
339 __print_err "<cr-path-id> missing or incorrect" $@
340 return 1
341 fi
342 __var_test "CR" "$CR_SERVICE_PATH/counter/" $2 "=" $3 $4
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100343 else
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100344 __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 +0100345 fi
346}
347
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100348# Tests if a variable value in the CR contains the target string and and optional timeout
349# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable contains
350# the target or not.
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100351# 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 +0100352# before setting pass or fail depending on if the variable value contains the target
353# value or not.
354# (Function for test scripts)
355cr_contains_str() {
356
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 "contain_str" $3 $4
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100365 return 0
366 else
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100367 __print_err "needs two or three args: <cr-path-id> <variable-name> <target-value> [ timeout ]"
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100368 return 1
369 fi
370}
371
372# Read a variable value from CR sim and send to stdout. Arg: <variable-name>
373cr_read() {
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100374 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
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100380 echo "$(__do_curl $CR_SERVICE_PATH/counter/$1)"
381}
382
383# Function to configure write delay on callbacks
384# Delay given in seconds.
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100385# arg <response-code> <cr-path-id> <delay-in-sec>
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100386# (Function for test scripts)
387cr_delay_callback() {
388 __log_conf_start $@
389
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100390 if [ $# -ne 3 ]; then
391 __print_err "<response-code> <cr-path-id> <delay-in-sec>]" $@
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100392 return 1
393 fi
394
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100395 CR_SERVICE_PATH=$(__cr_get_service_path $2)
396 CR_ADAPTER=$CR_SERVICE_PATH
397 if [ $? -ne 0 ]; then
398 __print_err "<cr-path-id> missing or incorrect" $@
399 return 1
400 fi
401
402 res="$(__do_curl_to_api CR POST /forcedelay?delay=$3)"
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100403 status=${res:${#res}-3}
404
405 if [ $status -ne 200 ]; then
406 __log_conf_fail_status_code $1 $status
407 return 1
408 fi
409
410 __log_conf_ok
411 return 0
412}
413
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100414# CR API: Check the contents of all current ric sync events for one id from PMS
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100415# <response-code> <cr-path-id> <id> [ EMPTY | ( <ric-id> )+ ]
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100416# (Function for test scripts)
417cr_api_check_all_sync_events() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100418 __log_test_start $@
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100419
420 if [ "$PMS_VERSION" != "V2" ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100421 __log_test_fail_not_supported
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100422 return 1
423 fi
424
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100425 if [ $# -lt 3 ]; then
426 __print_err "<response-code> <cr-path-id> <id> [ EMPTY | ( <ric-id> )+ ]" $@
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100427 return 1
428 fi
429
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100430 CR_SERVICE_PATH=$(__cr_get_service_path $2)
431 CR_ADAPTER=$CR_SERVICE_PATH
432 if [ $? -ne 0 ]; then
433 __print_err "<cr-path-id> missing or incorrect" $@
434 return 1
435 fi
436
437 query="/get-all-events/"$3
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100438 res="$(__do_curl_to_api CR GET $query)"
439 status=${res:${#res}-3}
440
441 if [ $status -ne $1 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100442 __log_test_fail_status_code $1 $status
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100443 return 1
444 fi
445
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100446 if [ $# -gt 3 ]; then
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100447 body=${res:0:${#res}-3}
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100448 if [ $# -eq 4 ] && [ $4 == "EMPTY" ]; then
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100449 targetJson="["
450 else
451 targetJson="["
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100452 arr=(${@:4})
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100453
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100454 for ((i=0; i<$(($#-3)); i=i+1)); do
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100455
456 if [ "$targetJson" != "[" ]; then
457 targetJson=$targetJson","
458 fi
459 targetJson=$targetJson"{\"ric_id\":\"${arr[$i]}\",\"event_type\":\"AVAILABLE\"}"
460 done
461 fi
462
463 targetJson=$targetJson"]"
464 echo "TARGET JSON: $targetJson" >> $HTTPLOG
465 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
466
467 if [ $res -ne 0 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100468 __log_test_fail_body
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100469 return 1
470 fi
471 fi
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100472 __log_test_pass
473 return 0
474}
475
476# CR API: Check the contents of all current status events for one id from ECS
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100477# <response-code> <cr-path-id> <id> [ EMPTY | ( <status> )+ ]
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100478# (Function for test scripts)
479cr_api_check_all_ecs_events() {
480 __log_test_start $@
481
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100482 if [ $# -lt 3 ]; then
483 __print_err "<response-code> <cr-path-id> <id> [ EMPTY | ( <status> )+ ]" $@
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100484 return 1
485 fi
486
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100487 CR_SERVICE_PATH=$(__cr_get_service_path $2)
488 CR_ADAPTER=$CR_SERVICE_PATH
489 if [ $? -ne 0 ]; then
490 __print_err "<cr-path-id> missing or incorrect" $@
491 return 1
492 fi
493
494 query="/get-all-events/"$3
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100495 res="$(__do_curl_to_api CR GET $query)"
496 status=${res:${#res}-3}
497
498 if [ $status -ne $1 ]; then
499 __log_test_fail_status_code $1 $status
500 return 1
501 fi
502
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100503 if [ $# -gt 3 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100504 body=${res:0:${#res}-3}
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100505 if [ $# -eq 4 ] && [ $4 == "EMPTY" ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100506 targetJson="["
507 else
508 targetJson="["
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100509 arr=(${@:4})
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100510
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100511 for ((i=0; i<$(($#-3)); i=i+1)); do
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100512
513 if [ "$targetJson" != "[" ]; then
514 targetJson=$targetJson","
515 fi
516 targetJson=$targetJson"{\"eiJobStatus\":\"${arr[$i]}\"}"
517 done
518 fi
519
520 targetJson=$targetJson"]"
521 echo "TARGET JSON: $targetJson" >> $HTTPLOG
522 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
523
524 if [ $res -ne 0 ]; then
525 __log_test_fail_body
526 return 1
527 fi
528 fi
529 __log_test_pass
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100530 return 0
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200531}
532
533# CR API: Check the contents of all current type subscription events for one id from ECS
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100534# <response-code> <cr-path-id> <id> [ EMPTY | ( <type-id> <schema> <registration-status> )+ ]
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200535# (Function for test scripts)
536cr_api_check_all_ecs_subscription_events() {
537 __log_test_start $@
538
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100539 #Valid number of parameter 3,4,8,12
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200540 paramError=1
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100541 if [ $# -eq 3 ]; then
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200542 paramError=0
543 fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100544 if [ $# -eq 4 ] && [ "$4" == "EMPTY" ]; then
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200545 paramError=0
546 fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100547 variablecount=$(($#-3))
548 if [ $# -gt 4 ] && [ $(($variablecount%3)) -eq 0 ]; then
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200549 paramError=0
550 fi
551 if [ $paramError -eq 1 ]; then
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100552 __print_err "<response-code> <cr-path-id> <id> [ EMPTY | ( <type-id> <schema> <registration-status> )+ ]" $@
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200553 return 1
554 fi
555
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100556 CR_SERVICE_PATH=$(__cr_get_service_path $2)
557 CR_ADAPTER=$CR_SERVICE_PATH
558 if [ $? -ne 0 ]; then
559 __print_err "<cr-path-id> missing or incorrect" $@
560 return 1
561 fi
562
563 query="/get-all-events/"$3
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200564 res="$(__do_curl_to_api CR GET $query)"
565 status=${res:${#res}-3}
566
567 if [ $status -ne $1 ]; then
568 __log_test_fail_status_code $1 $status
569 return 1
570 fi
571
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100572 if [ $# -gt 3 ]; then
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200573 body=${res:0:${#res}-3}
574 targetJson="["
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100575 if [ $# -gt 4 ]; then
576 arr=(${@:4})
577 for ((i=0; i<$(($#-4)); i=i+3)); do
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200578 if [ "$targetJson" != "[" ]; then
579 targetJson=$targetJson","
580 fi
581 if [ -f ${arr[$i+1]} ]; then
582 jobfile=$(cat ${arr[$i+1]})
583 else
584 __log_test_fail_general "Job schema file "${arr[$i+1]}", does not exist"
585 return 1
586 fi
587 targetJson=$targetJson"{\"info_type_id\":\"${arr[$i]}\",\"job_data_schema\":$jobfile,\"status\":\"${arr[$i+2]}\"}"
588 done
589 fi
590 targetJson=$targetJson"]"
591
592 echo " TARGET JSON: $targetJson" >> $HTTPLOG
593 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
594
595 if [ $res -ne 0 ]; then
596 __log_test_fail_body
597 return 1
598 fi
599 fi
600
601 __log_test_pass
602 return 0
603}
604
605
606# CR API: Reset all events and counters
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100607# Arg: <cr-path-id>
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200608# (Function for test scripts)
609cr_api_reset() {
610 __log_conf_start $@
611
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100612 if [ $# -ne 0 ]; then
613 __print_err "<cr-path-id>" $@
614 return 1
615 fi
616
617 CR_SERVICE_PATH=$(__cr_get_service_path $1)
618 CR_ADAPTER=$CR_SERVICE_PATH
619 if [ $? -ne 0 ]; then
620 __print_err "<cr-path-id> missing or incorrect" $@
621 return 1
622 fi
623
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200624 res="$(__do_curl_to_api CR GET /reset)"
625 status=${res:${#res}-3}
626
627 if [ $status -ne 200 ]; then
628 __log_conf_fail_status_code $1 $status
629 return 1
630 fi
631
632 __log_conf_ok
633 return 0
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100634}
635
636
637# CR API: Check the contents of all json events for path
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100638# <response-code> <cr-path-id> <topic-url> (EMPTY | <json-msg>+ )
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100639# (Function for test scripts)
640cr_api_check_all_genric_json_events() {
641 __log_test_start $@
642
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100643 if [ $# -lt 4 ]; then
644 __print_err "<response-code> <cr-path-id> <topic-url> (EMPTY | <json-msg>+ )" $@
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100645 return 1
646 fi
647
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100648 CR_SERVICE_PATH=$(__cr_get_service_path $2)
649 CR_ADAPTER=$CR_SERVICE_PATH
650 if [ $? -ne 0 ]; then
651 __print_err "<cr-path-id> missing or incorrect" $@
652 return 1
653 fi
654
655 query="/get-all-events/"$3
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100656 res="$(__do_curl_to_api CR GET $query)"
657 status=${res:${#res}-3}
658
659 if [ $status -ne $1 ]; then
660 __log_test_fail_status_code $1 $status
661 return 1
662 fi
663 body=${res:0:${#res}-3}
664 targetJson="["
665
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100666 if [ $4 != "EMPTY" ]; then
667 shift
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100668 shift
669 shift
670 while [ $# -gt 0 ]; do
671 if [ "$targetJson" != "[" ]; then
672 targetJson=$targetJson","
673 fi
674 targetJson=$targetJson$1
675 shift
676 done
677 fi
678 targetJson=$targetJson"]"
679
680 echo " TARGET JSON: $targetJson" >> $HTTPLOG
681 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
682
683 if [ $res -ne 0 ]; then
684 __log_test_fail_body
685 return 1
686 fi
687
688 __log_test_pass
689 return 0
690}
691
692
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100693# CR API: Check a single (oldest) json event (or none if empty) for path
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100694# <response-code> <cr-path-id> <topic-url> (EMPTY | <json-msg> )
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100695# (Function for test scripts)
696cr_api_check_single_genric_json_event() {
697 __log_test_start $@
698
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100699 if [ $# -ne 4 ]; then
700 __print_err "<response-code> <cr-path-id> <topic-url> (EMPTY | <json-msg> )" $@
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100701 return 1
702 fi
703
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100704 CR_SERVICE_PATH=$(__cr_get_service_path $2)
705 CR_ADAPTER=$CR_SERVICE_PATH
706 if [ $? -ne 0 ]; then
707 __print_err "<cr-path-id> missing or incorrect" $@
708 return 1
709 fi
710
711 query="/get-event/"$3
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100712 res="$(__do_curl_to_api CR GET $query)"
713 status=${res:${#res}-3}
714
715 if [ $status -ne $1 ]; then
716 __log_test_fail_status_code $1 $status
717 return 1
718 fi
719 body=${res:0:${#res}-3}
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100720 targetJson=$4
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100721
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100722 if [ $targetJson == "EMPTY" ] && [ ${#body} -ne 0 ]; then
723 __log_test_fail_body
724 return 1
725 fi
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100726 echo " TARGET JSON: $targetJson" >> $HTTPLOG
727 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
728
729 if [ $res -ne 0 ]; then
730 __log_test_fail_body
731 return 1
732 fi
733
734 __log_test_pass
735 return 0
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100736}
737
738# CR API: Check a single (oldest) json in md5 format (or none if empty) for path.
739# Note that if a json message is given, it shall be compact, no ws except inside string.
740# The MD5 will generate different hash if ws is present or not in otherwise equivalent json
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100741# arg: <response-code> <cr-path-id> <topic-url> (EMPTY | <data-msg> )
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100742# (Function for test scripts)
743cr_api_check_single_genric_event_md5() {
744 __log_test_start $@
745
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100746 if [ $# -ne 4 ]; then
747 __print_err "<response-code> <cr-path-id> <topic-url> (EMPTY | <data-msg> )" $@
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100748 return 1
749 fi
750
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100751 CR_SERVICE_PATH=$(__cr_get_service_path $2)
752 CR_ADAPTER=$CR_SERVICE_PATH
753 if [ $? -ne 0 ]; then
754 __print_err "<cr-path-id> missing or incorrect" $@
755 return 1
756 fi
757
758 query="/get-event/"$3
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100759 res="$(__do_curl_to_api CR GET $query)"
760 status=${res:${#res}-3}
761
762 if [ $status -ne $1 ]; then
763 __log_test_fail_status_code $1 $status
764 return 1
765 fi
766 body=${res:0:${#res}-3}
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100767 if [ $4 == "EMPTY" ]; then
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100768 if [ ${#body} -ne 0 ]; then
769 __log_test_fail_body
770 return 1
771 else
772 __log_test_pass
773 return 0
774 fi
775 fi
776 command -v md5 > /dev/null # Mac
777 if [ $? -eq 0 ]; then
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100778 targetMd5=$(echo -n "$4" | md5)
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100779 else
780 command -v md5sum > /dev/null # Linux
781 if [ $? -eq 0 ]; then
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100782 targetMd5=$(echo -n "$4" | md5sum | cut -d' ' -f 1) # Need to cut additional info printed by cmd
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100783 else
784 __log_test_fail_general "Command md5 nor md5sum is available"
785 return 1
786 fi
787 fi
788 targetMd5="\""$targetMd5"\"" #Quotes needed
789
790 echo " TARGET MD5 hash: $targetMd5" >> $HTTPLOG
791
792 if [ "$body" != "$targetMd5" ]; then
793 __log_test_fail_body
794 return 1
795 fi
796
797 __log_test_pass
798 return 0
799}
800
801# CR API: Check a single (oldest) event in md5 format (or none if empty) for path.
802# Note that if a file with json message is given, the json shall be compact, no ws except inside string and not newlines.
803# The MD5 will generate different hash if ws/newlines is present or not in otherwise equivalent json
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100804# arg: <response-code> <cr-path-id> <topic-url> (EMPTY | <data-file> )
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100805# (Function for test scripts)
806cr_api_check_single_genric_event_md5_file() {
807 __log_test_start $@
808
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100809 if [ $# -ne 4 ]; then
810 __print_err "<response-code> <cr-path-id> <topic-url> (EMPTY | <data-file> )" $@
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100811 return 1
812 fi
813
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100814 CR_SERVICE_PATH=$(__cr_get_service_path $2)
815 CR_ADAPTER=$CR_SERVICE_PATH
816 if [ $? -ne 0 ]; then
817 __print_err "<cr-path-id> missing or incorrect" $@
818 return 1
819 fi
820
821 query="/get-event/"$3
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100822 res="$(__do_curl_to_api CR GET $query)"
823 status=${res:${#res}-3}
824
825 if [ $status -ne $1 ]; then
826 __log_test_fail_status_code $1 $status
827 return 1
828 fi
829 body=${res:0:${#res}-3}
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100830 if [ $4 == "EMPTY" ]; then
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100831 if [ ${#body} -ne 0 ]; then
832 __log_test_fail_body
833 return 1
834 else
835 __log_test_pass
836 return 0
837 fi
838 fi
839
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100840 if [ ! -f $4 ]; then
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100841 __log_test_fail_general "File $3 does not exist"
842 return 1
843 fi
844
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100845 filedata=$(cat $4)
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100846
847 command -v md5 > /dev/null # Mac
848 if [ $? -eq 0 ]; then
849 targetMd5=$(echo -n "$filedata" | md5)
850 else
851 command -v md5sum > /dev/null # Linux
852 if [ $? -eq 0 ]; then
853 targetMd5=$(echo -n "$filedata" | md5sum | cut -d' ' -f 1) # Need to cut additional info printed by cmd
854 else
855 __log_test_fail_general "Command md5 nor md5sum is available"
856 return 1
857 fi
858 fi
859 targetMd5="\""$targetMd5"\"" #Quotes needed
860
861 echo " TARGET MD5 hash: $targetMd5" >> $HTTPLOG
862
863 if [ "$body" != "$targetMd5" ]; then
864 __log_test_fail_body
865 return 1
866 fi
867
868 __log_test_pass
869 return 0
BjornMagnussonXA2791e082020-11-12 00:52:08 +0100870}