2019-05-16 10:58:06 +02:00
package config_test
import (
2019-06-17 18:14:08 +02:00
"context"
2019-05-16 10:58:06 +02:00
"testing"
"github.com/containous/traefik/pkg/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// all the Routers/Middlewares/Services are considered fully qualified
func TestPopulateUsedby ( t * testing . T ) {
testCases := [ ] struct {
desc string
conf * config . RuntimeConfiguration
expected config . RuntimeConfiguration
} {
{
desc : "nil config" ,
conf : nil ,
expected : config . RuntimeConfiguration { } ,
} ,
{
desc : "One service used by two routers" ,
conf : & config . RuntimeConfiguration {
Routers : map [ string ] * config . RouterInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo" : {
2019-05-16 10:58:06 +02:00
Router : & config . Router {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-05-16 10:58:06 +02:00
Rule : "Host(`bar.foo`)" ,
} ,
} ,
2019-06-20 00:40:05 +02:00
"myprovider@bar" : {
2019-05-16 10:58:06 +02:00
Router : & config . Router {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-05-16 10:58:06 +02:00
Rule : "Host(`foo.bar`)" ,
} ,
} ,
} ,
Services : map [ string ] * config . ServiceInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo-service" : {
2019-05-16 10:58:06 +02:00
Service : & config . Service {
LoadBalancer : & config . LoadBalancerService {
Servers : [ ] config . Server {
2019-06-05 22:18:06 +02:00
{ URL : "http://127.0.0.1:8085" } ,
{ URL : "http://127.0.0.1:8086" } ,
2019-05-16 10:58:06 +02:00
} ,
HealthCheck : & config . HealthCheck {
Interval : "500ms" ,
Path : "/health" ,
} ,
} ,
} ,
} ,
} ,
} ,
expected : config . RuntimeConfiguration {
Routers : map [ string ] * config . RouterInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo" : { } ,
"myprovider@bar" : { } ,
2019-05-16 10:58:06 +02:00
} ,
Services : map [ string ] * config . ServiceInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo-service" : {
UsedBy : [ ] string { "myprovider@bar" , "myprovider@foo" } ,
2019-05-16 10:58:06 +02:00
} ,
} ,
} ,
} ,
{
desc : "One service used by two routers, but one router with wrong rule" ,
conf : & config . RuntimeConfiguration {
Services : map [ string ] * config . ServiceInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo-service" : {
2019-05-16 10:58:06 +02:00
Service : & config . Service {
LoadBalancer : & config . LoadBalancerService {
Servers : [ ] config . Server {
2019-06-05 22:18:06 +02:00
{ URL : "http://127.0.0.1" } ,
2019-05-16 10:58:06 +02:00
} ,
} ,
} ,
} ,
} ,
Routers : map [ string ] * config . RouterInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo" : {
2019-05-16 10:58:06 +02:00
Router : & config . Router {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-05-16 10:58:06 +02:00
Rule : "WrongRule(`bar.foo`)" ,
} ,
} ,
2019-06-20 00:40:05 +02:00
"myprovider@bar" : {
2019-05-16 10:58:06 +02:00
Router : & config . Router {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-05-16 10:58:06 +02:00
Rule : "Host(`foo.bar`)" ,
} ,
} ,
} ,
} ,
expected : config . RuntimeConfiguration {
Routers : map [ string ] * config . RouterInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo" : { } ,
"myprovider@bar" : { } ,
2019-05-16 10:58:06 +02:00
} ,
Services : map [ string ] * config . ServiceInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo-service" : {
UsedBy : [ ] string { "myprovider@bar" , "myprovider@foo" } ,
2019-05-16 10:58:06 +02:00
} ,
} ,
} ,
} ,
{
desc : "Broken Service used by one Router" ,
conf : & config . RuntimeConfiguration {
Services : map [ string ] * config . ServiceInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo-service" : {
2019-05-16 10:58:06 +02:00
Service : & config . Service {
LoadBalancer : nil ,
} ,
} ,
} ,
Routers : map [ string ] * config . RouterInfo {
2019-06-20 00:40:05 +02:00
"myprovider@bar" : {
2019-05-16 10:58:06 +02:00
Router : & config . Router {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-05-16 10:58:06 +02:00
Rule : "Host(`foo.bar`)" ,
} ,
} ,
} ,
} ,
expected : config . RuntimeConfiguration {
Routers : map [ string ] * config . RouterInfo {
2019-06-20 00:40:05 +02:00
"myprovider@bar" : { } ,
2019-05-16 10:58:06 +02:00
} ,
Services : map [ string ] * config . ServiceInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo-service" : {
UsedBy : [ ] string { "myprovider@bar" } ,
2019-05-16 10:58:06 +02:00
} ,
} ,
} ,
} ,
{
desc : "2 different Services each used by a disctinct router." ,
conf : & config . RuntimeConfiguration {
Services : map [ string ] * config . ServiceInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo-service" : {
2019-05-16 10:58:06 +02:00
Service : & config . Service {
LoadBalancer : & config . LoadBalancerService {
Servers : [ ] config . Server {
{
2019-06-05 22:18:06 +02:00
URL : "http://127.0.0.1:8085" ,
2019-05-16 10:58:06 +02:00
} ,
{
2019-06-05 22:18:06 +02:00
URL : "http://127.0.0.1:8086" ,
2019-05-16 10:58:06 +02:00
} ,
} ,
HealthCheck : & config . HealthCheck {
Interval : "500ms" ,
Path : "/health" ,
} ,
} ,
} ,
} ,
2019-06-20 00:40:05 +02:00
"myprovider@bar-service" : {
2019-05-16 10:58:06 +02:00
Service : & config . Service {
LoadBalancer : & config . LoadBalancerService {
Servers : [ ] config . Server {
{
2019-06-05 22:18:06 +02:00
URL : "http://127.0.0.1:8087" ,
2019-05-16 10:58:06 +02:00
} ,
{
2019-06-05 22:18:06 +02:00
URL : "http://127.0.0.1:8088" ,
2019-05-16 10:58:06 +02:00
} ,
} ,
HealthCheck : & config . HealthCheck {
Interval : "500ms" ,
Path : "/health" ,
} ,
} ,
} ,
} ,
} ,
Routers : map [ string ] * config . RouterInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo" : {
2019-05-16 10:58:06 +02:00
Router : & config . Router {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-05-16 10:58:06 +02:00
Rule : "Host(`bar.foo`)" ,
} ,
} ,
2019-06-20 00:40:05 +02:00
"myprovider@bar" : {
2019-05-16 10:58:06 +02:00
Router : & config . Router {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@bar-service" ,
2019-05-16 10:58:06 +02:00
Rule : "Host(`foo.bar`)" ,
} ,
} ,
} ,
} ,
expected : config . RuntimeConfiguration {
Routers : map [ string ] * config . RouterInfo {
2019-06-20 00:40:05 +02:00
"myprovider@bar" : { } ,
"myprovider@foo" : { } ,
2019-05-16 10:58:06 +02:00
} ,
Services : map [ string ] * config . ServiceInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo-service" : {
UsedBy : [ ] string { "myprovider@foo" } ,
2019-05-16 10:58:06 +02:00
} ,
2019-06-20 00:40:05 +02:00
"myprovider@bar-service" : {
UsedBy : [ ] string { "myprovider@bar" } ,
2019-05-16 10:58:06 +02:00
} ,
} ,
} ,
} ,
{
desc : "2 middlewares both used by 2 Routers" ,
conf : & config . RuntimeConfiguration {
Services : map [ string ] * config . ServiceInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo-service" : {
2019-05-16 10:58:06 +02:00
Service : & config . Service {
LoadBalancer : & config . LoadBalancerService {
Servers : [ ] config . Server {
{
2019-06-05 22:18:06 +02:00
URL : "http://127.0.0.1" ,
2019-05-16 10:58:06 +02:00
} ,
} ,
} ,
} ,
} ,
} ,
Middlewares : map [ string ] * config . MiddlewareInfo {
2019-06-20 00:40:05 +02:00
"myprovider@auth" : {
2019-05-16 10:58:06 +02:00
Middleware : & config . Middleware {
BasicAuth : & config . BasicAuth {
Users : [ ] string { "admin:admin" } ,
} ,
} ,
} ,
2019-06-20 00:40:05 +02:00
"myprovider@addPrefixTest" : {
2019-05-16 10:58:06 +02:00
Middleware : & config . Middleware {
AddPrefix : & config . AddPrefix {
Prefix : "/toto" ,
} ,
} ,
} ,
} ,
Routers : map [ string ] * config . RouterInfo {
2019-06-20 00:40:05 +02:00
"myprovider@bar" : {
2019-05-16 10:58:06 +02:00
Router : & config . Router {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-05-16 10:58:06 +02:00
Rule : "Host(`foo.bar`)" ,
Middlewares : [ ] string { "auth" , "addPrefixTest" } ,
} ,
} ,
2019-06-20 00:40:05 +02:00
"myprovider@test" : {
2019-05-16 10:58:06 +02:00
Router : & config . Router {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-05-16 10:58:06 +02:00
Rule : "Host(`foo.bar.other`)" ,
Middlewares : [ ] string { "addPrefixTest" , "auth" } ,
} ,
} ,
} ,
} ,
expected : config . RuntimeConfiguration {
Routers : map [ string ] * config . RouterInfo {
2019-06-20 00:40:05 +02:00
"myprovider@bar" : { } ,
"myprovider@test" : { } ,
2019-05-16 10:58:06 +02:00
} ,
Services : map [ string ] * config . ServiceInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo-service" : {
UsedBy : [ ] string { "myprovider@bar" , "myprovider@test" } ,
2019-05-16 10:58:06 +02:00
} ,
} ,
Middlewares : map [ string ] * config . MiddlewareInfo {
2019-06-20 00:40:05 +02:00
"myprovider@auth" : {
UsedBy : [ ] string { "myprovider@bar" , "myprovider@test" } ,
2019-05-16 10:58:06 +02:00
} ,
2019-06-20 00:40:05 +02:00
"myprovider@addPrefixTest" : {
UsedBy : [ ] string { "myprovider@bar" , "myprovider@test" } ,
2019-05-16 10:58:06 +02:00
} ,
} ,
} ,
} ,
{
desc : "Unknown middleware is not used by the Router" ,
conf : & config . RuntimeConfiguration {
Services : map [ string ] * config . ServiceInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo-service" : {
2019-05-16 10:58:06 +02:00
Service : & config . Service {
LoadBalancer : & config . LoadBalancerService {
Servers : [ ] config . Server {
{
2019-06-05 22:18:06 +02:00
URL : "http://127.0.0.1" ,
2019-05-16 10:58:06 +02:00
} ,
} ,
} ,
} ,
} ,
} ,
Middlewares : map [ string ] * config . MiddlewareInfo {
2019-06-20 00:40:05 +02:00
"myprovider@auth" : {
2019-05-16 10:58:06 +02:00
Middleware : & config . Middleware {
BasicAuth : & config . BasicAuth {
Users : [ ] string { "admin:admin" } ,
} ,
} ,
} ,
} ,
Routers : map [ string ] * config . RouterInfo {
2019-06-20 00:40:05 +02:00
"myprovider@bar" : {
2019-05-16 10:58:06 +02:00
Router : & config . Router {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-05-16 10:58:06 +02:00
Rule : "Host(`foo.bar`)" ,
Middlewares : [ ] string { "unknown" } ,
} ,
} ,
} ,
} ,
expected : config . RuntimeConfiguration {
Services : map [ string ] * config . ServiceInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo-service" : {
UsedBy : [ ] string { "myprovider@bar" } ,
2019-05-16 10:58:06 +02:00
} ,
} ,
} ,
} ,
{
desc : "Broken middleware is used by Router" ,
conf : & config . RuntimeConfiguration {
Services : map [ string ] * config . ServiceInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo-service" : {
2019-05-16 10:58:06 +02:00
Service : & config . Service {
LoadBalancer : & config . LoadBalancerService {
Servers : [ ] config . Server {
{
2019-06-05 22:18:06 +02:00
URL : "http://127.0.0.1" ,
2019-05-16 10:58:06 +02:00
} ,
} ,
} ,
} ,
} ,
} ,
Middlewares : map [ string ] * config . MiddlewareInfo {
2019-06-20 00:40:05 +02:00
"myprovider@auth" : {
2019-05-16 10:58:06 +02:00
Middleware : & config . Middleware {
BasicAuth : & config . BasicAuth {
Users : [ ] string { "badConf" } ,
} ,
} ,
} ,
} ,
Routers : map [ string ] * config . RouterInfo {
2019-06-20 00:40:05 +02:00
"myprovider@bar" : {
2019-05-16 10:58:06 +02:00
Router : & config . Router {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-05-16 10:58:06 +02:00
Rule : "Host(`foo.bar`)" ,
2019-06-20 00:40:05 +02:00
Middlewares : [ ] string { "myprovider@auth" } ,
2019-05-16 10:58:06 +02:00
} ,
} ,
} ,
} ,
expected : config . RuntimeConfiguration {
Routers : map [ string ] * config . RouterInfo {
2019-06-20 00:40:05 +02:00
"myprovider@bar" : { } ,
2019-05-16 10:58:06 +02:00
} ,
Services : map [ string ] * config . ServiceInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo-service" : {
UsedBy : [ ] string { "myprovider@bar" } ,
2019-05-16 10:58:06 +02:00
} ,
} ,
Middlewares : map [ string ] * config . MiddlewareInfo {
2019-06-20 00:40:05 +02:00
"myprovider@auth" : {
UsedBy : [ ] string { "myprovider@bar" } ,
2019-05-16 10:58:06 +02:00
} ,
} ,
} ,
} ,
{
desc : "2 middlewares from 2 disctinct providers both used by 2 Routers" ,
conf : & config . RuntimeConfiguration {
Services : map [ string ] * config . ServiceInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo-service" : {
2019-05-16 10:58:06 +02:00
Service : & config . Service {
LoadBalancer : & config . LoadBalancerService {
Servers : [ ] config . Server {
{
2019-06-05 22:18:06 +02:00
URL : "http://127.0.0.1" ,
2019-05-16 10:58:06 +02:00
} ,
} ,
} ,
} ,
} ,
} ,
Middlewares : map [ string ] * config . MiddlewareInfo {
2019-06-20 00:40:05 +02:00
"myprovider@auth" : {
2019-05-16 10:58:06 +02:00
Middleware : & config . Middleware {
BasicAuth : & config . BasicAuth {
Users : [ ] string { "admin:admin" } ,
} ,
} ,
} ,
2019-06-20 00:40:05 +02:00
"myprovider@addPrefixTest" : {
2019-05-16 10:58:06 +02:00
Middleware : & config . Middleware {
AddPrefix : & config . AddPrefix {
Prefix : "/titi" ,
} ,
} ,
} ,
2019-06-20 00:40:05 +02:00
"anotherprovider@addPrefixTest" : {
2019-05-16 10:58:06 +02:00
Middleware : & config . Middleware {
AddPrefix : & config . AddPrefix {
Prefix : "/toto" ,
} ,
} ,
} ,
} ,
Routers : map [ string ] * config . RouterInfo {
2019-06-20 00:40:05 +02:00
"myprovider@bar" : {
2019-05-16 10:58:06 +02:00
Router : & config . Router {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-05-16 10:58:06 +02:00
Rule : "Host(`foo.bar`)" ,
2019-06-20 00:40:05 +02:00
Middlewares : [ ] string { "auth" , "anotherprovider@addPrefixTest" } ,
2019-05-16 10:58:06 +02:00
} ,
} ,
2019-06-20 00:40:05 +02:00
"myprovider@test" : {
2019-05-16 10:58:06 +02:00
Router : & config . Router {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-05-16 10:58:06 +02:00
Rule : "Host(`foo.bar.other`)" ,
Middlewares : [ ] string { "addPrefixTest" , "auth" } ,
} ,
} ,
} ,
} ,
expected : config . RuntimeConfiguration {
Routers : map [ string ] * config . RouterInfo {
2019-06-20 00:40:05 +02:00
"myprovider@bar" : { } ,
"myprovider@test" : { } ,
2019-05-16 10:58:06 +02:00
} ,
Services : map [ string ] * config . ServiceInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo-service" : {
UsedBy : [ ] string { "myprovider@bar" , "myprovider@test" } ,
2019-05-16 10:58:06 +02:00
} ,
} ,
Middlewares : map [ string ] * config . MiddlewareInfo {
2019-06-20 00:40:05 +02:00
"myprovider@auth" : {
UsedBy : [ ] string { "myprovider@bar" , "myprovider@test" } ,
2019-05-16 10:58:06 +02:00
} ,
2019-06-20 00:40:05 +02:00
"myprovider@addPrefixTest" : {
UsedBy : [ ] string { "myprovider@test" } ,
2019-05-16 10:58:06 +02:00
} ,
2019-06-20 00:40:05 +02:00
"anotherprovider@addPrefixTest" : {
UsedBy : [ ] string { "myprovider@bar" } ,
2019-05-16 10:58:06 +02:00
} ,
} ,
} ,
} ,
// TCP tests from hereon
{
desc : "TCP, One service used by two routers" ,
conf : & config . RuntimeConfiguration {
TCPRouters : map [ string ] * config . TCPRouterInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo" : {
2019-05-16 10:58:06 +02:00
TCPRouter : & config . TCPRouter {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-05-16 10:58:06 +02:00
Rule : "Host(`bar.foo`)" ,
} ,
} ,
2019-06-20 00:40:05 +02:00
"myprovider@bar" : {
2019-05-16 10:58:06 +02:00
TCPRouter : & config . TCPRouter {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-05-16 10:58:06 +02:00
Rule : "Host(`foo.bar`)" ,
} ,
} ,
} ,
TCPServices : map [ string ] * config . TCPServiceInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo-service" : {
2019-05-16 10:58:06 +02:00
TCPService : & config . TCPService {
LoadBalancer : & config . TCPLoadBalancerService {
Servers : [ ] config . TCPServer {
{
Address : "127.0.0.1" ,
Port : "8085" ,
} ,
{
Address : "127.0.0.1" ,
Port : "8086" ,
} ,
} ,
} ,
} ,
} ,
} ,
} ,
expected : config . RuntimeConfiguration {
TCPRouters : map [ string ] * config . TCPRouterInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo" : { } ,
"myprovider@bar" : { } ,
2019-05-16 10:58:06 +02:00
} ,
TCPServices : map [ string ] * config . TCPServiceInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo-service" : {
UsedBy : [ ] string { "myprovider@bar" , "myprovider@foo" } ,
2019-05-16 10:58:06 +02:00
} ,
} ,
} ,
} ,
{
desc : "TCP, One service used by two routers, but one router with wrong rule" ,
conf : & config . RuntimeConfiguration {
TCPServices : map [ string ] * config . TCPServiceInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo-service" : {
2019-05-16 10:58:06 +02:00
TCPService : & config . TCPService {
LoadBalancer : & config . TCPLoadBalancerService {
Servers : [ ] config . TCPServer {
{
Address : "127.0.0.1" ,
} ,
} ,
} ,
} ,
} ,
} ,
TCPRouters : map [ string ] * config . TCPRouterInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo" : {
2019-05-16 10:58:06 +02:00
TCPRouter : & config . TCPRouter {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-05-16 10:58:06 +02:00
Rule : "WrongRule(`bar.foo`)" ,
} ,
} ,
2019-06-20 00:40:05 +02:00
"myprovider@bar" : {
2019-05-16 10:58:06 +02:00
TCPRouter : & config . TCPRouter {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-05-16 10:58:06 +02:00
Rule : "Host(`foo.bar`)" ,
} ,
} ,
} ,
} ,
expected : config . RuntimeConfiguration {
TCPRouters : map [ string ] * config . TCPRouterInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo" : { } ,
"myprovider@bar" : { } ,
2019-05-16 10:58:06 +02:00
} ,
TCPServices : map [ string ] * config . TCPServiceInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo-service" : {
UsedBy : [ ] string { "myprovider@bar" , "myprovider@foo" } ,
2019-05-16 10:58:06 +02:00
} ,
} ,
} ,
} ,
{
desc : "TCP, Broken Service used by one Router" ,
conf : & config . RuntimeConfiguration {
TCPServices : map [ string ] * config . TCPServiceInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo-service" : {
2019-05-16 10:58:06 +02:00
TCPService : & config . TCPService {
LoadBalancer : nil ,
} ,
} ,
} ,
TCPRouters : map [ string ] * config . TCPRouterInfo {
2019-06-20 00:40:05 +02:00
"myprovider@bar" : {
2019-05-16 10:58:06 +02:00
TCPRouter : & config . TCPRouter {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-05-16 10:58:06 +02:00
Rule : "Host(`foo.bar`)" ,
} ,
} ,
} ,
} ,
expected : config . RuntimeConfiguration {
TCPRouters : map [ string ] * config . TCPRouterInfo {
2019-06-20 00:40:05 +02:00
"myprovider@bar" : { } ,
2019-05-16 10:58:06 +02:00
} ,
TCPServices : map [ string ] * config . TCPServiceInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo-service" : {
UsedBy : [ ] string { "myprovider@bar" } ,
2019-05-16 10:58:06 +02:00
} ,
} ,
} ,
} ,
{
desc : "TCP, 2 different Services each used by a disctinct router." ,
conf : & config . RuntimeConfiguration {
TCPServices : map [ string ] * config . TCPServiceInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo-service" : {
2019-05-16 10:58:06 +02:00
TCPService : & config . TCPService {
LoadBalancer : & config . TCPLoadBalancerService {
Servers : [ ] config . TCPServer {
{
Address : "127.0.0.1" ,
Port : "8085" ,
} ,
{
Address : "127.0.0.1" ,
Port : "8086" ,
} ,
} ,
} ,
} ,
} ,
2019-06-20 00:40:05 +02:00
"myprovider@bar-service" : {
2019-05-16 10:58:06 +02:00
TCPService : & config . TCPService {
LoadBalancer : & config . TCPLoadBalancerService {
Servers : [ ] config . TCPServer {
{
Address : "127.0.0.1" ,
Port : "8087" ,
} ,
{
Address : "127.0.0.1" ,
Port : "8088" ,
} ,
} ,
} ,
} ,
} ,
} ,
TCPRouters : map [ string ] * config . TCPRouterInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo" : {
2019-05-16 10:58:06 +02:00
TCPRouter : & config . TCPRouter {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-05-16 10:58:06 +02:00
Rule : "Host(`bar.foo`)" ,
} ,
} ,
2019-06-20 00:40:05 +02:00
"myprovider@bar" : {
2019-05-16 10:58:06 +02:00
TCPRouter : & config . TCPRouter {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@bar-service" ,
2019-05-16 10:58:06 +02:00
Rule : "Host(`foo.bar`)" ,
} ,
} ,
} ,
} ,
expected : config . RuntimeConfiguration {
TCPRouters : map [ string ] * config . TCPRouterInfo {
2019-06-20 00:40:05 +02:00
"myprovider@bar" : { } ,
"myprovider@foo" : { } ,
2019-05-16 10:58:06 +02:00
} ,
TCPServices : map [ string ] * config . TCPServiceInfo {
2019-06-20 00:40:05 +02:00
"myprovider@foo-service" : {
UsedBy : [ ] string { "myprovider@foo" } ,
2019-05-16 10:58:06 +02:00
} ,
2019-06-20 00:40:05 +02:00
"myprovider@bar-service" : {
UsedBy : [ ] string { "myprovider@bar" } ,
2019-05-16 10:58:06 +02:00
} ,
} ,
} ,
} ,
}
for _ , test := range testCases {
test := test
t . Run ( test . desc , func ( t * testing . T ) {
t . Parallel ( )
runtimeConf := test . conf
runtimeConf . PopulateUsedBy ( )
for key , expectedService := range test . expected . Services {
require . NotNil ( t , runtimeConf . Services [ key ] )
assert . Equal ( t , expectedService . UsedBy , runtimeConf . Services [ key ] . UsedBy )
}
for key , expectedMiddleware := range test . expected . Middlewares {
require . NotNil ( t , runtimeConf . Middlewares [ key ] )
assert . Equal ( t , expectedMiddleware . UsedBy , runtimeConf . Middlewares [ key ] . UsedBy )
}
for key , expectedTCPService := range test . expected . TCPServices {
require . NotNil ( t , runtimeConf . TCPServices [ key ] )
assert . Equal ( t , expectedTCPService . UsedBy , runtimeConf . TCPServices [ key ] . UsedBy )
}
} )
}
}
2019-06-17 18:14:08 +02:00
func TestGetTCPRoutersByEntrypoints ( t * testing . T ) {
testCases := [ ] struct {
desc string
conf config . Configuration
entryPoints [ ] string
expected map [ string ] map [ string ] * config . TCPRouterInfo
} {
{
desc : "Empty Configuration without entrypoint" ,
conf : config . Configuration { } ,
entryPoints : [ ] string { "" } ,
expected : map [ string ] map [ string ] * config . TCPRouterInfo { } ,
} ,
{
desc : "Empty Configuration with unknown entrypoints" ,
conf : config . Configuration { } ,
entryPoints : [ ] string { "foo" } ,
expected : map [ string ] map [ string ] * config . TCPRouterInfo { } ,
} ,
{
desc : "Valid configuration with an unknown entrypoint" ,
conf : config . Configuration {
HTTP : & config . HTTPConfiguration {
Routers : map [ string ] * config . Router {
"foo" : {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-06-17 18:14:08 +02:00
Rule : "Host(`bar.foo`)" ,
} ,
} ,
} ,
TCP : & config . TCPConfiguration {
Routers : map [ string ] * config . TCPRouter {
"foo" : {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-06-17 18:14:08 +02:00
Rule : "HostSNI(`bar.foo`)" ,
} ,
} ,
} ,
} ,
entryPoints : [ ] string { "foo" } ,
expected : map [ string ] map [ string ] * config . TCPRouterInfo { } ,
} ,
{
desc : "Valid configuration with a known entrypoint" ,
conf : config . Configuration {
HTTP : & config . HTTPConfiguration {
Routers : map [ string ] * config . Router {
"foo" : {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-06-17 18:14:08 +02:00
Rule : "Host(`bar.foo`)" ,
} ,
"bar" : {
EntryPoints : [ ] string { "webs" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@bar-service" ,
2019-06-17 18:14:08 +02:00
Rule : "Host(`foo.bar`)" ,
} ,
"foobar" : {
EntryPoints : [ ] string { "web" , "webs" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foobar-service" ,
2019-06-17 18:14:08 +02:00
Rule : "Host(`bar.foobar`)" ,
} ,
} ,
} ,
TCP : & config . TCPConfiguration {
Routers : map [ string ] * config . TCPRouter {
"foo" : {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-06-17 18:14:08 +02:00
Rule : "HostSNI(`bar.foo`)" ,
} ,
"bar" : {
EntryPoints : [ ] string { "webs" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@bar-service" ,
2019-06-17 18:14:08 +02:00
Rule : "HostSNI(`foo.bar`)" ,
} ,
"foobar" : {
EntryPoints : [ ] string { "web" , "webs" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foobar-service" ,
2019-06-17 18:14:08 +02:00
Rule : "HostSNI(`bar.foobar`)" ,
} ,
} ,
} ,
} ,
entryPoints : [ ] string { "web" } ,
expected : map [ string ] map [ string ] * config . TCPRouterInfo {
"web" : {
"foo" : {
TCPRouter : & config . TCPRouter {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-06-17 18:14:08 +02:00
Rule : "HostSNI(`bar.foo`)" ,
} ,
} ,
"foobar" : {
TCPRouter : & config . TCPRouter {
EntryPoints : [ ] string { "web" , "webs" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foobar-service" ,
2019-06-17 18:14:08 +02:00
Rule : "HostSNI(`bar.foobar`)" ,
} ,
} ,
} ,
} ,
} ,
{
desc : "Valid configuration with multiple known entrypoints" ,
conf : config . Configuration {
HTTP : & config . HTTPConfiguration {
Routers : map [ string ] * config . Router {
"foo" : {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-06-17 18:14:08 +02:00
Rule : "Host(`bar.foo`)" ,
} ,
"bar" : {
EntryPoints : [ ] string { "webs" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@bar-service" ,
2019-06-17 18:14:08 +02:00
Rule : "Host(`foo.bar`)" ,
} ,
"foobar" : {
EntryPoints : [ ] string { "web" , "webs" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foobar-service" ,
2019-06-17 18:14:08 +02:00
Rule : "Host(`bar.foobar`)" ,
} ,
} ,
} ,
TCP : & config . TCPConfiguration {
Routers : map [ string ] * config . TCPRouter {
"foo" : {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-06-17 18:14:08 +02:00
Rule : "HostSNI(`bar.foo`)" ,
} ,
"bar" : {
EntryPoints : [ ] string { "webs" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@bar-service" ,
2019-06-17 18:14:08 +02:00
Rule : "HostSNI(`foo.bar`)" ,
} ,
"foobar" : {
EntryPoints : [ ] string { "web" , "webs" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foobar-service" ,
2019-06-17 18:14:08 +02:00
Rule : "HostSNI(`bar.foobar`)" ,
} ,
} ,
} ,
} ,
entryPoints : [ ] string { "web" , "webs" } ,
expected : map [ string ] map [ string ] * config . TCPRouterInfo {
"web" : {
"foo" : {
TCPRouter : & config . TCPRouter {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-06-17 18:14:08 +02:00
Rule : "HostSNI(`bar.foo`)" ,
} ,
} ,
"foobar" : {
TCPRouter : & config . TCPRouter {
EntryPoints : [ ] string { "web" , "webs" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foobar-service" ,
2019-06-17 18:14:08 +02:00
Rule : "HostSNI(`bar.foobar`)" ,
} ,
} ,
} ,
"webs" : {
"bar" : {
TCPRouter : & config . TCPRouter {
EntryPoints : [ ] string { "webs" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@bar-service" ,
2019-06-17 18:14:08 +02:00
Rule : "HostSNI(`foo.bar`)" ,
} ,
} ,
"foobar" : {
TCPRouter : & config . TCPRouter {
EntryPoints : [ ] string { "web" , "webs" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foobar-service" ,
2019-06-17 18:14:08 +02:00
Rule : "HostSNI(`bar.foobar`)" ,
} ,
} ,
} ,
} ,
} ,
}
for _ , test := range testCases {
test := test
t . Run ( test . desc , func ( t * testing . T ) {
t . Parallel ( )
runtimeConfig := config . NewRuntimeConfig ( test . conf )
actual := runtimeConfig . GetTCPRoutersByEntrypoints ( context . Background ( ) , test . entryPoints )
assert . Equal ( t , test . expected , actual )
} )
}
}
func TestGetRoutersByEntrypoints ( t * testing . T ) {
testCases := [ ] struct {
desc string
conf config . Configuration
entryPoints [ ] string
expected map [ string ] map [ string ] * config . RouterInfo
} {
{
desc : "Empty Configuration without entrypoint" ,
conf : config . Configuration { } ,
entryPoints : [ ] string { "" } ,
expected : map [ string ] map [ string ] * config . RouterInfo { } ,
} ,
{
desc : "Empty Configuration with unknown entrypoints" ,
conf : config . Configuration { } ,
entryPoints : [ ] string { "foo" } ,
expected : map [ string ] map [ string ] * config . RouterInfo { } ,
} ,
{
desc : "Valid configuration with an unknown entrypoint" ,
conf : config . Configuration {
HTTP : & config . HTTPConfiguration {
Routers : map [ string ] * config . Router {
"foo" : {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-06-17 18:14:08 +02:00
Rule : "Host(`bar.foo`)" ,
} ,
} ,
} ,
TCP : & config . TCPConfiguration {
Routers : map [ string ] * config . TCPRouter {
"foo" : {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-06-17 18:14:08 +02:00
Rule : "HostSNI(`bar.foo`)" ,
} ,
} ,
} ,
} ,
entryPoints : [ ] string { "foo" } ,
expected : map [ string ] map [ string ] * config . RouterInfo { } ,
} ,
{
desc : "Valid configuration with a known entrypoint" ,
conf : config . Configuration {
HTTP : & config . HTTPConfiguration {
Routers : map [ string ] * config . Router {
"foo" : {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-06-17 18:14:08 +02:00
Rule : "Host(`bar.foo`)" ,
} ,
"bar" : {
EntryPoints : [ ] string { "webs" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@bar-service" ,
2019-06-17 18:14:08 +02:00
Rule : "Host(`foo.bar`)" ,
} ,
"foobar" : {
EntryPoints : [ ] string { "web" , "webs" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foobar-service" ,
2019-06-17 18:14:08 +02:00
Rule : "Host(`bar.foobar`)" ,
} ,
} ,
} ,
TCP : & config . TCPConfiguration {
Routers : map [ string ] * config . TCPRouter {
"foo" : {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-06-17 18:14:08 +02:00
Rule : "HostSNI(`bar.foo`)" ,
} ,
"bar" : {
EntryPoints : [ ] string { "webs" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@bar-service" ,
2019-06-17 18:14:08 +02:00
Rule : "HostSNI(`foo.bar`)" ,
} ,
"foobar" : {
EntryPoints : [ ] string { "web" , "webs" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foobar-service" ,
2019-06-17 18:14:08 +02:00
Rule : "HostSNI(`bar.foobar`)" ,
} ,
} ,
} ,
} ,
entryPoints : [ ] string { "web" } ,
expected : map [ string ] map [ string ] * config . RouterInfo {
"web" : {
"foo" : {
Router : & config . Router {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-06-17 18:14:08 +02:00
Rule : "Host(`bar.foo`)" ,
} ,
} ,
"foobar" : {
Router : & config . Router {
EntryPoints : [ ] string { "web" , "webs" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foobar-service" ,
2019-06-17 18:14:08 +02:00
Rule : "Host(`bar.foobar`)" ,
} ,
} ,
} ,
} ,
} ,
{
desc : "Valid configuration with multiple known entrypoints" ,
conf : config . Configuration {
HTTP : & config . HTTPConfiguration {
Routers : map [ string ] * config . Router {
"foo" : {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-06-17 18:14:08 +02:00
Rule : "Host(`bar.foo`)" ,
} ,
"bar" : {
EntryPoints : [ ] string { "webs" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@bar-service" ,
2019-06-17 18:14:08 +02:00
Rule : "Host(`foo.bar`)" ,
} ,
"foobar" : {
EntryPoints : [ ] string { "web" , "webs" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foobar-service" ,
2019-06-17 18:14:08 +02:00
Rule : "Host(`bar.foobar`)" ,
} ,
} ,
} ,
TCP : & config . TCPConfiguration {
Routers : map [ string ] * config . TCPRouter {
"foo" : {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-06-17 18:14:08 +02:00
Rule : "HostSNI(`bar.foo`)" ,
} ,
"bar" : {
EntryPoints : [ ] string { "webs" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@bar-service" ,
2019-06-17 18:14:08 +02:00
Rule : "HostSNI(`foo.bar`)" ,
} ,
"foobar" : {
EntryPoints : [ ] string { "web" , "webs" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foobar-service" ,
2019-06-17 18:14:08 +02:00
Rule : "HostSNI(`bar.foobar`)" ,
} ,
} ,
} ,
} ,
entryPoints : [ ] string { "web" , "webs" } ,
expected : map [ string ] map [ string ] * config . RouterInfo {
"web" : {
"foo" : {
Router : & config . Router {
EntryPoints : [ ] string { "web" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foo-service" ,
2019-06-17 18:14:08 +02:00
Rule : "Host(`bar.foo`)" ,
} ,
} ,
"foobar" : {
Router : & config . Router {
EntryPoints : [ ] string { "web" , "webs" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foobar-service" ,
2019-06-17 18:14:08 +02:00
Rule : "Host(`bar.foobar`)" ,
} ,
} ,
} ,
"webs" : {
"bar" : {
Router : & config . Router {
EntryPoints : [ ] string { "webs" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@bar-service" ,
2019-06-17 18:14:08 +02:00
Rule : "Host(`foo.bar`)" ,
} ,
} ,
"foobar" : {
Router : & config . Router {
EntryPoints : [ ] string { "web" , "webs" } ,
2019-06-20 00:40:05 +02:00
Service : "myprovider@foobar-service" ,
2019-06-17 18:14:08 +02:00
Rule : "Host(`bar.foobar`)" ,
} ,
} ,
} ,
} ,
} ,
}
for _ , test := range testCases {
test := test
t . Run ( test . desc , func ( t * testing . T ) {
t . Parallel ( )
runtimeConfig := config . NewRuntimeConfig ( test . conf )
actual := runtimeConfig . GetRoutersByEntrypoints ( context . Background ( ) , test . entryPoints , false )
assert . Equal ( t , test . expected , actual )
} )
}
}