4
0

tests_ts3server.sh 10 KB

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