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.

533 lines
8.6 KiB

  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. mkdir -p "$CACHEDIR"
  21. }
  22. tear_test() {
  23. rm -rf "$test_tmpdir"
  24. }
  25. ##
  26. ## Tests
  27. ##
  28. function test_all_relations_simple {
  29. init_test
  30. export DISABLE_SYSTEM_CONFIG_FILE=true
  31. assert_list <<EOF
  32. ### Testing simple case
  33. ## -- simple case with only one service with one relation, no uses/provides
  34. export CHARM_STORE=$test_tmpdir
  35. mkdir -p $test_tmpdir/{www,mysql}
  36. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  37. EOF2
  38. touch $test_tmpdir/mysql/metadata.yml
  39. cat <<EOF2 > $test_tmpdir/compose.yml
  40. www:
  41. charm: www
  42. relations:
  43. mysql-db:
  44. mysql:
  45. label: value
  46. EOF2
  47. . "$tprog"
  48. _setup_state_dir
  49. COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  50. out=\$(set -o pipefail
  51. get_all_relations www mysql | tr '\0' '\n') || exit 3
  52. expected="www
  53. mysql-db
  54. mysql
  55. label: value
  56. True"
  57. [[ "\$out" == "\$expected" ]] || {
  58. echo "doesn't end with: \$expected" >&2
  59. echo "\$out"
  60. exit 1
  61. }
  62. ## -- simple case, 1 uses, already present
  63. export CHARM_STORE=$test_tmpdir
  64. mkdir -p $test_tmpdir/{www,mysql}
  65. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  66. uses:
  67. mysql-db:
  68. constraint: optional
  69. default-options:
  70. label: from-default-options
  71. label2: from-default-options
  72. EOF2
  73. touch $test_tmpdir/mysql/metadata.yml
  74. cat <<EOF2 > $test_tmpdir/compose.yml
  75. www:
  76. charm: www
  77. relations:
  78. mysql-db:
  79. mysql:
  80. label: value
  81. EOF2
  82. . "$tprog"
  83. _setup_state_dir
  84. COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  85. out=\$(set -o pipefail
  86. get_all_relations www mysql | tr '\0' '\n') || exit 3
  87. expected="www
  88. mysql-db
  89. mysql
  90. label: value
  91. label2: from-default-options
  92. True"
  93. [[ "\$out" == "\$expected" ]] || {
  94. echo "doesn't end with: \$expected" >&2
  95. echo --- OUT: >&2
  96. echo "\$out" >&2
  97. exit 1
  98. }
  99. ## -- simple case, 1 uses, optional, not present
  100. export CHARM_STORE=$test_tmpdir
  101. mkdir -p $test_tmpdir/{www,mysql}
  102. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  103. uses:
  104. mysql-db:
  105. constraint: optional
  106. default-options:
  107. label: from-default-options
  108. label2: from-default-options
  109. EOF2
  110. touch $test_tmpdir/mysql/metadata.yml
  111. cat <<EOF2 > $test_tmpdir/compose.yml
  112. www:
  113. charm: www
  114. EOF2
  115. . "$tprog"
  116. _setup_state_dir
  117. COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  118. out=\$(set -o pipefail
  119. get_all_relations www mysql 2>&1 >/dev/null) || exit 3
  120. expected=""
  121. [[ "\$out" == "\$expected" ]] || {
  122. echo "doesn't end with: \$expected" >&2
  123. echo "\$out"
  124. exit 1
  125. }
  126. out=\$(set -o pipefail
  127. get_all_relations www mysql | tr '\0' '\n') || exit 3
  128. expected=""
  129. [[ "\$out" == "\$expected" ]] || {
  130. echo "doesn't end with: \$expected" >&2
  131. echo "\$out"
  132. exit 1
  133. }
  134. ## -- simple case, 1 uses, optional with solves, not present
  135. export CHARM_STORE=$test_tmpdir
  136. mkdir -p $test_tmpdir/{www,mysql}
  137. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  138. uses:
  139. mysql-db:
  140. constraint: optional
  141. solves:
  142. missing-feature: foo
  143. default-options:
  144. label: from-default-options
  145. label2: from-default-options
  146. EOF2
  147. touch $test_tmpdir/mysql/metadata.yml
  148. cat <<EOF2 > $test_tmpdir/compose.yml
  149. www:
  150. charm: www
  151. EOF2
  152. . "$tprog"
  153. _setup_state_dir
  154. COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  155. out=\$(set -o pipefail
  156. get_all_relations www mysql 2>&1 >/dev/null) || exit 3
  157. expected="Notice"
  158. [[ "\$out" == *"\$expected"* ]] || {
  159. echo "doesn't contain: \$expected" >&2
  160. echo "\$out"
  161. exit 1
  162. }
  163. ## -- simple case, 1 uses, auto-pair, present
  164. export CHARM_STORE=$test_tmpdir
  165. mkdir -p $test_tmpdir/{www,mysql}
  166. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  167. uses:
  168. mysql-db:
  169. constraint: optional
  170. auto: pair
  171. default-options:
  172. label: from-default-options
  173. label2: from-default-options
  174. EOF2
  175. touch $test_tmpdir/mysql/metadata.yml
  176. cat <<EOF2 > $test_tmpdir/compose.yml
  177. www:
  178. charm: www
  179. relations:
  180. mysql-db:
  181. mysql:
  182. label: value
  183. EOF2
  184. . "$tprog"
  185. _setup_state_dir
  186. COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  187. out=\$(set -o pipefail
  188. get_all_relations www mysql | tr '\0' '\n') || exit 3
  189. expected="www
  190. mysql-db
  191. mysql
  192. label: value
  193. label2: from-default-options
  194. True"
  195. [[ "\$out" == "\$expected" ]] || {
  196. echo "doesn't end with: \$expected" >&2
  197. echo "\$out"
  198. exit 1
  199. }
  200. ## -- simple case, 1 uses, auto-pair, not present, no provider
  201. export CHARM_STORE=$test_tmpdir
  202. mkdir -p $test_tmpdir/{www,mysql}
  203. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  204. uses:
  205. mysql-db:
  206. constraint: optional
  207. auto: pair
  208. default-options:
  209. label: from-default-options
  210. label2: from-default-options
  211. EOF2
  212. touch $test_tmpdir/mysql/metadata.yml
  213. cat <<EOF2 > $test_tmpdir/compose.yml
  214. www:
  215. charm: www
  216. EOF2
  217. . "$tprog"
  218. _setup_state_dir
  219. COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  220. out=\$(set -o pipefail
  221. get_all_relations www mysql | tr '\0' '\n') || exit 3
  222. expected=""
  223. [[ "\$out" == "\$expected" ]] || {
  224. echo "doesn't end with: \$expected" >&2
  225. echo "\$out"
  226. exit 1
  227. }
  228. ## -- simple case, 1 uses, auto-pair, not present, 1 provider
  229. export CHARM_STORE=$test_tmpdir
  230. mkdir -p $test_tmpdir/{www,mysql}
  231. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  232. uses:
  233. mysql-db:
  234. constraint: optional
  235. auto: pair
  236. default-options:
  237. label: from-default-options
  238. label2: from-default-options
  239. EOF2
  240. cat <<EOF2 > $test_tmpdir/mysql/metadata.yml
  241. provides:
  242. mysql-db:
  243. EOF2
  244. cat <<EOF2 > $test_tmpdir/compose.yml
  245. www:
  246. charm: www
  247. EOF2
  248. . "$tprog"
  249. _setup_state_dir
  250. COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  251. out=\$(set -o pipefail
  252. get_all_relations www mysql | tr '\0' '\n') || exit 3
  253. expected="www
  254. mysql-db
  255. mysql
  256. label: from-default-options
  257. label2: from-default-options
  258. True"
  259. [[ "\$out" == "\$expected" ]] || {
  260. echo "--- EXPECTED" >&2
  261. echo "\$expected" >&2
  262. echo "--- OUT" >&2
  263. echo "\$out" >&2
  264. exit 1
  265. }
  266. ## -- simple case, 1 uses, auto-pair, not present, 2 providers
  267. export CHARM_STORE=$test_tmpdir
  268. mkdir -p $test_tmpdir/{www,mysql,mysql2}
  269. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  270. uses:
  271. mysql-db:
  272. constraint: optional
  273. auto: pair
  274. default-options:
  275. label: from-default-options
  276. label2: from-default-options
  277. EOF2
  278. cat <<EOF2 > $test_tmpdir/mysql/metadata.yml
  279. provides:
  280. mysql-db:
  281. EOF2
  282. cat <<EOF2 > $test_tmpdir/mysql2/metadata.yml
  283. provides:
  284. mysql-db:
  285. EOF2
  286. cat <<EOF2 > $test_tmpdir/compose.yml
  287. www:
  288. charm: www
  289. EOF2
  290. . "$tprog"
  291. _setup_state_dir
  292. COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  293. out=\$(set -o pipefail
  294. get_all_relations www mysql mysql2 2>&1 >/dev/null) || exit 3
  295. expected="> 1"
  296. [[ "\$out" == *"\$expected"* ]] || {
  297. echo "doesn't contain: \$expected" >&2
  298. echo "\$out"
  299. exit 1
  300. }
  301. # Remember, it is cached
  302. out=\$(set -o pipefail
  303. get_all_relations www mysql mysql2 | tr '\0' '\n') || exit 3
  304. expected=""
  305. [[ "\$out" == "\$expected" ]] || {
  306. echo "doesn't end with: \$expected" >&2
  307. echo "\$out"
  308. exit 1
  309. }
  310. EOF
  311. tear_test
  312. }
  313. function test_all_relations_missing {
  314. init_test
  315. export DISABLE_SYSTEM_CONFIG_FILE=true
  316. assert_list <<EOF
  317. ### Testing missing services at first call
  318. ## -- 1 service, connection to another, no use/provide
  319. export CHARM_STORE=$test_tmpdir
  320. mkdir -p $test_tmpdir/{www,mysql}
  321. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  322. EOF2
  323. cat <<EOF2 > $test_tmpdir/mysql/metadata.yml
  324. EOF2
  325. cat <<EOF2 > $test_tmpdir/compose.yml
  326. www:
  327. relations:
  328. mysql-db: mysql
  329. EOF2
  330. . "$tprog"
  331. _setup_state_dir
  332. COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  333. out=\$(set -o pipefail
  334. get_all_relations www | tr '\0' '\n') || exit 3
  335. expected="www
  336. mysql-db
  337. mysql
  338. True"
  339. [[ "\$out" == "\$expected" ]] || {
  340. echo "doesn't end with: \$expected" >&2
  341. echo "OUT:" >&2
  342. echo "\$out" >&2
  343. exit 1
  344. }
  345. ## -- 1 service, connection to another that auto-pair with first
  346. export CHARM_STORE=$test_tmpdir
  347. mkdir -p $test_tmpdir/{www,mysql}
  348. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  349. provides:
  350. www-proxy:
  351. EOF2
  352. cat <<EOF2 > $test_tmpdir/mysql/metadata.yml
  353. uses:
  354. www-proxy:
  355. constraint: optional
  356. auto: pair
  357. EOF2
  358. cat <<EOF2 > $test_tmpdir/compose.yml
  359. www:
  360. relations:
  361. mysql-db: mysql
  362. EOF2
  363. . "$tprog"
  364. _setup_state_dir
  365. COMPOSE_YML_FILE=$test_tmpdir/compose.yml
  366. out=\$(set -o pipefail
  367. get_all_relations www | tr '\0' '\n') || exit 3
  368. expected="\
  369. www
  370. mysql-db
  371. mysql
  372. True
  373. mysql
  374. www-proxy
  375. www
  376. True"
  377. [[ "\$out" == "\$expected" ]] || {
  378. echo "DIFF:" >&2
  379. diff -u <(echo "\$expected") <(echo "\$out") >&2
  380. exit 1
  381. }
  382. EOF
  383. tear_test
  384. }
  385. continue_on_error="0" testbench $*