tests_jc2server.sh 13 KB

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