Update DFC tests to use file based conifg
[integration.git] / test / mocks / datafilecollector-testharness / common / testcase_common.sh
1 #!/bin/bash
2 #
3 # Modifications copyright (C) 2021 Nokia. All rights reserved
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11
12 . ../common/test_env.sh
13
14
15
16 echo "Test case started as: ${BASH_SOURCE[$i+1]} "$1 $2
17
18 # Script containing all functions needed for auto testing of test cases
19 # Arg: local [<image-tag>] ]| remote [<image-tag>] ]| remote-remove [<image-tag>]] | manual-container | manual-app
20
21 STARTED_DFCS="" #DFC app names added to this var to keep track of started container in the script
22 START_ARG=$1
23 IMAGE_TAG="latest"
24
25 if [ $# -gt 1 ]; then
26         if [[ "$2" =~ ^1.1.* ]]; then
27                 echo "This version of auto-test does not support DFC image version of 1.1.X"
28                 exit 1
29         fi
30         IMAGE_TAG=$2
31 fi
32
33 if [ $# -lt 1 ] || [ $# -gt 2 ]; then
34         echo "Expected arg: local [<image-tag>] ]| remote [<image-tag>] ]| remote-remove [<image-tag>]] | manual-container | manual-app"
35         exit 1
36 elif [ $1 == "local" ]; then
37         if [ -z $DFC_LOCAL_IMAGE ]; then
38                 echo "DFC_LOCAL_IMAGE not set in test_env"
39                 exit 1
40         fi
41         DFC_IMAGE=$DFC_LOCAL_IMAGE":"$IMAGE_TAG
42 elif [ $1 == "remote" ] || [ $1 == "remote-remove" ]; then
43         if [ -z $DFC_REMOTE_IMAGE ]; then
44                 echo "DFC_REMOTE_IMAGE not set in test_env"
45                 exit 1
46         fi
47         DFC_IMAGE=$DFC_REMOTE_IMAGE":"$IMAGE_TAG
48 elif [ $1 == "manual-container" ] && [ $# -eq 1 ]; then
49         echo "DFC is expected to be started manually, when prompted,  as a container with name 'dfc_app'<index> with and index in the range from 0 to '${DFC_MAX_IDX}'"
50 elif [ $1 == "manual-app" ] && [ $# -eq 1 ]; then
51         echo "DFC is expected to be started manually, when prompted, as a java application"
52 else
53         echo "Expected arg: local [<image-tag>] ]| remote [<image-tag>] ]| remote-remove [<image-tag>]] | manual-container | manual-app"
54         exit 1
55 fi
56
57 # Set a description string for the test case
58 if [ -z "$TC_ONELINE_DESCR" ]; then
59         TC_ONELINE_DESCR="<no-description>"
60         echo "No test case description found, TC_ONELINE_DESCR should be set on in the test script , using "$TC_ONELINE_DESCR
61 fi
62
63 # Counter for test suites
64 if [ -f .tmp_tcsuite_ctr ]; then
65         tmpval=$(< .tmp_tcsuite_ctr)
66         ((tmpval++))
67         echo $tmpval > .tmp_tcsuite_ctr
68 fi
69
70 # Create a test case id, ATC (Auto Test Case), from the name of the test case script.
71 # FTC1.sh -> ATC == FTC1
72 ATC=$(basename "${BASH_SOURCE[$i+1]}" .sh)
73
74 # Create the logs dir if not already created in the current dir
75 if [ ! -d "logs" ]; then
76     mkdir logs
77 fi
78
79 TESTLOGS=$PWD/logs
80
81 # Create a log dir for the test case
82 mkdir -p $TESTLOGS/$ATC
83
84 # Clear the log dir for the test case
85 rm $TESTLOGS/$ATC/*.log &> /dev/null
86
87 # Log all output from the test case to a TC log
88 TCLOG=$TESTLOGS/$ATC/TC.log
89 exec &>  >(tee ${TCLOG})
90
91 #Variables for counting tests as well as passed and failed tests
92 RES_TEST=0
93 RES_PASS=0
94 RES_FAIL=0
95 TCTEST_START=$SECONDS
96
97 echo "-------------------------------------------------------------------------------------------------"
98 echo "-----------------------------------      Test case: "$ATC
99 echo "-----------------------------------      Started:   "$(date)
100 echo "-------------------------------------------------------------------------------------------------"
101 echo "-- Description: "$TC_ONELINE_DESCR
102 echo "-------------------------------------------------------------------------------------------------"
103 echo "-----------------------------------      Test case setup      -----------------------------------"
104
105 if [ -z "$SIM_GROUP" ]; then
106                 SIM_GROUP=$PWD/../simulator-group
107                 if [ ! -d  $SIM_GROUP ]; then
108                         echo "Trying to set env var SIM_GROUP to dir 'simulator-group' in the integration repo, but failed."
109                         echo "Please set the SIM_GROUP manually in the test_env.sh"
110                         exit 1
111                 else
112                         echo "SIM_GROUP auto set to: " $SIM_GROUP
113                 fi
114 elif [ $SIM_GROUP = *simulator_group ]; then
115                         echo "Env var SIM_GROUP does not seem to point to dir 'simulator-group' in the integration repo, check test_env.sh"
116                         exit 1
117 fi
118
119 echo ""
120
121 if [ $1 !=  "manual-container" ] && [ $1 !=  "manual-app" ]; then
122         echo -e "DFC image tag set to: \033[1m" $IMAGE_TAG"\033[0m"
123         echo "Configured image for DFC app(s) (${1}): "$DFC_IMAGE
124         tmp_im=$(docker images ${DFC_IMAGE} | grep -v REPOSITORY)
125
126         if [ $1 == "local" ]; then
127                 if [ -z "$tmp_im" ]; then
128                         echo "Local image (non nexus) "$DFC_IMAGE" does not exist in local registry, need to be built"
129                         exit 1
130                 else
131                         echo -e "DFC local image: \033[1m"$tmp_im"\033[0m"
132                         echo "If the DFC image seem outdated, rebuild the image and run the test again."
133                 fi
134         elif [ $1 == "remote" ] || [ $1 == "remote-remove" ]; then
135
136                 if [ $1 == "remote-remove" ]; then
137                         echo "Attempt to stop dfc_app container(s) if running"
138                         docker stop $(docker ps -q --filter name=${DFC_APP_BASE}]) &> /dev/null
139                         docker rm $(docker ps -q --filter name=${DFC_APP_BASE}) &> /dev/null
140                         docker rmi $DFC_IMAGE &> /dev/null
141                         tmp_im=""
142                 fi
143                 if [ -z "$tmp_im" ]; then
144                         echo "Pulling DFC image from nexus: "$DFC_IMAGE
145                         docker pull $DFC_IMAGE   > /dev/null
146                         tmp_im=$(docker images ${DFC_IMAGE} | grep -v REPOSITORY)
147                         if [ -z "$tmp_im" ]; then
148                                 echo "Image could not be pulled"
149                                 exit 1
150                         fi
151                         echo -e "DFC image: \033[1m"$tmp_im"\033[0m"
152                 else
153                         echo -e "DFC image: \033[1m"$tmp_im"\033[0m"
154                         echo "!! If the dfc image seem outdated, consider removing it from your docker registry and run the test again. Or run the script with 'remote-remove'"
155                 fi
156         fi
157 fi
158
159
160
161 echo ""
162
163 echo "Building images for the simulators if needed, MR, DR, DR Redir and FTPES."
164 echo "For HTTP simulator prebuilt containers exist in nexus repo."
165 curdir=$PWD
166 cd $SIM_GROUP
167 cd ../dr-sim
168 docker build -t drsim_common:latest . &> /dev/null
169 cd ../mr-sim
170 docker build -t mrsim:latest . &> /dev/null
171 cd ../ftpes-sftp-server
172 docker build -t ftpes_vsftpd:latest -f Dockerfile-ftpes . &> /dev/null
173 cd $curdir
174
175 echo ""
176
177 echo "Local registry images for simulators:"
178 echo "MR simulator              " $(docker images | grep mrsim)
179 echo "DR simulator:             " $(docker images | grep drsim_common)
180 echo "DR redir simulator:       " $(docker images | grep drsim_common)
181 echo "SFTP:                     " $(docker images | grep atmoz/sftp)
182 echo "FTPES:                    " $(docker images | grep ftpes_vsftpd)
183 echo "HTTP/HTTPS/HTTPS no auth: " $(docker images | grep http_https_httpd)
184 echo ""
185
186 #Configure MR sim to use correct host:port for running dfc as an app or as a container
187 #Configure DR sim with correct address for DR redirect simulator
188 if [ $START_ARG == "manual-app" ]; then
189         export SFTP_SIMS=$SFTP_SIMS_LOCALHOST
190         export FTPES_SIMS=$FTPES_SIMS_LOCALHOST
191         export HTTP_SIMS=$HTTP_SIMS_LOCALHOST
192         export HTTP_JWT_SIMS=$HTTP_JWT_SIMS_LOCALHOST
193         export HTTPS_SIMS=$HTTPS_SIMS_LOCALHOST
194         export HTTPS_SIMS_NO_AUTH=HTTPS_SIMS_NO_AUTH_LOCALHOST
195         export HTTPS_JWT_SIMS=$HTTPS_JWT_SIMS_LOCALHOST
196         export DR_REDIR_SIM="localhost"
197 fi
198 #else
199 #       export SFTP_SIMS=$SFTP_SIMS_CONTAINER
200 #       export FTPES_SIMS=$FTPES_SIMS_CONTAINER
201 #       export DR_REDIR_SIM="drsim_redir"
202 #fi
203
204 echo "-----------------------------------      Test case steps      -----------------------------------"
205
206 # Print error info for the call in the parent script (test case). Arg: <error-message-to-print>
207 # Not to be called from test script.
208 __print_err() {
209     echo ${FUNCNAME[1]} " "$1" " ${BASH_SOURCE[$i+2]} " line" ${BASH_LINENO[$i+1]}
210 }
211 # Execute curl using the host and variable. Arg: <host and variable-name>  [ <flag-to-strip-new-line> ]
212 #<flag-to-strip-new-line> may contain any string, it is just a flag
213 # Returns the variable value (if success) and return code 0 or an error message and return code 1
214 __do_curl() {
215         res=$(curl -skw "%{http_code}" $1)
216         http_code="${res:${#res}-3}"
217         if [ ${#res} -eq 3 ]; then
218                 echo "<no-response-from-server>"
219                 return 1
220         else
221                 if [ $http_code -lt 200 ] && [ $http_code -gt 299 ]; then
222                         echo "<not found, resp:${http_code}>"
223                         return 1
224                 fi
225                 if [ $# -eq 2 ]; then
226                         echo "${res:0:${#res}-3}" | xargs
227                 else
228                         echo "${res:0:${#res}-3}"
229                 fi
230
231                 return 0
232         fi
233 }
234
235 # Test a simulator variable value towards  target value using an condition operator with an optional timeout.
236 # Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value>  - This test is done
237 # immediately and sets pass or fail depending on the result of comparing variable and target using the operator.
238 # Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> <timeout>  - This test waits up to the timeout
239 # before setting pass or fail depending on the result of comparing variable and target using the operator.
240 # Not to be called from test script.
241
242 __var_test() {
243         if [ $# -eq 6 ]; then
244                 echo -e "---- ${1} sim test criteria: \033[1m ${3} \033[0m ${4} ${5} within ${6} seconds ----"
245                 ((RES_TEST++))
246                 start=$SECONDS
247                 ctr=0
248                 for (( ; ; ))
249                 do
250                         result="$(__do_curl $2$3)"
251                         retcode=$?
252                         result=${result//[[:blank:]]/} #Strip blanks
253                         duration=$((SECONDS-start))
254                         if [ $((ctr%30)) -eq 0 ]; then
255                                 echo "  Result=${result} after ${duration} seconds"
256                                 for (( i=0; i<=$DFC_MAX_IDX; i++ )); do
257                                         if [[ $STARTED_DFCS =~ "_"$DFC_APP_BASE$i"_" ]]; then
258                                                 echo "    HB ${DFC_APP_BASE}${i}: $(__do_curl http://127.0.0.1:$(($DFC_PORT+$i))/status strip)"
259                                         fi
260                                 done
261                         else
262                                 echo -ne "  Result=${result} after ${duration} seconds\033[0K\r"
263                         fi
264                         let ctr=ctr+1
265                         if [ $retcode -ne 0 ]; then
266                                 if [ $duration -gt $6 ]; then
267                                         ((RES_FAIL++))
268                                         echo -e "----  \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5}  not reached in ${6} seconds, result = ${result} ----"
269                                         return
270                                 fi
271                         elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
272                                 ((RES_PASS++))
273                                 echo -e "  Result=${result} after ${duration} seconds\033[0K\r"
274                                 echo -e "----  \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds ----"
275                                 return
276                         elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
277                                 ((RES_PASS++))
278                                 echo -e "  Result=${result} after ${duration} seconds\033[0K\r"
279                                 echo -e "----  \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds, result = ${result}  ----"
280                                 return
281                         elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
282                                 ((RES_PASS++))
283                                 echo -e "  Result=${result} after ${duration} seconds\033[0K\r"
284                                 echo -e "----  \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds, result = ${result}  ----"
285                                 return
286                         elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
287                                 ((RES_PASS++))
288                                 echo -e "  Result=${result} after ${duration} seconds\033[0K\r"
289                                 echo -e "----  \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds, result = ${result}  ----"
290                                 return
291                         else
292                                 if [ $duration -gt $6 ]; then
293                                         ((RES_FAIL++))
294                                         echo -e "----  \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5}  not reached in ${6} seconds, result = ${result} ----"
295                                         return
296                                 fi
297                         fi
298                         sleep 1
299                 done
300         elif [ $# -eq 5 ]; then
301                 echo -e "---- ${1} sim test criteria: \033[1m ${3} \033[0m ${4} ${5} ----"
302                 ((RES_TEST++))
303                 result="$(__do_curl $2$3)"
304                 retcode=$?
305                 result=${result//[[:blank:]]/}  #Strip blanks
306                 if [ $retcode -ne 0 ]; then
307                         ((RES_FAIL++))
308                         echo -e "----  \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached, result = ${result} ----"
309                 elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
310                         ((RES_PASS++))
311                         echo -e "----  \033[32m\033[1mPASS\033[0m - Test criteria met"
312                 elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
313                         ((RES_PASS++))
314                         echo -e "----  \033[32m\033[1mPASS\033[0m - Test criteria met, result = ${result} ----"
315                 elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
316                         ((RES_PASS++))
317                         echo -e "----  \033[32m\033[1mPASS\033[0m - Test criteria met, result = ${result} ----"
318                 elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
319                         ((RES_PASS++))
320                         echo -e "----  \033[32m\033[1mPASS\033[0m - Test criteria met, result = ${result} ----"
321                 else
322                         ((RES_FAIL++))
323                         echo -e "----  \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached, result = ${result} ----"
324                 fi
325         else
326                 echo "Wrong args to __var_test, needs five or six args: <simulator-name> <host> <variable-name> <condition-operator> <target-value> [ <timeout> ]"
327                 exit 1
328         fi
329 }
330 # Stops a named container
331 __docker_stop() {
332         if [ $# -ne 1 ]; then
333                 echo "__docker_stop need 1 arg <container-name>"
334                 exit 1
335         fi
336         tmp=$(docker stop $1  2>/dev/null)
337         if [ -z $tmp ] || [ $tmp != $1 ]; then
338                 echo " ${1} container not stopped or not existing"
339         else
340                 echo " ${1} container stopped"
341         fi
342 }
343
344 # Starts a named container (that has previously been stopped)
345 __docker_start() {
346         if [ $# -ne 1 ]; then
347                 echo "__docker_start need 1 arg <container-name>"
348                 exit 1
349         fi
350         tmp=$(docker start $1  2>/dev/null)
351         if [ -z $tmp ] || [ $tmp != $1 ]; then
352                 echo " ${1} container not started or not existing"
353         else
354                 echo " ${1} container started"
355         fi
356 }
357
358 # Removes a named container
359 __docker_rm() {
360         if [ $# -ne 1 ]; then
361                 echo "__docker_rm need 1 arg <container-name>"
362                 exit 1
363         fi
364         tmp=$(docker rm $1  2>/dev/null)
365         if [ -z $tmp ] || [ $tmp != $1 ]; then
366                 echo " ${1} container not removed or not existing"
367         else
368                 echo " ${1} container removed"
369         fi
370 }
371
372 __start_dfc_image() {
373         set -x
374         if [ $# != 2 ]; then
375         __print_err "need tow args, <dfc-instance-name> 0.."$$DFC_MAX_IDX
376                 exit 1
377         fi
378
379         if [ $2 -lt 0 ] || [ $2 -gt $DFC_MAX_IDX ]; then
380                 __print_err "need two args, <dfc-instance-name> 0.."$DFC_MAX_IDX
381                 exit 1
382         fi
383         appname=$1
384         localport=$(($DFC_PORT + $2))
385         localport_secure=$(($DFC_PORT_SECURE + $2))
386
387         echo "Creating docker network "$DOCKER_SIM_NWNAME", if needed"
388
389         docker network ls| grep "$DOCKER_SIM_NWNAME" > /dev/null || docker network create "$DOCKER_SIM_NWNAME"
390
391         echo "Starting DFC: " $appname " with ports mapped to " $localport " and " $localport_secure " in docker network "$DOCKER_SIM_NWNAME
392   docker run -d --volume $(pwd)/../simulator-group/tls/:/opt/app/datafile/etc/cert/ --volume $(pwd)/../simulator-group/dfc_config_volume/:/app-config/ -p $localport":8100" -p $localport_secure":8433" --network=$DOCKER_SIM_NWNAME -e CONFIG_BINDING_SERVICE=$CONFIG_BINDING_SERVICE -e CONFIG_BINDING_SERVICE_SERVICE_PORT=$CONFIG_BINDING_SERVICE_SERVICE_PORT -e HOSTNAME=$appname --name $appname $DFC_IMAGE
393         sleep 3
394         set +x
395         dfc_started=false
396         for i in {1..10}; do
397                 if [ $(docker inspect --format '{{ .State.Running }}' $appname) ]
398                         then
399                                 echo " Image: $(docker inspect --format '{{ .Config.Image }}' ${appname})"
400                                 echo "DFC container ${appname} running"
401                                 dfc_started=true
402                                 break
403                         else
404                                 sleep $i
405                 fi
406         done
407         if ! [ $dfc_started  ]; then
408                 echo "DFC container ${appname} could not be started"
409                 exit 1
410         fi
411
412         dfc_hb=false
413         echo "Waiting for DFC ${appname} heartbeat..."
414         for i in {1..10}; do
415                 result="$(__do_curl http://127.0.0.1:${localport}/heartbeat)"
416                 if [ $? -eq 0 ]; then
417                         echo "DFC ${appname} responds to heartbeat: " $result
418                         dfc_hb=true
419                         result="$(__do_curl http://127.0.0.1:${localport}/actuator/info)"
420                         echo "DFC ${appname} image build info: " $result
421                         break
422                 else
423                         sleep $i
424                 fi
425         done
426
427         if [ "$dfc_hb" = "false"  ]; then
428                 echo "DFC ${appname} did not respond to heartbeat"
429                 exit 1
430         fi
431 }
432
433 # Function for waiting for named container to be started manually.
434 __wait_for_container() {
435         start=$SECONDS
436         if [ $# != 2 ]; then
437                 echo "Need one arg: <container-name> <instance-id>"
438                 exit 1
439         fi
440         http=$(($DFC_PORT+$2))
441         https=$((DFC_PORT_SECURE+$2))
442         echo "The container is expected to map its ports (8100/8433) to the following port visibile on the host: http port ${http} and https port ${https}"
443         echo "Waiting for container with name '${1}' to be started manually...."
444
445         for (( ; ; ))
446         do
447                 if [ $(docker inspect --format '{{ .State.Running }}' $1 2> /dev/null) ]; then
448                         echo "Container running: "$1
449                         break
450                 else
451                         duration=$((SECONDS-start))
452                         echo -ne "  Waited ${duration} seconds\033[0K\r"
453                         sleep 1
454                 fi
455         done
456
457         echo "Connecting container "$1" to simulator network "$DOCKER_SIM_NWNAME
458         docker network connect $DOCKER_SIM_NWNAME $1
459 }
460
461 #WFunction for waiting for named container to be stopped manually.
462 __wait_for_container_gone() {
463         start=$SECONDS
464         if [ $# != 1 ]; then
465                 echo "Need one arg: <container-name>"
466                 exit 1
467         fi
468         echo "Disconnecting container "$1" from simulator network "$DOCKER_SIM_NWNAME
469         docker network disconnect $DOCKER_SIM_NWNAME $1
470         echo "Waiting for container with name '${1}' to be stopped manually...."
471
472         for (( ; ; ))
473         do
474                 if [ $(docker inspect --format '{{ .State.Running }}' $1 2> /dev/null) ]; then
475                         duration=$((SECONDS-start))
476                         echo -ne "  Waited ${duration} seconds\033[0K\r"
477                         sleep 1
478                 else
479                         echo "Container stopped: "$1
480                         break
481                 fi
482         done
483 }
484
485 #Function for waiting to dfc to be started manually
486 __wait_for_dfc() {
487         http=$(($DFC_PORT+$2))
488         https=$((DFC_PORT_SECURE+$2))
489         echo "The app is expected to listen to http port ${http} and https port ${https}"
490         echo "The app shall use ${1} for HOSTNAME."
491         read -p "Press enter to continue when app mapping to ${1} has been manually started"
492 }
493
494 #Function for waiting to dfc to be stopped manually
495 __wait_for_dfc_gone() {
496         read -p "Press enter to continue when when app mapping to ${1} has been manually stopped"
497 }
498
499 #############################################################
500 ############## Functions for auto test scripts ##############
501 #############################################################
502
503 # Print the env variables needed for the simulators and their setup
504 log_sim_settings() {
505         echo "Simulator settings"
506         echo "MR_TC=                 "$MR_TC
507         echo "MR_GROUPS=             "$MR_GROUPS
508     echo "MR_FILE_PREFIX_MAPPING="$MR_FILE_PREFIX_MAPPING
509         echo "DR_TC=                 "$DR_TC
510         echo "DR_FEEDS=              "$DR_FEEDS
511         echo "DR_REDIR_SIM=          "$DR_REDIR_SIM
512         echo "DR_REDIR_TC=           "$DR_REDIR_TC
513         echo "DR_REDIR_FEEDS=        "$DR_REDIR_FEEDS
514
515         echo "NUM_FTPFILES=          "$NUM_FTPFILES
516         echo "NUM_HTTPFILES=         "$NUM_HTTPFILES
517         echo "NUM_PNFS=              "$NUM_PNFS
518         echo "FILE_SIZE=             "$FILE_SIZE
519         echo "FTP_TYPE=              "$FTP_TYPE
520         echo "HTTP_TYPE=             "$HTTP_TYPE
521         echo "FTP_FILE_PREFIXES=     "$FTP_FILE_PREFIXES
522         echo "HTTP_FILE_PREFIXES=    "$HTTP_FILE_PREFIXES
523         echo "NUM_FTP_SERVERS=       "$NUM_FTP_SERVERS
524         echo "NUM_HTTP_SERVERS=      "$NUM_HTTP_SERVERS
525         echo "SFTP_SIMS=             "$SFTP_SIMS
526         echo "FTPES_SIMS=             "$FTPES_SIMS
527         echo "HTTP_SIMS=             "$HTTP_SIMS
528         echo "HTTP_JWT_SIMS=         "$HTTP_JWT_SIMS
529         echo "HTTPS_SIMS=            "$HTTPS_SIMS
530         echo "HTTPS_SIMS_NO_AUTH=     "$HTTPS_SIMS_NO_AUTH
531         echo "HTTPS_JWT_SIMS=         "$HTTPS_JWT_SIMS
532         echo ""
533 }
534
535 # Stop and remove all containers including dfc app and simulators
536 clean_containers() {
537         echo "Stopping all containers, dfc app(s) and simulators with name prefix 'dfc_'"
538         docker stop $(docker ps -q --filter name=dfc_) &> /dev/null
539         echo "Removing all containers, dfc app and simulators with name prefix 'dfc_'"
540         docker rm $(docker ps -a -q --filter name=dfc_) &> /dev/null
541         docker rm -f $(docker ps -a -q --filter name=oom-certservice-post-processor) &> /dev/null
542         echo "Removing unused docker networks with substring 'dfc' in network name"
543         docker network rm $(docker network ls -q --filter name=dfc)
544         echo ""
545 }
546
547 # Start all simulators in the simulator group
548 start_simulators() {
549
550         echo "Starting all simulators"
551         curdir=$PWD
552         cd $SIM_GROUP
553         export SIM_GROUP=$SIM_GROUP
554         $SIM_GROUP/simulators-start.sh
555         cd $curdir
556         echo ""
557 }
558
559 # Start the dfc application
560 start_dfc() {
561
562         if [ $# != 1 ]; then
563         __print_err "need one arg, <dfc-instance-id>"
564                 exit 1
565         fi
566
567         if [ $1 -lt 0 ] || [ $1 -gt $DFC_MAX_IDX ]; then
568                 __print_err "arg should be 0.."$DFC_MAX_IDX
569                 exit 1
570         fi
571         appname=$DFC_APP_BASE$1
572         STARTED_DFCS=$STARTED_DFCS"_"$appname"_"
573
574         if [ $START_ARG == "local" ] || [ $START_ARG == "remote" ] ||  [ $START_ARG == "remote-remove" ]; then
575                 __start_dfc_image $appname $1
576         elif [ $START_ARG == "manual-container" ]; then
577                 __wait_for_container $appname $1
578         elif [ $START_ARG == "manual-app" ]; then
579                 __wait_for_dfc $appname $1
580         fi
581 }
582
583 # Configure volume with dfc config, args <dfc-instance-id> <yaml-file-path>
584 # Not intended to be called directly by test scripts.
585 __dfc_config() {
586
587         if [ $# != 2 ]; then
588         __print_err "need two args, <dfc-instance-id> <yaml-file-path>"
589                 exit 1
590         fi
591
592         if [ $1 -lt 0 ] || [ $1 -gt $DFC_MAX_IDX ]; then
593                 __print_err "dfc-instance-id should be 0.."$DFC_MAX_IDX
594                 exit 1
595         fi
596         if ! [ -f $2 ]; then
597                 __print_err "yaml file does not exist: "$2
598                 exit 1
599         fi
600
601         appname=$DFC_APP_BASE$1
602
603         echo "Applying configuration for " $appname " from " $2
604         mkdir $(pwd)/../simulator-group/dfc_config_volume/
605         cp $2 $(pwd)/../simulator-group/dfc_config_volume/application_config.yaml
606 }
607
608 # Configure volume with dfc app config, args <dfc-instance-id> <yaml-file-path>
609 dfc_config_app() {
610         if [ $START_ARG == "manual-app" ]; then
611                 echo "Replacing 'mrsim' with 'localhost' in yaml app config"
612                 sed 's/mrsim/localhost/g' $2 > .tmp_app.yaml
613                 echo "Replacing 'drsim' with 'localhost' in yaml dmaap config"
614                 sed 's/drsim/localhost/g' .tmp_app.yaml > .app.yaml
615                 __dfc_config $1 .app.yaml
616         else
617                 __dfc_config $1 $2
618         fi
619 }
620
621 # Stop and remove the dfc app container
622 kill_dfc() {
623
624         if [ $# != 1 ]; then
625         __print_err "need one arg, <dfc-instance-id>"
626                 exit 1
627         fi
628
629         if [ $1 -lt 0 ] || [ $1 -gt $DFC_MAX_IDX ]; then
630                 __print_err "arg should be 0.."$DFC_MAX_IDX
631                 exit 1
632         fi
633         appname=$DFC_APP_BASE$1
634
635         echo "Killing DFC, instance id: "$1
636
637         if [ $START_ARG == "local" ] || [ $START_ARG == "remote" ] ||  [ $START_ARG == "remote-remove" ]; then
638                 __docker_stop $appname
639                 __docker_rm $appname
640         elif [ $START_ARG == "manual-container" ]; then
641                 __wait_for_container_gone $appname
642         elif [ $START_ARG == "manual-app" ]; then
643                 __wait_for_dfc_gone $appname
644         fi
645
646         rm -rf $(pwd)/../simulator-group/dfc_config_volume
647 }
648
649 # Stop and remove the DR simulator container
650 kill_dr() {
651         echo "Killing DR sim"
652         __docker_stop dfc_dr-sim
653         __docker_rm dfc_dr-sim
654 }
655
656 # Stop and remove the DR redir simulator container
657 kill_drr() {
658         echo "Killing DR redir sim"
659         __docker_stop dfc_dr-redir-sim
660         __docker_rm dfc_dr-redir-sim
661 }
662
663 # Stop and remove the MR simulator container
664 kill_mr() {
665         echo "Killing MR sim"
666         __docker_stop dfc_mr-sim
667         __docker_rm dfc_mr-sim
668 }
669
670 # Stop and remove the SFTP container, arg: <sftp-instance-id>
671 kill_sftp() {
672
673         if [ $# != 1 ]; then
674         __print_err "need one arg, <sftp-instance-id>"
675                 exit 1
676         fi
677
678         if [ $1 -lt 0 ] || [ $1 -gt $FTP_MAX_IDX ]; then
679                 __print_err "arg should be 0.."$FTP_MAX_IDX
680                 exit 1
681         fi
682         appname=$SFTP_BASE$1
683
684         echo "Killing SFTP, instance id: "$1
685
686         __docker_stop $appname
687         __docker_rm $appname
688 }
689
690 # Stop SFTP container, arg: <sftp-instance-id>
691 stop_sftp() {
692
693         if [ $# != 1 ]; then
694         __print_err "need one arg, <sftp-instance-id>"
695                 exit 1
696         fi
697
698         if [ $1 -lt 0 ] || [ $1 -gt $FTP_MAX_IDX ]; then
699                 __print_err "arg should be 0.."$FTP_MAX_IDX
700                 exit 1
701         fi
702         appname=$SFTP_BASE$1
703
704         echo "Stopping SFTP, instance id: "$1
705
706         __docker_stop $appname
707 }
708
709 # Starts a stopped SFTP container, arg: <sftp-instance-id>
710 start_sftp() {
711
712         if [ $# != 1 ]; then
713         __print_err "need one arg, <sftp-instance-id>"
714                 exit 1
715         fi
716
717         if [ $1 -lt 0 ] || [ $1 -gt $FTP_MAX_IDX ]; then
718                 __print_err "arg should be 0.."$FTP_MAX_IDX
719                 exit 1
720         fi
721         appname=$SFTP_BASE$1
722
723         echo "Starting SFTP, instance id: "$1
724
725         __docker_start $appname
726 }
727
728 # Stop and remove the FTPES container, arg: <ftpes-instance-id>
729 kill_ftpes() {
730
731         if [ $# != 1 ]; then
732         __print_err "need one arg, <ftpes-instance-id>"
733                 exit 1
734         fi
735
736         if [ $1 -lt 0 ] || [ $1 -gt $FTP_MAX_IDX ]; then
737                 __print_err "arg should be 0.."$FTP_MAX_IDX
738                 exit 1
739         fi
740         appname=$FTPES_BASE$1
741
742         echo "Killing FTPES, instance id: "$1
743
744         __docker_stop $appname
745         __docker_rm $appname
746 }
747
748 # Stop FTPES container, arg: <ftpes-instance-id>
749 stop_ftpes() {
750
751         if [ $# != 1 ]; then
752         __print_err "need one arg, <ftpes-instance-id>"
753                 exit 1
754         fi
755
756         if [ $1 -lt 0 ] || [ $1 -gt $FTP_MAX_IDX ]; then
757                 __print_err "arg should be 0.."$FTP_MAX_IDX
758                 exit 1
759         fi
760         appname=$FTPES_BASE$1
761
762         echo "Stopping FTPES, instance id: "$1
763
764         __docker_stop $appname
765 }
766
767 # Starts a stopped FTPES container, arg: <ftpes-instance-id>
768 start_ftpes() {
769
770         if [ $# != 1 ]; then
771         __print_err "need one arg, <ftpes-instance-id>"
772                 exit 1
773         fi
774
775         if [ $1 -lt 0 ] || [ $1 -gt $FTP_MAX_IDX ]; then
776                 __print_err "arg should be 0.."$FTP_MAX_IDX
777                 exit 1
778         fi
779         appname=$FTPES_BASE$1
780
781         echo "Starting FTPES, instance id: "$1
782
783         __docker_start $appname
784 }
785
786 # Stop and remove the HTTP container, arg: <http-instance-id>
787 kill_http_https() {
788
789         if [ $# != 1 ]; then
790         __print_err "need one arg, <http-instance-id>"
791                 exit 1
792         fi
793
794         if [ $1 -lt 0 ] || [ $1 -gt $HTTP_MAX_IDX ]; then
795                 __print_err "arg should be 0.."$HTTP_MAX_IDX
796                 exit 1
797         fi
798         appname=$HTTP_HTTPS_BASE$1
799
800         echo "Killing HTTP/HTTPS, instance id: "$1
801
802         __docker_stop $appname
803         __docker_rm $appname
804 }
805
806 # Stop HTTP container, arg: <http-instance-id>
807 stop_http_https() {
808
809         if [ $# != 1 ]; then
810         __print_err "need one arg, <http-instance-id>"
811                 exit 1
812         fi
813
814         if [ $1 -lt 0 ] || [ $1 -gt $HTTP_MAX_IDX ]; then
815                 __print_err "arg should be 0.."$HTTP_MAX_IDX
816                 exit 1
817         fi
818         appname=$HTTP_HTTPS_BASE$1
819
820         echo "Stopping HTTP/HTTPS, instance id: "$1
821
822         __docker_stop $appname
823 }
824
825 # Starts a stopped HTTP container, arg: <http-instance-id>
826 start_http_https() {
827
828         if [ $# != 1 ]; then
829         __print_err "need one arg, <http-instance-id>"
830                 exit 1
831         fi
832
833         if [ $1 -lt 0 ] || [ $1 -gt $HTTP_MAX_IDX ]; then
834                 __print_err "arg should be 0.."$HTTP_MAX_IDX
835                 exit 1
836         fi
837         appname=$HTTP_HTTPS_BASE$1
838
839         echo "Starting HTTP/HTTPS, instance id: "$1
840
841         __docker_start $appname
842 }
843
844 # Print a variable value from the MR simulator. Arg: <variable-name>
845 mr_print() {
846         if [ $# != 1 ]; then
847         __print_err "need one arg, <sim-param>"
848                 exit 1
849         fi
850         echo -e "---- MR sim, \033[1m $1 \033[0m: $(__do_curl http://127.0.0.1:$MR_PORT/$1)"
851 }
852
853 # Print a variable value from the MR https simulator. Arg: <variable-name>
854 mr_secure_print() {
855         if [ $# != 1 ]; then
856         __print_err "need one arg, <sim-param>"
857                 exit 1
858         fi
859         echo -e "---- MR sim, \033[1m $1 \033[0m: $(__do_curl https://127.0.0.1:$MR_PORT_SECURE/$1)"
860 }
861
862 # Print a variable value from the DR simulator. Arg: <variable-name>
863 dr_print() {
864         if [ $# != 1 ]; then
865         __print_err "need one arg, <sim-param>"
866                 exit 1
867         fi
868         echo -e "---- DR sim, \033[1m $1 \033[0m: $(__do_curl http://127.0.0.1:$DR_PORT/$1)"
869 }
870
871 # Print a variable value from the DR redir simulator. Arg: <variable-name>
872 drr_print() {
873         if [ $# != 1 ]; then
874         __print_err "need one arg, <sim-param>"
875                 exit 1
876         fi
877         echo -e "---- DR redir sim, \033[1m $1 \033[0m: $(__do_curl http://127.0.0.1:$DRR_PORT/$1)"
878 }
879 # Print a variable value from dfc. Arg: <dfc-instance-id> <variable-name>
880 dfc_print() {
881         if [ $# != 2 ]; then
882         __print_err "need two args, <dfc-instance-id> <dfc-param>"
883                 exit 1
884         fi
885         if [ $1 -lt 0 ] || [ $1 -gt $DFC_MAX_IDX ]; then
886                 __print_err "dfc instance id should be in range 0.."DFC_MAX_IDX
887                 exit 1
888         fi
889         localport=$(($DFC_PORT + $1))
890         appname=$DFC_APP_BASE$1
891         echo -e "---- DFC $appname, \033[1m $2 \033[0m: $(__do_curl http://127.0.0.1:$localport/$2)"
892 }
893
894 # Read a variable value from MR sim and send to stdout. Arg: <variable-name>
895 mr_read() {
896         echo "$(__do_curl http://127.0.0.1:$MR_PORT/$1)"
897 }
898
899 # Read a variable value from MR https sim and send to stdout. Arg: <variable-name>
900 mr_secure_read() {
901         echo "$(__do_curl https://127.0.0.1:$MR_PORT_SECURE/$1)"
902 }
903
904 # Read a variable value from DR sim and send to stdout. Arg: <variable-name>
905 dr_read() {
906         echo "$(__do_curl http://127.0.0.1:$DR_PORT/$1)"
907 }
908
909 # Read a variable value from DR redir sim and send to stdout. Arg: <variable-name>
910 drr_read() {
911         echo "$(__do_curl http://127.0.0.1:$DRR_PORT/$1)"
912 }
913
914
915 # Sleep. Arg: <sleep-time-in-sec>
916 sleep_wait() {
917         if [ $# != 1 ]; then
918                 __print_err "need one arg, <sleep-time-in-sec>"
919                 exit 1
920         fi
921         echo "---- Sleep for " $1 " seconds ----"
922         start=$SECONDS
923         duration=$((SECONDS-start))
924         while [ $duration -lt $1 ]; do
925                 echo -ne "  Slept for ${duration} seconds\033[0K\r"
926                 sleep 1
927                 duration=$((SECONDS-start))
928         done
929         echo ""
930 }
931
932 # Sleep and print dfc heartbeat. Arg: <sleep-time-in-sec>
933 sleep_heartbeat() {
934         if [ $# != 1 ]; then
935                 __print_err "need one arg, <sleep-time-in-sec>"
936                 exit 1
937         fi
938         echo "---- Sleep for " $1 " seconds ----"
939         echo ""
940         start=$SECONDS
941         duration=$((SECONDS-start))
942         ctr=0
943         rows=0
944         while [ $duration -lt $1 ]; do
945                 if [ $rows -eq 0 ]; then
946                         tput cuu1
947                 fi
948                 rows=0
949                 echo "  Slept for ${duration} seconds"
950                 if [ $((ctr%30)) -eq 0 ]; then
951                         for (( i=0; i<=$DFC_MAX_IDX; i++ )); do
952                                 if [[ $STARTED_DFCS =~ "_"$DFC_APP_BASE$i"_" ]]; then
953                                         let rows=rows+1
954                                         echo "    HB ${DFC_APP_BASE}${i}: $(__do_curl http://127.0.0.1:$(($DFC_PORT+$i))/heartbeat)"
955                                 fi
956                         done
957                 fi
958
959                 let ctr=ctr+1
960                 sleep 1
961                 duration=$((SECONDS-start))
962         done
963         echo ""
964 }
965
966 # Tests if a variable value in the MR simulator is equal to a target value and and optional timeout.
967 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
968 # equal to the target or not.
969 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
970 # before setting pass or fail depending on if the variable value becomes equal to the target
971 # value or not.
972 mr_equal() {
973         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
974                 __var_test "MR" "http://127.0.0.1:$MR_PORT/" $1 "=" $2 $3
975         else
976                 __print_err "Wrong args to mr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]"
977         fi
978 }
979
980 mr_secure_equal() {
981         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
982                 __var_test "MR" "https://127.0.0.1:$MR_PORT_SECURE/" $1 "=" $2 $3
983         else
984                 __print_err "Wrong args to mr_secure_equal, needs two or three args: <sim-param> <target-value> [ timeout ]"
985         fi
986 }
987
988 # Tests if a variable value in the MR simulator is greater than a target value and and optional timeout.
989 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
990 # greater than the target or not.
991 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
992 # before setting pass or fail depending on if the variable value greater than the target
993 # value or not.
994 mr_greater() {
995         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
996                 __var_test "MR" "http://127.0.0.1:$MR_PORT/" $1 ">" $2 $3
997         else
998                 __print_err "Wrong args to mr_greater, needs two or three args: <sim-param> <target-value> [ timeout ]"
999         fi
1000 }
1001
1002 mr_secure_greater() {
1003         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1004                 __var_test "MR" "https://127.0.0.1:$MR_PORT_SECURE/" $1 ">" $2 $3
1005         else
1006                 __print_err "Wrong args to mr_secure_greater, needs two or three args: <sim-param> <target-value> [ timeout ]"
1007         fi
1008 }
1009
1010 # Tests if a variable value in the MR simulator is less than a target value and and optional timeout.
1011 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1012 # less than the target or not.
1013 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
1014 # before setting pass or fail depending on if the variable value less than the target
1015 # value or not.
1016 mr_less() {
1017         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1018                 __var_test "MR" "http://127.0.0.1:$MR_PORT/" $1 "<" $2 $3
1019         else
1020                 __print_err "Wrong args to mr_less, needs two or three args: <sim-param> <target-value> [ timeout ]"
1021         fi
1022 }
1023 mr_secure_less() {
1024         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1025                 __var_test "MR" "https://127.0.0.1:$MR_PORT_SECURE/" $1 "<" $2 $3
1026         else
1027                 __print_err "Wrong args to mr_secure_less, needs two or three args: <sim-param> <target-value> [ timeout ]"
1028         fi
1029 }
1030
1031 # Tests if a variable value in the MR simulator contains the target string and and optional timeout.
1032 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable contains
1033 # the target or not.
1034 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
1035 # before setting pass or fail depending on if the variable value contains the target
1036 # value or not.
1037 mr_contain_str() {
1038         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1039                 __var_test "MR" "http://127.0.0.1:$MR_PORT/" $1 "contain_str" $2 $3
1040         else
1041                 __print_err "Wrong args to mr_contain_str, needs two or three args: <sim-param> <target-value> [ timeout ]"
1042         fi
1043 }
1044 mr_secure_contain_str() {
1045         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1046                 __var_test "MR" "https://127.0.0.1:$MR_PORT_SECURE/" $1 "contain_str" $2 $3
1047         else
1048                 __print_err "Wrong args to mr_secure_contain_str, needs two or three args: <sim-param> <target-value> [ timeout ]"
1049         fi
1050 }
1051
1052 # Tests if a variable value in the DR simulator is equal to a target value and and optional timeout.
1053 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1054 # equal to the target or not.
1055 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
1056 # before setting pass or fail depending on if the variable value becomes equal to the target
1057 # value or not.
1058 dr_equal() {
1059         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1060                 __var_test "DR" "http://127.0.0.1:$DR_PORT/" $1 "=" $2 $3
1061         else
1062                 __print_err "Wrong args to dr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]"
1063         fi
1064 }
1065
1066 # Tests if a variable value in the DR simulator is greater than a target value and and optional timeout.
1067 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1068 # greater than the target or not.
1069 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
1070 # before setting pass or fail depending on if the variable value greater than the target
1071 # value or not.
1072 dr_greater() {
1073         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1074                 __var_test "DR" "http://127.0.0.1:$DR_PORT/" $1 ">" $2 $3
1075         else
1076                 __print_err "Wrong args to dr_greater, needs two or three args: <sim-param> <target-value> [ timeout ]"
1077         fi
1078 }
1079
1080 # Tests if a variable value in the DR simulator is less than a target value and and optional timeout.
1081 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1082 # less than the target or not.
1083 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
1084 # before setting pass or fail depending on if the variable value less than the target
1085 # value or not.
1086 dr_less() {
1087         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1088                 __var_test "DR" "http://127.0.0.1:$DR_PORT/" $1 "<" $2 $3
1089         else
1090                 __print_err "Wrong args to dr_less, needs two or three args: <sim-param> <target-value> [ timeout ]"
1091         fi
1092 }
1093
1094 # Tests if a variable value in the DR simulator contains the target string and and optional timeout.
1095 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable contains
1096 # the target or not.
1097 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
1098 # before setting pass or fail depending on if the variable value contains the target
1099 # value or not.
1100 dr_contain_str() {
1101         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1102                 __var_test "DR" "http://127.0.0.1:$DR_PORT/" $1 "contain_str" $2 $3
1103         else
1104                 __print_err "Wrong args to dr_contain_str, needs two or three args: <sim-param> <target-value> [ timeout ]"
1105         fi
1106 }
1107
1108 # Tests if a variable value in the DR Redir simulator is equal to a target value and and optional timeout.
1109 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1110 # equal to the target or not.
1111 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
1112 # before setting pass or fail depending on if the variable value becomes equal to the target
1113 # value or not.
1114 drr_equal() {
1115         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1116                 __var_test "DR REDIR" "http://127.0.0.1:$DRR_PORT/" $1 "=" $2 $3
1117         else
1118                 __print_err "Wrong args to drr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]"
1119         fi
1120 }
1121
1122
1123 # Tests if a variable value in the DR Redir simulator is greater a target value and and optional timeout.
1124 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1125 # greater the target or not.
1126 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
1127 # before setting pass or fail depending on if the variable value greater than the target
1128 # value or not.
1129 drr_greater() {
1130         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1131                 __var_test "DR REDIR" "http://127.0.0.1:$DRR_PORT/" $1 ">" $2 $3
1132         else
1133                 __print_err "Wrong args to drr_greater, needs two or three args: <sim-param> <target-value> [ timeout ]"
1134         fi
1135 }
1136
1137 # Tests if a variable value in the DR Redir simulator is less than a target value and and optional timeout.
1138 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1139 # less than the target or not.
1140 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
1141 # before setting pass or fail depending on if the variable value less than the target
1142 # value or not.
1143 drr_less() {
1144         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1145                 __var_test "DR REDIR" "http://127.0.0.1:$DRR_PORT/" $1 "<" $2 $3
1146         else
1147                 __print_err "Wrong args to drr_less, needs two or three args: <sim-param> <target-value> [ timeout ]"
1148         fi
1149 }
1150
1151 # Tests if a variable value in the DR redir simulator contains the target string and and optional timeout.
1152 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable contains
1153 # the target or not.
1154 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
1155 # before setting pass or fail depending on if the variable value contains the target
1156 # value or not.
1157 drr_contain_str() {
1158         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1159                 __var_test "DR REDIR" "http://127.0.0.1:$DRR_PORT/" $1 "contain_str" $2 $3
1160         else
1161                 __print_err "Wrong args to drr_contain_str, needs two or three args: <sim-param> <target-value> [ timeout ]"
1162         fi
1163 }
1164
1165 #Test if a variable in the DFC contains a substring. Arg: <dfc-index> <variable-name> <substring-in-quotes>
1166 dfc_contain_str() {
1167         if [ $# -eq 3 ]; then
1168                 if [ $1 -lt 0 ] || [ $1 -gt $DFC_MAX_IDX ]; then
1169                         __print_err "arg should be 0.."$DFC_MAX_IDX
1170                         exit 1
1171                 fi
1172                 appname=$DFC_APP_BASE$1
1173                 localport=$(($DFC_PORT + $1))
1174                 echo -e "---- DFC test criteria: $appname \033[1m ${2} \033[0m contains: ${3} ----"
1175                 ((RES_TEST++))
1176                 result="$(__do_curl http://127.0.0.1:$localport/${2})"
1177                 if [[ $result =~ $3 ]]; then
1178                         ((RES_PASS++))
1179                         echo -e "----  \033[32m\033[1mPASS\033[0m - Test criteria met"
1180                 else
1181                         ((RES_FAIL++))
1182                         echo -e "----  \033[31m\033[1mFAIL\033[0m - Target '${3}' not reached, result = ${result} ----"
1183                 fi
1184         else
1185                 echo "Wrong args to dfc_contain_str, needs three arg: <dfc-index> <dfc-variable> <str>"
1186                 exit 1
1187         fi
1188 }
1189
1190 # Store all dfc app and simulators log to the test case log dir. All logs gets a prefix to
1191 # separate logs stored at different steps in the test script. Arg: <tc-id> <log-prefix>
1192 store_logs() {
1193         if [ $# != 1 ]; then
1194         __print_err "need one arg, <file-prefix>"
1195                 exit 1
1196         fi
1197         echo "Storing all container logs and dfc app log using prefix: "$1
1198         if ! [ $START_ARG == "manual-app" ]; then
1199                 for (( i=0; i<=$DFC_MAX_IDX; i++ )); do
1200                         appname=$DFC_APP_BASE$i
1201                         tmp=$(docker ps | grep $appname)
1202                         if ! [ -z "$tmp" ]; then   #Only stored logs from running DFC apps
1203                                 docker cp $appname:/var/log/ONAP/application.log $TESTLOGS/$ATC/${1}_${appname}_application.log
1204                                 docker logs $appname > $TESTLOGS/$ATC/$1_$appname-docker.log 2>&1
1205                         fi
1206                 done
1207         fi
1208         docker logs dfc_mr-sim > $TESTLOGS/$ATC/$1_dfc_mr-sim-docker.log 2>&1
1209         docker logs dfc_dr-sim > $TESTLOGS/$ATC/$1_dfc_dr-sim-docker.log 2>&1
1210         docker logs dfc_dr-redir-sim > $TESTLOGS/$ATC/$1_dfc_dr-redir-sim-docker.log 2>&1
1211
1212         for (( i=0; i<=$FTP_MAX_IDX; i++ )); do
1213                 appname=$SFTP_BASE$i
1214                 docker logs $appname > $TESTLOGS/$ATC/${1}_${appname}.log 2>&1
1215                 appname=$FTPES_BASE$i
1216                 docker logs $appname > $TESTLOGS/$ATC/${1}_${appname}.log 2>&1
1217         done
1218
1219         for (( i=0; i<=$HTTP_MAX_IDX; i++ )); do
1220                 appname=$HTTP_HTTPS_BASE$i
1221                 docker logs $appname > $TESTLOGS/$ATC/${1}_${appname}.log 2>&1
1222         done
1223
1224 }
1225 # Check the dfc application log, for all dfc instances, for WARN and ERR messages and print the count.
1226 check_dfc_logs() {
1227         for (( i=0; i<=$DFC_MAX_IDX; i++ )); do
1228                 appname=$DFC_APP_BASE$i
1229                 tmp=$(docker ps | grep $appname)
1230                 if ! [ -z "$tmp" ]; then  #Only check logs for running dfc_apps
1231                         _check_dfc_log $appname
1232                 fi
1233         done
1234 }
1235
1236 # Check dfc app log for one dfc instance, arg <dfc-app-name>
1237 _check_dfc_log() {
1238         echo "Checking $1 log $DFC_LOGPATH for WARNINGs and ERRORs"
1239         foundentries=$(docker exec -it $1 grep WARN /var/log/ONAP/application.log | wc -l)
1240         if [ $? -ne  0 ];then
1241                 echo "  Problem to search $1 log $DFC_LOGPATH"
1242         else
1243                 if [ $foundentries -eq 0 ]; then
1244                         echo "  No WARN entries found in $1 log $DFC_LOGPATH"
1245                 else
1246                         echo -e "  Found \033[1m"$foundentries"\033[0m WARN entries in $1 log $DFC_LOGPATH"
1247                 fi
1248         fi
1249         foundentries=$(docker exec -it $1 grep ERR $DFC_LOGPATH | wc -l)
1250         if [ $? -ne  0 ];then
1251                 echo "  Problem to search $1 log $DFC_LOGPATH"
1252         else
1253                 if [ $foundentries -eq 0 ]; then
1254                         echo "  No ERR entries found in $1 log $DFC_LOGPATH"
1255                 else
1256                         echo -e "  Found \033[1m"$foundentries"\033[0m ERR entries in $1 log $DFC_LOGPATH"
1257                 fi
1258         fi
1259 }
1260
1261 print_all() {
1262
1263         echo "---- DFC and all sim variables"
1264
1265         for (( i=0; i<=$DFC_MAX_IDX; i++ )); do
1266                 appname=$DFC_APP_BASE$i
1267                 tmp=$(docker ps | grep $appname)
1268                 if ! [ -z "$tmp" ]; then  #Only check running dfc_apps
1269                         dfc_print $i status
1270                 fi
1271         done
1272
1273
1274         mr_print tc_info
1275         mr_print status
1276         mr_print execution_time
1277         mr_print groups
1278         mr_print changeids
1279         mr_print fileprefixes
1280         mr_print exe_time_first_poll
1281         mr_print groups/exe_time_first_poll
1282         mr_print ctr_requests
1283         mr_print groups/ctr_requests
1284         mr_print ctr_responses
1285         mr_print groups/ctr_responses
1286         mr_print ctr_files
1287         mr_print groups/ctr_files
1288         mr_print ctr_unique_files
1289         mr_print groups/ctr_unique_files
1290         mr_print groups/ctr_events
1291         mr_print ctr_events
1292         mr_print ctr_unique_PNFs
1293         mr_print groups/ctr_unique_PNFs
1294
1295         dr_print tc_info
1296         dr_print execution_time
1297         dr_print feeds
1298         dr_print ctr_publish_query
1299         dr_print feeds/ctr_publish_query
1300         dr_print ctr_publish_query_bad_file_prefix
1301         dr_print feeds/ctr_publish_query_bad_file_prefix
1302         dr_print ctr_publish_query_published
1303         dr_print feeds/ctr_publish_query_published
1304         dr_print ctr_publish_query_not_published
1305         dr_print feeds/ctr_publish_query_not_published
1306         dr_print ctr_publish_req
1307         dr_print feeds/ctr_publish_req
1308         dr_print ctr_publish_req_bad_file_prefix
1309         dr_print feeds/ctr_publish_req_bad_file_prefix
1310         dr_print ctr_publish_req_redirect
1311         dr_print feeds/ctr_publish_req_redirect
1312         dr_print ctr_publish_req_published
1313         dr_print feeds/ctr_publish_req_published
1314         dr_print ctr_published_files
1315         dr_print feeds/ctr_published_files
1316         dr_print ctr_double_publish
1317         dr_print feeds/ctr_double_publish
1318
1319         drr_print tc_info
1320         drr_print execution_time
1321         drr_print feeds
1322         drr_print ctr_publish_requests
1323         drr_print feeds/ctr_publish_requests
1324         drr_print ctr_publish_requests_bad_file_prefix
1325         drr_print feeds/ctr_publish_requests_bad_file_prefix
1326         drr_print ctr_publish_responses
1327         drr_print feeds/ctr_publish_responses
1328         drr_print dwl_volume
1329         drr_print feeds/dwl_volume
1330         drr_print time_lastpublish
1331         drr_print feeds/time_lastpublish
1332 }
1333
1334 # Print the test result
1335 print_result() {
1336
1337         TCTEST_END=$SECONDS
1338         duration=$((TCTEST_END-TCTEST_START))
1339
1340         echo "-------------------------------------------------------------------------------------------------"
1341         echo "-------------------------------------     Test case: "$ATC
1342         echo "-------------------------------------     Ended:     "$(date)
1343         echo "-------------------------------------------------------------------------------------------------"
1344         echo "-- Description: "$TC_ONELINE_DESCR
1345         echo "-- Execution time: " $duration " seconds"
1346         echo "-------------------------------------------------------------------------------------------------"
1347         echo "-------------------------------------     RESULTS"
1348         echo ""
1349
1350
1351         total=$((RES_PASS+RES_FAIL))
1352         if [ $RES_TEST -eq 0 ]; then
1353                 echo -e "\033[1mNo tests seem to have executed. Check the script....\033[0m"
1354                 echo -e "\033[31m\033[1m ___  ___ ___ ___ ___ _____   ___ _   ___ _   _   _ ___ ___ \033[0m"
1355                 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_   _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
1356                 echo -e "\033[31m\033[1m\__ \ (__|   /| ||  _/ | |   | _/ _ \ | || |_| |_| |   / _| \033[0m"
1357                 echo -e "\033[31m\033[1m|___/\___|_|_\___|_|   |_|   |_/_/ \_\___|____\___/|_|_\___|\033[0m"
1358         elif [ $total != $RES_TEST ]; then
1359                 echo -e "\033[1mTotal number of tests does not match the sum of passed and failed tests. Check the script....\033[0m"
1360                 echo -e "\033[31m\033[1m ___  ___ ___ ___ ___ _____   ___ _   ___ _   _   _ ___ ___ \033[0m"
1361                 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_   _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
1362                 echo -e "\033[31m\033[1m\__ \ (__|   /| ||  _/ | |   | _/ _ \ | || |_| |_| |   / _| \033[0m"
1363                 echo -e "\033[31m\033[1m|___/\___|_|_\___|_|   |_|   |_/_/ \_\___|____\___/|_|_\___|\033[0m"
1364         elif [ $RES_PASS = $RES_TEST ]; then
1365                 echo -e "All tests \033[32m\033[1mPASS\033[0m"
1366                 echo -e "\033[32m\033[1m  ___  _   ___ ___ \033[0m"
1367                 echo -e "\033[32m\033[1m | _ \/_\ / __/ __| \033[0m"
1368                 echo -e "\033[32m\033[1m |  _/ _ \\__ \__ \\ \033[0m"
1369                 echo -e "\033[32m\033[1m |_|/_/ \_\___/___/ \033[0m"
1370                 echo ""
1371
1372                 # Update test suite counter
1373                 if [ -f .tmp_tcsuite_pass_ctr ]; then
1374                         tmpval=$(< .tmp_tcsuite_pass_ctr)
1375                         ((tmpval++))
1376                         echo $tmpval > .tmp_tcsuite_pass_ctr
1377                 fi
1378                 if [ -f .tmp_tcsuite_pass ]; then
1379                         echo " - "$ATC " -- "$TC_ONELINE_DESCR"  Execution time: "$duration" seconds" >> .tmp_tcsuite_pass
1380                 fi
1381         else
1382                 echo -e "One or more tests with status  \033[31m\033[1mFAIL\033[0m "
1383                 echo -e "\033[31m\033[1m  ___ _   ___ _    \033[0m"
1384                 echo -e "\033[31m\033[1m | __/_\ |_ _| |   \033[0m"
1385                 echo -e "\033[31m\033[1m | _/ _ \ | || |__ \033[0m"
1386                 echo -e "\033[31m\033[1m |_/_/ \_\___|____|\033[0m"
1387                 echo ""
1388                 # Update test suite counter
1389                 if [ -f .tmp_tcsuite_fail_ctr ]; then
1390                         tmpval=$(< .tmp_tcsuite_fail_ctr)
1391                         ((tmpval++))
1392                         echo $tmpval > .tmp_tcsuite_fail_ctr
1393                 fi
1394                 if [ -f .tmp_tcsuite_fail ]; then
1395                         echo " - "$ATC " -- "$TC_ONELINE_DESCR"  Execution time: "$duration" seconds" >> .tmp_tcsuite_fail
1396                 fi
1397         fi
1398
1399         echo "++++ Number of tests:        "$RES_TEST
1400         echo "++++ Number of passed tests: "$RES_PASS
1401         echo "++++ Number of failed tests: "$RES_FAIL
1402         echo "-------------------------------------     Test case complete    ---------------------------------"
1403         echo "-------------------------------------------------------------------------------------------------"
1404         echo ""
1405 }