-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_test.sh
executable file
·314 lines (276 loc) · 7.82 KB
/
run_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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/bin/bash
# ------------- PUBLIC ONES
source ./utils.sh
SS2D_TYPE="major"
GAME_TYPE="test"
# esme serveri ke khaliye
TEST_SERVER_NAME=
LOG_DIR=
LOG_BASE_DIR=
TEAM_RIGHT=hel
TEAM_LEFT_BINARY_LOCATION=
# Jaii ke file haye 01 server hastan
SERVER_DIRECTORY=
EXIT_COMMAND=
EVENT_DIR=
ARCHIVE_FILE_ADDRESS=
TEST_TEAM_NAME="t$( (tr -dc a-z0-9 </dev/urandom | head -c 6) && echo '')p"
TIME_STAMP=$TEST_TEAM_NAME
INIT_TEST_NETWORK=false
NUMBER_OF_TEST_SERVERS=2
ORIGINAL_TEAM_NAME=
prg=$0
if [ ! -e "$prg" ]; then
case $prg in
*/*) exit 1 ;;
*) prg=$(command -v -- "$prg") || exit ;;
esac
fi
dir=$(
cd -P -- "$(dirname -- "$prg")" && pwd -P
) || exit
prg=$dir/$(basename -- "$prg") || exit
#------------------------------------------------------------
createTestNetwork() {
RUN "rm -r $SERVER_DIRECTORY" -nc
RUN "mkdir -p $SERVER_DIRECTORY" -nc
for ((i = 1; i <= $NUMBER_OF_TEST_SERVERS; i++)); do
_SERVER_NAME="test$i"
RUN "docker network rm $_SERVER_NAME" -nc
RUN "docker network create --internal --subnet=172.$((90 + $i)).0.0/16 $_SERVER_NAME"
RUN "echo \"1\"> $SERVER_DIRECTORY/$_SERVER_NAME" -po
done
}
checkParams() {
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-st | --ss2d-type)
SS2D_TYPE="$2"
shift # past argument
shift # past value
;;
-bl | --binary-location)
BINARY_LOCATION="$2"
shift # past argument
shift # past value
;;
-ec | --exit-command)
EXIT_COMMAND="$2"
shift # past argument
shift # past value
;;
-gt | --game-type)
GAME_TYPE="$2"
shift # past argument
shift # past value
;;
-ld | --log_directory)
LOG_DIR="$2"
shift # past argument
shift # past value
;;
-ed | --event_directory)
EVENT_DIR="$2"
shift # past argument
shift # past value
;;
-r | --right_team)
TEAM_RIGHT="$2"
shift # past argument
shift # past value
;;
-sd | --server-directory)
SERVER_DIRECTORY="$2"
shift # past argument
shift # past value
;;
-ltbl | --left-team-binary-location)
TEAM_LEFT_BINARY_LOCATION="$2"
shift # past argument
;;
-otn | --original-team-name)
ORIGINAL_TEAM_NAME="$2"
shift # past argument
;;
-in | --init-test-networksm)
INIT_TEST_NETWORK=true
shift # past argument
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
success=1
if ! $INIT_TEST_NETWORK; then
if [ -z "$SS2D_TYPE" ]; then
success=0
echo "SS2D_TYPE is missing"
fi
if [ -z "$GAME_TYPE" ]; then
success=0
echo "GAME_TYPE is missing"
fi
if [ -z "$LOG_DIR" ]; then
success=0
echo "LOG_DIR is missing"
fi
if [ -z "$EVENT_DIR" ]; then
success=0
echo "EVENT_DIR is missing"
fi
if [ -z "$TEAM_RIGHT" ]; then
success=0
echo "TEAM_RIGHT is missing"
fi
if [ -z "$SERVER_DIRECTORY" ]; then
success=0
echo "SERVER_DIRECTORY is missing"
fi
if [ -z "$TEAM_LEFT_BINARY_LOCATION" ]; then
success=0
echo "TEAM_LEFT_BINARY_LOCATION is missing"
fi
if [ -z "$ORIGINAL_TEAM_NAME" ]; then
success=0
echo "ORIGINAL_TEAM_NAME is missing"
fi
fi
if [[ $INIT_TEST_NETWORK && -z "$SERVER_DIRECTORY" ]]; then
echo "init test network should be use with server directory (-sd) option"
exit 1
fi
if $INIT_TEST_NETWORK && [[ ! -z "$SERVER_DIRECTORY" ]]; then
createTestNetwork
exit 0
fi
if ((!success)); then
printHelp
exit 1
fi
}
printHelp() {
echo "
Usage : ./run_game.sh [OPTIONS]
Options:
-st, --ss2d-type [starter OR major] type of competition
-bl, --binary-location location of the binary
-ec, --exit-command exit command
-ld, --log_directory log directory
-sd, --server-directory network/server directory for test servers
-ltbl, --left-team-binary-location binary location of test team
-in, --init-test-networks init test networks
"
}
createLogDirectory() {
LOG_BASE_DIR=$LOG_DIR
# LOG_DIR=$LOG_BASE_DIR/$TEST_TEAM_NAME
# EVENT_DIR=$LOG_BASE_DIR/$TEST_TEAM_NAME
RUN "rm -r \"$LOG_DIR\"" -nc
RUN "mkdir -p \"$LOG_DIR\"" -nc
# RUN "rm -r \"$EVENT_DIR\"" -nc
# RUN "mkdir -p \"$EVENT_DIR\"" -nc
}
buildTestBinary() {
echo "--- start build ${TEAM_NAME}"
RUN "mkdir $TEAM_LEFT_BINARY_LOCATION/binABCDE" -po
RUN "mv $TEAM_LEFT_BINARY_LOCATION/* $TEAM_LEFT_BINARY_LOCATION/binABCDE" -po
RUN "mv $TEAM_LEFT_BINARY_LOCATION/binABCDE $TEAM_LEFT_BINARY_LOCATION/bin" -po
RUN "chmod 777 $TEAM_LEFT_BINARY_LOCATION/bin/* -R" -po
RUN "cp teams.Dockerfile $TEAM_LEFT_BINARY_LOCATION/Dockerfile" -po
OLD_PWD=$PWD
RUN "cd $TEAM_LEFT_BINARY_LOCATION && docker build -t $TEST_TEAM_NAME:latest ." -ef
cd $OLD_PWD
RUN "mkdir -p $dir/bins/$ORIGINAL_TEAM_NAME" -nc
RUN "cp -r $TEAM_LEFT_BINARY_LOCATION/* $dir/bins/$ORIGINAL_TEAM_NAME/"
echo "--- end build ${TEAM_NAME}"
}
findEmptyServer() {
if [ ! -d "$SERVER_DIRECTORY" ]; then
RUN "mkdir -p ${SERVER_DIRECTORY}"
RUN "echo 1 > ${SERVER_DIRECTORY}/test1" -po
RUN "echo 1 > ${SERVER_DIRECTORY}/test2" -po
RUN "echo 1 > ${SERVER_DIRECTORY}/test3" -po
RUN "echo 1 > ${SERVER_DIRECTORY}/test4" -po
# Take action if $DIR exists. #
echo "Installing config files in ${DIR}..."
fi
for server in $SERVER_DIRECTORY/*; do
S=$(<$server)
if [[ "$S" == "1" ]]; then
TEST_SERVER_NAME=$(echo "$server" | tr '/' '\n' | tail -n1)
RUN "echo \"0\"> $server" -po
NETWORK=$TEST_SERVER_NAME
break
fi
done
if [ -z "$TEST_SERVER_NAME" ]; then
echo "** no empty server!"
exit
fi
}
runTest() {
echo "--- running test"
printParams
RUN "./run_game.sh -st ${SS2D_TYPE} -gt ${GAME_TYPE} -ld ${LOG_DIR} -ed ${EVENT_DIR} -l ${TEST_TEAM_NAME} -r ${TEAM_RIGHT} -n ${TEST_SERVER_NAME} -nt" -po
echo "--- end running test"
}
printParams() {
echo "-------------------------------------"
echo " ENVIRONMENTS"
echo "-------------------------------------"
echo "SS2D_TYPE : " ${SS2D_TYPE}
echo "GAME_TYPE : " ${GAME_TYPE}
echo "LOG_DIR : " ${LOG_DIR}
echo "EVENT_DIR : " ${EVENT_DIR}
echo "TEAM_RIGHT : " ${TEAM_RIGHT}
echo "NETWORK : " ${NETWORK}
echo "TIME_STAMP : " ${TIME_STAMP}
echo "SERVER_DIRECTORY: " ${SERVER_DIRECTORY}
echo "-------------------------------------"
}
killTestServer() {
echo "--- kill last server & players"
echo "--- last server & players killed"
}
# mire to foldere TEST_SERVER_FOLDER donbale file haye serveraye 0 migarde
# age peyda kone esmesho mirize to TEST_SERVER_NAME va 1 mikone
# Q QUEUE mikhaim bara do ta server faghat -> telegram goftam behet
compressLogsAndOutputs() {
echo "--compress ${TEAM_NAME} Logs And Outputs"
ARCHIVE_FILE_ADDRESS=$LOG_BASE_DIR/$TIME_STAMP.tar.gz
RUN "tar -czf $ARCHIVE_FILE_ADDRESS $LOG_DIR $EVENT_DIR"
RUN "rm -r $LOG_DIR $EVENT_DIR"
echo "--END compress ${TEAM_NAME} Logs And Outputs"
}
emptyUsedTestServer() {
RUN "echo \"1\"> $SERVER_DIRECTORY/$TEST_SERVER_NAME" -po
}
runExitCommand() {
if [ ! -z "$EXIT_COMMAND" ]; then
EC="${EXIT_COMMAND/LOG_DIR/"$LOG_DIR"}"
EC="${EC/EVENT_DIR/"$EVENT_DIR"}"
RUN "$EC" -nc -po
fi
}
removeTestTeamDocker() {
RUN "docker rmi --force $TEST_TEAM_NAME"
}
main() {
checkParams "$@"
# clear
createLogDirectory
buildTestBinary
findEmptyServer
runTest
# killTestServer
# compressLogsAndOutputs
emptyUsedTestServer
removeTestTeamDocker
runExitCommand
echo "+ game end. exit... "
}
main "$@"
exit 0