2019-05-16 08:58:06 +00:00
|
|
|
package tcp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2019-08-03 01:58:23 +00:00
|
|
|
"github.com/containous/traefik/v2/pkg/config/dynamic"
|
|
|
|
"github.com/containous/traefik/v2/pkg/config/runtime"
|
|
|
|
"github.com/containous/traefik/v2/pkg/server/service/tcp"
|
|
|
|
"github.com/containous/traefik/v2/pkg/tls"
|
2019-05-16 08:58:06 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRuntimeConfiguration(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
2019-07-15 15:04:04 +00:00
|
|
|
serviceConfig map[string]*runtime.TCPServiceInfo
|
|
|
|
routerConfig map[string]*runtime.TCPRouterInfo
|
2019-05-16 08:58:06 +00:00
|
|
|
expectedError int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "No error",
|
2019-07-15 15:04:04 +00:00
|
|
|
serviceConfig: map[string]*runtime.TCPServiceInfo{
|
2019-05-16 08:58:06 +00:00
|
|
|
"foo-service": {
|
2019-07-10 07:26:04 +00:00
|
|
|
TCPService: &dynamic.TCPService{
|
2019-09-13 18:00:06 +00:00
|
|
|
LoadBalancer: &dynamic.TCPServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.TCPServer{
|
2019-05-16 08:58:06 +00:00
|
|
|
{
|
|
|
|
Port: "8085",
|
|
|
|
Address: "127.0.0.1:8085",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Address: "127.0.0.1:8086",
|
|
|
|
Port: "8086",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-15 15:04:04 +00:00
|
|
|
routerConfig: map[string]*runtime.TCPRouterInfo{
|
2019-05-16 08:58:06 +00:00
|
|
|
"foo": {
|
2019-07-10 07:26:04 +00:00
|
|
|
TCPRouter: &dynamic.TCPRouter{
|
2019-05-16 08:58:06 +00:00
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
|
|
|
Rule: "HostSNI(`bar.foo`)",
|
2019-07-10 07:26:04 +00:00
|
|
|
TLS: &dynamic.RouterTCPTLSConfig{
|
2019-06-17 16:14:08 +00:00
|
|
|
Passthrough: false,
|
|
|
|
Options: "foo",
|
|
|
|
},
|
2019-05-16 08:58:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"bar": {
|
2019-07-10 07:26:04 +00:00
|
|
|
TCPRouter: &dynamic.TCPRouter{
|
2019-05-16 08:58:06 +00:00
|
|
|
|
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
|
|
|
Rule: "HostSNI(`foo.bar`)",
|
2019-07-10 07:26:04 +00:00
|
|
|
TLS: &dynamic.RouterTCPTLSConfig{
|
2019-06-17 16:14:08 +00:00
|
|
|
Passthrough: false,
|
|
|
|
Options: "bar",
|
|
|
|
},
|
2019-05-16 08:58:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedError: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "One router with wrong rule",
|
2019-07-15 15:04:04 +00:00
|
|
|
serviceConfig: map[string]*runtime.TCPServiceInfo{
|
2019-05-16 08:58:06 +00:00
|
|
|
"foo-service": {
|
2019-07-10 07:26:04 +00:00
|
|
|
TCPService: &dynamic.TCPService{
|
2019-09-13 18:00:06 +00:00
|
|
|
LoadBalancer: &dynamic.TCPServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.TCPServer{
|
2019-05-16 08:58:06 +00:00
|
|
|
{
|
|
|
|
Address: "127.0.0.1:80",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-15 15:04:04 +00:00
|
|
|
routerConfig: map[string]*runtime.TCPRouterInfo{
|
2019-05-16 08:58:06 +00:00
|
|
|
"foo": {
|
2019-07-10 07:26:04 +00:00
|
|
|
TCPRouter: &dynamic.TCPRouter{
|
2019-05-16 08:58:06 +00:00
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
|
|
|
Rule: "WrongRule(`bar.foo`)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"bar": {
|
2019-07-10 07:26:04 +00:00
|
|
|
TCPRouter: &dynamic.TCPRouter{
|
2019-05-16 08:58:06 +00:00
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
|
|
|
Rule: "HostSNI(`foo.bar`)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedError: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "All router with wrong rule",
|
2019-07-15 15:04:04 +00:00
|
|
|
serviceConfig: map[string]*runtime.TCPServiceInfo{
|
2019-05-16 08:58:06 +00:00
|
|
|
"foo-service": {
|
2019-07-10 07:26:04 +00:00
|
|
|
TCPService: &dynamic.TCPService{
|
2019-09-13 18:00:06 +00:00
|
|
|
LoadBalancer: &dynamic.TCPServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.TCPServer{
|
2019-05-16 08:58:06 +00:00
|
|
|
{
|
|
|
|
Address: "127.0.0.1:80",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-15 15:04:04 +00:00
|
|
|
routerConfig: map[string]*runtime.TCPRouterInfo{
|
2019-05-16 08:58:06 +00:00
|
|
|
"foo": {
|
2019-07-10 07:26:04 +00:00
|
|
|
TCPRouter: &dynamic.TCPRouter{
|
2019-05-16 08:58:06 +00:00
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
|
|
|
Rule: "WrongRule(`bar.foo`)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"bar": {
|
2019-07-10 07:26:04 +00:00
|
|
|
TCPRouter: &dynamic.TCPRouter{
|
2019-05-16 08:58:06 +00:00
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
|
|
|
Rule: "WrongRule(`foo.bar`)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedError: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Router with unknown service",
|
2019-07-15 15:04:04 +00:00
|
|
|
serviceConfig: map[string]*runtime.TCPServiceInfo{
|
2019-05-16 08:58:06 +00:00
|
|
|
"foo-service": {
|
2019-07-10 07:26:04 +00:00
|
|
|
TCPService: &dynamic.TCPService{
|
2019-09-13 18:00:06 +00:00
|
|
|
LoadBalancer: &dynamic.TCPServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.TCPServer{
|
2019-05-16 08:58:06 +00:00
|
|
|
{
|
|
|
|
Address: "127.0.0.1:80",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-15 15:04:04 +00:00
|
|
|
routerConfig: map[string]*runtime.TCPRouterInfo{
|
2019-05-16 08:58:06 +00:00
|
|
|
"foo": {
|
2019-07-10 07:26:04 +00:00
|
|
|
TCPRouter: &dynamic.TCPRouter{
|
2019-05-16 08:58:06 +00:00
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "wrong-service",
|
|
|
|
Rule: "HostSNI(`bar.foo`)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"bar": {
|
2019-07-10 07:26:04 +00:00
|
|
|
TCPRouter: &dynamic.TCPRouter{
|
2019-05-16 08:58:06 +00:00
|
|
|
|
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
|
|
|
Rule: "HostSNI(`foo.bar`)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedError: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Router with broken service",
|
2019-07-15 15:04:04 +00:00
|
|
|
serviceConfig: map[string]*runtime.TCPServiceInfo{
|
2019-05-16 08:58:06 +00:00
|
|
|
"foo-service": {
|
2019-07-10 07:26:04 +00:00
|
|
|
TCPService: &dynamic.TCPService{
|
2019-05-16 08:58:06 +00:00
|
|
|
LoadBalancer: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-15 15:04:04 +00:00
|
|
|
routerConfig: map[string]*runtime.TCPRouterInfo{
|
2019-05-16 08:58:06 +00:00
|
|
|
"bar": {
|
2019-07-10 07:26:04 +00:00
|
|
|
TCPRouter: &dynamic.TCPRouter{
|
2019-05-16 08:58:06 +00:00
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
|
|
|
Rule: "HostSNI(`foo.bar`)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedError: 2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
entryPoints := []string{"web"}
|
|
|
|
|
2019-07-15 15:04:04 +00:00
|
|
|
conf := &runtime.Configuration{
|
2019-05-16 08:58:06 +00:00
|
|
|
TCPServices: test.serviceConfig,
|
|
|
|
TCPRouters: test.routerConfig,
|
|
|
|
}
|
|
|
|
serviceManager := tcp.NewManager(conf)
|
2019-06-17 16:14:08 +00:00
|
|
|
tlsManager := tls.NewManager()
|
|
|
|
tlsManager.UpdateConfigs(
|
2019-09-13 17:28:04 +00:00
|
|
|
context.Background(),
|
2019-06-17 16:14:08 +00:00
|
|
|
map[string]tls.Store{},
|
2019-06-27 21:58:03 +00:00
|
|
|
map[string]tls.Options{
|
2019-06-21 15:18:05 +00:00
|
|
|
"default": {
|
|
|
|
MinVersion: "VersionTLS10",
|
|
|
|
},
|
2019-06-17 16:14:08 +00:00
|
|
|
"foo": {
|
|
|
|
MinVersion: "VersionTLS12",
|
|
|
|
},
|
|
|
|
"bar": {
|
|
|
|
MinVersion: "VersionTLS11",
|
|
|
|
},
|
|
|
|
},
|
2019-06-27 21:58:03 +00:00
|
|
|
[]*tls.CertAndStores{})
|
2019-06-17 16:14:08 +00:00
|
|
|
|
2019-05-16 08:58:06 +00:00
|
|
|
routerManager := NewManager(conf, serviceManager,
|
2019-06-17 16:14:08 +00:00
|
|
|
nil, nil, tlsManager)
|
2019-05-16 08:58:06 +00:00
|
|
|
|
|
|
|
_ = routerManager.BuildHandlers(context.Background(), entryPoints)
|
|
|
|
|
|
|
|
// even though conf was passed by argument to the manager builders above,
|
|
|
|
// it's ok to use it as the result we check, because everything worth checking
|
|
|
|
// can be accessed by pointers in it.
|
|
|
|
var allErrors int
|
|
|
|
for _, v := range conf.TCPServices {
|
|
|
|
if v.Err != nil {
|
|
|
|
allErrors++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, v := range conf.TCPRouters {
|
2019-07-19 14:42:04 +00:00
|
|
|
if len(v.Err) > 0 {
|
2019-05-16 08:58:06 +00:00
|
|
|
allErrors++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.Equal(t, test.expectedError, allErrors)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|