forked from inspirehep/inspire-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.sh
executable file
·207 lines (173 loc) · 4.83 KB
/
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/bin/bash -e
REQUIRED_VARS=(
TRAVIS_BRANCH
DOCKER_PROJECT
DOCKER_IMAGE_TAG
)
INSTALL_COMPOSE=true
REDOWNLOAD_NEXT=true
BASE_DIR="$(cd "${0%%/*}"; pwd)"
NEXT_DIR="$BASE_DIR/inspire-next"
VENV_DIR="${DOCKER_DATA}/tmp/virtualenv"
help(){
cat <<EOH
Usage: $0 [options]
Options:
-h|--help
Show this help
-v|--verbose
Enable verbose mode
-n|--no-install-compose
Do not install docker-compose on the system, useful for local runs
-u|--use-existing-next
If already exists, use the inspire-next repo instead of removin it
and redownloading it. The repo that will be used must be at
$NEXT_DIR
Required environment variables:
$(for var in "${REQUIRED_VARS[@]}"; do echo -ne "$var\n "; done)
EOH
exit "${1:-0}"
}
retry() {
if ! "$@"; then
echo "******** Retrying $*"
"$@" || exit $?
fi
}
utils.in(){
local what="$1"
shift
local where=("$@")
for key in "${where[@]}"; do
[[ "$what" == "$key" ]] && return 0
done
return 1
}
get_images(){
grep -ohP "(?<=image: ).*" "$NEXT_DIR/"*.yml | sort | uniq
}
install_docker_compose(){
retry sudo pip install docker-compose
# Add docker-compose at the version specified in ENV.
sudo rm -f /usr/local/bin/docker-compose
curl -L "https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-$(uname -s)-$(uname -m)" > docker-compose
chmod +x docker-compose
sudo mv docker-compose /usr/local/bin
export PATH=$PATH:/usr/local/bin
}
clone_next(){
rm -rf "$NEXT_DIR"
git clone https://github.com/inspirehep/inspire-next.git "$NEXT_DIR"
}
prepare() {
if ! [[ -e "$NEXT_DIR" ]] || $REDOWNLOAD_NEXT; then
clone_next
fi
if $INSTALL_COMPOSE; then
install_docker_compose
fi
}
cleanup_env() {
cd "$NEXT_DIR"
docker-compose kill
docker-compose rm -f
sudo rm -rf "$VENV_DIR"
cd -
}
pull_all_images_except(){
local to_skip=("$@")
local images=($(get_images))
local image
echo "##### Pulling the docker images"
cd "$NEXT_DIR"
for image in "${images[@]}"; do
utils.in "$image" "${to_skip[@]}" && continue
retry docker pull "$image"
done
echo "#################"
}
run_unit_tests() {
cd "$NEXT_DIR"
echo "###### Running unit tests"
cleanup_env
echo "--- Available images"
docker images
echo "----------------"
retry docker-compose -f docker-compose.deps.yml run --rm pip
docker-compose -f docker-compose.deps.yml run --rm assets
docker-compose -f docker-compose.test.yml run --rm unit
cleanup_env
echo "#################"
}
run_integration_tests() {
cd "$NEXT_DIR"
echo "###### Running integraiton tests"
cleanup_env
echo "--- Available images"
docker images
echo "----------------"
retry docker-compose -f docker-compose.deps.yml run --rm pip
docker-compose -f docker-compose.deps.yml run --rm assets
docker-compose -f docker-compose.test.yml run --rm integration
cleanup_env
echo "#################"
}
parse_options(){
local opts
opts=$(\
getopt \
--options 'hnuv' \
--longoptions 'help,no-install-compose,use-existing-next,verbose' \
--name "$0" \
-- "$@" \
) || help 1
local failed=false
local env_var
eval set -- "$opts"
while true; do
opt="$1"
shift;
case $opt in
-n|--no-install-compose)
INSTALL_COMPOSE=false;;
-u|--use-existing-next)
REDOWNLOAD_NEXT=false;;
-h|--help) help 0;;
-v|--verbose) set -x;;
--) break;;
esac
done
for env_var in "${REQUIRED_VARS[@]}"; do
[[ -v $env_var ]] || {
echo "Undefined var $env_var, please define befor running this" \
"script"
failed=true
}
done
if $failed; then
echo -e "Some required env vars were not defined, aborting\n"
help 1
fi
}
main() {
parse_options "$@"
if [[ "$TRAVIS_BRANCH" != "master" ]]; then
TEST_TAG="$DOCKER_PROJECT:$DOCKER_IMAGE_TAG"
CUR_TAG="$DOCKER_PROJECT:dev.$TRAVIS_BRANCH-$DOCKER_IMAGE_TAG"
echo "Adding tag $TEST_TAG to the image for the testing"
docker tag "$CUR_TAG" "$TEST_TAG"
else
CUR_TAG="$DOCKER_PROJECT:$DOCKER_IMAGE_TAG"
fi
if [[ "$DOCKER_IMAGE_TAG" != "latest" ]]; then
LATEST_TAG="$DOCKER_PROJECT:latest"
echo "Adding latest tag $LATEST_TAG to the image for the testing"
docker tag "$CUR_TAG" "$LATEST_TAG"
fi
prepare
pull_all_images_except "$DOCKER_PROJECT"
run_unit_tests
run_integration_tests
echo "================== SUCCESS"
}
main "$@"