-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtest
executable file
·196 lines (172 loc) · 4.85 KB
/
test
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
#!/bin/bash
if [ -z "$COOG_CODE_DIR" ] || [ ! -d "$COOG_CODE_DIR" ] || [ -z "$COOG_DATA_DIR" ]
then
{
echo "Please make sure that these two env vars are set:"
echo " COOG_CODE_DIR: your coog-admin install folder"
echo " COOG_DATA_DIR: the folder where to keep your custom config"
} >&2 && exit 1
fi
REPORT_NAME="/tmp/coog-test-$(date '+%Y%m%d-%H%M%S')"
_get_postgres_container() {
echo "$NETWORK_NAME-postgres"
}
_get_redis_container() {
echo "$NETWORK_NAME-redis"
}
pull() {
echo "git pull"
(cd "$COOG_CODE_DIR" && git pull)
}
check() {
echo "check running containers"
local postgres_container
postgres_container=$(_get_postgres_container)
local postgres_running
postgres_running=$(docker inspect -f '{{.State.Running}}' "$postgres_container")
[ "$postgres_running" != true ] && return 1
local redis_container
redis_container=$(_get_redis_container)
redis_running=$(docker inspect -f '{{.State.Running}}' "$redis_container")
[ "$redis_running" != true ] && return 1
return 0
}
build() {
echo "clean old images"
docker rmi -f "$1" || return 1
rm -rf "$COOG_CODE_DIR/images/coog/dist" || return 1
echo "build image"
"$COOG_CODE_DIR/coog" build "$@"
}
clear_redis() {
echo "clear redis"
echo flushall | docker exec -i "$(_get_redis_container)" redis-cli
}
_clear_postgres_ls() {
echo "select datname from pg_database;" \
| docker exec -i "$(_get_postgres_container)" psql -U postgres -t \
| grep -P "(test_\d+$|test_postgresql_cache_.*)" \
| xargs -n 1 echo
}
_clear_postgres_drop() {
echo "drop database \"$1\";" \
| docker exec -i "$(_get_postgres_container)" psql -U postgres -t
}
clear_postgres() {
echo "clear postgres"
local old; old=$IFS; IFS=$'\n'
for db in $(_clear_postgres_ls)
do
printf "%s: " "$db"
_clear_postgres_drop "$db"
done
IFS=$old
}
start_workers() {
echo "start workers"
for i in $(seq 1 "$COOG_TEST_WORKERS")
do
"$COOG_CODE_DIR/coog" "rq-$i" test
done
}
report_summary() {
local ok; ok=$1; shift
local image
image=$(docker images -f reference="$IMAGE" --format '{{.ID}}')
echo "Test image: $image"
echo
"$COOG_CODE_DIR/coog" redis rq q test
[ "$ok" -eq 0 ] && return 0
echo
"$COOG_CODE_DIR/coog" redis rq qlist test fail | column -t
return 0
}
report_failed_job() {
local line; line=$1; shift
local j; j=$(echo "$line" | cut -d $'\t' -f 3)
local m; m=$(echo "$line" | cut -d $'\t' -f 5)
"$COOG_CODE_DIR/coog" redis rq j "$j" > "$REPORT_NAME/$m.txt"
}
report() {
local ok; ok=$1; shift
report_summary "$ok" > "$REPORT_NAME.eml"
[ $? -ne 0 ] && echo "report_summary failed" && return 1
mkdir "$REPORT_NAME"
local first
local old; old=$IFS; IFS=$'\n'
for line in $("$COOG_CODE_DIR/coog" redis rq qlist test fail)
do
[ -z "$first" ] && first="$line" && continue
report_failed_job "$line"
[ $? -ne 0 ] && echo "report_failed_job failed" && return 1
done
IFS=$old
return 0
}
email() {
local ok; ok=$1; shift
local subject
if [ "$ok" -eq 0 ]
then
subject="$USER - OK"
else
subject="$USER - KO"
fi
local args
args=()
if [ "$ok" -ne 0 ]
then
for f in $REPORT_NAME/*
do
args+=(-A $f)
done
fi
mail \
-s "$subject" \
-a "From:[email protected]" \
"${args[@]}" \
"$COOG_ADMIN_EMAIL" < "$REPORT_NAME.eml"
}
main() {
pull || return $?
local ok
source "$COOG_CODE_DIR/config"
export IMAGE="coopengohub/coog:${1##coopengohub/coog:}"
check || return $?
if [[ "${NO_BUILD:-yes_do_build}" = "yes_do_build" ]]; then
local args
args=()
while [ ! -z "$1" ] && [ "$1" != "--" ]
do
args+=($1) && shift
done
shift
build "${args[@]}" || return $?
else
shift
fi
clear_redis || return $?
clear_postgres || return $?
"$COOG_CODE_DIR/coog" test generate "$@" || return $?
start_workers || return $?
"$COOG_CODE_DIR/coog" test join
ok=$?
# Mail requires a proper configuration of the server which runs the tests,
# python can make it work all alone
# report "$ok" && email "$ok"
local branch_name
branch_name=$(git -C "$COOG_CODE_DIR" branch | grep \* | cut -d ' ' -f2)
report "$ok" && \
TARGET_RESULTS=$REPORT_NAME BRANCH=$branch_name \
python "$COOG_CODE_DIR/send_report"
# $ok does not actually mean that the tests are ok. What matters is whether
# the $REPORT_NAME folder is empty or not
if [[ ! -e "$REPORT_NAME" ]] || [[ "$(ls "$REPORT_NAME" | wc -l)" = 0 ]]
then
return 0
else
return 1
fi
}
return 2> /dev/null # avoid exec of main when sourced
main "$@"