|
| 1 | +#! /bin/bash |
| 2 | + |
| 3 | +. "$(dirname "$0")/config.sh" |
| 4 | + |
| 5 | +start_suite "Test CNI plugin with IP address assignment" |
| 6 | + |
| 7 | +cni_connect() { |
| 8 | + pid=$(container_pid $1 $2) |
| 9 | + id=$(docker_on $1 inspect -f '{{.Id}}' $2) |
| 10 | + run_on $1 sudo CNI_COMMAND=ADD CNI_CONTAINERID=$id CNI_IFNAME=eth0 \ |
| 11 | + CNI_NETNS=/proc/$pid/ns/net CNI_PATH=/opt/cni/bin /opt/cni/bin/weave-net |
| 12 | +} |
| 13 | + |
| 14 | +run_on $HOST1 sudo mkdir -p /opt/cni/bin |
| 15 | +# setup-cni is a subset of 'weave setup', without doing any 'docker pull's |
| 16 | +weave_on $HOST1 setup-cni |
| 17 | +weave_on $HOST1 launch |
| 18 | + |
| 19 | +C0=$(docker_on $HOST1 run --net=none --name=c0 -dt $SMALL_IMAGE /bin/sh) |
| 20 | +C1=$(docker_on $HOST1 run --net=none --privileged --name=c1 -dt $SMALL_IMAGE /bin/sh) |
| 21 | +C2=$(docker_on $HOST1 run --net=none --name=c2 -dt $SMALL_IMAGE /bin/sh) |
| 22 | +CH0=$(docker_on $HOST1 run --net=none --name=ch0 -dt $SMALL_IMAGE /bin/sh) |
| 23 | +CH1=$(docker_on $HOST1 run --net=none --name=ch1 -dt $SMALL_IMAGE /bin/sh) |
| 24 | +CH2=$(docker_on $HOST1 run --net=none --name=ch2 -dt $SMALL_IMAGE /bin/sh) |
| 25 | + |
| 26 | + |
| 27 | +# Enable unsolicited ARPs so that ping after the address reuse does not time out |
| 28 | +exec_on $HOST1 c1 sysctl -w net.ipv4.conf.all.arp_accept=1 |
| 29 | + |
| 30 | +# Contrived example to trigger the bug in #2839 |
| 31 | +cni_connect $HOST1 c0 <<EOF |
| 32 | +{ |
| 33 | + "name": "weave", |
| 34 | + "type": "weave-net", |
| 35 | + "ipam": { |
| 36 | + "type": "weave-ipam" |
| 37 | + } |
| 38 | +} |
| 39 | +EOF |
| 40 | + |
| 41 | +cni_connect $HOST1 c1 <<EOF |
| 42 | +{ |
| 43 | + "name": "weave", |
| 44 | + "type": "weave-net" |
| 45 | +} |
| 46 | +EOF |
| 47 | + |
| 48 | +cni_connect $HOST1 c2 <<EOF |
| 49 | +{ |
| 50 | + "cniVersion": "0.3.0", |
| 51 | + "name": "weave", |
| 52 | + "type": "weave-net", |
| 53 | + "ipam": { |
| 54 | + "type": "weave-ipam", |
| 55 | + "routes": [ { "dst": "10.32.0.0/12" } ] |
| 56 | + } |
| 57 | +} |
| 58 | +EOF |
| 59 | + |
| 60 | +# examples to test hairpin |
| 61 | +cni_connect $HOST1 ch0 <<EOF |
| 62 | +{ |
| 63 | + "name": "weave", |
| 64 | + "type": "weave-net", |
| 65 | + "hairpinMode": true |
| 66 | +} |
| 67 | +EOF |
| 68 | +cni_connect $HOST1 ch1 <<EOF |
| 69 | +{ |
| 70 | + "name": "weave", |
| 71 | + "type": "weave-net", |
| 72 | + "hairpinMode": false |
| 73 | +} |
| 74 | +EOF |
| 75 | +cni_connect $HOST1 ch2 <<EOF |
| 76 | +{ |
| 77 | + "name": "weave", |
| 78 | + "type": "weave-net" |
| 79 | +} |
| 80 | +EOF |
| 81 | + |
| 82 | + |
| 83 | +C0IP=$(container_ip $HOST1 c0) |
| 84 | +C1IP=$(container_ip $HOST1 c1) |
| 85 | +C2IP=$(container_ip $HOST1 c2) |
| 86 | + |
| 87 | +# Check the bridge IP is different from the container IPs |
| 88 | +BRIP=$(container_ip $HOST1 weave:expose) |
| 89 | +assert_raises "[ $BRIP != $C0IP ]" |
| 90 | +assert_raises "[ $BRIP != $C1IP ]" |
| 91 | +assert_raises "[ $BRIP != $C2IP ]" |
| 92 | + |
| 93 | +assert_raises "exec_on $HOST1 c1 $PING $C2IP" |
| 94 | +assert_raises "exec_on $HOST1 c2 $PING $C1IP" |
| 95 | +# Check if the route to the outside world works |
| 96 | +assert_raises "exec_on $HOST1 c1 $PING 8.8.8.8" |
| 97 | +# Container c2 should not have a default route to the world |
| 98 | +assert_raises "exec_on $HOST1 c2 sh -c '! $PING 8.8.8.8'" |
| 99 | + |
| 100 | +# Now remove and start a new container to see if anything breaks |
| 101 | +docker_on $HOST1 rm -f c2 |
| 102 | + |
| 103 | +C3=$(docker_on $HOST1 run --net=none --name=c3 -dt $SMALL_IMAGE /bin/sh) |
| 104 | + |
| 105 | +cni_connect $HOST1 c3 <<EOF |
| 106 | +{ "name": "weave", "type": "weave-net" } |
| 107 | +EOF |
| 108 | + |
| 109 | +C3IP=$(container_ip $HOST1 c3) |
| 110 | + |
| 111 | +# CNI shouldn't re-use the address until we call DEL |
| 112 | +assert_raises "[ $C2IP != $C3IP ]" |
| 113 | +assert_raises "[ $BRIP != $C3IP ]" |
| 114 | +assert_raises "exec_on $HOST1 c1 $PING $C3IP" |
| 115 | + |
| 116 | +##### |
| 117 | +# |
| 118 | +# Hairpin mode tests |
| 119 | +# |
| 120 | +# here we check that the bridge side of the veth is set to hairpin mode (true or false) correctly |
| 121 | +# check for each of |
| 122 | +# - ch0 explicitly true |
| 123 | +# - ch1 explicitly false |
| 124 | +# - ch2 unset, which should default to true |
| 125 | +# |
| 126 | +# we take advantage of the veth name being constructed from the container ID) |
| 127 | +# |
| 128 | +# should this reliance ever fail and we need to get it from the system, do the following for each: |
| 129 | +# CH0_PEER_ID=$(exec_on $HOST1 ch0 cat /sys/class/net/eth0/iflink) |
| 130 | +# CH0_PEER=$($SSH $HOST1 ip link show | awk "/^${CH0_PEER_ID}:/"' {print $2}' | sed 's/@.*$//g' ) |
| 131 | +# CH0_HAIRPIN=$($SSH $HOST1 cat /sys/devices/virtual/net/weave/brif/$CH0_PEER/hairpin_mode) |
| 132 | +# assert_raises "[ $CH0_HAIRPIN == 1 ]" |
| 133 | +# |
| 134 | +assert "$SSH $HOST1 cat /sys/devices/virtual/net/weave/brif/vethwepl${CH0:0:7}/hairpin_mode" "1" |
| 135 | +assert "$SSH $HOST1 cat /sys/devices/virtual/net/weave/brif/vethwepl${CH1:0:7}/hairpin_mode" "0" |
| 136 | +assert "$SSH $HOST1 cat /sys/devices/virtual/net/weave/brif/vethwepl${CH2:0:7}/hairpin_mode" "1" |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | +# Ensure existing containers can reclaim their IP addresses after CNI has been used -- see #2548 |
| 141 | +stop_weave_on $HOST1 |
| 142 | + |
| 143 | +# Ensure no warning is printed to the standard error: |
| 144 | +ACTUAL_OUTPUT=$(CHECKPOINT_DISABLE="$CHECKPOINT_DISABLE" DOCKER_HOST=tcp://$HOST1:$DOCKER_PORT $WEAVE launch 2>&1) |
| 145 | +EXPECTED_OUTPUT=$($SSH $HOST1 docker inspect --format="{{.Id}}" weave) |
| 146 | + |
| 147 | +assert_raises "[ $EXPECTED_OUTPUT == $ACTUAL_OUTPUT ]" |
| 148 | + |
| 149 | +assert "$SSH $HOST1 \"curl -s -X GET 127.0.0.1:6784/ip/$C1 | grep -o -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'\"" "$C1IP" |
| 150 | +assert "$SSH $HOST1 \"curl -s -X GET 127.0.0.1:6784/ip/$C3 | grep -o -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'\"" "$C3IP" |
| 151 | + |
| 152 | + |
| 153 | +end_suite |
0 commit comments