def __init__(self, name, start_calico=True, as_num=None): """ Create a container using an image made for docker-in-docker. Load saved images into it. """ self.name = name self.as_num = None pwd = sh.pwd().stdout.rstrip() docker.run("--privileged", "-v", pwd+":/code", "--name", self.name, "-tid", "jpetazzo/dind") # Since `calicoctl node` doesn't fix ipv6 forwarding and module loading, we must manually fix it self.calicoctl("checksystem --fix") self.ip = docker.inspect("--format", "{{ .NetworkSettings.IPAddress }}", self.name).stdout.rstrip() self.ip6 = docker.inspect("--format", "{{ .NetworkSettings.GlobalIPv6Address }}", self.name).stdout.rstrip() # Make sure docker is up docker_ps = partial(self.execute, "docker ps") retry_until_success(docker_ps, ex_class=ErrorReturnCode) self.execute("docker load --input /code/calico_containers/calico-node.tar") self.execute("docker load --input /code/calico_containers/busybox.tar") if start_calico: self.start_calico_node() self.assert_powerstrip_up()
def __init__(self, name, start_calico=True, dind=True): self.name = name self.dind = dind self.workloads = set() # This variable is used to assert on destruction that this object was # cleaned up. If not used as a context manager, users of this object self._cleaned = False if dind: docker.rm("-f", self.name, _ok_code=[0, 1]) docker.run("--privileged", "-v", os.getcwd()+":/code", "--name", self.name, "-e", "DOCKER_DAEMON_ARGS=" "--kv-store=consul:%s:8500" % utils.get_ip(), "-tid", "calico/dind") self.ip = docker.inspect("--format", "{{ .NetworkSettings.IPAddress }}", self.name).stdout.rstrip() self.ip6 = docker.inspect("--format", "{{ .NetworkSettings." "GlobalIPv6Address }}", self.name).stdout.rstrip() # Make sure docker is up docker_ps = partial(self.execute, "docker ps") retry_until_success(docker_ps, ex_class=CalledProcessError) self.execute("docker load --input /code/calico_containers/calico-node.tar && " "docker load --input /code/calico_containers/busybox.tar") else: self.ip = get_ip() if start_calico: self.start_calico_node() self.assert_driver_up()
def test_multi_host(self): """ Run a mainline multi-host test. Almost identical in function to the vagrant coreOS demo. """ host1 = DockerHost('host1') host2 = DockerHost('host2') host1.start_etcd() host1_ip = docker.inspect("--format", "'{{ .NetworkSettings.IPAddress }}'", host1.name).stdout.rstrip() host2_ip = docker.inspect("--format", "'{{ .NetworkSettings.IPAddress }}'", host2.name).stdout.rstrip() etcd_port = "ETCD_AUTHORITY=%s:2379" % host1_ip calicoctl = etcd_port + " /code/dist/calicoctl %s" host1.listen(calicoctl % "reset || true") host1.listen(calicoctl % ("node --ip=%s" % host1_ip)) host2.listen(calicoctl % ("node --ip=%s" % host2_ip)) calico_port = "DOCKER_HOST=localhost:2377" # Wait for the Calico nodes to be created. sleep(3) host1.listen("%s docker run -e CALICO_IP=192.168.1.1 --name workload-A -tid busybox" % calico_port) host1.listen("%s docker run -e CALICO_IP=192.168.1.2 --name workload-B -tid busybox" % calico_port) host1.listen("%s docker run -e CALICO_IP=192.168.1.3 --name workload-C -tid busybox" % calico_port) host2.listen("%s docker run -e CALICO_IP=192.168.1.4 --name workload-D -tid busybox" % calico_port) host2.listen("%s docker run -e CALICO_IP=192.168.1.5 --name workload-E -tid busybox" % calico_port) host1.listen(calicoctl % "profile add PROF_A_C_E") host1.listen(calicoctl % "profile add PROF_B") host1.listen(calicoctl % "profile add PROF_D") host1.listen(calicoctl % "profile PROF_A_C_E member add workload-A") host1.listen(calicoctl % "profile PROF_B member add workload-B") host1.listen(calicoctl % "profile PROF_A_C_E member add workload-C") host2.listen(calicoctl % "profile PROF_D member add workload-D") host2.listen(calicoctl % "profile PROF_A_C_E member add workload-E") # Wait for the workload networking to converge. sleep(1) host1.execute("docker exec workload-A ping -c 4 192.168.1.3") try: host1.execute("docker exec workload-A ping -c 4 192.168.1.2") raise except ErrorReturnCode_1: pass try: host1.execute("docker exec workload-A ping -c 4 192.168.1.4") raise except ErrorReturnCode_1: pass host1.execute("docker exec workload-A ping -c 4 192.168.1.5")
def __init__(self, name, start_calico=True, dind=True): self.name = name self.dind = dind self.workloads = set() # This variable is used to assert on destruction that this object was # cleaned up. If not used as a context manager, users of this object self._cleaned = False if dind: # TODO use pydocker docker.rm("-f", self.name, _ok_code=[0, 1]) docker.run("--privileged", "-v", os.getcwd()+":/code", "--name", self.name, "-tid", "calico/dind") self.ip = docker.inspect("--format", "{{ .NetworkSettings.IPAddress }}", self.name).stdout.rstrip() self.ip6 = docker.inspect("--format", "{{ .NetworkSettings." "GlobalIPv6Address }}", self.name).stdout.rstrip() # Make sure docker is up docker_ps = partial(self.execute, "docker ps") retry_until_success(docker_ps, ex_class=CalledProcessError, retries=100) self.execute("docker load --input /code/calico_containers/calico-node.tar && " "docker load --input /code/calico_containers/busybox.tar") else: self.ip = get_ip() if start_calico: self.start_calico_node()
def run_mainline(self, ip1, ip2): """ Setup two endpoints on one host and check connectivity. """ host = DockerHost('host') host.start_etcd() host_ip = docker.inspect("--format", "'{{ .NetworkSettings.IPAddress }}'", host.name).stdout.rstrip() etcd_port = "ETCD_AUTHORITY=%s:2379" % host_ip calicoctl = etcd_port + " /code/dist/calicoctl %s" calico_port = "DOCKER_HOST=localhost:2377" host.execute("docker run --rm -v `pwd`:/target jpetazzo/nsenter", _ok_code=[0, 1]) host.execute(calicoctl % "node --ip=127.0.0.1") host.execute(calicoctl % "profile add TEST_GROUP") # Wait for powerstrip to come up. for i in range(5): try: host.listen("%s docker ps" % calico_port) break except ErrorReturnCode: if i == 4: raise AssertionError("Powerstrip failed to come up.") else: sleep(1) host.listen("%s docker run -e CALICO_IP=%s -tid --name=node1 busybox" % (calico_port, ip1)) host.listen("%s docker run -e CALICO_IP=%s -tid --name=node2 busybox" % (calico_port, ip2)) # Perform a docker inspect to extract the configured IP addresses. node1_ip = host.execute("%s docker inspect --format '{{ .NetworkSettings.IPAddress }}' node1" % calico_port).stdout.rstrip() node2_ip = host.execute("%s docker inspect --format '{{ .NetworkSettings.IPAddress }}' node2" % calico_port).stdout.rstrip() # Configure the nodes with the same profiles. host.listen(calicoctl % "profile TEST_GROUP member add node1") host.listen(calicoctl % "profile TEST_GROUP member add node2") node1_pid = host.execute("docker inspect --format {{.State.Pid}} node1").stdout.rstrip() node2_pid = host.execute("docker inspect --format {{.State.Pid}} node2").stdout.rstrip() for i in range(10): try: host.listen("./nsenter -t %s ping %s -c 1 -W 1" % (node1_pid, node2_ip)) break except ErrorReturnCode: if i == 9: raise AssertionError("Network failed to come up.") else: sleep(1) # Check connectivity. host.execute("./nsenter -t %s ping %s -c 1" % (node1_pid, node1_ip)) host.execute("./nsenter -t %s ping %s -c 1" % (node1_pid, node2_ip)) host.execute("./nsenter -t %s ping %s -c 1" % (node2_pid, node1_ip)) host.execute("./nsenter -t %s ping %s -c 1" % (node2_pid, node2_ip)) # Test calicoctl teardown commands. host.execute(calicoctl % "profile remove TEST_GROUP") host.execute(calicoctl % "container remove node1") host.execute(calicoctl % "container remove node2") host.execute(calicoctl % "pool remove 192.168.0.0/16") host.execute(calicoctl % "node stop")