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