-
Notifications
You must be signed in to change notification settings - Fork 2
/
build-test.sh
executable file
·179 lines (151 loc) · 5.23 KB
/
build-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
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
#!/bin/bash
## Exit immediately on error.
set -e
# Set function to be called on exit
trap 'exit_err_cleanup $?' EXIT
###############################################################################
# exit_err_cleanup is called at exit, if there was an error it calls container
# cleanup and reports an error.
# Globals: none
# Parameters:
# - $1: Script exit code
###############################################################################
exit_err_cleanup() {
# Cleanup containers and report an error if any happened
if [ "$1" != "0" ]; then
cleanup_test_containers
echo "ERROR: Error $1 occured"
else
echo "SUCCESS: Build and test finished successfully"
fi
exit $1
}
###############################################################################
# cleanup_test_containers logs massages and calls the the subcall to remove
# test containers.
# Globals: none
# Parameters: none
###############################################################################
cleanup_test_containers() {
log_cmd "Removing test containers" cleanup_test_containers_subcall
}
###############################################################################
# cleanup_test_containers_subcall removes test containers without logs.
# Globals: none
# Parameters: none
###############################################################################
cleanup_test_containers_subcall() {
CONTAINERS=$(docker ps -a -q --filter "name=sia-ant-farm-test-container")
if [ ! -z $CONTAINERS ]; then
docker stop $CONTAINERS
docker rm $CONTAINERS
fi
}
###############################################################################
# log_cmd echoes the starting message, executes the command and echoes the
# success message.
# Globals: none
# Parameters:
# - $1: Log message
# - the rest: Command with parameters to be executed
###############################################################################
log_cmd() {
# Get and echo log message
local MSG=$1
echo "Starting: $MSG..."
# Execute the command
shift 1
"$@"
# Log success
echo "Success: $MSG"
}
###############################################################################
# wait_for_consensus waits for the ant consensus to be reachable.
# Globals: none
# Parameters:
# - $1: Timeout to wait for the consensus
# - $2: API port of the ant to be checked
###############################################################################
wait_for_consensus() {
# Parameters
local timeout=$1
local port=$2
# Set variables
local url=http://localhost:$port/consensus
local cmd="until wget --user-agent='Sia-Agent' -q -O - $url
do
sleep 1
done"
# Execute
log_cmd "Waiting for consensus at $url" timeout $timeout sh -c "$cmd"
}
###############################################################################
# wait_for_renter_upload_ready waits until the renter ant is upload ready.
# Globals: none
# Parameters:
# - $1: Timeout to wait for the renter to become upload ready
# - $2: API port of the renter ant to be checked
###############################################################################
wait_for_renter_upload_ready() {
# Parameters
local timeout=$1
local port=$2
# Set variables
local url=http://localhost:$port/renter/uploadready
local cmd="until wget --user-agent='Sia-Agent' -q -O - $url | grep '\"ready\":true'
do
sleep 1
done"
# Execute
log_cmd "Waiting for renter to become upload ready at $url" timeout $timeout sh -c "$cmd"
}
# Remove test containers before we start
cleanup_test_containers
# Iterate over all Dockerfiles
for DIR in .
do
# Build the image
log_cmd "Building a test image according to $DIR/Dockerfile" docker build \
--no-cache \
--tag sia-ant-farm-image-test \
-f $DIR/Dockerfile \
.
# Test with a single published standard renter port
# Run container in detached state
DUMMY_DATA_DIR=$(mktemp -d)
TEST1_RENTER_PORT=9980
log_cmd "Test 1: Starting test container" docker run \
--detach \
--publish 127.0.0.1:$TEST1_RENTER_PORT:9980 \
--volume "${DUMMY_DATA_DIR}:/sia-antfarm/data" \
--name sia-ant-farm-test-container \
sia-ant-farm-image-test
# Wait till API (/consensus) is accessible
wait_for_consensus 120 $TEST1_RENTER_PORT
# Wait for renter to become upload ready
wait_for_renter_upload_ready 300 $TEST1_RENTER_PORT
# Remove test container
cleanup_test_containers
# Test with 2 published ports:
# A non-standard renter port and a host port
# Run container in detached state
DUMMY_DATA_DIR=$(mktemp -d)
TEST2_RENTER_PORT=33333
TEST2_HOST_PORT=34444
log_cmd "Test 2: Starting test container" docker run \
--detach \
--publish 127.0.0.1:$TEST2_RENTER_PORT:9980 \
--publish 127.0.0.1:$TEST2_HOST_PORT:10980 \
--volume "${DUMMY_DATA_DIR}:/sia-antfarm/data" \
--volume "$(pwd)/config:/sia-antfarm/config" \
--env CONFIG=config/basic-renter-5-hosts-2-api-ports-docker.json \
--name sia-ant-farm-test-container \
sia-ant-farm-image-test
# Wait till both APIs (/consensus) are accessible
wait_for_consensus 120 $TEST2_RENTER_PORT
wait_for_consensus 10 $TEST2_HOST_PORT
# Wait for renter to become upload ready
wait_for_renter_upload_ready 300 $TEST2_RENTER_PORT
# Remove test container
cleanup_test_containers
done