Signed-off-by: David Calavera <david.calavera@gmail.com>
| ... | ... |
@@ -6,9 +6,9 @@ import ( |
| 6 | 6 |
|
| 7 | 7 |
"github.com/docker/docker/api/types/network" |
| 8 | 8 |
"github.com/docker/docker/api/types/registry" |
| 9 |
- "github.com/docker/docker/pkg/nat" |
|
| 10 | 9 |
"github.com/docker/docker/pkg/version" |
| 11 | 10 |
"github.com/docker/docker/runconfig" |
| 11 |
+ "github.com/docker/go-connections/nat" |
|
| 12 | 12 |
) |
| 13 | 13 |
|
| 14 | 14 |
// ContainerCreateResponse contains the information returned to a client on the |
| ... | ... |
@@ -4,8 +4,8 @@ package v1p19 |
| 4 | 4 |
import ( |
| 5 | 5 |
"github.com/docker/docker/api/types" |
| 6 | 6 |
"github.com/docker/docker/api/types/versions/v1p20" |
| 7 |
- "github.com/docker/docker/pkg/nat" |
|
| 8 | 7 |
"github.com/docker/docker/runconfig" |
| 8 |
+ "github.com/docker/go-connections/nat" |
|
| 9 | 9 |
) |
| 10 | 10 |
|
| 11 | 11 |
// ContainerJSON is a backcompatibility struct for APIs prior to 1.20. |
| ... | ... |
@@ -22,10 +22,10 @@ import ( |
| 22 | 22 |
"github.com/docker/docker/builder" |
| 23 | 23 |
derr "github.com/docker/docker/errors" |
| 24 | 24 |
flag "github.com/docker/docker/pkg/mflag" |
| 25 |
- "github.com/docker/docker/pkg/nat" |
|
| 26 | 25 |
"github.com/docker/docker/pkg/signal" |
| 27 | 26 |
"github.com/docker/docker/pkg/system" |
| 28 | 27 |
"github.com/docker/docker/runconfig" |
| 28 |
+ "github.com/docker/go-connections/nat" |
|
| 29 | 29 |
) |
| 30 | 30 |
|
| 31 | 31 |
const ( |
| ... | ... |
@@ -19,12 +19,12 @@ import ( |
| 19 | 19 |
derr "github.com/docker/docker/errors" |
| 20 | 20 |
"github.com/docker/docker/image" |
| 21 | 21 |
"github.com/docker/docker/layer" |
| 22 |
- "github.com/docker/docker/pkg/nat" |
|
| 23 | 22 |
"github.com/docker/docker/pkg/promise" |
| 24 | 23 |
"github.com/docker/docker/pkg/signal" |
| 25 | 24 |
"github.com/docker/docker/pkg/symlink" |
| 26 | 25 |
"github.com/docker/docker/runconfig" |
| 27 | 26 |
"github.com/docker/docker/volume" |
| 27 |
+ "github.com/docker/go-connections/nat" |
|
| 28 | 28 |
"github.com/opencontainers/runc/libcontainer/label" |
| 29 | 29 |
) |
| 30 | 30 |
|
| ... | ... |
@@ -17,11 +17,11 @@ import ( |
| 17 | 17 |
"github.com/docker/docker/daemon/execdriver" |
| 18 | 18 |
derr "github.com/docker/docker/errors" |
| 19 | 19 |
"github.com/docker/docker/pkg/chrootarchive" |
| 20 |
- "github.com/docker/docker/pkg/nat" |
|
| 21 | 20 |
"github.com/docker/docker/pkg/symlink" |
| 22 | 21 |
"github.com/docker/docker/pkg/system" |
| 23 | 22 |
"github.com/docker/docker/utils" |
| 24 | 23 |
"github.com/docker/docker/volume" |
| 24 |
+ "github.com/docker/go-connections/nat" |
|
| 25 | 25 |
"github.com/docker/libnetwork" |
| 26 | 26 |
"github.com/docker/libnetwork/netlabel" |
| 27 | 27 |
"github.com/docker/libnetwork/options" |
| ... | ... |
@@ -49,7 +49,6 @@ import ( |
| 49 | 49 |
"github.com/docker/docker/pkg/jsonmessage" |
| 50 | 50 |
"github.com/docker/docker/pkg/mount" |
| 51 | 51 |
"github.com/docker/docker/pkg/namesgenerator" |
| 52 |
- "github.com/docker/docker/pkg/nat" |
|
| 53 | 52 |
"github.com/docker/docker/pkg/progress" |
| 54 | 53 |
"github.com/docker/docker/pkg/signal" |
| 55 | 54 |
"github.com/docker/docker/pkg/streamformatter" |
| ... | ... |
@@ -64,6 +63,7 @@ import ( |
| 64 | 64 |
volumedrivers "github.com/docker/docker/volume/drivers" |
| 65 | 65 |
"github.com/docker/docker/volume/local" |
| 66 | 66 |
"github.com/docker/docker/volume/store" |
| 67 |
+ "github.com/docker/go-connections/nat" |
|
| 67 | 68 |
"github.com/docker/libnetwork" |
| 68 | 69 |
lntypes "github.com/docker/libnetwork/types" |
| 69 | 70 |
"github.com/docker/libtrust" |
| ... | ... |
@@ -12,7 +12,7 @@ import ( |
| 12 | 12 |
"github.com/docker/docker/container" |
| 13 | 13 |
"github.com/docker/docker/image" |
| 14 | 14 |
"github.com/docker/docker/pkg/graphdb" |
| 15 |
- "github.com/docker/docker/pkg/nat" |
|
| 15 |
+ "github.com/docker/go-connections/nat" |
|
| 16 | 16 |
) |
| 17 | 17 |
|
| 18 | 18 |
// iterationAction represents possible outcomes happening during the container iteration. |
| ... | ... |
@@ -20,8 +20,8 @@ import ( |
| 20 | 20 |
|
| 21 | 21 |
"github.com/docker/docker/pkg/integration/checker" |
| 22 | 22 |
"github.com/docker/docker/pkg/mount" |
| 23 |
- "github.com/docker/docker/pkg/nat" |
|
| 24 | 23 |
"github.com/docker/docker/runconfig" |
| 24 |
+ "github.com/docker/go-connections/nat" |
|
| 25 | 25 |
"github.com/docker/libnetwork/resolvconf" |
| 26 | 26 |
"github.com/go-check/check" |
| 27 | 27 |
) |
| 28 | 28 |
deleted file mode 100644 |
| ... | ... |
@@ -1,227 +0,0 @@ |
| 1 |
-package nat |
|
| 2 |
- |
|
| 3 |
-// nat is a convenience package for docker's manipulation of strings describing |
|
| 4 |
-// network ports. |
|
| 5 |
- |
|
| 6 |
-import ( |
|
| 7 |
- "fmt" |
|
| 8 |
- "net" |
|
| 9 |
- "strconv" |
|
| 10 |
- "strings" |
|
| 11 |
- |
|
| 12 |
- "github.com/docker/docker/pkg/parsers" |
|
| 13 |
-) |
|
| 14 |
- |
|
| 15 |
-const ( |
|
| 16 |
- // portSpecTemplate is the expected format for port specifications |
|
| 17 |
- portSpecTemplate = "ip:hostPort:containerPort" |
|
| 18 |
-) |
|
| 19 |
- |
|
| 20 |
-// PortBinding represents a binding between a Host IP address and a Host Port |
|
| 21 |
-type PortBinding struct {
|
|
| 22 |
- // HostIP is the host IP Address |
|
| 23 |
- HostIP string `json:"HostIp"` |
|
| 24 |
- // HostPort is the host port number |
|
| 25 |
- HostPort string |
|
| 26 |
-} |
|
| 27 |
- |
|
| 28 |
-// PortMap is a collection of PortBinding indexed by Port |
|
| 29 |
-type PortMap map[Port][]PortBinding |
|
| 30 |
- |
|
| 31 |
-// PortSet is a collection of structs indexed by Port |
|
| 32 |
-type PortSet map[Port]struct{}
|
|
| 33 |
- |
|
| 34 |
-// Port is a string containing port number and protocol in the format "80/tcp" |
|
| 35 |
-type Port string |
|
| 36 |
- |
|
| 37 |
-// NewPort creates a new instance of a Port given a protocol and port number or port range |
|
| 38 |
-func NewPort(proto, port string) (Port, error) {
|
|
| 39 |
- // Check for parsing issues on "port" now so we can avoid having |
|
| 40 |
- // to check it later on. |
|
| 41 |
- |
|
| 42 |
- portStartInt, portEndInt, err := ParsePortRange(port) |
|
| 43 |
- if err != nil {
|
|
| 44 |
- return "", err |
|
| 45 |
- } |
|
| 46 |
- |
|
| 47 |
- if portStartInt == portEndInt {
|
|
| 48 |
- return Port(fmt.Sprintf("%d/%s", portStartInt, proto)), nil
|
|
| 49 |
- } |
|
| 50 |
- return Port(fmt.Sprintf("%d-%d/%s", portStartInt, portEndInt, proto)), nil
|
|
| 51 |
-} |
|
| 52 |
- |
|
| 53 |
-// ParsePort parses the port number string and returns an int |
|
| 54 |
-func ParsePort(rawPort string) (int, error) {
|
|
| 55 |
- if len(rawPort) == 0 {
|
|
| 56 |
- return 0, nil |
|
| 57 |
- } |
|
| 58 |
- port, err := strconv.ParseUint(rawPort, 10, 16) |
|
| 59 |
- if err != nil {
|
|
| 60 |
- return 0, err |
|
| 61 |
- } |
|
| 62 |
- return int(port), nil |
|
| 63 |
-} |
|
| 64 |
- |
|
| 65 |
-// ParsePortRange parses the port range string and returns start/end ints |
|
| 66 |
-func ParsePortRange(rawPort string) (int, int, error) {
|
|
| 67 |
- if len(rawPort) == 0 {
|
|
| 68 |
- return 0, 0, nil |
|
| 69 |
- } |
|
| 70 |
- start, end, err := parsers.ParsePortRange(rawPort) |
|
| 71 |
- if err != nil {
|
|
| 72 |
- return 0, 0, err |
|
| 73 |
- } |
|
| 74 |
- return int(start), int(end), nil |
|
| 75 |
-} |
|
| 76 |
- |
|
| 77 |
-// Proto returns the protocol of a Port |
|
| 78 |
-func (p Port) Proto() string {
|
|
| 79 |
- proto, _ := SplitProtoPort(string(p)) |
|
| 80 |
- return proto |
|
| 81 |
-} |
|
| 82 |
- |
|
| 83 |
-// Port returns the port number of a Port |
|
| 84 |
-func (p Port) Port() string {
|
|
| 85 |
- _, port := SplitProtoPort(string(p)) |
|
| 86 |
- return port |
|
| 87 |
-} |
|
| 88 |
- |
|
| 89 |
-// Int returns the port number of a Port as an int |
|
| 90 |
-func (p Port) Int() int {
|
|
| 91 |
- portStr := p.Port() |
|
| 92 |
- if len(portStr) == 0 {
|
|
| 93 |
- return 0 |
|
| 94 |
- } |
|
| 95 |
- |
|
| 96 |
- // We don't need to check for an error because we're going to |
|
| 97 |
- // assume that any error would have been found, and reported, in NewPort() |
|
| 98 |
- port, _ := strconv.ParseUint(portStr, 10, 16) |
|
| 99 |
- return int(port) |
|
| 100 |
-} |
|
| 101 |
- |
|
| 102 |
-// Range returns the start/end port numbers of a Port range as ints |
|
| 103 |
-func (p Port) Range() (int, int, error) {
|
|
| 104 |
- return ParsePortRange(p.Port()) |
|
| 105 |
-} |
|
| 106 |
- |
|
| 107 |
-// SplitProtoPort splits a port in the format of proto/port |
|
| 108 |
-func SplitProtoPort(rawPort string) (string, string) {
|
|
| 109 |
- parts := strings.Split(rawPort, "/") |
|
| 110 |
- l := len(parts) |
|
| 111 |
- if len(rawPort) == 0 || l == 0 || len(parts[0]) == 0 {
|
|
| 112 |
- return "", "" |
|
| 113 |
- } |
|
| 114 |
- if l == 1 {
|
|
| 115 |
- return "tcp", rawPort |
|
| 116 |
- } |
|
| 117 |
- if len(parts[1]) == 0 {
|
|
| 118 |
- return "tcp", parts[0] |
|
| 119 |
- } |
|
| 120 |
- return parts[1], parts[0] |
|
| 121 |
-} |
|
| 122 |
- |
|
| 123 |
-func validateProto(proto string) bool {
|
|
| 124 |
- for _, availableProto := range []string{"tcp", "udp"} {
|
|
| 125 |
- if availableProto == proto {
|
|
| 126 |
- return true |
|
| 127 |
- } |
|
| 128 |
- } |
|
| 129 |
- return false |
|
| 130 |
-} |
|
| 131 |
- |
|
| 132 |
-// ParsePortSpecs receives port specs in the format of ip:public:private/proto and parses |
|
| 133 |
-// these in to the internal types |
|
| 134 |
-func ParsePortSpecs(ports []string) (map[Port]struct{}, map[Port][]PortBinding, error) {
|
|
| 135 |
- var ( |
|
| 136 |
- exposedPorts = make(map[Port]struct{}, len(ports))
|
|
| 137 |
- bindings = make(map[Port][]PortBinding) |
|
| 138 |
- ) |
|
| 139 |
- |
|
| 140 |
- for _, rawPort := range ports {
|
|
| 141 |
- proto := "tcp" |
|
| 142 |
- |
|
| 143 |
- if i := strings.LastIndex(rawPort, "/"); i != -1 {
|
|
| 144 |
- proto = rawPort[i+1:] |
|
| 145 |
- rawPort = rawPort[:i] |
|
| 146 |
- } |
|
| 147 |
- if !strings.Contains(rawPort, ":") {
|
|
| 148 |
- rawPort = fmt.Sprintf("::%s", rawPort)
|
|
| 149 |
- } else if len(strings.Split(rawPort, ":")) == 2 {
|
|
| 150 |
- rawPort = fmt.Sprintf(":%s", rawPort)
|
|
| 151 |
- } |
|
| 152 |
- |
|
| 153 |
- parts, err := parsers.PartParser(portSpecTemplate, rawPort) |
|
| 154 |
- if err != nil {
|
|
| 155 |
- return nil, nil, err |
|
| 156 |
- } |
|
| 157 |
- |
|
| 158 |
- var ( |
|
| 159 |
- containerPort = parts["containerPort"] |
|
| 160 |
- rawIP = parts["ip"] |
|
| 161 |
- hostPort = parts["hostPort"] |
|
| 162 |
- ) |
|
| 163 |
- |
|
| 164 |
- if rawIP != "" && net.ParseIP(rawIP) == nil {
|
|
| 165 |
- return nil, nil, fmt.Errorf("Invalid ip address: %s", rawIP)
|
|
| 166 |
- } |
|
| 167 |
- if containerPort == "" {
|
|
| 168 |
- return nil, nil, fmt.Errorf("No port specified: %s<empty>", rawPort)
|
|
| 169 |
- } |
|
| 170 |
- |
|
| 171 |
- startPort, endPort, err := parsers.ParsePortRange(containerPort) |
|
| 172 |
- if err != nil {
|
|
| 173 |
- return nil, nil, fmt.Errorf("Invalid containerPort: %s", containerPort)
|
|
| 174 |
- } |
|
| 175 |
- |
|
| 176 |
- var startHostPort, endHostPort uint64 = 0, 0 |
|
| 177 |
- if len(hostPort) > 0 {
|
|
| 178 |
- startHostPort, endHostPort, err = parsers.ParsePortRange(hostPort) |
|
| 179 |
- if err != nil {
|
|
| 180 |
- return nil, nil, fmt.Errorf("Invalid hostPort: %s", hostPort)
|
|
| 181 |
- } |
|
| 182 |
- } |
|
| 183 |
- |
|
| 184 |
- if hostPort != "" && (endPort-startPort) != (endHostPort-startHostPort) {
|
|
| 185 |
- // Allow host port range iff containerPort is not a range. |
|
| 186 |
- // In this case, use the host port range as the dynamic |
|
| 187 |
- // host port range to allocate into. |
|
| 188 |
- if endPort != startPort {
|
|
| 189 |
- return nil, nil, fmt.Errorf("Invalid ranges specified for container and host Ports: %s and %s", containerPort, hostPort)
|
|
| 190 |
- } |
|
| 191 |
- } |
|
| 192 |
- |
|
| 193 |
- if !validateProto(strings.ToLower(proto)) {
|
|
| 194 |
- return nil, nil, fmt.Errorf("Invalid proto: %s", proto)
|
|
| 195 |
- } |
|
| 196 |
- |
|
| 197 |
- for i := uint64(0); i <= (endPort - startPort); i++ {
|
|
| 198 |
- containerPort = strconv.FormatUint(startPort+i, 10) |
|
| 199 |
- if len(hostPort) > 0 {
|
|
| 200 |
- hostPort = strconv.FormatUint(startHostPort+i, 10) |
|
| 201 |
- } |
|
| 202 |
- // Set hostPort to a range only if there is a single container port |
|
| 203 |
- // and a dynamic host port. |
|
| 204 |
- if startPort == endPort && startHostPort != endHostPort {
|
|
| 205 |
- hostPort = fmt.Sprintf("%s-%s", hostPort, strconv.FormatUint(endHostPort, 10))
|
|
| 206 |
- } |
|
| 207 |
- port, err := NewPort(strings.ToLower(proto), containerPort) |
|
| 208 |
- if err != nil {
|
|
| 209 |
- return nil, nil, err |
|
| 210 |
- } |
|
| 211 |
- if _, exists := exposedPorts[port]; !exists {
|
|
| 212 |
- exposedPorts[port] = struct{}{}
|
|
| 213 |
- } |
|
| 214 |
- |
|
| 215 |
- binding := PortBinding{
|
|
| 216 |
- HostIP: rawIP, |
|
| 217 |
- HostPort: hostPort, |
|
| 218 |
- } |
|
| 219 |
- bslice, exists := bindings[port] |
|
| 220 |
- if !exists {
|
|
| 221 |
- bslice = []PortBinding{}
|
|
| 222 |
- } |
|
| 223 |
- bindings[port] = append(bslice, binding) |
|
| 224 |
- } |
|
| 225 |
- } |
|
| 226 |
- return exposedPorts, bindings, nil |
|
| 227 |
-} |
| 228 | 1 |
deleted file mode 100644 |
| ... | ... |
@@ -1,525 +0,0 @@ |
| 1 |
-package nat |
|
| 2 |
- |
|
| 3 |
-import ( |
|
| 4 |
- "testing" |
|
| 5 |
-) |
|
| 6 |
- |
|
| 7 |
-func TestParsePort(t *testing.T) {
|
|
| 8 |
- var ( |
|
| 9 |
- p int |
|
| 10 |
- err error |
|
| 11 |
- ) |
|
| 12 |
- |
|
| 13 |
- p, err = ParsePort("1234")
|
|
| 14 |
- |
|
| 15 |
- if err != nil || p != 1234 {
|
|
| 16 |
- t.Fatal("Parsing '1234' did not succeed")
|
|
| 17 |
- } |
|
| 18 |
- |
|
| 19 |
- // FIXME currently this is a valid port. I don't think it should be. |
|
| 20 |
- // I'm leaving this test commented out until we make a decision. |
|
| 21 |
- // - erikh |
|
| 22 |
- |
|
| 23 |
- /* |
|
| 24 |
- p, err = ParsePort("0123")
|
|
| 25 |
- |
|
| 26 |
- if err != nil {
|
|
| 27 |
- t.Fatal("Successfully parsed port '0123' to '123'")
|
|
| 28 |
- } |
|
| 29 |
- */ |
|
| 30 |
- |
|
| 31 |
- p, err = ParsePort("asdf")
|
|
| 32 |
- |
|
| 33 |
- if err == nil || p != 0 {
|
|
| 34 |
- t.Fatal("Parsing port 'asdf' succeeded")
|
|
| 35 |
- } |
|
| 36 |
- |
|
| 37 |
- p, err = ParsePort("1asdf")
|
|
| 38 |
- |
|
| 39 |
- if err == nil || p != 0 {
|
|
| 40 |
- t.Fatal("Parsing port '1asdf' succeeded")
|
|
| 41 |
- } |
|
| 42 |
-} |
|
| 43 |
- |
|
| 44 |
-func TestParsePortRange(t *testing.T) {
|
|
| 45 |
- var ( |
|
| 46 |
- begin int |
|
| 47 |
- end int |
|
| 48 |
- err error |
|
| 49 |
- ) |
|
| 50 |
- |
|
| 51 |
- type TestRange struct {
|
|
| 52 |
- Range string |
|
| 53 |
- Begin int |
|
| 54 |
- End int |
|
| 55 |
- } |
|
| 56 |
- validRanges := []TestRange{
|
|
| 57 |
- {"1234", 1234, 1234},
|
|
| 58 |
- {"1234-1234", 1234, 1234},
|
|
| 59 |
- {"1234-1235", 1234, 1235},
|
|
| 60 |
- {"8000-9000", 8000, 9000},
|
|
| 61 |
- {"0", 0, 0},
|
|
| 62 |
- {"0-0", 0, 0},
|
|
| 63 |
- } |
|
| 64 |
- |
|
| 65 |
- for _, r := range validRanges {
|
|
| 66 |
- begin, end, err = ParsePortRange(r.Range) |
|
| 67 |
- |
|
| 68 |
- if err != nil || begin != r.Begin {
|
|
| 69 |
- t.Fatalf("Parsing port range '%s' did not succeed. Expected begin %d, got %d", r.Range, r.Begin, begin)
|
|
| 70 |
- } |
|
| 71 |
- if err != nil || end != r.End {
|
|
| 72 |
- t.Fatalf("Parsing port range '%s' did not succeed. Expected end %d, got %d", r.Range, r.End, end)
|
|
| 73 |
- } |
|
| 74 |
- } |
|
| 75 |
- |
|
| 76 |
- invalidRanges := []string{
|
|
| 77 |
- "asdf", |
|
| 78 |
- "1asdf", |
|
| 79 |
- "9000-8000", |
|
| 80 |
- "9000-", |
|
| 81 |
- "-8000", |
|
| 82 |
- "-8000-", |
|
| 83 |
- } |
|
| 84 |
- |
|
| 85 |
- for _, r := range invalidRanges {
|
|
| 86 |
- begin, end, err = ParsePortRange(r) |
|
| 87 |
- |
|
| 88 |
- if err == nil || begin != 0 || end != 0 {
|
|
| 89 |
- t.Fatalf("Parsing port range '%s' succeeded", r)
|
|
| 90 |
- } |
|
| 91 |
- } |
|
| 92 |
-} |
|
| 93 |
- |
|
| 94 |
-func TestPort(t *testing.T) {
|
|
| 95 |
- p, err := NewPort("tcp", "1234")
|
|
| 96 |
- |
|
| 97 |
- if err != nil {
|
|
| 98 |
- t.Fatalf("tcp, 1234 had a parsing issue: %v", err)
|
|
| 99 |
- } |
|
| 100 |
- |
|
| 101 |
- if string(p) != "1234/tcp" {
|
|
| 102 |
- t.Fatal("tcp, 1234 did not result in the string 1234/tcp")
|
|
| 103 |
- } |
|
| 104 |
- |
|
| 105 |
- if p.Proto() != "tcp" {
|
|
| 106 |
- t.Fatal("protocol was not tcp")
|
|
| 107 |
- } |
|
| 108 |
- |
|
| 109 |
- if p.Port() != "1234" {
|
|
| 110 |
- t.Fatal("port string value was not 1234")
|
|
| 111 |
- } |
|
| 112 |
- |
|
| 113 |
- if p.Int() != 1234 {
|
|
| 114 |
- t.Fatal("port int value was not 1234")
|
|
| 115 |
- } |
|
| 116 |
- |
|
| 117 |
- p, err = NewPort("tcp", "asd1234")
|
|
| 118 |
- if err == nil {
|
|
| 119 |
- t.Fatal("tcp, asd1234 was supposed to fail")
|
|
| 120 |
- } |
|
| 121 |
- |
|
| 122 |
- p, err = NewPort("tcp", "1234-1230")
|
|
| 123 |
- if err == nil {
|
|
| 124 |
- t.Fatal("tcp, 1234-1230 was supposed to fail")
|
|
| 125 |
- } |
|
| 126 |
- |
|
| 127 |
- p, err = NewPort("tcp", "1234-1242")
|
|
| 128 |
- if err != nil {
|
|
| 129 |
- t.Fatalf("tcp, 1234-1242 had a parsing issue: %v", err)
|
|
| 130 |
- } |
|
| 131 |
- |
|
| 132 |
- if string(p) != "1234-1242/tcp" {
|
|
| 133 |
- t.Fatal("tcp, 1234-1242 did not result in the string 1234-1242/tcp")
|
|
| 134 |
- } |
|
| 135 |
-} |
|
| 136 |
- |
|
| 137 |
-func TestSplitProtoPort(t *testing.T) {
|
|
| 138 |
- var ( |
|
| 139 |
- proto string |
|
| 140 |
- port string |
|
| 141 |
- ) |
|
| 142 |
- |
|
| 143 |
- proto, port = SplitProtoPort("1234/tcp")
|
|
| 144 |
- |
|
| 145 |
- if proto != "tcp" || port != "1234" {
|
|
| 146 |
- t.Fatal("Could not split 1234/tcp properly")
|
|
| 147 |
- } |
|
| 148 |
- |
|
| 149 |
- proto, port = SplitProtoPort("")
|
|
| 150 |
- |
|
| 151 |
- if proto != "" || port != "" {
|
|
| 152 |
- t.Fatal("parsing an empty string yielded surprising results", proto, port)
|
|
| 153 |
- } |
|
| 154 |
- |
|
| 155 |
- proto, port = SplitProtoPort("1234")
|
|
| 156 |
- |
|
| 157 |
- if proto != "tcp" || port != "1234" {
|
|
| 158 |
- t.Fatal("tcp is not the default protocol for portspec '1234'", proto, port)
|
|
| 159 |
- } |
|
| 160 |
- |
|
| 161 |
- proto, port = SplitProtoPort("1234/")
|
|
| 162 |
- |
|
| 163 |
- if proto != "tcp" || port != "1234" {
|
|
| 164 |
- t.Fatal("parsing '1234/' yielded:" + port + "/" + proto)
|
|
| 165 |
- } |
|
| 166 |
- |
|
| 167 |
- proto, port = SplitProtoPort("/tcp")
|
|
| 168 |
- |
|
| 169 |
- if proto != "" || port != "" {
|
|
| 170 |
- t.Fatal("parsing '/tcp' yielded:" + port + "/" + proto)
|
|
| 171 |
- } |
|
| 172 |
-} |
|
| 173 |
- |
|
| 174 |
-func TestParsePortSpecs(t *testing.T) {
|
|
| 175 |
- var ( |
|
| 176 |
- portMap map[Port]struct{}
|
|
| 177 |
- bindingMap map[Port][]PortBinding |
|
| 178 |
- err error |
|
| 179 |
- ) |
|
| 180 |
- |
|
| 181 |
- portMap, bindingMap, err = ParsePortSpecs([]string{"1234/tcp", "2345/udp"})
|
|
| 182 |
- |
|
| 183 |
- if err != nil {
|
|
| 184 |
- t.Fatalf("Error while processing ParsePortSpecs: %s", err)
|
|
| 185 |
- } |
|
| 186 |
- |
|
| 187 |
- if _, ok := portMap[Port("1234/tcp")]; !ok {
|
|
| 188 |
- t.Fatal("1234/tcp was not parsed properly")
|
|
| 189 |
- } |
|
| 190 |
- |
|
| 191 |
- if _, ok := portMap[Port("2345/udp")]; !ok {
|
|
| 192 |
- t.Fatal("2345/udp was not parsed properly")
|
|
| 193 |
- } |
|
| 194 |
- |
|
| 195 |
- for portspec, bindings := range bindingMap {
|
|
| 196 |
- if len(bindings) != 1 {
|
|
| 197 |
- t.Fatalf("%s should have exactly one binding", portspec)
|
|
| 198 |
- } |
|
| 199 |
- |
|
| 200 |
- if bindings[0].HostIP != "" {
|
|
| 201 |
- t.Fatalf("HostIP should not be set for %s", portspec)
|
|
| 202 |
- } |
|
| 203 |
- |
|
| 204 |
- if bindings[0].HostPort != "" {
|
|
| 205 |
- t.Fatalf("HostPort should not be set for %s", portspec)
|
|
| 206 |
- } |
|
| 207 |
- } |
|
| 208 |
- |
|
| 209 |
- portMap, bindingMap, err = ParsePortSpecs([]string{"1234:1234/tcp", "2345:2345/udp"})
|
|
| 210 |
- |
|
| 211 |
- if err != nil {
|
|
| 212 |
- t.Fatalf("Error while processing ParsePortSpecs: %s", err)
|
|
| 213 |
- } |
|
| 214 |
- |
|
| 215 |
- if _, ok := portMap[Port("1234/tcp")]; !ok {
|
|
| 216 |
- t.Fatal("1234/tcp was not parsed properly")
|
|
| 217 |
- } |
|
| 218 |
- |
|
| 219 |
- if _, ok := portMap[Port("2345/udp")]; !ok {
|
|
| 220 |
- t.Fatal("2345/udp was not parsed properly")
|
|
| 221 |
- } |
|
| 222 |
- |
|
| 223 |
- for portspec, bindings := range bindingMap {
|
|
| 224 |
- _, port := SplitProtoPort(string(portspec)) |
|
| 225 |
- |
|
| 226 |
- if len(bindings) != 1 {
|
|
| 227 |
- t.Fatalf("%s should have exactly one binding", portspec)
|
|
| 228 |
- } |
|
| 229 |
- |
|
| 230 |
- if bindings[0].HostIP != "" {
|
|
| 231 |
- t.Fatalf("HostIP should not be set for %s", portspec)
|
|
| 232 |
- } |
|
| 233 |
- |
|
| 234 |
- if bindings[0].HostPort != port {
|
|
| 235 |
- t.Fatalf("HostPort should be %s for %s", port, portspec)
|
|
| 236 |
- } |
|
| 237 |
- } |
|
| 238 |
- |
|
| 239 |
- portMap, bindingMap, err = ParsePortSpecs([]string{"0.0.0.0:1234:1234/tcp", "0.0.0.0:2345:2345/udp"})
|
|
| 240 |
- |
|
| 241 |
- if err != nil {
|
|
| 242 |
- t.Fatalf("Error while processing ParsePortSpecs: %s", err)
|
|
| 243 |
- } |
|
| 244 |
- |
|
| 245 |
- if _, ok := portMap[Port("1234/tcp")]; !ok {
|
|
| 246 |
- t.Fatal("1234/tcp was not parsed properly")
|
|
| 247 |
- } |
|
| 248 |
- |
|
| 249 |
- if _, ok := portMap[Port("2345/udp")]; !ok {
|
|
| 250 |
- t.Fatal("2345/udp was not parsed properly")
|
|
| 251 |
- } |
|
| 252 |
- |
|
| 253 |
- for portspec, bindings := range bindingMap {
|
|
| 254 |
- _, port := SplitProtoPort(string(portspec)) |
|
| 255 |
- |
|
| 256 |
- if len(bindings) != 1 {
|
|
| 257 |
- t.Fatalf("%s should have exactly one binding", portspec)
|
|
| 258 |
- } |
|
| 259 |
- |
|
| 260 |
- if bindings[0].HostIP != "0.0.0.0" {
|
|
| 261 |
- t.Fatalf("HostIP is not 0.0.0.0 for %s", portspec)
|
|
| 262 |
- } |
|
| 263 |
- |
|
| 264 |
- if bindings[0].HostPort != port {
|
|
| 265 |
- t.Fatalf("HostPort should be %s for %s", port, portspec)
|
|
| 266 |
- } |
|
| 267 |
- } |
|
| 268 |
- |
|
| 269 |
- _, _, err = ParsePortSpecs([]string{"localhost:1234:1234/tcp"})
|
|
| 270 |
- |
|
| 271 |
- if err == nil {
|
|
| 272 |
- t.Fatal("Received no error while trying to parse a hostname instead of ip")
|
|
| 273 |
- } |
|
| 274 |
-} |
|
| 275 |
- |
|
| 276 |
-func TestParsePortSpecsWithRange(t *testing.T) {
|
|
| 277 |
- var ( |
|
| 278 |
- portMap map[Port]struct{}
|
|
| 279 |
- bindingMap map[Port][]PortBinding |
|
| 280 |
- err error |
|
| 281 |
- ) |
|
| 282 |
- |
|
| 283 |
- portMap, bindingMap, err = ParsePortSpecs([]string{"1234-1236/tcp", "2345-2347/udp"})
|
|
| 284 |
- |
|
| 285 |
- if err != nil {
|
|
| 286 |
- t.Fatalf("Error while processing ParsePortSpecs: %s", err)
|
|
| 287 |
- } |
|
| 288 |
- |
|
| 289 |
- if _, ok := portMap[Port("1235/tcp")]; !ok {
|
|
| 290 |
- t.Fatal("1234/tcp was not parsed properly")
|
|
| 291 |
- } |
|
| 292 |
- |
|
| 293 |
- if _, ok := portMap[Port("2346/udp")]; !ok {
|
|
| 294 |
- t.Fatal("2345/udp was not parsed properly")
|
|
| 295 |
- } |
|
| 296 |
- |
|
| 297 |
- for portspec, bindings := range bindingMap {
|
|
| 298 |
- if len(bindings) != 1 {
|
|
| 299 |
- t.Fatalf("%s should have exactly one binding", portspec)
|
|
| 300 |
- } |
|
| 301 |
- |
|
| 302 |
- if bindings[0].HostIP != "" {
|
|
| 303 |
- t.Fatalf("HostIP should not be set for %s", portspec)
|
|
| 304 |
- } |
|
| 305 |
- |
|
| 306 |
- if bindings[0].HostPort != "" {
|
|
| 307 |
- t.Fatalf("HostPort should not be set for %s", portspec)
|
|
| 308 |
- } |
|
| 309 |
- } |
|
| 310 |
- |
|
| 311 |
- portMap, bindingMap, err = ParsePortSpecs([]string{"1234-1236:1234-1236/tcp", "2345-2347:2345-2347/udp"})
|
|
| 312 |
- |
|
| 313 |
- if err != nil {
|
|
| 314 |
- t.Fatalf("Error while processing ParsePortSpecs: %s", err)
|
|
| 315 |
- } |
|
| 316 |
- |
|
| 317 |
- if _, ok := portMap[Port("1235/tcp")]; !ok {
|
|
| 318 |
- t.Fatal("1234/tcp was not parsed properly")
|
|
| 319 |
- } |
|
| 320 |
- |
|
| 321 |
- if _, ok := portMap[Port("2346/udp")]; !ok {
|
|
| 322 |
- t.Fatal("2345/udp was not parsed properly")
|
|
| 323 |
- } |
|
| 324 |
- |
|
| 325 |
- for portspec, bindings := range bindingMap {
|
|
| 326 |
- _, port := SplitProtoPort(string(portspec)) |
|
| 327 |
- if len(bindings) != 1 {
|
|
| 328 |
- t.Fatalf("%s should have exactly one binding", portspec)
|
|
| 329 |
- } |
|
| 330 |
- |
|
| 331 |
- if bindings[0].HostIP != "" {
|
|
| 332 |
- t.Fatalf("HostIP should not be set for %s", portspec)
|
|
| 333 |
- } |
|
| 334 |
- |
|
| 335 |
- if bindings[0].HostPort != port {
|
|
| 336 |
- t.Fatalf("HostPort should be %s for %s", port, portspec)
|
|
| 337 |
- } |
|
| 338 |
- } |
|
| 339 |
- |
|
| 340 |
- portMap, bindingMap, err = ParsePortSpecs([]string{"0.0.0.0:1234-1236:1234-1236/tcp", "0.0.0.0:2345-2347:2345-2347/udp"})
|
|
| 341 |
- |
|
| 342 |
- if err != nil {
|
|
| 343 |
- t.Fatalf("Error while processing ParsePortSpecs: %s", err)
|
|
| 344 |
- } |
|
| 345 |
- |
|
| 346 |
- if _, ok := portMap[Port("1235/tcp")]; !ok {
|
|
| 347 |
- t.Fatal("1234/tcp was not parsed properly")
|
|
| 348 |
- } |
|
| 349 |
- |
|
| 350 |
- if _, ok := portMap[Port("2346/udp")]; !ok {
|
|
| 351 |
- t.Fatal("2345/udp was not parsed properly")
|
|
| 352 |
- } |
|
| 353 |
- |
|
| 354 |
- for portspec, bindings := range bindingMap {
|
|
| 355 |
- _, port := SplitProtoPort(string(portspec)) |
|
| 356 |
- if len(bindings) != 1 || bindings[0].HostIP != "0.0.0.0" || bindings[0].HostPort != port {
|
|
| 357 |
- t.Fatalf("Expect single binding to port %s but found %s", port, bindings)
|
|
| 358 |
- } |
|
| 359 |
- } |
|
| 360 |
- |
|
| 361 |
- _, _, err = ParsePortSpecs([]string{"localhost:1234-1236:1234-1236/tcp"})
|
|
| 362 |
- |
|
| 363 |
- if err == nil {
|
|
| 364 |
- t.Fatal("Received no error while trying to parse a hostname instead of ip")
|
|
| 365 |
- } |
|
| 366 |
-} |
|
| 367 |
- |
|
| 368 |
-func TestParseNetworkOptsPrivateOnly(t *testing.T) {
|
|
| 369 |
- ports, bindings, err := ParsePortSpecs([]string{"192.168.1.100::80"})
|
|
| 370 |
- if err != nil {
|
|
| 371 |
- t.Fatal(err) |
|
| 372 |
- } |
|
| 373 |
- if len(ports) != 1 {
|
|
| 374 |
- t.Logf("Expected 1 got %d", len(ports))
|
|
| 375 |
- t.FailNow() |
|
| 376 |
- } |
|
| 377 |
- if len(bindings) != 1 {
|
|
| 378 |
- t.Logf("Expected 1 got %d", len(bindings))
|
|
| 379 |
- t.FailNow() |
|
| 380 |
- } |
|
| 381 |
- for k := range ports {
|
|
| 382 |
- if k.Proto() != "tcp" {
|
|
| 383 |
- t.Logf("Expected tcp got %s", k.Proto())
|
|
| 384 |
- t.Fail() |
|
| 385 |
- } |
|
| 386 |
- if k.Port() != "80" {
|
|
| 387 |
- t.Logf("Expected 80 got %s", k.Port())
|
|
| 388 |
- t.Fail() |
|
| 389 |
- } |
|
| 390 |
- b, exists := bindings[k] |
|
| 391 |
- if !exists {
|
|
| 392 |
- t.Log("Binding does not exist")
|
|
| 393 |
- t.FailNow() |
|
| 394 |
- } |
|
| 395 |
- if len(b) != 1 {
|
|
| 396 |
- t.Logf("Expected 1 got %d", len(b))
|
|
| 397 |
- t.FailNow() |
|
| 398 |
- } |
|
| 399 |
- s := b[0] |
|
| 400 |
- if s.HostPort != "" {
|
|
| 401 |
- t.Logf("Expected \"\" got %s", s.HostPort)
|
|
| 402 |
- t.Fail() |
|
| 403 |
- } |
|
| 404 |
- if s.HostIP != "192.168.1.100" {
|
|
| 405 |
- t.Fail() |
|
| 406 |
- } |
|
| 407 |
- } |
|
| 408 |
-} |
|
| 409 |
- |
|
| 410 |
-func TestParseNetworkOptsPublic(t *testing.T) {
|
|
| 411 |
- ports, bindings, err := ParsePortSpecs([]string{"192.168.1.100:8080:80"})
|
|
| 412 |
- if err != nil {
|
|
| 413 |
- t.Fatal(err) |
|
| 414 |
- } |
|
| 415 |
- if len(ports) != 1 {
|
|
| 416 |
- t.Logf("Expected 1 got %d", len(ports))
|
|
| 417 |
- t.FailNow() |
|
| 418 |
- } |
|
| 419 |
- if len(bindings) != 1 {
|
|
| 420 |
- t.Logf("Expected 1 got %d", len(bindings))
|
|
| 421 |
- t.FailNow() |
|
| 422 |
- } |
|
| 423 |
- for k := range ports {
|
|
| 424 |
- if k.Proto() != "tcp" {
|
|
| 425 |
- t.Logf("Expected tcp got %s", k.Proto())
|
|
| 426 |
- t.Fail() |
|
| 427 |
- } |
|
| 428 |
- if k.Port() != "80" {
|
|
| 429 |
- t.Logf("Expected 80 got %s", k.Port())
|
|
| 430 |
- t.Fail() |
|
| 431 |
- } |
|
| 432 |
- b, exists := bindings[k] |
|
| 433 |
- if !exists {
|
|
| 434 |
- t.Log("Binding does not exist")
|
|
| 435 |
- t.FailNow() |
|
| 436 |
- } |
|
| 437 |
- if len(b) != 1 {
|
|
| 438 |
- t.Logf("Expected 1 got %d", len(b))
|
|
| 439 |
- t.FailNow() |
|
| 440 |
- } |
|
| 441 |
- s := b[0] |
|
| 442 |
- if s.HostPort != "8080" {
|
|
| 443 |
- t.Logf("Expected 8080 got %s", s.HostPort)
|
|
| 444 |
- t.Fail() |
|
| 445 |
- } |
|
| 446 |
- if s.HostIP != "192.168.1.100" {
|
|
| 447 |
- t.Fail() |
|
| 448 |
- } |
|
| 449 |
- } |
|
| 450 |
-} |
|
| 451 |
- |
|
| 452 |
-func TestParseNetworkOptsPublicNoPort(t *testing.T) {
|
|
| 453 |
- ports, bindings, err := ParsePortSpecs([]string{"192.168.1.100"})
|
|
| 454 |
- |
|
| 455 |
- if err == nil {
|
|
| 456 |
- t.Logf("Expected error Invalid containerPort")
|
|
| 457 |
- t.Fail() |
|
| 458 |
- } |
|
| 459 |
- if ports != nil {
|
|
| 460 |
- t.Logf("Expected nil got %s", ports)
|
|
| 461 |
- t.Fail() |
|
| 462 |
- } |
|
| 463 |
- if bindings != nil {
|
|
| 464 |
- t.Logf("Expected nil got %s", bindings)
|
|
| 465 |
- t.Fail() |
|
| 466 |
- } |
|
| 467 |
-} |
|
| 468 |
- |
|
| 469 |
-func TestParseNetworkOptsNegativePorts(t *testing.T) {
|
|
| 470 |
- ports, bindings, err := ParsePortSpecs([]string{"192.168.1.100:-1:-1"})
|
|
| 471 |
- |
|
| 472 |
- if err == nil {
|
|
| 473 |
- t.Fail() |
|
| 474 |
- } |
|
| 475 |
- if len(ports) != 0 {
|
|
| 476 |
- t.Logf("Expected nil got %d", len(ports))
|
|
| 477 |
- t.Fail() |
|
| 478 |
- } |
|
| 479 |
- if len(bindings) != 0 {
|
|
| 480 |
- t.Logf("Expected 0 got %d", len(bindings))
|
|
| 481 |
- t.Fail() |
|
| 482 |
- } |
|
| 483 |
-} |
|
| 484 |
- |
|
| 485 |
-func TestParseNetworkOptsUdp(t *testing.T) {
|
|
| 486 |
- ports, bindings, err := ParsePortSpecs([]string{"192.168.1.100::6000/udp"})
|
|
| 487 |
- if err != nil {
|
|
| 488 |
- t.Fatal(err) |
|
| 489 |
- } |
|
| 490 |
- if len(ports) != 1 {
|
|
| 491 |
- t.Logf("Expected 1 got %d", len(ports))
|
|
| 492 |
- t.FailNow() |
|
| 493 |
- } |
|
| 494 |
- if len(bindings) != 1 {
|
|
| 495 |
- t.Logf("Expected 1 got %d", len(bindings))
|
|
| 496 |
- t.FailNow() |
|
| 497 |
- } |
|
| 498 |
- for k := range ports {
|
|
| 499 |
- if k.Proto() != "udp" {
|
|
| 500 |
- t.Logf("Expected udp got %s", k.Proto())
|
|
| 501 |
- t.Fail() |
|
| 502 |
- } |
|
| 503 |
- if k.Port() != "6000" {
|
|
| 504 |
- t.Logf("Expected 6000 got %s", k.Port())
|
|
| 505 |
- t.Fail() |
|
| 506 |
- } |
|
| 507 |
- b, exists := bindings[k] |
|
| 508 |
- if !exists {
|
|
| 509 |
- t.Log("Binding does not exist")
|
|
| 510 |
- t.FailNow() |
|
| 511 |
- } |
|
| 512 |
- if len(b) != 1 {
|
|
| 513 |
- t.Logf("Expected 1 got %d", len(b))
|
|
| 514 |
- t.FailNow() |
|
| 515 |
- } |
|
| 516 |
- s := b[0] |
|
| 517 |
- if s.HostPort != "" {
|
|
| 518 |
- t.Logf("Expected \"\" got %s", s.HostPort)
|
|
| 519 |
- t.Fail() |
|
| 520 |
- } |
|
| 521 |
- if s.HostIP != "192.168.1.100" {
|
|
| 522 |
- t.Fail() |
|
| 523 |
- } |
|
| 524 |
- } |
|
| 525 |
-} |
| 526 | 1 |
deleted file mode 100644 |
| ... | ... |
@@ -1,98 +0,0 @@ |
| 1 |
-package nat |
|
| 2 |
- |
|
| 3 |
-import ( |
|
| 4 |
- "sort" |
|
| 5 |
- "strings" |
|
| 6 |
- |
|
| 7 |
- "github.com/docker/docker/pkg/parsers" |
|
| 8 |
-) |
|
| 9 |
- |
|
| 10 |
-type portSorter struct {
|
|
| 11 |
- ports []Port |
|
| 12 |
- by func(i, j Port) bool |
|
| 13 |
-} |
|
| 14 |
- |
|
| 15 |
-func (s *portSorter) Len() int {
|
|
| 16 |
- return len(s.ports) |
|
| 17 |
-} |
|
| 18 |
- |
|
| 19 |
-func (s *portSorter) Swap(i, j int) {
|
|
| 20 |
- s.ports[i], s.ports[j] = s.ports[j], s.ports[i] |
|
| 21 |
-} |
|
| 22 |
- |
|
| 23 |
-func (s *portSorter) Less(i, j int) bool {
|
|
| 24 |
- ip := s.ports[i] |
|
| 25 |
- jp := s.ports[j] |
|
| 26 |
- |
|
| 27 |
- return s.by(ip, jp) |
|
| 28 |
-} |
|
| 29 |
- |
|
| 30 |
-// Sort sorts a list of ports using the provided predicate |
|
| 31 |
-// This function should compare `i` and `j`, returning true if `i` is |
|
| 32 |
-// considered to be less than `j` |
|
| 33 |
-func Sort(ports []Port, predicate func(i, j Port) bool) {
|
|
| 34 |
- s := &portSorter{ports, predicate}
|
|
| 35 |
- sort.Sort(s) |
|
| 36 |
-} |
|
| 37 |
- |
|
| 38 |
-type portMapEntry struct {
|
|
| 39 |
- port Port |
|
| 40 |
- binding PortBinding |
|
| 41 |
-} |
|
| 42 |
- |
|
| 43 |
-type portMapSorter []portMapEntry |
|
| 44 |
- |
|
| 45 |
-func (s portMapSorter) Len() int { return len(s) }
|
|
| 46 |
-func (s portMapSorter) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
| 47 |
- |
|
| 48 |
-// sort the port so that the order is: |
|
| 49 |
-// 1. port with larger specified bindings |
|
| 50 |
-// 2. larger port |
|
| 51 |
-// 3. port with tcp protocol |
|
| 52 |
-func (s portMapSorter) Less(i, j int) bool {
|
|
| 53 |
- pi, pj := s[i].port, s[j].port |
|
| 54 |
- hpi, hpj := toInt(s[i].binding.HostPort), toInt(s[j].binding.HostPort) |
|
| 55 |
- return hpi > hpj || pi.Int() > pj.Int() || (pi.Int() == pj.Int() && strings.ToLower(pi.Proto()) == "tcp") |
|
| 56 |
-} |
|
| 57 |
- |
|
| 58 |
-// SortPortMap sorts the list of ports and their respected mapping. The ports |
|
| 59 |
-// will explicit HostPort will be placed first. |
|
| 60 |
-func SortPortMap(ports []Port, bindings PortMap) {
|
|
| 61 |
- s := portMapSorter{}
|
|
| 62 |
- for _, p := range ports {
|
|
| 63 |
- if binding, ok := bindings[p]; ok {
|
|
| 64 |
- for _, b := range binding {
|
|
| 65 |
- s = append(s, portMapEntry{port: p, binding: b})
|
|
| 66 |
- } |
|
| 67 |
- bindings[p] = []PortBinding{}
|
|
| 68 |
- } else {
|
|
| 69 |
- s = append(s, portMapEntry{port: p})
|
|
| 70 |
- } |
|
| 71 |
- } |
|
| 72 |
- |
|
| 73 |
- sort.Sort(s) |
|
| 74 |
- var ( |
|
| 75 |
- i int |
|
| 76 |
- pm = make(map[Port]struct{})
|
|
| 77 |
- ) |
|
| 78 |
- // reorder ports |
|
| 79 |
- for _, entry := range s {
|
|
| 80 |
- if _, ok := pm[entry.port]; !ok {
|
|
| 81 |
- ports[i] = entry.port |
|
| 82 |
- pm[entry.port] = struct{}{}
|
|
| 83 |
- i++ |
|
| 84 |
- } |
|
| 85 |
- // reorder bindings for this port |
|
| 86 |
- if _, ok := bindings[entry.port]; ok {
|
|
| 87 |
- bindings[entry.port] = append(bindings[entry.port], entry.binding) |
|
| 88 |
- } |
|
| 89 |
- } |
|
| 90 |
-} |
|
| 91 |
- |
|
| 92 |
-func toInt(s string) uint64 {
|
|
| 93 |
- i, _, err := parsers.ParsePortRange(s) |
|
| 94 |
- if err != nil {
|
|
| 95 |
- i = 0 |
|
| 96 |
- } |
|
| 97 |
- return i |
|
| 98 |
-} |
| 99 | 1 |
deleted file mode 100644 |
| ... | ... |
@@ -1,85 +0,0 @@ |
| 1 |
-package nat |
|
| 2 |
- |
|
| 3 |
-import ( |
|
| 4 |
- "fmt" |
|
| 5 |
- "reflect" |
|
| 6 |
- "testing" |
|
| 7 |
-) |
|
| 8 |
- |
|
| 9 |
-func TestSortUniquePorts(t *testing.T) {
|
|
| 10 |
- ports := []Port{
|
|
| 11 |
- Port("6379/tcp"),
|
|
| 12 |
- Port("22/tcp"),
|
|
| 13 |
- } |
|
| 14 |
- |
|
| 15 |
- Sort(ports, func(ip, jp Port) bool {
|
|
| 16 |
- return ip.Int() < jp.Int() || (ip.Int() == jp.Int() && ip.Proto() == "tcp") |
|
| 17 |
- }) |
|
| 18 |
- |
|
| 19 |
- first := ports[0] |
|
| 20 |
- if fmt.Sprint(first) != "22/tcp" {
|
|
| 21 |
- t.Log(fmt.Sprint(first)) |
|
| 22 |
- t.Fail() |
|
| 23 |
- } |
|
| 24 |
-} |
|
| 25 |
- |
|
| 26 |
-func TestSortSamePortWithDifferentProto(t *testing.T) {
|
|
| 27 |
- ports := []Port{
|
|
| 28 |
- Port("8888/tcp"),
|
|
| 29 |
- Port("8888/udp"),
|
|
| 30 |
- Port("6379/tcp"),
|
|
| 31 |
- Port("6379/udp"),
|
|
| 32 |
- } |
|
| 33 |
- |
|
| 34 |
- Sort(ports, func(ip, jp Port) bool {
|
|
| 35 |
- return ip.Int() < jp.Int() || (ip.Int() == jp.Int() && ip.Proto() == "tcp") |
|
| 36 |
- }) |
|
| 37 |
- |
|
| 38 |
- first := ports[0] |
|
| 39 |
- if fmt.Sprint(first) != "6379/tcp" {
|
|
| 40 |
- t.Fail() |
|
| 41 |
- } |
|
| 42 |
-} |
|
| 43 |
- |
|
| 44 |
-func TestSortPortMap(t *testing.T) {
|
|
| 45 |
- ports := []Port{
|
|
| 46 |
- Port("22/tcp"),
|
|
| 47 |
- Port("22/udp"),
|
|
| 48 |
- Port("8000/tcp"),
|
|
| 49 |
- Port("6379/tcp"),
|
|
| 50 |
- Port("9999/tcp"),
|
|
| 51 |
- } |
|
| 52 |
- |
|
| 53 |
- portMap := PortMap{
|
|
| 54 |
- Port("22/tcp"): []PortBinding{
|
|
| 55 |
- {},
|
|
| 56 |
- }, |
|
| 57 |
- Port("8000/tcp"): []PortBinding{
|
|
| 58 |
- {},
|
|
| 59 |
- }, |
|
| 60 |
- Port("6379/tcp"): []PortBinding{
|
|
| 61 |
- {},
|
|
| 62 |
- {HostIP: "0.0.0.0", HostPort: "32749"},
|
|
| 63 |
- }, |
|
| 64 |
- Port("9999/tcp"): []PortBinding{
|
|
| 65 |
- {HostIP: "0.0.0.0", HostPort: "40000"},
|
|
| 66 |
- }, |
|
| 67 |
- } |
|
| 68 |
- |
|
| 69 |
- SortPortMap(ports, portMap) |
|
| 70 |
- if !reflect.DeepEqual(ports, []Port{
|
|
| 71 |
- Port("9999/tcp"),
|
|
| 72 |
- Port("6379/tcp"),
|
|
| 73 |
- Port("8000/tcp"),
|
|
| 74 |
- Port("22/tcp"),
|
|
| 75 |
- Port("22/udp"),
|
|
| 76 |
- }) {
|
|
| 77 |
- t.Errorf("failed to prioritize port with explicit mappings, got %v", ports)
|
|
| 78 |
- } |
|
| 79 |
- if pm := portMap[Port("6379/tcp")]; !reflect.DeepEqual(pm, []PortBinding{
|
|
| 80 |
- {HostIP: "0.0.0.0", HostPort: "32749"},
|
|
| 81 |
- {},
|
|
| 82 |
- }) {
|
|
| 83 |
- t.Errorf("failed to prioritize bindings with explicit mappings, got %v", pm)
|
|
| 84 |
- } |
|
| 85 |
-} |
| ... | ... |
@@ -7,8 +7,8 @@ import ( |
| 7 | 7 |
|
| 8 | 8 |
"github.com/docker/docker/api/types/blkiodev" |
| 9 | 9 |
"github.com/docker/docker/api/types/strslice" |
| 10 |
- "github.com/docker/docker/pkg/nat" |
|
| 11 | 10 |
"github.com/docker/docker/pkg/ulimit" |
| 11 |
+ "github.com/docker/go-connections/nat" |
|
| 12 | 12 |
) |
| 13 | 13 |
|
| 14 | 14 |
// KeyValuePair is a structure that hold a value for a key. |
| ... | ... |
@@ -10,10 +10,10 @@ import ( |
| 10 | 10 |
"github.com/docker/docker/opts" |
| 11 | 11 |
flag "github.com/docker/docker/pkg/mflag" |
| 12 | 12 |
"github.com/docker/docker/pkg/mount" |
| 13 |
- "github.com/docker/docker/pkg/nat" |
|
| 14 | 13 |
"github.com/docker/docker/pkg/parsers" |
| 15 | 14 |
"github.com/docker/docker/pkg/signal" |
| 16 | 15 |
"github.com/docker/docker/volume" |
| 16 |
+ "github.com/docker/go-connections/nat" |
|
| 17 | 17 |
"github.com/docker/go-units" |
| 18 | 18 |
) |
| 19 | 19 |
|