ab-dropin.sh 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/bin/sh
  2. # A/B drop-in test: unbound-sec:v0.1.0 vs. unbound-sec:test (v0.2.0)
  3. # Same config, same network, identical queries -> responses must match.
  4. set -u
  5. N=ubsec-ab
  6. C1=ab-v010
  7. C2=ab-v020
  8. CFG=$(pwd)/test/fixture-ab
  9. PASS=0
  10. FAIL=0
  11. ok() { PASS=$((PASS+1)); printf ' [PASS] %s\n' "$1"; }
  12. bad() { FAIL=$((FAIL+1)); printf ' [FAIL] %s\n' "$1"; }
  13. cleanup() {
  14. docker rm -f "$C1" "$C2" >/dev/null 2>&1
  15. docker network rm "$N" >/dev/null 2>&1
  16. rm -rf "$CFG"
  17. }
  18. trap cleanup EXIT
  19. cleanup
  20. docker network create "$N" >/dev/null
  21. mkdir -p "$CFG"
  22. printf 'server:\n statistics-interval: 0\n' > "$CFG/99-ab.conf"
  23. docker run -d --name "$C1" --network "$N" \
  24. --tmpfs "/run/unbound:uid=1500,gid=1500" -v "$CFG:/config:ro" \
  25. unbound-sec:v0.1.0 >/dev/null
  26. docker run -d --name "$C2" --network "$N" \
  27. --tmpfs "/run/unbound:uid=1500,gid=1500" -v "$CFG:/config:ro" \
  28. unbound-sec:test >/dev/null
  29. sleep 4
  30. for c in "$C1" "$C2"; do
  31. [ "$(docker inspect -f '{{.State.Running}}' "$c")" = "true" ] \
  32. && ok "$c running" || { bad "$c running"; exit 1; }
  33. done
  34. # Extract the ANSWER section of a drill response (empty = no answers).
  35. ans() {
  36. docker exec "$1" drill -p 5053 "$2" @127.0.0.1 2>/dev/null \
  37. | awk '/;; ANSWER SECTION:/{f=1;next} /;; AUTHORITY SECTION:/{f=0} f && NF'
  38. }
  39. # 1. identical plain resolution
  40. a1="$(ans "$C1" example.com)"; a2="$(ans "$C2" example.com)"
  41. if [ -n "$a1" ] && [ "$a1" = "$a2" ]; then
  42. ok "identical non-empty answer for example.com"
  43. else
  44. bad "identical non-empty answer for example.com"
  45. printf ' v0.1.0: %s\n v0.2.0: %s\n' "$a1" "$a2"
  46. fi
  47. # 2. DNSSEC: AD bit must be set on both (raw DO-bit query via python probe)
  48. P() {
  49. docker run --rm --network "$N" -v "$(pwd)/test/dns-check.py:/probe.py:ro" \
  50. python:alpine python /probe.py "$1" 5053 2>&1
  51. }
  52. p1="$(P "$C1")"; p2="$(P "$C2")"
  53. if printf '%s' "$p1" | grep -q "PASS" && printf '%s' "$p2" | grep -q "PASS" \
  54. && [ "$(printf '%s' "$p1" | grep -c PASS)" = "$(printf '%s' "$p2" | grep -c PASS)" ]; then
  55. ok "DNSSEC AD-bit on both versions (identical probe results)"
  56. else
  57. bad "DNSSEC AD-bit on both versions"
  58. printf ' v0.1.0: %s\n v0.2.0: %s\n' "$p1" "$p2"
  59. fi
  60. # 3. identity behavior identical (hide-identity: yes in base config)
  61. chaos() {
  62. docker exec "$1" drill -p 5053 -C CHAOS id.server. @127.0.0.1 2>/dev/null \
  63. | awk '/;; ANSWER SECTION:/{f=1;next} /;; AUTHORITY SECTION:/{f=0} f && NF'
  64. }
  65. c1="$(chaos "$C1")"; c2="$(chaos "$C2")"
  66. if [ -z "$c1" ] && [ -z "$c2" ]; then
  67. ok "identical CHAOS id.server behavior (empty answer section, hide-identity)"
  68. else
  69. bad "identical CHAOS id.server behavior (v0.1.0: '$c1' / v0.2.0: '$c2')"
  70. fi
  71. # 4. same user/port/healthcheck contract (from image metadata + runtime uid)
  72. for c in "$C1" "$C2"; do
  73. uid="$(docker exec "$c" sh -c 'id -u')"
  74. ports="$(docker inspect -f '{{json .Config.ExposedPorts}}' "$c")"
  75. hc="$(docker inspect -f '{{json .Config.Healthcheck.Test}}' "$c")"
  76. vol="$(docker inspect -f '{{json .Config.Volumes}}' "$c")"
  77. if [ "$uid" = "1500" ] && printf '%s' "$ports" | grep -q '5053' \
  78. && printf '%s' "$hc" | grep -q "5053" && printf '%s' "$vol" | grep -q '/config'; then
  79. ok "$c contract: uid 1500, expose 5053, healthcheck 5053, volume /config"
  80. else
  81. bad "$c contract (uid=$uid ports=$ports hc=$hc vol=$vol)"
  82. fi
  83. done
  84. printf '\nAB RESULT: %s passed, %s failed\n' "$PASS" "$FAIL"
  85. [ "$FAIL" -eq 0 ]