sub-tests + Fatalf/Errorf

Signed-off-by: Emile Vauge <emile@vauge.com>
This commit is contained in:
Emile Vauge 2017-03-20 13:34:07 +01:00
parent a456d36cc6
commit 6edc0926eb
No known key found for this signature in database
GPG key ID: D808B4C167352E59

View file

@ -15,6 +15,7 @@ import (
"github.com/docker/engine-api/types/swarm" "github.com/docker/engine-api/types/swarm"
"github.com/docker/go-connections/nat" "github.com/docker/go-connections/nat"
"golang.org/x/net/context" "golang.org/x/net/context"
"strconv"
) )
func TestDockerGetFrontendName(t *testing.T) { func TestDockerGetFrontendName(t *testing.T) {
@ -103,12 +104,16 @@ func TestDockerGetFrontendName(t *testing.T) {
}, },
} }
for _, e := range containers { for containerID, e := range containers {
dockerData := parseContainer(e.container) e := e
actual := provider.getFrontendName(dockerData) t.Run(strconv.Itoa(containerID), func(t *testing.T) {
if actual != e.expected { t.Parallel()
t.Fatalf("expected %q, got %q", e.expected, actual) dockerData := parseContainer(e.container)
} actual := provider.getFrontendName(dockerData)
if actual != e.expected {
t.Errorf("expected %q, got %q", e.expected, actual)
}
})
} }
} }
@ -180,12 +185,16 @@ func TestDockerGetFrontendRule(t *testing.T) {
}, },
} }
for _, e := range containers { for containerID, e := range containers {
dockerData := parseContainer(e.container) e := e
actual := provider.getFrontendRule(dockerData) t.Run(strconv.Itoa(containerID), func(t *testing.T) {
if actual != e.expected { t.Parallel()
t.Fatalf("expected %q, got %q", e.expected, actual) dockerData := parseContainer(e.container)
} actual := provider.getFrontendRule(dockerData)
if actual != e.expected {
t.Errorf("expected %q, got %q", e.expected, actual)
}
})
} }
} }
@ -243,12 +252,16 @@ func TestDockerGetBackend(t *testing.T) {
}, },
} }
for _, e := range containers { for containerID, e := range containers {
dockerData := parseContainer(e.container) e := e
actual := provider.getBackend(dockerData) t.Run(strconv.Itoa(containerID), func(t *testing.T) {
if actual != e.expected { t.Parallel()
t.Fatalf("expected %q, got %q", e.expected, actual) dockerData := parseContainer(e.container)
} actual := provider.getBackend(dockerData)
if actual != e.expected {
t.Errorf("expected %q, got %q", e.expected, actual)
}
})
} }
} }
@ -344,12 +357,16 @@ func TestDockerGetIPAddress(t *testing.T) { // TODO
}, },
} }
for _, e := range containers { for containerID, e := range containers {
dockerData := parseContainer(e.container) e := e
actual := provider.getIPAddress(dockerData) t.Run(strconv.Itoa(containerID), func(t *testing.T) {
if actual != e.expected { t.Parallel()
t.Fatalf("expected %q, got %q", e.expected, actual) dockerData := parseContainer(e.container)
} actual := provider.getIPAddress(dockerData)
if actual != e.expected {
t.Errorf("expected %q, got %q", e.expected, actual)
}
})
} }
} }
@ -457,12 +474,16 @@ func TestDockerGetPort(t *testing.T) {
}, },
} }
for _, e := range containers { for containerID, e := range containers {
dockerData := parseContainer(e.container) e := e
actual := provider.getPort(dockerData) t.Run(strconv.Itoa(containerID), func(t *testing.T) {
if actual != e.expected { t.Parallel()
t.Fatalf("expected %q, got %q", e.expected, actual) dockerData := parseContainer(e.container)
} actual := provider.getPort(dockerData)
if actual != e.expected {
t.Errorf("expected %q, got %q", e.expected, actual)
}
})
} }
} }
@ -497,12 +518,16 @@ func TestDockerGetWeight(t *testing.T) {
}, },
} }
for _, e := range containers { for containerID, e := range containers {
dockerData := parseContainer(e.container) e := e
actual := provider.getWeight(dockerData) t.Run(strconv.Itoa(containerID), func(t *testing.T) {
if actual != e.expected { t.Parallel()
t.Fatalf("expected %q, got %q", e.expected, actual) dockerData := parseContainer(e.container)
} actual := provider.getWeight(dockerData)
if actual != e.expected {
t.Errorf("expected %q, got %q", e.expected, actual)
}
})
} }
} }
@ -539,12 +564,16 @@ func TestDockerGetDomain(t *testing.T) {
}, },
} }
for _, e := range containers { for containerID, e := range containers {
dockerData := parseContainer(e.container) e := e
actual := provider.getDomain(dockerData) t.Run(strconv.Itoa(containerID), func(t *testing.T) {
if actual != e.expected { t.Parallel()
t.Fatalf("expected %q, got %q", e.expected, actual) dockerData := parseContainer(e.container)
} actual := provider.getDomain(dockerData)
if actual != e.expected {
t.Errorf("expected %q, got %q", e.expected, actual)
}
})
} }
} }
@ -579,12 +608,16 @@ func TestDockerGetProtocol(t *testing.T) {
}, },
} }
for _, e := range containers { for containerID, e := range containers {
dockerData := parseContainer(e.container) e := e
actual := provider.getProtocol(dockerData) t.Run(strconv.Itoa(containerID), func(t *testing.T) {
if actual != e.expected { t.Parallel()
t.Fatalf("expected %q, got %q", e.expected, actual) dockerData := parseContainer(e.container)
} actual := provider.getProtocol(dockerData)
if actual != e.expected {
t.Errorf("expected %q, got %q", e.expected, actual)
}
})
} }
} }
@ -618,12 +651,16 @@ func TestDockerGetPassHostHeader(t *testing.T) {
}, },
} }
for _, e := range containers { for containerID, e := range containers {
dockerData := parseContainer(e.container) e := e
actual := provider.getPassHostHeader(dockerData) t.Run(strconv.Itoa(containerID), func(t *testing.T) {
if actual != e.expected { t.Parallel()
t.Fatalf("expected %q, got %q", e.expected, actual) dockerData := parseContainer(e.container)
} actual := provider.getPassHostHeader(dockerData)
if actual != e.expected {
t.Errorf("expected %q, got %q", e.expected, actual)
}
})
} }
} }
@ -656,18 +693,22 @@ func TestDockerGetLabel(t *testing.T) {
}, },
} }
for _, e := range containers { for containerID, e := range containers {
dockerData := parseContainer(e.container) e := e
label, err := getLabel(dockerData, "foo") t.Run(strconv.Itoa(containerID), func(t *testing.T) {
if e.expected != "" { t.Parallel()
if err == nil || !strings.Contains(err.Error(), e.expected) { dockerData := parseContainer(e.container)
t.Fatalf("expected an error with %q, got %v", e.expected, err) label, err := getLabel(dockerData, "foo")
if e.expected != "" {
if err == nil || !strings.Contains(err.Error(), e.expected) {
t.Errorf("expected an error with %q, got %v", e.expected, err)
}
} else {
if label != "bar" {
t.Errorf("expected label 'bar', got %s", label)
}
} }
} else { })
if label != "bar" {
t.Fatalf("expected label 'bar', got %s", label)
}
}
} }
} }
@ -723,17 +764,21 @@ func TestDockerGetLabels(t *testing.T) {
}, },
} }
for _, e := range containers { for containerID, e := range containers {
dockerData := parseContainer(e.container) e := e
labels, err := getLabels(dockerData, []string{"foo", "bar"}) t.Run(strconv.Itoa(containerID), func(t *testing.T) {
if !reflect.DeepEqual(labels, e.expectedLabels) { t.Parallel()
t.Fatalf("expect %v, got %v", e.expectedLabels, labels) dockerData := parseContainer(e.container)
} labels, err := getLabels(dockerData, []string{"foo", "bar"})
if e.expectedError != "" { if !reflect.DeepEqual(labels, e.expectedLabels) {
if err == nil || !strings.Contains(err.Error(), e.expectedError) { t.Errorf("expect %v, got %v", e.expectedLabels, labels)
t.Fatalf("expected an error with %q, got %v", e.expectedError, err)
} }
} if e.expectedError != "" {
if err == nil || !strings.Contains(err.Error(), e.expectedError) {
t.Errorf("expected an error with %q, got %v", e.expectedError, err)
}
}
})
} }
} }
@ -957,13 +1002,17 @@ func TestDockerTraefikFilter(t *testing.T) {
}, },
} }
for _, e := range containers { for containerID, e := range containers {
provider.ExposedByDefault = e.exposedByDefault e := e
dockerData := parseContainer(e.container) t.Run(strconv.Itoa(containerID), func(t *testing.T) {
actual := provider.containerFilter(dockerData) t.Parallel()
if actual != e.expected { provider.ExposedByDefault = e.exposedByDefault
t.Fatalf("expected %v for %+v, got %+v", e.expected, e, actual) dockerData := parseContainer(e.container)
} actual := provider.containerFilter(dockerData)
if actual != e.expected {
t.Errorf("expected %v for %+v, got %+v", e.expected, e, actual)
}
})
} }
} }
@ -1179,21 +1228,25 @@ func TestDockerLoadDockerConfig(t *testing.T) {
ExposedByDefault: true, ExposedByDefault: true,
} }
for _, c := range cases { for caseID, c := range cases {
var dockerDataList []dockerData c := c
for _, container := range c.containers { t.Run(strconv.Itoa(caseID), func(t *testing.T) {
dockerData := parseContainer(container) t.Parallel()
dockerDataList = append(dockerDataList, dockerData) var dockerDataList []dockerData
} for _, container := range c.containers {
dockerData := parseContainer(container)
dockerDataList = append(dockerDataList, dockerData)
}
actualConfig := provider.loadDockerConfig(dockerDataList) actualConfig := provider.loadDockerConfig(dockerDataList)
// Compare backends // Compare backends
if !reflect.DeepEqual(actualConfig.Backends, c.expectedBackends) { if !reflect.DeepEqual(actualConfig.Backends, c.expectedBackends) {
t.Fatalf("expected %#v, got %#v", c.expectedBackends, actualConfig.Backends) t.Errorf("expected %#v, got %#v", c.expectedBackends, actualConfig.Backends)
} }
if !reflect.DeepEqual(actualConfig.Frontends, c.expectedFrontends) { if !reflect.DeepEqual(actualConfig.Frontends, c.expectedFrontends) {
t.Fatalf("expected %#v, got %#v", c.expectedFrontends, actualConfig.Frontends) t.Errorf("expected %#v, got %#v", c.expectedFrontends, actualConfig.Frontends)
} }
})
} }
} }
@ -1277,12 +1330,16 @@ func TestSwarmGetFrontendName(t *testing.T) {
}, },
} }
for _, e := range services { for serviceID, e := range services {
dockerData := parseService(e.service, e.networks) e := e
actual := provider.getFrontendName(dockerData) t.Run(strconv.Itoa(serviceID), func(t *testing.T) {
if actual != e.expected { t.Parallel()
t.Fatalf("expected %q, got %q", e.expected, actual) dockerData := parseService(e.service, e.networks)
} actual := provider.getFrontendName(dockerData)
if actual != e.expected {
t.Errorf("expected %q, got %q", e.expected, actual)
}
})
} }
} }
@ -1349,12 +1406,16 @@ func TestSwarmGetFrontendRule(t *testing.T) {
}, },
} }
for _, e := range services { for serviceID, e := range services {
dockerData := parseService(e.service, e.networks) e := e
actual := provider.getFrontendRule(dockerData) t.Run(strconv.Itoa(serviceID), func(t *testing.T) {
if actual != e.expected { t.Parallel()
t.Fatalf("expected %q, got %q", e.expected, actual) dockerData := parseService(e.service, e.networks)
} actual := provider.getFrontendRule(dockerData)
if actual != e.expected {
t.Errorf("expected %q, got %q", e.expected, actual)
}
})
} }
} }
@ -1406,12 +1467,16 @@ func TestSwarmGetBackend(t *testing.T) {
}, },
} }
for _, e := range services { for serviceID, e := range services {
dockerData := parseService(e.service, e.networks) e := e
actual := provider.getBackend(dockerData) t.Run(strconv.Itoa(serviceID), func(t *testing.T) {
if actual != e.expected { t.Parallel()
t.Fatalf("expected %q, got %q", e.expected, actual) dockerData := parseService(e.service, e.networks)
} actual := provider.getBackend(dockerData)
if actual != e.expected {
t.Errorf("expected %q, got %q", e.expected, actual)
}
})
} }
} }
@ -1503,12 +1568,16 @@ func TestSwarmGetIPAddress(t *testing.T) {
}, },
} }
for _, e := range services { for serviceID, e := range services {
dockerData := parseService(e.service, e.networks) e := e
actual := provider.getIPAddress(dockerData) t.Run(strconv.Itoa(serviceID), func(t *testing.T) {
if actual != e.expected { t.Parallel()
t.Fatalf("expected %q, got %q", e.expected, actual) dockerData := parseService(e.service, e.networks)
} actual := provider.getIPAddress(dockerData)
if actual != e.expected {
t.Errorf("expected %q, got %q", e.expected, actual)
}
})
} }
} }
@ -1541,12 +1610,16 @@ func TestSwarmGetPort(t *testing.T) {
}, },
} }
for _, e := range services { for serviceID, e := range services {
dockerData := parseService(e.service, e.networks) e := e
actual := provider.getPort(dockerData) t.Run(strconv.Itoa(serviceID), func(t *testing.T) {
if actual != e.expected { t.Parallel()
t.Fatalf("expected %q, got %q", e.expected, actual) dockerData := parseService(e.service, e.networks)
} actual := provider.getPort(dockerData)
if actual != e.expected {
t.Errorf("expected %q, got %q", e.expected, actual)
}
})
} }
} }
@ -1593,12 +1666,16 @@ func TestSwarmGetWeight(t *testing.T) {
}, },
} }
for _, e := range services { for serviceID, e := range services {
dockerData := parseService(e.service, e.networks) e := e
actual := provider.getWeight(dockerData) t.Run(strconv.Itoa(serviceID), func(t *testing.T) {
if actual != e.expected { t.Parallel()
t.Fatalf("expected %q, got %q", e.expected, actual) dockerData := parseService(e.service, e.networks)
} actual := provider.getWeight(dockerData)
if actual != e.expected {
t.Errorf("expected %q, got %q", e.expected, actual)
}
})
} }
} }
@ -1646,12 +1723,16 @@ func TestSwarmGetDomain(t *testing.T) {
}, },
} }
for _, e := range services { for serviceID, e := range services {
dockerData := parseService(e.service, e.networks) e := e
actual := provider.getDomain(dockerData) t.Run(strconv.Itoa(serviceID), func(t *testing.T) {
if actual != e.expected { t.Parallel()
t.Fatalf("expected %q, got %q", e.expected, actual) dockerData := parseService(e.service, e.networks)
} actual := provider.getDomain(dockerData)
if actual != e.expected {
t.Errorf("expected %q, got %q", e.expected, actual)
}
})
} }
} }
@ -1698,12 +1779,16 @@ func TestSwarmGetProtocol(t *testing.T) {
}, },
} }
for _, e := range services { for serviceID, e := range services {
dockerData := parseService(e.service, e.networks) e := e
actual := provider.getProtocol(dockerData) t.Run(strconv.Itoa(serviceID), func(t *testing.T) {
if actual != e.expected { t.Parallel()
t.Fatalf("expected %q, got %q", e.expected, actual) dockerData := parseService(e.service, e.networks)
} actual := provider.getProtocol(dockerData)
if actual != e.expected {
t.Errorf("expected %q, got %q", e.expected, actual)
}
})
} }
} }
@ -1750,17 +1835,20 @@ func TestSwarmGetPassHostHeader(t *testing.T) {
}, },
} }
for _, e := range services { for serviceID, e := range services {
dockerData := parseService(e.service, e.networks) e := e
actual := provider.getPassHostHeader(dockerData) t.Run(strconv.Itoa(serviceID), func(t *testing.T) {
if actual != e.expected { t.Parallel()
t.Fatalf("expected %q, got %q", e.expected, actual) dockerData := parseService(e.service, e.networks)
} actual := provider.getPassHostHeader(dockerData)
if actual != e.expected {
t.Errorf("expected %q, got %q", e.expected, actual)
}
})
} }
} }
func TestSwarmGetLabel(t *testing.T) { func TestSwarmGetLabel(t *testing.T) {
services := []struct { services := []struct {
service swarm.Service service swarm.Service
expected string expected string
@ -1799,18 +1887,22 @@ func TestSwarmGetLabel(t *testing.T) {
}, },
} }
for _, e := range services { for serviceID, e := range services {
dockerData := parseService(e.service, e.networks) e := e
label, err := getLabel(dockerData, "foo") t.Run(strconv.Itoa(serviceID), func(t *testing.T) {
if e.expected != "" { t.Parallel()
if err == nil || !strings.Contains(err.Error(), e.expected) { dockerData := parseService(e.service, e.networks)
t.Fatalf("expected an error with %q, got %v", e.expected, err) label, err := getLabel(dockerData, "foo")
if e.expected != "" {
if err == nil || !strings.Contains(err.Error(), e.expected) {
t.Errorf("expected an error with %q, got %v", e.expected, err)
}
} else {
if label != "bar" {
t.Errorf("expected label 'bar', got %s", label)
}
} }
} else { })
if label != "bar" {
t.Fatalf("expected label 'bar', got %s", label)
}
}
} }
} }
@ -1871,17 +1963,21 @@ func TestSwarmGetLabels(t *testing.T) {
}, },
} }
for _, e := range services { for serviceID, e := range services {
dockerData := parseService(e.service, e.networks) e := e
labels, err := getLabels(dockerData, []string{"foo", "bar"}) t.Run(strconv.Itoa(serviceID), func(t *testing.T) {
if !reflect.DeepEqual(labels, e.expectedLabels) { t.Parallel()
t.Fatalf("expect %v, got %v", e.expectedLabels, labels) dockerData := parseService(e.service, e.networks)
} labels, err := getLabels(dockerData, []string{"foo", "bar"})
if e.expectedError != "" { if !reflect.DeepEqual(labels, e.expectedLabels) {
if err == nil || !strings.Contains(err.Error(), e.expectedError) { t.Errorf("expect %v, got %v", e.expectedLabels, labels)
t.Fatalf("expected an error with %q, got %v", e.expectedError, err)
} }
} if e.expectedError != "" {
if err == nil || !strings.Contains(err.Error(), e.expectedError) {
t.Errorf("expected an error with %q, got %v", e.expectedError, err)
}
}
})
} }
} }
@ -2035,13 +2131,17 @@ func TestSwarmTraefikFilter(t *testing.T) {
}, },
} }
for _, e := range services { for serviceID, e := range services {
dockerData := parseService(e.service, e.networks) e := e
provider.ExposedByDefault = e.exposedByDefault t.Run(strconv.Itoa(serviceID), func(t *testing.T) {
actual := provider.containerFilter(dockerData) t.Parallel()
if actual != e.expected { dockerData := parseService(e.service, e.networks)
t.Fatalf("expected %v for %+v, got %+v", e.expected, e, actual) provider.ExposedByDefault = e.exposedByDefault
} actual := provider.containerFilter(dockerData)
if actual != e.expected {
t.Errorf("expected %v for %+v, got %+v", e.expected, e, actual)
}
})
} }
} }
@ -2212,21 +2312,25 @@ func TestSwarmLoadDockerConfig(t *testing.T) {
SwarmMode: true, SwarmMode: true,
} }
for _, c := range cases { for caseID, c := range cases {
var dockerDataList []dockerData c := c
for _, service := range c.services { t.Run(strconv.Itoa(caseID), func(t *testing.T) {
dockerData := parseService(service, c.networks) t.Parallel()
dockerDataList = append(dockerDataList, dockerData) var dockerDataList []dockerData
} for _, service := range c.services {
dockerData := parseService(service, c.networks)
dockerDataList = append(dockerDataList, dockerData)
}
actualConfig := provider.loadDockerConfig(dockerDataList) actualConfig := provider.loadDockerConfig(dockerDataList)
// Compare backends // Compare backends
if !reflect.DeepEqual(actualConfig.Backends, c.expectedBackends) { if !reflect.DeepEqual(actualConfig.Backends, c.expectedBackends) {
t.Fatalf("expected %#v, got %#v", c.expectedBackends, actualConfig.Backends) t.Errorf("expected %#v, got %#v", c.expectedBackends, actualConfig.Backends)
} }
if !reflect.DeepEqual(actualConfig.Frontends, c.expectedFrontends) { if !reflect.DeepEqual(actualConfig.Frontends, c.expectedFrontends) {
t.Fatalf("expected %#v, got %#v", c.expectedFrontends, actualConfig.Frontends) t.Errorf("expected %#v, got %#v", c.expectedFrontends, actualConfig.Frontends)
} }
})
} }
} }
@ -2305,15 +2409,19 @@ func TestSwarmTaskParsing(t *testing.T) {
}, },
} }
for _, e := range cases { for caseID, e := range cases {
dockerData := parseService(e.service, e.networks) e := e
t.Run(strconv.Itoa(caseID), func(t *testing.T) {
t.Parallel()
dockerData := parseService(e.service, e.networks)
for _, task := range e.tasks { for _, task := range e.tasks {
taskDockerData := parseTasks(task, dockerData, map[string]*docker.NetworkResource{}, e.isGlobalSVC) taskDockerData := parseTasks(task, dockerData, map[string]*docker.NetworkResource{}, e.isGlobalSVC)
if !reflect.DeepEqual(taskDockerData.Name, e.expectedNames[task.ID]) { if !reflect.DeepEqual(taskDockerData.Name, e.expectedNames[task.ID]) {
t.Fatalf("expect %v, got %v", e.expectedNames[task.ID], taskDockerData.Name) t.Errorf("expect %v, got %v", e.expectedNames[task.ID], taskDockerData.Name)
}
} }
} })
} }
} }
@ -2390,20 +2498,24 @@ func TestListTasks(t *testing.T) {
}, },
} }
for _, e := range cases { for caseID, e := range cases {
dockerData := parseService(e.service, e.networks) e := e
dockerClient := &fakeTasksClient{tasks: e.tasks} t.Run(strconv.Itoa(caseID), func(t *testing.T) {
taskDockerData, _ := listTasks(context.Background(), dockerClient, e.service.ID, dockerData, map[string]*docker.NetworkResource{}, e.isGlobalSVC) t.Parallel()
dockerData := parseService(e.service, e.networks)
dockerClient := &fakeTasksClient{tasks: e.tasks}
taskDockerData, _ := listTasks(context.Background(), dockerClient, e.service.ID, dockerData, map[string]*docker.NetworkResource{}, e.isGlobalSVC)
if len(e.expectedTasks) != len(taskDockerData) { if len(e.expectedTasks) != len(taskDockerData) {
t.Fatalf("expected tasks %v, got %v", spew.Sprint(e.expectedTasks), spew.Sprint(taskDockerData)) t.Errorf("expected tasks %v, got %v", spew.Sdump(e.expectedTasks), spew.Sdump(taskDockerData))
}
for i, taskID := range e.expectedTasks {
if taskDockerData[i].Name != taskID {
t.Fatalf("expect task id %v, got %v", taskID, taskDockerData[i].Name)
} }
}
for i, taskID := range e.expectedTasks {
if taskDockerData[i].Name != taskID {
t.Errorf("expect task id %v, got %v", taskID, taskDockerData[i].Name)
}
}
})
} }
} }