tests_jc2server.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. #!/bin/bash
  2. # TravisCI Tests
  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. # Steam login
  23. steamuser="anonymous"
  24. steampass=""
  25. # Start Variables
  26. updateonstart="off"
  27. fn_parms(){
  28. parms=""
  29. }
  30. #### Advanced Variables ####
  31. # Github Branch Select
  32. # Allows for the use of different function files
  33. # from a different repo and/or branch.
  34. githubuser="dgibbs64"
  35. githubrepo="linuxgsm"
  36. githubbranch="$TRAVIS_BRANCH"
  37. githubbranch="exitcodes"
  38. # Steam
  39. appid="261140"
  40. # Server Details
  41. servicename="jc2-server"
  42. gamename="Just Cause 2"
  43. engine="avalanche"
  44. # Directories
  45. rootdir="$(dirname $(readlink -f "${BASH_SOURCE[0]}"))"
  46. selfname="$(basename $(readlink -f "${BASH_SOURCE[0]}"))"
  47. lockselfname=".${servicename}.lock"
  48. lgsmdir="${rootdir}/lgsm"
  49. functionsdir="${lgsmdir}/functions"
  50. libdir="${lgsmdir}/lib"
  51. filesdir="${rootdir}/serverfiles"
  52. systemdir="${filesdir}"
  53. executabledir="${filesdir}"
  54. executable="./Jcmp-Server"
  55. servercfg="config.lua"
  56. servercfgdir="${filesdir}"
  57. servercfgfullpath="${servercfgdir}/${servercfg}"
  58. servercfgdefault="${servercfgdir}/default_config.lua"
  59. backupdir="${rootdir}/backups"
  60. # Logging
  61. logdays="7"
  62. #gamelogdir="" # No server logs available
  63. scriptlogdir="${rootdir}/log/script"
  64. consolelogdir="${rootdir}/log/console"
  65. consolelogging="on"
  66. scriptlog="${scriptlogdir}/${servicename}-script.log"
  67. consolelog="${consolelogdir}/${servicename}-console.log"
  68. emaillog="${scriptlogdir}/${servicename}-email.log"
  69. scriptlogdate="${scriptlogdir}/${servicename}-script-$(date '+%d-%m-%Y-%H-%M-%S').log"
  70. consolelogdate="${consolelogdir}/${servicename}-console-$(date '+%d-%m-%Y-%H-%M-%S').log"
  71. ##### Script #####
  72. # Do not edit
  73. # Fetches core_dl for file downloads
  74. fn_fetch_core_dl(){
  75. github_file_url_dir="lgsm/functions"
  76. github_file_url_name="${functionfile}"
  77. filedir="${functionsdir}"
  78. filename="${github_file_url_name}"
  79. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  80. # If the file is missing, then download
  81. if [ ! -f "${filedir}/${filename}" ]; then
  82. if [ ! -d "${filedir}" ]; then
  83. mkdir -p "${filedir}"
  84. fi
  85. echo -e " fetching ${filename}...\c"
  86. # Check curl exists and use available path
  87. curlpaths="$(command -v curl 2>/dev/null) $(which curl >/dev/null 2>&1) /usr/bin/curl /bin/curl /usr/sbin/curl /sbin/curl)"
  88. for curlcmd in ${curlpaths}
  89. do
  90. if [ -x "${curlcmd}" ]; then
  91. break
  92. fi
  93. done
  94. # If curl exists download file
  95. if [ "$(basename ${curlcmd})" == "curl" ]; then
  96. curlfetch=$(${curlcmd} -s --fail -o "${filedir}/${filename}" "${githuburl}" 2>&1)
  97. if [ $? -ne 0 ]; then
  98. echo -e "\e[0;31mFAIL\e[0m\n"
  99. echo "${curlfetch}"
  100. echo -e "${githuburl}\n"
  101. exit 1
  102. else
  103. echo -e "\e[0;32mOK\e[0m"
  104. fi
  105. else
  106. echo -e "\e[0;31mFAIL\e[0m\n"
  107. echo "Curl is not installed!"
  108. echo -e ""
  109. exit 1
  110. fi
  111. chmod +x "${filedir}/${filename}"
  112. fi
  113. source "${filedir}/${filename}"
  114. }
  115. core_dl.sh(){
  116. # Functions are defined in core_functions.sh.
  117. functionfile="${FUNCNAME}"
  118. fn_fetch_core_dl
  119. }
  120. core_functions.sh(){
  121. # Functions are defined in core_functions.sh.
  122. functionfile="${FUNCNAME}"
  123. fn_fetch_core_dl
  124. }
  125. core_dl.sh
  126. core_functions.sh
  127. fn_currentstatus_tmux(){
  128. pid=$(tmux list-sessions 2>&1 | awk '{print $1}' | grep -Ec "^${servicename}:")
  129. if [ "${pid}" != "0" ]; then
  130. currentstatus="ONLINE"
  131. else
  132. currentstatus="OFFLINE"
  133. fi
  134. }
  135. fn_currentstatus_ts3(){
  136. ts3status=$(${executable} status servercfgfullpathfile=${servercfgfullpath})
  137. if [ "${ts3status}" == "Server is running" ]; then
  138. currentstatus="ONLINE"
  139. else
  140. currentstatus="OFFLINE"
  141. fi
  142. }
  143. fn_setstatus(){
  144. fn_currentstatus_tmux
  145. echo""
  146. echo "Required status: ${requiredstatus}"
  147. counter=0
  148. echo "Current status: ${currentstatus}"
  149. while [ "${requiredstatus}" != "${currentstatus}" ]; do
  150. counter=$((counter+1))
  151. fn_currentstatus_tmux
  152. echo -ne "New status: ${currentstatus}\\r"
  153. if [ "${requiredstatus}" == "ONLINE" ]; then
  154. (command_start.sh > /dev/null 2>&1)
  155. else
  156. (command_stop.sh > /dev/null 2>&1)
  157. fi
  158. if [ "${counter}" -gt "5" ]; then
  159. currentstatus="FAIL"
  160. echo "Current status: ${currentstatus}"
  161. echo ""
  162. echo "Unable to start or stop server."
  163. exit 1
  164. fi
  165. done
  166. echo -ne "New status: ${currentstatus}\\r"
  167. echo -e "\n"
  168. echo "Test starting:"
  169. echo ""
  170. sleep 0.5
  171. }
  172. # End of every test will expect the result to either pass or fail
  173. # If the script does not do as intended the whole test will fail
  174. # if excpecting a pass
  175. fn_test_result_pass(){
  176. if [ $? != 0 ]; then
  177. echo "================================="
  178. echo "Expected result: PASS"
  179. echo "Actual result: FAIL"
  180. fn_print_fail_nl "TEST FAILED"
  181. exitcode=1
  182. core_exit.sh
  183. else
  184. echo "================================="
  185. echo "Expected result: PASS"
  186. echo "Actual result: PASS"
  187. fn_print_ok_nl "TEST PASSED"
  188. echo ""
  189. fi
  190. }
  191. # if excpecting a fail
  192. fn_test_result_fail(){
  193. if [ $? == 0 ]; then
  194. echo "================================="
  195. echo "Expected result: FAIL"
  196. echo "Actual result: PASS"
  197. fn_print_fail_nl "TEST FAILED"
  198. exitcode=1
  199. core_exit.sh
  200. else
  201. echo "================================="
  202. echo "Expected result: FAIL"
  203. echo "Actual result: FAIL"
  204. fn_print_ok_nl "TEST PASSED"
  205. echo ""
  206. fi
  207. }
  208. echo "================================="
  209. echo "TravisCI Tests"
  210. echo "Linux Game Server Manager"
  211. echo "by Daniel Gibbs"
  212. echo "https://gameservermanagers.com"
  213. echo "================================="
  214. echo ""
  215. echo "================================="
  216. echo "Server Tests"
  217. echo "Using: ${gamename}"
  218. echo "Testing Branch: $TRAVIS_BRANCH"
  219. echo "================================="
  220. echo ""
  221. echo "================================="
  222. echo "Description:"
  223. echo ""
  224. (install_logs.sh)
  225. echo "================================="
  226. echo "Description:"
  227. echo "Enable dev-debug"
  228. echo ""
  229. (command_dev_debug.sh)
  230. fn_test_result_pass
  231. echo "1.0 - start - no files"
  232. echo "================================="
  233. echo "Description:"
  234. echo "test script reaction to missing server files."
  235. echo "Command: ./jc2server start"
  236. echo ""
  237. (command_start.sh)
  238. fn_test_result_fail
  239. echo ""
  240. echo "1.1 - getopt"
  241. echo "================================="
  242. echo "Description:"
  243. echo "displaying options messages."
  244. echo "Command: ./jc2server"
  245. echo ""
  246. (core_getopt.sh)
  247. fn_test_result_pass
  248. echo ""
  249. echo "1.2 - getopt with incorrect args"
  250. echo "================================="
  251. echo "Description:"
  252. echo "displaying options messages."
  253. echo "Command: ./jc2server abc123"
  254. echo ""
  255. getopt="abc123"
  256. (core_getopt.sh)
  257. fn_test_result_fail
  258. echo ""
  259. echo "2.0 - install"
  260. echo "================================="
  261. echo "Description:"
  262. echo "install ${gamename} server."
  263. echo "Command: ./jc2server auto-install"
  264. (fn_autoinstall)
  265. fn_test_result_pass
  266. echo ""
  267. echo "3.1 - start"
  268. echo "================================="
  269. echo "Description:"
  270. echo "start ${gamename} server."
  271. echo "Command: ./jc2server start"
  272. requiredstatus="OFFLINE"
  273. fn_setstatus
  274. (command_start.sh)
  275. fn_test_result_pass
  276. echo ""
  277. echo "3.2 - start - online"
  278. echo "================================="
  279. echo "Description:"
  280. echo "start ${gamename} server while already running."
  281. echo "Command: ./jc2server start"
  282. requiredstatus="ONLINE"
  283. fn_setstatus
  284. (command_start.sh)
  285. fn_test_result_fail
  286. echo ""
  287. echo "3.3 - start - updateonstart"
  288. echo "================================="
  289. echo "Description:"
  290. echo "will update server on start."
  291. echo "Command: ./jc2server start"
  292. requiredstatus="OFFLINE"
  293. fn_setstatus
  294. (updateonstart="on";command_start.sh)
  295. fn_test_result_pass
  296. echo ""
  297. echo "3.4 - stop"
  298. echo "================================="
  299. echo "Description:"
  300. echo "stop ${gamename} server."
  301. echo "Command: ./jc2server stop"
  302. requiredstatus="ONLINE"
  303. fn_setstatus
  304. (command_stop.sh)
  305. fn_test_result_pass
  306. echo ""
  307. echo "3.5 - stop - offline"
  308. echo "================================="
  309. echo "Description:"
  310. echo "stop ${gamename} server while already stopped."
  311. echo "Command: ./jc2server stop"
  312. requiredstatus="OFFLINE"
  313. (command_stop.sh)
  314. fn_test_result_fail
  315. echo ""
  316. echo "3.6 - restart"
  317. echo "================================="
  318. echo "Description:"
  319. echo "restart ${gamename}."
  320. echo "Command: ./jc2server restart"
  321. requiredstatus="ONLINE"
  322. fn_setstatus
  323. command_restart.sh
  324. fn_test_result_pass
  325. echo ""
  326. echo "3.7 - restart - offline"
  327. echo "================================="
  328. echo "Description:"
  329. echo "restart ${gamename} while already stopped."
  330. echo "Command: ./jc2server restart"
  331. requiredstatus="OFFLINE"
  332. fn_setstatus
  333. command_restart.sh
  334. fn_test_result_pass
  335. echo "4.1 - update"
  336. echo "================================="
  337. echo "Description:"
  338. echo "check for updates."
  339. echo "Command: ./jc2server update"
  340. requiredstatus="OFFLINE"
  341. fn_setstatus
  342. (command_update.sh)
  343. fn_test_result_pass
  344. echo ""
  345. echo "4.2 - update - change buildid"
  346. echo "================================="
  347. echo "Description:"
  348. echo "change the buildid tricking SteamCMD to update."
  349. echo "Command: ./jc2server update"
  350. requiredstatus="OFFLINE"
  351. fn_setstatus
  352. fn_print_info_nl "changed buildid to 0."
  353. sed -i 's/[0-9]\+/0/' "${filesdir}/steamapps/appmanifest_${appid}.acf"
  354. (command_update.sh)
  355. fn_test_result_pass
  356. echo ""
  357. echo "4.3 - update - change buildid - online"
  358. echo "================================="
  359. echo "Description:"
  360. echo "change the buildid tricking SteamCMD to update server while already running."
  361. echo "Command: ./jc2server update"
  362. requiredstatus="ONLINE"
  363. fn_setstatus
  364. fn_print_info_nl "changed buildid to 0."
  365. sed -i 's/[0-9]\+/0/' "${filesdir}/steamapps/appmanifest_${appid}.acf"
  366. (command_update.sh)
  367. fn_test_result_pass
  368. echo ""
  369. echo "4.4 - update - remove appmanifest file"
  370. echo "================================="
  371. echo "Description:"
  372. echo "removing appmanifest file will cause script to repair."
  373. echo "Command: ./jc2server update"
  374. requiredstatus="OFFLINE"
  375. fn_setstatus
  376. fn_print_info_nl "removed appmanifest_${appid}.acf."
  377. rm --verbose "${filesdir}/steamapps/appmanifest_${appid}.acf"
  378. (command_update.sh)
  379. fn_test_result_pass
  380. echo ""
  381. echo "4.5 - force-update"
  382. echo "================================="
  383. echo "Description:"
  384. echo "force-update bypassing update check."
  385. echo "Command: ./jc2server force-update"
  386. requiredstatus="OFFLINE"
  387. fn_setstatus
  388. (forceupdate=1;command_update.sh)
  389. fn_test_result_pass
  390. echo ""
  391. echo "4.6 - force-update - online"
  392. echo "================================="
  393. echo "Description:"
  394. echo "force-update bypassing update check server while already running."
  395. echo "Command: ./jc2server force-update"
  396. requiredstatus="ONLINE"
  397. fn_setstatus
  398. (forceupdate=1;command_update.sh)
  399. fn_test_result_pass
  400. echo ""
  401. echo "4.7 - validate"
  402. echo "================================="
  403. echo "Description:"
  404. echo "validate server files."
  405. echo "Command: ./jc2server validate"
  406. requiredstatus="OFFLINE"
  407. fn_setstatus
  408. (command_validate.sh)
  409. fn_test_result_pass
  410. echo ""
  411. echo "4.8 - validate - online"
  412. echo "================================="
  413. echo "Description:"
  414. echo "validate server files while server while already running."
  415. echo ""
  416. echo "Command: ./jc2server validate"
  417. requiredstatus="ONLINE"
  418. fn_setstatus
  419. (command_validate.sh)
  420. fn_test_result_pass
  421. echo ""
  422. echo "5.1 - monitor - online"
  423. echo "================================="
  424. echo "Description:"
  425. echo "run monitor server while already running."
  426. echo "Command: ./jc2server monitor"
  427. requiredstatus="ONLINE"
  428. fn_setstatus
  429. (command_monitor.sh)
  430. fn_test_result_pass
  431. echo ""
  432. echo "5.2 - monitor - offline - with lockfile"
  433. echo "================================="
  434. echo "Description:"
  435. echo "run monitor while server is offline with lockfile."
  436. echo "Command: ./jc2server monitor"
  437. requiredstatus="OFFLINE"
  438. fn_setstatus
  439. fn_print_info_nl "creating lockfile."
  440. date > "${rootdir}/${lockselfname}"
  441. (command_monitor.sh)
  442. fn_test_result_fail
  443. echo ""
  444. echo "5.3 - monitor - offline - no lockfile"
  445. echo "================================="
  446. echo "Description:"
  447. echo "run monitor while server is offline with no lockfile."
  448. echo "Command: ./jc2server monitor"
  449. requiredstatus="OFFLINE"
  450. fn_setstatus
  451. (command_monitor.sh)
  452. fn_test_result_fail
  453. echo ""
  454. echo "5.4 - monitor - gsquery.py failure"
  455. echo "================================="
  456. echo "Description:"
  457. echo "gsquery.py will fail to query port."
  458. echo "Command: ./jc2server monitor"
  459. requiredstatus="ONLINE"
  460. fn_setstatus
  461. sed -i 's/[0-9]\+/0/' "${servercfgfullpath}"
  462. (command_monitor.sh)
  463. fn_test_result_fail
  464. echo ""
  465. fn_print_info_nl "Reseting ${servercfg}."
  466. install_config.sh
  467. fn_test_result_fail
  468. echo ""
  469. echo "6.0 - details"
  470. echo "================================="
  471. echo "Description:"
  472. echo "display details."
  473. echo "Command: ./jc2server details"
  474. requiredstatus="ONLINE"
  475. fn_setstatus
  476. command_details.sh
  477. fn_test_result_pass
  478. echo ""
  479. echo "================================="
  480. echo "Server Tests - Complete!"
  481. echo "Using: ${gamename}"
  482. echo "================================="
  483. requiredstatus="OFFLINE"
  484. fn_setstatus
  485. sleep 1
  486. fn_print_info "Tidying up directories."
  487. sleep 1
  488. rm -rfv "${serverfiles}"
  489. core_exit.sh