tests_jc2server.sh 13 KB

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