tests_ts3server.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. #!/bin/bash
  2. # TravisCI Tests: Teamspeak 3
  3. # Server Management Script
  4. # Author: Daniel Gibbs
  5. # Website: https://gameservermanagers.com
  6. version="101716"
  7. if [ -f ".dev-debug" ]; then
  8. exec 5>dev-debug.log
  9. BASH_XTRACEFD="5"
  10. set -x
  11. fi
  12. #### Variables ####
  13. # Notification Alerts
  14. # (on|off)
  15. # Email
  16. emailalert="off"
  17. email="email@example.com"
  18. #emailfrom="email@example.com"
  19. # Pushbullet
  20. # https://www.pushbullet.com/#settings
  21. pushbulletalert="off"
  22. pushbullettoken="accesstoken"
  23. # Start Variables
  24. updateonstart="off"
  25. fn_parms(){
  26. parms=""
  27. }
  28. #### Advanced Variables ####
  29. # Github Branch Select
  30. # Allows for the use of different function files
  31. # from a different repo and/or branch.
  32. githubuser="GameServerManagers"
  33. githubrepo="LinuxGSM"
  34. githubbranch="$TRAVIS_BRANCH"
  35. # Server Details
  36. gamename="Teamspeak 3"
  37. servername="Teamspeak 3 Server"
  38. servicename="ts3-server"
  39. # Directories
  40. rootdir="$(dirname $(readlink -f "${BASH_SOURCE[0]}"))"
  41. selfname="$(basename $(readlink -f "${BASH_SOURCE[0]}"))"
  42. lockselfname=".${servicename}.lock"
  43. lgsmdir="${rootdir}/lgsm"
  44. functionsdir="${lgsmdir}/functions"
  45. libdir="${lgsmdir}/lib"
  46. tmpdir="${lgsmdir}/tmp"
  47. filesdir="${rootdir}/serverfiles"
  48. systemdir="${filesdir}"
  49. executabledir="${filesdir}"
  50. executable="./ts3server_startscript.sh"
  51. servercfg="${servicename}.ini"
  52. servercfgdefault="ts3server.ini"
  53. servercfgdir="${filesdir}"
  54. servercfgfullpath="${servercfgdir}/${servercfg}"
  55. backupdir="${rootdir}/backups"
  56. # Logging
  57. logdays="7"
  58. gamelogdir="${filesdir}/logs"
  59. scriptlogdir="${rootdir}/log/script"
  60. scriptlog="${scriptlogdir}/${servicename}-script.log"
  61. emaillog="${scriptlogdir}/${servicename}-email.log"
  62. scriptlogdate="${scriptlogdir}/${servicename}-script-$(date '+%d-%m-%Y-%H-%M-%S').log"
  63. ##### Script #####
  64. # Do not edit
  65. # Fetches core_dl for file downloads
  66. fn_fetch_core_dl(){
  67. github_file_url_dir="lgsm/functions"
  68. github_file_url_name="${functionfile}"
  69. filedir="${functionsdir}"
  70. filename="${github_file_url_name}"
  71. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  72. # If the file is missing, then download
  73. if [ ! -f "${filedir}/${filename}" ]; then
  74. if [ ! -d "${filedir}" ]; then
  75. mkdir -p "${filedir}"
  76. fi
  77. echo -e " fetching ${filename}...\c"
  78. # Check curl exists and use available path
  79. curlpaths="$(command -v curl 2>/dev/null) $(which curl >/dev/null 2>&1) /usr/bin/curl /bin/curl /usr/sbin/curl /sbin/curl)"
  80. for curlcmd in ${curlpaths}
  81. do
  82. if [ -x "${curlcmd}" ]; then
  83. break
  84. fi
  85. done
  86. # If curl exists download file
  87. if [ "$(basename ${curlcmd})" == "curl" ]; then
  88. curlfetch=$(${curlcmd} -s --fail -o "${filedir}/${filename}" "${githuburl}" 2>&1)
  89. if [ $? -ne 0 ]; then
  90. echo -e "\e[0;31mFAIL\e[0m\n"
  91. echo "${curlfetch}"
  92. echo -e "${githuburl}\n"
  93. exit 1
  94. else
  95. echo -e "\e[0;32mOK\e[0m"
  96. fi
  97. else
  98. echo -e "\e[0;31mFAIL\e[0m\n"
  99. echo "Curl is not installed!"
  100. echo -e ""
  101. exit 1
  102. fi
  103. chmod +x "${filedir}/${filename}"
  104. fi
  105. source "${filedir}/${filename}"
  106. }
  107. core_dl.sh(){
  108. # Functions are defined in core_functions.sh.
  109. functionfile="${FUNCNAME}"
  110. fn_fetch_core_dl
  111. }
  112. core_functions.sh(){
  113. # Functions are defined in core_functions.sh.
  114. functionfile="${FUNCNAME}"
  115. fn_fetch_core_dl
  116. }
  117. core_dl.sh
  118. core_functions.sh
  119. fn_currentstatus_tmux(){
  120. check_status.sh
  121. if [ "${status}" != "0" ]; then
  122. currentstatus="ONLINE"
  123. else
  124. currentstatus="OFFLINE"
  125. fi
  126. }
  127. fn_currentstatus_ts3(){
  128. check_status.sh
  129. if [ "${status}" != "0" ]; then
  130. currentstatus="ONLINE"
  131. else
  132. currentstatus="OFFLINE"
  133. fi
  134. }
  135. fn_setstatus(){
  136. fn_currentstatus_ts3
  137. echo""
  138. echo "Required status: ${requiredstatus}"
  139. counter=0
  140. echo "Current status: ${currentstatus}"
  141. while [ "${requiredstatus}" != "${currentstatus}" ]; do
  142. counter=$((counter+1))
  143. fn_currentstatus_ts3
  144. echo -ne "New status: ${currentstatus}\\r"
  145. if [ "${requiredstatus}" == "ONLINE" ]; then
  146. (command_start.sh > /dev/null 2>&1)
  147. else
  148. (command_stop.sh > /dev/null 2>&1)
  149. fi
  150. if [ "${counter}" -gt "5" ]; then
  151. currentstatus="FAIL"
  152. echo "Current status: ${currentstatus}"
  153. echo ""
  154. echo "Unable to start or stop server."
  155. exit 1
  156. fi
  157. done
  158. echo -ne "New status: ${currentstatus}\\r"
  159. echo -e "\n"
  160. echo "Test starting:"
  161. echo ""
  162. sleep 0.5
  163. }
  164. # End of every test will expect the result to either pass or fail
  165. # If the script does not do as intended the whole test will fail
  166. # if excpecting a pass
  167. fn_test_result_pass(){
  168. if [ $? != 0 ]; then
  169. echo "================================="
  170. echo "Expected result: PASS"
  171. echo "Actual result: FAIL"
  172. fn_print_fail_nl "TEST FAILED"
  173. exitcode=1
  174. core_exit.sh
  175. else
  176. echo "================================="
  177. echo "Expected result: PASS"
  178. echo "Actual result: PASS"
  179. fn_print_ok_nl "TEST PASSED"
  180. echo ""
  181. fi
  182. }
  183. # if excpecting a fail
  184. fn_test_result_fail(){
  185. if [ $? == 0 ]; then
  186. echo "================================="
  187. echo "Expected result: FAIL"
  188. echo "Actual result: PASS"
  189. fn_print_fail_nl "TEST FAILED"
  190. exitcode=1
  191. core_exit.sh
  192. else
  193. echo "================================="
  194. echo "Expected result: FAIL"
  195. echo "Actual result: FAIL"
  196. fn_print_ok_nl "TEST PASSED"
  197. echo ""
  198. fi
  199. }
  200. echo "================================="
  201. echo "TravisCI Tests"
  202. echo "Linux Game Server Manager"
  203. echo "by Daniel Gibbs"
  204. echo "https://gameservermanagers.com"
  205. echo "================================="
  206. echo ""
  207. echo "================================="
  208. echo "Server Tests"
  209. echo "Using: ${gamename}"
  210. echo "Testing Branch: $TRAVIS_BRANCH"
  211. echo "================================="
  212. echo ""
  213. echo "0.1 - Create log dir's"
  214. echo "================================="
  215. echo "Description:"
  216. echo "Create log dir's"
  217. echo ""
  218. (install_logs.sh)
  219. echo "0.2 - Enable dev-debug"
  220. echo "================================="
  221. echo "Description:"
  222. echo "Enable dev-debug"
  223. echo ""
  224. (command_dev_debug.sh)
  225. fn_test_result_pass
  226. echo "1.0 - start - no files"
  227. echo "================================="
  228. echo "Description:"
  229. echo "test script reaction to missing server files."
  230. echo "Command: ./ts3server start"
  231. echo ""
  232. (command_start.sh)
  233. fn_test_result_fail
  234. echo ""
  235. echo "1.1 - getopt"
  236. echo "================================="
  237. echo "Description:"
  238. echo "displaying options messages."
  239. echo "Command: ./ts3server"
  240. echo ""
  241. (core_getopt.sh)
  242. fn_test_result_pass
  243. echo ""
  244. echo "1.2 - getopt with incorrect args"
  245. echo "================================="
  246. echo "Description:"
  247. echo "displaying options messages."
  248. echo "Command: ./ts3server abc123"
  249. echo ""
  250. getopt="abc123"
  251. (core_getopt.sh)
  252. fn_test_result_fail
  253. echo ""
  254. echo "2.0 - install"
  255. echo "================================="
  256. echo "Description:"
  257. echo "install ${gamename} server."
  258. echo "Command: ./ts3server auto-install"
  259. (fn_autoinstall)
  260. fn_test_result_pass
  261. echo ""
  262. echo "3.1 - start"
  263. echo "================================="
  264. echo "Description:"
  265. echo "start ${gamename} server."
  266. echo "Command: ./ts3server start"
  267. requiredstatus="OFFLINE"
  268. fn_setstatus
  269. (command_start.sh)
  270. fn_test_result_pass
  271. echo ""
  272. echo "3.2 - start - online"
  273. echo "================================="
  274. echo "Description:"
  275. echo "start ${gamename} server while already running."
  276. echo "Command: ./ts3server start"
  277. requiredstatus="ONLINE"
  278. fn_setstatus
  279. (command_start.sh)
  280. fn_test_result_fail
  281. echo ""
  282. echo "3.3 - start - updateonstart"
  283. echo "================================="
  284. echo "Description:"
  285. echo "will update server on start."
  286. echo "Command: ./ts3server start"
  287. requiredstatus="OFFLINE"
  288. fn_setstatus
  289. (updateonstart="on";command_start.sh)
  290. fn_test_result_pass
  291. echo ""
  292. echo "3.4 - stop"
  293. echo "================================="
  294. echo "Description:"
  295. echo "stop ${gamename} server."
  296. echo "Command: ./ts3server stop"
  297. requiredstatus="ONLINE"
  298. fn_setstatus
  299. (command_stop.sh)
  300. fn_test_result_pass
  301. echo ""
  302. echo "3.5 - stop - offline"
  303. echo "================================="
  304. echo "Description:"
  305. echo "stop ${gamename} server while already stopped."
  306. echo "Command: ./ts3server stop"
  307. requiredstatus="OFFLINE"
  308. fn_setstatus
  309. (command_stop.sh)
  310. fn_test_result_fail
  311. echo ""
  312. echo "3.6 - restart"
  313. echo "================================="
  314. echo "Description:"
  315. echo "restart ${gamename}."
  316. echo "Command: ./ts3server restart"
  317. requiredstatus="ONLINE"
  318. fn_setstatus
  319. (command_restart.sh)
  320. fn_test_result_pass
  321. echo ""
  322. echo "3.7 - restart - offline"
  323. echo "================================="
  324. echo "Description:"
  325. echo "restart ${gamename} while already stopped."
  326. echo "Command: ./ts3server restart"
  327. requiredstatus="OFFLINE"
  328. fn_setstatus
  329. (command_restart.sh)
  330. fn_test_result_pass
  331. echo ""
  332. echo "4.1 - update"
  333. echo "================================="
  334. echo "Description:"
  335. echo "check for updates."
  336. echo "Command: ./jc2server update"
  337. requiredstatus="OFFLINE"
  338. fn_setstatus
  339. (command_update.sh)
  340. fn_test_result_pass
  341. echo ""
  342. echo "4.2 - update-functions"
  343. echo "================================="
  344. echo "Description:"
  345. echo "runs update-functions."
  346. echo ""
  347. echo "Command: ./jc2server update-functions"
  348. requiredstatus="OFFLINE"
  349. fn_setstatus
  350. (command_update_functions.sh)
  351. fn_test_result_pass
  352. echo ""
  353. echo "5.1 - monitor - online"
  354. echo "================================="
  355. echo "Description:"
  356. echo "run monitor server while already running."
  357. echo "Command: ./ts3server monitor"
  358. requiredstatus="ONLINE"
  359. fn_setstatus
  360. (command_monitor.sh)
  361. fn_test_result_pass
  362. echo ""
  363. echo "5.2 - monitor - offline - with lockfile"
  364. echo "================================="
  365. echo "Description:"
  366. echo "run monitor while server is offline with lockfile."
  367. echo "Command: ./ts3server monitor"
  368. requiredstatus="OFFLINE"
  369. fn_setstatus
  370. fn_print_info_nl "creating lockfile."
  371. date > "${rootdir}/${lockselfname}"
  372. (command_monitor.sh)
  373. fn_test_result_pass
  374. echo ""
  375. echo "5.3 - monitor - offline - no lockfile"
  376. echo "================================="
  377. echo "Description:"
  378. echo "run monitor while server is offline with no lockfile."
  379. echo "Command: ./ts3server monitor"
  380. requiredstatus="OFFLINE"
  381. fn_setstatus
  382. (command_monitor.sh)
  383. fn_test_result_fail
  384. echo ""
  385. echo "6.0 - details"
  386. echo "================================="
  387. echo "Description:"
  388. echo "display details."
  389. echo "Command: ./ts3server details"
  390. requiredstatus="ONLINE"
  391. fn_setstatus
  392. (command_details.sh)
  393. fn_test_result_pass
  394. echo ""
  395. echo "================================="
  396. echo "Server Tests - Complete!"
  397. echo "Using: ${gamename}"
  398. echo "================================="
  399. requiredstatus="OFFLINE"
  400. fn_setstatus
  401. sleep 1
  402. fn_print_info "Tidying up directories."
  403. sleep 1
  404. rm -rfv "${serverfiles}"
  405. core_exit.sh