tests_jc2server.sh 13 KB

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