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.

151 lines
2.9 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. " || {
  27. echo "Couldn't build prefix cmd" >&2
  28. exit 1
  29. }
  30. # mock
  31. cfg-get-value() {
  32. local key="$1"
  33. shyaml get-value "$key" 2>/dev/null
  34. }
  35. export -f cfg-get-value
  36. yaml_get_interpret() {
  37. shyaml get-value
  38. }
  39. export -f yaml_get_interpret
  40. export CACHEDIR=$(mktemp -d -t tmp.XXXXXXXXXX)
  41. export state_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
  42. trap "rm -rf \"$state_tmpdir\"" EXIT
  43. trap "rm -rf \"$CACHEDIR\"" EXIT
  44. ##
  45. ## Tests
  46. ##
  47. try "
  48. cron:get_config ''"
  49. is errlvl 1
  50. is err reg 'Error: .*empty.*'
  51. is out ''
  52. try "
  53. cron:get_config 'xxx'"
  54. is errlvl 1
  55. is err reg 'Error: .*syntax.*'
  56. is out ''
  57. try "
  58. set pipefail &&
  59. cron:get_config '(@daily) {} /bin/true' | tr '\0' ':'
  60. " "str simple example without label"
  61. noerror
  62. is out "@daily:::/bin/true:"
  63. try "
  64. set pipefail &&
  65. cron:get_config 'foo (@daily) {} /bin/true' | tr '\0' ':'
  66. " "str simple example with label"
  67. noerror
  68. is out "@daily::foo:/bin/true:"
  69. try "
  70. set pipefail &&
  71. cron:get_config 'foo (@daily) {-p 10 -D} /bin/true' | tr '\0' ':'
  72. " "str simple example with lock options"
  73. noerror
  74. is out "@daily:-p 10 -D:foo:/bin/true:"
  75. try "
  76. set pipefail &&
  77. cron:get_config 'foo (*/2 * * * *) {-p 10 -D} /bin/true' | tr '\0' ':'
  78. " "str simple example with all fields"
  79. noerror
  80. is out "*/2 * * * *:-p 10 -D:foo:/bin/true:"
  81. try "
  82. set pipefail &&
  83. cron:get_config '- foo (*/2 * * * *) {-p 10 -D} /bin/true' | tr '\0' ':'
  84. " "list 1 elt with str simple example with all fields"
  85. noerror
  86. is out "*/2 * * * *:-p 10 -D:foo:/bin/true:"
  87. try "
  88. set pipefail &&
  89. cron:get_config '
  90. - foo (*/2 * * * *) {-p 10 -D} /bin/true
  91. - bar (*/3 * * * *) {-p 10 -D -k} /bin/false
  92. ' | tr '\0' ':'
  93. " "list 2 elts with str simple example with all fields"
  94. noerror
  95. is out "*/2 * * * *:-p 10 -D:foo:/bin/true:*/3 * * * *:-p 10 -D -k:bar:/bin/false:"
  96. try "
  97. set pipefail &&
  98. cron:get_config '
  99. foo: (*/2 * * * *) {-p 10 -D} /bin/true
  100. bar: (*/3 * * * *) {-p 10 -D -k} /bin/false
  101. ' | tr '\0' ':'
  102. " "struct 2 elts with str simple example with all fields"
  103. noerror
  104. is out "*/2 * * * *:-p 10 -D:foo:/bin/true:*/3 * * * *:-p 10 -D -k:bar:/bin/false:"
  105. try "
  106. cron:get_config '!!float 3.7'
  107. " "bad type"
  108. is errlvl 1
  109. is err reg 'Error: .*type.*'
  110. is out ''
  111. try "
  112. export FOO=bar
  113. set pipefail &&
  114. cron:get_config '!var-expand (*/2 * * * *) {-p 10 -D} \"/bin/\${FOO}\"' | tr '\0' ':'
  115. " "var-expand"
  116. is errlvl 0
  117. is err ''
  118. is out '*/2 * * * *:-p 10 -D::"/bin/bar":'