restore.bats 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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. # Testing Hestia backups
  125. @test "Restore[1]: Hestia archive for a non-existing user" {
  126. if [ -d "$HOMEDIR/$userbk" ]; then
  127. run v-delete-user $userbk
  128. assert_success
  129. refute_output
  130. fi
  131. mkdir -p /backup
  132. local archive_name="hestia111.2020-03-26"
  133. 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"
  134. assert_success
  135. run v-restore-user $userbk "${archive_name}.tar"
  136. assert_success
  137. rm "/backup/${archive_name}.tar"
  138. }
  139. @test "Restore[1]: From Hestia [WEB]" {
  140. local domain="test.hestia.com"
  141. validate_web_domain $userbk $domain 'Hello Hestia'
  142. }
  143. @test "Restore[1]: From Hestia [DNS]" {
  144. local domain="test.hestia.com"
  145. run v-list-dns-domain $userbk $domain
  146. assert_success
  147. run nslookup $domain 127.0.0.1
  148. assert_success
  149. }
  150. @test "Restore[1]: From Hestia [MAIL]" {
  151. local domain="test.hestia.com"
  152. run v-list-mail-domain $userbk $domain
  153. assert_success
  154. }
  155. @test "Restore[1]: From Hestia [MAIL-Account]" {
  156. local domain="test.hestia.com"
  157. run v-list-mail-account $userbk $domain testaccount
  158. assert_success
  159. }
  160. @test "Restore[1]: From Hestia [DB]" {
  161. run v-list-database $userbk "${userbk}_db"
  162. assert_success
  163. }
  164. @test "Restore[1]: From Hestia [CRON]" {
  165. run v-list-cron-job $userbk 1
  166. assert_success
  167. }
  168. @test "Restore[1]: From Hestia Cleanup" {
  169. run v-delete-user $userbk
  170. assert_success
  171. refute_output
  172. }
  173. @test "Restore[2]: Hestia archive over a existing user" {
  174. if [ -d "$HOMEDIR/$userbk" ]; then
  175. run v-delete-user $userbk
  176. assert_success
  177. refute_output
  178. fi
  179. if [ ! -d "$HOMEDIR/$userbk" ]; then
  180. run v-add-user $userbk $userbk test@hestia.com
  181. assert_success
  182. fi
  183. mkdir -p /backup
  184. local archive_name="hestia111.2020-03-26"
  185. 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"
  186. assert_success
  187. run v-restore-user $userbk "${archive_name}.tar"
  188. assert_success
  189. rm "/backup/${archive_name}.tar"
  190. }
  191. @test "Restore[2]: From Hestia [WEB]" {
  192. local domain="test.hestia.com"
  193. validate_web_domain $userbk "${domain}" 'Hello Hestia'
  194. }
  195. @test "Restore[2]: From Hestia [DNS]" {
  196. local domain="test.hestia.com"
  197. run v-list-dns-domain $userbk $domain
  198. assert_success
  199. run nslookup $domain 127.0.0.1
  200. assert_success
  201. }
  202. @test "Restore[2]: From Hestia [MAIL]" {
  203. local domain="test.hestia.com"
  204. run v-list-mail-domain $userbk $domain
  205. assert_success
  206. }
  207. @test "Restore[2]: From Hestia [MAIL-Account]" {
  208. local domain="test.hestia.com"
  209. run v-list-mail-account $userbk $domain testaccount
  210. assert_success
  211. }
  212. @test "Restore[2]: From Hestia [DB]" {
  213. run v-list-database $userbk "${userbk}_db"
  214. assert_success
  215. }
  216. @test "Restore[2]: From Hestia [CRON]" {
  217. run v-list-cron-job $userbk 1
  218. assert_success
  219. }
  220. @test "Restore[2]: From Hestia Cleanup" {
  221. run v-delete-user $userbk
  222. assert_success
  223. refute_output
  224. }
  225. @test "Restore[3]: Hestia (zstd) archive for a non-existing user" {
  226. if [ -d "$HOMEDIR/$userbk" ]; then
  227. run v-delete-user $userbk
  228. assert_success
  229. refute_output
  230. fi
  231. mkdir -p /backup
  232. local archive_name="hestia170.2022-08-23"
  233. 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"
  234. assert_success
  235. run v-restore-user $userbk "${archive_name}.tar"
  236. assert_success
  237. rm "/backup/${archive_name}.tar"
  238. }
  239. @test "Restore[3]: From Hestia [WEB]" {
  240. local domain="test.hestia.com"
  241. validate_web_domain $userbk $domain 'Hello Hestia'
  242. }
  243. @test "Restore[3]: From Hestia [WEB] FTP" {
  244. local domain="test.hestia.com"
  245. assert_file_contains /etc/passwd "$userbk_test"
  246. assert_file_contains /etc/passwd "/home/$userbk/web/$domain"
  247. }
  248. @test "Restore[3]: From Hestia [WEB] Awstats" {
  249. local domain="test.hestia.com"
  250. assert_file_exist /home/$userbk/conf/web/$domain/awstats.conf
  251. }
  252. @test "Restore[3]: From Hestia [WEB] Custom rule" {
  253. # check if custom rule is still working
  254. local domain="test.hestia.com"
  255. validate_web_domain $userbk $domain 'hestia-yes' '/hestia/hestia' 'no'
  256. }
  257. @test "Restore[3]: From Hestia [DNS]" {
  258. local domain="test.hestia.com"
  259. run v-list-dns-domain $userbk $domain
  260. assert_success
  261. run nslookup $domain 127.0.0.1
  262. assert_success
  263. }
  264. @test "Restore[3]: From Hestia [MAIL]" {
  265. local domain="test.hestia.com"
  266. run v-list-mail-domain $userbk $domain
  267. assert_success
  268. }
  269. @test "Restore[3]: From Hestia [MAIL-Account]" {
  270. local domain="test.hestia.com"
  271. run v-list-mail-account $userbk $domain testaccount
  272. assert_success
  273. # Check if alias is created
  274. assert_file_contains /etc/exim4/domains/$domain/aliases "testaccount@$domain"
  275. # Check if expected rate limits are set
  276. assert_file_contains /etc/exim4/domains/$domain/limits "testaccount@$domain:20"
  277. assert_file_contains /etc/exim4/domains/$domain/limits "support@$domain:10"
  278. }
  279. @test "Restore[3]: From Hestia [DB]" {
  280. run v-list-database $userbk "${userbk}_db"
  281. assert_success
  282. }
  283. @test "Restore[3]: From Hestia [CRON]" {
  284. run v-list-cron-job $userbk 1
  285. assert_success
  286. }
  287. @test "Restore[3]: From Hestia Cleanup" {
  288. run v-delete-user $userbk
  289. assert_success
  290. refute_output
  291. }
  292. @test "Restore[4]: Hestia (zstd) archive for a existing user" {
  293. if [ -d "$HOMEDIR/$userbk" ]; then
  294. run v-delete-user $userbk
  295. assert_success
  296. refute_output
  297. fi
  298. if [ ! -d "$HOMEDIR/$userbk" ]; then
  299. run v-add-user $userbk $userbk test@hestia.com
  300. assert_success
  301. fi
  302. mkdir -p /backup
  303. local archive_name="hestia170.2022-08-23"
  304. 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"
  305. assert_success
  306. run v-restore-user $userbk "${archive_name}.tar"
  307. assert_success
  308. rm "/backup/${archive_name}.tar"
  309. }
  310. @test "Restore[4]: From Hestia [WEB]" {
  311. local domain="test.hestia.com"
  312. validate_web_domain $userbk $domain 'Hello Hestia'
  313. }
  314. @test "Restore[4]: From Hestia [WEB] FTP" {
  315. local domain="test.hestia.com"
  316. assert_file_contains /etc/passwd "$userbk_test"
  317. assert_file_contains /etc/passwd "/home/$userbk/web/$domain"
  318. }
  319. @test "Restore[4]: From Hestia [WEB] Awstats" {
  320. local domain="test.hestia.com"
  321. assert_file_exist /home/$userbk/conf/web/$domain/awstats.conf
  322. }
  323. @test "Restore[4]: From Hestia [WEB] Custom rule" {
  324. # check if custom rule is still working
  325. local domain="test.hestia.com"
  326. validate_web_domain $userbk $domain 'hestia-yes' '/hestia/hestia' 'no'
  327. }
  328. @test "Restore[4]: From Hestia [DNS]" {
  329. local domain="test.hestia.com"
  330. run v-list-dns-domain $userbk $domain
  331. assert_success
  332. run nslookup $domain 127.0.0.1
  333. assert_success
  334. }
  335. @test "Restore[4]: From Hestia [MAIL]" {
  336. local domain="test.hestia.com"
  337. run v-list-mail-domain $userbk $domain
  338. assert_success
  339. }
  340. @test "Restore[4]: From Hestia [MAIL-Account]" {
  341. local domain="test.hestia.com"
  342. run v-list-mail-account $userbk $domain testaccount
  343. assert_success
  344. # Check if alias is created
  345. assert_file_contains /etc/exim4/domains/$domain/aliases "testaccount@$domain"
  346. # Check if expected rate limits are set
  347. assert_file_contains /etc/exim4/domains/$domain/limits "testaccount@$domain:20"
  348. assert_file_contains /etc/exim4/domains/$domain/limits "support@$domain:10"
  349. }
  350. @test "Restore[4]: From Hestia [DB]" {
  351. run v-list-database $userbk "${userbk}_db"
  352. assert_success
  353. }
  354. @test "Restore[4]: From Hestia [CRON]" {
  355. run v-list-cron-job $userbk 1
  356. assert_success
  357. }
  358. @test "Restore[4]: From Hestia Cleanup" {
  359. run v-delete-user $userbk
  360. assert_success
  361. refute_output
  362. }
  363. # Testing Vesta Backups
  364. @test "Restore[1]: Vesta archive for a non-existing user" {
  365. if [ -d "$HOMEDIR/$userbk" ]; then
  366. run v-delete-user $userbk
  367. assert_success
  368. refute_output
  369. fi
  370. mkdir -p /backup
  371. local archive_name="vesta09823.2018-10-18"
  372. 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"
  373. assert_success
  374. run v-restore-user $userbk "${archive_name}.tar"
  375. assert_success
  376. rm "/backup/${archive_name}.tar"
  377. }
  378. @test "Restore[1]: From Vesta [WEB]" {
  379. local domain="vesta09823.tld"
  380. validate_web_domain $userbk $domain 'Hello Vesta'
  381. }
  382. @test "Restore[1]: From Vesta [DNS]" {
  383. local domain="vesta09823.tld"
  384. run v-list-dns-domain $userbk $domain
  385. assert_success
  386. run nslookup $domain 127.0.0.1
  387. assert_success
  388. }
  389. @test "Restore[1]: From Vesta [MAIL]" {
  390. local domain="vesta09823.tld"
  391. run v-list-mail-domain $userbk $domain
  392. assert_success
  393. }
  394. @test "Restore[1]: From Vesta [MAIL-Account]" {
  395. local domain="vesta09823.tld"
  396. run v-list-mail-account $userbk $domain testaccount
  397. assert_success
  398. }
  399. @test "Restore[1]: From Vesta [DB]" {
  400. run v-list-database $userbk "${userbk}_db"
  401. assert_success
  402. }
  403. @test "Restore[1]: From Vesta [CRON]" {
  404. run v-list-cron-job $userbk 1
  405. assert_success
  406. }
  407. @test "Restore[1]: From Vesta Cleanup" {
  408. run v-delete-user $userbk
  409. assert_success
  410. refute_output
  411. }
  412. @test "Restore[2]: Vesta archive over a existing user" {
  413. if [ -d "$HOMEDIR/$userbk" ]; then
  414. run v-delete-user $userbk
  415. assert_success
  416. refute_output
  417. fi
  418. if [ ! -d "$HOMEDIR/$userbk" ]; then
  419. run v-add-user $userbk $userbk test@hestia.com
  420. assert_success
  421. fi
  422. mkdir -p /backup
  423. local archive_name="vesta09823.2018-10-18"
  424. 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"
  425. assert_success
  426. run v-restore-user $userbk "${archive_name}.tar"
  427. assert_success
  428. rm "/backup/${archive_name}.tar"
  429. }
  430. @test "Restore[2]: From Vesta [WEB]" {
  431. local domain="vesta09823.tld"
  432. validate_web_domain $userbk "${domain}" 'Hello Vesta'
  433. }
  434. @test "Restore[2]: From Vesta [DNS]" {
  435. local domain="vesta09823.tld"
  436. run v-list-dns-domain $userbk $domain
  437. assert_success
  438. run nslookup $domain 127.0.0.1
  439. assert_success
  440. }
  441. @test "Restore[2]: From Vesta [MAIL]" {
  442. local domain="vesta09823.tld"
  443. run v-list-mail-domain $userbk $domain
  444. assert_success
  445. }
  446. @test "Restore[2]: From Vesta [MAIL-Account]" {
  447. local domain="vesta09823.tld"
  448. run v-list-mail-account $userbk $domain testaccount
  449. assert_success
  450. }
  451. @test "Restore[2]: From Vesta [DB]" {
  452. run v-list-database $userbk "${userbk}_db"
  453. assert_success
  454. }
  455. @test "Restore[2]: From Vesta [CRON]" {
  456. run v-list-cron-job $userbk 1
  457. assert_success
  458. }
  459. @test "Restore[2]: From Vesta Cleanup" {
  460. run v-delete-user $userbk
  461. assert_success
  462. refute_output
  463. }