github.com/rumpl/bof@v23.0.0-rc.2+incompatible/libnetwork/test/integration/dnet/helpers.bash (about)

     1  function get_docker_bridge_ip() {
     2      echo $(docker run --rm -it busybox ip route show | grep default | cut -d" " -f3)
     3  }
     4  
     5  function inst_id2port() {
     6      echo $((41000+${1}-1))
     7  }
     8  
     9  function dnet_container_name() {
    10      echo dnet-$1-$2
    11  }
    12  
    13  function dnet_container_ip() {
    14      docker inspect --format '{{.NetworkSettings.IPAddress}}' dnet-$1-$2
    15  }
    16  
    17  function get_sbox_id() {
    18      local line
    19  
    20      line=$(dnet_cmd $(inst_id2port ${1}) service ls | grep ${2})
    21      echo ${line} | cut -d" " -f5
    22  }
    23  
    24  function net_connect() {
    25  	local al gl
    26  	if [ -n "$4" ]; then
    27  	    if [ "${4}" != ":" ]; then
    28  		al="--alias=${4}"
    29  	    fi
    30  	fi
    31  	if [ -n "$5" ]; then
    32  	    gl="--alias=${5}"
    33  	fi
    34  	dnet_cmd $(inst_id2port ${1}) service publish $gl ${2}.${3}
    35  	dnet_cmd $(inst_id2port ${1}) service attach $al ${2} ${2}.${3}
    36  }
    37  
    38  function net_disconnect() {
    39  	dnet_cmd $(inst_id2port ${1}) service detach ${2} ${2}.${3}
    40  	dnet_cmd $(inst_id2port ${1}) service unpublish ${2}.${3}
    41  }
    42  
    43  hrun() {
    44      local e E T oldIFS
    45      [[ ! "$-" =~ e ]] || e=1
    46      [[ ! "$-" =~ E ]] || E=1
    47      [[ ! "$-" =~ T ]] || T=1
    48      set +e
    49      set +E
    50      set +T
    51      output="$("$@" 2>&1)"
    52      status="$?"
    53      oldIFS=$IFS
    54      IFS=$'\n' lines=($output)
    55      [ -z "$e" ] || set -e
    56      [ -z "$E" ] || set -E
    57      [ -z "$T" ] || set -T
    58      IFS=$oldIFS
    59  }
    60  
    61  function wait_for_dnet() {
    62      local hport
    63  
    64      hport=$1
    65      echo "waiting on dnet to come up ..."
    66      for i in `seq 1 10`;
    67      do
    68  	hrun ./bin/dnet -H tcp://127.0.0.1:${hport} network ls
    69  	echo ${output}
    70  	if [ "$status" -eq 0 ]; then
    71  	    return
    72  	fi
    73  
    74  	if [[ "${lines[1]}" =~ .*EOF.* ]]
    75  	then
    76  	    docker logs ${2}
    77  	fi
    78  	echo "still waiting after ${i} seconds"
    79  	sleep 1
    80      done
    81  }
    82  
    83  function parse_discovery_str() {
    84      local d provider address
    85      discovery=$1
    86      provider=$(echo ${discovery} | cut -d":" -f1)
    87      address=$(echo ${discovery} | cut -d":" -f2):$(echo ${discovery} | cut -d":" -f3)
    88      address=${address:2}
    89      echo "${discovery} ${provider} ${address}"
    90  }
    91  
    92  function start_dnet() {
    93      local inst suffix name hport cport hopt store bridge_ip labels tomlfile nip
    94      local discovery provider address
    95  
    96      inst=$1
    97      shift
    98      suffix=$1
    99      shift
   100  
   101      store=$(echo $suffix | cut -d":" -f1)
   102      nip=$(echo $suffix | cut -s -d":" -f2)
   103  
   104  
   105      stop_dnet ${inst} ${store}
   106      name=$(dnet_container_name ${inst} ${store})
   107  
   108      hport=$((41000+${inst}-1))
   109      cport=2385
   110      hopt=""
   111  
   112      while [ -n "$1" ]
   113      do
   114  	if [[ "$1" =~ ^[0-9]+$ ]]
   115  	then
   116  	    hport=$1
   117  	    cport=$1
   118  	    hopt="-H tcp://0.0.0.0:${cport}"
   119  	else
   120  	    store=$1
   121  	fi
   122  	shift
   123      done
   124  
   125      bridge_ip=$(get_docker_bridge_ip)
   126  
   127      echo "start_dnet parsed values: " ${inst} ${suffix} ${name} ${hport} ${cport} ${hopt} ${store}
   128  
   129      mkdir -p /tmp/dnet/${name}
   130      tomlfile="/tmp/dnet/${name}/libnetwork.toml"
   131  
   132      # Try discovery URLs with or without path
   133      neigh_ip=""
   134      neighbors=""
   135  	if [ "$nip" != "" ]; then
   136  	    neighbors=${nip}
   137  	fi
   138  
   139  	discovery=""
   140  	provider=""
   141  	address=""
   142  
   143      if [ "$discovery" != "" ]; then
   144  	cat > ${tomlfile} <<EOF
   145  title = "LibNetwork Configuration file for ${name}"
   146  
   147  [daemon]
   148    debug = false
   149  [cluster]
   150    discovery = "${discovery}"
   151    Heartbeat = 10
   152  [scopes]
   153    [scopes.global]
   154      [scopes.global.client]
   155        provider = "${provider}"
   156        address = "${address}"
   157  EOF
   158      else
   159  	cat > ${tomlfile} <<EOF
   160  title = "LibNetwork Configuration file for ${name}"
   161  
   162  [daemon]
   163    debug = false
   164  [orchestration]
   165    agent = true
   166    bind = "eth0"
   167    peer = "${neighbors}"
   168  EOF
   169      fi
   170  
   171      cat ${tomlfile}
   172      docker run \
   173  	   -d \
   174  	   --hostname=$(echo ${name} | sed s/_/-/g) \
   175  	   --name=${name}  \
   176  	   --privileged \
   177  	   -p ${hport}:${cport} \
   178  	   -e _OVERLAY_HOST_MODE \
   179  	   -v $(pwd)/:/go/src/github.com/docker/libnetwork \
   180  	   -v /tmp:/tmp \
   181  	   -v $(pwd)/${TMPC_ROOT}:/scratch \
   182  	   -v /usr/local/bin/runc:/usr/local/bin/runc \
   183  	   -w /go/src/github.com/docker/libnetwork \
   184  	   mrjana/golang ./bin/dnet -d -D ${hopt} -c ${tomlfile}
   185  
   186      wait_for_dnet $(inst_id2port ${inst}) ${name}
   187  }
   188  
   189  function start_ovrouter() {
   190      local name=${1}
   191      local parent=${2}
   192  
   193      docker run \
   194  	   -d \
   195  	   --name=${name} \
   196  	   --net=container:${parent} \
   197  	   --volumes-from ${parent} \
   198  	   -w /go/src/github.com/docker/libnetwork \
   199  	   mrjana/golang ./cmd/ovrouter/ovrouter eth0
   200  }
   201  
   202  function stop_dnet() {
   203      local name
   204  
   205      name=$(dnet_container_name $1 $2)
   206      rm -rf /tmp/dnet/${name} || true
   207      docker rm -f ${name} || true
   208  }
   209  
   210  function dnet_cmd() {
   211      local hport
   212  
   213      hport=$1
   214      shift
   215      ./bin/dnet -H tcp://127.0.0.1:${hport} $*
   216  }
   217  
   218  function dnet_exec() {
   219      docker exec -it ${1} bash -c "trap \"echo SIGHUP\" SIGHUP; $2"
   220  }
   221  
   222  function runc() {
   223      local dnet
   224  
   225      dnet=${1}
   226      shift
   227      dnet_exec ${dnet} "cp /var/lib/docker/network/files/${1}*/* /scratch/rootfs/etc"
   228      dnet_exec ${dnet} "mkdir -p /var/run/netns"
   229      dnet_exec ${dnet} "touch /var/run/netns/c && mount -o bind /var/run/docker/netns/${1} /var/run/netns/c"
   230      dnet_exec ${dnet} "ip netns exec c unshare -fmuip --mount-proc chroot \"/scratch/rootfs\" /bin/sh -c \"/bin/mount -t proc proc /proc && ${2}\""
   231      dnet_exec ${dnet} "umount /var/run/netns/c && rm /var/run/netns/c"
   232  }
   233  
   234  function runc_nofail() {
   235      local dnet
   236  
   237      dnet=${1}
   238      shift
   239      dnet_exec ${dnet} "cp /var/lib/docker/network/files/${1}*/* /scratch/rootfs/etc"
   240      dnet_exec ${dnet} "mkdir -p /var/run/netns"
   241      dnet_exec ${dnet} "touch /var/run/netns/c && mount -o bind /var/run/docker/netns/${1} /var/run/netns/c"
   242      set +e
   243      dnet_exec ${dnet} "ip netns exec c unshare -fmuip --mount-proc chroot \"/scratch/rootfs\" /bin/sh -c \"/bin/mount -t proc proc /proc && ${2}\""
   244      status="$?"
   245      set -e
   246      dnet_exec ${dnet} "umount /var/run/netns/c && rm /var/run/netns/c"
   247  }
   248  
   249  function test_overlay() {
   250      dnet_suffix=$1
   251  
   252      echo $(docker ps)
   253  
   254      start=1
   255      end=3
   256      # Setup overlay network and connect containers to it
   257      if [ -z "${2}" -o "${2}" != "skip_add" ]; then
   258  	if [ -z "${2}" -o "${2}" != "internal" ]; then
   259  	    dnet_cmd $(inst_id2port 1) network create -d overlay multihost
   260  	else
   261  	    dnet_cmd $(inst_id2port 1) network create -d overlay --internal multihost
   262  	fi
   263      fi
   264  
   265      for i in `seq ${start} ${end}`;
   266      do
   267  	dnet_cmd $(inst_id2port $i) container create container_${i}
   268  	net_connect ${i} container_${i} multihost
   269      done
   270  
   271      # Now test connectivity between all the containers using service names
   272      for i in `seq ${start} ${end}`;
   273      do
   274  	if [ -z "${2}" -o "${2}" != "internal" ]; then
   275  	    runc $(dnet_container_name $i $dnet_suffix) $(get_sbox_id ${i} container_${i}) \
   276  		"ping -c 1 www.google.com"
   277  	else
   278  	    default_route=`runc $(dnet_container_name $i $dnet_suffix) $(get_sbox_id ${i} container_${i}) "ip route | grep default"`
   279  	    [ "$default_route" = "" ]
   280  	fi
   281  	for j in `seq ${start} ${end}`;
   282  	do
   283  	    if [ "$i" -eq "$j" ]; then
   284  		continue
   285  	    fi
   286  	    runc $(dnet_container_name $i $dnet_suffix) $(get_sbox_id ${i} container_${i}) \
   287  		 "ping -c 1 container_$j"
   288  	done
   289      done
   290  
   291      # Setup bridge network and connect containers to it
   292      if [ -z "${2}" -o "${2}" != "skip_add" ]; then
   293  	if [ -z "${2}" -o "${2}" != "internal" ]; then
   294  	    dnet_cmd $(inst_id2port 1) network create -d bridge br1
   295  	    dnet_cmd $(inst_id2port 1) network create -d bridge br2
   296  	    net_connect ${start} container_${start} br1
   297  	    net_connect ${start} container_${start} br2
   298  
   299  	    # Make sure external connectivity works
   300  	    runc $(dnet_container_name ${start} $dnet_suffix) $(get_sbox_id ${start} container_${start}) \
   301  		"ping -c 1 www.google.com"
   302  	    net_disconnect ${start} container_${start} br1
   303  	    net_disconnect ${start} container_${start} br2
   304  
   305  	    # Make sure external connectivity works
   306  	    runc $(dnet_container_name ${start} $dnet_suffix) $(get_sbox_id ${start} container_${start}) \
   307  		"ping -c 1 www.google.com"
   308  	    dnet_cmd $(inst_id2port 1) network rm br1
   309  	    dnet_cmd $(inst_id2port 1) network rm br2
   310  
   311  	    # Disconnect from overlay network
   312  	    net_disconnect ${start} container_${start} multihost
   313  
   314  	    # Connect to overlay network again
   315  	    net_connect ${start} container_${start} multihost
   316  
   317  	    # Make sure external connectivity still works
   318  	    runc $(dnet_container_name ${start} $dnet_suffix) $(get_sbox_id ${start} container_${start}) \
   319  		"ping -c 1 www.google.com"
   320  	fi
   321      fi
   322  
   323      # Teardown the container connections and the network
   324      for i in `seq ${start} ${end}`;
   325      do
   326  	net_disconnect ${i} container_${i} multihost
   327  	dnet_cmd $(inst_id2port $i) container rm container_${i}
   328      done
   329  
   330      if [ -z "${2}" -o "${2}" != "skip_rm" ]; then
   331  	dnet_cmd $(inst_id2port 2) network rm multihost
   332      fi
   333  }
   334  
   335  function check_etchosts() {
   336      local dnet sbid retval
   337      dnet=${1}
   338      shift
   339      sbid=${1}
   340      shift
   341  
   342      retval="true"
   343  
   344      for i in $*;
   345      do
   346  	run runc ${dnet} ${sbid} "cat /etc/hosts"
   347  	if [ "$status" -ne 0 ]; then
   348  	    retval="${output}"
   349  	    break
   350  	fi
   351  
   352  	line=$(echo ${output} | grep ${i})
   353  	if [ "${line}" == "" ]; then
   354  	    retval="false"
   355  	fi
   356      done
   357  
   358      echo ${retval}
   359  }
   360  
   361  function test_overlay_singlehost() {
   362      dnet_suffix=$1
   363      shift
   364  
   365      echo $(docker ps)
   366  
   367      start=1
   368      end=3
   369      # Setup overlay network and connect containers to it
   370      dnet_cmd $(inst_id2port 1) network create -d overlay multihost
   371      for i in `seq ${start} ${end}`;
   372      do
   373  	dnet_cmd $(inst_id2port 1) container create container_${i}
   374  	net_connect 1 container_${i} multihost
   375      done
   376  
   377      # Now test connectivity between all the containers using service names
   378      for i in `seq ${start} ${end}`;
   379      do
   380  	for j in `seq ${start} ${end}`;
   381  	do
   382  	    if [ "$i" -eq "$j" ]; then
   383  		continue
   384  	    fi
   385  	    runc $(dnet_container_name 1 $dnet_suffix) $(get_sbox_id 1 container_${i}) \
   386  		 "ping -c 1 container_$j"
   387  	done
   388      done
   389  
   390      # Teardown the container connections and the network
   391      for i in `seq ${start} ${end}`;
   392      do
   393  	net_disconnect 1 container_${i} multihost
   394  	dnet_cmd $(inst_id2port 1) container rm container_${i}
   395      done
   396  
   397      dnet_cmd $(inst_id2port 1) network rm multihost
   398  }
   399  
   400  function test_overlay_hostmode() {
   401      dnet_suffix=$1
   402      shift
   403  
   404      echo $(docker ps)
   405  
   406      start=1
   407      end=2
   408      # Setup overlay network and connect containers to it
   409      dnet_cmd $(inst_id2port 1) network create -d overlay multihost1
   410      dnet_cmd $(inst_id2port 1) network create -d overlay multihost2
   411      dnet_cmd $(inst_id2port 1) network ls
   412  
   413      for i in `seq ${start} ${end}`;
   414      do
   415  	dnet_cmd $(inst_id2port 1) container create mh1_${i}
   416  	net_connect 1 mh1_${i} multihost1
   417      done
   418  
   419      for i in `seq ${start} ${end}`;
   420      do
   421  	dnet_cmd $(inst_id2port 1) container create mh2_${i}
   422  	net_connect 1 mh2_${i} multihost2
   423      done
   424  
   425      # Now test connectivity between all the containers using service names
   426      for i in `seq ${start} ${end}`;
   427      do
   428  	for j in `seq ${start} ${end}`;
   429  	do
   430  	    if [ "$i" -eq "$j" ]; then
   431  		continue
   432  	    fi
   433  
   434  	    # Find the IP addresses of the j containers on both networks
   435  	    hrun runc $(dnet_container_name 1 $dnet_suffix) $(get_sbox_id 1 mh1_${i}) "nslookup mh1_$j"
   436  	    mh1_j_ip=$(echo ${output} | awk '{print $11}')
   437  
   438  	    hrun runc $(dnet_container_name 1 $dnet_suffix) $(get_sbox_id 1 mh2_${i}) "nslookup mh2_$j"
   439  	    mh2_j_ip=$(echo ${output} | awk '{print $11}')
   440  
   441  	    # Ping the j containers in the same network and ensure they are successful
   442  	    runc $(dnet_container_name 1 $dnet_suffix) $(get_sbox_id 1 mh1_${i}) \
   443  		 "ping -c 1 mh1_$j"
   444  	    runc $(dnet_container_name 1 $dnet_suffix) $(get_sbox_id 1 mh2_${i}) \
   445  		 "ping -c 1 mh2_$j"
   446  
   447  	    # Try pinging j container IPs from the container in the other network and make sure that they are not successful
   448  	    runc_nofail $(dnet_container_name 1 $dnet_suffix) $(get_sbox_id 1 mh1_${i}) "ping -c 1 ${mh2_j_ip}"
   449  	    [ "${status}" -ne 0 ]
   450  
   451  	    runc_nofail $(dnet_container_name 1 $dnet_suffix) $(get_sbox_id 1 mh2_${i}) "ping -c 1 ${mh1_j_ip}"
   452  	    [ "${status}" -ne 0 ]
   453  
   454  	    # Try pinging the j container IPS from the host(dnet container in this case) and make syre that they are not successful
   455  	    hrun docker exec -it $(dnet_container_name 1 $dnet_suffix) "ping -c 1 ${mh1_j_ip}"
   456  	    [ "${status}" -ne 0 ]
   457  
   458  	    hrun docker exec -it $(dnet_container_name 1 $dnet_suffix) "ping -c 1 ${mh2_j_ip}"
   459  	    [ "${status}" -ne 0 ]
   460  	done
   461      done
   462  
   463      # Teardown the container connections and the network
   464      for i in `seq ${start} ${end}`;
   465      do
   466  	net_disconnect 1 mh1_${i} multihost1
   467  	dnet_cmd $(inst_id2port 1) container rm mh1_${i}
   468      done
   469  
   470      for i in `seq ${start} ${end}`;
   471      do
   472  	net_disconnect 1 mh2_${i} multihost2
   473  	dnet_cmd $(inst_id2port 1) container rm mh2_${i}
   474      done
   475  
   476      dnet_cmd $(inst_id2port 1) network rm multihost1
   477      dnet_cmd $(inst_id2port 1) network rm multihost2
   478  }