Run Rancher tests cases in parallel.

This commit is contained in:
Ludovic Fernandez 2017-11-20 11:40:04 +01:00 committed by Traefiker
parent be306d651e
commit ab87bad952
3 changed files with 219 additions and 166 deletions

View file

@ -213,6 +213,7 @@ func TestTaskRecords(t *testing.T) {
} }
func TestMesosLoadConfig(t *testing.T) { func TestMesosLoadConfig(t *testing.T) {
// FIXME this test is dead?
cases := []struct { cases := []struct {
applicationsError bool applicationsError bool
tasksError bool tasksError bool
@ -223,6 +224,7 @@ func TestMesosLoadConfig(t *testing.T) {
expectedFrontends map[string]*types.Frontend expectedFrontends map[string]*types.Frontend
expectedBackends map[string]*types.Backend expectedBackends map[string]*types.Backend
}{} }{}
for _, c := range cases { for _, c := range cases {
provider := &Provider{ provider := &Provider{
Domain: "docker.localhost", Domain: "docker.localhost",

View file

@ -250,9 +250,7 @@ func (p *Provider) loadRancherConfig(services []rancherData) *types.Configuratio
} }
// filter services // filter services
filteredServices := fun.Filter(func(service rancherData) bool { filteredServices := fun.Filter(p.serviceFilter, services).([]rancherData)
return p.serviceFilter(service)
}, services).([]rancherData)
frontends := map[string]rancherData{} frontends := map[string]rancherData{}
backends := map[string]rancherData{} backends := map[string]rancherData{}

View file

@ -1,7 +1,6 @@
package rancher package rancher
import ( import (
"reflect"
"strings" "strings"
"testing" "testing"
@ -9,7 +8,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestRancherServiceFilter(t *testing.T) { func TestProviderServiceFilter(t *testing.T) {
provider := &Provider{ provider := &Provider{
Domain: "rancher.localhost", Domain: "rancher.localhost",
EnableServiceHealthFilter: true, EnableServiceHealthFilter: true,
@ -18,11 +17,13 @@ func TestRancherServiceFilter(t *testing.T) {
constraint, _ := types.NewConstraint("tag==ch*se") constraint, _ := types.NewConstraint("tag==ch*se")
provider.Constraints = types.Constraints{constraint} provider.Constraints = types.Constraints{constraint}
services := []struct { testCases := []struct {
desc string
service rancherData service rancherData
expected bool expected bool
}{ }{
{ {
desc: "missing Port labels, don't respect constraint",
service: rancherData{ service: rancherData{
Labels: map[string]string{ Labels: map[string]string{
types.LabelEnable: "true", types.LabelEnable: "true",
@ -33,6 +34,7 @@ func TestRancherServiceFilter(t *testing.T) {
expected: false, expected: false,
}, },
{ {
desc: "don't respect constraint",
service: rancherData{ service: rancherData{
Labels: map[string]string{ Labels: map[string]string{
types.LabelPort: "80", types.LabelPort: "80",
@ -44,8 +46,10 @@ func TestRancherServiceFilter(t *testing.T) {
expected: false, expected: false,
}, },
{ {
desc: "unhealthy",
service: rancherData{ service: rancherData{
Labels: map[string]string{ Labels: map[string]string{
types.LabelTags: "cheese",
types.LabelPort: "80", types.LabelPort: "80",
types.LabelEnable: "true", types.LabelEnable: "true",
}, },
@ -55,6 +59,7 @@ func TestRancherServiceFilter(t *testing.T) {
expected: false, expected: false,
}, },
{ {
desc: "inactive",
service: rancherData{ service: rancherData{
Labels: map[string]string{ Labels: map[string]string{
types.LabelTags: "not-cheesy", types.LabelTags: "not-cheesy",
@ -67,6 +72,7 @@ func TestRancherServiceFilter(t *testing.T) {
expected: false, expected: false,
}, },
{ {
desc: "healthy & active, tag: cheese",
service: rancherData{ service: rancherData{
Labels: map[string]string{ Labels: map[string]string{
types.LabelTags: "cheese", types.LabelTags: "cheese",
@ -79,18 +85,7 @@ func TestRancherServiceFilter(t *testing.T) {
expected: true, expected: true,
}, },
{ {
service: rancherData{ desc: "healthy & active, tag: chose",
Labels: map[string]string{
types.LabelTags: "cheeeeese",
types.LabelPort: "80",
types.LabelEnable: "true",
},
Health: "healthy",
State: "upgraded",
},
expected: true,
},
{
service: rancherData{ service: rancherData{
Labels: map[string]string{ Labels: map[string]string{
types.LabelTags: "chose", types.LabelTags: "chose",
@ -102,18 +97,34 @@ func TestRancherServiceFilter(t *testing.T) {
}, },
expected: true, expected: true,
}, },
{
desc: "healthy & upgraded",
service: rancherData{
Labels: map[string]string{
types.LabelTags: "cheeeeese",
types.LabelPort: "80",
types.LabelEnable: "true",
},
Health: "healthy",
State: "upgraded",
},
expected: true,
},
} }
for _, e := range services { for _, test := range testCases {
actual := provider.serviceFilter(e.service) test := test
if actual != e.expected { t.Run(test.desc, func(t *testing.T) {
t.Fatalf("expected %t, got %t", e.expected, actual) t.Parallel()
}
actual := provider.serviceFilter(test.service)
assert.Equal(t, test.expected, actual)
})
} }
} }
func TestRancherContainerFilter(t *testing.T) { func TestContainerFilter(t *testing.T) {
containers := []struct { testCases := []struct {
name string name string
healthState string healthState string
state string state string
@ -145,156 +156,167 @@ func TestRancherContainerFilter(t *testing.T) {
}, },
} }
for _, container := range containers { for _, test := range testCases {
actual := containerFilter(container.name, container.healthState, container.state) test := test
if actual != container.expected { t.Run(test.healthState+" "+test.state, func(t *testing.T) {
t.Fatalf("expected %t, got %t", container.expected, actual) t.Parallel()
}
actual := containerFilter(test.name, test.healthState, test.state)
assert.Equal(t, test.expected, actual)
})
} }
} }
func TestRancherGetFrontendName(t *testing.T) { func TestProviderGetFrontendName(t *testing.T) {
provider := &Provider{ provider := &Provider{Domain: "rancher.localhost"}
Domain: "rancher.localhost",
}
services := []struct { testCases := []struct {
desc string
service rancherData service rancherData
expected string expected string
}{ }{
{ {
desc: "default",
service: rancherData{ service: rancherData{
Name: "foo", Name: "foo",
}, },
expected: "Host-foo-rancher-localhost", expected: "Host-foo-rancher-localhost",
}, },
{ {
desc: "with Headers label",
service: rancherData{ service: rancherData{
Name: "test-service", Name: "test-service",
Labels: map[string]string{ Labels: map[string]string{
types.LabelFrontendRule: "Headers:User-Agent,bat/0.1.0", types.LabelFrontendRule: "Headers:User-Agent,bat/0.1.0",
}, },
}, },
expected: "Headers-User-Agent-bat-0-1-0", expected: "Headers-User-Agent-bat-0-1-0",
}, },
{ {
desc: "with Host label",
service: rancherData{ service: rancherData{
Name: "test-service", Name: "test-service",
Labels: map[string]string{ Labels: map[string]string{
types.LabelFrontendRule: "Host:foo.bar", types.LabelFrontendRule: "Host:foo.bar",
}, },
}, },
expected: "Host-foo-bar", expected: "Host-foo-bar",
}, },
{ {
desc: "with Path label",
service: rancherData{ service: rancherData{
Name: "test-service", Name: "test-service",
Labels: map[string]string{ Labels: map[string]string{
types.LabelFrontendRule: "Path:/test", types.LabelFrontendRule: "Path:/test",
}, },
}, },
expected: "Path-test", expected: "Path-test",
}, },
{ {
desc: "with PathPrefix label",
service: rancherData{ service: rancherData{
Name: "test-service", Name: "test-service",
Labels: map[string]string{ Labels: map[string]string{
types.LabelFrontendRule: "PathPrefix:/test2", types.LabelFrontendRule: "PathPrefix:/test2",
}, },
}, },
expected: "PathPrefix-test2", expected: "PathPrefix-test2",
}, },
} }
for _, e := range services { for _, test := range testCases {
actual := provider.getFrontendName(e.service) test := test
if actual != e.expected { t.Run(test.desc, func(t *testing.T) {
t.Fatalf("expected %q, got %q", e.expected, actual) t.Parallel()
}
actual := provider.getFrontendName(test.service)
assert.Equal(t, test.expected, actual)
})
} }
} }
func TestRancherGetFrontendRule(t *testing.T) { func TestProviderGetFrontendRule(t *testing.T) {
provider := &Provider{ provider := &Provider{Domain: "rancher.localhost"}
Domain: "rancher.localhost",
}
services := []struct { testCases := []struct {
desc string
service rancherData service rancherData
expected string expected string
}{ }{
{ {
desc: "host",
service: rancherData{ service: rancherData{
Name: "foo", Name: "foo",
}, },
expected: "Host:foo.rancher.localhost", expected: "Host:foo.rancher.localhost",
}, },
{ {
desc: "host with /",
service: rancherData{ service: rancherData{
Name: "foo/bar", Name: "foo/bar",
}, },
expected: "Host:foo.bar.rancher.localhost", expected: "Host:foo.bar.rancher.localhost",
}, },
{ {
desc: "with Host label",
service: rancherData{ service: rancherData{
Name: "test-service", Name: "test-service",
Labels: map[string]string{ Labels: map[string]string{
types.LabelFrontendRule: "Host:foo.bar.com", types.LabelFrontendRule: "Host:foo.bar.com",
}, },
}, },
expected: "Host:foo.bar.com", expected: "Host:foo.bar.com",
}, },
{ {
desc: "with Path label",
service: rancherData{ service: rancherData{
Name: "test-service", Name: "test-service",
Labels: map[string]string{ Labels: map[string]string{
types.LabelFrontendRule: "Path:/test", types.LabelFrontendRule: "Path:/test",
}, },
}, },
expected: "Path:/test", expected: "Path:/test",
}, },
{ {
desc: "with PathPrefix label",
service: rancherData{ service: rancherData{
Name: "test-service", Name: "test-service",
Labels: map[string]string{ Labels: map[string]string{
types.LabelFrontendRule: "PathPrefix:/test2", types.LabelFrontendRule: "PathPrefix:/test2",
}, },
}, },
expected: "PathPrefix:/test2", expected: "PathPrefix:/test2",
}, },
} }
for _, e := range services { for _, test := range testCases {
actual := provider.getFrontendRule(e.service) test := test
if actual != e.expected { t.Run(test.desc, func(t *testing.T) {
t.Fatalf("expected %q, got %q", e.expected, actual) t.Parallel()
}
actual := provider.getFrontendRule(test.service)
assert.Equal(t, test.expected, actual)
})
} }
} }
func TestRancherGetBackend(t *testing.T) { func TestProviderGetBackend(t *testing.T) {
provider := &Provider{ provider := &Provider{Domain: "rancher.localhost"}
Domain: "rancher.localhost",
}
services := []struct { testCases := []struct {
desc string
service rancherData service rancherData
expected string expected string
}{ }{
{ {
desc: "without label",
service: rancherData{ service: rancherData{
Name: "test-service", Name: "test-service",
}, },
expected: "test-service", expected: "test-service",
}, },
{ {
desc: "with label",
service: rancherData{ service: rancherData{
Name: "test-service", Name: "test-service",
Labels: map[string]string{ Labels: map[string]string{
@ -306,267 +328,308 @@ func TestRancherGetBackend(t *testing.T) {
}, },
} }
for _, e := range services { for _, test := range testCases {
actual := provider.getBackend(e.service) test := test
if actual != e.expected { t.Run(test.desc, func(t *testing.T) {
t.Fatalf("expected %q, got %q", e.expected, actual) t.Parallel()
}
actual := provider.getBackend(test.service)
assert.Equal(t, test.expected, actual)
})
} }
} }
func TestRancherGetWeight(t *testing.T) { func TestProviderGetWeight(t *testing.T) {
provider := &Provider{ provider := &Provider{Domain: "rancher.localhost"}
Domain: "rancher.localhost",
}
services := []struct { testCases := []struct {
desc string
service rancherData service rancherData
expected string expected string
}{ }{
{ {
desc: "without label",
service: rancherData{ service: rancherData{
Name: "test-service", Name: "test-service",
}, },
expected: "0", expected: "0",
}, },
{ {
desc: "with label",
service: rancherData{ service: rancherData{
Name: "test-service", Name: "test-service",
Labels: map[string]string{ Labels: map[string]string{
types.LabelWeight: "5", types.LabelWeight: "5",
}, },
}, },
expected: "5", expected: "5",
}, },
} }
for _, e := range services { for _, test := range testCases {
actual := provider.getWeight(e.service) test := test
if actual != e.expected { t.Run(test.desc, func(t *testing.T) {
t.Fatalf("expected %q, got %q", e.expected, actual) t.Parallel()
}
actual := provider.getWeight(test.service)
assert.Equal(t, test.expected, actual)
})
} }
} }
func TestRancherGetPort(t *testing.T) { func TestProviderGetPort(t *testing.T) {
provider := &Provider{ provider := &Provider{Domain: "rancher.localhost"}
Domain: "rancher.localhost",
}
services := []struct { testCases := []struct {
desc string
service rancherData service rancherData
expected string expected string
}{ }{
{ {
desc: "without label",
service: rancherData{ service: rancherData{
Name: "test-service", Name: "test-service",
}, },
expected: "", expected: "",
}, },
{ {
desc: "with label",
service: rancherData{ service: rancherData{
Name: "test-service", Name: "test-service",
Labels: map[string]string{ Labels: map[string]string{
types.LabelPort: "1337", types.LabelPort: "1337",
}, },
}, },
expected: "1337", expected: "1337",
}, },
} }
for _, e := range services { for _, test := range testCases {
actual := provider.getPort(e.service) test := test
if actual != e.expected { t.Run(test.desc, func(t *testing.T) {
t.Fatalf("expected %q, got %q", e.expected, actual) t.Parallel()
}
actual := provider.getPort(test.service)
assert.Equal(t, test.expected, actual)
})
} }
} }
func TestRancherGetDomain(t *testing.T) { func TestProviderGetDomain(t *testing.T) {
provider := &Provider{ provider := &Provider{Domain: "rancher.localhost"}
Domain: "rancher.localhost",
}
services := []struct { testCases := []struct {
desc string
service rancherData service rancherData
expected string expected string
}{ }{
{ {
desc: "without label",
service: rancherData{ service: rancherData{
Name: "test-service", Name: "test-service",
}, },
expected: "rancher.localhost", expected: "rancher.localhost",
}, },
{ {
desc: "with label",
service: rancherData{ service: rancherData{
Name: "test-service", Name: "test-service",
Labels: map[string]string{ Labels: map[string]string{
types.LabelDomain: "foo.bar", types.LabelDomain: "foo.bar",
}, },
}, },
expected: "foo.bar", expected: "foo.bar",
}, },
} }
for _, e := range services { for _, test := range testCases {
actual := provider.getDomain(e.service) test := test
if actual != e.expected { t.Run(test.desc, func(t *testing.T) {
t.Fatalf("expected %q, got %q", e.expected, actual) t.Parallel()
}
actual := provider.getDomain(test.service)
assert.Equal(t, test.expected, actual)
})
} }
} }
func TestRancherGetProtocol(t *testing.T) { func TestProviderGetProtocol(t *testing.T) {
provider := &Provider{ provider := &Provider{Domain: "rancher.localhost"}
Domain: "rancher.localhost",
}
services := []struct { testCases := []struct {
desc string
service rancherData service rancherData
expected string expected string
}{ }{
{ {
desc: "without label",
service: rancherData{ service: rancherData{
Name: "test-service", Name: "test-service",
}, },
expected: "http", expected: "http",
}, },
{ {
desc: "with label",
service: rancherData{ service: rancherData{
Name: "test-service", Name: "test-service",
Labels: map[string]string{ Labels: map[string]string{
types.LabelProtocol: "https", types.LabelProtocol: "https",
}, },
}, },
expected: "https", expected: "https",
}, },
} }
for _, e := range services { for _, test := range testCases {
actual := provider.getProtocol(e.service) test := test
if actual != e.expected { t.Run(test.desc, func(t *testing.T) {
t.Fatalf("expected %q, got %q", e.expected, actual) t.Parallel()
}
actual := provider.getProtocol(test.service)
assert.Equal(t, test.expected, actual)
})
} }
} }
func TestRancherGetPassHostHeader(t *testing.T) { func TestProviderGetPassHostHeader(t *testing.T) {
provider := &Provider{ provider := &Provider{Domain: "rancher.localhost"}
Domain: "rancher.localhost",
}
services := []struct { testCases := []struct {
desc string
service rancherData service rancherData
expected string expected string
}{ }{
{ {
desc: "without label",
service: rancherData{ service: rancherData{
Name: "test-service", Name: "test-service",
}, },
expected: "true", expected: "true",
}, },
{ {
desc: "with label",
service: rancherData{ service: rancherData{
Name: "test-service", Name: "test-service",
Labels: map[string]string{ Labels: map[string]string{
types.LabelFrontendPassHostHeader: "false", types.LabelFrontendPassHostHeader: "false",
}, },
}, },
expected: "false", expected: "false",
}, },
} }
for _, e := range services { for _, test := range testCases {
actual := provider.getPassHostHeader(e.service) test := test
if actual != e.expected { t.Run(test.desc, func(t *testing.T) {
t.Fatalf("expected %q, got %q", e.expected, actual) t.Parallel()
}
actual := provider.getPassHostHeader(test.service)
assert.Equal(t, test.expected, actual)
})
} }
} }
func TestRancherGetRedirect(t *testing.T) { func TestProviderGetRedirect(t *testing.T) {
provider := &Provider{ provider := &Provider{Domain: "rancher.localhost"}
Domain: "rancher.localhost",
}
testCases := []struct { testCases := []struct {
desc string
service rancherData service rancherData
expected string expected string
}{ }{
{ {
desc: "without label",
service: rancherData{
Name: "test-service",
},
expected: "",
},
{
desc: "with label",
service: rancherData{ service: rancherData{
Name: "test-service", Name: "test-service",
Labels: map[string]string{ Labels: map[string]string{
types.LabelFrontendRedirect: "https", types.LabelFrontendRedirect: "https",
}, },
}, },
expected: "https", expected: "https",
}, },
} }
for _, test := range testCases { for _, test := range testCases {
actual := provider.getRedirect(test.service) test := test
if actual != test.expected { t.Run(test.desc, func(t *testing.T) {
t.Fatalf("got %q, expected %q", actual, test.expected) t.Parallel()
}
actual := provider.getRedirect(test.service)
assert.Equal(t, test.expected, actual)
})
} }
} }
func TestRancherGetLabel(t *testing.T) { func TestProviderGetLabel(t *testing.T) {
services := []struct { testCases := []struct {
desc string
service rancherData service rancherData
expected string expected string
}{ }{
{ {
desc: "without label",
service: rancherData{ service: rancherData{
Name: "test-service", Name: "test-service",
}, },
expected: "label not found", expected: "label not found",
}, },
{ {
desc: "with label",
service: rancherData{ service: rancherData{
Name: "test-service", Name: "test-service",
Labels: map[string]string{ Labels: map[string]string{
"foo": "bar", "foo": "bar",
}, },
}, },
expected: "", expected: "",
}, },
} }
for _, e := range services { for _, test := range testCases {
label, err := getServiceLabel(e.service, "foo") test := test
if e.expected != "" { t.Run("", func(t *testing.T) {
if err == nil || !strings.Contains(err.Error(), e.expected) { t.Parallel()
t.Fatalf("expected an error with %q, got %v", e.expected, err)
label, err := getServiceLabel(test.service, "foo")
if test.expected != "" {
if err == nil || !strings.Contains(err.Error(), test.expected) {
t.Fatalf("expected an error with %q, got %v", test.expected, err)
}
} else {
assert.Equal(t, "bar", label)
} }
} else { })
if label != "bar" {
t.Fatalf("expected label 'bar', got %s", label)
}
}
} }
} }
func TestRancherLoadRancherConfig(t *testing.T) { func TestProviderLoadRancherConfig(t *testing.T) {
cases := []struct { provider := &Provider{
Domain: "rancher.localhost",
ExposedByDefault: true,
}
testCases := []struct {
desc string
services []rancherData services []rancherData
expectedFrontends map[string]*types.Frontend expectedFrontends map[string]*types.Frontend
expectedBackends map[string]*types.Backend expectedBackends map[string]*types.Backend
}{ }{
{ {
desc: "without services",
services: []rancherData{}, services: []rancherData{},
expectedFrontends: map[string]*types.Frontend{}, expectedFrontends: map[string]*types.Frontend{},
expectedBackends: map[string]*types.Backend{}, expectedBackends: map[string]*types.Backend{},
}, },
{ {
desc: "with services",
services: []rancherData{ services: []rancherData{
{ {
Name: "test/service", Name: "test/service",
@ -587,7 +650,6 @@ func TestRancherLoadRancherConfig(t *testing.T) {
BasicAuth: []string{"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/", "test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"}, BasicAuth: []string{"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/", "test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"},
Priority: 0, Priority: 0,
Redirect: "https", Redirect: "https",
Routes: map[string]types.Route{ Routes: map[string]types.Route{
"route-frontend-Host-test-service-rancher-localhost": { "route-frontend-Host-test-service-rancher-localhost": {
Rule: "Host:test.service.rancher.localhost", Rule: "Host:test.service.rancher.localhost",
@ -609,31 +671,22 @@ func TestRancherLoadRancherConfig(t *testing.T) {
}, },
} }
provider := &Provider{ for _, test := range testCases {
Domain: "rancher.localhost", test := test
ExposedByDefault: true,
}
for _, c := range cases { t.Run(test.desc, func(t *testing.T) {
var rancherDataList []rancherData t.Parallel()
rancherDataList = append(rancherDataList, c.services...)
actualConfig := provider.loadRancherConfig(rancherDataList) actualConfig := provider.loadRancherConfig(test.services)
// Compare backends assert.EqualValues(t, test.expectedBackends, actualConfig.Backends)
if !reflect.DeepEqual(actualConfig.Backends, c.expectedBackends) { assert.EqualValues(t, test.expectedFrontends, actualConfig.Frontends)
t.Fatalf("expected %#v, got %#v", c.expectedBackends, actualConfig.Backends) })
}
if !reflect.DeepEqual(actualConfig.Frontends, c.expectedFrontends) {
t.Fatalf("expected %#v, got %#v", c.expectedFrontends, actualConfig.Frontends)
}
} }
} }
func TestRancherHasStickinessLabel(t *testing.T) { func TestProviderHasStickinessLabel(t *testing.T) {
provider := &Provider{ provider := &Provider{Domain: "rancher.localhost"}
Domain: "rancher.localhost",
}
testCases := []struct { testCases := []struct {
desc string desc string