-
Notifications
You must be signed in to change notification settings - Fork 301
/
test-all.sh
executable file
·135 lines (114 loc) · 3.23 KB
/
test-all.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
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
#!/usr/bin/env bash
#
# Sample use of overriding parameters:
# - host, name of the host where all microservices are running
# - port, the port where the API is available
#
# $ host=myhost port=8765 ./test-all.sh
#
: ${host=localhost}
: ${port=443}
# set -e
function testUrl() {
url=$@
if curl $url -ks -f -o /dev/null
then
echo "Ok"
return 0
else
echo -n "not yet"
return 1
fi;
}
function waitForService() {
url=$@
echo -n "Wait for: $url... "
n=0
until testUrl $url
do
((n++))
if [[ $n == 20 ]]
then
echo " Give up"
exit 1
else
sleep 6
echo -n ", retry #$n "
fi
done
}
function waitForServices() {
waitForService $host:8761
waitForService $host:8761/eureka/apps/config-server
waitForService $host:8761/eureka/apps/edge-server
waitForService $host:8761/eureka/apps/product-service
waitForService $host:8761/eureka/apps/review-service
waitForService $host:8761/eureka/apps/recommendation-service
waitForService $host:8761/eureka/apps/composite-service
}
function testAPI() {
url=$@
echo "Is the API awake?"
echo "$ curl -ks https://$host:$port/api/product/123 -H \"Authorization: Bearer \$TOKEN\" | jq ."
if curl -ks https://$host:$port/api/product/123 -H "Authorization: Bearer $TOKEN" -f -o /dev/null
then
echo "Ok"
return 0
else
echo -n "not yet"
return 1
fi;
}
function waitForAPI() {
url=$@
echo -n "Wait for API: $url... "
n=0
until testAPI $url
do
((n++))
if [[ $n == 20 ]]
then
echo " Give up"
exit 1
else
sleep 6
echo -n ", retry #$n "
fi
done
}
echo "Start:" `date`
. ./setup-env.sh
if [[ $@ == *"start"* ]]
then
echo "Restarting the test environment..."
echo "$ docker-compose down"
docker-compose down
echo "$ docker-compose up -d"
docker-compose up -d
fi
waitForServices
#echo ''
#echo "Call /info on each microservice:"
#docker-compose exec composite wget -qO- localhost:8080/info | jq
#docker-compose exec pro wget -qO- localhost:8080/info | jq
#docker-compose exec rev wget -qO- localhost:8080/info | jq
#docker-compose exec rec wget -qO- localhost:8080/info | jq
echo ''
echo "Get an OAuth Access Token:"
echo "$ curl -ks https://acme:acmesecret@$host:9999/uaa/oauth/token -d grant_type=password -d client_id=acme -d scope=webshop -d username=user -d password=password | jq ."
OAUTH_RESPOSE=`curl -ks https://acme:acmesecret@$host:9999/uaa/oauth/token -d grant_type=password -d client_id=acme -d scope=webshop -d username=user -d password=password`
echo $OAUTH_RESPOSE | jq .
export TOKEN=`echo $OAUTH_RESPOSE | jq -r .access_token`
echo "ACCESS TOKEN: $TOKEN"
echo ''
echo "Call API with Access Token... "
waitForAPI
echo "$ curl -ks https://$host:$port/api/product/123 -H \"Authorization: Bearer \$TOKEN\" | jq ."
curl -ks https://$host:$port/api/product/123 -H "Authorization: Bearer $TOKEN" | jq .
if [[ $@ == *"stop"* ]]
then
echo "We are done, stopping the test environment..."
echo "$ docker-compose down"
docker-compose down
fi
echo "End:" `date`