2016-02-02 18:03:40 +01:00
package provider
import (
"reflect"
2016-06-20 19:13:22 +02:00
"sort"
2016-02-02 18:03:40 +01:00
"testing"
2016-02-24 16:43:39 +01:00
"github.com/containous/traefik/types"
2016-02-02 18:03:40 +01:00
"github.com/hashicorp/consul/api"
)
2016-03-27 01:05:17 +01:00
func TestConsulCatalogGetFrontendRule ( t * testing . T ) {
2016-02-02 18:03:40 +01:00
provider := & ConsulCatalog {
Domain : "localhost" ,
}
services := [ ] struct {
2016-04-12 09:49:37 +02:00
service serviceUpdate
2016-02-02 18:03:40 +01:00
expected string
} {
{
2016-04-12 09:49:37 +02:00
service : serviceUpdate {
ServiceName : "foo" ,
Attributes : [ ] string { } ,
} ,
2016-03-27 01:05:17 +01:00
expected : "Host:foo.localhost" ,
2016-02-02 18:03:40 +01:00
} ,
2016-04-12 09:49:37 +02:00
{
service : serviceUpdate {
ServiceName : "foo" ,
Attributes : [ ] string {
"traefik.frontend.rule=Host:*.example.com" ,
} ,
} ,
expected : "Host:*.example.com" ,
} ,
2016-02-02 18:03:40 +01:00
}
for _ , e := range services {
2016-04-12 09:49:37 +02:00
actual := provider . getFrontendRule ( e . service )
if actual != e . expected {
t . Fatalf ( "expected %q, got %q" , e . expected , actual )
}
}
}
func TestConsulCatalogGetAttribute ( t * testing . T ) {
provider := & ConsulCatalog {
Domain : "localhost" ,
}
services := [ ] struct {
tags [ ] string
key string
defaultValue string
expected string
} {
{
tags : [ ] string {
"foo.bar=ramdom" ,
"traefik.backend.weight=42" ,
} ,
key : "backend.weight" ,
defaultValue : "" ,
expected : "42" ,
} ,
{
tags : [ ] string {
"foo.bar=ramdom" ,
"traefik.backend.wei=42" ,
} ,
key : "backend.weight" ,
defaultValue : "" ,
expected : "" ,
} ,
}
for _ , e := range services {
actual := provider . getAttribute ( e . key , e . tags , e . defaultValue )
2016-02-02 18:03:40 +01:00
if actual != e . expected {
t . Fatalf ( "expected %q, got %q" , e . expected , actual )
}
}
}
2016-04-15 09:56:06 +02:00
func TestConsulCatalogGetBackendAddress ( t * testing . T ) {
provider := & ConsulCatalog {
Domain : "localhost" ,
}
services := [ ] struct {
node * api . ServiceEntry
expected string
} {
{
node : & api . ServiceEntry {
Node : & api . Node {
Address : "10.1.0.1" ,
} ,
Service : & api . AgentService {
Address : "10.2.0.1" ,
} ,
} ,
expected : "10.2.0.1" ,
} ,
{
node : & api . ServiceEntry {
Node : & api . Node {
Address : "10.1.0.1" ,
} ,
Service : & api . AgentService {
Address : "" ,
} ,
} ,
expected : "10.1.0.1" ,
} ,
}
for _ , e := range services {
actual := provider . getBackendAddress ( e . node )
if actual != e . expected {
t . Fatalf ( "expected %q, got %q" , e . expected , actual )
}
}
}
func TestConsulCatalogGetBackendName ( t * testing . T ) {
provider := & ConsulCatalog {
Domain : "localhost" ,
}
services := [ ] struct {
node * api . ServiceEntry
expected string
} {
{
node : & api . ServiceEntry {
Service : & api . AgentService {
Service : "api" ,
Address : "10.0.0.1" ,
Port : 80 ,
Tags : [ ] string { } ,
} ,
} ,
expected : "api--10-0-0-1--80--0" ,
} ,
{
node : & api . ServiceEntry {
Service : & api . AgentService {
Service : "api" ,
Address : "10.0.0.1" ,
Port : 80 ,
Tags : [ ] string { "traefik.weight=42" , "traefik.enable=true" } ,
} ,
} ,
expected : "api--10-0-0-1--80--traefik-weight-42--traefik-enable-true--1" ,
} ,
2016-05-02 16:14:21 +02:00
{
node : & api . ServiceEntry {
Service : & api . AgentService {
Service : "api" ,
Address : "10.0.0.1" ,
Port : 80 ,
Tags : [ ] string { "a funny looking tag" } ,
} ,
} ,
expected : "api--10-0-0-1--80--a-funny-looking-tag--2" ,
} ,
2016-04-15 09:56:06 +02:00
}
for i , e := range services {
actual := provider . getBackendName ( e . node , i )
if actual != e . expected {
t . Fatalf ( "expected %q, got %q" , e . expected , actual )
}
}
}
2016-02-02 18:03:40 +01:00
func TestConsulCatalogBuildConfig ( t * testing . T ) {
provider := & ConsulCatalog {
Domain : "localhost" ,
}
cases := [ ] struct {
nodes [ ] catalogUpdate
expectedFrontends map [ string ] * types . Frontend
expectedBackends map [ string ] * types . Backend
} {
{
nodes : [ ] catalogUpdate { } ,
expectedFrontends : map [ string ] * types . Frontend { } ,
expectedBackends : map [ string ] * types . Backend { } ,
} ,
{
nodes : [ ] catalogUpdate {
{
2016-04-12 09:49:37 +02:00
Service : & serviceUpdate {
ServiceName : "test" ,
Attributes : [ ] string { } ,
} ,
2016-02-02 18:03:40 +01:00
} ,
} ,
expectedFrontends : map [ string ] * types . Frontend { } ,
expectedBackends : map [ string ] * types . Backend { } ,
} ,
{
nodes : [ ] catalogUpdate {
{
2016-04-12 09:49:37 +02:00
Service : & serviceUpdate {
ServiceName : "test" ,
Attributes : [ ] string {
"traefik.backend.loadbalancer=drr" ,
"traefik.backend.circuitbreaker=NetworkErrorRatio() > 0.5" ,
"random.foo=bar" ,
2016-08-24 23:46:47 -04:00
"traefik.backend.maxconn.amount=1000" ,
"traefik.backend.maxconn.extractorfunc=client.ip" ,
2016-04-12 09:49:37 +02:00
} ,
} ,
2016-02-02 18:03:40 +01:00
Nodes : [ ] * api . ServiceEntry {
{
Service : & api . AgentService {
Service : "test" ,
2016-04-08 15:08:28 +02:00
Address : "127.0.0.1" ,
2016-02-02 18:03:40 +01:00
Port : 80 ,
2016-04-12 09:49:37 +02:00
Tags : [ ] string {
"traefik.backend.weight=42" ,
"random.foo=bar" ,
"traefik.backend.passHostHeader=true" ,
"traefik.protocol=https" ,
} ,
2016-02-02 18:03:40 +01:00
} ,
Node : & api . Node {
Node : "localhost" ,
Address : "127.0.0.1" ,
} ,
} ,
} ,
} ,
} ,
expectedFrontends : map [ string ] * types . Frontend {
"frontend-test" : {
2016-05-10 07:43:24 -04:00
Backend : "backend-test" ,
PassHostHeader : true ,
2016-02-02 18:03:40 +01:00
Routes : map [ string ] types . Route {
"route-host-test" : {
2016-03-27 01:05:17 +01:00
Rule : "Host:test.localhost" ,
2016-02-02 18:03:40 +01:00
} ,
} ,
} ,
} ,
expectedBackends : map [ string ] * types . Backend {
"backend-test" : {
Servers : map [ string ] types . Server {
2016-04-15 09:56:06 +02:00
"test--127-0-0-1--80--traefik-backend-weight-42--random-foo-bar--traefik-backend-passHostHeader-true--traefik-protocol-https--0" : {
2016-04-12 09:49:37 +02:00
URL : "https://127.0.0.1:80" ,
Weight : 42 ,
2016-02-02 18:03:40 +01:00
} ,
} ,
2016-04-12 09:49:37 +02:00
CircuitBreaker : & types . CircuitBreaker {
Expression : "NetworkErrorRatio() > 0.5" ,
} ,
LoadBalancer : & types . LoadBalancer {
Method : "drr" ,
} ,
2016-08-24 23:46:47 -04:00
MaxConn : & types . MaxConn {
Amount : 1000 ,
ExtractorFunc : "client.ip" ,
} ,
2016-02-02 18:03:40 +01:00
} ,
} ,
} ,
}
for _ , c := range cases {
actualConfig := provider . buildConfig ( c . nodes )
if ! reflect . DeepEqual ( actualConfig . Backends , c . expectedBackends ) {
t . Fatalf ( "expected %#v, got %#v" , c . expectedBackends , actualConfig . Backends )
}
if ! reflect . DeepEqual ( actualConfig . Frontends , c . expectedFrontends ) {
t . Fatalf ( "expected %#v, got %#v" , c . expectedFrontends , actualConfig . Frontends )
}
}
}
2016-06-20 19:13:22 +02:00
func TestConsulCatalogNodeSorter ( t * testing . T ) {
cases := [ ] struct {
nodes [ ] * api . ServiceEntry
expected [ ] * api . ServiceEntry
} {
{
nodes : [ ] * api . ServiceEntry { } ,
expected : [ ] * api . ServiceEntry { } ,
} ,
{
nodes : [ ] * api . ServiceEntry {
{
Service : & api . AgentService {
Service : "foo" ,
Address : "127.0.0.1" ,
Port : 80 ,
} ,
Node : & api . Node {
Node : "localhost" ,
Address : "127.0.0.1" ,
} ,
} ,
} ,
expected : [ ] * api . ServiceEntry {
{
Service : & api . AgentService {
Service : "foo" ,
Address : "127.0.0.1" ,
Port : 80 ,
} ,
Node : & api . Node {
Node : "localhost" ,
Address : "127.0.0.1" ,
} ,
} ,
} ,
} ,
{
nodes : [ ] * api . ServiceEntry {
{
Service : & api . AgentService {
Service : "foo" ,
Address : "127.0.0.2" ,
Port : 80 ,
} ,
Node : & api . Node {
Node : "localhost" ,
Address : "127.0.0.2" ,
} ,
} ,
{
Service : & api . AgentService {
Service : "bar" ,
Address : "127.0.0.2" ,
Port : 81 ,
} ,
Node : & api . Node {
Node : "localhost" ,
Address : "127.0.0.2" ,
} ,
} ,
{
Service : & api . AgentService {
Service : "foo" ,
Address : "127.0.0.1" ,
Port : 80 ,
} ,
Node : & api . Node {
Node : "localhost" ,
Address : "127.0.0.1" ,
} ,
} ,
{
Service : & api . AgentService {
Service : "bar" ,
Address : "127.0.0.2" ,
Port : 80 ,
} ,
Node : & api . Node {
Node : "localhost" ,
Address : "127.0.0.2" ,
} ,
} ,
} ,
expected : [ ] * api . ServiceEntry {
{
Service : & api . AgentService {
Service : "bar" ,
Address : "127.0.0.2" ,
Port : 80 ,
} ,
Node : & api . Node {
Node : "localhost" ,
Address : "127.0.0.2" ,
} ,
} ,
{
Service : & api . AgentService {
Service : "bar" ,
Address : "127.0.0.2" ,
Port : 81 ,
} ,
Node : & api . Node {
Node : "localhost" ,
Address : "127.0.0.2" ,
} ,
} ,
{
Service : & api . AgentService {
Service : "foo" ,
Address : "127.0.0.1" ,
Port : 80 ,
} ,
Node : & api . Node {
Node : "localhost" ,
Address : "127.0.0.1" ,
} ,
} ,
{
Service : & api . AgentService {
Service : "foo" ,
Address : "127.0.0.2" ,
Port : 80 ,
} ,
Node : & api . Node {
Node : "localhost" ,
Address : "127.0.0.2" ,
} ,
} ,
} ,
} ,
{
nodes : [ ] * api . ServiceEntry {
{
Service : & api . AgentService {
Service : "foo" ,
Address : "" ,
Port : 80 ,
} ,
Node : & api . Node {
Node : "localhost" ,
Address : "127.0.0.2" ,
} ,
} ,
{
Service : & api . AgentService {
Service : "foo" ,
Address : "" ,
Port : 80 ,
} ,
Node : & api . Node {
Node : "localhost" ,
Address : "127.0.0.1" ,
} ,
} ,
} ,
expected : [ ] * api . ServiceEntry {
{
Service : & api . AgentService {
Service : "foo" ,
Address : "" ,
Port : 80 ,
} ,
Node : & api . Node {
Node : "localhost" ,
Address : "127.0.0.1" ,
} ,
} ,
{
Service : & api . AgentService {
Service : "foo" ,
Address : "" ,
Port : 80 ,
} ,
Node : & api . Node {
Node : "localhost" ,
Address : "127.0.0.2" ,
} ,
} ,
} ,
} ,
}
for _ , c := range cases {
sort . Sort ( nodeSorter ( c . nodes ) )
actual := c . nodes
if ! reflect . DeepEqual ( actual , c . expected ) {
t . Fatalf ( "expected %q, got %q" , c . expected , actual )
}
}
}