aboutsummaryrefslogtreecommitdiffstats
path: root/test/mocks/datafilecollector-testharness/common/testcase_common.sh
blob: deed26dfda628bd756fa3b7381e93ff96a76256e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
#!/bin/bash

. ../common/test_env.sh



echo "Test case started as: ${BASH_SOURCE[$i+1]} "$1 $2

# Script containing all functions needed for auto testing of test cases
# Arg: local [<image-tag>] ]| remote [<image-tag>] ]| remote-remove [<image-tag>]] | manual-container | manual-app

STARTED_DFCS="" #DFC app names added to this var to keep track of started container in the script
START_ARG=$1
IMAGE_TAG="latest"

if [ $# -gt 1 ]; then
	if [[ "$2" =~ ^1.1.* ]]; then
		echo "This version of auto-test does not support DFC image version of 1.1.X"
		exit 1
	fi
	IMAGE_TAG=$2
fi

if [ $# -lt 1 ] || [ $# -gt 2 ]; then
	echo "Expected arg: local [<image-tag>] ]| remote [<image-tag>] ]| remote-remove [<image-tag>]] | manual-container | manual-app"
	exit 1
elif [ $1 == "local" ]; then
	if [ -z $DFC_LOCAL_IMAGE ]; then
		echo "DFC_LOCAL_IMAGE not set in test_env"
		exit 1
	fi
	DFC_IMAGE=$DFC_LOCAL_IMAGE":"$IMAGE_TAG
elif [ $1 == "remote" ] || [ $1 == "remote-remove" ]; then
	if [ -z $DFC_REMOTE_IMAGE ]; then
		echo "DFC_REMOTE_IMAGE not set in test_env"
		exit 1
	fi
	DFC_IMAGE=$DFC_REMOTE_IMAGE":"$IMAGE_TAG
elif [ $1 == "manual-container" ] && [ $# -eq 1 ]; then
	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}'"
elif [ $1 == "manual-app" ] && [ $# -eq 1 ]; then
	echo "DFC is expected to be started manually, when prompted, as a java application"
else
	echo "Expected arg: local [<image-tag>] ]| remote [<image-tag>] ]| remote-remove [<image-tag>]] | manual-container | manual-app"
	exit 1
fi

# Set a description string for the test case
if [ -z "$TC_ONELINE_DESCR" ]; then
	TC_ONELINE_DESCR="<no-description>"
	echo "No test case description found, TC_ONELINE_DESCR should be set on in the test script , using "$TC_ONELINE_DESCR
fi

# Counter for test suites
if [ -f .tmp_tcsuite_ctr ]; then
	tmpval=$(< .tmp_tcsuite_ctr)
	((tmpval++))
	echo $tmpval > .tmp_tcsuite_ctr
fi

# Create a test case id, ATC (Auto Test Case), from the name of the test case script.
# FTC1.sh -> ATC == FTC1
ATC=$(basename "${BASH_SOURCE[$i+1]}" .sh)

# Create the logs dir if not already created in the current dir
if [ ! -d "logs" ]; then
    mkdir logs
fi

TESTLOGS=$PWD/logs

# Create a log dir for the test case
mkdir -p $TESTLOGS/$ATC

# Clear the log dir for the test case
rm $TESTLOGS/$ATC/*.log &> /dev/null

# Log all output from the test case to a TC log
TCLOG=$TESTLOGS/$ATC/TC.log
exec &>  >(tee ${TCLOG})

#Variables for counting tests as well as passed and failed tests
RES_TEST=0
RES_PASS=0
RES_FAIL=0
TCTEST_START=$SECONDS

echo "-------------------------------------------------------------------------------------------------"
echo "-----------------------------------      Test case: "$ATC
echo "-----------------------------------      Started:   "$(date)
echo "-------------------------------------------------------------------------------------------------"
echo "-- Description: "$TC_ONELINE_DESCR
echo "-------------------------------------------------------------------------------------------------"
echo "-----------------------------------      Test case setup      -----------------------------------"

if [ -z "$SIM_GROUP" ]; then
		SIM_GROUP=$PWD/../simulator-group
		if [ ! -d  $SIM_GROUP ]; then
			echo "Trying to set env var SIM_GROUP to dir 'simulator-group' in the integration repo, but failed."
			echo "Please set the SIM_GROUP manually in the test_env.sh"
			exit 1
		else
			echo "SIM_GROUP auto set to: " $SIM_GROUP
		fi
elif [ $SIM_GROUP = *simulator_group ]; then
			echo "Env var SIM_GROUP does not seem to point to dir 'simulator-group' in the integration repo, check test_env.sh"
			exit 1
fi

echo ""

if [ $1 !=  "manual-container" ] && [ $1 !=  "manual-app" ]; then
	echo -e "DFC image tag set to: \033[1m" $IMAGE_TAG"\033[0m"
	echo "Configured image for DFC app(s) (${1}): "$DFC_IMAGE
	tmp_im=$(docker images ${DFC_IMAGE} | grep -v REPOSITORY)

	if [ $1 == "local" ]; then
		if [ -z "$tmp_im" ]; then
			echo "Local image (non nexus) "$DFC_IMAGE" does not exist in local registry, need to be built"
			exit 1
		else
			echo -e "DFC local image: \033[1m"$tmp_im"\033[0m"
			echo "If the DFC image seem outdated, rebuild the image and run the test again."
		fi
	elif [ $1 == "remote" ] || [ $1 == "remote-remove" ]; then

		if [ $1 == "remote-remove" ]; then
			echo "Attempt to stop dfc_app container(s) if running"
			docker stop $(docker ps -q --filter name=${DFC_APP_BASE}]) &> /dev/null
			docker rm $(docker ps -q --filter name=${DFC_APP_BASE}) &> /dev/null
			docker rmi $DFC_IMAGE &> /dev/null
			tmp_im=""
		fi
		if [ -z "$tmp_im" ]; then
			echo "Pulling DFC image from nexus: "$DFC_IMAGE
			docker pull $DFC_IMAGE	 > /dev/null
			tmp_im=$(docker images ${DFC_IMAGE} | grep -v REPOSITORY)
			if [ -z "$tmp_im" ]; then
				echo "Image could not be pulled"
				exit 1
			fi
			echo -e "DFC image: \033[1m"$tmp_im"\033[0m"
		else
			echo -e "DFC image: \033[1m"$tmp_im"\033[0m"
			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'"
		fi
	fi
fi



echo ""

echo "Building images for the simulators if needed, MR, DR, DR Redir and FTPS simulators"
curdir=$PWD
cd $SIM_GROUP
cd ../dr-sim
docker build -t drsim_common:latest . &> /dev/null
cd ../mr-sim
docker build -t mrsim:latest . &> /dev/null
cd ../ftps-sftp-server
docker build -t ftps_vsftpd:latest -f Dockerfile-ftps . &> /dev/null
cd $curdir

echo ""

echo "Local registry images for simulators:"
echo "MR simulator        " $(docker images | grep mrsim)
echo "DR simulator:       " $(docker images | grep drsim_common)
echo "DR redir simulator: " $(docker images | grep drsim_common)
echo "SFTP:               " $(docker images | grep atmoz/sftp)
echo "FTPS:               " $(docker images | grep ftps_vsftpd)
echo "Consul:             " $(docker images | grep consul)
echo "CBS:                " $(docker images | grep platform.configbinding.app)
echo ""

#Configure MR sim to use correct host:port for running dfc as an app or as a container
#Configure DR sim with correct address for DR redirect simulator
if [ $START_ARG == "manual-app" ]; then
	export SFTP_SIMS=$SFTP_SIMS_LOCALHOST
	export FTPS_SIMS=$FTPS_SIMS_LOCALHOST
	export DR_REDIR_SIM="localhost"
fi
#else
#	export SFTP_SIMS=$SFTP_SIMS_CONTAINER
#	export FTPS_SIMS=$FTPS_SIMS_CONTAINER
#	export DR_REDIR_SIM="drsim_redir"
#fi

echo "-----------------------------------      Test case steps      -----------------------------------"

# Print error info for the call in the parent script (test case). Arg: <error-message-to-print>
# Not to be called from test script.
__print_err() {
    echo ${FUNCNAME[1]} " "$1" " ${BASH_SOURCE[$i+2]} " line" ${BASH_LINENO[$i+1]}
}
# Execute curl using the host and variable. Arg: <host and variable-name>  [ <flag-to-strip-new-line> ]
#<flag-to-strip-new-line> may contain any string, it is just a flag
# Returns the variable value (if success) and return code 0 or an error message and return code 1
__do_curl() {
	res=$(curl -skw "%{http_code}" $1)
	http_code="${res:${#res}-3}"
	if [ ${#res} -eq 3 ]; then
  		echo "<no-response-from-server>"
		return 1
	else
		if [ $http_code -lt 200 ] && [ $http_code -gt 299]; then
			echo "<not found, resp:${http_code}>"
			return 1
		fi
		if [ $# -eq 2 ]; then
  			echo "${res:0:${#res}-3}" | xargs
		else
  			echo "${res:0:${#res}-3}"
		fi

		return 0
	fi
}

# Test a simulator variable value towards  target value using an condition operator with an optional timeout.
# Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value>  - This test is done
# immediately and sets pass or fail depending on the result of comparing variable and target using the operator.
# Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> <timeout>  - This test waits up to the timeout
# before setting pass or fail depending on the result of comparing variable and target using the operator.
# Not to be called from test script.

__var_test() {
	if [ $# -eq 6 ]; then
		echo -e "---- ${1} sim test criteria: \033[1m ${3} \033[0m ${4} ${5} within ${6} seconds ----"
		((RES_TEST++))
		start=$SECONDS
		ctr=0
		for (( ; ; ))
		do
			result="$(__do_curl $2$3)"
			retcode=$?
			result=${result//[[:blank:]]/} #Strip blanks
			duration=$((SECONDS-start))
			if [ $((ctr%30)) -eq 0 ]; then
				echo "  Result=${result} after ${duration} seconds"
				for (( i=0; i<=$DFC_MAX_IDX; i++ )); do
					if [[ $STARTED_DFCS =~ "_"$DFC_APP_BASE$i"_" ]]; then
						echo "    HB ${DFC_APP_BASE}${i}: $(__do_curl http://127.0.0.1:$(($DFC_PORT+$i))/status strip)"
					fi
				done
			else
				echo -ne "  Result=${result} after ${duration} seconds\033[0K\r"
			fi
			let ctr=ctr+1
			if [ $retcode -ne 0 ]; then
				if [ $duration -gt $6 ]; then
					((RES_FAIL++))
					echo -e "----  \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5}  not reached in ${6} seconds, result = ${result} ----"
					return
				fi
			elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
				((RES_PASS++))
				echo -e "  Result=${result} after ${duration} seconds\033[0K\r"
				echo -e "----  \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds ----"
				return
			elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
				((RES_PASS++))
				echo -e "  Result=${result} after ${duration} seconds\033[0K\r"
				echo -e "----  \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds, result = ${result}  ----"
				return
			elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
				((RES_PASS++))
				echo -e "  Result=${result} after ${duration} seconds\033[0K\r"
				echo -e "----  \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds, result = ${result}  ----"
				return
			elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
				((RES_PASS++))
				echo -e "  Result=${result} after ${duration} seconds\033[0K\r"
				echo -e "----  \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds, result = ${result}  ----"
				return
			else
				if [ $duration -gt $6 ]; then
					((RES_FAIL++))
					echo -e "----  \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5}  not reached in ${6} seconds, result = ${result} ----"
					return
				fi
			fi
			sleep 1
		done
	elif [ $# -eq 5 ]; then
		echo -e "---- ${1} sim test criteria: \033[1m ${3} \033[0m ${4} ${5} ----"
		((RES_TEST++))
		result="$(__do_curl $2$3)"
		retcode=$?
		result=${result//[[:blank:]]/}  #Strip blanks
		if [ $retcode -ne 0 ]; then
			((RES_FAIL++))
			echo -e "----  \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached, result = ${result} ----"
		elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
			((RES_PASS++))
			echo -e "----  \033[32m\033[1mPASS\033[0m - Test criteria met"
		elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
			((RES_PASS++))
			echo -e "----  \033[32m\033[1mPASS\033[0m - Test criteria met, result = ${result} ----"
		elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
			((RES_PASS++))
			echo -e "----  \033[32m\033[1mPASS\033[0m - Test criteria met, result = ${result} ----"
		elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
			((RES_PASS++))
			echo -e "----  \033[32m\033[1mPASS\033[0m - Test criteria met, result = ${result} ----"
		else
			((RES_FAIL++))
			echo -e "----  \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached, result = ${result} ----"
		fi
	else
		echo "Wrong args to __var_test, needs five or six args: <simulator-name> <host> <variable-name> <condition-operator> <target-value> [ <timeout> ]"
		exit 1
	fi
}
# Stops a named container
__docker_stop() {
	if [ $# -ne 1 ]; then
		echo "__docker_stop need 1 arg <container-name>"
		exit 1
	fi
	tmp=$(docker stop $1  2>/dev/null)
	if [ -z $tmp ] || [ $tmp != $1 ]; then
		echo " ${1} container not stopped or not existing"
	else
		echo " ${1} container stopped"
	fi
}

# Starts a named container (that has previously been stopped)
__docker_start() {
	if [ $# -ne 1 ]; then
		echo "__docker_start need 1 arg <container-name>"
		exit 1
	fi
	tmp=$(docker start $1  2>/dev/null)
	if [ -z $tmp ] || [ $tmp != $1 ]; then
		echo " ${1} container not started or not existing"
	else
		echo " ${1} container started"
	fi
}

# Removes a named container
__docker_rm() {
	if [ $# -ne 1 ]; then
		echo "__docker_rm need 1 arg <container-name>"
		exit 1
	fi
	tmp=$(docker rm $1  2>/dev/null)
	if [ -z $tmp ] || [ $tmp != $1 ]; then
		echo " ${1} container not removed or not existing"
	else
		echo " ${1} container removed"
	fi
}

__start_dfc_image() {
	set -x
	if [ $# != 2 ]; then
    	__print_err "need tow args, <dfc-instance-name> 0.."$$DFC_MAX_IDX
		exit 1
	fi

	if [ $2 -lt 0 ] || [ $2 -gt $DFC_MAX_IDX ]; then
		__print_err "need two args, <dfc-instance-name> 0.."$DFC_MAX_IDX
		exit 1
	fi
	appname=$1
	localport=$(($DFC_PORT + $2))
	localport_secure=$(($DFC_PORT_SECURE + $2))

	echo "Creating docker network $DOCKER_SIM_NWNAME, if needed"

	docker network ls| grep $DOCKER_SIM_NWNAME > /dev/null || docker network create $DOCKER_SIM_NWNAME

	echo "Starting DFC: " $appname " with ports mapped to " $localport " and " $localport_secure " in docker network "$DOCKER_SIM_NWNAME
	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 HOSTNAME=$appname --name $appname $DFC_IMAGE
	sleep 3
	set +x
	dfc_started=false
	for i in {1..10}; do
		if [ $(docker inspect --format '{{ .State.Running }}' $appname) ]
		 	then
			 	echo " Image: $(docker inspect --format '{{ .Config.Image }}' ${appname})"
		   		echo "DFC container ${appname} running"
				dfc_started=true
		   		break
		 	else
		   		sleep $i
	 	fi
	done
	if ! [ $dfc_started  ]; then
		echo "DFC container ${appname} could not be started"
		exit 1
	fi

	dfc_hb=false
	echo "Waiting for DFC ${appname} heartbeat..."
	for i in {1..10}; do
		result="$(__do_curl http://127.0.0.1:${localport}/heartbeat)"
		if [ $? -eq 0 ]; then
	   		echo "DFC ${appname} responds to heartbeat: " $result
	   		dfc_hb=true
	   		result="$(__do_curl http://127.0.0.1:${localport}/actuator/info)"
	   		echo "DFC ${appname} image build info: " $result
	   		break
	 	else
	   		sleep $i
	 	fi
	done

	if [ "$dfc_hb" = "false"  ]; then
		echo "DFC ${appname} did not respond to heartbeat"
		exit 1
	fi
}

# Function for waiting for named container to be started manually.
__wait_for_container() {
	start=$SECONDS
	if [ $# != 2 ]; then
		echo "Need one arg: <container-name> <instance-id>"
		exit 1
	fi
	http=$(($DFC_PORT+$2))
	https=$((DFC_PORT_SECURE+$2))
	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}"
	echo "Waiting for container with name '${1}' to be started manually...."

	for (( ; ; ))
	do
		if [ $(docker inspect --format '{{ .State.Running }}' $1 2> /dev/null) ]; then
   			echo "Container running: "$1
   			break
 		else
	 		duration=$((SECONDS-start))
			echo -ne "  Waited ${duration} seconds\033[0K\r"
   			sleep 1
 		fi
	done

	echo "Connecting container "$1" to simulator network "$DOCKER_SIM_NWNAME
	docker network connect $DOCKER_SIM_NWNAME $1
}

#WFunction for waiting for named container to be stopped manually.
__wait_for_container_gone() {
	start=$SECONDS
	if [ $# != 1 ]; then
		echo "Need one arg: <container-name>"
		exit 1
	fi
	echo "Disconnecting container "$1" from simulator network "$DOCKER_SIM_NWNAME
	docker network disconnect $DOCKER_SIM_NWNAME $1
	echo "Waiting for container with name '${1}' to be stopped manually...."

	for (( ; ; ))
	do
		if [ $(docker inspect --format '{{ .State.Running }}' $1 2> /dev/null) ]; then
	 		duration=$((SECONDS-start))
			echo -ne "  Waited ${duration} seconds\033[0K\r"
   			sleep 1
		else
   			echo "Container stopped: "$1
   			break
 		fi
	done
}

#Function for waiting to dfc to be started manually
__wait_for_dfc() {
	http=$(($DFC_PORT+$2))
	https=$((DFC_PORT_SECURE+$2))
	echo "The app is expected to listen to http port ${http} and https port ${https}"
	echo "The app shall use 'localhost' and '8500' for CONSUL_HOST and CONSUL_PORT."
	echo "The app shale use 'config-binding-service-localhost' for CONFIG_BINDING_SERVICE"
	echo "The app shall use ${1} for HOSTNAME."
	read -p "Press enter to continue when app mapping to ${1} has been manually started"
}

#Function for waiting to dfc to be stopped manually
__wait_for_dfc_gone() {
	read -p "Press enter to continue when when app mapping to ${1} has been manually stopped"
}

#############################################################
############## Functions for auto test scripts ##############
#############################################################

# Print the env variables needed for the simulators and their setup
log_sim_settings() {
	echo "Simulator settings"
	echo "MR_TC=                 "$MR_TC
	echo "MR_GROUPS=             "$MR_GROUPS
    echo "MR_FILE_PREFIX_MAPPING="$MR_FILE_PREFIX_MAPPING
	echo "DR_TC=                 "$DR_TC
	echo "DR_FEEDS=              "$DR_FEEDS
	echo "DR_REDIR_SIM=          "$DR_REDIR_SIM
	echo "DR_REDIR_TC=           "$DR_REDIR_TC
	echo "DR_REDIR_FEEDS=        "$DR_REDIR_FEEDS

	echo "NUM_FTPFILES=          "$NUM_FTPFILES
	echo "NUM_PNFS=              "$NUM_PNFS
	echo "FILE_SIZE=             "$FILE_SIZE
	echo "FTP_TYPE=              "$FTP_TYPE
	echo "FTP_FILE_PREFIXES=     "$FTP_FILE_PREFIXES
	echo "NUM_FTP_SERVERS=       "$NUM_FTP_SERVERS
	echo "SFTP_SIMS=             "$SFTP_SIMS
	echo "FTPS_SIMS=             "$FTPS_SIMS
	echo ""
}

# Stop and remove all containers including dfc app and simulators
clean_containers() {
	echo "Stopping all containers, dfc app(s) and simulators with name prefix 'dfc_'"
	docker stop $(docker ps -q --filter name=dfc_) &> /dev/null
	echo "Removing all containers, dfc app and simulators with name prefix 'dfc_'"
	docker rm $(docker ps -a -q --filter name=dfc_) &> /dev/null
	echo "Removing unused docker networks with substring 'dfc' in network name"
	docker network rm $(docker network ls -q --filter name=dfc)
	echo ""
}

# Start all simulators in the simulator group
start_simulators() {

	echo "Starting all simulators"
	curdir=$PWD
	cd $SIM_GROUP
	$SIM_GROUP/simulators-start.sh
	cd $curdir
	echo ""
}

# Start the dfc application
start_dfc() {

	if [ $# != 1 ]; then
    	__print_err "need one arg, <dfc-instance-id>"
		exit 1
	fi

	if [ $1 -lt 0 ] || [ $1 -gt $DFC_MAX_IDX ]; then
		__print_err "arg should be 0.."$DFC_MAX_IDX
		exit 1
	fi
	appname=$DFC_APP_BASE$1
	STARTED_DFCS=$STARTED_DFCS"_"$appname"_"

	if [ $START_ARG == "local" ] || [ $START_ARG == "remote" ] ||  [ $START_ARG == "remote-remove" ]; then
		__start_dfc_image $appname $1
	elif [ $START_ARG == "manual-container" ]; then
		__wait_for_container $appname $1
	elif [ $START_ARG == "manual-app" ]; then
		__wait_for_dfc $appname $1
	fi
}

# Configure consul with dfc config, args <app|dmaap> <dfc-instance-id> <json-file-path>
# Not intended to be called directly by test scripts.
__consul_config() {

	if [ $# != 3 ]; then
    	__print_err "need three args, <app|dmaap> <dfc-instance-id> <json-file-path>"
		exit 1
	fi

	if [ $2 -lt 0 ] || [ $2 -gt $DFC_MAX_IDX ]; then
		__print_err "dfc-instance-id should be 0.."$DFC_MAX_IDX
		exit 1
	fi
	if ! [ -f $3 ]; then
		__print_err "json file does not extis: "$3
		exit 1
	fi

	if [ $1 == "app" ]; then
		appname=$DFC_APP_BASE$2
	elif [ $1 == "dmaap" ]; then
		appname=$DFC_APP_BASE$2":dmaap"
	else
		__print_err "config type should be 'app' or 'dmaap'"
		exit 1
	fi

	echo "Configuring consul for " $appname " from " $3
	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 "@"$3 >/dev/null
}

# Configure consul with dfc app config, args <dfc-instance-id> <json-file-path>
consul_config_app() {
	if [ $START_ARG == "manual-app" ]; then
		echo "Replacing 'mrsim' with 'localhost' in json app config for consul"
		sed 's/mrsim/localhost/g' $2 > .tmp_app.json
		__consul_config app $1 .tmp_app.json
	else
		__consul_config app $1 $2
	fi
}

# Configure consul with dfc dmaap config, args <dfc-instance-id> <json-file-path>
consul_config_dmaap() {
	if [ $START_ARG == "manual-app" ]; then
		echo "Replacing 'drsim' with 'localhost' in json dmaap config for consul"
		sed 's/drsim/localhost/g' $2 > .tmp_dmaap.json
		__consul_config dmaap $1 .tmp_dmaap.json
	else
		__consul_config dmaap $1 $2
	fi
}

# Stop and remove the dfc app container
kill_dfc() {

	if [ $# != 1 ]; then
    	__print_err "need one arg, <dfc-instance-id>"
		exit 1
	fi

	if [ $1 -lt 0 ] || [ $1 -gt $DFC_MAX_IDX ]; then
		__print_err "arg should be 0.."$DFC_MAX_IDX
		exit 1
	fi
	appname=$DFC_APP_BASE$1

	echo "Killing DFC, instance id: "$1

	if [ $START_ARG == "local" ] || [ $START_ARG == "remote" ] ||  [ $START_ARG == "remote-remove" ]; then
		__docker_stop $appname
		__docker_rm $appname
	elif [ $START_ARG == "manual-container" ]; then
		__wait_for_container_gone $appname
	elif [ $START_ARG == "manual-app" ]; then
		__wait_for_dfc_gone $appname
	fi
}

# Stop and remove the DR simulator container
kill_dr() {
	echo "Killing DR sim"
	__docker_stop dfc_dr-sim
	__docker_rm dfc_dr-sim
}

# Stop and remove the DR redir simulator container
kill_drr() {
	echo "Killing DR redir sim"
	__docker_stop dfc_dr-redir-sim
	__docker_rm dfc_dr-redir-sim
}

# Stop and remove the MR simulator container
kill_mr() {
	echo "Killing MR sim"
	__docker_stop dfc_mr-sim
	__docker_rm dfc_mr-sim
}

# Stop and remove the SFTP container, arg: <sftp-instance-id>
kill_sftp() {

	if [ $# != 1 ]; then
    	__print_err "need one arg, <sftp-instance-id>"
		exit 1
	fi

	if [ $1 -lt 0 ] || [ $1 -gt $FTP_MAX_IDX ]; then
		__print_err "arg should be 0.."$FTP_MAX_IDX
		exit 1
	fi
	appname=$SFTP_BASE$1

	echo "Killing SFTP, instance id: "$1

	__docker_stop $appname
	__docker_rm $appname
}

# Stop SFTP container, arg: <sftp-instance-id>
stop_sftp() {

	if [ $# != 1 ]; then
    	__print_err "need one arg, <sftp-instance-id>"
		exit 1
	fi

	if [ $1 -lt 0 ] || [ $1 -gt $FTP_MAX_IDX ]; then
		__print_err "arg should be 0.."$FTP_MAX_IDX
		exit 1
	fi
	appname=$SFTP_BASE$1

	echo "Stopping SFTP, instance id: "$1

	__docker_stop $appname
}

# Starts a stopped SFTP container, arg: <sftp-instance-id>
start_sftp() {

	if [ $# != 1 ]; then
    	__print_err "need one arg, <sftp-instance-id>"
		exit 1
	fi

	if [ $1 -lt 0 ] || [ $1 -gt $FTP_MAX_IDX ]; then
		__print_err "arg should be 0.."$FTP_MAX_IDX
		exit 1
	fi
	appname=$SFTP_BASE$1

	echo "Starting SFTP, instance id: "$1

	__docker_start $appname
}

# Stop and remove the FTPS container, arg: <ftps-instance-id>
kill_ftps() {

	if [ $# != 1 ]; then
    	__print_err "need one arg, <ftpS-instance-id>"
		exit 1
	fi

	if [ $1 -lt 0 ] || [ $1 -gt $FTP_MAX_IDX ]; then
		__print_err "arg should be 0.."$FTP_MAX_IDX
		exit 1
	fi
	appname=$FTPS_BASE$1

	echo "Killing FTPS, instance id: "$1

	__docker_stop $appname
	__docker_rm $appname
}

# Stop FTPS container, arg: <ftps-instance-id>
stop_ftps() {

	if [ $# != 1 ]; then
    	__print_err "need one arg, <ftps-instance-id>"
		exit 1
	fi

	if [ $1 -lt 0 ] || [ $1 -gt $FTP_MAX_IDX ]; then
		__print_err "arg should be 0.."$FTP_MAX_IDX
		exit 1
	fi
	appname=$FTPS_BASE$1

	echo "Stopping FTPS, instance id: "$1

	__docker_stop $appname
}

# Starts a stopped FTPS container, arg: <ftps-instance-id>
start_ftps() {

	if [ $# != 1 ]; then
    	__print_err "need one arg, <ftps-instance-id>"
		exit 1
	fi

	if [ $1 -lt 0 ] || [ $1 -gt $FTP_MAX_IDX ]; then
		__print_err "arg should be 0.."$FTP_MAX_IDX
		exit 1
	fi
	appname=$FTPS_BASE$1

	echo "Starting FTPS, instance id: "$1

	__docker_start $appname
}

# Print a variable value from the MR simulator. Arg: <variable-name>
mr_print() {
	if [ $# != 1 ]; then
    	__print_err "need one arg, <sim-param>"
		exit 1
	fi
	echo -e "---- MR sim, \033[1m $1 \033[0m: $(__do_curl http://127.0.0.1:$MR_PORT/$1)"
}

# Print a variable value from the MR https simulator. Arg: <variable-name>
mr_secure_print() {
	if [ $# != 1 ]; then
    	__print_err "need one arg, <sim-param>"
		exit 1
	fi
	echo -e "---- MR sim, \033[1m $1 \033[0m: $(__do_curl https://127.0.0.1:$MR_PORT_SECURE/$1)"
}

# Print a variable value from the DR simulator. Arg: <variable-name>
dr_print() {
	if [ $# != 1 ]; then
    	__print_err "need one arg, <sim-param>"
		exit 1
	fi
	echo -e "---- DR sim, \033[1m $1 \033[0m: $(__do_curl http://127.0.0.1:$DR_PORT/$1)"
}

# Print a variable value from the DR redir simulator. Arg: <variable-name>
drr_print() {
	if [ $# != 1 ]; then
    	__print_err "need one arg, <sim-param>"
		exit 1
	fi
	echo -e "---- DR redir sim, \033[1m $1 \033[0m: $(__do_curl http://127.0.0.1:$DRR_PORT/$1)"
}
# Print a variable value from dfc. Arg: <dfc-instance-id> <variable-name>
dfc_print() {
	if [ $# != 2 ]; then
    	__print_err "need two args, <dfc-instance-id> <dfc-param>"
		exit 1
	fi
	if [ $1 -lt 0 ] || [ $1 -gt $DFC_MAX_IDX ]; then
		__print_err "dfc instance id should be in range 0.."DFC_MAX_IDX
		exit 1
	fi
	localport=$(($DFC_PORT + $1))
	appname=$DFC_APP_BASE$1
	echo -e "---- DFC $appname, \033[1m $2 \033[0m: $(__do_curl http://127.0.0.1:$localport/$2)"
}

# Read a variable value from MR sim and send to stdout. Arg: <variable-name>
mr_read() {
	echo "$(__do_curl http://127.0.0.1:$MR_PORT/$1)"
}

# Read a variable value from MR https sim and send to stdout. Arg: <variable-name>
mr_secure_read() {
	echo "$(__do_curl https://127.0.0.1:$MR_PORT_SECURE/$1)"
}

# Read a variable value from DR sim and send to stdout. Arg: <variable-name>
dr_read() {
	echo "$(__do_curl http://127.0.0.1:$DR_PORT/$1)"
}

# Read a variable value from DR redir sim and send to stdout. Arg: <variable-name>
drr_read() {
	echo "$(__do_curl http://127.0.0.1:$DRR_PORT/$1)"
}


# Sleep. Arg: <sleep-time-in-sec>
sleep_wait() {
	if [ $# != 1 ]; then
		__print_err "need one arg, <sleep-time-in-sec>"
		exit 1
	fi
	echo "---- Sleep for " $1 " seconds ----"
	start=$SECONDS
	duration=$((SECONDS-start))
	while [ $duration -lt $1 ]; do
		echo -ne "  Slept for ${duration} seconds\033[0K\r"
		sleep 1
		duration=$((SECONDS-start))
	done
	echo ""
}

# Sleep and print dfc heartbeat. Arg: <sleep-time-in-sec>
sleep_heartbeat() {
	if [ $# != 1 ]; then
		__print_err "need one arg, <sleep-time-in-sec>"
		exit 1
	fi
	echo "---- Sleep for " $1 " seconds ----"
	echo ""
	start=$SECONDS
	duration=$((SECONDS-start))
	ctr=0
	rows=0
	while [ $duration -lt $1 ]; do
		if [ $rows -eq 0 ]; then
			tput cuu1
		fi
		rows=0
		echo "  Slept for ${duration} seconds"
		if [ $((ctr%30)) -eq 0 ]; then
			for (( i=0; i<=$DFC_MAX_IDX; i++ )); do
				if [[ $STARTED_DFCS =~ "_"$DFC_APP_BASE$i"_" ]]; then
					let rows=rows+1
					echo "    HB ${DFC_APP_BASE}${i}: $(__do_curl http://127.0.0.1:$(($DFC_PORT+$i))/heartbeat)"
				fi
			done
		fi

		let ctr=ctr+1
		sleep 1
		duration=$((SECONDS-start))
	done
	echo ""
}

# Tests if a variable value in the MR simulator is equal to a target value and and optional timeout.
# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
# equal to the target or not.
# Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
# before setting pass or fail depending on if the variable value becomes equal to the target
# value or not.
mr_equal() {
	if [ $# -eq 2 ] || [ $# -eq 3 ]; then
		__var_test "MR" "http://127.0.0.1:$MR_PORT/" $1 "=" $2 $3
	else
		__print_err "Wrong args to mr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]"
	fi
}

mr_secure_equal() {
	if [ $# -eq 2 ] || [ $# -eq 3 ]; then
		__var_test "MR" "https://127.0.0.1:$MR_PORT_SECURE/" $1 "=" $2 $3
	else
		__print_err "Wrong args to mr_secure_equal, needs two or three args: <sim-param> <target-value> [ timeout ]"
	fi
}

# Tests if a variable value in the MR simulator is greater than a target value and and optional timeout.
# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
# greater than the target or not.
# Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
# before setting pass or fail depending on if the variable value greater than the target
# value or not.
mr_greater() {
	if [ $# -eq 2 ] || [ $# -eq 3 ]; then
		__var_test "MR" "http://127.0.0.1:$MR_PORT/" $1 ">" $2 $3
	else
		__print_err "Wrong args to mr_greater, needs two or three args: <sim-param> <target-value> [ timeout ]"
	fi
}

mr_secure_greater() {
	if [ $# -eq 2 ] || [ $# -eq 3 ]; then
		__var_test "MR" "https://127.0.0.1:$MR_PORT_SECURE/" $1 ">" $2 $3
	else
		__print_err "Wrong args to mr_secure_greater, needs two or three args: <sim-param> <target-value> [ timeout ]"
	fi
}

# Tests if a variable value in the MR simulator is less than a target value and and optional timeout.
# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
# less than the target or not.
# Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
# before setting pass or fail depending on if the variable value less than the target
# value or not.
mr_less() {
	if [ $# -eq 2 ] || [ $# -eq 3 ]; then
		__var_test "MR" "http://127.0.0.1:$MR_PORT/" $1 "<" $2 $3
	else
		__print_err "Wrong args to mr_less, needs two or three args: <sim-param> <target-value> [ timeout ]"
	fi
}
mr_secure_less() {
	if [ $# -eq 2 ] || [ $# -eq 3 ]; then
		__var_test "MR" "https://127.0.0.1:$MR_PORT_SECURE/" $1 "<" $2 $3
	else
		__print_err "Wrong args to mr_secure_less, needs two or three args: <sim-param> <target-value> [ timeout ]"
	fi
}

# Tests if a variable value in the MR simulator contains the target string and and optional timeout.
# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable contains
# the target or not.
# Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
# before setting pass or fail depending on if the variable value contains the target
# value or not.
mr_contain_str() {
	if [ $# -eq 2 ] || [ $# -eq 3 ]; then
		__var_test "MR" "http://127.0.0.1:$MR_PORT/" $1 "contain_str" $2 $3
	else
		__print_err "Wrong args to mr_contain_str, needs two or three args: <sim-param> <target-value> [ timeout ]"
	fi
}
mr_secure_contain_str() {
	if [ $# -eq 2 ] || [ $# -eq 3 ]; then
		__var_test "MR" "https://127.0.0.1:$MR_PORT_SECURE/" $1 "contain_str" $2 $3
	else
		__print_err "Wrong args to mr_secure_contain_str, needs two or three args: <sim-param> <target-value> [ timeout ]"
	fi
}

# Tests if a variable value in the DR simulator is equal to a target value and and optional timeout.
# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
# equal to the target or not.
# Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
# before setting pass or fail depending on if the variable value becomes equal to the target
# value or not.
dr_equal() {
	if [ $# -eq 2 ] || [ $# -eq 3 ]; then
		__var_test "DR" "http://127.0.0.1:$DR_PORT/" $1 "=" $2 $3
	else
		__print_err "Wrong args to dr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]"
	fi
}

# Tests if a variable value in the DR simulator is greater than a target value and and optional timeout.
# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
# greater than the target or not.
# Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
# before setting pass or fail depending on if the variable value greater than the target
# value or not.
dr_greater() {
	if [ $# -eq 2 ] || [ $# -eq 3 ]; then
		__var_test "DR" "http://127.0.0.1:$DR_PORT/" $1 ">" $2 $3
	else
		__print_err "Wrong args to dr_greater, needs two or three args: <sim-param> <target-value> [ timeout ]"
	fi
}

# Tests if a variable value in the DR simulator is less than a target value and and optional timeout.
# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
# less than the target or not.
# Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
# before setting pass or fail depending on if the variable value less than the target
# value or not.
dr_less() {
	if [ $# -eq 2 ] || [ $# -eq 3 ]; then
		__var_test "DR" "http://127.0.0.1:$DR_PORT/" $1 "<" $2 $3
	else
		__print_err "Wrong args to dr_less, needs two or three args: <sim-param> <target-value> [ timeout ]"
	fi
}

# Tests if a variable value in the DR simulator contains the target string and and optional timeout.
# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable contains
# the target or not.
# Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
# before setting pass or fail depending on if the variable value contains the target
# value or not.
dr_contain_str() {
	if [ $# -eq 2 ] || [ $# -eq 3 ]; then
		__var_test "DR" "http://127.0.0.1:$DR_PORT/" $1 "contain_str" $2 $3
	else
		__print_err "Wrong args to dr_contain_str, needs two or three args: <sim-param> <target-value> [ timeout ]"
	fi
}

# Tests if a variable value in the DR Redir simulator is equal to a target value and and optional timeout.
# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
# equal to the target or not.
# Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
# before setting pass or fail depending on if the variable value becomes equal to the target
# value or not.
drr_equal() {
	if [ $# -eq 2 ] || [ $# -eq 3 ]; then
		__var_test "DR REDIR" "http://127.0.0.1:$DRR_PORT/" $1 "=" $2 $3
	else
		__print_err "Wrong args to drr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]"
	fi
}


# Tests if a variable value in the DR Redir simulator is greater a target value and and optional timeout.
# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
# greater the target or not.
# Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
# before setting pass or fail depending on if the variable value greater than the target
# value or not.
drr_greater() {
	if [ $# -eq 2 ] || [ $# -eq 3 ]; then
		__var_test "DR REDIR" "http://127.0.0.1:$DRR_PORT/" $1 ">" $2 $3
	else
		__print_err "Wrong args to drr_greater, needs two or three args: <sim-param> <target-value> [ timeout ]"
	fi
}

# Tests if a variable value in the DR Redir simulator is less than a target value and and optional timeout.
# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
# less than the target or not.
# Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
# before setting pass or fail depending on if the variable value less than the target
# value or not.
drr_less() {
	if [ $# -eq 2 ] || [ $# -eq 3 ]; then
		__var_test "DR REDIR" "http://127.0.0.1:$DRR_PORT/" $1 "<" $2 $3
	else
		__print_err "Wrong args to drr_less, needs two or three args: <sim-param> <target-value> [ timeout ]"
	fi
}

# Tests if a variable value in the DR redir simulator contains the target string and and optional timeout.
# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable contains
# the target or not.
# Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
# before setting pass or fail depending on if the variable value contains the target
# value or not.
drr_contain_str() {
	if [ $# -eq 2 ] || [ $# -eq 3 ]; then
		__var_test "DR REDIR" "http://127.0.0.1:$DRR_PORT/" $1 "contain_str" $2 $3
	else
		__print_err "Wrong args to drr_contain_str, needs two or three args: <sim-param> <target-value> [ timeout ]"
	fi
}

#Test if a variable in the DFC contains a substring. Arg: <dfc-index> <variable-name> <substring-in-quotes>
dfc_contain_str() {
	if [ $# -eq 3 ]; then
		if [ $1 -lt 0 ] || [ $1 -gt $DFC_MAX_IDX ]; then
			__print_err "arg should be 0.."$DFC_MAX_IDX
			exit 1
		fi
		appname=$DFC_APP_BASE$1
		localport=$(($DFC_PORT + $1))
		echo -e "---- DFC test criteria: $appname \033[1m ${2} \033[0m contains: ${3} ----"
		((RES_TEST++))
		result="$(__do_curl http://127.0.0.1:$localport/${2})"
		if [[ $result =~ $3 ]]; then
			((RES_PASS++))
			echo -e "----  \033[32m\033[1mPASS\033[0m - Test criteria met"
		else
			((RES_FAIL++))
			echo -e "----  \033[31m\033[1mFAIL\033[0m - Target '${3}' not reached, result = ${result} ----"
		fi
	else
		echo "Wrong args to dfc_contain_str, needs three arg: <dfc-index> <dfc-variable> <str>"
		exit 1
	fi
}

# Store all dfc app and simulators log to the test case log dir. All logs gets a prefix to
# separate logs stored at different steps in the test script. Arg: <tc-id> <log-prefix>
store_logs() {
	if [ $# != 1 ]; then
    	__print_err "need one arg, <file-prefix>"
		exit 1
	fi
	echo "Storing all container logs and dfc app log using prefix: "$1
	if ! [ $START_ARG == "manual-app" ]; then
		for (( i=0; i<=$DFC_MAX_IDX; i++ )); do
			appname=$DFC_APP_BASE$i
			tmp=$(docker ps | grep $appname)
			if ! [ -z "$tmp" ]; then   #Only stored logs from running DFC apps
				docker cp $appname:/var/log/ONAP/application.log $TESTLOGS/$ATC/${1}_${appname}_application.log
				docker logs $appname > $TESTLOGS/$ATC/$1_$appname-docker.log 2>&1
			fi
		done
	fi
	docker logs dfc_mr-sim > $TESTLOGS/$ATC/$1_dfc_mr-sim-docker.log 2>&1
	docker logs dfc_dr-sim > $TESTLOGS/$ATC/$1_dfc_dr-sim-docker.log 2>&1
	docker logs dfc_dr-redir-sim > $TESTLOGS/$ATC/$1_dfc_dr-redir-sim-docker.log 2>&1

	for (( i=0; i<=$FTP_MAX_IDX; i++ )); do
		appname=$SFTP_BASE$i
		docker logs $appname > $TESTLOGS/$ATC/${1}_${appname}.log 2>&1
		appname=$FTPS_BASE$i
		docker logs $appname > $TESTLOGS/$ATC/${1}_${appname}.log 2>&1
	done

	docker logs dfc_consul > $TESTLOGS/$ATC/$1_consul.log 2>&1
	docker logs dfc_cbs > $TESTLOGS/$ATC/$1_cbs.log 2>&1
}
# Check the dfc application log, for all dfc instances, for WARN and ERR messages and print the count.
check_dfc_logs() {
	for (( i=0; i<=$DFC_MAX_IDX; i++ )); do
		appname=$DFC_APP_BASE$i
		tmp=$(docker ps | grep $appname)
		if ! [ -z "$tmp" ]; then  #Only check logs for running dfc_apps
			_check_dfc_log $appname
		fi
	done
}

# Check dfc app log for one dfc instance, arg <dfc-app-name>
_check_dfc_log() {
	echo "Checking $1 log $DFC_LOGPATH for WARNINGs and ERRORs"
	foundentries=$(docker exec -it $1 grep WARN /var/log/ONAP/application.log | wc -l)
	if [ $? -ne  0 ];then
		echo "  Problem to search $1 log $DFC_LOGPATH"
	else
		if [ $foundentries -eq 0 ]; then
			echo "  No WARN entries found in $1 log $DFC_LOGPATH"
		else
			echo -e "  Found \033[1m"$foundentries"\033[0m WARN entries in $1 log $DFC_LOGPATH"
		fi
	fi
	foundentries=$(docker exec -it $1 grep ERR $DFC_LOGPATH | wc -l)
	if [ $? -ne  0 ];then
		echo "  Problem to search $1 log $DFC_LOGPATH"
	else
		if [ $foundentries -eq 0 ]; then
			echo "  No ERR entries found in $1 log $DFC_LOGPATH"
		else
			echo -e "  Found \033[1m"$foundentries"\033[0m ERR entries in $1 log $DFC_LOGPATH"
		fi
	fi
}

print_all() {

	echo "---- DFC and all sim variables"

	for (( i=0; i<=$DFC_MAX_IDX; i++ )); do
		appname=$DFC_APP_BASE$i
		tmp=$(docker ps | grep $appname)
		if ! [ -z "$tmp" ]; then  #Only check running dfc_apps
			dfc_print $i status
		fi
	done


	mr_print tc_info
	mr_print status
	mr_print execution_time
	mr_print groups
	mr_print changeids
	mr_print fileprefixes
	mr_print exe_time_first_poll
	mr_print groups/exe_time_first_poll
	mr_print ctr_requests
	mr_print groups/ctr_requests
	mr_print ctr_responses
	mr_print groups/ctr_responses
	mr_print ctr_files
	mr_print groups/ctr_files
	mr_print ctr_unique_files
	mr_print groups/ctr_unique_files
	mr_print groups/ctr_events
	mr_print ctr_events
	mr_print ctr_unique_PNFs
	mr_print groups/ctr_unique_PNFs

	dr_print tc_info
	dr_print execution_time
	dr_print feeds
	dr_print ctr_publish_query
	dr_print feeds/ctr_publish_query
	dr_print ctr_publish_query_bad_file_prefix
	dr_print feeds/ctr_publish_query_bad_file_prefix
	dr_print ctr_publish_query_published
	dr_print feeds/ctr_publish_query_published
	dr_print ctr_publish_query_not_published
	dr_print feeds/ctr_publish_query_not_published
	dr_print ctr_publish_req
	dr_print feeds/ctr_publish_req
	dr_print ctr_publish_req_bad_file_prefix
	dr_print feeds/ctr_publish_req_bad_file_prefix
	dr_print ctr_publish_req_redirect
	dr_print feeds/ctr_publish_req_redirect
	dr_print ctr_publish_req_published
	dr_print feeds/ctr_publish_req_published
	dr_print ctr_published_files
	dr_print feeds/ctr_published_files
	dr_print ctr_double_publish
	dr_print feeds/ctr_double_publish

	drr_print tc_info
	drr_print execution_time
	drr_print feeds
	drr_print ctr_publish_requests
	drr_print feeds/ctr_publish_requests
	drr_print ctr_publish_requests_bad_file_prefix
	drr_print feeds/ctr_publish_requests_bad_file_prefix
	drr_print ctr_publish_responses
	drr_print feeds/ctr_publish_responses
	drr_print dwl_volume
	drr_print feeds/dwl_volume
	drr_print time_lastpublish
	drr_print feeds/time_lastpublish
}

# Print the test result
print_result() {

	TCTEST_END=$SECONDS
	duration=$((TCTEST_END-TCTEST_START))

	echo "-------------------------------------------------------------------------------------------------"
	echo "-------------------------------------     Test case: "$ATC
	echo "-------------------------------------     Ended:     "$(date)
	echo "-------------------------------------------------------------------------------------------------"
	echo "-- Description: "$TC_ONELINE_DESCR
	echo "-- Execution time: " $duration " seconds"
	echo "-------------------------------------------------------------------------------------------------"
	echo "-------------------------------------     RESULTS"
	echo ""


	total=$((RES_PASS+RES_FAIL))
	if [ $RES_TEST -eq 0 ]; then
		echo -e "\033[1mNo tests seem to have executed. Check the script....\033[0m"
 		echo -e "\033[31m\033[1m ___  ___ ___ ___ ___ _____   ___ _   ___ _   _   _ ___ ___ \033[0m"
 		echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_   _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
		echo -e "\033[31m\033[1m\__ \ (__|   /| ||  _/ | |   | _/ _ \ | || |_| |_| |   / _| \033[0m"
 		echo -e "\033[31m\033[1m|___/\___|_|_\___|_|   |_|   |_/_/ \_\___|____\___/|_|_\___|\033[0m"
	elif [ $total != $RES_TEST ]; then
		echo -e "\033[1mTotal number of tests does not match the sum of passed and failed tests. Check the script....\033[0m"
		echo -e "\033[31m\033[1m ___  ___ ___ ___ ___ _____   ___ _   ___ _   _   _ ___ ___ \033[0m"
		echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_   _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
		echo -e "\033[31m\033[1m\__ \ (__|   /| ||  _/ | |   | _/ _ \ | || |_| |_| |   / _| \033[0m"
 		echo -e "\033[31m\033[1m|___/\___|_|_\___|_|   |_|   |_/_/ \_\___|____\___/|_|_\___|\033[0m"
	elif [ $RES_PASS = $RES_TEST ]; then
		echo -e "All tests \033[32m\033[1mPASS\033[0m"
		echo -e "\033[32m\033[1m  ___  _   ___ ___ \033[0m"
		echo -e "\033[32m\033[1m | _ \/_\ / __/ __| \033[0m"
		echo -e "\033[32m\033[1m |  _/ _ \\__ \__ \\ \033[0m"
		echo -e "\033[32m\033[1m |_|/_/ \_\___/___/ \033[0m"
		echo ""

		# Update test suite counter
		if [ -f .tmp_tcsuite_pass_ctr ]; then
			tmpval=$(< .tmp_tcsuite_pass_ctr)
			((tmpval++))
			echo $tmpval > .tmp_tcsuite_pass_ctr
		fi
		if [ -f .tmp_tcsuite_pass ]; then
			echo " - "$ATC " -- "$TC_ONELINE_DESCR"  Execution time: "$duration" seconds" >> .tmp_tcsuite_pass
		fi
	else
		echo -e "One or more tests with status  \033[31m\033[1mFAIL\033[0m "
		echo -e "\033[31m\033[1m  ___ _   ___ _    \033[0m"
		echo -e "\033[31m\033[1m | __/_\ |_ _| |   \033[0m"
		echo -e "\033[31m\033[1m | _/ _ \ | || |__ \033[0m"
		echo -e "\033[31m\033[1m |_/_/ \_\___|____|\033[0m"
		echo ""
		# Update test suite counter
		if [ -f .tmp_tcsuite_fail_ctr ]; then
			tmpval=$(< .tmp_tcsuite_fail_ctr)
			((tmpval++))
			echo $tmpval > .tmp_tcsuite_fail_ctr
		fi
		if [ -f .tmp_tcsuite_fail ]; then
			echo " - "$ATC " -- "$TC_ONELINE_DESCR"  Execution time: "$duration" seconds" >> .tmp_tcsuite_fail
		fi
	fi

	echo "++++ Number of tests:        "$RES_TEST
	echo "++++ Number of passed tests: "$RES_PASS
	echo "++++ Number of failed tests: "$RES_FAIL
	echo "-------------------------------------     Test case complete    ---------------------------------"
	echo "-------------------------------------------------------------------------------------------------"
	echo ""
}