1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #15902 from SvenDowideit/add-more-tests-for-parsehost

Exploring the parsehost using unit tests
This commit is contained in:
Alexander Morozov 2015-08-28 09:14:35 -07:00
commit 50bcf17c39

View file

@ -1,6 +1,7 @@
package parsers
import (
"runtime"
"strings"
"testing"
)
@ -9,14 +10,20 @@ func TestParseHost(t *testing.T) {
var (
defaultHTTPHost = "127.0.0.1"
defaultUnix = "/var/run/docker.sock"
defaultHOST = "unix:///var/run/docker.sock"
)
invalids := map[string]string{
"0.0.0.0": "Invalid bind address format: 0.0.0.0",
"0.0.0.0:": "Invalid bind address format: 0.0.0.0:",
"tcp://": "Invalid proto, expected tcp: ",
"tcp:a.b.c.d": "Invalid bind address format: tcp:a.b.c.d",
"tcp:a.b.c.d/path": "Invalid bind address format: tcp:a.b.c.d/path",
"udp://127.0.0.1": "Invalid bind address format: udp://127.0.0.1",
"udp://127.0.0.1:2375": "Invalid bind address format: udp://127.0.0.1:2375",
"tcp://unix:///run/docker.sock": "Invalid bind address format: unix",
"tcp": "Invalid bind address format: tcp",
"unix": "Invalid bind address format: unix",
"fd": "Invalid bind address format: fd",
}
valids := map[string]string{
"0.0.0.1:5555": "tcp://0.0.0.1:5555",
@ -25,12 +32,24 @@ func TestParseHost(t *testing.T) {
":6666/path": "tcp://127.0.0.1:6666/path",
"tcp://:7777": "tcp://127.0.0.1:7777",
"tcp://:7777/path": "tcp://127.0.0.1:7777/path",
"": "unix:///var/run/docker.sock",
// as there's a TrimSpace in there, we should test for it.
" tcp://:7777/path ": "tcp://127.0.0.1:7777/path",
"unix:///run/docker.sock": "unix:///run/docker.sock",
"unix://": "unix:///var/run/docker.sock",
"fd://": "fd://",
"fd://something": "fd://something",
}
if runtime.GOOS == "windows" {
defaultHOST = "Invalid bind address format: 127.0.0.1"
// SVEN: an example of the conflicted defaultHTTPHost
invalids[""] = defaultHOST
invalids[" "] = defaultHOST
invalids[" "] = defaultHOST
} else {
valids[""] = defaultHOST
valids[" "] = defaultHOST
valids[" "] = defaultHOST
}
for invalidAddr, expectedError := range invalids {
if addr, err := ParseHost(defaultHTTPHost, defaultUnix, invalidAddr); err == nil || err.Error() != expectedError {
t.Errorf("tcp %v address expected error %v return, got %s and addr %v", invalidAddr, expectedError, err, addr)
@ -38,15 +57,56 @@ func TestParseHost(t *testing.T) {
}
for validAddr, expectedAddr := range valids {
if addr, err := ParseHost(defaultHTTPHost, defaultUnix, validAddr); err != nil || addr != expectedAddr {
t.Errorf("%v -> expected %v, got %v", validAddr, expectedAddr, addr)
t.Errorf("%v -> expected %v, got (%v) addr (%v)", validAddr, expectedAddr, err, addr)
}
}
}
func TestParseTCP(t *testing.T) {
var (
//SVEN if this is set to tcp://127.0.0.1, then we end up with results like 'tcp://tcp://127.0.0.1:6666'
defaultHTTPHost = "127.0.0.1"
)
invalids := map[string]string{
"0.0.0.0": "Invalid bind address format: 0.0.0.0",
"0.0.0.0:": "Invalid bind address format: 0.0.0.0:",
"tcp://": "Invalid proto, expected tcp: ",
"tcp:a.b.c.d": "Invalid bind address format: tcp:a.b.c.d",
"tcp:a.b.c.d/path": "Invalid bind address format: tcp:a.b.c.d/path",
"udp://127.0.0.1": "Invalid proto, expected tcp: udp://127.0.0.1",
"udp://127.0.0.1:2375": "Invalid proto, expected tcp: udp://127.0.0.1:2375",
"": "Invalid proto, expected tcp: ",
}
valids := map[string]string{
"0.0.0.1:5555": "tcp://0.0.0.1:5555",
"0.0.0.1:5555/path": "tcp://0.0.0.1:5555/path",
":6666": "tcp://127.0.0.1:6666",
":6666/path": "tcp://127.0.0.1:6666/path",
"tcp://:7777": "tcp://127.0.0.1:7777",
"tcp://:7777/path": "tcp://127.0.0.1:7777/path",
}
for invalidAddr, expectedError := range invalids {
if addr, err := ParseTCPAddr(invalidAddr, defaultHTTPHost); err == nil || err.Error() != expectedError {
t.Errorf("tcp %v address expected error %v return, got %s and addr %v", invalidAddr, expectedError, err, addr)
}
}
for validAddr, expectedAddr := range valids {
if addr, err := ParseTCPAddr(validAddr, defaultHTTPHost); err != nil || addr != expectedAddr {
t.Errorf("%v -> expected %v, got %v and addr %v", validAddr, expectedAddr, err, addr)
}
}
}
func TestParseInvalidUnixAddrInvalid(t *testing.T) {
if _, err := ParseUnixAddr("tcp://127.0.0.1", "unix:///var/run/docker.sock"); err == nil || err.Error() != "Invalid proto, expected unix: tcp://127.0.0.1" {
t.Fatalf("Expected an error, got %v", err)
}
if _, err := ParseUnixAddr("unix://tcp://127.0.0.1", "unix:///var/run/docker.sock"); err == nil || err.Error() != "Invalid proto, expected unix: tcp://127.0.0.1" {
t.Fatalf("Expected an error, got %v", err)
}
if v, err := ParseUnixAddr("", "/var/run/docker.sock"); err != nil || v != "unix:///var/run/docker.sock" {
t.Fatalf("Expected an %v, got %v", v, "unix:///var/run/docker.sock")
}
}
func TestParseRepositoryTag(t *testing.T) {