tests_jc2server.sh 15 KB

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