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.

1482 lines
24 KiB

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