tests_jc2server.sh 15 KB

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