smoke-relay.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/bin/bash
  2. set -e -x
  3. set -o pipefail
  4. mkdir -p logs
  5. NETWORK="nebula-smoke-relay"
  6. cleanup() {
  7. echo
  8. echo " *** cleanup"
  9. echo
  10. set +e
  11. if [ "$(jobs -r)" ]
  12. then
  13. docker kill lighthouse1 host2 host3 host4
  14. fi
  15. docker network rm "$NETWORK" >/dev/null 2>&1
  16. }
  17. trap cleanup EXIT
  18. # Create a dedicated smoke network with an explicit subnet (required for --ip
  19. # below). Probe a short list of candidates so a locally-used range doesn't
  20. # fail the whole test — we only need one to be free.
  21. docker network rm "$NETWORK" >/dev/null 2>&1 || true
  22. for candidate in 172.30.0.0/24 172.31.0.0/24 10.98.0.0/24 10.99.0.0/24 192.168.230.0/24; do
  23. if docker network create --subnet "$candidate" "$NETWORK" >/dev/null 2>&1; then
  24. break
  25. fi
  26. done
  27. if ! docker network inspect "$NETWORK" >/dev/null 2>&1; then
  28. echo "failed to create $NETWORK: every candidate subnet is in use" >&2
  29. exit 1
  30. fi
  31. # Derive container IPs from the network's assigned subnet. Slots: .2 lighthouse1,
  32. # .3 host2, .4 host3, .5 host4 — matches the placeholders in build-relay.sh.
  33. SUBNET="$(docker network inspect -f '{{(index .IPAM.Config 0).Subnet}}' "$NETWORK")"
  34. PREFIX="${SUBNET%/*}"
  35. PREFIX="${PREFIX%.*}"
  36. LIGHTHOUSE_IP="$PREFIX.2"
  37. HOST2_IP="$PREFIX.3"
  38. HOST3_IP="$PREFIX.4"
  39. HOST4_IP="$PREFIX.5"
  40. # Sed the placeholder TEST-NET-3 IPs in the host configs to the real ones.
  41. for f in build/host2.yml build/host3.yml build/host4.yml; do
  42. sed "s|203\.0\.113\.|$PREFIX.|g" "$f" >"$f.tmp"
  43. mv "$f.tmp" "$f"
  44. done
  45. docker run --name lighthouse1 --rm nebula:smoke-relay -config lighthouse1.yml -test
  46. docker run --name host2 --rm -v "$PWD/build/host2.yml:/nebula/host2.yml:ro" nebula:smoke-relay -config host2.yml -test
  47. docker run --name host3 --rm -v "$PWD/build/host3.yml:/nebula/host3.yml:ro" nebula:smoke-relay -config host3.yml -test
  48. docker run --name host4 --rm -v "$PWD/build/host4.yml:/nebula/host4.yml:ro" nebula:smoke-relay -config host4.yml -test
  49. docker run --name lighthouse1 --network "$NETWORK" --ip "$LIGHTHOUSE_IP" --device /dev/net/tun:/dev/net/tun --cap-add NET_ADMIN --rm nebula:smoke-relay -config lighthouse1.yml 2>&1 | tee logs/lighthouse1 | sed -u 's/^/ [lighthouse1] /' &
  50. sleep 1
  51. docker run --name host2 --network "$NETWORK" --ip "$HOST2_IP" -v "$PWD/build/host2.yml:/nebula/host2.yml:ro" --device /dev/net/tun:/dev/net/tun --cap-add NET_ADMIN --rm nebula:smoke-relay -config host2.yml 2>&1 | tee logs/host2 | sed -u 's/^/ [host2] /' &
  52. sleep 1
  53. docker run --name host3 --network "$NETWORK" --ip "$HOST3_IP" -v "$PWD/build/host3.yml:/nebula/host3.yml:ro" --device /dev/net/tun:/dev/net/tun --cap-add NET_ADMIN --rm nebula:smoke-relay -config host3.yml 2>&1 | tee logs/host3 | sed -u 's/^/ [host3] /' &
  54. sleep 1
  55. docker run --name host4 --network "$NETWORK" --ip "$HOST4_IP" -v "$PWD/build/host4.yml:/nebula/host4.yml:ro" --device /dev/net/tun:/dev/net/tun --cap-add NET_ADMIN --rm nebula:smoke-relay -config host4.yml 2>&1 | tee logs/host4 | sed -u 's/^/ [host4] /' &
  56. sleep 1
  57. set +x
  58. echo
  59. echo " *** Testing ping from lighthouse1"
  60. echo
  61. set -x
  62. docker exec lighthouse1 ping -c1 192.168.100.2
  63. docker exec lighthouse1 ping -c1 192.168.100.3
  64. docker exec lighthouse1 ping -c1 192.168.100.4
  65. set +x
  66. echo
  67. echo " *** Testing ping from host2"
  68. echo
  69. set -x
  70. docker exec host2 ping -c1 192.168.100.1
  71. # Should fail because no relay configured in this direction
  72. ! docker exec host2 ping -c1 192.168.100.3 -w5 || exit 1
  73. ! docker exec host2 ping -c1 192.168.100.4 -w5 || exit 1
  74. set +x
  75. echo
  76. echo " *** Testing ping from host3"
  77. echo
  78. set -x
  79. docker exec host3 ping -c1 192.168.100.1
  80. docker exec host3 ping -c1 192.168.100.2
  81. docker exec host3 ping -c1 192.168.100.4
  82. set +x
  83. echo
  84. echo " *** Testing ping from host4"
  85. echo
  86. set -x
  87. docker exec host4 ping -c1 192.168.100.1
  88. # Should fail because relays not allowed
  89. ! docker exec host4 ping -c1 192.168.100.2 -w5 || exit 1
  90. docker exec host4 ping -c1 192.168.100.3
  91. docker exec host4 sh -c 'kill 1'
  92. docker exec host3 sh -c 'kill 1'
  93. docker exec host2 sh -c 'kill 1'
  94. docker exec lighthouse1 sh -c 'kill 1'
  95. # Wait up to 30s for all backgrounded jobs to exit rather than relying on a
  96. # fixed sleep.
  97. for _ in $(seq 1 30); do
  98. [ -z "$(jobs -r)" ] && break
  99. sleep 1
  100. done
  101. if [ "$(jobs -r)" ]
  102. then
  103. echo "nebula still running after SIGTERM sent" >&2
  104. exit 1
  105. fi