Exemplo n.º 1
0
def test_present_ingress(network, docker, docker_network):
    with network() as net:
        ret = docker_network.present(name=net.name, ingress=True)
        assert ret.result is True

        net_info = docker.inspect_network(net.name)
        assert net_info["Ingress"] is True
Exemplo n.º 2
0
def test_static_ip_one_network(docker_container, container_name, image,
                               modules, network):
    """
    Ensure that if a network is created and specified as network_mode, that is the only network, and
    the static IP is applied.
    """
    with network(subnet="10.247.197.96/27") as net:
        requested_ip = "10.247.197.100"
        kwargs = {
            "name": container_name,
            "image": image,
            "network_mode": net.name,
            "networks": [{
                net.name: [{
                    "ipv4_address": requested_ip
                }]
            }],
            "shutdown_timeout": 1,
        }
        # Create a container
        ret = docker_container.running(**kwargs)
        assert ret.result is True

        inspect_result = modules.docker.inspect_container(container_name)
        connected_networks = inspect_result["NetworkSettings"]["Networks"]

        assert list(connected_networks.keys()) == [net.name]
        assert inspect_result["HostConfig"]["NetworkMode"] == net.name
        assert connected_networks[
            net.name]["IPAMConfig"]["IPv4Address"] == requested_ip
Exemplo n.º 3
0
def test_bridge_dupname_update(network, docker, docker_network):
    # com.docker.network.bridge.name can not have names over 15 chars. so grab the last 8
    with network(subnet="10.247.197.96/27") as net:
        ret = docker_network.present(
            name=net.name,
            subnet=net.subnet,
            driver="bridge",
            driver_opts=[{
                "com.docker.network.bridge.name": net.name[-8:]
            }],
        )
        assert ret.result is True
        # Second run to make sure everything is still fine.
        ret = docker_network.present(
            name=net.name,
            subnet=net.subnet,
            driver="bridge",
            driver_opts=[{
                "com.docker.network.bridge.name": net.name[-8:]
            }],
        )
        assert ret.result is True
        assert not ret.changes
        assert (ret.comment ==
                "Network '{}' already exists, and is configured as specified".
                format(net.name))
Exemplo n.º 4
0
def test_present_with_reconnect(network, docker, docker_network, container,
                                reconnect):
    """
    Test reconnecting with containers not passed to state
    """
    with network() as net:
        ret = docker_network.present(name=net.name, driver="bridge")
        assert ret.result is True
        assert ret.changes
        assert ret.changes == {"created": True}
        assert ret.comment == "Network '{}' created".format(net.name)

        # Connect the container
        docker.connect_container_to_network(container.name, net.name)

        # Change the driver to force the network to be replaced
        ret = docker_network.present(name=net.name,
                                     driver="macvlan",
                                     reconnect=reconnect)
        assert ret.result is True
        assert ret.changes
        assert ret.changes == {
            "recreated": True,
            "reconnected" if reconnect else "disconnected": [container.name],
            net.name: {
                "Driver": {
                    "old": "bridge",
                    "new": "macvlan",
                },
            },
        }
        assert ret.comment == "Network '{}' was replaced with updated config".format(
            net.name)
Exemplo n.º 5
0
def test_present_scope(network, docker, docker_network):
    with network() as net:
        ret = docker_network.present(name=net.name, scope="global")
        assert ret.result is True

        net_info = docker.inspect_network(net.name)
        assert net_info["Scope"] == "global"
Exemplo n.º 6
0
def test_present_with_custom_ipv4(network, docker, docker_network):
    # First run will test passing the IPAM arguments individually
    with network(subnet="10.247.197.96/27") as net1, network(
            subnet="10.247.197.128/27") as net2:
        ret = docker_network.present(
            name=net1.name,
            subnet=net1.subnet,
            gateway=net1.gateway,
        )
        assert ret.result is True

        # Second run will pass them in the ipam_pools argument
        ret = docker_network.present(
            name=net1.name,  # We want to keep the same network name
            ipam_pools=[{
                "subnet": net2.subnet,
                "gateway": net2.gateway
            }],
        )
        assert ret.result is True

        # Docker requires there to be IPv4, even when only an IPv6 subnet was
        # provided. So, there will be both an IPv4 and IPv6 pool in the
        # configuration.
        expected = {
            "recreated": True,
            net1.name: {
                "IPAM": {
                    "Config": {
                        "old": [{
                            "Subnet": net1.subnet,
                            "Gateway": net1.gateway
                        }],
                        "new": [{
                            "Subnet": net2.subnet,
                            "Gateway": net2.gateway
                        }],
                    }
                }
            },
        }
        assert ret.changes
        assert ret.changes == expected
        assert ret.comment == "Network '{}' was replaced with updated config".format(
            net1.name)
Exemplo n.º 7
0
def test_present_enable_ipv6(network, docker, docker_network):
    with network(subnet="10.247.197.96/27") as net1:
        with network(subnet="fe3f:2180:26:1::/123") as net2:
            ret = docker_network.present(
                name=net1.name,
                enable_ipv6=True,
                ipam_pools=[
                    {
                        "subnet": net1.subnet
                    },
                    {
                        "subnet": net2.subnet
                    },
                ],
            )
            assert ret.result is True
            net_info = docker.inspect_network(net1.name)
            assert net_info["EnableIPv6"] is True
Exemplo n.º 8
0
def existing_network(network, docker_network):
    with network() as net:
        ret = docker_network.present(name=net.name)
        assert ret.result is True
        assert ret.changes
        assert ret.changes == {"created": True}
        try:
            yield net
        finally:
            docker_network.absent(name=net.name)
Exemplo n.º 9
0
def test_present_attachable(network, docker, docker_network, grains):
    if grains["os_family"] == "RedHat" and grains.get("osmajorrelease",
                                                      0) <= 7:
        pytest.skip("Cannot reliably manage attachable on RHEL <= 7")

    with network() as net:
        ret = docker_network.present(name=net.name, attachable=True)
        assert ret.result is True

        net_info = docker.inspect_network(net.name)
        assert net_info["Attachable"] is True
Exemplo n.º 10
0
def test_present(docker, network, docker_network):
    with network() as net:
        ret = docker_network.present(name=net.name)
        assert ret.result is True
        assert ret.changes
        assert ret.changes == {"created": True}
        assert ret.comment == "Network '{}' created".format(net.name)

        # Now check to see that the network actually exists. If it doesn't,
        # this next function call will raise an exception.
        docker.inspect_network(net.name)
Exemplo n.º 11
0
def existing_network_with_container(network, docker_network, container):
    with network() as net:
        ret = docker_network.present(name=net.name,
                                     containers=[container.name])
        assert ret.result is True
        assert ret.changes
        assert ret.changes == {"created": True, "connected": [container.name]}
        try:
            yield net
        finally:
            docker_network.absent(name=net.name)
Exemplo n.º 12
0
def test_present_labels(network, docker, docker_network):
    # Test a mix of different ways of specifying labels
    with network() as net:
        ret = docker_network.present(
            name=net.name,
            labels=["foo", "bar=baz", {
                "hello": "world"
            }],
        )
        assert ret.result is True
        net_info = docker.inspect_network(net.name)
        assert net_info["Labels"] == {
            "foo": "",
            "bar": "baz",
            "hello": "world"
        }
Exemplo n.º 13
0
def test_running_explicit_networks(docker_container, container_name, image,
                                   modules, network):
    """
    Ensure that if we use an explicit network configuration, we remove any
    default networks not specified (e.g. the default "bridge" network).
    """
    with network(subnet="10.247.197.96/27") as net:
        # Create a container with no specific network configuration. The only
        # networks connected will be the default ones.
        ret = docker_container.running(
            name=container_name,
            image=image,
            shutdown_timeout=1,
        )
        assert ret.result is True

        inspect_result = modules.docker.inspect_container(container_name)
        # Get the default network names
        default_networks = list(inspect_result["NetworkSettings"]["Networks"])

        # Re-run the state with an explicit network configuration. All of the
        # default networks should be disconnected.
        ret = docker_container.running(
            name=container_name,
            image=image,
            networks=[net.name],
            shutdown_timeout=1,
        )
        assert ret.result is True
        net_changes = ret.changes["container"]["Networks"]

        assert ("Container '{}' is already configured as specified.".format(
            container_name) in ret.comment)

        updated_networks = modules.docker.inspect_container(
            container_name)["NetworkSettings"]["Networks"]

        for default_network in default_networks:
            assert ("Disconnected from network '{}'.".format(default_network)
                    in ret.comment)
            assert default_network in net_changes
            # We've tested that the state return is correct, but let's be extra
            # paranoid and check the actual connected networks.
            assert default_network not in updated_networks

        assert "Connected to network '{}'.".format(net.name) in ret.comment
Exemplo n.º 14
0
def test_running_no_changes_hostname_network(docker_container, container_name,
                                             image, network):
    """
    Test that changes are not detected when a hostname is specified for a container
    on a custom network
    """
    with network(subnet="10.247.197.96/27") as net:
        # Create a container
        kwargs = {
            "name": container_name,
            "image": image,
            "shutdown_timeout": 1,
            "network_mode": net.name,
            "networks": [net.name],
            "hostname": "foo",
        }
        ret = docker_container.running(**kwargs)
        assert ret.result is True

        ret = docker_container.running(**kwargs)
        assert ret.result is True
        # Should be no changes
        assert not ret.changes
Exemplo n.º 15
0
def test_present_with_custom_ipv6(network, docker, docker_network):
    with network(subnet="10.247.197.96/27") as ipv4_net, network(
            subnet="fe3f:2180:26:1::/123") as ipv6_net1, network(
                subnet="fe3f:2180:26:1::20/123") as ipv6_net2:
        ret = docker_network.present(
            name=ipv4_net.name,
            enable_ipv6=True,
            ipam_pools=[
                {
                    "subnet": ipv4_net.subnet,
                    "gateway": ipv4_net.gateway
                },
                {
                    "subnet": ipv6_net1.subnet,
                    "gateway": ipv6_net1.gateway
                },
            ],
        )
        assert ret.result is True

        ret = docker_network.present(
            name=ipv4_net.name,  # We want to keep the same network name
            enable_ipv6=True,
            ipam_pools=[
                {
                    "subnet": ipv4_net.subnet,
                    "gateway": ipv4_net.gateway
                },
                {
                    "subnet": ipv6_net2.subnet,
                    "gateway": ipv6_net2.gateway
                },
            ],
        )
        assert ret.result is True

        # Docker requires there to be IPv4, even when only an IPv6 subnet was
        # provided. So, there will be both an IPv4 and IPv6 pool in the
        # configuration.
        expected = {
            "recreated": True,
            ipv4_net.name: {
                "IPAM": {
                    "Config": {
                        "old": [
                            {
                                "Subnet": ipv4_net.subnet,
                                "Gateway": ipv4_net.gateway
                            },
                            {
                                "Subnet": ipv6_net1.subnet,
                                "Gateway": ipv6_net1.gateway
                            },
                        ],
                        "new": [
                            {
                                "Subnet": ipv4_net.subnet,
                                "Gateway": ipv4_net.gateway
                            },
                            {
                                "Subnet": ipv6_net2.subnet,
                                "Gateway": ipv6_net2.gateway
                            },
                        ],
                    }
                }
            },
        }
        assert ret.changes
        assert ret.changes == expected
        assert ret.comment == "Network '{}' was replaced with updated config".format(
            ipv4_net.name)
Exemplo n.º 16
0
def test_absent_when_not_present(network, docker_network):
    with network() as net:
        ret = docker_network.absent(name=net.name)
        assert ret.result is True
        assert not ret.changes
        assert ret.comment == "Network '{}' already absent".format(net.name)