fork 0k-charms
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.

171 lines
4.2 KiB

  1. #!/bin/bash
  2. exname=$(basename $0)
  3. compose_core=$(which compose-core) || {
  4. echo "Requires compose-core executable to be in \$PATH." >&2
  5. exit 1
  6. }
  7. fetch-def() {
  8. local path="$1" fname="$2"
  9. ( . "$path" 1>&2 || {
  10. echo "Failed to load '$path'." >&2
  11. exit 1
  12. }
  13. declare -f "$fname"
  14. )
  15. }
  16. prefix_cmd="
  17. . /etc/shlib
  18. include common
  19. include parse
  20. . ../lib/common
  21. $(fetch-def "$compose_core" yaml_get_values)
  22. $(fetch-def "$compose_core" yaml_get_interpret)
  23. $(fetch-def "$compose_core" read-0-err)
  24. $(fetch-def "$compose_core" p-err)
  25. $(fetch-def "$compose_core" expand_vars)
  26. SERVICE_NAME='bar'
  27. " || {
  28. echo "Couldn't build prefix cmd" >&2
  29. exit 1
  30. }
  31. # mock
  32. cfg-get-value() {
  33. local key="$1"
  34. shyaml get-value "$key" 2>/dev/null
  35. }
  36. export -f cfg-get-value
  37. get_top_master_service_for_service() {
  38. local service="$1"
  39. echo "$service"
  40. }
  41. export -f get_top_master_service_for_service
  42. get_service_charm() {
  43. local service="$1"
  44. echo "$service"
  45. }
  46. export -f get_service_charm
  47. charm.get_dir() {
  48. local charm="$1"
  49. echo "/path/to/charm-store/$charm"
  50. }
  51. export -f charm.get_dir
  52. export CACHEDIR=$(mktemp -d -t tmp.XXXXXXXXXX)
  53. export state_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
  54. trap "rm -rf \"$state_tmpdir\"" EXIT
  55. trap "rm -rf \"$CACHEDIR\"" EXIT
  56. ##
  57. ## Tests
  58. ##
  59. try "
  60. cron:entries_from_service 'foo' ''"
  61. is errlvl 1
  62. is err reg "Error:.*ailed to get.*."
  63. is err reg "Error:.*empty.*."
  64. is out '' TRIM
  65. try "
  66. cron:entries_from_service 'foo' '
  67. (0 0 * * *) {XX} dc run --rm foo
  68. '" "wrong lock args"
  69. is errlvl 1
  70. is err reg "Error:.*lock argument.*."
  71. is err reg "Error:.*parse lock.*."
  72. is out '' TRIM
  73. try "
  74. cron:entries_from_service 'foo' '
  75. (0 0 * * * *) {} dc run --rm foo
  76. '" "wrong schedule"
  77. is errlvl 1
  78. is err reg "Error:.*schedule.*"
  79. is out '' TRIM
  80. try "
  81. cron:entries_from_service 'foo' '
  82. (0 0 * * *) {}
  83. '" "wrong command"
  84. is errlvl 1
  85. is err reg "Error:.*empty command.*"
  86. is out '' TRIM
  87. try "
  88. set -o pipefail &&
  89. cron:entries_from_service 'foo' '
  90. (0 0 * * *) {-p 10 -k} dc run --rm foo
  91. ' | tr '\0' '\n'" "one command no label"
  92. noerror
  93. is out "\
  94. 0 0 * * * lock launch-foo -p 10 -k -c \"dc run --rm foo\" 2>&1 | awk '{ print strftime(\"%Y-%m-%d %H:%M:%S %Z\"), \$0; fflush(); }' >> /var/log/cron/launch-foo_script.log\
  95. " TRIM
  96. try "
  97. set -o pipefail &&
  98. cron:entries_from_service 'foo' '
  99. wiz: (0 0 * * *) {-p 10 -k} dc run --rm foo
  100. ' | tr '\0' '\n'" "one command with label"
  101. noerror
  102. is out "\
  103. 0 0 * * * lock launch-foo-wiz -p 10 -k -c \"dc run --rm foo\" 2>&1 | awk '{ print strftime(\"%Y-%m-%d %H:%M:%S %Z\"), \$0; fflush(); }' >> /var/log/cron/launch-foo-wiz_script.log\
  104. " TRIM
  105. try "
  106. set -o pipefail &&
  107. cron:entries_from_service 'foo' '
  108. wiz: (0 0 * * *) {-p 10 -k} dc run --rm foo
  109. bam: (@daily) {-p 10 -D -k} dc run --rm foo --hop
  110. ' | tr '\0' '\n'" "multi command with label"
  111. noerror
  112. is out "\
  113. 0 0 * * * lock launch-foo-wiz -p 10 -k -c \"dc run --rm foo\" 2>&1 | awk '{ print strftime(\"%Y-%m-%d %H:%M:%S %Z\"), \$0; fflush(); }' >> /var/log/cron/launch-foo-wiz_script.log
  114. @daily lock launch-foo-bam -p 10 -D -k -c \"dc run --rm foo --hop\" 2>&1 | awk '{ print strftime(\"%Y-%m-%d %H:%M:%S %Z\"), \$0; fflush(); }' >> /var/log/cron/launch-foo-bam_script.log\
  115. " TRIM
  116. try "
  117. set -o pipefail &&
  118. cron:entries_from_service 'foo' '!var-expand
  119. (0 0 * * *) {-p 10 -k} dc run --rm \$BASE_SERVICE_NAME \$MASTER_BASE_SERVICE_NAME
  120. ' | tr '\0' '\n'" "using relation's var"
  121. noerror
  122. is out "\
  123. 0 0 * * * lock launch-foo -p 10 -k -c \"dc run --rm foo foo\" 2>&1 | awk '{ print strftime(\"%Y-%m-%d %H:%M:%S %Z\"), \$0; fflush(); }' >> /var/log/cron/launch-foo_script.log" TRIM
  124. try "
  125. set -o pipefail &&
  126. cfg='!var-expand
  127. (0 0 * * *) {-p 10 -k} dc run --rm \$BASE_SERVICE_NAME \$MASTER_BASE_SERVICE_NAME
  128. '
  129. cron:entries_from_service 'foo1' \"\$cfg\" | tr '\0' '\n'
  130. cron:entries_from_service 'foo2' \"\$cfg\" | tr '\0' '\n'
  131. " "using relation's var, same conf, different env"
  132. noerror
  133. is out "\
  134. 0 0 * * * lock launch-foo1 -p 10 -k -c \"dc run --rm foo1 foo1\" 2>&1 | awk '{ print strftime(\"%Y-%m-%d %H:%M:%S %Z\"), \$0; fflush(); }' >> /var/log/cron/launch-foo1_script.log
  135. 0 0 * * * lock launch-foo2 -p 10 -k -c \"dc run --rm foo2 foo2\" 2>&1 | awk '{ print strftime(\"%Y-%m-%d %H:%M:%S %Z\"), \$0; fflush(); }' >> /var/log/cron/launch-foo2_script.log\
  136. " TRIM