tests_jc2server.sh 15 KB

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