2019-05-09 12:30:06 +00:00
|
|
|
package tcp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/containous/traefik/pkg/config"
|
2019-05-16 08:58:06 +00:00
|
|
|
"github.com/containous/traefik/pkg/server/internal"
|
|
|
|
"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-05-16 08:58:06 +00:00
|
|
|
configs map[string]*config.TCPServiceInfo
|
|
|
|
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-05-16 08:58:06 +00:00
|
|
|
configs: map[string]*config.TCPServiceInfo{
|
|
|
|
"test": {
|
|
|
|
TCPService: &config.TCPService{},
|
|
|
|
},
|
2019-05-09 12:30:06 +00:00
|
|
|
},
|
|
|
|
expectedError: `the service "test" doesn't have any TCP load balancer`,
|
|
|
|
},
|
|
|
|
{
|
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-05-16 08:58:06 +00:00
|
|
|
configs: map[string]*config.TCPServiceInfo{
|
2019-05-09 12:30:06 +00:00
|
|
|
"test": {
|
2019-05-16 08:58:06 +00:00
|
|
|
TCPService: &config.TCPService{
|
|
|
|
LoadBalancer: &config.TCPLoadBalancerService{
|
|
|
|
Servers: []config.TCPServer{
|
|
|
|
{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-05-16 08:58:06 +00:00
|
|
|
configs: map[string]*config.TCPServiceInfo{
|
2019-05-09 12:30:06 +00:00
|
|
|
"test": {
|
2019-05-16 08:58:06 +00:00
|
|
|
TCPService: &config.TCPService{
|
|
|
|
LoadBalancer: &config.TCPLoadBalancerService{
|
|
|
|
Servers: []config.TCPServer{
|
|
|
|
{Address: "foobar"},
|
|
|
|
},
|
2019-05-09 12:30:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-05-16 08:58:06 +00:00
|
|
|
{
|
|
|
|
desc: "Simple service name",
|
|
|
|
serviceName: "serviceName",
|
|
|
|
configs: map[string]*config.TCPServiceInfo{
|
|
|
|
"serviceName": {
|
|
|
|
TCPService: &config.TCPService{
|
2019-06-05 20:18:06 +00:00
|
|
|
LoadBalancer: &config.TCPLoadBalancerService{},
|
2019-05-16 08:58:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Service name with provider",
|
|
|
|
serviceName: "provider-1.serviceName",
|
|
|
|
configs: map[string]*config.TCPServiceInfo{
|
|
|
|
"provider-1.serviceName": {
|
|
|
|
TCPService: &config.TCPService{
|
2019-06-05 20:18:06 +00:00
|
|
|
LoadBalancer: &config.TCPLoadBalancerService{},
|
2019-05-16 08:58:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Service name with provider in context",
|
|
|
|
serviceName: "serviceName",
|
|
|
|
configs: map[string]*config.TCPServiceInfo{
|
|
|
|
"provider-1.serviceName": {
|
|
|
|
TCPService: &config.TCPService{
|
2019-06-05 20:18:06 +00:00
|
|
|
LoadBalancer: &config.TCPLoadBalancerService{},
|
2019-05-16 08:58:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
providerName: "provider-1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Server with correct host:port as address",
|
|
|
|
serviceName: "serviceName",
|
|
|
|
configs: map[string]*config.TCPServiceInfo{
|
|
|
|
"provider-1.serviceName": {
|
|
|
|
TCPService: &config.TCPService{
|
|
|
|
LoadBalancer: &config.TCPLoadBalancerService{
|
|
|
|
Servers: []config.TCPServer{
|
|
|
|
{
|
|
|
|
Address: "foobar.com:80",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
providerName: "provider-1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Server with correct ip:port as address",
|
|
|
|
serviceName: "serviceName",
|
|
|
|
configs: map[string]*config.TCPServiceInfo{
|
|
|
|
"provider-1.serviceName": {
|
|
|
|
TCPService: &config.TCPService{
|
|
|
|
LoadBalancer: &config.TCPLoadBalancerService{
|
|
|
|
Servers: []config.TCPServer{
|
|
|
|
{
|
|
|
|
Address: "192.168.0.12:80",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
providerName: "provider-1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing port in address with hostname, server is skipped, error is logged",
|
|
|
|
serviceName: "serviceName",
|
|
|
|
configs: map[string]*config.TCPServiceInfo{
|
|
|
|
"provider-1.serviceName": {
|
|
|
|
TCPService: &config.TCPService{
|
|
|
|
LoadBalancer: &config.TCPLoadBalancerService{
|
|
|
|
Servers: []config.TCPServer{
|
|
|
|
{
|
|
|
|
Address: "foobar.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
providerName: "provider-1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing port in address with ip, server is skipped, error is logged",
|
|
|
|
serviceName: "serviceName",
|
|
|
|
configs: map[string]*config.TCPServiceInfo{
|
|
|
|
"provider-1.serviceName": {
|
|
|
|
TCPService: &config.TCPService{
|
|
|
|
LoadBalancer: &config.TCPLoadBalancerService{
|
|
|
|
Servers: []config.TCPServer{
|
|
|
|
{
|
|
|
|
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-05-16 08:58:06 +00:00
|
|
|
manager := NewManager(&config.RuntimeConfiguration{
|
|
|
|
TCPServices: test.configs,
|
|
|
|
})
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
if len(test.providerName) > 0 {
|
|
|
|
ctx = internal.AddProviderInContext(ctx, test.providerName+".foobar")
|
|
|
|
}
|
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 {
|
2019-05-16 08:58:06 +00:00
|
|
|
assert.Nil(t, err)
|
2019-05-09 12:30:06 +00:00
|
|
|
require.NotNil(t, handler)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|