driverutil: refactor splitPortProto to parse port as str

Signed-off-by: André Carvalho <andre.carvalho@corp.globo.com>
This commit is contained in:
André Carvalho 2016-08-22 09:29:51 -03:00 committed by Misty Stanley-Jones
parent e2e2395a3d
commit a171962328
2 changed files with 7 additions and 15 deletions

View File

@ -1,23 +1,16 @@
package driverutil
import (
"fmt"
"strconv"
"strings"
)
import "strings"
// SplitPortProto splits a string in the format port/protocol, defaulting
// protocol to "tcp" if not provided.
func SplitPortProto(raw string) (port int, protocol string, err error) {
func SplitPortProto(raw string) (port string, protocol string, err error) {
parts := strings.Split(raw, "/")
if len(parts) == 1 {
protocol = "tcp"
} else {
protocol = parts[1]
}
port, err = strconv.Atoi(parts[0])
if err != nil {
return 0, "", fmt.Errorf("invalid port number %s: %s", parts[0], err)
}
port = parts[0]
return port, protocol, nil
}

View File

@ -9,14 +9,13 @@ import (
func TestSplitPortProtocol(t *testing.T) {
tests := []struct {
raw string
expectedPort int
expectedPort string
expectedProto string
expectedErr bool
}{
{"8080/tcp", 8080, "tcp", false},
{"90/udp", 90, "udp", false},
{"80", 80, "tcp", false},
{"abc", 0, "", true},
{"8080/tcp", "8080", "tcp", false},
{"90/udp", "90", "udp", false},
{"80", "80", "tcp", false},
}
for _, tc := range tests {