config.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/bin/bash
  2. # NB only to be sourced
  3. set -e
  4. # these ought to match what is in Vagrantfile
  5. # exported to override weave config.sh
  6. export SSH_DIR="$PWD"
  7. export HOSTS
  8. # shellcheck disable=SC1091
  9. . "../tools/integration/config.sh"
  10. WEAVE="./weave"
  11. SCOPE="../scope"
  12. scope_on() {
  13. local host=$1
  14. shift 1
  15. [ -z "$DEBUG" ] || greyly echo "Scope on $host: $*" >&2
  16. DOCKER_HOST=tcp://$host:$DOCKER_PORT CHECKPOINT_DISABLE=true "$SCOPE" "$@"
  17. }
  18. weave_on() {
  19. local host=$1
  20. shift 1
  21. [ -z "$DEBUG" ] || greyly echo "Weave on $host: $*" >&2
  22. DOCKER_HOST=tcp://$host:$DOCKER_PORT CHECKPOINT_DISABLE=true "$WEAVE" "$@"
  23. }
  24. weave_proxy_on() {
  25. local host=$1
  26. shift 1
  27. [ -z "$DEBUG" ] || greyly echo "Weave proxy on $host: $*" >&2
  28. DOCKER_PORT=12375 docker_on "$host" "$@"
  29. }
  30. server_on() {
  31. local host=$1
  32. local name=${2:-nginx}
  33. weave_proxy_on "$1" run -d --name "$name" nginx
  34. }
  35. client_on() {
  36. local host=$1
  37. local name=${2:-client}
  38. local server=${3:-nginx}
  39. weave_proxy_on "$1" run -d --name "$name" alpine /bin/sh -c "while true; do \
  40. wget http://$server.weave.local:80/ -O - >/dev/null || true; \
  41. sleep 1; \
  42. done"
  43. }
  44. scope_end_suite() {
  45. end_suite
  46. for host in $HOSTS; do
  47. docker_on "$host" rm -f "$(docker_on "$host" ps -a -q)" || true
  48. # Unfortunately, "docker rm" might not work: the CircleCI's Docker
  49. # client is unable to delete containers on GCE's Docker server. As a
  50. # workaround, restart the Docker daemon: at least the containers from
  51. # previous tests will not be running.
  52. run_on "$host" "sudo service docker restart"
  53. done
  54. }
  55. list_containers() {
  56. local host=$1
  57. echo "Listing containers on ${host}:"
  58. curl -s "http://${host}:4040/api/topology/containers?system=show" | jq -r '.nodes[] | select(has("metadata")) | { "image": .metadata[] | select(.id == "docker_image_name") | .value, "label": .label, "id": .id} | .id + " (" + .image + ", " + .label + ")"'
  59. echo
  60. }
  61. list_connections() {
  62. local host=$1
  63. echo "Listing connections on ${host}:"
  64. curl -s "http://${host}:4040/api/topology/containers?system=show" | jq -r '.nodes[] | select(has("adjacency")) | { "from_name": .label, "from_id": .id, "to": .adjacency[]} | .from_id + " (" + .from_name+ ") -> " + .to'
  65. echo
  66. }
  67. # this checks we have a named node in the given view
  68. has() {
  69. local view=$1
  70. local host=$2
  71. local name=$3
  72. local count=${4:-1}
  73. assert "curl -s http://${host}:4040/api/topology/${view}?system=show | jq -r '[.nodes[] | select(.label == \"${name}\")] | length'" "$count"
  74. }
  75. # this checks we have a named container
  76. has_container() {
  77. has containers "$@"
  78. }
  79. node_id() {
  80. local view="$1"
  81. local host="$2"
  82. local name="$3"
  83. curl -s "http://${host}:4040/api/topology/${view}?system=show" | jq -r ".nodes[] | select(.label == \"${name}\") | .id"
  84. }
  85. container_id() {
  86. node_id containers "$@"
  87. }
  88. # this checks we have an edge from container 1 to container 2
  89. has_connection_by_id() {
  90. local view="$1"
  91. local host="$2"
  92. local from_id="$3"
  93. local to_id="$4"
  94. local timeout="${5:-60}"
  95. local max_edges="$6:10"
  96. for i in $(seq "$timeout"); do
  97. local nodes
  98. local edge
  99. nodes=$(curl -s "http://$host:4040/api/topology/${view}?system=show" || true)
  100. edge=$(echo "$nodes" | (jq -r ".nodes[\"$from_id\"].adjacency | contains([\"$to_id\"])" || true) 2>/dev/null)
  101. if [ "$edge" = "true" ]; then
  102. echo "Found edge $from -> $to after $i secs"
  103. count=$(echo "$nodes" | jq -r ".nodes[\"$from_id\"].adjacency | length" 2>/dev/null)
  104. assert "[ $count -le $max_edges ]"
  105. return
  106. fi
  107. sleep 1
  108. done
  109. echo "Failed to find edge $from -> $to after $timeout secs"
  110. assert "curl -s http://$host:4040/api/topology/${view}?system=show | jq -r '.nodes[\"$from_id\"].adjacency | contains([\"$to_id\"])'" true
  111. }
  112. has_connection() {
  113. local view="$1"
  114. local host="$2"
  115. local from="$3"
  116. local to="$4"
  117. local timeout="${5:-60}"
  118. local from_id
  119. local to_id
  120. from_id="$(node_id "${view}" "${host}" "${from}")"
  121. to_id="$(node_id "${view}" "${host}" "${to}")"
  122. has_connection_by_id "${view}" "${host}" "${from_id}" "${to_id}" "${timeout}"
  123. }
  124. wait_for() {
  125. local view="$1"
  126. local host="$2"
  127. local timeout="$3"
  128. shift 3
  129. for i in $(seq "${timeout}"); do
  130. local nodes
  131. local found=0
  132. nodes=$(curl -s "http://$host:4040/api/topology/${view}?system=show" || true)
  133. for name in "$@"; do
  134. local count
  135. count=$(echo "${nodes}" | jq -r "[.nodes[] | select(.label == \"${name}\")] | length")
  136. if [ -n "${count}" ] && [ "${count}" -ge 1 ]; then
  137. found=$((found + 1))
  138. fi
  139. done
  140. if [ "${found}" -eq $# ]; then
  141. echo "Found ${found} nodes after $i secs"
  142. return
  143. fi
  144. sleep 1
  145. done
  146. echo "Failed to find nodes $* after $i secs"
  147. }
  148. wait_for_containers() {
  149. wait_for containers "$@"
  150. }