-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.sh
executable file
·581 lines (409 loc) · 16.5 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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#!/bin/bash
USERNAME="minecraft"
DIR="${MSM_DIR:-$(pwd)}"
DEFUALT_CONF="${MSM_DEFAULT_CONF:-${DIR}/msm.conf}"
TESTS_DIR="${MSM_TESTS_DIR:-${DIR}/tests}"
TMP_DIR="/tmp/msmtest"
TEST_RAM="256"
# Exit codes
EX_OK=0
EX_INVALID_USER=64
EX_INVALID_COMMAND=65
EX_INVALID_ARGUMENT=66
EX_SERVER_STOPPED=67
EX_SERVER_RUNNING=68
EX_NAME_NOT_FOUND=69
EX_FILE_NOT_FOUND=70
EX_DUPLICATE_NAME=71
EX_LOGS_NOT_ROLLED=72
EX_CONF_ERROR=73
oneTimeSetUp() {
# Variables used in tests
TEST_SCRIPT="${MSM_TEST_SCRIPT:-${DIR}/init/msm}"
export MSM_CONF="${TMP_DIR}/msm.conf"
}
setUp() {
# Create the testing conf from the default one
mkdir -p "$TMP_DIR" && chown "$USERNAME" "$TMP_DIR"
cp "$DEFUALT_CONF" "$MSM_CONF" && chown "$USERNAME" "$MSM_CONF"
# Coppy versioning files
cp -r "${DIR}/versioning" "${TMP_DIR}/versioning"
# Overwrite variables to use for testing purposes
echo "" >> "$MSM_CONF"
echo "# Auto appended by test script:" >> "$MSM_CONF"
echo "SERVER_STORAGE_PATH=\"${TMP_DIR}/servers\"" >> "$MSM_CONF"
echo "JAR_STORAGE_PATH=\"${TMP_DIR}/jars\"" >> "$MSM_CONF"
echo "RAMDISK_STORAGE_PATH=\"${TMP_DIR}/fakeramdisk\"" >> "$MSM_CONF"
echo "VERSIONING_STORAGE_PATH=\"${TMP_DIR}/versioning\"" >> "$MSM_CONF"
echo "WORLD_ARCHIVE_PATH=\"${TMP_DIR}/archives/worlds\"" >> "$MSM_CONF"
echo "LOG_ARCHIVE_PATH=\"${TMP_DIR}/archives/logs\"" >> "$MSM_CONF"
echo "BACKUP_ARCHIVE_PATH=\"${TMP_DIR}/archives/backups\"" >> "$MSM_CONF"
echo "DEBUG=\"true\"" >> "$MSM_CONF"
echo "DEFAULT_USERNAME=\"${USERNAME}\"" >> "$MSM_CONF"
echo "DEFAULT_SCREEN_NAME=\"msmtest-{SERVER_NAME}\"" >> "$MSM_CONF"
echo "DEFAULT_INVOCATION=\"java -Xmx${TEST_RAM}M -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalPacing -XX:+AggressiveOpts -jar {JAR} nogui\"" >> "$MSM_CONF"
source $TEST_SCRIPT
# Variables accessible by all tests, which are set by the stdall, stderr,
# stdout and quiet utility functions.
declare OUTPUT
declare EXIT_CODE
}
tearDown() {
# Clear the variables used in test functions
unset EXIT_CODE
unset OUTPUT
manager_dirty_all
# Remove the temporary testing directory
if [ -d "${TMP_DIR}" ]; then
rm -rf "${TMP_DIR}"
fi
}
# Utils
# -----
reset_vars() {
unset OUTPUT
unset EXIT_CODE
}
# Ensure there is output in stderr
expect_stderr() {
reset_vars
OUTPUT=$("$@" 2>&1 1>/dev/null)
EXIT_CODE=$?
assertNotNull "No stderr given when expected." "$OUTPUT"
}
# Ensure there is NO output in stderr
expect_stderr_empty() {
reset_vars
OUTPUT=$("$@" 2>&1 1>/dev/null)
EXIT_CODE=$?
assertNull "$OUTPUT" "$OUTPUT"
}
# Ensure there is output in stdout
expect_stdout() {
reset_vars
OUTPUT=$("$@")
EXIT_CODE=$?
assertNotNull "No stdout given when expected." "$OUTPUT"
}
# Ensure there is NO output in stdout
expect_stdout_empty() {
reset_vars
OUTPUT=$("$@")
EXIT_CODE=$?
assertNull "$OUTPUT" "$OUTPUT"
}
# Execute a command silently (ignore stdout and stderr)
quiet() {
reset_vars
"$@" >& /dev/null
EXIT_CODE=$?
}
# Global Command Tests
# -----------------------
### "msm start" tests
### "msm stop" tests
### "msm restart" tests
### "msm version" tests
# Server Management Tests
# -----------------------
### "msm server list" tests
test_listing_no_servers() {
expect_stderr_empty $TEST_SCRIPT server list
assertEquals "Incorrect exit code." $EX_OK $EXIT_CODE
}
### "msm server create" tests
test_reserved_server_names() {
manager_property SERVER_STORAGE_PATH
for name in "start" "stop" "restart" "server" "version" "jargroup" "all"; do
expect_stderr $TEST_SCRIPT server create $name
assertEquals "Incorrect exit code when creating server name \"$name\"." $EX_INVALID_ARGUMENT $EXIT_CODE
assertFalse "Server \"$name\" directory was created when it should not have been." "[ -d \"$SETTINGS_SERVER_STORAGE_PATH/$name\" ]"
done
}
test_common_invalid_server_names() {
manager_property SERVER_STORAGE_PATH
expect_stderr $TEST_SCRIPT server create "name with spaces"
assertEquals "Incorrect exit code when creating server name \"name with spaces\"." $EX_INVALID_ARGUMENT $EXIT_CODE
assertFalse "Server \"name with spaces\" directory was created when it should not have been." "[ -d \"$SETTINGS_SERVER_STORAGE_PATH/name with spaces\" ]"
}
test_valid_edge_case_server_names() {
manager_property SERVER_STORAGE_PATH
for name in "serverstart" "CapitalLetters" "0987654321" "name-with-dashes" "name_with_underscores" "Combination-of_different1Things2"; do
expect_stderr_empty $TEST_SCRIPT server create $name
assertEquals "Incorrect exit code when creating server name \"$name\"." $EX_OK $EXIT_CODE
assertTrue "Server \"$name\" directory was NOT created when it should have been." "[ -d \"$SETTINGS_SERVER_STORAGE_PATH/$name\" ]"
done
}
test_creating_server_without_any_jargroups() {
manager_property SERVER_STORAGE_PATH
expect_stderr_empty $TEST_SCRIPT server create example
assertEquals "Incorrect exit code." $EX_OK $EXIT_CODE
assertTrue "Server was not created." "[ -d \"$SETTINGS_SERVER_STORAGE_PATH/example\" ]"
}
# Assumes: test_create_server_without_any_jargroups
test_creating_server_when_that_name_already_exists() {
# Create server "example"
quiet $TEST_SCRIPT server create example
# Create another server called "example", should be prevented
expect_stderr $TEST_SCRIPT server create example
assertEquals "Incorrect exit code." $EX_DUPLICATE_NAME $EXIT_CODE
}
# Assumes: test_creating_jargroup
test_creating_server_with_jar_groups() {
# Create the "minecraft" jar group, which is used by default when creating
# new servers.
quiet $TEST_SCRIPT jargroup create minecraft "https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.jar"
# Create a new server that will use the "minecraft" jar group.
expect_stderr_empty $TEST_SCRIPT server create example
# Script must be sourced again to recognise the new server
source $TEST_SCRIPT
manager_property SERVER_STORAGE_PATH
server_get_id "example"
local sid="$RETURN"
server_property "$sid" JAR_PATH
server_property "$sid" PATH
assertEquals "Incorrect exit code." $EX_OK $EXIT_CODE
assertTrue "Server direcotry was not created." "[ -d \"${SERVER_PATH[$sid]}\" ]"
assertTrue "Server jar (${SERVER_JAR_PATH[$sid]}) was not linked." "[ -f \"${SERVER_JAR_PATH[$sid]}\" ]"
}
### "msm server delete" tests
test_deleting_server_that_does_not_exist() {
expect_stderr $TEST_SCRIPT server delete example
assertEquals "Incorrect exit code." $EX_NAME_NOT_FOUND $EXIT_CODE
}
# Assumes: test_creating_server_without_any_jargroups
test_deleting_server_that_exists_and_is_stopped() {
quiet $TEST_SCRIPT server create example
expect_stderr_empty $TEST_SCRIPT server delete example <<< "y"
assertEquals "Incorrect exit code." $EX_OK $EXIT_CODE
assertFalse "Server directory was not removed." "[ -d \"$SERVER_STORAGE_PATH/example\" ]"
}
### "msm server rename" tests
test_renaming_server_that_does_not_exist() {
expect_stderr $TEST_SCRIPT server rename example example_new_name
assertEquals "Incorrect exit code." $EX_NAME_NOT_FOUND $EXIT_CODE
}
# Assumes: test_creating_server_without_any_jargroups
test_renaming_server_that_exists_and_is_stopped() {
quiet $TEST_SCRIPT server create example
expect_stderr_empty $TEST_SCRIPT server rename example example_new_name
manager_property SERVER_STORAGE_PATH
assertEquals "Incorrect exit code." $EX_OK $EXIT_CODE
assertFalse "Original server name directory still exists." "[ -d \"$SETTINGS_SERVER_STORAGE_PATH/example\" ]"
assertTrue "New server name directory was not created." "[ -d \"$SETTINGS_SERVER_STORAGE_PATH/example_new_name\" ]"
}
# Stopped Server Tests
# --------------------
### "msm <server> stop" tests
test_stopped_server_stop() {
quiet $TEST_SCRIPT server create stoppedserver
expect_stdout $TEST_SCRIPT stoppedserver stop
assertEquals "Incorrect exit code." $EX_OK $EXIT_CODE
}
### "msm <server> status" tests
test_stopped_server_status() {
quiet $TEST_SCRIPT server create stoppedserver
expect_stdout $TEST_SCRIPT stoppedserver status
assertEquals "Incorrect exit code." $EX_OK $EXIT_CODE
}
### "msm <server> connected" tests
test_stopped_server_connected() {
quiet $TEST_SCRIPT server create stoppedserver
expect_stdout $TEST_SCRIPT stoppedserver connected
assertEquals "Incorrect exit code." $EX_OK $EXIT_CODE
}
### "msm <server> worlds list" tests
test_stopped_server_worlds_list_none() {
quiet $TEST_SCRIPT server create stoppedserver
expect_stdout $TEST_SCRIPT stoppedserver worlds list
assertEquals "Incorrect exit code." $EX_OK $EXIT_CODE
}
### "msm <server> worlds load" tests
test_stopped_server_worlds_load_none() {
quiet $TEST_SCRIPT server create stoppedserver
expect_stdout $TEST_SCRIPT stoppedserver worlds load
assertEquals "Incorrect exit code." $EX_OK $EXIT_CODE
}
### "msm <server> worlds ram" tests
test_stopped_server_worlds_ram_name_not_found() {
quiet $TEST_SCRIPT server create stoppedserver
expect_stderr $TEST_SCRIPT stoppedserver worlds ram non_existant_world_name
assertEquals "Incorrect exit code." $EX_NAME_NOT_FOUND $EXIT_CODE
}
### "msm <server> worlds todisk" tests
test_stopped_server_worlds_todisk_none() {
quiet $TEST_SCRIPT server create stoppedserver
expect_stdout $TEST_SCRIPT stoppedserver worlds todisk
assertEquals "Incorrect exit code." $EX_OK $EXIT_CODE
}
### "msm <server> worlds backup" tests
test_stopped_server_worlds_todisk_none() {
quiet $TEST_SCRIPT server create stoppedserver
expect_stdout $TEST_SCRIPT stoppedserver worlds backup
assertEquals "Incorrect exit code." $EX_OK $EXIT_CODE
}
### "msm <server> worlds on" tests
### "msm <server> worlds off" tests
### "msm <server> logroll" tests
test_stopped_server_logroll_empty() {
quiet $TEST_SCRIPT server create stoppedserver
expect_stdout $TEST_SCRIPT stoppedserver logroll
assertEquals "Incorrect exit code." $EX_OK $EXIT_CODE
}
### "msm <server> backup" tests
test_stopped_server_backup() {
manager_property BACKUP_ARCHIVE_PATH
quiet $TEST_SCRIPT server create stoppedserver
expect_stdout $TEST_SCRIPT stoppedserver backup
assertEquals "Incorrect exit code." $EX_OK $EXIT_CODE
assertNotNull "Server backup was not created." "find '$SETTINGS_BACKUP_ARCHIVE_PATH/stoppedserver' -mindepth 1 -maxdepth 1 -type f -name '*.zip'"
}
### "msm <server> jar" tests
test_stopped_server_jar() {
quiet $TEST_SCRIPT server create stoppedserver
source $TEST_SCRIPT
server_get_id "stoppedserver"
local sid="$RETURN"
server_property "$sid" JAR_PATH
# Remove the existing jar file
rm -f "$JAR_PATH"
# Create new jargroup
quiet $TEST_SCRIPT jargroup create newgroup "https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.jar"
# Assign jargroup's latest jar to server
expect_stdout $TEST_SCRIPT stoppedserver jar newgroup
assertEquals "Incorrect exit code." $EX_OK $EXIT_CODE
assertTrue "Server jar (${SERVER_JAR_PATH[$sid]}) was not linked." "[ -f \"${SERVER_JAR_PATH[$sid]}\" ]"
}
### "msm <server> whitelist on" tests
### "msm <server> whitelist off" tests
### "msm <server> whitelist add" tests
### "msm <server> whitelist remove" tests
### "msm <server> whitelist list" tests
### "msm <server> blacklist player add" tests
### "msm <server> blacklist player remove" tests
### "msm <server> blacklist ip add" tests
### "msm <server> blacklist ip remove" tests
### "msm <server> blacklist list" tests
### "msm <server> operator add" tests
### "msm <server> operator remove" tests
### "msm <server> operator list" tests
### "msm <server> gamemode" tests
### "msm <server> kick" tests
### "msm <server> say" tests
### "msm <server> time set" tests
### "msm <server> time add" tests
### "msm <server> toggledownfall" tests
### "msm <server> save on" tests
### "msm <server> save off" tests
### "msm <server> save all" tests
### "msm <server> cmd" tests
### "msm <server> cmdlog" tests
### "msm <server> console" tests
# Individual Server Tests
# -----------------------
### "msm <server> start" tests
### "msm <server> stop" tests
### "msm <server> restart" tests
### "msm <server> status" tests
### "msm <server> connected" tests
### "msm <server> worlds list" tests
### "msm <server> worlds load" tests
### "msm <server> worlds ram" tests
### "msm <server> worlds todisk" tests
### "msm <server> worlds backup" tests
### "msm <server> worlds on" tests
### "msm <server> worlds off" tests
### "msm <server> worlds off" tests
### "msm <server> logroll" tests
### "msm <server> backup" tests
### "msm <server> jar" tests
### "msm <server> whitelist on" tests
### "msm <server> whitelist off" tests
### "msm <server> whitelist add" tests
### "msm <server> whitelist remove" tests
### "msm <server> whitelist list" tests
### "msm <server> blacklist player add" tests
### "msm <server> blacklist player remove" tests
### "msm <server> blacklist ip add" tests
### "msm <server> blacklist ip remove" tests
### "msm <server> blacklist list" tests
### "msm <server> operator add" tests
### "msm <server> operator remove" tests
### "msm <server> operator list" tests
### "msm <server> gamemode" tests
### "msm <server> kick" tests
### "msm <server> say" tests
### "msm <server> time set" tests
### "msm <server> time add" tests
### "msm <server> toggledownfall" tests
### "msm <server> save on" tests
### "msm <server> save off" tests
### "msm <server> save all" tests
### "msm <server> cmd" tests
### "msm <server> cmdlog" tests
### "msm <server> console" tests
# Jargroup Tests
# --------------
### "msm jargroup create" test
test_reserved_jargroup_names() {
for name in "start" "stop" "restart" "server" "version" "jargroup" "all"; do
expect_stderr $TEST_SCRIPT jargroup create "$name" https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.jar
assertEquals "Incorrect exit code when creating jar group name \"$name\"." $EX_INVALID_ARGUMENT $EXIT_CODE
assertFalse "Jar group \"$name\" directory was created when it should not have been." "[ -d \"$JAR_STORAGE_PATH/$name\" ]"
done
}
test_common_invalid_jargroups_names() {
expect_stderr $TEST_SCRIPT jargroup create "name with spaces" https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.jar
assertEquals "Incorrect exit code when creating jar group name \"name with spaces\"." $EX_INVALID_ARGUMENT $EXIT_CODE
assertFalse "Jar group \"name with spaces\" directory was created when it should not have been." "[ -d \"$JAR_STORAGE_PATH/name with spaces\" ]"
}
test_valid_edge_case_jargroup_names() {
manager_property JAR_STORAGE_PATH
for name in "serverstart" "CapitalLetters" "0987654321" "name-with-dashes" "name_with_underscores" "Combination-of_different1Things2"; do
expect_stderr_empty $TEST_SCRIPT jargroup create $name https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.jar
assertEquals "Incorrect exit code when creating jar group name \"$name\"." $EX_OK $EXIT_CODE
assertTrue "Jar group \"$name\" directory was NOT created when it should have been." "[ -d \"$SETTINGS_JAR_STORAGE_PATH/$name\" ]"
done
}
test_creating_jargroup() {
expect_stderr_empty $TEST_SCRIPT jargroup create minecraft https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.jar
assertEquals "Incorrect exit code." $EX_OK $EXIT_CODE
}
# Assumes: test_creating_jargroup
test_creating_jargroup_when_that_name_already_exists() {
quiet $TEST_SCRIPT jargroup create minecraft https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.jar
expect_stderr $TEST_SCRIPT jargroup create minecraft https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.jar
assertEquals "Incorrect exit code." $EX_DUPLICATE_NAME $EXIT_CODE
}
### "msm jargroup list" test
test_listing_no_jargroups() {
expect_stderr_empty $TEST_SCRIPT jargroup list
assertEquals "Incorrect exit code." $EX_OK $EXIT_CODE
}
# Assumes: test_creating_jargroup
test_listing_one_jargroup() {
quiet $TEST_SCRIPT jargroup create minecraft https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.jar
expect_stderr_empty $TEST_SCRIPT jargroup list
assertEquals "Incorrect exit code." $EX_OK $EXIT_CODE
}
# Assumes: test_creating_jargroup
test_listing_multiple_jargroups() {
quiet $TEST_SCRIPT jargroup create minecraft https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.jar
quiet $TEST_SCRIPT jargroup create craftbukkit http://dl.bukkit.org/latest-rb/craftbukkit.jar
expect_stderr_empty $TEST_SCRIPT jargroup list
assertEquals "Incorrect exit code." $EX_OK $EXIT_CODE
}
### "msm jargroup delete" test
### "msm jargroup rename" test
### "msm jargroup changetarget" test
### "msm jargroup getlatest" test
# Perform tests
# local copy of shunit2
source shunit2 2>/dev/null
if [ $? -ne 0 ]; then
# default install location for shunit2
source "/usr/share/shunit2/shunit2" 2>/dev/null
if [ $? -ne 0 ]; then
echo "Could not find shUnit2 install."
fi
fi