-
Notifications
You must be signed in to change notification settings - Fork 223
/
Copy pathbuild_integration_test.sh
executable file
·144 lines (110 loc) · 3.25 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/bash
set -e # Exit script immediately on first error.
set -x # Print commands and their arguments as they are executed.
set -o pipefail # Print commands and their arguments as they are
cli="./bin/faas-cli"
TEMPLATE_NAME="python3-http"
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"
;;
esac
;;
esac
echo "Using package $cli"
echo "Using template $TEMPLATE_NAME"
}
build_faas_function() {
function_name=$1
eval $cli new $function_name --lang $TEMPLATE_NAME
cat << EOF > $function_name/handler.py
def handle(event, context):
return {
"statusCode": 200,
"body": {"message": "Hello from OpenFaaS!"},
"headers": {
"Content-Type": "application/json"
}
}
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_templates() {
echo "Getting templates..."
eval $cli template store pull $TEMPLATE_NAME
}
get_templates
get_package
build_faas_function $FUNCTION
run_and_test $FUNCTION $PORT $FUNCTION_UP_TIMEOUT