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.

155 lines
3.5 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. yaml_get_interpret() {
  38. shyaml get-value
  39. }
  40. export -f yaml_get_interpret
  41. get_top_master_service_for_service() {
  42. local service="$1"
  43. echo "$service"
  44. }
  45. export -f get_top_master_service_for_service
  46. get_service_charm() {
  47. local service="$1"
  48. echo "$service"
  49. }
  50. export -f get_service_charm
  51. export CACHEDIR=$(mktemp -d -t tmp.XXXXXXXXXX)
  52. export state_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
  53. trap "rm -rf \"$state_tmpdir\"" EXIT
  54. trap "rm -rf \"$CACHEDIR\"" EXIT
  55. ##
  56. ## Tests
  57. ##
  58. try "
  59. cron:entries_from_service 'foo' ''"
  60. is errlvl 1
  61. is err reg "Error:.*ailed to get.*."
  62. is err reg "Error:.*empty.*."
  63. is out '' TRIM
  64. try "
  65. cron:entries_from_service 'foo' '
  66. (0 0 * * *) {XX} dc run --rm foo
  67. '" "wrong lock args"
  68. is errlvl 1
  69. is err reg "Error:.*lock argument.*."
  70. is err reg "Error:.*parse lock.*."
  71. is out '' TRIM
  72. try "
  73. cron:entries_from_service 'foo' '
  74. (0 0 * * * *) {} dc run --rm foo
  75. '" "wrong schedule"
  76. is errlvl 1
  77. is err reg "Error:.*schedule.*"
  78. is out '' TRIM
  79. try "
  80. cron:entries_from_service 'foo' '
  81. (0 0 * * *) {}
  82. '" "wrong command"
  83. is errlvl 1
  84. is err reg "Error:.*empty command.*"
  85. is out '' TRIM
  86. try "
  87. set pipefail &&
  88. cron:entries_from_service 'foo' '
  89. (0 0 * * *) {-p 10 -k} dc run --rm foo
  90. ' | tr '\0' '\n'" "one command no label"
  91. noerror
  92. is out "\
  93. 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\
  94. " TRIM
  95. try "
  96. set pipefail &&
  97. cron:entries_from_service 'foo' '
  98. wiz: (0 0 * * *) {-p 10 -k} dc run --rm foo
  99. ' | tr '\0' '\n'" "one command with label"
  100. noerror
  101. is out "\
  102. 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\
  103. " TRIM
  104. try "
  105. set pipefail &&
  106. cron:entries_from_service 'foo' '
  107. wiz: (0 0 * * *) {-p 10 -k} dc run --rm foo
  108. bam: (@daily) {-p 10 -D -k} dc run --rm foo --hop
  109. ' | tr '\0' '\n'" "multi command with label"
  110. noerror
  111. is out "\
  112. 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
  113. @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\
  114. " TRIM
  115. try "
  116. set pipefail &&
  117. cron:entries_from_service 'foo' '!var-expand
  118. (0 0 * * *) {-p 10 -k} dc run --rm \$BASE_SERVICE_NAME \$MASTER_BASE_SERVICE_NAME
  119. ' | tr '\0' '\n'" "using relation's var"
  120. noerror
  121. is out "\
  122. 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