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