-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathheader.sh
More file actions
201 lines (159 loc) · 5.18 KB
/
header.sh
File metadata and controls
201 lines (159 loc) · 5.18 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/bin/bash
set -o errexit
set -x
set -e
err_report() {
echo "Error on line $1 : $2"
}
FILENAME=`basename $0`
# The first argument to the script is TOML env for the testground daemon.
# If no argument is passed, the default is env-kind.toml.
DAEMON_ENV="${1:-env-kind.toml}"
trap 'err_report $LINENO $FILENAME' ERR
function finish {
# if the SKIP_AUTO_START flag is unset or set to 0, kill the daemon we started
if [[ ! -n "$SKIP_AUTO_START" || $SKIP_AUTO_START = 0 ]]; then
kill -15 "$DAEMONPID"
fi
}
trap finish EXIT
# Assert the status of a testground run ("failure", "success")
#
# Usage:
# assert_run_outcome_is "./testground-run-logs.out" "failed"
function assert_run_outcome_is {
RUN_OUT_FILEPATH="$1"
EXPECTED_OUTCOME="$2"
RUN_ID=$(awk '/run is queued with ID/ { print $10 }' "${RUN_OUT_FILEPATH}")
echo "checking run ${RUN_ID}"
assert_testground_task_status "$RUN_ID" "$EXPECTED_OUTCOME"
}
# Assert the status of a multiple testground run ("failure", "success")
#
# Usage:
# assert_run_outcome_is "./testground-run-logs.out" "failed"
function assert_runs_outcome_are {
RUN_OUT_FILEPATH="$1"
EXPECTED_OUTCOMES=("${@:2}")
RUN_IDS=$(awk '/run is queued with ID/ { print $10 }' "${RUN_OUT_FILEPATH}")
i=0
for RUN_ID in $RUN_IDS; do
echo "checking run ${RUN_ID} for outcome ${EXPECTED_OUTCOMES[$i]}"
assert_testground_task_status "$RUN_ID" "${EXPECTED_OUTCOMES[$i]}"
i=$((i+1))
done
if [ $i -ne ${#EXPECTED_OUTCOMES[@]} ]; then
echo "expected ${#EXPECTED_OUTCOMES[@]} runs, but found $i"
exit 1
fi
}
function assert_runs_instance_count {
# TODO: implement
exit 1
}
function assert_runs_results {
RESULT_FILEPATH="$1"
EXPECTED_OUTCOMES=("${@:2}")
RESULTS=`cat $RESULT_FILEPATH | tail -n +2 | awk -F, '{print $3}'`
i=0
for RESULT in $RESULTS; do
echo "checking line ${i} for outcome ${EXPECTED_OUTCOMES[$i]}"
if [ "${RESULT}" != "${EXPECTED_OUTCOMES[$i]}" ]; then
echo "expected outcome ${EXPECTED_OUTCOMES[$i]} but got ${RESULT}"
exit 1
fi
i=$((i+1))
done
if [ $i -ne ${#EXPECTED_OUTCOMES[@]} ]; then
echo "expected ${#EXPECTED_OUTCOMES[@]} runs, but found $i"
exit 1
fi
}
# Assert the status of a testground task, as provided by the CLI output
#
# Usage:
# assert_testground_task_status "###run_id123###" "success"
function assert_testground_task_status {
RUN_ID="$1"
EXPECTED_OUTCOME="$2"
OUTCOME=$(testground status -t "${RUN_ID}" | awk '/Outcome:/{ print $2 }')
if [ "${OUTCOME}" != "${EXPECTED_OUTCOME}" ]; then
exit 1;
fi
}
# Assert that a testground run has no errors and some logs.
# use `SKIP_LOG_PARSING` to check for errors only (required when working with SDKs that don't output logs).
#
# Usage:
# assert_run_output_is_correct "./testground-run-logs.out"
# SKIP_LOG_PARSING=1 assert_run_output_is_correct "./testground-run-logs.out"
function assert_run_output_is_correct {
RUN_OUT_FILEPATH="$1"
RUN_ID=$(awk '/finished run with ID/ { print $9 }' "${RUN_OUT_FILEPATH}")
echo "checking run $RUN_ID"
file ${RUN_ID}.tgz
LENGTH=${#RUN_ID}
test $LENGTH -eq 20
tar -xzvvf ${RUN_ID}.tgz
if [[ -n "$SKIP_LOG_PARSING" ]]; then
return
fi
SIZEOUT=$(cat ./"${RUN_ID}/single/0/run.out" | wc -c)
echo "run.out is $SIZEOUT bytes."
SIZEERR=$(cat ./"${RUN_ID}/single/0/run.err" | wc -c)
echo "run.err is $SIZEERR bytes."
test $SIZEOUT -gt 0 && test $SIZEERR -eq 0
}
# Assert that a testground run has a certain number of instances outputs.
# Expects that the testground call was run with `--collect`
#
# Usage:
# assert_run_instance_count "./run.out" 3
# SKIP_LOG_PARSING=1 assert_run_output_is_correct "./testground-run-logs.out"
function assert_run_instance_count {
RUN_OUT_FILEPATH="$1"
EXPECTED_COUNT="$2"
RUN_ID=$(awk '/finished run with ID/ { print $9 }' "${RUN_OUT_FILEPATH}")
echo "checking run $RUN_ID"
file ${RUN_ID}.tgz
LENGTH=${#RUN_ID}
test $LENGTH -eq 20
tar -xzvvf ${RUN_ID}.tgz
echo ${PWD}
echo `ls -l`
COUNT_INSTANCES=$(ls ./${RUN_ID}/*/ | wc -l)
echo "instances counts is ${COUNT_INSTANCES}, expected: ${EXPECTED_COUNT}."
test $COUNT_INSTANCES -eq $EXPECTED_COUNT
}
# Directory where the daemon and each test will store its outputs
TEMPDIR=`mktemp -d`
# Start the testground daemon, loading the .env file from the first parameter
function start_daemon {
env_file="$1"
backend_env_file="${TEMPDIR}/.env.toml.bak"
mkdir -p ${HOME}/testground
# backup previous env file
if [ -f ${HOME}/testground/.env ]; then
mv ${HOME}/testground/.env.toml ${backend_env_file}
fi
cp $env_file ${HOME}/testground/.env.toml
echo "Starting daemon and logging outputs to $TEMPDIR"
testground daemon > $TEMPDIR/daemon.out 2>&1 &
DAEMONPID=$!
sleep 2
echo "Waiting for Testground to launch on 8040..."
while ! nc -z localhost 8040; do
sleep 1
done
echo "Testground launched"
# restore previous env file
if [ -f ${backend_env_file} ]; then
mv ${backend_env_file} ${HOME}/testground/.env.toml
fi
}
set -x
# if the SKIP_AUTO_START flag is unset or set to 0, start the daemon immediately
if [[ ! -n "$SKIP_AUTO_START" || $SKIP_AUTO_START = 0 ]]; then
echo "Starting daemon automatically"
start_daemon $DAEMON_ENV
fi