Merge pull request #14804 from dave-tucker/golint_nat

golint: Fix issues in pkg/nat
This commit is contained in:
Jessie Frazelle 2015-07-21 20:38:40 -07:00
commit 64ddf3420c
4 changed files with 42 additions and 29 deletions

View file

@ -13,22 +13,28 @@ import (
) )
const ( const (
PortSpecTemplate = "ip:hostPort:containerPort" // portSpecTemplate is the expected format for port specifications
PortSpecTemplateFormat = "ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort" portSpecTemplate = "ip:hostPort:containerPort"
) )
// PortBinding represents a binding between a Host IP address and a Host Port
type PortBinding struct { type PortBinding struct {
HostIp string // HostIP is the host IP Address
HostIP string `json:"HostIp"`
// HostPort is the host port number
HostPort string HostPort string
} }
// PortMap is a collection of PortBinding indexed by Port
type PortMap map[Port][]PortBinding type PortMap map[Port][]PortBinding
// PortSet is a collection of structs indexed by Port
type PortSet map[Port]struct{} type PortSet map[Port]struct{}
// 80/tcp // Port is a string containing port number and protocol in the format "80/tcp"
type Port string type Port string
// NewPort creates a new instance of a Port given a protocol and port number
func NewPort(proto, port string) (Port, error) { 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.
@ -41,6 +47,7 @@ func NewPort(proto, port string) (Port, error) {
return Port(fmt.Sprintf("%d/%s", portInt, proto)), nil return Port(fmt.Sprintf("%d/%s", portInt, proto)), nil
} }
// ParsePort parses the port number string and returns an int
func ParsePort(rawPort string) (int, error) { func ParsePort(rawPort string) (int, error) {
if len(rawPort) == 0 { if len(rawPort) == 0 {
return 0, nil return 0, nil
@ -52,16 +59,19 @@ func ParsePort(rawPort string) (int, error) {
return int(port), nil return int(port), nil
} }
// Proto returns the protocol of a Port
func (p Port) Proto() string { func (p Port) Proto() string {
proto, _ := SplitProtoPort(string(p)) proto, _ := SplitProtoPort(string(p))
return proto return proto
} }
// Port returns the port number of a Port
func (p Port) Port() string { func (p Port) Port() string {
_, port := SplitProtoPort(string(p)) _, port := SplitProtoPort(string(p))
return port return port
} }
// Int returns the port number of a Port as an int
func (p Port) Int() int { func (p Port) Int() int {
portStr := p.Port() portStr := p.Port()
if len(portStr) == 0 { if len(portStr) == 0 {
@ -74,7 +84,7 @@ func (p Port) Int() int {
return int(port) return int(port)
} }
// Splits a port in the format of proto/port // SplitProtoPort splits a port in the format of proto/port
func SplitProtoPort(rawPort string) (string, string) { func SplitProtoPort(rawPort string) (string, string) {
parts := strings.Split(rawPort, "/") parts := strings.Split(rawPort, "/")
l := len(parts) l := len(parts)
@ -99,8 +109,8 @@ func validateProto(proto string) bool {
return false return false
} }
// We will receive port specs in the format of ip:public:private/proto and these need to be // ParsePortSpecs receives port specs in the format of ip:public:private/proto and parses
// parsed in the internal types // these in to the internal types
func ParsePortSpecs(ports []string) (map[Port]struct{}, map[Port][]PortBinding, error) { func ParsePortSpecs(ports []string) (map[Port]struct{}, map[Port][]PortBinding, error) {
var ( var (
exposedPorts = make(map[Port]struct{}, len(ports)) exposedPorts = make(map[Port]struct{}, len(ports))
@ -120,19 +130,19 @@ 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 := parsers.PartParser(portSpecTemplate, rawPort)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
var ( var (
containerPort = parts["containerPort"] containerPort = parts["containerPort"]
rawIp = parts["ip"] rawIP = parts["ip"]
hostPort = parts["hostPort"] hostPort = parts["hostPort"]
) )
if rawIp != "" && net.ParseIP(rawIp) == nil { if rawIP != "" && net.ParseIP(rawIP) == nil {
return nil, nil, fmt.Errorf("Invalid ip address: %s", rawIp) return nil, nil, fmt.Errorf("Invalid ip address: %s", rawIP)
} }
if containerPort == "" { if containerPort == "" {
return nil, nil, fmt.Errorf("No port specified: %s<empty>", rawPort) return nil, nil, fmt.Errorf("No port specified: %s<empty>", rawPort)
@ -173,7 +183,7 @@ func ParsePortSpecs(ports []string) (map[Port]struct{}, map[Port][]PortBinding,
} }
binding := PortBinding{ binding := PortBinding{
HostIp: rawIp, HostIP: rawIP,
HostPort: hostPort, HostPort: hostPort,
} }
bslice, exists := bindings[port] bslice, exists := bindings[port]

View file

@ -133,8 +133,8 @@ func TestParsePortSpecs(t *testing.T) {
t.Fatalf("%s should have exactly one binding", portspec) t.Fatalf("%s should have exactly one binding", portspec)
} }
if bindings[0].HostIp != "" { if bindings[0].HostIP != "" {
t.Fatalf("HostIp should not be set for %s", portspec) t.Fatalf("HostIP should not be set for %s", portspec)
} }
if bindings[0].HostPort != "" { if bindings[0].HostPort != "" {
@ -163,8 +163,8 @@ func TestParsePortSpecs(t *testing.T) {
t.Fatalf("%s should have exactly one binding", portspec) t.Fatalf("%s should have exactly one binding", portspec)
} }
if bindings[0].HostIp != "" { if bindings[0].HostIP != "" {
t.Fatalf("HostIp should not be set for %s", portspec) t.Fatalf("HostIP should not be set for %s", portspec)
} }
if bindings[0].HostPort != port { if bindings[0].HostPort != port {
@ -193,8 +193,8 @@ func TestParsePortSpecs(t *testing.T) {
t.Fatalf("%s should have exactly one binding", portspec) t.Fatalf("%s should have exactly one binding", portspec)
} }
if bindings[0].HostIp != "0.0.0.0" { if bindings[0].HostIP != "0.0.0.0" {
t.Fatalf("HostIp is not 0.0.0.0 for %s", portspec) t.Fatalf("HostIP is not 0.0.0.0 for %s", portspec)
} }
if bindings[0].HostPort != port { if bindings[0].HostPort != port {
@ -235,8 +235,8 @@ func TestParsePortSpecsWithRange(t *testing.T) {
t.Fatalf("%s should have exactly one binding", portspec) t.Fatalf("%s should have exactly one binding", portspec)
} }
if bindings[0].HostIp != "" { if bindings[0].HostIP != "" {
t.Fatalf("HostIp should not be set for %s", portspec) t.Fatalf("HostIP should not be set for %s", portspec)
} }
if bindings[0].HostPort != "" { if bindings[0].HostPort != "" {
@ -264,8 +264,8 @@ func TestParsePortSpecsWithRange(t *testing.T) {
t.Fatalf("%s should have exactly one binding", portspec) t.Fatalf("%s should have exactly one binding", portspec)
} }
if bindings[0].HostIp != "" { if bindings[0].HostIP != "" {
t.Fatalf("HostIp should not be set for %s", portspec) t.Fatalf("HostIP should not be set for %s", portspec)
} }
if bindings[0].HostPort != port { if bindings[0].HostPort != port {
@ -289,7 +289,7 @@ func TestParsePortSpecsWithRange(t *testing.T) {
for portspec, bindings := range bindingMap { for portspec, bindings := range bindingMap {
_, port := SplitProtoPort(string(portspec)) _, port := SplitProtoPort(string(portspec))
if len(bindings) != 1 || bindings[0].HostIp != "0.0.0.0" || bindings[0].HostPort != port { if len(bindings) != 1 || bindings[0].HostIP != "0.0.0.0" || bindings[0].HostPort != port {
t.Fatalf("Expect single binding to port %s but found %s", port, bindings) t.Fatalf("Expect single binding to port %s but found %s", port, bindings)
} }
} }
@ -337,7 +337,7 @@ func TestParseNetworkOptsPrivateOnly(t *testing.T) {
t.Logf("Expected \"\" got %s", s.HostPort) t.Logf("Expected \"\" got %s", s.HostPort)
t.Fail() t.Fail()
} }
if s.HostIp != "192.168.1.100" { if s.HostIP != "192.168.1.100" {
t.Fail() t.Fail()
} }
} }
@ -379,7 +379,7 @@ func TestParseNetworkOptsPublic(t *testing.T) {
t.Logf("Expected 8080 got %s", s.HostPort) t.Logf("Expected 8080 got %s", s.HostPort)
t.Fail() t.Fail()
} }
if s.HostIp != "192.168.1.100" { if s.HostIP != "192.168.1.100" {
t.Fail() t.Fail()
} }
} }
@ -454,7 +454,7 @@ func TestParseNetworkOptsUdp(t *testing.T) {
t.Logf("Expected \"\" got %s", s.HostPort) t.Logf("Expected \"\" got %s", s.HostPort)
t.Fail() t.Fail()
} }
if s.HostIp != "192.168.1.100" { if s.HostIP != "192.168.1.100" {
t.Fail() t.Fail()
} }
} }

View file

@ -26,6 +26,9 @@ func (s *portSorter) Less(i, j int) bool {
return s.by(ip, jp) return s.by(ip, jp)
} }
// Sort sorts a list of ports using the provided predicate
// This function should compare `i` and `j`, returning true if `i` is
// considered to be less than `j`
func Sort(ports []Port, predicate func(i, j Port) bool) { func Sort(ports []Port, predicate func(i, j Port) bool) {
s := &portSorter{ports, predicate} s := &portSorter{ports, predicate}
sort.Sort(s) sort.Sort(s)

View file

@ -59,10 +59,10 @@ func TestSortPortMap(t *testing.T) {
}, },
Port("6379/tcp"): []PortBinding{ Port("6379/tcp"): []PortBinding{
{}, {},
{HostIp: "0.0.0.0", HostPort: "32749"}, {HostIP: "0.0.0.0", HostPort: "32749"},
}, },
Port("9999/tcp"): []PortBinding{ Port("9999/tcp"): []PortBinding{
{HostIp: "0.0.0.0", HostPort: "40000"}, {HostIP: "0.0.0.0", HostPort: "40000"},
}, },
} }
@ -77,7 +77,7 @@ func TestSortPortMap(t *testing.T) {
t.Errorf("failed to prioritize port with explicit mappings, got %v", ports) t.Errorf("failed to prioritize port with explicit mappings, got %v", ports)
} }
if pm := portMap[Port("6379/tcp")]; !reflect.DeepEqual(pm, []PortBinding{ if pm := portMap[Port("6379/tcp")]; !reflect.DeepEqual(pm, []PortBinding{
{HostIp: "0.0.0.0", HostPort: "32749"}, {HostIP: "0.0.0.0", HostPort: "32749"},
{}, {},
}) { }) {
t.Errorf("failed to prioritize bindings with explicit mappings, got %v", pm) t.Errorf("failed to prioritize bindings with explicit mappings, got %v", pm)