2017-04-17 10:50:02 +00:00
|
|
|
package rancher
|
2017-02-05 23:58:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2017-04-29 19:37:54 +00:00
|
|
|
|
|
|
|
"github.com/containous/traefik/types"
|
2017-10-12 15:50:03 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-02-05 23:58:05 +00:00
|
|
|
)
|
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
func TestProviderServiceFilter(t *testing.T) {
|
2017-04-29 19:37:54 +00:00
|
|
|
provider := &Provider{
|
|
|
|
Domain: "rancher.localhost",
|
|
|
|
EnableServiceHealthFilter: true,
|
|
|
|
}
|
|
|
|
|
2017-05-02 14:51:02 +00:00
|
|
|
constraint, _ := types.NewConstraint("tag==ch*se")
|
|
|
|
provider.Constraints = types.Constraints{constraint}
|
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
2017-04-29 19:37:54 +00:00
|
|
|
service rancherData
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "missing Port labels, don't respect constraint",
|
2017-04-29 19:37:54 +00:00
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelEnable: "true",
|
2017-04-29 19:37:54 +00:00
|
|
|
},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "don't respect constraint",
|
2017-04-29 19:37:54 +00:00
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelPort: "80",
|
|
|
|
types.LabelEnable: "false",
|
2017-04-29 19:37:54 +00:00
|
|
|
},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "unhealthy",
|
2017-04-29 19:37:54 +00:00
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
2017-11-20 10:40:04 +00:00
|
|
|
types.LabelTags: "cheese",
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelPort: "80",
|
|
|
|
types.LabelEnable: "true",
|
2017-04-29 19:37:54 +00:00
|
|
|
},
|
|
|
|
Health: "unhealthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "inactive",
|
2017-04-29 19:37:54 +00:00
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelTags: "not-cheesy",
|
|
|
|
types.LabelPort: "80",
|
|
|
|
types.LabelEnable: "true",
|
2017-04-29 19:37:54 +00:00
|
|
|
},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "inactive",
|
|
|
|
},
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "healthy & active, tag: cheese",
|
2017-04-29 19:37:54 +00:00
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelTags: "cheese",
|
|
|
|
types.LabelPort: "80",
|
|
|
|
types.LabelEnable: "true",
|
2017-04-29 19:37:54 +00:00
|
|
|
},
|
|
|
|
Health: "healthy",
|
|
|
|
State: "active",
|
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "healthy & active, tag: chose",
|
2017-04-29 19:37:54 +00:00
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
2017-11-20 10:40:04 +00:00
|
|
|
types.LabelTags: "chose",
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelPort: "80",
|
|
|
|
types.LabelEnable: "true",
|
2017-04-29 19:37:54 +00:00
|
|
|
},
|
|
|
|
Health: "healthy",
|
2017-11-20 10:40:04 +00:00
|
|
|
State: "active",
|
2017-04-29 19:37:54 +00:00
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
2017-05-02 14:51:02 +00:00
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "healthy & upgraded",
|
2017-05-02 14:51:02 +00:00
|
|
|
service: rancherData{
|
|
|
|
Labels: map[string]string{
|
2017-11-20 10:40:04 +00:00
|
|
|
types.LabelTags: "cheeeeese",
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelPort: "80",
|
|
|
|
types.LabelEnable: "true",
|
2017-05-02 14:51:02 +00:00
|
|
|
},
|
|
|
|
Health: "healthy",
|
2017-11-20 10:40:04 +00:00
|
|
|
State: "upgraded",
|
2017-05-02 14:51:02 +00:00
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
2017-04-29 19:37:54 +00:00
|
|
|
}
|
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
actual := provider.serviceFilter(test.service)
|
|
|
|
assert.Equal(t, test.expected, actual)
|
|
|
|
})
|
2017-04-29 19:37:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
func TestContainerFilter(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2017-05-08 01:20:38 +00:00
|
|
|
name string
|
|
|
|
healthState string
|
|
|
|
state string
|
|
|
|
expected bool
|
2017-04-29 19:37:54 +00:00
|
|
|
}{
|
|
|
|
{
|
2017-05-08 01:20:38 +00:00
|
|
|
healthState: "unhealthy",
|
|
|
|
state: "running",
|
|
|
|
expected: false,
|
2017-04-29 19:37:54 +00:00
|
|
|
},
|
|
|
|
{
|
2017-05-08 01:20:38 +00:00
|
|
|
healthState: "healthy",
|
|
|
|
state: "stopped",
|
|
|
|
expected: false,
|
2017-04-29 19:37:54 +00:00
|
|
|
},
|
|
|
|
{
|
2017-05-08 01:20:38 +00:00
|
|
|
state: "stopped",
|
2017-04-29 19:37:54 +00:00
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
2017-05-08 01:20:38 +00:00
|
|
|
healthState: "healthy",
|
|
|
|
state: "running",
|
|
|
|
expected: true,
|
2017-04-29 19:37:54 +00:00
|
|
|
},
|
|
|
|
{
|
2017-05-08 01:20:38 +00:00
|
|
|
healthState: "updating-healthy",
|
|
|
|
state: "updating-running",
|
|
|
|
expected: true,
|
2017-04-29 19:37:54 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.healthState+" "+test.state, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
actual := containerFilter(test.name, test.healthState, test.state)
|
|
|
|
assert.Equal(t, test.expected, actual)
|
|
|
|
})
|
2017-04-29 19:37:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
func TestProviderGetFrontendName(t *testing.T) {
|
|
|
|
provider := &Provider{Domain: "rancher.localhost"}
|
2017-02-05 23:58:05 +00:00
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
2017-02-05 23:58:05 +00:00
|
|
|
service rancherData
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "default",
|
2017-02-05 23:58:05 +00:00
|
|
|
service: rancherData{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
expected: "Host-foo-rancher-localhost",
|
|
|
|
},
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "with Headers label",
|
2017-02-05 23:58:05 +00:00
|
|
|
service: rancherData{
|
|
|
|
Name: "test-service",
|
|
|
|
Labels: map[string]string{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelFrontendRule: "Headers:User-Agent,bat/0.1.0",
|
2017-02-05 23:58:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "Headers-User-Agent-bat-0-1-0",
|
|
|
|
},
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "with Host label",
|
2017-02-05 23:58:05 +00:00
|
|
|
service: rancherData{
|
|
|
|
Name: "test-service",
|
|
|
|
Labels: map[string]string{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelFrontendRule: "Host:foo.bar",
|
2017-02-05 23:58:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "Host-foo-bar",
|
|
|
|
},
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "with Path label",
|
2017-02-05 23:58:05 +00:00
|
|
|
service: rancherData{
|
|
|
|
Name: "test-service",
|
|
|
|
Labels: map[string]string{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelFrontendRule: "Path:/test",
|
2017-02-05 23:58:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "Path-test",
|
|
|
|
},
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "with PathPrefix label",
|
2017-02-05 23:58:05 +00:00
|
|
|
service: rancherData{
|
|
|
|
Name: "test-service",
|
|
|
|
Labels: map[string]string{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelFrontendRule: "PathPrefix:/test2",
|
2017-02-05 23:58:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "PathPrefix-test2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
actual := provider.getFrontendName(test.service)
|
|
|
|
assert.Equal(t, test.expected, actual)
|
|
|
|
})
|
2017-02-05 23:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
func TestProviderGetFrontendRule(t *testing.T) {
|
|
|
|
provider := &Provider{Domain: "rancher.localhost"}
|
2017-02-05 23:58:05 +00:00
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
2017-02-05 23:58:05 +00:00
|
|
|
service rancherData
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "host",
|
2017-02-05 23:58:05 +00:00
|
|
|
service: rancherData{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
expected: "Host:foo.rancher.localhost",
|
|
|
|
},
|
2017-02-20 19:41:28 +00:00
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "host with /",
|
2017-02-20 19:41:28 +00:00
|
|
|
service: rancherData{
|
|
|
|
Name: "foo/bar",
|
|
|
|
},
|
|
|
|
expected: "Host:foo.bar.rancher.localhost",
|
|
|
|
},
|
2017-02-05 23:58:05 +00:00
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "with Host label",
|
2017-02-05 23:58:05 +00:00
|
|
|
service: rancherData{
|
|
|
|
Name: "test-service",
|
|
|
|
Labels: map[string]string{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelFrontendRule: "Host:foo.bar.com",
|
2017-02-05 23:58:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "Host:foo.bar.com",
|
|
|
|
},
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "with Path label",
|
2017-02-05 23:58:05 +00:00
|
|
|
service: rancherData{
|
|
|
|
Name: "test-service",
|
|
|
|
Labels: map[string]string{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelFrontendRule: "Path:/test",
|
2017-02-05 23:58:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "Path:/test",
|
|
|
|
},
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "with PathPrefix label",
|
2017-02-05 23:58:05 +00:00
|
|
|
service: rancherData{
|
|
|
|
Name: "test-service",
|
|
|
|
Labels: map[string]string{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelFrontendRule: "PathPrefix:/test2",
|
2017-02-05 23:58:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "PathPrefix:/test2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
actual := provider.getFrontendRule(test.service)
|
|
|
|
assert.Equal(t, test.expected, actual)
|
|
|
|
})
|
2017-02-05 23:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
func TestProviderGetBackend(t *testing.T) {
|
|
|
|
provider := &Provider{Domain: "rancher.localhost"}
|
2017-02-05 23:58:05 +00:00
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
2017-02-05 23:58:05 +00:00
|
|
|
service rancherData
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "without label",
|
2017-02-05 23:58:05 +00:00
|
|
|
service: rancherData{
|
|
|
|
Name: "test-service",
|
|
|
|
},
|
|
|
|
expected: "test-service",
|
|
|
|
},
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "with label",
|
2017-02-05 23:58:05 +00:00
|
|
|
service: rancherData{
|
|
|
|
Name: "test-service",
|
|
|
|
Labels: map[string]string{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelBackend: "foobar",
|
2017-02-05 23:58:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
expected: "foobar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
actual := provider.getBackend(test.service)
|
|
|
|
assert.Equal(t, test.expected, actual)
|
|
|
|
})
|
2017-02-05 23:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
func TestProviderGetWeight(t *testing.T) {
|
|
|
|
provider := &Provider{Domain: "rancher.localhost"}
|
2017-02-05 23:58:05 +00:00
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
2017-02-05 23:58:05 +00:00
|
|
|
service rancherData
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "without label",
|
2017-02-05 23:58:05 +00:00
|
|
|
service: rancherData{
|
|
|
|
Name: "test-service",
|
|
|
|
},
|
|
|
|
expected: "0",
|
|
|
|
},
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "with label",
|
2017-02-05 23:58:05 +00:00
|
|
|
service: rancherData{
|
|
|
|
Name: "test-service",
|
|
|
|
Labels: map[string]string{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelWeight: "5",
|
2017-02-05 23:58:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "5",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
actual := provider.getWeight(test.service)
|
|
|
|
assert.Equal(t, test.expected, actual)
|
|
|
|
})
|
2017-02-05 23:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
func TestProviderGetPort(t *testing.T) {
|
|
|
|
provider := &Provider{Domain: "rancher.localhost"}
|
2017-02-05 23:58:05 +00:00
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
2017-02-05 23:58:05 +00:00
|
|
|
service rancherData
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "without label",
|
2017-02-05 23:58:05 +00:00
|
|
|
service: rancherData{
|
|
|
|
Name: "test-service",
|
|
|
|
},
|
|
|
|
expected: "",
|
|
|
|
},
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "with label",
|
2017-02-05 23:58:05 +00:00
|
|
|
service: rancherData{
|
|
|
|
Name: "test-service",
|
|
|
|
Labels: map[string]string{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelPort: "1337",
|
2017-02-05 23:58:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "1337",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
actual := provider.getPort(test.service)
|
|
|
|
assert.Equal(t, test.expected, actual)
|
|
|
|
})
|
2017-02-05 23:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
func TestProviderGetDomain(t *testing.T) {
|
|
|
|
provider := &Provider{Domain: "rancher.localhost"}
|
2017-02-05 23:58:05 +00:00
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
2017-02-05 23:58:05 +00:00
|
|
|
service rancherData
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "without label",
|
2017-02-05 23:58:05 +00:00
|
|
|
service: rancherData{
|
|
|
|
Name: "test-service",
|
|
|
|
},
|
|
|
|
expected: "rancher.localhost",
|
|
|
|
},
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "with label",
|
2017-02-05 23:58:05 +00:00
|
|
|
service: rancherData{
|
|
|
|
Name: "test-service",
|
|
|
|
Labels: map[string]string{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelDomain: "foo.bar",
|
2017-02-05 23:58:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "foo.bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
actual := provider.getDomain(test.service)
|
|
|
|
assert.Equal(t, test.expected, actual)
|
|
|
|
})
|
2017-02-05 23:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
func TestProviderGetProtocol(t *testing.T) {
|
|
|
|
provider := &Provider{Domain: "rancher.localhost"}
|
2017-02-05 23:58:05 +00:00
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
2017-02-05 23:58:05 +00:00
|
|
|
service rancherData
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "without label",
|
2017-02-05 23:58:05 +00:00
|
|
|
service: rancherData{
|
|
|
|
Name: "test-service",
|
|
|
|
},
|
|
|
|
expected: "http",
|
|
|
|
},
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "with label",
|
2017-02-05 23:58:05 +00:00
|
|
|
service: rancherData{
|
|
|
|
Name: "test-service",
|
|
|
|
Labels: map[string]string{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelProtocol: "https",
|
2017-02-05 23:58:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "https",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
actual := provider.getProtocol(test.service)
|
|
|
|
assert.Equal(t, test.expected, actual)
|
|
|
|
})
|
2017-02-05 23:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
func TestProviderGetPassHostHeader(t *testing.T) {
|
|
|
|
provider := &Provider{Domain: "rancher.localhost"}
|
2017-02-05 23:58:05 +00:00
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
2017-02-05 23:58:05 +00:00
|
|
|
service rancherData
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "without label",
|
2017-02-05 23:58:05 +00:00
|
|
|
service: rancherData{
|
|
|
|
Name: "test-service",
|
|
|
|
},
|
|
|
|
expected: "true",
|
|
|
|
},
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "with label",
|
2017-02-05 23:58:05 +00:00
|
|
|
service: rancherData{
|
|
|
|
Name: "test-service",
|
|
|
|
Labels: map[string]string{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelFrontendPassHostHeader: "false",
|
2017-02-05 23:58:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "false",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
actual := provider.getPassHostHeader(test.service)
|
|
|
|
assert.Equal(t, test.expected, actual)
|
|
|
|
})
|
2017-02-05 23:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
func TestProviderGetRedirect(t *testing.T) {
|
|
|
|
provider := &Provider{Domain: "rancher.localhost"}
|
2017-11-18 12:50:03 +00:00
|
|
|
|
|
|
|
testCases := []struct {
|
2017-11-20 10:40:04 +00:00
|
|
|
desc string
|
2017-11-18 12:50:03 +00:00
|
|
|
service rancherData
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "without label",
|
|
|
|
service: rancherData{
|
|
|
|
Name: "test-service",
|
|
|
|
},
|
|
|
|
expected: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "with label",
|
2017-11-18 12:50:03 +00:00
|
|
|
service: rancherData{
|
|
|
|
Name: "test-service",
|
|
|
|
Labels: map[string]string{
|
|
|
|
types.LabelFrontendRedirect: "https",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "https",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
2017-11-20 10:40:04 +00:00
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
actual := provider.getRedirect(test.service)
|
|
|
|
assert.Equal(t, test.expected, actual)
|
|
|
|
})
|
2017-11-18 12:50:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
func TestProviderGetLabel(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
2017-02-05 23:58:05 +00:00
|
|
|
service rancherData
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "without label",
|
2017-02-05 23:58:05 +00:00
|
|
|
service: rancherData{
|
|
|
|
Name: "test-service",
|
|
|
|
},
|
2017-05-08 01:20:38 +00:00
|
|
|
expected: "label not found",
|
2017-02-05 23:58:05 +00:00
|
|
|
},
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "with label",
|
2017-02-05 23:58:05 +00:00
|
|
|
service: rancherData{
|
|
|
|
Name: "test-service",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
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)
|
2017-02-05 23:58:05 +00:00
|
|
|
}
|
2017-11-20 10:40:04 +00:00
|
|
|
})
|
2017-02-05 23:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
func TestProviderLoadRancherConfig(t *testing.T) {
|
|
|
|
provider := &Provider{
|
|
|
|
Domain: "rancher.localhost",
|
|
|
|
ExposedByDefault: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
2017-02-05 23:58:05 +00:00
|
|
|
services []rancherData
|
|
|
|
expectedFrontends map[string]*types.Frontend
|
|
|
|
expectedBackends map[string]*types.Backend
|
|
|
|
}{
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "without services",
|
2017-02-05 23:58:05 +00:00
|
|
|
services: []rancherData{},
|
|
|
|
expectedFrontends: map[string]*types.Frontend{},
|
|
|
|
expectedBackends: map[string]*types.Backend{},
|
|
|
|
},
|
|
|
|
{
|
2017-11-20 10:40:04 +00:00
|
|
|
desc: "with services",
|
2017-02-05 23:58:05 +00:00
|
|
|
services: []rancherData{
|
|
|
|
{
|
2017-02-20 19:41:28 +00:00
|
|
|
Name: "test/service",
|
2017-02-05 23:58:05 +00:00
|
|
|
Labels: map[string]string{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelPort: "80",
|
|
|
|
types.LabelFrontendAuthBasic: "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
|
2017-11-18 12:50:03 +00:00
|
|
|
types.LabelFrontendRedirect: "https",
|
2017-02-05 23:58:05 +00:00
|
|
|
},
|
|
|
|
Health: "healthy",
|
|
|
|
Containers: []string{"127.0.0.1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedFrontends: map[string]*types.Frontend{
|
|
|
|
"frontend-Host-test-service-rancher-localhost": {
|
|
|
|
Backend: "backend-test-service",
|
|
|
|
PassHostHeader: true,
|
|
|
|
EntryPoints: []string{},
|
2017-04-19 09:14:05 +00:00
|
|
|
BasicAuth: []string{"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/", "test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"},
|
2017-02-05 23:58:05 +00:00
|
|
|
Priority: 0,
|
2017-11-18 12:50:03 +00:00
|
|
|
Redirect: "https",
|
2017-02-05 23:58:05 +00:00
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"route-frontend-Host-test-service-rancher-localhost": {
|
2017-02-20 19:41:28 +00:00
|
|
|
Rule: "Host:test.service.rancher.localhost",
|
2017-02-05 23:58:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedBackends: map[string]*types.Backend{
|
|
|
|
"backend-test-service": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"server-0": {
|
|
|
|
URL: "http://127.0.0.1:80",
|
|
|
|
Weight: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CircuitBreaker: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
2017-02-05 23:58:05 +00:00
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2017-02-05 23:58:05 +00:00
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
actualConfig := provider.loadRancherConfig(test.services)
|
2017-02-05 23:58:05 +00:00
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
assert.EqualValues(t, test.expectedBackends, actualConfig.Backends)
|
|
|
|
assert.EqualValues(t, test.expectedFrontends, actualConfig.Frontends)
|
|
|
|
})
|
2017-02-05 23:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-12 15:50:03 +00:00
|
|
|
|
2017-11-20 10:40:04 +00:00
|
|
|
func TestProviderHasStickinessLabel(t *testing.T) {
|
|
|
|
provider := &Provider{Domain: "rancher.localhost"}
|
2017-10-12 15:50:03 +00:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
service rancherData
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "no labels",
|
|
|
|
service: rancherData{
|
|
|
|
Name: "test-service",
|
|
|
|
},
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "stickiness=true",
|
|
|
|
service: rancherData{
|
|
|
|
Name: "test-service",
|
|
|
|
Labels: map[string]string{
|
|
|
|
types.LabelBackendLoadbalancerStickiness: "true",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
2017-10-16 15:38:03 +00:00
|
|
|
desc: "stickiness=true",
|
2017-10-12 15:50:03 +00:00
|
|
|
service: rancherData{
|
|
|
|
Name: "test-service",
|
|
|
|
Labels: map[string]string{
|
|
|
|
types.LabelBackendLoadbalancerStickiness: "false",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
actual := provider.hasStickinessLabel(test.service)
|
|
|
|
assert.Equal(t, actual, test.expected)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|