traefik/pkg/config/runtime/runtime_tcp_test.go

219 lines
5.9 KiB
Go
Raw Permalink Normal View History

2019-08-14 15:28:04 +00:00
package runtime
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
2023-02-03 14:24:05 +00:00
"github.com/traefik/traefik/v3/pkg/config/dynamic"
2019-08-14 15:28:04 +00:00
)
func TestGetTCPRoutersByEntryPoints(t *testing.T) {
testCases := []struct {
desc string
conf dynamic.Configuration
entryPoints []string
expected map[string]map[string]*TCPRouterInfo
}{
{
desc: "Empty Configuration without entrypoint",
conf: dynamic.Configuration{},
entryPoints: []string{""},
expected: map[string]map[string]*TCPRouterInfo{},
},
{
desc: "Empty Configuration with unknown entrypoints",
conf: dynamic.Configuration{},
entryPoints: []string{"foo"},
expected: map[string]map[string]*TCPRouterInfo{},
},
{
desc: "Valid configuration with an unknown entrypoint",
conf: dynamic.Configuration{
HTTP: &dynamic.HTTPConfiguration{
Routers: map[string]*dynamic.Router{
"foo": {
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "Host(`bar.foo`)",
},
},
},
TCP: &dynamic.TCPConfiguration{
Routers: map[string]*dynamic.TCPRouter{
"foo": {
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "HostSNI(`bar.foo`)",
},
},
},
},
entryPoints: []string{"foo"},
expected: map[string]map[string]*TCPRouterInfo{},
},
{
desc: "Valid configuration with a known entrypoint",
conf: dynamic.Configuration{
HTTP: &dynamic.HTTPConfiguration{
Routers: map[string]*dynamic.Router{
"foo": {
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "Host(`bar.foo`)",
},
"bar": {
EntryPoints: []string{"webs"},
Service: "bar-service@myprovider",
Rule: "Host(`foo.bar`)",
},
"foobar": {
EntryPoints: []string{"web", "webs"},
Service: "foobar-service@myprovider",
Rule: "Host(`bar.foobar`)",
},
},
},
TCP: &dynamic.TCPConfiguration{
Routers: map[string]*dynamic.TCPRouter{
"foo": {
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "HostSNI(`bar.foo`)",
},
"bar": {
EntryPoints: []string{"webs"},
Service: "bar-service@myprovider",
Rule: "HostSNI(`foo.bar`)",
},
"foobar": {
EntryPoints: []string{"web", "webs"},
Service: "foobar-service@myprovider",
Rule: "HostSNI(`bar.foobar`)",
},
},
},
},
entryPoints: []string{"web"},
expected: map[string]map[string]*TCPRouterInfo{
"web": {
"foo": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "HostSNI(`bar.foo`)",
},
Status: "enabled",
2019-09-02 09:38:04 +00:00
Using: []string{"web"},
2019-08-14 15:28:04 +00:00
},
"foobar": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web", "webs"},
Service: "foobar-service@myprovider",
Rule: "HostSNI(`bar.foobar`)",
},
2019-09-02 09:38:04 +00:00
Status: "warning",
Err: []string{`entryPoint "webs" doesn't exist`},
Using: []string{"web"},
2019-08-14 15:28:04 +00:00
},
},
},
},
{
desc: "Valid configuration with multiple known entrypoints",
conf: dynamic.Configuration{
HTTP: &dynamic.HTTPConfiguration{
Routers: map[string]*dynamic.Router{
"foo": {
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "Host(`bar.foo`)",
},
"bar": {
EntryPoints: []string{"webs"},
Service: "bar-service@myprovider",
Rule: "Host(`foo.bar`)",
},
"foobar": {
EntryPoints: []string{"web", "webs"},
Service: "foobar-service@myprovider",
Rule: "Host(`bar.foobar`)",
},
},
},
TCP: &dynamic.TCPConfiguration{
Routers: map[string]*dynamic.TCPRouter{
"foo": {
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "HostSNI(`bar.foo`)",
},
"bar": {
EntryPoints: []string{"webs"},
Service: "bar-service@myprovider",
Rule: "HostSNI(`foo.bar`)",
},
"foobar": {
EntryPoints: []string{"web", "webs"},
Service: "foobar-service@myprovider",
Rule: "HostSNI(`bar.foobar`)",
},
},
},
},
entryPoints: []string{"web", "webs"},
expected: map[string]map[string]*TCPRouterInfo{
"web": {
"foo": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "HostSNI(`bar.foo`)",
},
Status: "enabled",
2019-09-02 09:38:04 +00:00
Using: []string{"web"},
2019-08-14 15:28:04 +00:00
},
"foobar": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web", "webs"},
Service: "foobar-service@myprovider",
Rule: "HostSNI(`bar.foobar`)",
},
Status: "enabled",
2019-09-02 09:38:04 +00:00
Using: []string{"web", "webs"},
2019-08-14 15:28:04 +00:00
},
},
"webs": {
"bar": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"webs"},
Service: "bar-service@myprovider",
Rule: "HostSNI(`foo.bar`)",
},
Status: "enabled",
2019-09-02 09:38:04 +00:00
Using: []string{"webs"},
2019-08-14 15:28:04 +00:00
},
"foobar": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web", "webs"},
Service: "foobar-service@myprovider",
Rule: "HostSNI(`bar.foobar`)",
},
Status: "enabled",
2019-09-02 09:38:04 +00:00
Using: []string{"web", "webs"},
2019-08-14 15:28:04 +00:00
},
},
},
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
runtimeConfig := NewConfig(test.conf)
actual := runtimeConfig.GetTCPRoutersByEntryPoints(context.Background(), test.entryPoints)
assert.Equal(t, test.expected, actual)
})
}
}