Exemple #1
0
 def test_split_port_with_protocol(self):
     for protocol in ['tcp', 'udp', 'sctp']:
         internal_port, external_port = split_port(
             "127.0.0.1:1000:2000/" + protocol
         )
         assert internal_port == ["2000/" + protocol]
         assert external_port == [("127.0.0.1", "1000")]
Exemple #2
0
def build_container_ports(container_options, options):
    ports = []
    all_ports = container_options.get("ports", []) + options.get("expose", [])
    for port_range in all_ports:
        internal_range, _ = split_port(port_range)
        for port in internal_range:
            port = str(port)
            if "/" in port:
                port = tuple(port.split("/"))
            ports.append(port)
    return ports
Exemple #3
0
def build_container_ports(container_ports, options):
    ports = []
    all_ports = container_ports + options.get('expose', [])
    for port_range in all_ports:
        internal_range, _ = split_port(port_range)
        for port in internal_range:
            port = str(port)
            if '/' in port:
                port = tuple(port.split('/'))
            ports.append(port)
    return ports
Exemple #4
0
def make_port_specs(service_dict):
    ports = []

    internal_ports = [
        internal_port
        for port_def in service_dict.get('ports', [])
        for internal_port in split_port(port_def)[0]
    ]

    internal_ports += service_dict.get('expose', [])

    for internal_port in internal_ports:
        spec = make_port_spec(internal_port)
        if spec not in ports:
            ports.append(spec)

    return ports
Exemple #5
0
        def has_host_port(binding):
            _, external_bindings = split_port(binding)

            # there are no external bindings
            if external_bindings is None:
                return False

            # we only need to check the first binding from the range
            external_binding = external_bindings[0]

            # non-tuple binding means there is a host port specified
            if not isinstance(external_binding, tuple):
                return True

            # extract actual host port from tuple of (host_ip, host_port)
            _, host_port = external_binding
            if host_port is not None:
                return True

            return False
Exemple #6
0
 def test_port_and_range_invalid(self):
     with pytest.raises(ValueError):
         split_port("0.0.0.0:1000:2000-2002/tcp")
Exemple #7
0
 def test_host_only_with_colon(self):
     with pytest.raises(ValueError):
         split_port("localhost:")
Exemple #8
0
 def test_port_and_range_invalid(self):
     self.assertRaises(ValueError,
                       lambda: split_port("0.0.0.0:1000:2000-2002/tcp"))
Exemple #9
0
 def test_split_port_empty_string(self):
     with pytest.raises(ValueError):
         split_port("")
Exemple #10
0
    def _get_container_create_options(self, override_options, number, one_off=False, previous_container=None):
        add_config_hash = not one_off and not override_options

        container_options = dict((k, self.options[k]) for k in DOCKER_CONFIG_KEYS if k in self.options)
        container_options.update(override_options)

        if self.custom_container_name() and not one_off:
            container_options["name"] = self.custom_container_name()
        elif not container_options.get("name"):
            container_options["name"] = self.get_container_name(number, one_off)

        if "detach" not in container_options:
            container_options["detach"] = True

        # If a qualified hostname was given, split it into an
        # unqualified hostname and a domainname unless domainname
        # was also given explicitly. This matches the behavior of
        # the official Docker CLI in that scenario.
        if (
            "hostname" in container_options
            and "domainname" not in container_options
            and "." in container_options["hostname"]
        ):
            parts = container_options["hostname"].partition(".")
            container_options["hostname"] = parts[0]
            container_options["domainname"] = parts[2]

        if "ports" in container_options or "expose" in self.options:
            ports = []
            all_ports = container_options.get("ports", []) + self.options.get("expose", [])
            for port_range in all_ports:
                internal_range, _ = split_port(port_range)
                for port in internal_range:
                    port = str(port)
                    if "/" in port:
                        port = tuple(port.split("/"))
                    ports.append(port)
            container_options["ports"] = ports

        override_options["binds"] = merge_volume_bindings(container_options.get("volumes") or [], previous_container)

        if "volumes" in container_options:
            container_options["volumes"] = dict((v.internal, {}) for v in container_options["volumes"])

        container_options["environment"] = merge_environment(
            self.options.get("environment"), override_options.get("environment")
        )

        if previous_container:
            container_options["environment"]["affinity:container"] = "=" + previous_container.id

        container_options["image"] = self.image_name

        container_options["labels"] = build_container_labels(
            container_options.get("labels", {}),
            self.labels(one_off=one_off),
            number,
            self.config_hash if add_config_hash else None,
        )

        # Delete options which are only used when starting
        for key in DOCKER_START_KEYS:
            container_options.pop(key, None)

        container_options["host_config"] = self._get_container_host_config(override_options, one_off=one_off)

        return container_options
Exemple #11
0
 def test_split_port_with_host_port(self):
     internal_port, external_port = split_port("1000:2000")
     self.assertEqual(internal_port, ["2000"])
     self.assertEqual(external_port, ["1000"])
Exemple #12
0
 def test_port_and_range_invalid(self):
     self.assertRaises(ValueError,
                       lambda: split_port("0.0.0.0:1000:2000-2002/tcp"))
Exemple #13
0
 def test_split_port_empty_string(self):
     self.assertRaises(ValueError, lambda: split_port(""))
Exemple #14
0
 def test_split_port_invalid(self):
     self.assertRaises(ValueError,
                       lambda: split_port("0.0.0.0:1000:2000:tcp"))
Exemple #15
0
 def test_non_matching_length_port_ranges(self):
     self.assertRaises(
         ValueError, lambda: split_port("0.0.0.0:1000-1010:2000-2002/tcp"))
Exemple #16
0
 def test_split_port_range_with_protocol(self):
     internal_port, external_port = split_port(
         "127.0.0.1:1000-1001:2000-2001/udp")
     self.assertEqual(internal_port, ["2000/udp", "2001/udp"])
     self.assertEqual(external_port, [("127.0.0.1", "1000"),
                                      ("127.0.0.1", "1001")])
Exemple #17
0
 def test_split_port_range_no_host_port(self):
     internal_port, external_port = split_port("2000-2001")
     self.assertEqual(internal_port, ["2000", "2001"])
     self.assertEqual(external_port, None)
Exemple #18
0
 def test_split_port_range_with_host_port(self):
     internal_port, external_port = split_port("1000-1001:2000-2001")
     self.assertEqual(internal_port, ["2000", "2001"])
     self.assertEqual(external_port, ["1000", "1001"])
Exemple #19
0
 def test_split_port_empty_string(self):
     with pytest.raises(ValueError):
         split_port("")
Exemple #20
0
 def test_port_only_with_colon(self):
     self.assertRaises(ValueError, lambda: split_port(":80"))
Exemple #21
0
 def test_split_port_with_ipv6_address(self):
     internal_port, external_port = split_port(
         "2001:abcd:ef00::2:1000:2000")
     self.assertEqual(internal_port, ["2000"])
     self.assertEqual(external_port, [("2001:abcd:ef00::2", "1000")])
Exemple #22
0
 def test_host_only_with_colon(self):
     self.assertRaises(ValueError, lambda: split_port("localhost:"))
Exemple #23
0
def format_ports(instance):
    try:
        split_port(instance)
    except ValueError as e:
        raise ValidationError(six.text_type(e))
    return True
Exemple #24
0
def format_ports(instance):
    try:
        split_port(instance)
    except ValueError as e:
        raise ValidationError(six.text_type(e))
    return True
Exemple #25
0
 def test_split_port_non_string(self):
     assert split_port(1243) == (['1243'], None)
Exemple #26
0
def format_ports(instance):
    try:
        split_port(instance)
    except ValueError:
        return False
    return True
Exemple #27
0
 def test_split_port_invalid(self):
     self.assertRaises(ValueError,
                       lambda: split_port("0.0.0.0:1000:2000:tcp"))
Exemple #28
0
 def test_split_port_with_host_ip(self):
     internal_port, external_port = split_port("127.0.0.1:1000:2000")
     assert internal_port == ["2000"]
     assert external_port == [("127.0.0.1", "1000")]
Exemple #29
0
 def test_host_only_with_colon(self):
     self.assertRaises(ValueError,
                       lambda: split_port("localhost:"))
Exemple #30
0
 def test_split_port_with_protocol(self):
     for protocol in ['tcp', 'udp', 'sctp']:
         internal_port, external_port = split_port("127.0.0.1:1000:2000/" +
                                                   protocol)
         assert internal_port == ["2000/" + protocol]
         assert external_port == [("127.0.0.1", "1000")]
Exemple #31
0
 def test_non_matching_length_port_ranges(self):
     with pytest.raises(ValueError):
         split_port("0.0.0.0:1000-1010:2000-2002/tcp")
Exemple #32
0
 def test_split_port_range_with_host_ip_no_port(self):
     internal_port, external_port = split_port("127.0.0.1::2000-2001")
     assert internal_port == ["2000", "2001"]
     assert external_port == [("127.0.0.1", None), ("127.0.0.1", None)]
Exemple #33
0
 def test_port_only_with_colon(self):
     with pytest.raises(ValueError):
         split_port(":80")
Exemple #34
0
 def test_split_port_with_host_port(self):
     internal_port, external_port = split_port("1000:2000")
     assert internal_port == ["2000"]
     assert external_port == ["1000"]
Exemple #35
0
 def test_with_no_container_port(self):
     with pytest.raises(ValueError):
         split_port("localhost:80:")
Exemple #36
0
 def test_split_port_random_port_range_with_host_port(self):
     internal_port, external_port = split_port("1000-1001:2000")
     assert internal_port == ["2000"]
     assert external_port == ["1000-1001"]
Exemple #37
0
 def test_split_port_with_host_ip(self):
     internal_port, external_port = split_port("127.0.0.1:1000:2000")
     self.assertEqual(internal_port, ["2000"])
     self.assertEqual(external_port, [("127.0.0.1", "1000")])
Exemple #38
0
 def test_split_port_no_host_port(self):
     internal_port, external_port = split_port("2000")
     assert internal_port == ["2000"]
     assert external_port is None
Exemple #39
0
 def test_with_no_container_port(self):
     self.assertRaises(ValueError,
                       lambda: split_port("localhost:80:"))
Exemple #40
0
 def test_split_port_range_no_host_port(self):
     internal_port, external_port = split_port("2000-2001")
     assert internal_port == ["2000", "2001"]
     assert external_port is None
Exemple #41
0
 def test_split_port_non_string(self):
     assert split_port(1243) == (['1243'], None)
Exemple #42
0
 def test_split_port_with_host_ip(self):
     internal_port, external_port = split_port("127.0.0.1:1000:2000")
     self.assertEqual(internal_port, ["2000"])
     self.assertEqual(external_port, [("127.0.0.1", "1000")])
Exemple #43
0
    def _get_container_create_options(self,
                                      override_options,
                                      number,
                                      one_off=False,
                                      previous_container=None):
        add_config_hash = (not one_off and not override_options)

        container_options = dict((k, self.options[k])
                                 for k in DOCKER_CONFIG_KEYS
                                 if k in self.options)
        container_options.update(override_options)

        if self.custom_container_name() and not one_off:
            container_options['name'] = self.custom_container_name()
        elif not container_options.get('name'):
            container_options['name'] = self.get_container_name(
                number, one_off)

        if 'detach' not in container_options:
            container_options['detach'] = True

        # If a qualified hostname was given, split it into an
        # unqualified hostname and a domainname unless domainname
        # was also given explicitly. This matches the behavior of
        # the official Docker CLI in that scenario.
        if ('hostname' in container_options
                and 'domainname' not in container_options
                and '.' in container_options['hostname']):
            parts = container_options['hostname'].partition('.')
            container_options['hostname'] = parts[0]
            container_options['domainname'] = parts[2]

        if 'ports' in container_options or 'expose' in self.options:
            ports = []
            all_ports = container_options.get('ports', []) + self.options.get(
                'expose', [])
            for port_range in all_ports:
                internal_range, _ = split_port(port_range)
                for port in internal_range:
                    port = str(port)
                    if '/' in port:
                        port = tuple(port.split('/'))
                    ports.append(port)
            container_options['ports'] = ports

        override_options['binds'] = merge_volume_bindings(
            container_options.get('volumes') or [], previous_container)

        if 'volumes' in container_options:
            container_options['volumes'] = dict(
                (v.internal, {}) for v in container_options['volumes'])

        container_options['environment'] = merge_environment(
            self.options.get('environment'),
            override_options.get('environment'))

        if previous_container:
            container_options['environment']['affinity:container'] = (
                '=' + previous_container.id)

        container_options['image'] = self.image_name

        container_options['labels'] = build_container_labels(
            container_options.get('labels', {}), self.labels(one_off=one_off),
            number, self.config_hash if add_config_hash else None)

        # Delete options which are only used when starting
        for key in DOCKER_START_KEYS:
            container_options.pop(key, None)

        container_options['host_config'] = self._get_container_host_config(
            override_options, one_off=one_off)

        return container_options
Exemple #44
0
 def test_split_port_with_host_port(self):
     internal_port, external_port = split_port("1000:2000")
     self.assertEqual(internal_port, ["2000"])
     self.assertEqual(external_port, ["1000"])
Exemple #45
0
    def _get_container_create_options(
            self,
            override_options,
            number,
            one_off=False,
            previous_container=None):
        add_config_hash = (not one_off and not override_options)

        container_options = dict(
            (k, self.options[k])
            for k in DOCKER_CONFIG_KEYS if k in self.options)
        container_options.update(override_options)

        if self.custom_container_name() and not one_off:
            container_options['name'] = self.custom_container_name()
        elif not container_options.get('name'):
            container_options['name'] = self.get_container_name(number, one_off)

        if 'detach' not in container_options:
            container_options['detach'] = True

        # If a qualified hostname was given, split it into an
        # unqualified hostname and a domainname unless domainname
        # was also given explicitly. This matches the behavior of
        # the official Docker CLI in that scenario.
        if ('hostname' in container_options
                and 'domainname' not in container_options
                and '.' in container_options['hostname']):
            parts = container_options['hostname'].partition('.')
            container_options['hostname'] = parts[0]
            container_options['domainname'] = parts[2]

        if 'hostname' not in container_options and self.use_networking:
            container_options['hostname'] = self.name

        if 'ports' in container_options or 'expose' in self.options:
            ports = []
            all_ports = container_options.get('ports', []) + self.options.get('expose', [])
            for port_range in all_ports:
                internal_range, _ = split_port(port_range)
                for port in internal_range:
                    port = str(port)
                    if '/' in port:
                        port = tuple(port.split('/'))
                    ports.append(port)
            container_options['ports'] = ports

        override_options['binds'] = merge_volume_bindings(
            container_options.get('volumes') or [],
            previous_container)

        if 'volumes' in container_options:
            container_options['volumes'] = dict(
                (parse_volume_spec(v).internal, {})
                for v in container_options['volumes'])

        container_options['environment'] = merge_environment(
            self.options.get('environment'),
            override_options.get('environment'))

        if previous_container:
            container_options['environment']['affinity:container'] = ('=' + previous_container.id)

        container_options['image'] = self.image_name

        container_options['labels'] = build_container_labels(
            container_options.get('labels', {}),
            self.labels(one_off=one_off),
            number,
            self.config_hash if add_config_hash else None)

        # Delete options which are only used when starting
        for key in DOCKER_START_KEYS:
            container_options.pop(key, None)

        container_options['host_config'] = self._get_container_host_config(
            override_options,
            one_off=one_off)

        return container_options
Exemple #46
0
 def test_split_port_range_no_host_port(self):
     internal_port, external_port = split_port("2000-2001")
     self.assertEqual(internal_port, ["2000", "2001"])
     self.assertEqual(external_port, None)
Exemple #47
0
 def test_split_port_range_with_protocol(self):
     internal_port, external_port = split_port(
         "127.0.0.1:1000-1001:2000-2001/udp")
     assert internal_port == ["2000/udp", "2001/udp"]
     assert external_port == [("127.0.0.1", "1000"), ("127.0.0.1", "1001")]
Exemple #48
0
 def test_split_port_range_with_host_ip_no_port(self):
     internal_port, external_port = split_port("127.0.0.1::2000-2001")
     self.assertEqual(internal_port, ["2000", "2001"])
     self.assertEqual(external_port, [("127.0.0.1", None),
                                      ("127.0.0.1", None)])
Exemple #49
0
 def test_split_port_range_with_host_ip_no_port(self):
     internal_port, external_port = split_port("127.0.0.1::2000-2001")
     self.assertEqual(internal_port, ["2000", "2001"])
     self.assertEqual(external_port,
                      [("127.0.0.1", None), ("127.0.0.1", None)])
Exemple #50
0
 def test_split_port_with_ipv6_address(self):
     internal_port, external_port = split_port(
         "2001:abcd:ef00::2:1000:2000")
     assert internal_port == ["2000"]
     assert external_port == [("2001:abcd:ef00::2", "1000")]
Exemple #51
0
 def test_split_port_range_with_host_port(self):
     internal_port, external_port = split_port("1000-1001:2000-2001")
     self.assertEqual(internal_port, ["2000", "2001"])
     self.assertEqual(external_port, ["1000", "1001"])
Exemple #52
0
 def test_split_port_invalid(self):
     with pytest.raises(ValueError):
         split_port("0.0.0.0:1000:2000:tcp")
Exemple #53
0
 def test_split_port_range_with_protocol(self):
     internal_port, external_port = split_port(
         "127.0.0.1:1000-1001:2000-2001/udp")
     self.assertEqual(internal_port, ["2000/udp", "2001/udp"])
     self.assertEqual(external_port,
                      [("127.0.0.1", "1000"), ("127.0.0.1", "1001")])
Exemple #54
0
 def test_split_port_invalid_protocol(self):
     with pytest.raises(ValueError):
         split_port("0.0.0.0:1000:2000/ftp")
Exemple #55
0
 def test_non_matching_length_port_ranges(self):
     self.assertRaises(
         ValueError,
         lambda: split_port("0.0.0.0:1000-1010:2000-2002/tcp")
     )
Exemple #56
0
 def test_non_matching_length_port_ranges(self):
     with pytest.raises(ValueError):
         split_port("0.0.0.0:1000-1010:2000-2002/tcp")
Exemple #57
0
 def test_port_only_with_colon(self):
     self.assertRaises(ValueError,
                       lambda: split_port(":80"))
Exemple #58
0
 def test_port_and_range_invalid(self):
     with pytest.raises(ValueError):
         split_port("0.0.0.0:1000:2000-2002/tcp")
Exemple #59
0
def format_ports(instance):
    try:
        split_port(instance)
    except ValueError:
        return False
    return True
Exemple #60
0
 def test_port_only_with_colon(self):
     with pytest.raises(ValueError):
         split_port(":80")