Upgrade vendored github.com/docker/go-connections to latest version, which includes new nat parse functions.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2015-12-22 18:59:47 -05:00
parent 1105caa7f1
commit fcc24995e0
6 changed files with 123 additions and 17 deletions

View File

@ -21,7 +21,7 @@ clone git github.com/tchap/go-patricia v2.1.0
clone git github.com/vdemeester/shakers 3c10293ce22b900c27acad7b28656196fcc2f73b clone git github.com/vdemeester/shakers 3c10293ce22b900c27acad7b28656196fcc2f73b
clone git golang.org/x/net 47990a1ba55743e6ef1affd3a14e5bac8553615d https://github.com/golang/net.git clone git golang.org/x/net 47990a1ba55743e6ef1affd3a14e5bac8553615d https://github.com/golang/net.git
clone git github.com/docker/go-units v0.2.0 clone git github.com/docker/go-units v0.2.0
clone git github.com/docker/go-connections 96cdf8190a45946d0f19576732e2a410c6f84a31 clone git github.com/docker/go-connections 4e42727957c146776e5de9cec8c39e4059ed9f20
#get libnetwork packages #get libnetwork packages
clone git github.com/docker/libnetwork bbd6e6d8ca1e7c9b42f6f53277b0bde72847ff90 clone git github.com/docker/libnetwork bbd6e6d8ca1e7c9b42f6f53277b0bde72847ff90

View File

@ -6,8 +6,6 @@ import (
"net" "net"
"strconv" "strconv"
"strings" "strings"
"github.com/docker/docker/pkg/parsers"
) )
const ( const (
@ -37,7 +35,7 @@ func NewPort(proto, port string) (Port, error) {
// Check for parsing issues on "port" now so we can avoid having // Check for parsing issues on "port" now so we can avoid having
// to check it later on. // to check it later on.
portStartInt, portEndInt, err := ParsePortRange(port) portStartInt, portEndInt, err := ParsePortRangeToInt(port)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -60,12 +58,12 @@ func ParsePort(rawPort string) (int, error) {
return int(port), nil return int(port), nil
} }
// ParsePortRange parses the port range string and returns start/end ints // ParsePortRangeToInt parses the port range string and returns start/end ints
func ParsePortRange(rawPort string) (int, int, error) { func ParsePortRangeToInt(rawPort string) (int, int, error) {
if len(rawPort) == 0 { if len(rawPort) == 0 {
return 0, 0, nil return 0, 0, nil
} }
start, end, err := parsers.ParsePortRange(rawPort) start, end, err := ParsePortRange(rawPort)
if err != nil { if err != nil {
return 0, 0, err return 0, 0, err
} }
@ -99,7 +97,7 @@ func (p Port) Int() int {
// Range returns the start/end port numbers of a Port range as ints // Range returns the start/end port numbers of a Port range as ints
func (p Port) Range() (int, int, error) { func (p Port) Range() (int, int, error) {
return ParsePortRange(p.Port()) return ParsePortRangeToInt(p.Port())
} }
// SplitProtoPort splits a port in the format of proto/port // SplitProtoPort splits a port in the format of proto/port
@ -148,7 +146,7 @@ func ParsePortSpecs(ports []string) (map[Port]struct{}, map[Port][]PortBinding,
rawPort = fmt.Sprintf(":%s", rawPort) rawPort = fmt.Sprintf(":%s", rawPort)
} }
parts, err := parsers.PartParser(portSpecTemplate, rawPort) parts, err := PartParser(portSpecTemplate, rawPort)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@ -166,14 +164,14 @@ func ParsePortSpecs(ports []string) (map[Port]struct{}, map[Port][]PortBinding,
return nil, nil, fmt.Errorf("No port specified: %s<empty>", rawPort) return nil, nil, fmt.Errorf("No port specified: %s<empty>", rawPort)
} }
startPort, endPort, err := parsers.ParsePortRange(containerPort) startPort, endPort, err := ParsePortRange(containerPort)
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("Invalid containerPort: %s", containerPort) return nil, nil, fmt.Errorf("Invalid containerPort: %s", containerPort)
} }
var startHostPort, endHostPort uint64 = 0, 0 var startHostPort, endHostPort uint64 = 0, 0
if len(hostPort) > 0 { if len(hostPort) > 0 {
startHostPort, endHostPort, err = parsers.ParsePortRange(hostPort) startHostPort, endHostPort, err = ParsePortRange(hostPort)
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("Invalid hostPort: %s", hostPort) return nil, nil, fmt.Errorf("Invalid hostPort: %s", hostPort)
} }

View File

@ -41,7 +41,7 @@ func TestParsePort(t *testing.T) {
} }
} }
func TestParsePortRange(t *testing.T) { func TestParsePortRangeToInt(t *testing.T) {
var ( var (
begin int begin int
end int end int
@ -63,7 +63,7 @@ func TestParsePortRange(t *testing.T) {
} }
for _, r := range validRanges { for _, r := range validRanges {
begin, end, err = ParsePortRange(r.Range) begin, end, err = ParsePortRangeToInt(r.Range)
if err != nil || begin != r.Begin { if err != nil || begin != r.Begin {
t.Fatalf("Parsing port range '%s' did not succeed. Expected begin %d, got %d", r.Range, r.Begin, begin) t.Fatalf("Parsing port range '%s' did not succeed. Expected begin %d, got %d", r.Range, r.Begin, begin)
@ -83,7 +83,7 @@ func TestParsePortRange(t *testing.T) {
} }
for _, r := range invalidRanges { for _, r := range invalidRanges {
begin, end, err = ParsePortRange(r) begin, end, err = ParsePortRangeToInt(r)
if err == nil || begin != 0 || end != 0 { if err == nil || begin != 0 || end != 0 {
t.Fatalf("Parsing port range '%s' succeeded", r) t.Fatalf("Parsing port range '%s' succeeded", r)

View File

@ -0,0 +1,56 @@
package nat
import (
"fmt"
"strconv"
"strings"
)
// PartParser parses and validates the specified string (data) using the specified template
// e.g. ip:public:private -> 192.168.0.1:80:8000
func PartParser(template, data string) (map[string]string, error) {
// ip:public:private
var (
templateParts = strings.Split(template, ":")
parts = strings.Split(data, ":")
out = make(map[string]string, len(templateParts))
)
if len(parts) != len(templateParts) {
return nil, fmt.Errorf("Invalid format to parse. %s should match template %s", data, template)
}
for i, t := range templateParts {
value := ""
if len(parts) > i {
value = parts[i]
}
out[t] = value
}
return out, nil
}
// ParsePortRange parses and validates the specified string as a port-range (8000-9000)
func ParsePortRange(ports string) (uint64, uint64, error) {
if ports == "" {
return 0, 0, fmt.Errorf("Empty string specified for ports.")
}
if !strings.Contains(ports, "-") {
start, err := strconv.ParseUint(ports, 10, 16)
end := start
return start, end, err
}
parts := strings.Split(ports, "-")
start, err := strconv.ParseUint(parts[0], 10, 16)
if err != nil {
return 0, 0, err
}
end, err := strconv.ParseUint(parts[1], 10, 16)
if err != nil {
return 0, 0, err
}
if end < start {
return 0, 0, fmt.Errorf("Invalid range specified for the Port: %s", ports)
}
return start, end, nil
}

View File

@ -0,0 +1,54 @@
package nat
import (
"strings"
"testing"
)
func TestParsePortRange(t *testing.T) {
if start, end, err := ParsePortRange("8000-8080"); err != nil || start != 8000 || end != 8080 {
t.Fatalf("Error: %s or Expecting {start,end} values {8000,8080} but found {%d,%d}.", err, start, end)
}
}
func TestParsePortRangeEmpty(t *testing.T) {
if _, _, err := ParsePortRange(""); err == nil || err.Error() != "Empty string specified for ports." {
t.Fatalf("Expected error 'Empty string specified for ports.', got %v", err)
}
}
func TestParsePortRangeWithNoRange(t *testing.T) {
start, end, err := ParsePortRange("8080")
if err != nil {
t.Fatal(err)
}
if start != 8080 || end != 8080 {
t.Fatalf("Expected start and end to be the same and equal to 8080, but were %v and %v", start, end)
}
}
func TestParsePortRangeIncorrectRange(t *testing.T) {
if _, _, err := ParsePortRange("9000-8080"); err == nil || !strings.Contains(err.Error(), "Invalid range specified for the Port") {
t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err)
}
}
func TestParsePortRangeIncorrectEndRange(t *testing.T) {
if _, _, err := ParsePortRange("8000-a"); err == nil || !strings.Contains(err.Error(), "invalid syntax") {
t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err)
}
if _, _, err := ParsePortRange("8000-30a"); err == nil || !strings.Contains(err.Error(), "invalid syntax") {
t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err)
}
}
func TestParsePortRangeIncorrectStartRange(t *testing.T) {
if _, _, err := ParsePortRange("a-8000"); err == nil || !strings.Contains(err.Error(), "invalid syntax") {
t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err)
}
if _, _, err := ParsePortRange("30a-8000"); err == nil || !strings.Contains(err.Error(), "invalid syntax") {
t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err)
}
}

View File

@ -3,8 +3,6 @@ package nat
import ( import (
"sort" "sort"
"strings" "strings"
"github.com/docker/docker/pkg/parsers"
) )
type portSorter struct { type portSorter struct {
@ -90,7 +88,7 @@ func SortPortMap(ports []Port, bindings PortMap) {
} }
func toInt(s string) uint64 { func toInt(s string) uint64 {
i, _, err := parsers.ParsePortRange(s) i, _, err := ParsePortRange(s)
if err != nil { if err != nil {
i = 0 i = 0
} }