tests_jc2server.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. # 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. bash -x tests/tests_jc2server.sh start > /tmp/output 2>&1
  317. curl --upload-file /tmp/output https://transfer.sh/lgsm
  318. (command_start.sh)
  319. fn_test_result_pass
  320. find . -name *.log
  321. echo ""
  322. echo "3.2 - start - online"
  323. echo "================================="
  324. echo "Description:"
  325. echo "start ${gamename} server while already running."
  326. echo "Command: ./jc2server start"
  327. requiredstatus="ONLINE"
  328. fn_setstatus
  329. (command_start.sh)
  330. fn_test_result_fail
  331. echo ""
  332. echo "3.3 - start - updateonstart"
  333. echo "================================="
  334. echo "Description:"
  335. echo "will update server on start."
  336. echo "Command: ./jc2server start"
  337. requiredstatus="OFFLINE"
  338. fn_setstatus
  339. (updateonstart="on";command_start.sh)
  340. fn_test_result_pass
  341. echo ""
  342. echo "3.4 - stop"
  343. echo "================================="
  344. echo "Description:"
  345. echo "stop ${gamename} server."
  346. echo "Command: ./jc2server stop"
  347. requiredstatus="ONLINE"
  348. fn_setstatus
  349. (command_stop.sh)
  350. fn_test_result_pass
  351. echo ""
  352. echo "3.5 - stop - offline"
  353. echo "================================="
  354. echo "Description:"
  355. echo "stop ${gamename} server while already stopped."
  356. echo "Command: ./jc2server stop"
  357. requiredstatus="OFFLINE"
  358. fn_setstatus
  359. (command_stop.sh)
  360. fn_test_result_fail
  361. echo ""
  362. echo "3.6 - restart"
  363. echo "================================="
  364. echo "Description:"
  365. echo "restart ${gamename}."
  366. echo "Command: ./jc2server restart"
  367. requiredstatus="ONLINE"
  368. fn_setstatus
  369. (command_restart.sh)
  370. fn_test_result_pass
  371. echo ""
  372. echo "3.7 - restart - offline"
  373. echo "================================="
  374. echo "Description:"
  375. echo "restart ${gamename} while already stopped."
  376. echo "Command: ./jc2server restart"
  377. requiredstatus="OFFLINE"
  378. fn_setstatus
  379. (command_restart.sh)
  380. fn_test_result_pass
  381. echo "4.1 - update"
  382. echo "================================="
  383. echo "Description:"
  384. echo "check for updates."
  385. echo "Command: ./jc2server update"
  386. requiredstatus="OFFLINE"
  387. fn_setstatus
  388. (command_update.sh)
  389. fn_test_result_pass
  390. echo ""
  391. echo "4.2 - update - change buildid"
  392. echo "================================="
  393. echo "Description:"
  394. echo "change the buildid tricking SteamCMD to update."
  395. echo "Command: ./jc2server update"
  396. requiredstatus="OFFLINE"
  397. fn_setstatus
  398. fn_print_info_nl "changed buildid to 0."
  399. sed -i 's/[0-9]\+/0/' "${filesdir}/steamapps/appmanifest_${appid}.acf"
  400. (command_update.sh)
  401. fn_test_result_pass
  402. echo ""
  403. echo "4.3 - update - change buildid - online"
  404. echo "================================="
  405. echo "Description:"
  406. echo "change the buildid tricking SteamCMD to update server while already running."
  407. echo "Command: ./jc2server update"
  408. requiredstatus="ONLINE"
  409. fn_setstatus
  410. fn_print_info_nl "changed buildid to 0."
  411. sed -i 's/[0-9]\+/0/' "${filesdir}/steamapps/appmanifest_${appid}.acf"
  412. (command_update.sh)
  413. fn_test_result_pass
  414. echo ""
  415. echo "4.4 - update - remove appmanifest file"
  416. echo "================================="
  417. echo "Description:"
  418. echo "removing appmanifest file will cause script to repair."
  419. echo "Command: ./jc2server update"
  420. requiredstatus="OFFLINE"
  421. fn_setstatus
  422. fn_print_info_nl "removed appmanifest_${appid}.acf."
  423. rm --verbose "${filesdir}/steamapps/appmanifest_${appid}.acf"
  424. (command_update.sh)
  425. fn_test_result_pass
  426. echo ""
  427. echo "4.5 - force-update"
  428. echo "================================="
  429. echo "Description:"
  430. echo "force-update bypassing update check."
  431. echo "Command: ./jc2server force-update"
  432. requiredstatus="OFFLINE"
  433. fn_setstatus
  434. (forceupdate=1;command_update.sh)
  435. fn_test_result_pass
  436. echo ""
  437. echo "4.6 - force-update - online"
  438. echo "================================="
  439. echo "Description:"
  440. echo "force-update bypassing update check server while already running."
  441. echo "Command: ./jc2server force-update"
  442. requiredstatus="ONLINE"
  443. fn_setstatus
  444. (forceupdate=1;command_update.sh)
  445. fn_test_result_pass
  446. echo ""
  447. echo "4.7 - validate"
  448. echo "================================="
  449. echo "Description:"
  450. echo "validate server files."
  451. echo "Command: ./jc2server validate"
  452. requiredstatus="OFFLINE"
  453. fn_setstatus
  454. (command_validate.sh)
  455. fn_test_result_pass
  456. echo ""
  457. echo "4.8 - validate - online"
  458. echo "================================="
  459. echo "Description:"
  460. echo "validate server files while server already running."
  461. echo ""
  462. echo "Command: ./jc2server validate"
  463. requiredstatus="ONLINE"
  464. fn_setstatus
  465. (command_validate.sh)
  466. fn_test_result_pass
  467. echo ""
  468. echo "4.9 - update-functions"
  469. echo "================================="
  470. echo "Description:"
  471. echo "runs update-functions."
  472. echo ""
  473. echo "Command: ./jc2server update-functions"
  474. requiredstatus="OFFLINE"
  475. fn_setstatus
  476. (command_update_functions.sh)
  477. fn_test_result_pass
  478. echo ""
  479. echo "5.1 - monitor - online"
  480. echo "================================="
  481. echo "Description:"
  482. echo "run monitor server while already running."
  483. echo "Command: ./jc2server monitor"
  484. requiredstatus="ONLINE"
  485. fn_setstatus
  486. (command_monitor.sh)
  487. fn_test_result_pass
  488. echo ""
  489. echo "5.2 - monitor - offline - with lockfile"
  490. echo "================================="
  491. echo "Description:"
  492. echo "run monitor while server is offline with lockfile."
  493. echo "Command: ./jc2server monitor"
  494. requiredstatus="OFFLINE"
  495. fn_setstatus
  496. fn_print_info_nl "creating lockfile."
  497. date > "${rootdir}/${lockselfname}"
  498. (command_monitor.sh)
  499. fn_test_result_pass
  500. echo ""
  501. echo "5.3 - monitor - offline - no lockfile"
  502. echo "================================="
  503. echo "Description:"
  504. echo "run monitor while server is offline with no lockfile."
  505. echo "Command: ./jc2server monitor"
  506. requiredstatus="OFFLINE"
  507. fn_setstatus
  508. (command_monitor.sh)
  509. fn_test_result_fail
  510. echo ""
  511. echo "5.4 - monitor - gsquery.py failure"
  512. echo "================================="
  513. echo "Description:"
  514. echo "gsquery.py will fail to query port."
  515. echo "Command: ./jc2server monitor"
  516. requiredstatus="ONLINE"
  517. fn_setstatus
  518. sed -i 's/[0-9]\+/0/' "${servercfgfullpath}"
  519. (command_monitor.sh)
  520. fn_test_result_fail
  521. echo ""
  522. fn_print_info_nl "Re-generating ${servercfg}."
  523. install_config.sh
  524. echo "================================="
  525. echo ""
  526. echo "6.0 - details"
  527. echo "================================="
  528. echo "Description:"
  529. echo "display details."
  530. echo "Command: ./jc2server details"
  531. requiredstatus="ONLINE"
  532. fn_setstatus
  533. (command_details.sh)
  534. fn_test_result_pass
  535. echo ""
  536. echo "================================="
  537. echo "Server Tests - Complete!"
  538. echo "Using: ${gamename}"
  539. echo "================================="
  540. requiredstatus="OFFLINE"
  541. fn_setstatus
  542. sleep 1
  543. fn_print_info "Tidying up directories."
  544. sleep 1
  545. rm -rfv "${serverfiles}"
  546. core_exit.sh