예제 #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")]
예제 #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
예제 #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
예제 #4
0
파일: bundle.py 프로젝트: GM-Alex/compose
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
예제 #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
예제 #6
0
 def test_port_and_range_invalid(self):
     with pytest.raises(ValueError):
         split_port("0.0.0.0:1000:2000-2002/tcp")
예제 #7
0
 def test_host_only_with_colon(self):
     with pytest.raises(ValueError):
         split_port("localhost:")
예제 #8
0
 def test_port_and_range_invalid(self):
     self.assertRaises(ValueError,
                       lambda: split_port("0.0.0.0:1000:2000-2002/tcp"))
예제 #9
0
 def test_split_port_empty_string(self):
     with pytest.raises(ValueError):
         split_port("")
예제 #10
0
파일: service.py 프로젝트: husobee/compose
    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
예제 #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"])
예제 #12
0
 def test_port_and_range_invalid(self):
     self.assertRaises(ValueError,
                       lambda: split_port("0.0.0.0:1000:2000-2002/tcp"))
예제 #13
0
 def test_split_port_empty_string(self):
     self.assertRaises(ValueError, lambda: split_port(""))
예제 #14
0
 def test_split_port_invalid(self):
     self.assertRaises(ValueError,
                       lambda: split_port("0.0.0.0:1000:2000:tcp"))
예제 #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"))
예제 #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")])
예제 #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)
예제 #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"])
예제 #19
0
 def test_split_port_empty_string(self):
     with pytest.raises(ValueError):
         split_port("")
예제 #20
0
 def test_port_only_with_colon(self):
     self.assertRaises(ValueError, lambda: split_port(":80"))
예제 #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")])
예제 #22
0
 def test_host_only_with_colon(self):
     self.assertRaises(ValueError, lambda: split_port("localhost:"))
예제 #23
0
def format_ports(instance):
    try:
        split_port(instance)
    except ValueError as e:
        raise ValidationError(six.text_type(e))
    return True
예제 #24
0
def format_ports(instance):
    try:
        split_port(instance)
    except ValueError as e:
        raise ValidationError(six.text_type(e))
    return True
예제 #25
0
 def test_split_port_non_string(self):
     assert split_port(1243) == (['1243'], None)
예제 #26
0
파일: validation.py 프로젝트: alunduil/fig
def format_ports(instance):
    try:
        split_port(instance)
    except ValueError:
        return False
    return True
예제 #27
0
 def test_split_port_invalid(self):
     self.assertRaises(ValueError,
                       lambda: split_port("0.0.0.0:1000:2000:tcp"))
예제 #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")]
예제 #29
0
 def test_host_only_with_colon(self):
     self.assertRaises(ValueError,
                       lambda: split_port("localhost:"))
예제 #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")]
예제 #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")
예제 #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)]
예제 #33
0
 def test_port_only_with_colon(self):
     with pytest.raises(ValueError):
         split_port(":80")
예제 #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"]
예제 #35
0
 def test_with_no_container_port(self):
     with pytest.raises(ValueError):
         split_port("localhost:80:")
예제 #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"]
예제 #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")])
예제 #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
예제 #39
0
 def test_with_no_container_port(self):
     self.assertRaises(ValueError,
                       lambda: split_port("localhost:80:"))
예제 #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
예제 #41
0
 def test_split_port_non_string(self):
     assert split_port(1243) == (['1243'], None)
예제 #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")])
예제 #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
예제 #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"])
예제 #45
0
파일: service.py 프로젝트: mnowster/compose
    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
예제 #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)
예제 #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")]
예제 #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)])
예제 #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)])
예제 #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")]
예제 #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"])
예제 #52
0
 def test_split_port_invalid(self):
     with pytest.raises(ValueError):
         split_port("0.0.0.0:1000:2000:tcp")
예제 #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")])
예제 #54
0
 def test_split_port_invalid_protocol(self):
     with pytest.raises(ValueError):
         split_port("0.0.0.0:1000:2000/ftp")
예제 #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")
     )
예제 #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")
예제 #57
0
 def test_port_only_with_colon(self):
     self.assertRaises(ValueError,
                       lambda: split_port(":80"))
예제 #58
0
 def test_port_and_range_invalid(self):
     with pytest.raises(ValueError):
         split_port("0.0.0.0:1000:2000-2002/tcp")
예제 #59
0
def format_ports(instance):
    try:
        split_port(instance)
    except ValueError:
        return False
    return True
예제 #60
0
 def test_port_only_with_colon(self):
     with pytest.raises(ValueError):
         split_port(":80")