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