2019-05-09 12:30: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"
|
2020-01-27 09:40:05 +00:00
|
|
|
"github.com/containous/traefik/v2/pkg/server/provider"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
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",
|
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",
|
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",
|
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",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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",
|
|
|
|
},
|
2019-05-09 12:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-07-15 15:04:04 +00:00
|
|
|
manager := NewManager(&runtime.Configuration{
|
2019-05-16 08:58:06 +00:00
|
|
|
TCPServices: test.configs,
|
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|