Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions nat/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ func splitParts(rawport string) (hostIP, hostPort, containerPort string) {
func ParsePortSpec(rawPort string) ([]PortMapping, error) {
ip, hostPort, containerPort := splitParts(rawPort)
proto, containerPort := SplitProtoPort(containerPort)
if containerPort == "" {
return nil, fmt.Errorf("no port specified: %s<empty>", rawPort)
}

proto = strings.ToLower(proto)
if err := validateProto(proto); err != nil {
return nil, err
Expand All @@ -189,9 +193,6 @@ func ParsePortSpec(rawPort string) ([]PortMapping, error) {
if ip != "" && net.ParseIP(ip) == nil {
return nil, errors.New("invalid IP address: " + ip)
}
if containerPort == "" {
return nil, fmt.Errorf("no port specified: %s<empty>", rawPort)
}

startPort, endPort, err := ParsePortRange(containerPort)
if err != nil {
Expand Down
32 changes: 32 additions & 0 deletions nat/nat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,38 @@ func TestSplitProtoPort(t *testing.T) {
}
}

func TestParsePortSpecEmptyContainerPort(t *testing.T) {
tests := []struct {
name string
spec string
expError string
}{
{
name: "empty spec",
spec: "",
expError: `no port specified: <empty>`,
},
{
name: "empty container port",
spec: `0.0.0.0:1234-1235:/tcp`,
expError: `no port specified: 0.0.0.0:1234-1235:/tcp<empty>`,
},
{
name: "empty container port and proto",
spec: `0.0.0.0:1234-1235:`,
expError: `no port specified: 0.0.0.0:1234-1235:<empty>`,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
_, err := ParsePortSpec(tc.spec)
if err == nil || err.Error() != tc.expError {
t.Fatalf("expected %v, got: %v", tc.expError, err)
}
})
}
}

func TestParsePortSpecFull(t *testing.T) {
portMappings, err := ParsePortSpec("0.0.0.0:1234-1235:3333-3334/tcp")
if err != nil {
Expand Down
Loading