functional-test-k8s.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/bin/bash
  2. ETCD_IMG="${ETCD_IMG:-quay.io/coreos/etcd:v3.2.7}"
  3. ETCD_LOCATION="${ETCD_LOCATION:-etcd}"
  4. FLANNEL_NET="${FLANNEL_NET:-10.10.0.0/16}"
  5. TAG=`git describe --tags --dirty`
  6. FLANNEL_DOCKER_IMAGE="${FLANNEL_DOCKER_IMAGE:-quay.io/coreos/flannel:$TAG}"
  7. K8S_VERSION="${K8S_VERSION:-1.7.6}"
  8. docker_ip=$(ip -o -f inet addr show docker0 | grep -Po 'inet \K[\d.]+')
  9. etcd_endpt="http://$docker_ip:2379"
  10. k8s_endpt="http://$docker_ip:8080"
  11. setup_suite() {
  12. # Run etcd, killing any existing one that was running
  13. # Start etcd
  14. docker rm -f flannel-e2e-test-etcd >/dev/null 2>/dev/null
  15. docker run --name=flannel-e2e-test-etcd -d -p 2379:2379 $ETCD_IMG etcd --listen-client-urls http://0.0.0.0:2379 --advertise-client-urls $etcd_endpt >/dev/null
  16. sleep 1
  17. # Start a kubernetes API server
  18. docker rm -f flannel-e2e-k8s-apiserver >/dev/null 2>/dev/null
  19. docker run -d --net=host --name flannel-e2e-k8s-apiserver gcr.io/google_containers/hyperkube-amd64:v$K8S_VERSION \
  20. /hyperkube apiserver --etcd-servers=$etcd_endpt \
  21. --service-cluster-ip-range=10.101.0.0/16 --insecure-bind-address=0.0.0.0 --allow-privileged >/dev/null
  22. sleep 1
  23. while ! cat <<EOF | docker run -i --rm --net=host gcr.io/google_containers/hyperkube-amd64:v$K8S_VERSION /hyperkube kubectl create -f - >/dev/null 2>/dev/null
  24. apiVersion: v1
  25. kind: Node
  26. metadata:
  27. name: flannel1
  28. annotations:
  29. dummy: value
  30. spec:
  31. podCIDR: 10.10.1.0/24
  32. EOF
  33. do
  34. sleep 1
  35. done
  36. cat <<EOF | docker run -i --rm --net=host gcr.io/google_containers/hyperkube-amd64:v$K8S_VERSION /hyperkube kubectl create -f - >/dev/null 2>/dev/null
  37. apiVersion: v1
  38. kind: Node
  39. metadata:
  40. name: flannel2
  41. annotations:
  42. dummy: value
  43. spec:
  44. podCIDR: 10.10.2.0/24
  45. EOF
  46. }
  47. teardown_suite() {
  48. # Teardown the etcd server
  49. docker rm -f flannel-e2e-test-etcd >/dev/null
  50. docker rm -f flannel-e2e-k8s-apiserver >/dev/null
  51. }
  52. teardown() {
  53. docker rm -f flannel-e2e-test-flannel1 >/dev/null 2>/dev/null
  54. docker rm -f flannel-e2e-test-flannel2 >/dev/null 2>/dev/null
  55. }
  56. start_flannel() {
  57. local backend=$1
  58. flannel_conf="{ \"Network\": \"$FLANNEL_NET\", \"Backend\": { \"Type\": \"${backend}\" } }"
  59. for host_num in 1 2; do
  60. docker rm -f flannel-e2e-test-flannel$host_num >/dev/null 2>/dev/null
  61. docker run -e NODE_NAME=flannel$host_num --privileged --name flannel-e2e-test-flannel$host_num -tid --entrypoint /bin/sh $FLANNEL_DOCKER_IMAGE >/dev/null
  62. docker exec flannel-e2e-test-flannel$host_num /bin/sh -c 'mkdir -p /etc/kube-flannel'
  63. echo $flannel_conf | docker exec -i flannel-e2e-test-flannel$host_num /bin/sh -c 'cat > /etc/kube-flannel/net-conf.json'
  64. docker exec -d flannel-e2e-test-flannel$host_num /opt/bin/flanneld --kube-subnet-mgr --kube-api-url $k8s_endpt
  65. done
  66. }
  67. create_ping_dest() {
  68. # add a dummy interface with $FLANNEL_SUBNET so we have a known working IP to ping
  69. for host_num in 1 2; do
  70. while ! docker exec flannel-e2e-test-flannel$host_num ls /run/flannel/subnet.env >/dev/null 2>&1; do
  71. sleep 0.1
  72. done
  73. # Use declare to allow the host_num variable to be part of the ping_dest variable name. -g is needed to make it global
  74. declare -g ping_dest$host_num=$(docker "exec" --privileged flannel-e2e-test-flannel$host_num /bin/sh -c '\
  75. source /run/flannel/subnet.env && \
  76. ip link add name dummy0 type dummy && \
  77. ip addr add $FLANNEL_SUBNET dev dummy0 && ip link set dummy0 up && \
  78. echo $FLANNEL_SUBNET | cut -f 1 -d "/" ')
  79. done
  80. }
  81. test_vxlan() {
  82. start_flannel vxlan
  83. create_ping_dest # creates ping_dest1 and ping_dest2 variables
  84. pings
  85. }
  86. test_udp() {
  87. start_flannel udp
  88. create_ping_dest # creates ping_dest1 and ping_dest2 variables
  89. pings
  90. }
  91. test_host-gw() {
  92. start_flannel host-gw
  93. create_ping_dest # creates ping_dest1 and ping_dest2 variables
  94. pings
  95. }
  96. pings() {
  97. # ping in both directions
  98. assert "docker exec -it --privileged flannel-e2e-test-flannel1 /bin/ping -c 5 $ping_dest2" "Host 1 cannot ping host 2"
  99. assert "docker exec -it --privileged flannel-e2e-test-flannel2 /bin/ping -c 5 $ping_dest1" "Host 2 cannot ping host 1"
  100. }
  101. test_manifest() {
  102. # This just tests that the API server accepts the manifest, not that it actually acts on it correctly.
  103. assert "cat ../Documentation/kube-flannel.yml | docker run -i --rm --net=host gcr.io/google_containers/hyperkube-amd64:v$K8S_VERSION /hyperkube kubectl create -f -"
  104. }