2019-05-09 12:30:06 +00:00
|
|
|
package tcp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2019-05-16 08:58:06 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-05-09 12:30:06 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/config/dynamic"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/config/runtime"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/server/provider"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/tcp"
|
2019-05-09 12:30:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestManager_BuildTCP(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
serviceName string
|
2019-07-15 15:04:04 +00:00
|
|
|
configs map[string]*runtime.TCPServiceInfo
|
2022-12-09 08:58:05 +00:00
|
|
|
stConfigs map[string]*dynamic.TCPServersTransport
|
2019-05-16 08:58:06 +00:00
|
|
|
providerName string
|
2019-05-09 12:30:06 +00:00
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "without configuration",
|
|
|
|
serviceName: "test",
|
|
|
|
configs: nil,
|
2019-05-16 08:58:06 +00:00
|
|
|
expectedError: `the service "test" does not exist`,
|
2019-05-09 12:30:06 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing lb configuration",
|
|
|
|
serviceName: "test",
|
2019-07-15 15:04:04 +00:00
|
|
|
configs: map[string]*runtime.TCPServiceInfo{
|
2019-05-16 08:58:06 +00:00
|
|
|
"test": {
|
2019-07-10 07:26:04 +00:00
|
|
|
TCPService: &dynamic.TCPService{},
|
2019-05-16 08:58:06 +00:00
|
|
|
},
|
2019-05-09 12:30:06 +00:00
|
|
|
},
|
2020-02-11 00:26:04 +00:00
|
|
|
expectedError: `the service "test" does not have any type defined`,
|
2019-05-09 12:30:06 +00:00
|
|
|
},
|
|
|
|
{
|
2019-05-16 08:58:06 +00:00
|
|
|
desc: "no such host, server is skipped, error is logged",
|
2019-05-09 12:30:06 +00:00
|
|
|
serviceName: "test",
|
2022-12-09 08:58:05 +00:00
|
|
|
stConfigs: map[string]*dynamic.TCPServersTransport{"default@internal": {}},
|
2019-07-15 15:04:04 +00:00
|
|
|
configs: map[string]*runtime.TCPServiceInfo{
|
2019-05-09 12:30:06 +00:00
|
|
|
"test": {
|
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: "test:31"},
|
|
|
|
},
|
2019-05-09 12:30:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-05-16 08:58:06 +00:00
|
|
|
desc: "invalid IP address, server is skipped, error is logged",
|
2019-05-09 12:30:06 +00:00
|
|
|
serviceName: "test",
|
2019-07-15 15:04:04 +00:00
|
|
|
configs: map[string]*runtime.TCPServiceInfo{
|
2019-05-09 12:30:06 +00:00
|
|
|
"test": {
|
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: "foobar"},
|
|
|
|
},
|
2019-05-09 12:30:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-05-16 08:58:06 +00:00
|
|
|
{
|
|
|
|
desc: "Simple service name",
|
|
|
|
serviceName: "serviceName",
|
2019-07-15 15:04:04 +00:00
|
|
|
configs: map[string]*runtime.TCPServiceInfo{
|
2019-05-16 08:58:06 +00:00
|
|
|
"serviceName": {
|
2019-07-10 07:26:04 +00:00
|
|
|
TCPService: &dynamic.TCPService{
|
2019-09-13 18:00:06 +00:00
|
|
|
LoadBalancer: &dynamic.TCPServersLoadBalancer{},
|
2019-05-16 08:58:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Service name with provider",
|
2019-06-21 07:54:04 +00:00
|
|
|
serviceName: "serviceName@provider-1",
|
2019-07-15 15:04:04 +00:00
|
|
|
configs: map[string]*runtime.TCPServiceInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"serviceName@provider-1": {
|
2019-07-10 07:26:04 +00:00
|
|
|
TCPService: &dynamic.TCPService{
|
2019-09-13 18:00:06 +00:00
|
|
|
LoadBalancer: &dynamic.TCPServersLoadBalancer{},
|
2019-05-16 08:58:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Service name with provider in context",
|
|
|
|
serviceName: "serviceName",
|
2019-07-15 15:04:04 +00:00
|
|
|
configs: map[string]*runtime.TCPServiceInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"serviceName@provider-1": {
|
2019-07-10 07:26:04 +00:00
|
|
|
TCPService: &dynamic.TCPService{
|
2019-09-13 18:00:06 +00:00
|
|
|
LoadBalancer: &dynamic.TCPServersLoadBalancer{},
|
2019-05-16 08:58:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
providerName: "provider-1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Server with correct host:port as address",
|
|
|
|
serviceName: "serviceName",
|
2022-12-09 08:58:05 +00:00
|
|
|
stConfigs: map[string]*dynamic.TCPServersTransport{"default@internal": {}},
|
2019-07-15 15:04:04 +00:00
|
|
|
configs: map[string]*runtime.TCPServiceInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"serviceName@provider-1": {
|
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: "foobar.com:80",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
providerName: "provider-1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Server with correct ip:port as address",
|
|
|
|
serviceName: "serviceName",
|
2022-12-09 08:58:05 +00:00
|
|
|
stConfigs: map[string]*dynamic.TCPServersTransport{"default@internal": {}},
|
2019-07-15 15:04:04 +00:00
|
|
|
configs: map[string]*runtime.TCPServiceInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"serviceName@provider-1": {
|
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: "192.168.0.12:80",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
providerName: "provider-1",
|
|
|
|
},
|
2022-12-09 08:58:05 +00:00
|
|
|
{
|
|
|
|
desc: "empty server address, server is skipped, error is logged",
|
|
|
|
serviceName: "serviceName",
|
|
|
|
configs: map[string]*runtime.TCPServiceInfo{
|
|
|
|
"serviceName@provider-1": {
|
|
|
|
TCPService: &dynamic.TCPService{
|
|
|
|
LoadBalancer: &dynamic.TCPServersLoadBalancer{
|
|
|
|
Servers: []dynamic.TCPServer{
|
|
|
|
{
|
|
|
|
Address: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
providerName: "provider-1",
|
|
|
|
},
|
2019-05-16 08:58:06 +00:00
|
|
|
{
|
|
|
|
desc: "missing port in address with hostname, server is skipped, error is logged",
|
|
|
|
serviceName: "serviceName",
|
2019-07-15 15:04:04 +00:00
|
|
|
configs: map[string]*runtime.TCPServiceInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"serviceName@provider-1": {
|
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: "foobar.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
providerName: "provider-1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing port in address with ip, server is skipped, error is logged",
|
|
|
|
serviceName: "serviceName",
|
2019-07-15 15:04:04 +00:00
|
|
|
configs: map[string]*runtime.TCPServiceInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"serviceName@provider-1": {
|
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: "192.168.0.12",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
providerName: "provider-1",
|
|
|
|
},
|
2022-12-09 08:58:05 +00:00
|
|
|
{
|
|
|
|
desc: "user defined serversTransport reference",
|
|
|
|
serviceName: "serviceName",
|
|
|
|
stConfigs: map[string]*dynamic.TCPServersTransport{"myServersTransport@provider-1": {}},
|
|
|
|
configs: map[string]*runtime.TCPServiceInfo{
|
|
|
|
"serviceName@provider-1": {
|
|
|
|
TCPService: &dynamic.TCPService{
|
|
|
|
LoadBalancer: &dynamic.TCPServersLoadBalancer{
|
|
|
|
Servers: []dynamic.TCPServer{
|
|
|
|
{
|
|
|
|
Address: "192.168.0.12:80",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ServersTransport: "myServersTransport@provider-1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
providerName: "provider-1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "user defined serversTransport reference not found",
|
|
|
|
serviceName: "serviceName",
|
|
|
|
configs: map[string]*runtime.TCPServiceInfo{
|
|
|
|
"serviceName@provider-1": {
|
|
|
|
TCPService: &dynamic.TCPService{
|
|
|
|
LoadBalancer: &dynamic.TCPServersLoadBalancer{
|
|
|
|
Servers: []dynamic.TCPServer{
|
|
|
|
{
|
|
|
|
Address: "192.168.0.12:80",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ServersTransport: "myServersTransport@provider-1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
providerName: "provider-1",
|
|
|
|
expectedError: "TCP dialer not found myServersTransport@provider-1",
|
|
|
|
},
|
2019-05-09 12:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2022-12-09 08:58:05 +00:00
|
|
|
dialerManager := tcp.NewDialerManager(nil)
|
|
|
|
if test.stConfigs != nil {
|
|
|
|
dialerManager.Update(test.stConfigs)
|
|
|
|
}
|
|
|
|
|
2019-07-15 15:04:04 +00:00
|
|
|
manager := NewManager(&runtime.Configuration{
|
2019-05-16 08:58:06 +00:00
|
|
|
TCPServices: test.configs,
|
2022-12-09 08:58:05 +00:00
|
|
|
}, dialerManager)
|
2019-05-16 08:58:06 +00:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
if len(test.providerName) > 0 {
|
2020-01-27 09:40:05 +00:00
|
|
|
ctx = provider.AddInContext(ctx, "foobar@"+test.providerName)
|
2019-05-16 08:58:06 +00:00
|
|
|
}
|
2019-05-09 12:30:06 +00:00
|
|
|
|
2019-05-16 08:58:06 +00:00
|
|
|
handler, err := manager.BuildTCP(ctx, test.serviceName)
|
2019-05-09 12:30:06 +00:00
|
|
|
|
|
|
|
if test.expectedError != "" {
|
2019-05-16 08:58:06 +00:00
|
|
|
assert.EqualError(t, err, test.expectedError)
|
|
|
|
require.Nil(t, handler)
|
2019-05-09 12:30:06 +00:00
|
|
|
} else {
|
2020-07-19 11:10:03 +00:00
|
|
|
assert.NoError(t, err)
|
2019-05-09 12:30:06 +00:00
|
|
|
require.NotNil(t, handler)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|