#!/bin/bash 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 declare -r EX_OK=0 declare -r EX_INVALID_USER=64 declare -r EX_INVALID_COMMAND=65 declare -r EX_INVALID_ARGUMENT=66 declare -r EX_SERVER_STOPPED=67 declare -r EX_SERVER_RUNNING=68 declare -r EX_NAME_NOT_FOUND=69 declare -r EX_FILE_NOT_FOUND=70 declare -r EX_DUPLICATE_NAME=71 declare -r EX_LOGS_NOT_ROLLED=72 declare -r EX_CONF_ERROR=73 oneTimeSetUp() { # Variables used in tests SCRIPT="${MSM_SCRIPT:-${DIR}/init/msm}" export MSM_CONF="${TMP_DIR}/msm.conf" } setUp() { source "$DEFUALT_CONF" # 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" # Overwrite the directories to use for testin 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 "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_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 "$MSM_CONF" # 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 # 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 $SCRIPT server list assertEquals "Incorrect exit code." $EX_OK $EXIT_CODE } ### "msm server create" tests test_reserved_server_names() { for name in "start" "stop" "restart" "server" "version" "jargroup" "all"; do expect_stderr $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 \"$SERVER_STORAGE_PATH/$name\" ]" done } test_common_invalid_server_names() { expect_stderr $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 \"$SERVER_STORAGE_PATH/name with spaces\" ]" } test_valid_edge_case_server_names() { for name in "serverstart" "CapitalLetters" "0987654321" "name-with-dashes" "name_with_underscores" "Combination-of_different1Things2"; do expect_stderr_empty $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 \"$SERVER_STORAGE_PATH/$name\" ]" done } test_creating_server_without_any_jargroups() { expect_stderr_empty $SCRIPT server create example assertEquals "Incorrect exit code." $EX_OK $EXIT_CODE assertTrue "Server was not created." "[ -d \"$SERVER_STORAGE_PATH/example\" ]" } # Assumes: test_create_server_without_any_jargroups test_creating_server_when_that_name_already_exists() { # Create server "example" quiet $SCRIPT server create example # Create another server called "example", should be prevented expect_stderr $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 $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 $SCRIPT server create example assertEquals "Incorrect exit code." $EX_OK $EXIT_CODE assertTrue "Server direcotry was not created." "[ -d \"$SERVER_STORAGE_PATH/example\" ]" assertTrue "Server jar was not linked." "[ -f \"$SERVER_STORAGE_PATH/example/$DEFAULT_JAR\" ]" } ### "msm server delete" tests test_deleting_server_that_does_not_exist() { expect_stderr $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 $SCRIPT server create example expect_stderr_empty $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 $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 $SCRIPT server create example expect_stderr_empty $SCRIPT server rename example example_new_name assertEquals "Incorrect exit code." $EX_OK $EXIT_CODE assertFalse "Original server name directory still exists." "[ -d \"$SERVER_STORAGE_PATH/example\" ]" assertTrue "New server name directory was not created." "[ -d \"$SERVER_STORAGE_PATH/example_new_name\" ]" } # Individual Server Tests # ----------------------- ### "msm start" tests ### "msm stop" tests ### "msm restart" tests ### "msm status" tests ### "msm connected" tests ### "msm worlds list" tests ### "msm worlds load" tests ### "msm worlds ram" tests ### "msm worlds todisk" tests ### "msm worlds backup" tests ### "msm worlds on" tests ### "msm worlds off" tests ### "msm worlds off" tests ### "msm logroll" tests ### "msm backup" tests ### "msm jar" tests ### "msm whitelist on" tests ### "msm whitelist off" tests ### "msm whitelist add" tests ### "msm whitelist remove" tests ### "msm whitelist list" tests ### "msm blacklist player add" tests ### "msm blacklist player remove" tests ### "msm blacklist ip add" tests ### "msm blacklist ip remove" tests ### "msm blacklist list" tests ### "msm operator add" tests ### "msm operator remove" tests ### "msm operator list" tests ### "msm gamemode" tests ### "msm kick" tests ### "msm say" tests ### "msm time set" tests ### "msm time add" tests ### "msm toggledownfall" tests ### "msm save on" tests ### "msm save off" tests ### "msm save all" tests ### "msm cmd" tests ### "msm cmdlog" tests ### "msm 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 $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 $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() { for name in "serverstart" "CapitalLetters" "0987654321" "name-with-dashes" "name_with_underscores" "Combination-of_different1Things2"; do expect_stderr_empty $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 \"$JAR_STORAGE_PATH/$name\" ]" done } test_creating_jargroup() { expect_stderr_empty $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 $SCRIPT jargroup create minecraft https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.jar expect_stderr $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 $SCRIPT jargroup list assertEquals "Incorrect exit code." $EX_OK $EXIT_CODE } # Assumes: test_creating_jargroup test_listing_one_jargroup() { quiet $SCRIPT jargroup create minecraft https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.jar expect_stderr_empty $SCRIPT jargroup list assertEquals "Incorrect exit code." $EX_OK $EXIT_CODE } # Assumes: test_creating_jargroup test_listing_multiple_jargroups() { quiet $SCRIPT jargroup create minecraft https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.jar quiet $SCRIPT jargroup create craftbukkit http://dl.bukkit.org/latest-rb/craftbukkit.jar expect_stderr_empty $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 source shunit2