示例#1
0
    def test_connect(self, mock):
        adapter = mock.post(
            "http+unix://localhost:9999/v1.40/networks/podman/connect", )
        net = Network(attrs=FIRST_NETWORK, client=self.client.api)

        net.connect(
            "podman_ctnr",
            aliases=["production"],
            ipv4_address="172.16.0.1",
        )
        self.assertEqual(adapter.call_count, 1)
        self.assertDictEqual(
            adapter.last_request.json(),
            {
                'Container': 'podman_ctnr',
                "EndpointConfig": {
                    'Aliases': ['production'],
                    'IPAMConfig': {
                        'IPv4Address': '172.16.0.1'
                    },
                    'IPAddress':
                    '172.16.0.1',
                    'NetworkID':
                    '2f259bab93aaaaa2542ba43ef33eb990d0999ee1b9924b557b7be53c0b7a1bb9',
                },
            },
        )
示例#2
0
    def test_id(self):
        expected = {"Id": "1cf06390-709d-4ffa-a054-c3083abe367c"}
        actual = Network(attrs=expected)
        self.assertEqual(actual.id, expected["Id"])

        actual = Network(attrs={"name": "database"})
        self.assertEqual(
            actual.id,
            "3549b0028b75d981cdda2e573e9cb49dedc200185876df299f912b79f69dabd8")
示例#3
0
    def test_disconnect(self, mock):
        adapter = mock.post(
            "http+unix://localhost:9999/v1.40/networks/podman/disconnect", )
        net = Network(attrs=FIRST_NETWORK, client=self.client.api)

        net.disconnect("podman_ctnr", force=True)
        self.assertEqual(adapter.call_count, 1)
        self.assertDictEqual(
            adapter.last_request.json(),
            {
                'Container': 'podman_ctnr',
                "Force": True,
            },
        )
示例#4
0
    def test_disconnect(self, mock):
        adapter = mock.post(tests.COMPATIBLE_URL +
                            "/networks/podman/disconnect")
        net = Network(attrs=FIRST_NETWORK, client=self.client.api)

        net.disconnect("podman_ctnr", force=True)
        self.assertEqual(adapter.call_count, 1)
        self.assertDictEqual(
            adapter.last_request.json(),
            {
                'Container': 'podman_ctnr',
                "Force": True,
            },
        )
示例#5
0
    def _connect_to_network(self, container: Container, network: Network):
        """There is no way to get MAC address of remote interface except
        executing "ip l" inside container.
        """
        logging.debug(f"Connecting {container.name} to {network.name}")
        try:
            network.connect(container)
        except APIError:
            raise PoolManagerError(
                f"Could not connect {container.name} to {network.name}")

        container.reload()

        logging.debug(
            f"Getting MAC address of remote interface at {container.name} for {network.name}"
        )
        interfaces = loads(
            subprocess.check_output(
                ["podman", "exec", "-it", container.name, "ip", "-j",
                 "a"]).decode("utf-8"))
        interface = max(interfaces, key=(
            lambda inf: inf["ifindex"]))  # get interface with highest index

        eth = interface["ifname"]
        if "link_index" in interface:
            eth += f"@{interface['link_index']}"

        machine = self._pool[container.attrs["Config"]["Hostname"]]
        machine["interfaces"][eth] = {
            "params": {
                "hwaddr": interface["address"],
                "driver": "veth"
            },
            "network": network.name,
        }

        return True
示例#6
0
    def test_name(self):
        actual = Network(attrs={"Name": "database"})
        self.assertEqual(actual.name, "database")

        actual = Network({"name": "database"})
        self.assertEqual(actual.name, "database")