restore.bats 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. #!/usr/bin/env bats
  2. if [ "${PATH#*/usr/local/hestia/bin*}" = "$PATH" ]; then
  3. . /etc/profile.d/hestia.sh
  4. fi
  5. load 'test_helper/bats-support/load'
  6. load 'test_helper/bats-assert/load'
  7. load 'test_helper/bats-file/load'
  8. function random() {
  9. head /dev/urandom | tr -dc 0-9 | head -c$1
  10. }
  11. function setup() {
  12. # echo "# Setup_file" > &3
  13. if [ $BATS_TEST_NUMBER = 1 ]; then
  14. echo 'user=test-5285' > /tmp/hestia-test-env.sh
  15. echo 'user2=test-5286' >> /tmp/hestia-test-env.sh
  16. echo 'userbk=testbk-5285' >> /tmp/hestia-test-env.sh
  17. echo 'userpass1=test-5285' >> /tmp/hestia-test-env.sh
  18. echo 'userpass2=t3st-p4ssw0rd' >> /tmp/hestia-test-env.sh
  19. echo 'HESTIA=/usr/local/hestia' >> /tmp/hestia-test-env.sh
  20. echo 'domain=test-5285.hestiacp.com' >> /tmp/hestia-test-env.sh
  21. echo 'domainuk=test-5285.hestiacp.com.uk' >> /tmp/hestia-test-env.sh
  22. echo 'rootdomain=testhestiacp.com' >> /tmp/hestia-test-env.sh
  23. echo 'subdomain=cdn.testhestiacp.com' >> /tmp/hestia-test-env.sh
  24. echo 'database=test-5285_database' >> /tmp/hestia-test-env.sh
  25. echo 'dbuser=test-5285_dbuser' >> /tmp/hestia-test-env.sh
  26. fi
  27. source /tmp/hestia-test-env.sh
  28. source $HESTIA/func/main.sh
  29. source $HESTIA/conf/hestia.conf
  30. source $HESTIA/func/ip.sh
  31. }
  32. function validate_web_domain() {
  33. local user=$1
  34. local domain=$2
  35. local webproof=$3
  36. local webpath=${4}
  37. local valwebpath=${5}
  38. refute [ -z "$user" ]
  39. refute [ -z "$domain" ]
  40. refute [ -z "$webproof" ]
  41. source $HESTIA/func/ip.sh
  42. run v-list-web-domain $user $domain
  43. assert_success
  44. USER_DATA=$HESTIA/data/users/$user
  45. local domain_ip=$(get_object_value 'web' 'DOMAIN' "$domain" '$IP')
  46. SSL=$(get_object_value 'web' 'DOMAIN' "$domain" '$SSL')
  47. domain_ip=$(get_real_ip "$domain_ip")
  48. if [ -z $valwebpath ]; then
  49. if [ ! -z $webpath ]; then
  50. domain_docroot=$(get_object_value 'web' 'DOMAIN' "$domain" '$CUSTOM_DOCROOT')
  51. if [ -n "$domain_docroot" ] && [ -d "$domain_docroot" ]; then
  52. assert_file_exist "${domain_docroot}/${webpath}"
  53. else
  54. assert_file_exist "${HOMEDIR}/${user}/web/${domain}/public_html/${webpath}"
  55. fi
  56. fi
  57. fi
  58. # Test HTTP
  59. run curl --location --silent --show-error --insecure --resolve "${domain}:80:${domain_ip}" "http://${domain}/${webpath}"
  60. assert_success
  61. assert_output --partial "$webproof"
  62. # Test HTTPS
  63. if [ "$SSL" = "yes" ]; then
  64. run v-list-web-domain-ssl $user $domain
  65. assert_success
  66. run curl --location --silent --show-error --insecure --resolve "${domain}:443:${domain_ip}" "https://${domain}/${webpath}"
  67. assert_success
  68. assert_output --partial "$webproof"
  69. fi
  70. }
  71. #----------------------------------------------------------#
  72. # Backup / Restore #
  73. #----------------------------------------------------------#
  74. #Test backup
  75. # Hestia v1.1.1 archive contains:
  76. # user: hestia111
  77. # web:
  78. # - test.hestia.com (+SSL self-signed)
  79. # dns:
  80. # - test.hestia.com
  81. # mail:
  82. # - test.hestia.com
  83. # mail acc:
  84. # - testaccount@test.hestia.com
  85. # db:
  86. # - hestia111_db
  87. # cron:
  88. # - 1: /bin/true
  89. # Hestia 1.7.0 archive contains (As zstd format)
  90. # user: hestia131
  91. # web:
  92. # - test.hestia.com (+SSL self-signed)
  93. # FTP Account
  94. # Awstats enabled
  95. # dns:
  96. # - test.hestia.com
  97. # mail:
  98. # - test.hestia.com
  99. # Ratelimit: 10
  100. # mail acc:
  101. # - testaccount@test.hestia.com
  102. # Alias: info@test.hestiacp.com
  103. # Ratelimit: 20
  104. # - support@test.hestia.com
  105. # db:
  106. # - hestia170_db
  107. # cron:
  108. # - 1: /bin/true
  109. # Vesta 0.9.8-23 archive contains:
  110. # user: vesta09823
  111. # web:
  112. # - vesta09823.tld (+SSL self-signed)
  113. # dns:
  114. # - vesta09823.tld
  115. # mail:
  116. # - vesta09823.tld
  117. # mail acc:
  118. # - testaccount@vesta09823.tld
  119. # db:
  120. # - vesta09823_db
  121. # cron:
  122. # - 1: /bin/true
  123. #
  124. @test "Check if test.hestiacp.com is present" {
  125. assert_file_contains /etc/hosts test.hestia.com
  126. }
  127. # Testing Hestia backups
  128. @test "Restore[1]: Hestia archive for a non-existing user" {
  129. if [ -d "$HOMEDIR/$userbk" ]; then
  130. run v-delete-user $userbk
  131. assert_success
  132. refute_output
  133. fi
  134. mkdir -p /backup
  135. local archive_name="hestia111.2020-03-26"
  136. run wget --quiet --tries=3 --timeout=15 --read-timeout=15 --waitretry=3 --no-dns-cache "https://storage.hestiacp.com/testing/data/${archive_name}.tar" -O "/backup/${archive_name}.tar"
  137. assert_success
  138. run v-restore-user $userbk "${archive_name}.tar"
  139. assert_success
  140. rm "/backup/${archive_name}.tar"
  141. }
  142. @test "Restore[1]: From Hestia [WEB]" {
  143. local domain="test.hestia.com"
  144. validate_web_domain $userbk $domain 'Hello Hestia'
  145. }
  146. @test "Restore[1]: From Hestia [DNS]" {
  147. local domain="test.hestia.com"
  148. run v-list-dns-domain $userbk $domain
  149. assert_success
  150. run nslookup $domain 127.0.0.1
  151. assert_success
  152. }
  153. @test "Restore[1]: From Hestia [MAIL]" {
  154. local domain="test.hestia.com"
  155. run v-list-mail-domain $userbk $domain
  156. assert_success
  157. }
  158. @test "Restore[1]: From Hestia [MAIL-Account]" {
  159. local domain="test.hestia.com"
  160. run v-list-mail-account $userbk $domain testaccount
  161. assert_success
  162. }
  163. @test "Restore[1]: From Hestia [DB]" {
  164. run v-list-database $userbk "${userbk}_db"
  165. assert_success
  166. }
  167. @test "Restore[1]: From Hestia [CRON]" {
  168. run v-list-cron-job $userbk 1
  169. assert_success
  170. }
  171. @test "Restore[1]: From Hestia Cleanup" {
  172. run v-delete-user $userbk
  173. assert_success
  174. refute_output
  175. }
  176. @test "Restore[2]: Hestia archive over a existing user" {
  177. if [ -d "$HOMEDIR/$userbk" ]; then
  178. run v-delete-user $userbk
  179. assert_success
  180. refute_output
  181. fi
  182. if [ ! -d "$HOMEDIR/$userbk" ]; then
  183. run v-add-user $userbk $userbk test@hestia.com
  184. assert_success
  185. fi
  186. mkdir -p /backup
  187. local archive_name="hestia111.2020-03-26"
  188. run wget --quiet --tries=3 --timeout=15 --read-timeout=15 --waitretry=3 --no-dns-cache "https://storage.hestiacp.com/testing/data/${archive_name}.tar" -O "/backup/${archive_name}.tar"
  189. assert_success
  190. run v-restore-user $userbk "${archive_name}.tar"
  191. assert_success
  192. rm "/backup/${archive_name}.tar"
  193. }
  194. @test "Restore[2]: From Hestia [WEB]" {
  195. local domain="test.hestia.com"
  196. validate_web_domain $userbk "${domain}" 'Hello Hestia'
  197. }
  198. @test "Restore[2]: From Hestia [DNS]" {
  199. local domain="test.hestia.com"
  200. run v-list-dns-domain $userbk $domain
  201. assert_success
  202. run nslookup $domain 127.0.0.1
  203. assert_success
  204. }
  205. @test "Restore[2]: From Hestia [MAIL]" {
  206. local domain="test.hestia.com"
  207. run v-list-mail-domain $userbk $domain
  208. assert_success
  209. }
  210. @test "Restore[2]: From Hestia [MAIL-Account]" {
  211. local domain="test.hestia.com"
  212. run v-list-mail-account $userbk $domain testaccount
  213. assert_success
  214. }
  215. @test "Restore[2]: From Hestia [DB]" {
  216. run v-list-database $userbk "${userbk}_db"
  217. assert_success
  218. }
  219. @test "Restore[2]: From Hestia [CRON]" {
  220. run v-list-cron-job $userbk 1
  221. assert_success
  222. }
  223. @test "Restore[2]: From Hestia Cleanup" {
  224. run v-delete-user $userbk
  225. assert_success
  226. refute_output
  227. }
  228. @test "Restore[3]: Hestia (zstd) archive for a non-existing user" {
  229. if [ -d "$HOMEDIR/$userbk" ]; then
  230. run v-delete-user $userbk
  231. assert_success
  232. refute_output
  233. fi
  234. mkdir -p /backup
  235. local archive_name="hestia170.2022-08-23"
  236. run wget --quiet --tries=3 --timeout=15 --read-timeout=15 --waitretry=3 --no-dns-cache "https://storage.hestiacp.com/testing/data/${archive_name}.tar" -O "/backup/${archive_name}.tar"
  237. assert_success
  238. run v-restore-user $userbk "${archive_name}.tar"
  239. assert_success
  240. rm "/backup/${archive_name}.tar"
  241. }
  242. @test "Restore[3]: From Hestia [WEB]" {
  243. local domain="test.hestia.com"
  244. validate_web_domain $userbk $domain 'Hello Hestia'
  245. }
  246. @test "Restore[3]: From Hestia [WEB] FTP" {
  247. local domain="test.hestia.com"
  248. assert_file_contains /etc/passwd "$userbk_test"
  249. assert_file_contains /etc/passwd "/home/$userbk/web/$domain"
  250. }
  251. @test "Restore[3]: From Hestia [WEB] Awstats" {
  252. local domain="test.hestia.com"
  253. assert_file_exist /home/$userbk/conf/web/$domain/awstats.conf
  254. }
  255. @test "Restore[3]: From Hestia [WEB] Custom rule" {
  256. # check if custom rule is still working
  257. local domain="test.hestia.com"
  258. validate_web_domain $userbk $domain 'hestia-yes' '/hestia/hestia' 'no'
  259. }
  260. @test "Restore[3]: From Hestia [DNS]" {
  261. local domain="test.hestia.com"
  262. run v-list-dns-domain $userbk $domain
  263. assert_success
  264. run nslookup $domain 127.0.0.1
  265. assert_success
  266. }
  267. @test "Restore[3]: From Hestia [MAIL]" {
  268. local domain="test.hestia.com"
  269. run v-list-mail-domain $userbk $domain
  270. assert_success
  271. }
  272. @test "Restore[3]: From Hestia [MAIL-Account]" {
  273. local domain="test.hestia.com"
  274. run v-list-mail-account $userbk $domain testaccount
  275. assert_success
  276. # Check if alias is created
  277. assert_file_contains /etc/exim4/domains/$domain/aliases "testaccount@$domain"
  278. # Check if expected rate limits are set
  279. assert_file_contains /etc/exim4/domains/$domain/limits "testaccount@$domain:20"
  280. assert_file_contains /etc/exim4/domains/$domain/limits "support@$domain:10"
  281. }
  282. @test "Restore[3]: From Hestia [DB]" {
  283. run v-list-database $userbk "${userbk}_db"
  284. assert_success
  285. }
  286. @test "Restore[3]: From Hestia [CRON]" {
  287. run v-list-cron-job $userbk 1
  288. assert_success
  289. }
  290. @test "Restore[3]: From Hestia Cleanup" {
  291. run v-delete-user $userbk
  292. assert_success
  293. refute_output
  294. }
  295. @test "Restore[4]: Hestia (zstd) archive for a existing user" {
  296. if [ -d "$HOMEDIR/$userbk" ]; then
  297. run v-delete-user $userbk
  298. assert_success
  299. refute_output
  300. fi
  301. if [ ! -d "$HOMEDIR/$userbk" ]; then
  302. run v-add-user $userbk $userbk test@hestia.com
  303. assert_success
  304. fi
  305. mkdir -p /backup
  306. local archive_name="hestia170.2022-08-23"
  307. run wget --quiet --tries=3 --timeout=15 --read-timeout=15 --waitretry=3 --no-dns-cache "https://storage.hestiacp.com/testing/data/${archive_name}.tar" -O "/backup/${archive_name}.tar"
  308. assert_success
  309. run v-restore-user $userbk "${archive_name}.tar"
  310. assert_success
  311. rm "/backup/${archive_name}.tar"
  312. }
  313. @test "Restore[4]: From Hestia [WEB]" {
  314. local domain="test.hestia.com"
  315. validate_web_domain $userbk $domain 'Hello Hestia'
  316. }
  317. @test "Restore[4]: From Hestia [WEB] FTP" {
  318. local domain="test.hestia.com"
  319. assert_file_contains /etc/passwd "$userbk_test"
  320. assert_file_contains /etc/passwd "/home/$userbk/web/$domain"
  321. }
  322. @test "Restore[4]: From Hestia [WEB] Awstats" {
  323. local domain="test.hestia.com"
  324. assert_file_exist /home/$userbk/conf/web/$domain/awstats.conf
  325. }
  326. @test "Restore[4]: From Hestia [WEB] Custom rule" {
  327. # check if custom rule is still working
  328. local domain="test.hestia.com"
  329. validate_web_domain $userbk $domain 'hestia-yes' '/hestia/hestia' 'no'
  330. }
  331. @test "Restore[4]: From Hestia [DNS]" {
  332. local domain="test.hestia.com"
  333. run v-list-dns-domain $userbk $domain
  334. assert_success
  335. run nslookup $domain 127.0.0.1
  336. assert_success
  337. }
  338. @test "Restore[4]: From Hestia [MAIL]" {
  339. local domain="test.hestia.com"
  340. run v-list-mail-domain $userbk $domain
  341. assert_success
  342. }
  343. @test "Restore[4]: From Hestia [MAIL-Account]" {
  344. local domain="test.hestia.com"
  345. run v-list-mail-account $userbk $domain testaccount
  346. assert_success
  347. # Check if alias is created
  348. assert_file_contains /etc/exim4/domains/$domain/aliases "testaccount@$domain"
  349. # Check if expected rate limits are set
  350. assert_file_contains /etc/exim4/domains/$domain/limits "testaccount@$domain:20"
  351. assert_file_contains /etc/exim4/domains/$domain/limits "support@$domain:10"
  352. }
  353. @test "Restore[4]: From Hestia [DB]" {
  354. run v-list-database $userbk "${userbk}_db"
  355. assert_success
  356. }
  357. @test "Restore[4]: From Hestia [CRON]" {
  358. run v-list-cron-job $userbk 1
  359. assert_success
  360. }
  361. @test "Restore[4]: From Hestia Cleanup" {
  362. run v-delete-user $userbk
  363. assert_success
  364. refute_output
  365. }
  366. # Testing Vesta Backups
  367. @test "Restore[1]: Vesta archive for a non-existing user" {
  368. if [ -d "$HOMEDIR/$userbk" ]; then
  369. run v-delete-user $userbk
  370. assert_success
  371. refute_output
  372. fi
  373. mkdir -p /backup
  374. local archive_name="vesta09823.2018-10-18"
  375. run wget --quiet --tries=3 --timeout=15 --read-timeout=15 --waitretry=3 --no-dns-cache "https://storage.hestiacp.com/testing/data/${archive_name}.tar" -O "/backup/${archive_name}.tar"
  376. assert_success
  377. run v-restore-user $userbk "${archive_name}.tar"
  378. assert_success
  379. rm "/backup/${archive_name}.tar"
  380. }
  381. @test "Restore[1]: From Vesta [WEB]" {
  382. local domain="vesta09823.tld"
  383. validate_web_domain $userbk $domain 'Hello Vesta'
  384. }
  385. @test "Restore[1]: From Vesta [DNS]" {
  386. local domain="vesta09823.tld"
  387. run v-list-dns-domain $userbk $domain
  388. assert_success
  389. run nslookup $domain 127.0.0.1
  390. assert_success
  391. }
  392. @test "Restore[1]: From Vesta [MAIL]" {
  393. local domain="vesta09823.tld"
  394. run v-list-mail-domain $userbk $domain
  395. assert_success
  396. }
  397. @test "Restore[1]: From Vesta [MAIL-Account]" {
  398. local domain="vesta09823.tld"
  399. run v-list-mail-account $userbk $domain testaccount
  400. assert_success
  401. }
  402. @test "Restore[1]: From Vesta [DB]" {
  403. run v-list-database $userbk "${userbk}_db"
  404. assert_success
  405. }
  406. @test "Restore[1]: From Vesta [CRON]" {
  407. run v-list-cron-job $userbk 1
  408. assert_success
  409. }
  410. @test "Restore[1]: From Vesta Cleanup" {
  411. run v-delete-user $userbk
  412. assert_success
  413. refute_output
  414. }
  415. @test "Restore[2]: Vesta archive over a existing user" {
  416. if [ -d "$HOMEDIR/$userbk" ]; then
  417. run v-delete-user $userbk
  418. assert_success
  419. refute_output
  420. fi
  421. if [ ! -d "$HOMEDIR/$userbk" ]; then
  422. run v-add-user $userbk $userbk test@hestia.com
  423. assert_success
  424. fi
  425. mkdir -p /backup
  426. local archive_name="vesta09823.2018-10-18"
  427. run wget --quiet --tries=3 --timeout=15 --read-timeout=15 --waitretry=3 --no-dns-cache "https://storage.hestiacp.com/testing/data/${archive_name}.tar" -O "/backup/${archive_name}.tar"
  428. assert_success
  429. run v-restore-user $userbk "${archive_name}.tar"
  430. assert_success
  431. rm "/backup/${archive_name}.tar"
  432. }
  433. @test "Restore[2]: From Vesta [WEB]" {
  434. local domain="vesta09823.tld"
  435. validate_web_domain $userbk "${domain}" 'Hello Vesta'
  436. }
  437. @test "Restore[2]: From Vesta [DNS]" {
  438. local domain="vesta09823.tld"
  439. run v-list-dns-domain $userbk $domain
  440. assert_success
  441. run nslookup $domain 127.0.0.1
  442. assert_success
  443. }
  444. @test "Restore[2]: From Vesta [MAIL]" {
  445. local domain="vesta09823.tld"
  446. run v-list-mail-domain $userbk $domain
  447. assert_success
  448. }
  449. @test "Restore[2]: From Vesta [MAIL-Account]" {
  450. local domain="vesta09823.tld"
  451. run v-list-mail-account $userbk $domain testaccount
  452. assert_success
  453. }
  454. @test "Restore[2]: From Vesta [DB]" {
  455. run v-list-database $userbk "${userbk}_db"
  456. assert_success
  457. }
  458. @test "Restore[2]: From Vesta [CRON]" {
  459. run v-list-cron-job $userbk 1
  460. assert_success
  461. }
  462. @test "Restore[2]: From Vesta Cleanup" {
  463. run v-delete-user $userbk
  464. assert_success
  465. refute_output
  466. }