forked from openfaas/faas-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_integration_test.sh
executable file
·128 lines (97 loc) · 2.86 KB
/
build_integration_test.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
#!/bin/bash
cli="./bin/faas-cli"
template="python3"
get_package() {
uname=$(uname)
arch=$(uname -m)
echo "Getting faas-cli package for $uname..."
echo "Having architecture $arch..."
case $uname in
"Darwin")
cli="./faas-cli-darwin"
case $arnch in
"arm64")
cli="./faas-cli-darwin-arm64"
;;
esac
;;
"Linux")
case $arch in
"armv6l" | "armv7l")
cli="./faas-cli-armhf"
template="python3-armhf"
;;
esac
;;
esac
echo "Using package $cli"
echo "Using template $template"
}
build_faas_function() {
function_name=$1
eval $cli new $function_name --lang $template
cat << EOF > $function_name/handler.py
def handle(req):
return "Function output from integration testing: Hello World!"
EOF
eval $cli build -f $function_name.yml
}
wait_for_function_up() {
function_name=$1
port=$2
timeout=$3
function_up=false
for (( i=1; i<=$timeout; i++ ))
do
echo "Checking if 127.0.0.1:$port is up.. ${i}/$timeout"
status_code=$(curl 127.0.0.1:$port/_/health -o /dev/null -sw '%{http_code}')
if [ $status_code == 200 ] ; then
echo "Function $function_name is up on 127.0.0.1:$port"
function_up=true
break
else
echo "Waiting for function $function_name to run on 127.0.0.1:$port"
sleep 1
fi
done
if [ "$function_up" != true ] ; then
echo "Failed to reach function on 127.0.0.1:$port.. Service timeout"
echo "Removing container..."
docker rm -f $id
echo "Removing function image $function_name:latest"
docker rmi -f $function_name:latest
exit 1
fi
}
run_and_test() {
function_name=$1
port=$2
timeout=$3
if lsof -Pi :$port -sTCP:LISTEN -t >/dev/null ; then
echo "Port $port is already allocated.. Cannot use this port for testing.
Exiting test..."
echo "Removing function image $function_name:latest"
docker rmi -f $function_name:latest
exit 1
fi
id=$(docker run --env fprocess="python3 index.py" --name $function_name -p $port:8080 -d $function_name:latest)
wait_for_function_up $function_name $port $timeout
curl -s 127.0.0.1:$port > got.txt
cat << EOF > want.txt
Function output from integration testing: Hello World!
EOF
if cmp got.txt want.txt ; then
echo "SUCCESS testing function $function_name"
else
echo "FAILED testing function $function_name"
fi
echo "Removing container..."
docker rm -f $id
echo "Removing function image $function_name:latest"
docker rmi -f $function_name:latest
echo "Removing created files..."
rm -rf got.txt want.txt $function_name*
}
get_package
build_faas_function $FUNCTION
run_and_test $FUNCTION $PORT $FUNCTION_UP_TIMEOUT