-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun-tests.sh
executable file
·92 lines (79 loc) · 3.34 KB
/
run-tests.sh
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
#!/usr/bin/env bash
set -e
./vcap-services-template-reformat.sh
cat vcap-service.env
echo "Starting sample app"
container_name="cnb-app-container"
container_id=$(docker run -d --rm -e PORT=80 -p 8080:80 --env-file vcap-service.env --name "$container_name" ${CNB_IMAGE_NAME})
echo "Cnb app started (id: $container_id)"
echo "Waiting to ensure app is up and running"
while [ "$( docker container inspect -f '{{.State.Status}}' $container_name )" != "running" ]; do
echo "waiting for $container_name to be running current: $(docker inspect -f '{{.State.Status}}' $container_name)"
sleep 1
done
sleep 3 # to ensure app is up and running
service_container_name="$(docker ps -f "ancestor=$SERVICE_IMAGE" --format "{{.Names}}")"
CONTAINER_APP_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $container_name)
echo "CONTAINER_APP_IP: $CONTAINER_APP_IP"
if [ "$DEBUG" = "1" ]; then
echo "----------------- $container_name --------------------"
docker inspect -f '{{json .NetworkSettings}}' $container_name
echo "----------------- $SERVICE_NAME: $service_container_name --------------------"
docker inspect -f '{{json .NetworkSettings}}' "$service_container_name"
fi
echo "=== Redirect logs to cnb-app-container.log ==="
docker logs -f cnb-app-container &> cnb-app-container.log &
docker ps -a
echo "=== Check connectivity ==="
if nc -vz 127.0.0.1 8080;then echo "port 8080 available";else echo "port 8080 UNAVAILABLE";exit_status=1;fi
if nc -vz 127.0.0.1 "${SERVICE_PORT}";then echo "port ${SERVICE_PORT} available";else echo "port ${SERVICE_PORT} UNAVAILABLE";exit_status=1;fi
echo "=== Check access as $SERVICE_USERNAME === "
docker exec -i $service_container_name bash -c "mongosh \"mongodb://$SERVICE_USERNAME:$SERVICE_PASSWORD@localhost:$SERVICE_PORT/$DATABASE_NAME\" -eval \"db = db.getSiblingDB('my-sample-mongo-db'); if (!db.runCommand({ping: 1 }).ok) { print('Database not found'); exit(1); }\""
function check_service() {
type="$1"
cmd="$2"
cmd_prefix="$3"
if [ -z "$cmd_prefix" ];then
cmd_prefix="curl"
fi
status=0
echo "$type using > $cmd <" 1>&2
if ! $cmd;then
echo "" 1>&2
echo "$type failed: retry in verbose mode" 1>&2
$cmd_prefix -vvv ${cmd##$cmd_prefix}
status=1
else
echo "" 1>&2
echo "$type successful" 1>&2
fi
echo "check_service - status: $status" 1>&2
return $status
}
export APP="http://127.0.0.1:8080"
echo "=== Test App ==="
set +e
exit_status=0
create_service_output="$(check_service "Create" "curl -sSLf -X POST $APP/myCollection -d ''")"
create_service=$?
#get_service_output="$(check_service "Get" "curl -sSLf -X GET $APP/foo" )"
#get_service=$?
delete_service_output="$(check_service "Delete" "curl -sSLf -X DELETE $APP/myCollection -d ''")"
delete_service=$?
set -e
exit_status=$(($create_service + $delete_service))
echo "exit status: $exit_status"
echo "======================================================================================================"
echo "Dumping logs using docker logs cnb-app-container"
docker logs cnb-app-container 2>&1
ls -lrt *.log
if [ $exit_status -ne 0 ];then
echo "-------------------------------------------------------------------------------------------------"
echo "Dumping service logs"
if [ "$DEBUG" = 0 ];then
docker logs $SERVICE_NAME-service -n 30 2>&1
else
docker logs $SERVICE_NAME-service 2>&1
fi
fi
exit $exit_status