tests_jc2server.sh 14 KB

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