functional-test.sh 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #!/bin/bash
  2. ARCH="${ARCH:-amd64}"
  3. ETCD_IMG="${ETCD_IMG:-quay.io/coreos/etcd:v3.2.7}"
  4. # etcd might take a bit to come up - use a known etcd version so we know we have etcdctl available
  5. ETCDCTL_IMG="quay.io/coreos/etcd:v3.2.7"
  6. ETCD_LOCATION="${ETCD_LOCATION:-etcd}"
  7. FLANNEL_NET="${FLANNEL_NET:-10.10.0.0/16}"
  8. TAG=`git describe --tags --dirty`
  9. FLANNEL_DOCKER_IMAGE="${FLANNEL_DOCKER_IMAGE:-quay.io/coreos/flannel:$TAG}"
  10. # Set the proper imagename according to architecture
  11. if [[ ${ARCH} == "ppc64le" ]]; then
  12. ETCD_IMG+="-ppc64le"
  13. ETCDCTL_IMG+="-ppc64le"
  14. elif [[ ${ARCH} == "arm64" ]]; then
  15. ETCD_IMG+="-arm64"
  16. ETCDCTL_IMG+="-arm64"
  17. fi
  18. setup_suite() {
  19. # Run etcd, killing any existing one that was running
  20. docker_ip=$(ip -o -f inet addr show docker0 | grep -Po 'inet \K[\d.]+')
  21. etcd_endpt="http://$docker_ip:2379"
  22. # Start etcd
  23. docker rm -f flannel-e2e-test-etcd >/dev/null 2>/dev/null
  24. docker run --name=flannel-e2e-test-etcd -d -p 2379:2379 $ETCD_IMG $ETCD_LOCATION --listen-client-urls http://0.0.0.0:2379 --advertise-client-urls $etcd_endpt >/dev/null
  25. }
  26. teardown_suite() {
  27. # Teardown the etcd server
  28. docker rm -f flannel-e2e-test-etcd >/dev/null
  29. }
  30. setup() {
  31. # rm any old flannel container that maybe running, ignore error as it might not exist
  32. docker rm -f flannel-e2e-test-flannel1 >/dev/null 2>/dev/null
  33. assert "docker run --name=flannel-e2e-test-flannel1 -d --privileged $FLANNEL_DOCKER_IMAGE --etcd-endpoints=$etcd_endpt -v 10 >/dev/null"
  34. # rm any old flannel container that maybe running, ignore error as it might not exist
  35. docker rm -f flannel-e2e-test-flannel2 >/dev/null 2>/dev/null
  36. assert "docker run --name=flannel-e2e-test-flannel2 -d --privileged $FLANNEL_DOCKER_IMAGE --etcd-endpoints=$etcd_endpt -v 10 >/dev/null"
  37. }
  38. teardown() {
  39. docker rm -f flannel-e2e-test-flannel1 flannel-e2e-test-flannel2 flannel-e2e-test-flannel1-iperf flannel-host1 flannel-host2 > /dev/null 2>&1
  40. }
  41. write_config_etcd() {
  42. local backend=$1
  43. if [ -e "$backend" ]; then
  44. echo "Reading custom conf from $backend"
  45. flannel_conf=`cat "$backend"`
  46. else
  47. flannel_conf="{ \"Network\": \"$FLANNEL_NET\", \"Backend\": { \"Type\": \"${backend}\" } }"
  48. fi
  49. while ! docker run --rm $ETCDCTL_IMG etcdctl --endpoints=$etcd_endpt set /coreos.com/network/config "$flannel_conf" >/dev/null
  50. do
  51. sleep 0.1
  52. done
  53. }
  54. create_ping_dest() {
  55. # add a dummy interface with $FLANNEL_SUBNET so we have a known working IP to ping
  56. for host_num in 1 2; do
  57. while ! docker exec flannel-e2e-test-flannel$host_num ls /run/flannel/subnet.env >/dev/null 2>&1; do
  58. sleep 0.1
  59. done
  60. # Use declare to allow the host_num variable to be part of the ping_dest variable name. -g is needed to make it global
  61. declare -g ping_dest$host_num=$(docker "exec" --privileged flannel-e2e-test-flannel$host_num /bin/sh -c '\
  62. source /run/flannel/subnet.env && \
  63. ip link add name dummy0 type dummy && \
  64. ip addr add $FLANNEL_SUBNET dev dummy0 && ip link set dummy0 up && \
  65. echo $FLANNEL_SUBNET | cut -f 1 -d "/" ')
  66. done
  67. }
  68. #test_wireguard_ping() {
  69. # write_config_etcd extension-wireguard
  70. # create_ping_dest # creates ping_dest1 and ping_dest2 variables
  71. # pings
  72. #}
  73. test_vxlan_ping() {
  74. write_config_etcd vxlan
  75. create_ping_dest # creates ping_dest1 and ping_dest2 variables
  76. pings
  77. }
  78. if [[ ${ARCH} == "amd64" ]]; then
  79. test_udp_ping() {
  80. write_config_etcd udp
  81. create_ping_dest # creates ping_dest1 and ping_dest2 variables
  82. pings
  83. }
  84. fi
  85. test_hostgw_ping() {
  86. write_config_etcd host-gw
  87. create_ping_dest # creates ping_dest1 and ping_dest2 variables
  88. pings
  89. }
  90. test_ipip_ping() {
  91. write_config_etcd ipip
  92. create_ping_dest # creates ping_dest1 and ping_dest2 variables
  93. pings
  94. }
  95. test_ipsec_ping() {
  96. write_config_etcd ipsec
  97. create_ping_dest # creates ping_dest1 and ping_dest2 variables
  98. pings
  99. }
  100. pings() {
  101. # ping in both directions
  102. assert "docker exec --privileged flannel-e2e-test-flannel1 /bin/ping -I $ping_dest1 -c 3 $ping_dest2" "Host 1 cannot ping host 2"
  103. assert "docker exec --privileged flannel-e2e-test-flannel2 /bin/ping -I $ping_dest2 -c 3 $ping_dest1" "Host 2 cannot ping host 1"
  104. }
  105. # These perf tests don't actually assert on anything
  106. test_hostgw_perf() {
  107. write_config_etcd host-gw
  108. create_ping_dest
  109. perf
  110. }
  111. test_vxlan_perf() {
  112. write_config_etcd vxlan
  113. create_ping_dest
  114. perf
  115. }
  116. if [[ ${ARCH} == "amd64" ]]; then
  117. test_udp_perf() {
  118. write_config_etcd udp
  119. create_ping_dest
  120. perf
  121. }
  122. fi
  123. test_ipip_perf() {
  124. write_config_etcd ipip
  125. create_ping_dest
  126. perf
  127. }
  128. test_ipsec_perf() {
  129. write_config_etcd ipsec
  130. create_ping_dest
  131. perf
  132. }
  133. #test_wireguard_perf() {
  134. # write_config_etcd extension-wireguard
  135. # create_ping_dest
  136. # perf
  137. #}
  138. perf() {
  139. # Perf test - run iperf server on flannel1 and client on flannel2
  140. docker rm -f flannel-e2e-test-flannel1-iperf 2>/dev/null
  141. docker run -d --name flannel-e2e-test-flannel1-iperf --net=container:flannel-e2e-test-flannel1 iperf3:latest >/dev/null
  142. docker run --rm --net=container:flannel-e2e-test-flannel2 iperf3:latest -c $ping_dest1 -B $ping_dest2
  143. }
  144. test_multi() {
  145. flannel_conf_vxlan='{"Network": "10.11.0.0/16", "Backend": {"Type": "vxlan"}}'
  146. flannel_conf_host_gw='{"Network": "10.12.0.0/16", "Backend": {"Type": "host-gw"}}'
  147. while ! docker run --rm $ETCD_IMG etcdctl --endpoints=$etcd_endpt set /vxlan/network/config "$flannel_conf_vxlan" >/dev/null
  148. do
  149. sleep 0.1
  150. done
  151. while ! docker run --rm $ETCD_IMG etcdctl --endpoints=$etcd_endpt set /hostgw/network/config "$flannel_conf_host_gw" >/dev/null
  152. do
  153. sleep 0.1
  154. done
  155. for host in 1 2; do
  156. # rm any old flannel container, ignore error as it might not exist
  157. docker rm -f flannel-host$host 2>/dev/null >/dev/null
  158. # Start the hosts
  159. docker run --name=flannel-host$host -id --privileged --entrypoint /bin/sh $FLANNEL_DOCKER_IMAGE >/dev/null
  160. # Start two flanneld instances
  161. docker exec -d flannel-host$host sh -c "/opt/bin/flanneld -v 10 -subnet-file /vxlan.env -etcd-prefix=/vxlan/network --etcd-endpoints=$etcd_endpt 2>vxlan.log"
  162. docker exec -d flannel-host$host sh -c "/opt/bin/flanneld -v 10 -subnet-file /hostgw.env -etcd-prefix=/hostgw/network --etcd-endpoints=$etcd_endpt 2>hostgw.log"
  163. done
  164. for host in 1 2; do
  165. for backend_type in vxlan hostgw; do
  166. while ! docker exec flannel-host$host ls /$backend_type.env >/dev/null 2>&1; do
  167. sleep 0.1
  168. done
  169. done
  170. done
  171. # add dummy interface on host1 only so we have a known working IP to ping then ping it from host2
  172. vxlan_ping_dest=$(docker exec flannel-host1 /bin/sh -c '\
  173. source /vxlan.env &&
  174. ip link add name dummy_vxlan type dummy && \
  175. ip addr add $FLANNEL_SUBNET dev dummy_vxlan && \
  176. ip link set dummy_vxlan up && \
  177. echo $FLANNEL_SUBNET | cut -f 1 -d "/" ')
  178. hostgw_ping_dest=$(docker exec flannel-host1 /bin/sh -c '\
  179. source /hostgw.env &&
  180. ip link add name dummy_hostgw type dummy && \
  181. ip addr add $FLANNEL_SUBNET dev dummy_hostgw && \
  182. ip link set dummy_hostgw up && \
  183. echo $FLANNEL_SUBNET | cut -f 1 -d "/" ')
  184. # Send some pings from host2. Make sure we can send traffic over vxlan or directly.
  185. # If a particular (wrong) interface is forced then pings should fail
  186. assert "docker exec flannel-host2 ping -c 3 $hostgw_ping_dest"
  187. assert "docker exec flannel-host2 ping -c 3 $vxlan_ping_dest"
  188. assert_fails "docker exec flannel-host2 ping -W 1 -c 1 -I flannel.1 $hostgw_ping_dest"
  189. assert_fails "docker exec flannel-host2 ping -W 1 -c 1 -I eth0 $vxlan_ping_dest"
  190. }