tests_jc2server.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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.1 - Create log dir's"
  222. echo "================================="
  223. echo "Description:"
  224. echo "Create log dir's"
  225. echo ""
  226. (install_logs.sh)
  227. echo "0.2 - Enable dev-debug"
  228. echo "================================="
  229. echo "Description:"
  230. echo "Enable dev-debug"
  231. echo ""
  232. (command_dev_debug.sh)
  233. fn_test_result_pass
  234. echo "1.0 - start - no files"
  235. echo "================================="
  236. echo "Description:"
  237. echo "test script reaction to missing server files."
  238. echo "Command: ./jc2server start"
  239. echo ""
  240. (command_start.sh)
  241. fn_test_result_fail
  242. echo ""
  243. echo "1.1 - getopt"
  244. echo "================================="
  245. echo "Description:"
  246. echo "displaying options messages."
  247. echo "Command: ./jc2server"
  248. echo ""
  249. (core_getopt.sh)
  250. fn_test_result_pass
  251. echo ""
  252. echo "1.2 - getopt with incorrect args"
  253. echo "================================="
  254. echo "Description:"
  255. echo "displaying options messages."
  256. echo "Command: ./jc2server abc123"
  257. echo ""
  258. getopt="abc123"
  259. (core_getopt.sh)
  260. fn_test_result_fail
  261. echo ""
  262. echo "2.0 - install"
  263. echo "================================="
  264. echo "Description:"
  265. echo "install ${gamename} server."
  266. echo "Command: ./jc2server auto-install"
  267. (fn_autoinstall)
  268. fn_test_result_pass
  269. echo ""
  270. echo "3.1 - start"
  271. echo "================================="
  272. echo "Description:"
  273. echo "start ${gamename} server."
  274. echo "Command: ./jc2server start"
  275. requiredstatus="OFFLINE"
  276. fn_setstatus
  277. (command_start.sh)
  278. fn_test_result_pass
  279. echo ""
  280. echo "3.2 - start - online"
  281. echo "================================="
  282. echo "Description:"
  283. echo "start ${gamename} server while already running."
  284. echo "Command: ./jc2server start"
  285. requiredstatus="ONLINE"
  286. fn_setstatus
  287. (command_start.sh)
  288. fn_test_result_fail
  289. echo ""
  290. echo "3.3 - start - updateonstart"
  291. echo "================================="
  292. echo "Description:"
  293. echo "will update server on start."
  294. echo "Command: ./jc2server start"
  295. requiredstatus="OFFLINE"
  296. fn_setstatus
  297. (updateonstart="on";command_start.sh)
  298. fn_test_result_pass
  299. echo ""
  300. echo "3.4 - stop"
  301. echo "================================="
  302. echo "Description:"
  303. echo "stop ${gamename} server."
  304. echo "Command: ./jc2server stop"
  305. requiredstatus="ONLINE"
  306. fn_setstatus
  307. (command_stop.sh)
  308. fn_test_result_pass
  309. echo ""
  310. echo "3.5 - stop - offline"
  311. echo "================================="
  312. echo "Description:"
  313. echo "stop ${gamename} server while already stopped."
  314. echo "Command: ./jc2server stop"
  315. requiredstatus="OFFLINE"
  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 while 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_fail
  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 "Reseting ${servercfg}."
  469. install_config.sh
  470. fn_test_result_fail
  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