tests_ts3server.sh 9.9 KB

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