<commit_before>from sh import ErrorReturnCode, ErrorReturnCode_1
from time import sleep

from test_base import TestBase
from docker_host import DockerHost


class MultiHostMainline(TestBase):
    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')

        calicoctl = "/code/dist/calicoctl %s"
        try:
            host1.execute(calicoctl % "reset")
        except ErrorReturnCode:
            pass

        host1.execute(calicoctl % ("node --ip=%s" % host1.ip))
        host2.execute(calicoctl % ("node --ip=%s" % host2.ip))

        self.assert_powerstrip_up(host1)
        host1.execute("docker run -e CALICO_IP=192.168.1.1 --name workload-A -tid busybox", use_powerstrip=True)
        host1.execute("docker run -e CALICO_IP=192.168.1.2 --name workload-B -tid busybox", use_powerstrip=True)
        host1.execute("docker run -e CALICO_IP=192.168.1.3 --name workload-C -tid busybox", use_powerstrip=True)

        self.assert_powerstrip_up(host2)
        host2.execute("docker run -e CALICO_IP=192.168.1.4 --name workload-D -tid busybox", use_powerstrip=True)
        host2.execute("docker run -e CALICO_IP=192.168.1.5 --name workload-E -tid busybox", use_powerstrip=True)

        host1.execute(calicoctl % "profile add PROF_A_C_E")
        host1.execute(calicoctl % "profile add PROF_B")
        host1.execute(calicoctl % "profile add PROF_D")

        host1.execute(calicoctl % "profile PROF_A_C_E member add workload-A")
        host1.execute(calicoctl % "profile PROF_B member add workload-B")
        host1.execute(calicoctl % "profile PROF_A_C_E member add workload-C")

        host2.execute(calicoctl % "profile PROF_D member add workload-D")
        host2.execute(calicoctl % "profile PROF_A_C_E member add workload-E")

        # Wait for the workload networking to converge.
        sleep(1)

        assert host1.execute("docker exec workload-A ping -c 4 192.168.1.3")

        with self.assertRaises(ErrorReturnCode_1):
            host1.execute("docker exec workload-A ping -c 4 192.168.1.2")

        with self.assertRaises(ErrorReturnCode_1):
            host1.execute("docker exec workload-A ping -c 4 192.168.1.4")

        assert host1.execute("docker exec workload-A ping -c 4 192.168.1.5")
<commit_msg>Improve test_multi_host.<commit_after>from sh import ErrorReturnCode_1
from functools import partial

from test_base import TestBase
from docker_host import DockerHost


class MultiHostMainline(TestBase):
    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')

        calicoctl = "/code/dist/calicoctl %s"

        host1.execute(calicoctl % ("node --ip=%s" % host1.ip))
        host2.execute(calicoctl % ("node --ip=%s" % host2.ip))

        ip1 = "192.168.1.1"
        ip2 = "192.168.1.2"
        ip3 = "192.168.1.3"
        ip4 = "192.168.1.4"
        ip5 = "192.168.1.5"

        self.assert_powerstrip_up(host1)
        host1.execute("docker run -e CALICO_IP=%s --name workload1 -tid busybox" % ip1,
                      use_powerstrip=True)
        host1.execute("docker run -e CALICO_IP=%s --name workload2 -tid busybox" % ip2,
                      use_powerstrip=True)
        host1.execute("docker run -e CALICO_IP=%s --name workload3 -tid busybox" % ip3,
                      use_powerstrip=True)

        self.assert_powerstrip_up(host2)
        host2.execute("docker run -e CALICO_IP=%s --name workload4 -tid busybox" % ip4,
                      use_powerstrip=True)
        host2.execute("docker run -e CALICO_IP=%s --name workload5 -tid busybox" % ip5,
                      use_powerstrip=True)

        host1.execute(calicoctl % "profile add PROF_1_3_5")
        host1.execute(calicoctl % "profile add PROF_2")
        host1.execute(calicoctl % "profile add PROF_4")

        host1.execute(calicoctl % "profile PROF_1_3_5 member add workload1")
        host1.execute(calicoctl % "profile PROF_2 member add workload2")
        host1.execute(calicoctl % "profile PROF_1_3_5 member add workload3")

        host2.execute(calicoctl % "profile PROF_4 member add workload4")
        host2.execute(calicoctl % "profile PROF_1_3_5 member add workload5")

        # Wait for the workload networking to converge.
        ping = partial(host1.execute, "docker exec workload1 ping -c 4 %s" % ip3)
        self.retry_until_success(ping, ex_class=ErrorReturnCode_1)

        with self.assertRaises(ErrorReturnCode_1):
            host1.execute("docker exec workload1 ping -c 4 %s" % ip2)

        with self.assertRaises(ErrorReturnCode_1):
            host1.execute("docker exec workload1 ping -c 4 %s" % ip4)

        host1.execute("docker exec workload1 ping -c 4 %s" % ip5)

{'fraction_non_alphanumeric': 0.10666130006037432, 'fraction_numerical': 0.045683236063594285, 'mean_word_length': 2.606676342525399, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 14, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1681543', 'n_tokens_mistral': 1781, 'n_tokens_neox': 1576, 'n_words': 388}