2017-05-26 13:32:03 +00:00
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-06-26 16:18:04 +00:00
|
|
|
"io"
|
2017-05-26 13:32:03 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2019-06-26 16:18:04 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2017-05-26 13:32:03 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-08-03 01:58:23 +00:00
|
|
|
"github.com/containous/traefik/v2/pkg/config/dynamic"
|
|
|
|
"github.com/containous/traefik/v2/pkg/safe"
|
2017-05-26 13:32:03 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-01-29 15:46:09 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-05-26 13:32:03 +00:00
|
|
|
)
|
|
|
|
|
2018-05-22 10:02:03 +00:00
|
|
|
type ProvideTestCase struct {
|
2019-07-18 14:26:05 +00:00
|
|
|
desc string
|
|
|
|
directoryPaths []string
|
|
|
|
filePath string
|
|
|
|
expectedNumRouter int
|
|
|
|
expectedNumService int
|
|
|
|
expectedNumTLSConf int
|
|
|
|
expectedNumTLSOptions int
|
2018-05-22 10:02:03 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 16:18:04 +00:00
|
|
|
func TestTLSContent(t *testing.T) {
|
|
|
|
tempDir := createTempDir(t, "testdir")
|
|
|
|
defer os.RemoveAll(tempDir)
|
|
|
|
|
|
|
|
fileTLS, err := createTempFile("./fixtures/toml/tls_file.cert", tempDir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
fileConfig, err := ioutil.TempFile(tempDir, "temp*.toml")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
content := `
|
2019-06-27 21:58:03 +00:00
|
|
|
[[tls.certificates]]
|
|
|
|
certFile = "` + fileTLS.Name() + `"
|
|
|
|
keyFile = "` + fileTLS.Name() + `"
|
2019-06-26 16:18:04 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
_, err = fileConfig.Write([]byte(content))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
provider := &Provider{}
|
2019-09-13 17:28:04 +00:00
|
|
|
configuration, err := provider.loadFileConfig(context.Background(), fileConfig.Name(), true)
|
2019-06-26 16:18:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-06-27 21:58:03 +00:00
|
|
|
require.Equal(t, "CONTENT", configuration.TLS.Certificates[0].Certificate.CertFile.String())
|
|
|
|
require.Equal(t, "CONTENT", configuration.TLS.Certificates[0].Certificate.KeyFile.String())
|
2019-06-26 16:18:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrorWhenEmptyConfig(t *testing.T) {
|
|
|
|
provider := &Provider{}
|
2019-07-10 07:26:04 +00:00
|
|
|
configChan := make(chan dynamic.Message)
|
2019-06-26 16:18:04 +00:00
|
|
|
errorChan := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
err := provider.Provide(configChan, safe.NewPool(context.Background()))
|
|
|
|
assert.Error(t, err)
|
|
|
|
close(errorChan)
|
|
|
|
}()
|
|
|
|
|
|
|
|
timeout := time.After(time.Second)
|
|
|
|
select {
|
|
|
|
case <-configChan:
|
|
|
|
t.Fatal("We should not receive config message")
|
|
|
|
case <-timeout:
|
|
|
|
t.Fatal("timeout while waiting for config")
|
|
|
|
case <-errorChan:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-22 10:02:03 +00:00
|
|
|
func TestProvideWithoutWatch(t *testing.T) {
|
|
|
|
for _, test := range getTestCases() {
|
|
|
|
t.Run(test.desc+" without watch", func(t *testing.T) {
|
|
|
|
provider, clean := createProvider(t, test, false)
|
|
|
|
defer clean()
|
2019-07-10 07:26:04 +00:00
|
|
|
configChan := make(chan dynamic.Message)
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
provider.DebugLogGeneratedTemplate = true
|
2018-05-22 10:02:03 +00:00
|
|
|
|
|
|
|
go func() {
|
2018-07-11 07:08:03 +00:00
|
|
|
err := provider.Provide(configChan, safe.NewPool(context.Background()))
|
2018-05-22 10:02:03 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
timeout := time.After(time.Second)
|
|
|
|
select {
|
2018-11-14 09:18:03 +00:00
|
|
|
case conf := <-configChan:
|
2019-06-27 21:58:03 +00:00
|
|
|
require.NotNil(t, conf.Configuration.HTTP)
|
2019-03-14 08:30:04 +00:00
|
|
|
assert.Len(t, conf.Configuration.HTTP.Services, test.expectedNumService)
|
|
|
|
assert.Len(t, conf.Configuration.HTTP.Routers, test.expectedNumRouter)
|
2019-06-27 21:58:03 +00:00
|
|
|
require.NotNil(t, conf.Configuration.TLS)
|
|
|
|
assert.Len(t, conf.Configuration.TLS.Certificates, test.expectedNumTLSConf)
|
2019-07-18 14:26:05 +00:00
|
|
|
assert.Len(t, conf.Configuration.TLS.Options, test.expectedNumTLSOptions)
|
2018-05-22 10:02:03 +00:00
|
|
|
case <-timeout:
|
|
|
|
t.Errorf("timeout while waiting for config")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvideWithWatch(t *testing.T) {
|
|
|
|
for _, test := range getTestCases() {
|
|
|
|
t.Run(test.desc+" with watch", func(t *testing.T) {
|
|
|
|
provider, clean := createProvider(t, test, true)
|
|
|
|
defer clean()
|
2019-07-10 07:26:04 +00:00
|
|
|
configChan := make(chan dynamic.Message)
|
2018-05-22 10:02:03 +00:00
|
|
|
|
|
|
|
go func() {
|
2018-07-11 07:08:03 +00:00
|
|
|
err := provider.Provide(configChan, safe.NewPool(context.Background()))
|
2018-05-22 10:02:03 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
timeout := time.After(time.Second)
|
|
|
|
select {
|
2018-11-14 09:18:03 +00:00
|
|
|
case conf := <-configChan:
|
2019-06-27 21:58:03 +00:00
|
|
|
require.NotNil(t, conf.Configuration.HTTP)
|
2019-03-14 08:30:04 +00:00
|
|
|
assert.Len(t, conf.Configuration.HTTP.Services, 0)
|
|
|
|
assert.Len(t, conf.Configuration.HTTP.Routers, 0)
|
2019-06-27 21:58:03 +00:00
|
|
|
require.NotNil(t, conf.Configuration.TLS)
|
|
|
|
assert.Len(t, conf.Configuration.TLS.Certificates, 0)
|
2018-05-22 10:02:03 +00:00
|
|
|
case <-timeout:
|
|
|
|
t.Errorf("timeout while waiting for config")
|
|
|
|
}
|
|
|
|
|
2019-06-26 16:18:04 +00:00
|
|
|
if len(test.filePath) > 0 {
|
|
|
|
err := copyFile(test.filePath, provider.Filename)
|
|
|
|
require.NoError(t, err)
|
2018-05-22 10:02:03 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 16:18:04 +00:00
|
|
|
if len(test.directoryPaths) > 0 {
|
|
|
|
for i, filePath := range test.directoryPaths {
|
|
|
|
err := copyFile(filePath, filepath.Join(provider.Directory, strconv.Itoa(i)+filepath.Ext(filePath)))
|
|
|
|
require.NoError(t, err)
|
2018-05-22 10:02:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout = time.After(time.Second * 1)
|
2018-11-14 09:18:03 +00:00
|
|
|
var numUpdates, numServices, numRouters, numTLSConfs int
|
2018-10-04 07:26:03 +00:00
|
|
|
for {
|
2018-05-22 10:02:03 +00:00
|
|
|
select {
|
2018-11-14 09:18:03 +00:00
|
|
|
case conf := <-configChan:
|
2018-10-04 07:26:03 +00:00
|
|
|
numUpdates++
|
2019-03-14 08:30:04 +00:00
|
|
|
numServices = len(conf.Configuration.HTTP.Services)
|
|
|
|
numRouters = len(conf.Configuration.HTTP.Routers)
|
2019-06-27 21:58:03 +00:00
|
|
|
numTLSConfs = len(conf.Configuration.TLS.Certificates)
|
2018-11-14 09:18:03 +00:00
|
|
|
t.Logf("received update #%d: services %d/%d, routers %d/%d, TLS configs %d/%d", numUpdates, numServices, test.expectedNumService, numRouters, test.expectedNumRouter, numTLSConfs, test.expectedNumTLSConf)
|
2018-10-04 07:26:03 +00:00
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
if numServices == test.expectedNumService && numRouters == test.expectedNumRouter && numTLSConfs == test.expectedNumTLSConf {
|
2018-10-04 07:26:03 +00:00
|
|
|
return
|
|
|
|
}
|
2018-05-22 10:02:03 +00:00
|
|
|
case <-timeout:
|
2018-10-04 07:26:03 +00:00
|
|
|
t.Fatal("timeout while waiting for config")
|
2018-05-22 10:02:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
func getTestCases() []ProvideTestCase {
|
|
|
|
return []ProvideTestCase{
|
|
|
|
{
|
|
|
|
desc: "simple file",
|
2019-06-26 16:18:04 +00:00
|
|
|
filePath: "./fixtures/toml/simple_file_01.toml",
|
|
|
|
expectedNumRouter: 3,
|
|
|
|
expectedNumService: 6,
|
|
|
|
expectedNumTLSConf: 5,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "simple file yaml",
|
|
|
|
filePath: "./fixtures/yaml/simple_file_01.yml",
|
2018-11-14 09:18:03 +00:00
|
|
|
expectedNumRouter: 3,
|
|
|
|
expectedNumService: 6,
|
|
|
|
expectedNumTLSConf: 5,
|
|
|
|
},
|
|
|
|
{
|
2019-06-26 16:18:04 +00:00
|
|
|
desc: "template file",
|
|
|
|
filePath: "./fixtures/toml/template_file.toml",
|
|
|
|
expectedNumRouter: 20,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "template file yaml",
|
|
|
|
filePath: "./fixtures/yaml/template_file.yml",
|
|
|
|
expectedNumRouter: 20,
|
|
|
|
},
|
2018-11-14 09:18:03 +00:00
|
|
|
{
|
2019-06-26 16:18:04 +00:00
|
|
|
desc: "simple directory",
|
|
|
|
directoryPaths: []string{
|
|
|
|
"./fixtures/toml/dir01_file01.toml",
|
|
|
|
"./fixtures/toml/dir01_file02.toml",
|
|
|
|
"./fixtures/toml/dir01_file03.toml",
|
|
|
|
},
|
2019-07-18 14:26:05 +00:00
|
|
|
expectedNumRouter: 2,
|
|
|
|
expectedNumService: 3,
|
|
|
|
expectedNumTLSConf: 4,
|
|
|
|
expectedNumTLSOptions: 1,
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
2019-06-26 16:18:04 +00:00
|
|
|
{
|
|
|
|
desc: "simple directory yaml",
|
|
|
|
directoryPaths: []string{
|
|
|
|
"./fixtures/yaml/dir01_file01.yml",
|
|
|
|
"./fixtures/yaml/dir01_file02.yml",
|
|
|
|
"./fixtures/yaml/dir01_file03.yml",
|
|
|
|
},
|
2019-07-18 14:26:05 +00:00
|
|
|
expectedNumRouter: 2,
|
|
|
|
expectedNumService: 3,
|
|
|
|
expectedNumTLSConf: 4,
|
|
|
|
expectedNumTLSOptions: 1,
|
2019-06-26 16:18:04 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "template in directory",
|
|
|
|
directoryPaths: []string{
|
|
|
|
"./fixtures/toml/template_in_directory_file01.toml",
|
|
|
|
"./fixtures/toml/template_in_directory_file02.toml",
|
|
|
|
},
|
|
|
|
expectedNumRouter: 20,
|
|
|
|
expectedNumService: 20,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "template in directory yaml",
|
|
|
|
directoryPaths: []string{
|
|
|
|
"./fixtures/yaml/template_in_directory_file01.yml",
|
|
|
|
"./fixtures/yaml/template_in_directory_file02.yml",
|
|
|
|
},
|
|
|
|
expectedNumRouter: 20,
|
|
|
|
expectedNumService: 20,
|
|
|
|
},
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-22 10:02:03 +00:00
|
|
|
func createProvider(t *testing.T, test ProvideTestCase, watch bool) (*Provider, func()) {
|
|
|
|
tempDir := createTempDir(t, "testdir")
|
|
|
|
|
|
|
|
provider := &Provider{}
|
2019-06-17 09:48:05 +00:00
|
|
|
provider.Watch = true
|
2018-05-22 10:02:03 +00:00
|
|
|
|
2019-06-26 16:18:04 +00:00
|
|
|
if len(test.directoryPaths) > 0 {
|
2018-05-22 10:02:03 +00:00
|
|
|
if !watch {
|
2019-06-26 16:18:04 +00:00
|
|
|
for _, filePath := range test.directoryPaths {
|
|
|
|
var err error
|
|
|
|
_, err = createTempFile(filePath, tempDir)
|
|
|
|
require.NoError(t, err)
|
2018-05-22 10:02:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
provider.Directory = tempDir
|
|
|
|
}
|
|
|
|
|
2019-06-26 16:18:04 +00:00
|
|
|
if len(test.filePath) > 0 {
|
|
|
|
|
|
|
|
var file *os.File
|
2018-05-22 10:02:03 +00:00
|
|
|
if watch {
|
2019-06-26 16:18:04 +00:00
|
|
|
var err error
|
|
|
|
file, err = ioutil.TempFile(tempDir, "temp*"+filepath.Ext(test.filePath))
|
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
file, err = createTempFile(test.filePath, tempDir)
|
|
|
|
require.NoError(t, err)
|
2018-05-22 10:02:03 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 16:18:04 +00:00
|
|
|
provider.Filename = file.Name()
|
2018-05-22 10:02:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return provider, func() {
|
2019-03-18 08:34:03 +00:00
|
|
|
os.RemoveAll(tempDir)
|
2018-05-22 10:02:03 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
// createTempDir Helper
|
|
|
|
func createTempDir(t *testing.T, dir string) string {
|
|
|
|
t.Helper()
|
2019-06-26 16:18:04 +00:00
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
d, err := ioutil.TempDir("", dir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2019-06-26 16:18:04 +00:00
|
|
|
func copyFile(srcPath, dstPath string) error {
|
|
|
|
dst, err := os.OpenFile(dstPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
2019-06-26 16:18:04 +00:00
|
|
|
defer dst.Close()
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2019-06-26 16:18:04 +00:00
|
|
|
src, err := os.Open(srcPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
2019-06-26 16:18:04 +00:00
|
|
|
defer src.Close()
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2019-06-26 16:18:04 +00:00
|
|
|
_, err = io.Copy(dst, src)
|
|
|
|
return err
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
2019-01-29 15:46:09 +00:00
|
|
|
|
2019-06-26 16:18:04 +00:00
|
|
|
func createTempFile(srcPath, tempDir string) (*os.File, error) {
|
|
|
|
file, err := ioutil.TempFile(tempDir, "temp*"+filepath.Ext(srcPath))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
2019-01-29 15:46:09 +00:00
|
|
|
|
2019-06-26 16:18:04 +00:00
|
|
|
src, err := os.Open(srcPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer src.Close()
|
2019-01-29 15:46:09 +00:00
|
|
|
|
2019-06-26 16:18:04 +00:00
|
|
|
_, err = io.Copy(file, src)
|
|
|
|
return file, err
|
2019-01-29 15:46:09 +00:00
|
|
|
}
|