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.

1298 lines
21 KiB

9 years ago
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. get_all_relations www >/dev/null || exit 3
  388. test "\$(get_master_service_for_service www)" == "www"
  389. ## -- subordinate
  390. export CHARM_STORE=$test_tmpdir
  391. mkdir -p $test_tmpdir/{www,mysql}
  392. touch $test_tmpdir/mysql/metadata.yml
  393. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  394. subordinate: true
  395. uses:
  396. a-name-relation:
  397. constraint: required
  398. scope: container
  399. EOF2
  400. cat <<EOF2 > $test_tmpdir/compose.yml
  401. www:
  402. charm: www
  403. relations:
  404. a-name-relation:
  405. mysql:
  406. label: value
  407. EOF2
  408. . "$tprog"
  409. _setup_state_dir
  410. get_all_relations www >/dev/null || exit 3
  411. COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  412. test "\$(get_master_service_for_service www)" == "mysql"
  413. EOF
  414. }
  415. ##
  416. ##
  417. ##
  418. function test_get_docker_compose_service_mixin() {
  419. init_test
  420. assert_list <<EOF
  421. ### Testing get_docker_compose_service_mixin
  422. ## -- Simple (no compose, no subordinate)
  423. export CHARM_STORE=$test_tmpdir
  424. mkdir $test_tmpdir/{www,mysql}
  425. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  426. data-resources:
  427. - /tmp/a
  428. config-resources:
  429. - /tmp/b
  430. EOF2
  431. . "$tprog"
  432. _setup_state_dir
  433. get_all_relations www >/dev/null || exit 3
  434. out=\$(_get_docker_compose_service_mixin www | shyaml get-value www.volumes)
  435. [[ "\$out" == "\
  436. - /www/tmp/a:/tmp/a:rw
  437. - /www/tmp/b:/tmp/b:rw" ]] || {
  438. echo -e "** _get_docker_compose_service_mixin www:\n\$out"; exit 1
  439. }
  440. ## -- Simple (compose, but no subordinate)
  441. export CHARM_STORE=$test_tmpdir
  442. mkdir -p $test_tmpdir/{www,mysql}
  443. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  444. data-resources:
  445. - /tmp/a
  446. config-resources:
  447. - /tmp/b
  448. EOF2
  449. touch $test_tmpdir/mysql/metadata.yml
  450. cat <<EOF2 > $test_tmpdir/compose.yml
  451. www:
  452. charm: www
  453. relations:
  454. a-name-relation:
  455. mysql:
  456. label: value
  457. EOF2
  458. . "$tprog"
  459. _setup_state_dir
  460. COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  461. get_all_relations www >/dev/null || exit 3
  462. out="\$(_get_docker_compose_service_mixin www)" || exit 1
  463. [ "\$out" == "www:
  464. labels:
  465. - compose.service=www
  466. - compose.master-service=www
  467. - compose.project=\$(basename "$test_tmpdir")
  468. - compose.charm=www
  469. links:
  470. - mysql
  471. volumes:
  472. - /www/tmp/a:/tmp/a:rw
  473. - /www/tmp/b:/tmp/b:rw" ] || {
  474. echo -e "OUT:\n\$out"
  475. exit 1
  476. }
  477. ## -- compose, subordinate
  478. export CHARM_STORE=$test_tmpdir
  479. mkdir -p $test_tmpdir/{www,mysql}
  480. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  481. subordinate: true
  482. data-resources:
  483. - /tmp/a
  484. config-resources:
  485. - /tmp/b
  486. uses:
  487. a-name-relation:
  488. constraint: required
  489. scope: container
  490. EOF2
  491. cat <<EOF2 > $test_tmpdir/compose.yml
  492. www:
  493. charm: www
  494. relations:
  495. a-name-relation:
  496. mysql:
  497. label: value
  498. EOF2
  499. . "$tprog"
  500. _setup_state_dir
  501. COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  502. get_all_relations www >/dev/null || exit 3
  503. out="\$(_get_docker_compose_service_mixin www)" || exit 1
  504. expected="mysql:
  505. labels:
  506. - compose.service=www
  507. - compose.master-service=mysql
  508. - compose.project=$(basename "$test_tmpdir")
  509. - compose.charm=www
  510. volumes:
  511. - /www/tmp/a:/tmp/a:rw
  512. - /www/tmp/b:/tmp/b:rw"
  513. [ "\$out" == "\$expected" ] || {
  514. echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
  515. exit 1
  516. }
  517. EOF
  518. }
  519. function test_get_docker_compose {
  520. init_test
  521. assert_list <<EOF
  522. ### Testing get_docker_compose
  523. ## -- no docker-compose
  524. export CHARM_STORE=$test_tmpdir
  525. mkdir $test_tmpdir/{www,mysql}
  526. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  527. data-resources:
  528. - /tmp/a
  529. config-resources:
  530. - /tmp/b
  531. EOF2
  532. . "$tprog"
  533. _setup_state_dir
  534. get_all_relations www >/dev/null || exit 3
  535. out=\$(get_docker_compose www)
  536. echo "OUT:"
  537. echo "\$out"
  538. out=\$(echo "\$out" | shyaml get-value services.www.volumes)
  539. echo "OUT volumes:"
  540. echo "\$out"
  541. test "\$out" == "\\
  542. - /www/tmp/a:/tmp/a:rw
  543. - /www/tmp/b:/tmp/b:rw"
  544. ## -- simple with docker-compose
  545. export CHARM_STORE=$test_tmpdir
  546. mkdir -p $test_tmpdir/{www,mysql}
  547. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  548. data-resources:
  549. - /tmp/a
  550. config-resources:
  551. - /tmp/b
  552. EOF2
  553. cat <<EOF2 > $test_tmpdir/mysql/metadata.yml
  554. data-resources:
  555. - /tmp/c
  556. config-resources:
  557. - /tmp/d
  558. EOF2
  559. cat <<EOF2 > $test_tmpdir/compose.yml
  560. web_site:
  561. charm: www
  562. relations:
  563. db-connection:
  564. mysql:
  565. user: toto
  566. dbname: tata
  567. EOF2
  568. . "$tprog"
  569. COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  570. _setup_state_dir
  571. get_all_relations web_site >/dev/null || exit 3
  572. out=\$(get_docker_compose www) || exit 4
  573. out=\$(printf "%s" "\$out" | shyaml get-value services.www.volumes) || exit 5
  574. test "\$out" == "\\
  575. - /www/tmp/a:/tmp/a:rw
  576. - /www/tmp/b:/tmp/b:rw" || {
  577. echo -e "** get_docker_compose www:\n\$out"
  578. exit 1
  579. }
  580. out=\$(get_docker_compose_links web_site) || exit 6
  581. out=\$(printf "%s" "\$out" | shyaml get-value web_site.links) || exit 7
  582. test "\$out" == "- mysql" || {
  583. echo -e "** get_docker_compose_links web_site:\n\$out"
  584. exit 1
  585. }
  586. out=\$(get_docker_compose web_site | shyaml get-value services)
  587. expected="\
  588. mysql:
  589. labels:
  590. - compose.service=mysql
  591. - compose.master-service=mysql
  592. - compose.project=$(basename "$test_tmpdir")
  593. - compose.charm=mysql
  594. volumes:
  595. - /mysql/tmp/c:/tmp/c:rw
  596. - /mysql/tmp/d:/tmp/d:rw
  597. web_site:
  598. labels:
  599. - compose.service=web_site
  600. - compose.master-service=web_site
  601. - compose.project=$(basename "$test_tmpdir")
  602. - compose.charm=www
  603. links:
  604. - mysql
  605. volumes:
  606. - /web_site/tmp/a:/tmp/a:rw
  607. - /web_site/tmp/b:/tmp/b:rw"
  608. test "\$out" == "\$expected" || {
  609. echo -e "** get_docker_compose web_site:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
  610. exit 1
  611. }
  612. ## -- subordinate
  613. export CHARM_STORE=$test_tmpdir
  614. mkdir -p $test_tmpdir/{www,mysql}
  615. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  616. subordinate: true
  617. data-resources:
  618. - /tmp/a
  619. config-resources:
  620. - /tmp/b
  621. uses:
  622. db-connection:
  623. constraint: required
  624. scope: container
  625. EOF2
  626. cat <<EOF2 > $test_tmpdir/mysql/metadata.yml
  627. data-resources:
  628. - /tmp/c
  629. config-resources:
  630. - /tmp/d
  631. EOF2
  632. cat <<EOF2 > $test_tmpdir/compose.yml
  633. web_site:
  634. charm: www
  635. relations:
  636. db-connection:
  637. mysql:
  638. user: toto
  639. dbname: tata
  640. EOF2
  641. . "$tprog"
  642. COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  643. _setup_state_dir
  644. get_all_relations web_site >/dev/null || exit 3
  645. # should fail because of missing relations
  646. ! get_docker_compose www || exit 1
  647. # volumes gets mixed
  648. out="\$(get_docker_compose web_site | shyaml get-value services.mysql.volumes)"
  649. test "\$out" == "\
  650. - /web_site/tmp/a:/tmp/a:rw
  651. - /web_site/tmp/b:/tmp/b:rw
  652. - /mysql/tmp/c:/tmp/c:rw
  653. - /mysql/tmp/d:/tmp/d:rw" || {
  654. echo -e "OUT:\n\$out"
  655. exit 1
  656. }
  657. ## -- subordinate with complex features
  658. export CHARM_STORE=$test_tmpdir
  659. mkdir -p $test_tmpdir/{www,mysql}
  660. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  661. subordinate: true
  662. data-resources:
  663. - /tmp/a
  664. config-resources:
  665. - /tmp/b
  666. uses:
  667. db-connection:
  668. constraint: required
  669. scope: container
  670. docker-compose:
  671. volumes:
  672. - /special-volume-from-www:/special-volume-from-www
  673. EOF2
  674. cat <<EOF2 > $test_tmpdir/mysql/metadata.yml
  675. data-resources:
  676. - /tmp/c
  677. config-resources:
  678. - /tmp/d
  679. docker-compose:
  680. entrypoint: custom-entrypoint
  681. volumes:
  682. - /special-volume-from-mysql:/special-volume-from-mysql
  683. EOF2
  684. cat <<EOF2 > $test_tmpdir/compose.yml
  685. web_site:
  686. charm: www
  687. relations:
  688. db-connection:
  689. mysql:
  690. user: toto
  691. dbname: tata
  692. EOF2
  693. . "$tprog"
  694. COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  695. _setup_state_dir
  696. get_all_relations web_site >/dev/null || exit 3
  697. # should fail because of missing relations
  698. #! get_docker_compose www || exit 1
  699. # volumes gets mixed
  700. out="\$(get_docker_compose web_site | shyaml get-value services.mysql)"
  701. expected="\
  702. entrypoint: custom-entrypoint
  703. labels:
  704. - compose.service=web_site
  705. - compose.charm=www
  706. - compose.service=mysql
  707. - compose.master-service=mysql
  708. - compose.project=$(basename "$test_tmpdir")
  709. - compose.charm=mysql
  710. volumes:
  711. - /web_site/tmp/a:/tmp/a:rw
  712. - /web_site/tmp/b:/tmp/b:rw
  713. - /special-volume-from-www:/special-volume-from-www
  714. - /mysql/tmp/c:/tmp/c:rw
  715. - /mysql/tmp/d:/tmp/d:rw
  716. - /special-volume-from-mysql:/special-volume-from-mysql"
  717. test "\$out" == "\$expected" || {
  718. echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
  719. exit 1
  720. }
  721. EOF
  722. tear_test
  723. }
  724. ## XXXvlab: only broken due to Wrap being used for relations
  725. # function test_run_service_relations {
  726. # init_test
  727. # assert_list <<EOF
  728. # ### Testing run_service_relations
  729. # ## -- no docker-compose
  730. # export CHARM_STORE=$test_tmpdir
  731. # mkdir $test_tmpdir/{www,mysql}
  732. # cat <<EOF2 > $test_tmpdir/www/metadata.yml
  733. # data-resources:
  734. # - /tmp/a
  735. # config-resources:
  736. # - /tmp/b
  737. # EOF2
  738. # . "$tprog"
  739. # _setup_state_dir
  740. # echo "Docker Compose:"
  741. # get_docker_compose www
  742. # echo "Run Service relations:"
  743. # _run_service_relation() {
  744. # echo "\$FUNCNAME: received $*"
  745. # }
  746. # out=\$(run_service_relations www)
  747. # test -z "\$out"
  748. # ## -- simple with docker-compose
  749. # export CHARM_STORE=$test_tmpdir
  750. # mkdir -p $test_tmpdir/{www,mysql}
  751. # cat <<EOF2 > $test_tmpdir/www/metadata.yml
  752. # data-resources:
  753. # - /tmp/a
  754. # config-resources:
  755. # - /tmp/b
  756. # EOF2
  757. # cat <<EOF2 > $test_tmpdir/mysql/metadata.yml
  758. # data-resources:
  759. # - /tmp/c
  760. # config-resources:
  761. # - /tmp/d
  762. # EOF2
  763. # cat <<EOF2 > $test_tmpdir/compose.yml
  764. # web_site:
  765. # charm: www
  766. # relations:
  767. # db-connection:
  768. # mysql:
  769. # user: toto
  770. # dbname: tata
  771. # EOF2
  772. # . "$tprog"
  773. # COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  774. # _setup_state_dir
  775. # _setup_state_dir
  776. # echo "Docker Compose:"
  777. # get_docker_compose web_site
  778. # echo "Run Service relations:"
  779. # _run_service_relation() {
  780. # echo "\$FUNCNAME \$2 <-- \$1 --> \$3"
  781. # }
  782. # export -f _run_service_relation
  783. # out=\$(run_service_relations www)
  784. # test -z "\$out" || exit 1
  785. # out=\$(run_service_relations web_site)
  786. # echo "OUT: \$out"
  787. # test "\$out" == "_run_service_relation web_site <-- db-connection --> mysql"
  788. # ## -- subordinate
  789. # export CHARM_STORE=$test_tmpdir
  790. # mkdir -p $test_tmpdir/{www,mysql}
  791. # cat <<EOF2 > $test_tmpdir/www/metadata.yml
  792. # subordinate: true
  793. # data-resources:
  794. # - /tmp/a
  795. # config-resources:
  796. # - /tmp/b
  797. # uses:
  798. # db-connection:
  799. # constraint: required
  800. # scope: container
  801. # EOF2
  802. # cat <<EOF2 > $test_tmpdir/mysql/metadata.yml
  803. # data-resources:
  804. # - /tmp/c
  805. # config-resources:
  806. # - /tmp/d
  807. # EOF2
  808. # cat <<EOF2 > $test_tmpdir/compose.yml
  809. # web_site:
  810. # charm: www
  811. # relations:
  812. # db-connection:
  813. # mysql:
  814. # user: toto
  815. # dbname: tata
  816. # EOF2
  817. # . "$tprog"
  818. # COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  819. # _setup_state_dir
  820. # echo "Docker Compose:"
  821. # get_docker_compose web_site
  822. # echo "Run Service relations:"
  823. # _run_service_relation() {
  824. # echo "\$FUNCNAME \$2 <-- \$1 --> \$3"
  825. # }
  826. # export -f _run_service_relation
  827. # out=\$(run_service_relations www)
  828. # test -z "\$out" || exit 1
  829. # out=\$(run_service_relations web_site)
  830. # echo "\$out"
  831. # test "\$out" == "_run_service_relation web_site <-- db-connection --> mysql"
  832. # EOF
  833. # tear_test
  834. # }
  835. function test_get_docker_compose_2() {
  836. init_test
  837. assert_list <<EOF
  838. ## -- Simple
  839. export CHARM_STORE=$test_tmpdir
  840. mkdir -p $test_tmpdir/{www,mysql,pg}
  841. touch $test_tmpdir/www/metadata.yml
  842. touch $test_tmpdir/mysql/metadata.yml
  843. cat <<EOF2 > $test_tmpdir/compose.yml
  844. app:
  845. charm: app
  846. relations:
  847. web-proxy:
  848. www:
  849. user: toto
  850. db: mysql
  851. EOF2
  852. . "$tprog"
  853. COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  854. _setup_state_dir
  855. get_all_relations app >/dev/null || exit 3
  856. out=\$(get_docker_compose_links "app")
  857. test "\$out" == "app:
  858. links:
  859. - www
  860. - mysql" || {
  861. echo -e "** get_docker_compose_links:\n\$out"; exit 1
  862. }
  863. ## -- reverse-tech-dep
  864. export CHARM_STORE=$test_tmpdir
  865. mkdir -p $test_tmpdir/{www,mysql}
  866. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  867. provides:
  868. web-proxy:
  869. tech-dep: reversed
  870. EOF2
  871. touch $test_tmpdir/mysql/metadata.yml
  872. cat <<EOF2 > $test_tmpdir/compose.yml
  873. web_site:
  874. charm: mysql
  875. relations:
  876. web-proxy:
  877. www:
  878. user: toto
  879. EOF2
  880. . "$tprog"
  881. COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  882. _setup_state_dir
  883. get_all_relations web_site >/dev/null || exit 3
  884. out=\$(get_charm_relation_def "www" "web-proxy") || exit 1
  885. test "\$out" == "tech-dep: reversed" || {
  886. echo -e "** get_charm_relation_def:\n\$out"; exit 1
  887. }
  888. out=\$(get_charm_tech_dep_orientation_for_relation "www" "web-proxy")
  889. test "\$out" == "reversed" || {
  890. echo -e "** get_charm_tech_dep_orientation_for_relation:\n\$out"; exit 1
  891. }
  892. out=\$(get_docker_compose_links "web_site")
  893. expected="www:
  894. links:
  895. - web_site"
  896. test "\$out" == "\$expected" || {
  897. echo -e "** get_docker_compose_links:\n\$out\nExpected:\n\$expected"; exit 1
  898. }
  899. out=\$(get_docker_compose web_site | shyaml get-value services.www.links)
  900. test "\$out" == "- web_site" || {
  901. echo -e "** get_docker_compose:\n\$out"; exit 1
  902. }
  903. EOF
  904. tear_test
  905. }
  906. function test_compose_config {
  907. init_test
  908. export CHARM_STORE=$test_tmpdir
  909. mkdir -p $test_tmpdir/{www,mysql}
  910. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  911. subordinate: true
  912. data-resources:
  913. - /tmp/a
  914. config-resources:
  915. - /tmp/b
  916. uses:
  917. db-connection:
  918. constraint: required
  919. scope: container
  920. docker-compose:
  921. volumes:
  922. - /special-volume-from-www:/special-volume-from-www
  923. EOF2
  924. cat <<EOF2 > $test_tmpdir/mysql/metadata.yml
  925. docker-image:
  926. docker.0k.io/mysql
  927. data-resources:
  928. - /tmp/c
  929. config-resources:
  930. - /tmp/d
  931. docker-compose:
  932. entrypoint: custom-entrypoint
  933. volumes:
  934. - /special-volume-from-mysql:/special-volume-from-mysql
  935. EOF2
  936. cat <<EOF2 > $test_tmpdir/compose.yml
  937. web_site:
  938. charm: www
  939. relations:
  940. db-connection:
  941. mysql:
  942. user: toto
  943. dbname: tata
  944. EOF2
  945. assert_list <<EOF
  946. ### Testing get_compose_config - syntax validation from docker-compose
  947. ## -- no service provided
  948. cd "$test_tmpdir"
  949. export DISABLE_SYSTEM_CONFIG_FILE=true
  950. out=\$("$tprog" config) || exit 1
  951. expected="services: {}
  952. version: '2.0'"
  953. [ "\$out" == "\$expected" ] || {
  954. echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
  955. exit 1
  956. }
  957. ## -- simple service provided
  958. cd "$test_tmpdir"
  959. export DISABLE_SYSTEM_CONFIG_FILE=true
  960. "$tprog" config mysql
  961. ## -- complex service provided
  962. cd "$test_tmpdir"
  963. export DISABLE_SYSTEM_CONFIG_FILE=true
  964. "$tprog" config web_site
  965. EOF
  966. tear_test
  967. }
  968. continue_on_error="0" testbench $*