You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1279 lines
21 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. #!/usr/bin/env bash-shlib
  2. # -*- mode: shell-script -*-
  3. include shunit
  4. depends sed grep git mkdir readlink
  5. export -f matches
  6. export grep
  7. tmp=/tmp
  8. tprog="../bin/compose-core"
  9. tprog=$(readlink -f $tprog)
  10. export PATH=".:$PATH"
  11. short_tprog=$(basename "$tprog")
  12. ##
  13. ## Convenience function
  14. ##
  15. init_test() {
  16. test_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
  17. cd "$test_tmpdir"
  18. export CACHEDIR="$test_tmpdir/.cache"
  19. export VARDIR="$test_tmpdir/.var"
  20. export COMPOSE_DISABLE_DOCKER_COMPOSE_STORE="1"
  21. mkdir -p "$CACHEDIR"
  22. }
  23. tear_test() {
  24. rm -rf "$test_tmpdir"
  25. }
  26. ##
  27. ## Tests
  28. ##
  29. ##
  30. # Checking arguments
  31. test_calling_sourcing() {
  32. assert_list <<EOF
  33. ### Calling and sourcing
  34. ## -- call of '$short_tprog' with no arg fails (errlvl != 0)
  35. ! "$tprog"
  36. ## -- source '$short_tprog' should not fail
  37. . "$tprog"
  38. EOF
  39. }
  40. test_mixin_functions() {
  41. init_test
  42. assert_list <<EOF
  43. ### Testing get_docker_compose_mixin_from_metadata
  44. ## -- Empty metadata.yml
  45. export CHARM_STORE=$test_tmpdir
  46. mkdir $test_tmpdir/testcharm
  47. cat <<EOF2 > $test_tmpdir/testcharm/metadata.yml
  48. EOF2
  49. . "$tprog"
  50. _setup_state_dir
  51. out="\$(get_docker_compose_mixin_from_metadata testcharm)"
  52. expected='\
  53. labels:
  54. - compose.charm=testcharm'
  55. [ "\$out" == "\$expected" ] || {
  56. echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
  57. exit 1
  58. }
  59. ## -- volumes
  60. export CHARM_STORE=$test_tmpdir
  61. export CONFIGSTORE=/tmp/CONFIG
  62. export DATASTORE=/tmp/DATA
  63. mkdir -p $test_tmpdir/testcharm
  64. cat <<EOF2 > $test_tmpdir/testcharm/metadata.yml
  65. data-resources:
  66. - /a
  67. config-resources:
  68. - /b
  69. host-resources:
  70. - /tmp:/tmp
  71. EOF2
  72. . "$tprog"
  73. _setup_state_dir
  74. out=\$(get_docker_compose_mixin_from_metadata testcharm) || exit 1
  75. expected="\
  76. labels:
  77. - compose.charm=testcharm
  78. volumes:
  79. - /tmp/DATA/testcharm/a:/a:rw
  80. - /tmp/CONFIG/testcharm/b:/b:rw
  81. - /tmp:/tmp:rw"
  82. [ "\$out" == "\$expected" ] || {
  83. echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
  84. exit 1
  85. }
  86. ## -- docker-compose
  87. export CHARM_STORE=$test_tmpdir
  88. mkdir -p $test_tmpdir/testcharm
  89. cat <<EOF2 > $test_tmpdir/testcharm/metadata.yml
  90. docker-compose:
  91. volumes:
  92. - /any:/vol
  93. entrypoint: any
  94. EOF2
  95. . "$tprog"
  96. _setup_state_dir
  97. out="\$(get_docker_compose_mixin_from_metadata testcharm)" || exit 1
  98. expected="\
  99. entrypoint: any
  100. labels:
  101. - compose.charm=testcharm
  102. volumes:
  103. - /any:/vol"
  104. [ "\$out" == "\$expected" ] || {
  105. echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
  106. exit 1
  107. }
  108. ## -- image
  109. export CHARM_STORE=$test_tmpdir
  110. mkdir -p $test_tmpdir/testcharm
  111. cat <<EOF2 > $test_tmpdir/testcharm/metadata.yml
  112. docker-image: toto
  113. EOF2
  114. . "$tprog"
  115. _setup_state_dir
  116. out="\$(get_docker_compose_mixin_from_metadata testcharm)" || exit 1
  117. expected="\
  118. image: toto
  119. labels:
  120. - compose.charm=testcharm"
  121. [ "\$out" == "\$expected" ] || {
  122. echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
  123. exit 1
  124. }
  125. ## -- build
  126. export CHARM_STORE=$test_tmpdir
  127. mkdir -p $test_tmpdir/testcharm/build
  128. cat <<EOF2 > $test_tmpdir/testcharm/metadata.yml
  129. # XXX new content to invalidate cache
  130. EOF2
  131. . "$tprog"
  132. _setup_state_dir
  133. out="\$(get_docker_compose_mixin_from_metadata testcharm)" || exit 1
  134. expected="\
  135. build: $test_tmpdir/testcharm/build
  136. labels:
  137. - compose.charm=testcharm"
  138. [ "\$out" == "\$expected" ] || {
  139. echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
  140. exit 1
  141. }
  142. ## -- subordinate with image
  143. export CHARM_STORE=$test_tmpdir
  144. mkdir -p $test_tmpdir/testcharm
  145. cat <<EOF2 > $test_tmpdir/testcharm/metadata.yml
  146. subordinate: true
  147. docker-image: toto
  148. EOF2
  149. . "$tprog"
  150. _setup_state_dir
  151. ! get_docker_compose_mixin_from_metadata testcharm
  152. ## -- subordinate with build subdir
  153. export CHARM_STORE=$test_tmpdir
  154. mkdir -p $test_tmpdir/testcharm/build
  155. cat <<EOF2 > $test_tmpdir/testcharm/metadata.yml
  156. subordinate: true
  157. EOF2
  158. . "$tprog"
  159. _setup_state_dir
  160. ! get_docker_compose_mixin_from_metadata testcharm
  161. EOF
  162. tear_test
  163. }
  164. function test_merge_yaml {
  165. init_test
  166. assert_list <<EOF
  167. ### Testing merge_yaml
  168. ## -- basic example
  169. . "$tprog"
  170. _setup_state_dir
  171. test "\$(merge_yaml <(echo "
  172. a:
  173. b: 1
  174. c:
  175. - d
  176. - e
  177. ") <(echo "
  178. a:
  179. y: 3
  180. b: 2
  181. c:
  182. - new
  183. x:
  184. 1
  185. "))" == "a:
  186. b: 2
  187. c:
  188. - d
  189. - e
  190. - new
  191. y: 3
  192. x: 1"
  193. ## -- Nothing
  194. . "$tprog"
  195. _setup_state_dir
  196. test -z "\$(merge_yaml <(echo) <(echo))"
  197. ## -- No variable expansions
  198. . "$tprog"
  199. _setup_state_dir
  200. out=\$(merge_yaml <(echo '- \$\$') <(echo "- a"))
  201. test "\$out" == '- \$\$
  202. - a' || {
  203. echo -e "** merge_yaml:\n\$out"; exit 1
  204. }
  205. EOF
  206. }
  207. function test_merge_yaml_str {
  208. init_test
  209. assert_list <<EOF
  210. ### Testing merge_yaml_str
  211. ## -- basic example
  212. . "$tprog"
  213. _setup_state_dir
  214. test "\$(merge_yaml_str "
  215. a:
  216. b: 1
  217. c:
  218. - d
  219. - e
  220. " "
  221. a:
  222. y: 3
  223. b: 2
  224. c:
  225. - new
  226. x:
  227. 1
  228. ")" == "a:
  229. b: 2
  230. c:
  231. - d
  232. - e
  233. - new
  234. y: 3
  235. x: 1"
  236. ## -- Nothing
  237. . "$tprog"
  238. _setup_state_dir
  239. test -z "\$(merge_yaml_str "" "")"
  240. ## -- No variable expansions
  241. . "$tprog"
  242. _setup_state_dir
  243. out=\$(merge_yaml_str '- \$\$' "- a")
  244. test "\$out" == '- \$\$
  245. - a' || {
  246. echo -e "** merge_yaml_str:\n\$out"; exit 1
  247. }
  248. EOF
  249. }
  250. function test_merge_yaml_str {
  251. init_test
  252. assert_list <<EOF
  253. ### Testing merge_yaml_str
  254. ## -- basic example
  255. . "$tprog"
  256. _setup_state_dir
  257. test "\$(merge_yaml_str "
  258. a:
  259. b: 1
  260. c:
  261. - d
  262. - e
  263. " "
  264. a:
  265. y: 3
  266. b: 2
  267. c:
  268. - new
  269. x:
  270. 1
  271. ")" == "a:
  272. b: 2
  273. c:
  274. - d
  275. - e
  276. - new
  277. y: 3
  278. x: 1"
  279. ## -- Nothing
  280. . "$tprog"
  281. _setup_state_dir
  282. test -z "\$(merge_yaml_str "" "")"
  283. ## -- No variable expansions
  284. . "$tprog"
  285. _setup_state_dir
  286. out=\$(merge_yaml_str '- \$\$' "- a")
  287. test "\$out" == '- \$\$
  288. - a' || {
  289. echo -e "** merge_yaml_str:\n\$out"; exit 1
  290. }
  291. EOF
  292. }
  293. function test_yaml_key_val_str {
  294. init_test
  295. assert_list <<EOF
  296. ### Testing yaml_key_val_str
  297. ## -- basic example
  298. . "$tprog"
  299. _setup_state_dir
  300. out="\$(yaml_key_val_str "a" "data: |
  301. hello
  302. multi
  303. line
  304. b:
  305. x: 1
  306. y: 2
  307. ")"
  308. test "\$out" == "a:
  309. data: 'hello
  310. multi
  311. line
  312. '
  313. b:
  314. x: 1
  315. y: 2" || {
  316. echo -e "** yaml_key_val_str:\n\$out"
  317. exit 1
  318. }
  319. EOF
  320. }
  321. test_get_compose_service_def() {
  322. init_test
  323. assert_list <<EOF
  324. ### Testing get_compose_service_def
  325. ## -- Simple (no docker-compose)
  326. export CHARM_STORE=$test_tmpdir
  327. mkdir $test_tmpdir/www
  328. touch $test_tmpdir/www/metadata.yml
  329. . "$tprog"
  330. _setup_state_dir
  331. out="\$(get_compose_service_def www)"
  332. test "\$out" == "charm: www" || {
  333. echo OUTPUT:
  334. echo "\$out"
  335. false
  336. }
  337. ## -- Simple (no docker-compose, no charm dir)
  338. export CHARM_STORE=$test_tmpdir
  339. . "$tprog"
  340. _setup_state_dir
  341. ! get_compose_service_def www_not_existent
  342. ## -- Simple (compose is self sufficient)
  343. export CHARM_STORE=$test_tmpdir
  344. mkdir -p $test_tmpdir/www
  345. cat <<EOF2 > $test_tmpdir/compose.yml
  346. toto:
  347. charm: www
  348. blabla: xxx
  349. EOF2
  350. . "$tprog"
  351. _setup_state_dir
  352. COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  353. out="\$(get_compose_service_def toto)"
  354. test "\$out" == "charm: www
  355. blabla: xxx" || {
  356. echo -e "** get_compose_service_def toto:\n\$out"
  357. exit 1
  358. }
  359. ## -- Addition of default charm
  360. export CHARM_STORE=$test_tmpdir
  361. mkdir -p $test_tmpdir/www
  362. cat <<EOF2 > $test_tmpdir/compose.yml
  363. www:
  364. blabla: xxx
  365. EOF2
  366. . "$tprog"
  367. _setup_state_dir
  368. COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  369. test "\$(get_compose_service_def www)" == "blabla: xxx
  370. charm: www"
  371. EOF
  372. }
  373. ##
  374. ##
  375. ##
  376. function test_get_master_service_for_service() {
  377. init_test
  378. assert_list <<EOF
  379. ### Testing get_master_service_for_service
  380. ## -- Simple (no subordinate)
  381. export CHARM_STORE=$test_tmpdir
  382. mkdir $test_tmpdir/www
  383. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  384. EOF2
  385. . "$tprog"
  386. _setup_state_dir
  387. test "\$(get_master_service_for_service www)" == "www"
  388. ## -- subordinate
  389. export CHARM_STORE=$test_tmpdir
  390. mkdir -p $test_tmpdir/{www,mysql}
  391. touch $test_tmpdir/mysql/metadata.yml
  392. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  393. subordinate: true
  394. requires:
  395. a-label-for-relation:
  396. interface: a-name-relation
  397. scope: container
  398. EOF2
  399. cat <<EOF2 > $test_tmpdir/compose.yml
  400. www:
  401. charm: www
  402. relations:
  403. a-name-relation:
  404. mysql:
  405. label: value
  406. EOF2
  407. . "$tprog"
  408. _setup_state_dir
  409. COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  410. test "\$(get_master_service_for_service www)" == "mysql"
  411. EOF
  412. }
  413. ##
  414. ##
  415. ##
  416. function test_get_docker_compose_service_mixin() {
  417. init_test
  418. assert_list <<EOF
  419. ### Testing get_docker_compose_service_mixin
  420. ## -- Simple (no compose, no subordinate)
  421. export CHARM_STORE=$test_tmpdir
  422. mkdir $test_tmpdir/{www,mysql}
  423. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  424. data-resources:
  425. - /tmp/a
  426. config-resources:
  427. - /tmp/b
  428. EOF2
  429. . "$tprog"
  430. _setup_state_dir
  431. out=\$(_get_docker_compose_service_mixin www | shyaml get-value www.volumes)
  432. [[ "\$out" == "\
  433. - /www/tmp/a:/tmp/a:rw
  434. - /www/tmp/b:/tmp/b:rw" ]] || {
  435. echo -e "** _get_docker_compose_service_mixin www:\n\$out"; exit 1
  436. }
  437. ## -- Simple (compose, but no subordinate)
  438. export CHARM_STORE=$test_tmpdir
  439. mkdir -p $test_tmpdir/{www,mysql}
  440. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  441. data-resources:
  442. - /tmp/a
  443. config-resources:
  444. - /tmp/b
  445. EOF2
  446. touch $test_tmpdir/mysql/metadata.yml
  447. cat <<EOF2 > $test_tmpdir/compose.yml
  448. www:
  449. charm: www
  450. relations:
  451. a-name-relation:
  452. mysql:
  453. label: value
  454. EOF2
  455. . "$tprog"
  456. _setup_state_dir
  457. COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  458. out="\$(_get_docker_compose_service_mixin www)" || exit 1
  459. [ "\$out" == "www:
  460. labels:
  461. - compose.service=www
  462. - compose.master-service=www
  463. - compose.project=\$(basename "$test_tmpdir")
  464. - compose.charm=www
  465. links:
  466. - mysql
  467. volumes:
  468. - /www/tmp/a:/tmp/a:rw
  469. - /www/tmp/b:/tmp/b:rw" ] || {
  470. echo -e "OUT:\n\$out"
  471. exit 1
  472. }
  473. ## -- compose, subordinate
  474. export CHARM_STORE=$test_tmpdir
  475. mkdir -p $test_tmpdir/{www,mysql}
  476. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  477. subordinate: true
  478. data-resources:
  479. - /tmp/a
  480. config-resources:
  481. - /tmp/b
  482. requires:
  483. a-name-relation:
  484. interface: a-name-relation
  485. scope: container
  486. EOF2
  487. cat <<EOF2 > $test_tmpdir/compose.yml
  488. www:
  489. charm: www
  490. relations:
  491. a-name-relation:
  492. mysql:
  493. label: value
  494. EOF2
  495. . "$tprog"
  496. _setup_state_dir
  497. COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  498. out="\$(_get_docker_compose_service_mixin www)" || exit 1
  499. expected="mysql:
  500. labels:
  501. - compose.service=www
  502. - compose.master-service=mysql
  503. - compose.project=$(basename "$test_tmpdir")
  504. - compose.charm=www
  505. volumes:
  506. - /www/tmp/a:/tmp/a:rw
  507. - /www/tmp/b:/tmp/b:rw"
  508. [ "\$out" == "\$expected" ] || {
  509. echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
  510. exit 1
  511. }
  512. EOF
  513. }
  514. function test_get_docker_compose {
  515. init_test
  516. assert_list <<EOF
  517. ### Testing get_docker_compose
  518. ## -- no docker-compose
  519. export CHARM_STORE=$test_tmpdir
  520. mkdir $test_tmpdir/{www,mysql}
  521. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  522. data-resources:
  523. - /tmp/a
  524. config-resources:
  525. - /tmp/b
  526. EOF2
  527. . "$tprog"
  528. _setup_state_dir
  529. out=\$(get_docker_compose www)
  530. echo "OUT:"
  531. echo "\$out"
  532. out=\$(echo "\$out" | shyaml get-value services.www.volumes)
  533. echo "OUT volumes:"
  534. echo "\$out"
  535. test "\$out" == "\\
  536. - /www/tmp/a:/tmp/a:rw
  537. - /www/tmp/b:/tmp/b:rw"
  538. ## -- simple with docker-compose
  539. export CHARM_STORE=$test_tmpdir
  540. mkdir -p $test_tmpdir/{www,mysql}
  541. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  542. data-resources:
  543. - /tmp/a
  544. config-resources:
  545. - /tmp/b
  546. EOF2
  547. cat <<EOF2 > $test_tmpdir/mysql/metadata.yml
  548. data-resources:
  549. - /tmp/c
  550. config-resources:
  551. - /tmp/d
  552. EOF2
  553. cat <<EOF2 > $test_tmpdir/compose.yml
  554. web_site:
  555. charm: www
  556. relations:
  557. db-connection:
  558. mysql:
  559. user: toto
  560. dbname: tata
  561. EOF2
  562. . "$tprog"
  563. COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  564. _setup_state_dir
  565. out=\$(get_docker_compose www | shyaml get-value services.www.volumes)
  566. test "\$out" == "\\
  567. - /www/tmp/a:/tmp/a:rw
  568. - /www/tmp/b:/tmp/b:rw" || {
  569. echo -e "** get_docker_compose www:\n\$out"
  570. exit 1
  571. }
  572. out=\$(get_docker_compose_links web_site | shyaml get-value web_site.links)
  573. test "\$out" == "- mysql" || {
  574. echo -e "** get_docker_compose_links web_site:\n\$out"
  575. exit 1
  576. }
  577. out=\$(get_docker_compose web_site | shyaml get-value services)
  578. expected="\
  579. mysql:
  580. labels:
  581. - compose.service=mysql
  582. - compose.master-service=mysql
  583. - compose.project=$(basename "$test_tmpdir")
  584. - compose.charm=mysql
  585. volumes:
  586. - /mysql/tmp/c:/tmp/c:rw
  587. - /mysql/tmp/d:/tmp/d:rw
  588. web_site:
  589. labels:
  590. - compose.service=web_site
  591. - compose.master-service=web_site
  592. - compose.project=$(basename "$test_tmpdir")
  593. - compose.charm=www
  594. links:
  595. - mysql
  596. volumes:
  597. - /web_site/tmp/a:/tmp/a:rw
  598. - /web_site/tmp/b:/tmp/b:rw"
  599. test "\$out" == "\$expected" || {
  600. echo -e "** get_docker_compose web_site:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
  601. exit 1
  602. }
  603. ## -- subordinate
  604. export CHARM_STORE=$test_tmpdir
  605. mkdir -p $test_tmpdir/{www,mysql}
  606. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  607. subordinate: true
  608. data-resources:
  609. - /tmp/a
  610. config-resources:
  611. - /tmp/b
  612. requires:
  613. my-db-connection:
  614. interface: db-connection
  615. scope: container
  616. EOF2
  617. cat <<EOF2 > $test_tmpdir/mysql/metadata.yml
  618. data-resources:
  619. - /tmp/c
  620. config-resources:
  621. - /tmp/d
  622. EOF2
  623. cat <<EOF2 > $test_tmpdir/compose.yml
  624. web_site:
  625. charm: www
  626. relations:
  627. db-connection:
  628. mysql:
  629. user: toto
  630. dbname: tata
  631. EOF2
  632. . "$tprog"
  633. COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  634. _setup_state_dir
  635. # should fail because of missing relations
  636. ! get_docker_compose www || exit 1
  637. # volumes gets mixed
  638. out="\$(get_docker_compose web_site | shyaml get-value services.mysql.volumes)"
  639. test "\$out" == "\
  640. - /web_site/tmp/a:/tmp/a:rw
  641. - /web_site/tmp/b:/tmp/b:rw
  642. - /mysql/tmp/c:/tmp/c:rw
  643. - /mysql/tmp/d:/tmp/d:rw" || {
  644. echo -e "OUT:\n\$out"
  645. exit 1
  646. }
  647. ## -- subordinate with complex features
  648. export CHARM_STORE=$test_tmpdir
  649. mkdir -p $test_tmpdir/{www,mysql}
  650. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  651. subordinate: true
  652. data-resources:
  653. - /tmp/a
  654. config-resources:
  655. - /tmp/b
  656. requires:
  657. my-db-connection:
  658. interface: db-connection
  659. scope: container
  660. docker-compose:
  661. volumes:
  662. - /special-volume-from-www:/special-volume-from-www
  663. EOF2
  664. cat <<EOF2 > $test_tmpdir/mysql/metadata.yml
  665. data-resources:
  666. - /tmp/c
  667. config-resources:
  668. - /tmp/d
  669. docker-compose:
  670. entrypoint: custom-entrypoint
  671. volumes:
  672. - /special-volume-from-mysql:/special-volume-from-mysql
  673. EOF2
  674. cat <<EOF2 > $test_tmpdir/compose.yml
  675. web_site:
  676. charm: www
  677. relations:
  678. db-connection:
  679. mysql:
  680. user: toto
  681. dbname: tata
  682. EOF2
  683. . "$tprog"
  684. COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  685. _setup_state_dir
  686. # should fail because of missing relations
  687. #! get_docker_compose www || exit 1
  688. # volumes gets mixed
  689. out="\$(get_docker_compose web_site | shyaml get-value services.mysql)"
  690. expected="\
  691. entrypoint: custom-entrypoint
  692. labels:
  693. - compose.service=web_site
  694. - compose.charm=www
  695. - compose.service=mysql
  696. - compose.master-service=mysql
  697. - compose.project=$(basename "$test_tmpdir")
  698. - compose.charm=mysql
  699. volumes:
  700. - /web_site/tmp/a:/tmp/a:rw
  701. - /web_site/tmp/b:/tmp/b:rw
  702. - /special-volume-from-www:/special-volume-from-www
  703. - /mysql/tmp/c:/tmp/c:rw
  704. - /mysql/tmp/d:/tmp/d:rw
  705. - /special-volume-from-mysql:/special-volume-from-mysql"
  706. test "\$out" == "\$expected" || {
  707. echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
  708. exit 1
  709. }
  710. EOF
  711. tear_test
  712. }
  713. ## XXXvlab: only broken due to Wrap being used for relations
  714. # function test_run_service_relations {
  715. # init_test
  716. # assert_list <<EOF
  717. # ### Testing run_service_relations
  718. # ## -- no docker-compose
  719. # export CHARM_STORE=$test_tmpdir
  720. # mkdir $test_tmpdir/{www,mysql}
  721. # cat <<EOF2 > $test_tmpdir/www/metadata.yml
  722. # data-resources:
  723. # - /tmp/a
  724. # config-resources:
  725. # - /tmp/b
  726. # EOF2
  727. # . "$tprog"
  728. # _setup_state_dir
  729. # echo "Docker Compose:"
  730. # get_docker_compose www
  731. # echo "Run Service relations:"
  732. # _run_service_relation() {
  733. # echo "\$FUNCNAME: received $*"
  734. # }
  735. # out=\$(run_service_relations www)
  736. # test -z "\$out"
  737. # ## -- simple with docker-compose
  738. # export CHARM_STORE=$test_tmpdir
  739. # mkdir -p $test_tmpdir/{www,mysql}
  740. # cat <<EOF2 > $test_tmpdir/www/metadata.yml
  741. # data-resources:
  742. # - /tmp/a
  743. # config-resources:
  744. # - /tmp/b
  745. # EOF2
  746. # cat <<EOF2 > $test_tmpdir/mysql/metadata.yml
  747. # data-resources:
  748. # - /tmp/c
  749. # config-resources:
  750. # - /tmp/d
  751. # EOF2
  752. # cat <<EOF2 > $test_tmpdir/compose.yml
  753. # web_site:
  754. # charm: www
  755. # relations:
  756. # db-connection:
  757. # mysql:
  758. # user: toto
  759. # dbname: tata
  760. # EOF2
  761. # . "$tprog"
  762. # COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  763. # _setup_state_dir
  764. # _setup_state_dir
  765. # echo "Docker Compose:"
  766. # get_docker_compose web_site
  767. # echo "Run Service relations:"
  768. # _run_service_relation() {
  769. # echo "\$FUNCNAME \$2 <-- \$1 --> \$3"
  770. # }
  771. # export -f _run_service_relation
  772. # out=\$(run_service_relations www)
  773. # test -z "\$out" || exit 1
  774. # out=\$(run_service_relations web_site)
  775. # echo "OUT: \$out"
  776. # test "\$out" == "_run_service_relation web_site <-- db-connection --> mysql"
  777. # ## -- subordinate
  778. # export CHARM_STORE=$test_tmpdir
  779. # mkdir -p $test_tmpdir/{www,mysql}
  780. # cat <<EOF2 > $test_tmpdir/www/metadata.yml
  781. # subordinate: true
  782. # data-resources:
  783. # - /tmp/a
  784. # config-resources:
  785. # - /tmp/b
  786. # requires:
  787. # my-db-connection:
  788. # interface: db-connection
  789. # scope: container
  790. # EOF2
  791. # cat <<EOF2 > $test_tmpdir/mysql/metadata.yml
  792. # data-resources:
  793. # - /tmp/c
  794. # config-resources:
  795. # - /tmp/d
  796. # EOF2
  797. # cat <<EOF2 > $test_tmpdir/compose.yml
  798. # web_site:
  799. # charm: www
  800. # relations:
  801. # db-connection:
  802. # mysql:
  803. # user: toto
  804. # dbname: tata
  805. # EOF2
  806. # . "$tprog"
  807. # COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  808. # _setup_state_dir
  809. # echo "Docker Compose:"
  810. # get_docker_compose web_site
  811. # echo "Run Service relations:"
  812. # _run_service_relation() {
  813. # echo "\$FUNCNAME \$2 <-- \$1 --> \$3"
  814. # }
  815. # export -f _run_service_relation
  816. # out=\$(run_service_relations www)
  817. # test -z "\$out" || exit 1
  818. # out=\$(run_service_relations web_site)
  819. # echo "\$out"
  820. # test "\$out" == "_run_service_relation web_site <-- db-connection --> mysql"
  821. # EOF
  822. # tear_test
  823. # }
  824. function test_get_docker_compose_2() {
  825. init_test
  826. assert_list <<EOF
  827. ## -- Simple
  828. export CHARM_STORE=$test_tmpdir
  829. mkdir -p $test_tmpdir/{www,mysql,pg}
  830. touch $test_tmpdir/www/metadata.yml
  831. touch $test_tmpdir/mysql/metadata.yml
  832. cat <<EOF2 > $test_tmpdir/compose.yml
  833. app:
  834. charm: app
  835. relations:
  836. web-proxy:
  837. www:
  838. user: toto
  839. db: mysql
  840. EOF2
  841. . "$tprog"
  842. COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  843. _setup_state_dir
  844. out=\$(get_docker_compose_links "app")
  845. test "\$out" == "app:
  846. links:
  847. - www
  848. - mysql" || {
  849. echo -e "** get_docker_compose_links:\n\$out"; exit 1
  850. }
  851. ## -- reverse-tech-dep
  852. export CHARM_STORE=$test_tmpdir
  853. mkdir -p $test_tmpdir/{www,mysql}
  854. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  855. provides:
  856. web-proxy:
  857. tech-dep: reversed
  858. EOF2
  859. touch $test_tmpdir/mysql/metadata.yml
  860. cat <<EOF2 > $test_tmpdir/compose.yml
  861. web_site:
  862. charm: mysql
  863. relations:
  864. web-proxy:
  865. www:
  866. user: toto
  867. EOF2
  868. . "$tprog"
  869. COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  870. _setup_state_dir
  871. out=\$(get_charm_relation_def "www" "web-proxy") || exit 1
  872. test "\$out" == "tech-dep: reversed" || {
  873. echo -e "** get_charm_relation_def:\n\$out"; exit 1
  874. }
  875. out=\$(get_charm_tech_dep_orientation_for_relation "www" "web-proxy")
  876. test "\$out" == "reversed" || {
  877. echo -e "** get_charm_tech_dep_orientation_for_relation:\n\$out"; exit 1
  878. }
  879. out=\$(get_docker_compose_links "web_site")
  880. expected="www:
  881. links:
  882. - web_site"
  883. test "\$out" == "\$expected" || {
  884. echo -e "** get_docker_compose_links:\n\$out\nExpected:\n\$expected"; exit 1
  885. }
  886. out=\$(get_docker_compose web_site | shyaml get-value services.www.links)
  887. test "\$out" == "- web_site" || {
  888. echo -e "** get_docker_compose:\n\$out"; exit 1
  889. }
  890. EOF
  891. tear_test
  892. }
  893. function test_compose_config {
  894. init_test
  895. export CHARM_STORE=$test_tmpdir
  896. mkdir -p $test_tmpdir/{www,mysql}
  897. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  898. subordinate: true
  899. data-resources:
  900. - /tmp/a
  901. config-resources:
  902. - /tmp/b
  903. requires:
  904. my-db-connection:
  905. interface: db-connection
  906. scope: container
  907. docker-compose:
  908. volumes:
  909. - /special-volume-from-www:/special-volume-from-www
  910. EOF2
  911. cat <<EOF2 > $test_tmpdir/mysql/metadata.yml
  912. docker-image:
  913. docker.0k.io/mysql
  914. data-resources:
  915. - /tmp/c
  916. config-resources:
  917. - /tmp/d
  918. docker-compose:
  919. entrypoint: custom-entrypoint
  920. volumes:
  921. - /special-volume-from-mysql:/special-volume-from-mysql
  922. EOF2
  923. cat <<EOF2 > $test_tmpdir/compose.yml
  924. web_site:
  925. charm: www
  926. relations:
  927. db-connection:
  928. mysql:
  929. user: toto
  930. dbname: tata
  931. EOF2
  932. assert_list <<EOF
  933. ### Testing get_compose_config - syntax validation from docker-compose
  934. ## -- no service provided
  935. cd "$test_tmpdir"
  936. export DISABLE_SYSTEM_CONFIG_FILE=true
  937. out=\$("$tprog" config) || exit 1
  938. expected="services: {}
  939. version: '2.0'"
  940. [ "\$out" == "\$expected" ] || {
  941. echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
  942. exit 1
  943. }
  944. ## -- simple service provided
  945. cd "$test_tmpdir"
  946. export DISABLE_SYSTEM_CONFIG_FILE=true
  947. "$tprog" config mysql
  948. ## -- complex service provided
  949. cd "$test_tmpdir"
  950. export DISABLE_SYSTEM_CONFIG_FILE=true
  951. "$tprog" config web_site
  952. EOF
  953. tear_test
  954. }
  955. continue_on_error="0" testbench $*