コード例 #1
0
ファイル: test_net.py プロジェクト: EvilMcJerkface/blockade
    def _network_state(self, state, output):
        mock_host_exec = mock.Mock()
        mock_run = mock_host_exec.run
        mock_run.return_value = output

        net = BlockadeNetwork(mock.Mock(), mock_host_exec)
        self.assertEqual(net.network_state('somedevice'), state)
        self.assertIn('somedevice', mock_run.call_args[0][0])
コード例 #2
0
    def _network_state(self, state, output):
        mock_host_exec = mock.Mock()
        mock_run = mock_host_exec.run
        mock_run.return_value = output

        net = BlockadeNetwork(mock.Mock(), mock_host_exec)
        self.assertEqual(net.network_state('somedevice'), state)
        self.assertIn('somedevice', mock_run.call_args[0][0])
コード例 #3
0
ファイル: test_net.py プロジェクト: EvilMcJerkface/blockade
    def test_network_already_normal(self):
        mock_host_exec = mock.Mock()
        mock_run = mock_host_exec.run
        net = BlockadeNetwork(None, mock_host_exec)
        mock_run.side_effect = HostExecError("", exit_code=2,
            output=QDISC_DEL_NOENT)

        # ensure we don't raise an error
        net.fast('somedevice')
        self.assertIn('somedevice', mock_run.call_args[0][0])
コード例 #4
0
ファイル: test_net.py プロジェクト: EvilMcJerkface/blockade
 def test_slow(self):
     slow_config = "75ms 100ms distribution normal"
     mock_host_exec = mock.Mock()
     mock_run = mock_host_exec.run
     mock_config = mock.Mock()
     mock_config.network = {"slow": slow_config}
     net = BlockadeNetwork(mock_config, mock_host_exec)
     net.slow("mydevice")
     cmd = ["tc", "qdisc", "replace", "dev", "mydevice",
            "root", "netem", "delay"] + slow_config.split()
     mock_run.assert_called_once_with(cmd)
コード例 #5
0
    def test_get_ip_partitions(self):
        mock_host_exec = mock.Mock()
        mock_run = mock_host_exec.run
        blockade_id = "e5dcf85cd2"
        mock_run.return_value = _IPTABLES_LIST_FORWARD_1

        net = BlockadeNetwork(None, mock_host_exec)
        result = net.get_ip_partitions(blockade_id)

        self.assertEqual(1, mock_run.call_count)
        self.assertEqual({"172.17.0.162": 1, "172.17.0.164": 1}, result)
コード例 #6
0
    def test_network_already_normal(self):
        mock_host_exec = mock.Mock()
        mock_run = mock_host_exec.run
        net = BlockadeNetwork(None, mock_host_exec)
        mock_run.side_effect = HostExecError("",
                                             exit_code=2,
                                             output=QDISC_DEL_NOENT)

        # ensure we don't raise an error
        net.fast('somedevice')
        self.assertIn('somedevice', mock_run.call_args[0][0])
コード例 #7
0
ファイル: test_net.py プロジェクト: EvilMcJerkface/blockade
 def test_flaky(self):
     flaky_config = "30%"
     mock_host_exec = mock.Mock()
     mock_run = mock_host_exec.run
     mock_config = mock.Mock()
     mock_config.network = {"flaky": flaky_config}
     net = BlockadeNetwork(mock_config, mock_host_exec)
     net.flaky("mydevice")
     cmd = ["tc", "qdisc", "replace", "dev", "mydevice",
            "root", "netem", "loss"] + flaky_config.split()
     mock_run.assert_called_once_with(cmd)
コード例 #8
0
ファイル: test_net.py プロジェクト: EvilMcJerkface/blockade
 def test_duplicate(self):
     duplicate_config = "5%"
     mock_host_exec = mock.Mock()
     mock_run = mock_host_exec.run
     mock_config = mock.Mock()
     mock_config.network = {"duplicate": duplicate_config}
     net = BlockadeNetwork(mock_config, mock_host_exec)
     net.duplicate("mydevice")
     cmd = ["tc", "qdisc", "replace", "dev", "mydevice",
            "root", "netem", "duplicate"] + duplicate_config.split()
     mock_run.assert_called_once_with(cmd)
コード例 #9
0
ファイル: test_net.py プロジェクト: EvilMcJerkface/blockade
    def test_get_ip_partitions(self):
        mock_host_exec = mock.Mock()
        mock_run = mock_host_exec.run
        blockade_id = "e5dcf85cd2"
        mock_run.return_value = _IPTABLES_LIST_FORWARD_1

        net = BlockadeNetwork(None, mock_host_exec)
        result = net.get_ip_partitions(blockade_id)

        self.assertEqual(1, mock_run.call_count)
        self.assertEqual({"172.17.0.162": 1, "172.17.0.164": 1}, result)
コード例 #10
0
 def test_duplicate(self):
     duplicate_config = "5%"
     mock_host_exec = mock.Mock()
     mock_run = mock_host_exec.run
     mock_config = mock.Mock()
     mock_config.network = {"duplicate": duplicate_config}
     net = BlockadeNetwork(mock_config, mock_host_exec)
     net.duplicate("mydevice")
     cmd = [
         "tc", "qdisc", "replace", "dev", "mydevice", "root", "netem",
         "duplicate"
     ] + duplicate_config.split()
     mock_run.assert_called_once_with(cmd)
コード例 #11
0
 def test_flaky(self):
     flaky_config = "30%"
     mock_host_exec = mock.Mock()
     mock_run = mock_host_exec.run
     mock_config = mock.Mock()
     mock_config.network = {"flaky": flaky_config}
     net = BlockadeNetwork(mock_config, mock_host_exec)
     net.flaky("mydevice")
     cmd = [
         "tc", "qdisc", "replace", "dev", "mydevice", "root", "netem",
         "loss"
     ] + flaky_config.split()
     mock_run.assert_called_once_with(cmd)
コード例 #12
0
 def test_slow(self):
     slow_config = "75ms 100ms distribution normal"
     mock_host_exec = mock.Mock()
     mock_run = mock_host_exec.run
     mock_config = mock.Mock()
     mock_config.network = {"slow": slow_config}
     net = BlockadeNetwork(mock_config, mock_host_exec)
     net.slow("mydevice")
     cmd = [
         "tc", "qdisc", "replace", "dev", "mydevice", "root", "netem",
         "delay"
     ] + slow_config.split()
     mock_run.assert_called_once_with(cmd)
コード例 #13
0
 def test_iptables_create_chain(self):
     mock_host_exec = mock.Mock()
     mock_run = mock_host_exec.run
     net = BlockadeNetwork(None, mock_host_exec)
     net.iptables.create_chain("hats")
     cmd = ["iptables", "-N", "hats"]
     mock_run.assert_called_once_with(cmd)
コード例 #14
0
 def test_iptables_insert_rule_3(self):
     mock_host_exec = mock.Mock()
     mock_run = mock_host_exec.run
     net = BlockadeNetwork(None, mock_host_exec)
     net.iptables.insert_rule("FORWARD", dest="192.168.0.2", target="DROP")
     cmd = ["iptables", "-I", "FORWARD", "-d", "192.168.0.2", "-j", "DROP"]
     mock_run.assert_called_once_with(cmd)
コード例 #15
0
 def get_blockade(name):
     global BLOCKADE_CONFIGS
     config = BLOCKADE_CONFIGS[name]
     return Blockade(config,
                     blockade_id=name,
                     state=BlockadeManager.load_state(name),
                     network=BlockadeNetwork(config))
コード例 #16
0
    def _network_state(self, state, output):
        with mock.patch('blockade.net.docker_run') as mock_docker_run:
            mock_docker_run.return_value = output

            net = BlockadeNetwork(mock.Mock())
            self.assertEqual(net.network_state('somedevice'), state)
            self.assertIn('somedevice', mock_docker_run.call_args[0][0])
コード例 #17
0
ファイル: test_net.py プロジェクト: EvilMcJerkface/blockade
    def test_partition_1(self):

        def iptables(args):
            if args == ["iptables", "-n", "-L"]:
                return _IPTABLES_LIST_2
            if args == ["iptables", "-n", "-L", "FORWARD"]:
                return _IPTABLES_LIST_FORWARD_2
            return ""

        blockade_id = "e5dcf85cd2"
        mock_host_exec = mock.Mock()
        mock_run = mock_host_exec.run
        mock_run.side_effect = iptables
        net = BlockadeNetwork(None, mock_host_exec)

        net.partition_containers(blockade_id, [
            # partition 1
            [mock.Mock(name="c1", ip_address="10.0.1.1"),
             mock.Mock(name="c2", ip_address="10.0.1.2")],
            # partition 2
            [mock.Mock(name="c3", ip_address="10.0.1.3")]
        ])

        mock_run.assert_has_calls([
            # create a chain for each partition
            mock.call(shlex.split("iptables -N blockade-e5dcf85cd2-p1")),
            # forward traffic from each node in p1 to its new chain
            mock.call(shlex.split(
                "iptables -I FORWARD -s 10.0.1.1 -j blockade-e5dcf85cd2-p1")),
            mock.call(shlex.split(
                "iptables -I FORWARD -s 10.0.1.2 -j blockade-e5dcf85cd2-p1")),
            # and drop any traffic from this partition directed
            # at any container not in this partition
            mock.call(shlex.split(
                "iptables -I blockade-e5dcf85cd2-p1 -d 10.0.1.3 -j DROP")),
        ], any_order=True)

        mock_run.assert_has_calls([
            # now repeat the process for the second partition
            mock.call(shlex.split("iptables -N blockade-e5dcf85cd2-p2")),
            mock.call(shlex.split(
                "iptables -I FORWARD -s 10.0.1.3 -j blockade-e5dcf85cd2-p2")),
            mock.call(shlex.split(
                "iptables -I blockade-e5dcf85cd2-p2 -d 10.0.1.1 -j DROP")),
            mock.call(shlex.split(
                "iptables -I blockade-e5dcf85cd2-p2 -d 10.0.1.2 -j DROP")),
        ], any_order=True)
コード例 #18
0
 def test_iptables_delete_blockade_rules_2(self):
     blockade_id = "e5dcf85cd2"
     mock_host_exec = mock.Mock()
     mock_run = mock_host_exec.run
     mock_run.return_value = _IPTABLES_LIST_FORWARD_2
     net = BlockadeNetwork(None, mock_host_exec)
     net.iptables.delete_blockade_rules(blockade_id)
     self.assertEqual(1, mock_run.call_count)
コード例 #19
0
    def test_network_already_normal(self):
        with mock.patch('blockade.net.docker_run') as mock_docker_run:
            mock_docker_run.return_value = "", QDISC_DEL_NOENT

            net = BlockadeNetwork(mock.Mock())

            # ensure we don't raise an error
            net.fast('somedevice')
            self.assertIn('somedevice', mock_docker_run.call_args[0][0])
コード例 #20
0
ファイル: test_net.py プロジェクト: ramitsurana/blockade
    def _network_state(self, state, output):
        with mock.patch('blockade.net.subprocess') as mock_subprocess:
            mock_subprocess.CalledProcessError = subprocess.CalledProcessError
            mock_subprocess.check_output.return_value = output

            net = BlockadeNetwork(mock.Mock())
            self.assertEqual(net.network_state('somedevice'), state)
            self.assertIn('somedevice',
                          mock_subprocess.check_output.call_args[0][0])
コード例 #21
0
ファイル: manager.py プロジェクト: yuchenhecs/blockade
 def get_blockade(name):
     global BLOCKADE_CONFIGS
     config = BLOCKADE_CONFIGS[name]
     host_exec = BlockadeManager.host_exec
     if host_exec is None:
         raise ValueError("host exec not set")
     return Blockade(config,
                     blockade_id=name,
                     state=BlockadeManager.load_state(name),
                     network=BlockadeNetwork(config, host_exec))
コード例 #22
0
ファイル: test_net.py プロジェクト: ramitsurana/blockade
 def test_duplicate(self):
     duplicate_config = "5%"
     with mock.patch('blockade.net.subprocess') as mock_subprocess:
         mock_config = mock.Mock()
         mock_config.network = {"duplicate": duplicate_config}
         net = BlockadeNetwork(mock_config)
         net.duplicate("mydevice")
         mock_subprocess.check_call.assert_called_once_with([
             "tc", "qdisc", "replace", "dev", "mydevice", "root", "netem",
             "duplicate"
         ] + duplicate_config.split())
コード例 #23
0
ファイル: test_net.py プロジェクト: ramitsurana/blockade
 def test_flaky(self):
     flaky_config = "30%"
     with mock.patch('blockade.net.subprocess') as mock_subprocess:
         mock_config = mock.Mock()
         mock_config.network = {"flaky": flaky_config}
         net = BlockadeNetwork(mock_config)
         net.flaky("mydevice")
         mock_subprocess.check_call.assert_called_once_with([
             "tc", "qdisc", "replace", "dev", "mydevice", "root", "netem",
             "loss"
         ] + flaky_config.split())
コード例 #24
0
ファイル: test_net.py プロジェクト: ramitsurana/blockade
 def test_slow(self):
     slow_config = "75ms 100ms distribution normal"
     with mock.patch('blockade.net.subprocess') as mock_subprocess:
         mock_config = mock.Mock()
         mock_config.network = {"slow": slow_config}
         net = BlockadeNetwork(mock_config)
         net.slow("mydevice")
         mock_subprocess.check_call.assert_called_once_with([
             "tc", "qdisc", "replace", "dev", "mydevice", "root", "netem",
             "delay"
         ] + slow_config.split())
コード例 #25
0
ファイル: test_net.py プロジェクト: ramitsurana/blockade
    def test_network_already_normal(self):
        with mock.patch('blockade.net.subprocess') as mock_subprocess:
            mock_subprocess.CalledProcessError = subprocess.CalledProcessError
            mock_process = mock_subprocess.Popen.return_value = mock.Mock()
            mock_process.communicate.return_value = "", QDISC_DEL_NOENT
            mock_process.returncode = 2

            net = BlockadeNetwork(mock.Mock())

            # ensure we don't raise an error
            net.fast('somedevice')
            self.assertIn('somedevice', mock_subprocess.Popen.call_args[0][0])
コード例 #26
0
 def test_duplicate(self):
     duplicate_config = "5%"
     with mock.patch('blockade.net.docker_run') as mock_docker_run:
         mock_config = mock.Mock()
         mock_config.network = {"duplicate": duplicate_config}
         net = BlockadeNetwork(mock_config)
         net.duplicate("mydevice")
         cmd = [
             "tc", "qdisc", "replace", "dev", "mydevice", "root", "netem",
             "duplicate"
         ] + duplicate_config.split()
         mock_docker_run.assert_called_once_with(
             ' '.join(cmd), image=blockade.net.IPTABLES_DOCKER_IMAGE)
コード例 #27
0
 def test_flaky(self):
     flaky_config = "30%"
     with mock.patch('blockade.net.docker_run') as mock_docker_run:
         mock_config = mock.Mock()
         mock_config.network = {"flaky": flaky_config}
         net = BlockadeNetwork(mock_config)
         net.flaky("mydevice")
         cmd = [
             "tc", "qdisc", "replace", "dev", "mydevice", "root", "netem",
             "loss"
         ] + flaky_config.split()
         mock_docker_run.assert_called_once_with(
             ' '.join(cmd), image=blockade.net.IPTABLES_DOCKER_IMAGE)
コード例 #28
0
 def test_slow(self):
     slow_config = "75ms 100ms distribution normal"
     with mock.patch('blockade.net.docker_run') as mock_docker_run:
         mock_config = mock.Mock()
         mock_config.network = {"slow": slow_config}
         net = BlockadeNetwork(mock_config)
         net.slow("mydevice")
         cmd = [
             "tc", "qdisc", "replace", "dev", "mydevice", "root", "netem",
             "delay"
         ] + slow_config.split()
         mock_docker_run.assert_called_once_with(
             ' '.join(cmd), image=blockade.net.IPTABLES_DOCKER_IMAGE)
コード例 #29
0
    def test_iptables_delete_blockade_rules_1(self):
        blockade_id = "e5dcf85cd2"
        mock_host_exec = mock.Mock()
        mock_run = mock_host_exec.run
        mock_run.return_value = _IPTABLES_LIST_FORWARD_1

        net = BlockadeNetwork(None, mock_host_exec)
        net.iptables.delete_blockade_rules(blockade_id)

        self.assertEqual(3, mock_run.call_count)

        # rules should be removed in reverse order
        expected_calls = [
            mock.call(["iptables", "-n", "-L", "FORWARD"]),
            mock.call(["iptables", "-D", "FORWARD", "4"]),
            mock.call(["iptables", "-D", "FORWARD", "3"])
        ]
        self.assertEqual(expected_calls, mock_run.call_args_list)
コード例 #30
0
    def test_iptables_delete_blockade_chains_1(self):
        blockade_id = "e5dcf85cd2"
        mock_host_exec = mock.Mock()
        mock_run = mock_host_exec.run
        mock_run.return_value = _IPTABLES_LIST_1
        net = BlockadeNetwork(None, mock_host_exec)

        net.iptables.delete_blockade_chains(blockade_id)

        self.assertEqual(5, mock_run.call_count)

        expected_calls = [
            mock.call(shlex.split("iptables -n -L")),
            mock.call(shlex.split("iptables -F blockade-e5dcf85cd2-p1")),
            mock.call(shlex.split("iptables -X blockade-e5dcf85cd2-p1")),
            mock.call(shlex.split("iptables -F blockade-e5dcf85cd2-p2")),
            mock.call(shlex.split("iptables -X blockade-e5dcf85cd2-p2"))
        ]
        self.assertEqual(expected_calls, mock_run.call_args_list)
コード例 #31
0
    def test_partition_1(self):
        def iptables(args):
            if args == ["iptables", "-n", "-L"]:
                return _IPTABLES_LIST_2
            if args == ["iptables", "-n", "-L", "FORWARD"]:
                return _IPTABLES_LIST_FORWARD_2
            return ""

        blockade_id = "e5dcf85cd2"
        mock_host_exec = mock.Mock()
        mock_run = mock_host_exec.run
        mock_run.side_effect = iptables
        net = BlockadeNetwork(None, mock_host_exec)

        net.partition_containers(
            blockade_id,
            [
                # partition 1
                [
                    mock.Mock(name="c1", ip_address="10.0.1.1"),
                    mock.Mock(name="c2", ip_address="10.0.1.2")
                ],
                # partition 2
                [mock.Mock(name="c3", ip_address="10.0.1.3")]
            ])

        mock_run.assert_has_calls(
            [
                # create a chain for each partition
                mock.call(shlex.split("iptables -N blockade-e5dcf85cd2-p1")),
                # forward traffic from each node in p1 to its new chain
                mock.call(
                    shlex.split(
                        "iptables -I FORWARD -s 10.0.1.1 -j blockade-e5dcf85cd2-p1"
                    )),
                mock.call(
                    shlex.split(
                        "iptables -I FORWARD -s 10.0.1.2 -j blockade-e5dcf85cd2-p1"
                    )),
                # and drop any traffic from this partition directed
                # at any container not in this partition
                mock.call(
                    shlex.split(
                        "iptables -I blockade-e5dcf85cd2-p1 -d 10.0.1.3 -j DROP"
                    )),
            ],
            any_order=True)

        mock_run.assert_has_calls(
            [
                # now repeat the process for the second partition
                mock.call(shlex.split("iptables -N blockade-e5dcf85cd2-p2")),
                mock.call(
                    shlex.split(
                        "iptables -I FORWARD -s 10.0.1.3 -j blockade-e5dcf85cd2-p2"
                    )),
                mock.call(
                    shlex.split(
                        "iptables -I blockade-e5dcf85cd2-p2 -d 10.0.1.1 -j DROP"
                    )),
                mock.call(
                    shlex.split(
                        "iptables -I blockade-e5dcf85cd2-p2 -d 10.0.1.2 -j DROP"
                    )),
            ],
            any_order=True)