2015-11-13 13:50:32 +03:00
package provider
import (
"reflect"
"strings"
"testing"
2016-02-24 18:43:39 +03:00
"github.com/containous/traefik/types"
2016-04-08 15:20:54 +03:00
docker "github.com/docker/engine-api/types"
"github.com/docker/engine-api/types/container"
"github.com/docker/engine-api/types/network"
"github.com/docker/go-connections/nat"
2015-11-13 13:50:32 +03:00
)
func TestDockerGetFrontendName ( t * testing . T ) {
provider := & Docker {
Domain : "docker.localhost" ,
}
containers := [ ] struct {
2016-04-08 15:20:54 +03:00
container docker . ContainerJSON
2015-11-13 13:50:32 +03:00
expected string
} {
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "foo" ,
} ,
Config : & container . Config { } ,
2015-11-13 13:50:32 +03:00
} ,
expected : "Host-foo-docker-localhost" ,
} ,
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "bar" ,
} ,
Config : & container . Config {
2015-11-13 13:50:32 +03:00
Labels : map [ string ] string {
2016-03-27 03:05:17 +03:00
"traefik.frontend.rule" : "Headers:User-Agent,bat/0.1.0" ,
2015-11-13 13:50:32 +03:00
} ,
} ,
} ,
2016-03-27 03:05:17 +03:00
expected : "Headers-User-Agent-bat-0-1-0" ,
2015-11-13 13:50:32 +03:00
} ,
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "test" ,
} ,
Config : & container . Config {
2015-11-13 13:50:32 +03:00
Labels : map [ string ] string {
2016-03-27 03:05:17 +03:00
"traefik.frontend.rule" : "Host:foo.bar" ,
2015-11-13 13:50:32 +03:00
} ,
} ,
} ,
expected : "Host-foo-bar" ,
} ,
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "test" ,
} ,
Config : & container . Config {
2015-11-13 13:50:32 +03:00
Labels : map [ string ] string {
2016-03-27 03:05:17 +03:00
"traefik.frontend.rule" : "Path:/test" ,
2015-11-13 13:50:32 +03:00
} ,
} ,
} ,
2016-03-27 03:05:17 +03:00
expected : "Path-test" ,
2015-11-13 13:50:32 +03:00
} ,
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "test" ,
} ,
Config : & container . Config {
2015-11-13 13:50:32 +03:00
Labels : map [ string ] string {
2016-03-27 03:05:17 +03:00
"traefik.frontend.rule" : "PathPrefix:/test2" ,
2015-11-13 13:50:32 +03:00
} ,
} ,
} ,
2016-03-27 03:05:17 +03:00
expected : "PathPrefix-test2" ,
2015-11-13 13:50:32 +03:00
} ,
}
for _ , e := range containers {
actual := provider . getFrontendName ( e . container )
if actual != e . expected {
t . Fatalf ( "expected %q, got %q" , e . expected , actual )
}
}
}
2016-03-27 03:05:17 +03:00
func TestDockerGetFrontendRule ( t * testing . T ) {
2015-11-13 13:50:32 +03:00
provider := & Docker {
Domain : "docker.localhost" ,
}
containers := [ ] struct {
2016-04-08 15:20:54 +03:00
container docker . ContainerJSON
2015-11-13 13:50:32 +03:00
expected string
} {
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "foo" ,
} ,
Config : & container . Config { } ,
2015-11-13 13:50:32 +03:00
} ,
2016-03-27 03:05:17 +03:00
expected : "Host:foo.docker.localhost" ,
2015-11-13 13:50:32 +03:00
} ,
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "bar" ,
} ,
Config : & container . Config { } ,
2015-11-13 13:50:32 +03:00
} ,
2016-03-27 03:05:17 +03:00
expected : "Host:bar.docker.localhost" ,
2015-11-13 13:50:32 +03:00
} ,
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "test" ,
} ,
Config : & container . Config {
2015-11-13 13:50:32 +03:00
Labels : map [ string ] string {
2016-03-27 03:05:17 +03:00
"traefik.frontend.rule" : "Host:foo.bar" ,
2015-11-13 13:50:32 +03:00
} ,
} ,
} ,
2016-03-27 03:05:17 +03:00
expected : "Host:foo.bar" ,
2015-11-13 13:50:32 +03:00
} ,
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "test" ,
} ,
Config : & container . Config {
2015-11-13 13:50:32 +03:00
Labels : map [ string ] string {
2016-03-27 03:05:17 +03:00
"traefik.frontend.rule" : "Path:/test" ,
2015-11-13 13:50:32 +03:00
} ,
} ,
} ,
2016-03-27 03:05:17 +03:00
expected : "Path:/test" ,
2015-11-13 13:50:32 +03:00
} ,
}
for _ , e := range containers {
actual := provider . getFrontendRule ( e . container )
if actual != e . expected {
t . Fatalf ( "expected %q, got %q" , e . expected , actual )
}
}
}
func TestDockerGetBackend ( t * testing . T ) {
provider := & Docker { }
containers := [ ] struct {
2016-04-08 15:20:54 +03:00
container docker . ContainerJSON
2015-11-13 13:50:32 +03:00
expected string
} {
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "foo" ,
} ,
Config : & container . Config { } ,
2015-11-13 13:50:32 +03:00
} ,
expected : "foo" ,
} ,
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "bar" ,
} ,
Config : & container . Config { } ,
2015-11-13 13:50:32 +03:00
} ,
expected : "bar" ,
} ,
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "test" ,
} ,
Config : & container . Config {
2015-11-13 13:50:32 +03:00
Labels : map [ string ] string {
"traefik.backend" : "foobar" ,
} ,
} ,
} ,
expected : "foobar" ,
} ,
}
for _ , e := range containers {
actual := provider . getBackend ( e . container )
if actual != e . expected {
t . Fatalf ( "expected %q, got %q" , e . expected , actual )
}
}
}
2016-06-01 08:11:17 +03:00
func TestDockerGetIPAddress ( t * testing . T ) { // TODO
provider := & Docker { }
containers := [ ] struct {
container docker . ContainerJSON
expected string
} {
{
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "bar" ,
} ,
Config : & container . Config { } ,
NetworkSettings : & docker . NetworkSettings {
Networks : map [ string ] * network . EndpointSettings {
"testnet" : {
IPAddress : "10.11.12.13" ,
} ,
} ,
} ,
} ,
expected : "10.11.12.13" ,
} ,
{
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "bar" ,
} ,
Config : & container . Config {
Labels : map [ string ] string {
"traefik.docker.network" : "testnet" ,
} ,
} ,
NetworkSettings : & docker . NetworkSettings {
Networks : map [ string ] * network . EndpointSettings {
"nottestnet" : {
IPAddress : "10.11.12.13" ,
} ,
} ,
} ,
} ,
expected : "10.11.12.13" ,
} ,
{
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "bar" ,
} ,
Config : & container . Config {
Labels : map [ string ] string {
"traefik.docker.network" : "testnet2" ,
} ,
} ,
NetworkSettings : & docker . NetworkSettings {
Networks : map [ string ] * network . EndpointSettings {
"testnet1" : {
IPAddress : "10.11.12.13" ,
} ,
"testnet2" : {
IPAddress : "10.11.12.14" ,
} ,
} ,
} ,
} ,
expected : "10.11.12.14" ,
} ,
2016-07-05 12:44:30 +03:00
{
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "bar" ,
HostConfig : & container . HostConfig {
NetworkMode : "host" ,
} ,
} ,
Config : & container . Config {
Labels : map [ string ] string { } ,
} ,
NetworkSettings : & docker . NetworkSettings {
Networks : map [ string ] * network . EndpointSettings {
"testnet1" : {
IPAddress : "10.11.12.13" ,
} ,
"testnet2" : {
IPAddress : "10.11.12.14" ,
} ,
} ,
} ,
} ,
expected : "127.0.0.1" ,
} ,
2016-06-01 08:11:17 +03:00
}
for _ , e := range containers {
actual := provider . getIPAddress ( e . container )
if actual != e . expected {
t . Fatalf ( "expected %q, got %q" , e . expected , actual )
}
}
}
2015-11-13 13:50:32 +03:00
func TestDockerGetPort ( t * testing . T ) {
provider := & Docker { }
containers := [ ] struct {
2016-04-08 15:20:54 +03:00
container docker . ContainerJSON
2015-11-13 13:50:32 +03:00
expected string
} {
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "foo" ,
} ,
Config : & container . Config { } ,
2015-11-13 13:50:32 +03:00
NetworkSettings : & docker . NetworkSettings { } ,
} ,
expected : "" ,
} ,
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "bar" ,
} ,
Config : & container . Config { } ,
2015-11-13 13:50:32 +03:00
NetworkSettings : & docker . NetworkSettings {
2016-04-08 15:20:54 +03:00
NetworkSettingsBase : docker . NetworkSettingsBase {
Ports : nat . PortMap {
"80/tcp" : { } ,
} ,
2015-11-13 13:50:32 +03:00
} ,
} ,
} ,
expected : "80" ,
} ,
// FIXME handle this better..
// {
2016-04-08 15:20:54 +03:00
// container: docker.ContainerJSON{
2015-11-13 13:50:32 +03:00
// Name: "bar",
2016-04-08 15:20:54 +03:00
// Config: &container.Config{},
2015-11-13 13:50:32 +03:00
// NetworkSettings: &docker.NetworkSettings{
// Ports: map[docker.Port][]docker.PortBinding{
// "80/tcp": []docker.PortBinding{},
// "443/tcp": []docker.PortBinding{},
// },
// },
// },
// expected: "80",
// },
2016-05-29 01:16:57 +03:00
{
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "test" ,
} ,
Config : & container . Config {
Labels : map [ string ] string {
"traefik.port" : "8080" ,
} ,
} ,
NetworkSettings : & docker . NetworkSettings { } ,
} ,
expected : "8080" ,
} ,
2015-11-13 13:50:32 +03:00
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "test" ,
} ,
Config : & container . Config {
2015-11-13 13:50:32 +03:00
Labels : map [ string ] string {
"traefik.port" : "8080" ,
} ,
} ,
NetworkSettings : & docker . NetworkSettings {
2016-04-08 15:20:54 +03:00
NetworkSettingsBase : docker . NetworkSettingsBase {
Ports : nat . PortMap {
2016-05-29 01:16:57 +03:00
"80/tcp" : { } ,
2016-04-08 15:20:54 +03:00
} ,
2015-11-13 13:50:32 +03:00
} ,
} ,
} ,
expected : "8080" ,
} ,
}
for _ , e := range containers {
actual := provider . getPort ( e . container )
if actual != e . expected {
t . Fatalf ( "expected %q, got %q" , e . expected , actual )
}
}
}
func TestDockerGetWeight ( t * testing . T ) {
provider := & Docker { }
containers := [ ] struct {
2016-04-08 15:20:54 +03:00
container docker . ContainerJSON
2015-11-13 13:50:32 +03:00
expected string
} {
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "foo" ,
} ,
Config : & container . Config { } ,
2015-11-13 13:50:32 +03:00
} ,
2016-03-27 03:05:17 +03:00
expected : "1" ,
2015-11-13 13:50:32 +03:00
} ,
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "test" ,
} ,
Config : & container . Config {
2015-11-13 13:50:32 +03:00
Labels : map [ string ] string {
"traefik.weight" : "10" ,
} ,
} ,
} ,
expected : "10" ,
} ,
}
for _ , e := range containers {
actual := provider . getWeight ( e . container )
if actual != e . expected {
t . Fatalf ( "expected %q, got %q" , e . expected , actual )
}
}
}
func TestDockerGetDomain ( t * testing . T ) {
provider := & Docker {
Domain : "docker.localhost" ,
}
containers := [ ] struct {
2016-04-08 15:20:54 +03:00
container docker . ContainerJSON
2015-11-13 13:50:32 +03:00
expected string
} {
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "foo" ,
} ,
Config : & container . Config { } ,
2015-11-13 13:50:32 +03:00
} ,
expected : "docker.localhost" ,
} ,
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "test" ,
} ,
Config : & container . Config {
2015-11-13 13:50:32 +03:00
Labels : map [ string ] string {
"traefik.domain" : "foo.bar" ,
} ,
} ,
} ,
expected : "foo.bar" ,
} ,
}
for _ , e := range containers {
actual := provider . getDomain ( e . container )
if actual != e . expected {
t . Fatalf ( "expected %q, got %q" , e . expected , actual )
}
}
}
func TestDockerGetProtocol ( t * testing . T ) {
provider := & Docker { }
containers := [ ] struct {
2016-04-08 15:20:54 +03:00
container docker . ContainerJSON
2015-11-13 13:50:32 +03:00
expected string
} {
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "foo" ,
} ,
Config : & container . Config { } ,
2015-11-13 13:50:32 +03:00
} ,
expected : "http" ,
} ,
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "test" ,
} ,
Config : & container . Config {
2015-11-13 13:50:32 +03:00
Labels : map [ string ] string {
"traefik.protocol" : "https" ,
} ,
} ,
} ,
expected : "https" ,
} ,
}
for _ , e := range containers {
actual := provider . getProtocol ( e . container )
if actual != e . expected {
t . Fatalf ( "expected %q, got %q" , e . expected , actual )
}
}
}
func TestDockerGetPassHostHeader ( t * testing . T ) {
provider := & Docker { }
containers := [ ] struct {
2016-04-08 15:20:54 +03:00
container docker . ContainerJSON
2015-11-13 13:50:32 +03:00
expected string
} {
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "foo" ,
} ,
Config : & container . Config { } ,
2015-11-13 13:50:32 +03:00
} ,
2016-05-10 14:43:24 +03:00
expected : "true" ,
2015-11-13 13:50:32 +03:00
} ,
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "test" ,
} ,
Config : & container . Config {
2015-11-13 13:50:32 +03:00
Labels : map [ string ] string {
2016-05-10 14:43:24 +03:00
"traefik.frontend.passHostHeader" : "false" ,
2015-11-13 13:50:32 +03:00
} ,
} ,
} ,
2016-05-10 14:43:24 +03:00
expected : "false" ,
2015-11-13 13:50:32 +03:00
} ,
}
for _ , e := range containers {
actual := provider . getPassHostHeader ( e . container )
if actual != e . expected {
t . Fatalf ( "expected %q, got %q" , e . expected , actual )
}
}
}
func TestDockerGetLabel ( t * testing . T ) {
containers := [ ] struct {
2016-04-08 15:20:54 +03:00
container docker . ContainerJSON
2015-11-13 13:50:32 +03:00
expected string
} {
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
Config : & container . Config { } ,
2015-11-13 13:50:32 +03:00
} ,
expected : "Label not found:" ,
} ,
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
Config : & container . Config {
2015-11-13 13:50:32 +03:00
Labels : map [ string ] string {
"foo" : "bar" ,
} ,
} ,
} ,
expected : "" ,
} ,
}
for _ , e := range containers {
label , err := getLabel ( e . container , "foo" )
if e . expected != "" {
if err == nil || ! strings . Contains ( err . Error ( ) , e . expected ) {
t . Fatalf ( "expected an error with %q, got %v" , e . expected , err )
}
} else {
if label != "bar" {
t . Fatalf ( "expected label 'bar', got %s" , label )
}
}
}
}
func TestDockerGetLabels ( t * testing . T ) {
containers := [ ] struct {
2016-04-08 15:20:54 +03:00
container docker . ContainerJSON
2015-11-13 13:50:32 +03:00
expectedLabels map [ string ] string
expectedError string
} {
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
Config : & container . Config { } ,
2015-11-13 13:50:32 +03:00
} ,
expectedLabels : map [ string ] string { } ,
expectedError : "Label not found:" ,
} ,
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
Config : & container . Config {
2015-11-13 13:50:32 +03:00
Labels : map [ string ] string {
"foo" : "fooz" ,
} ,
} ,
} ,
expectedLabels : map [ string ] string {
"foo" : "fooz" ,
} ,
expectedError : "Label not found: bar" ,
} ,
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
Config : & container . Config {
2015-11-13 13:50:32 +03:00
Labels : map [ string ] string {
"foo" : "fooz" ,
"bar" : "barz" ,
} ,
} ,
} ,
expectedLabels : map [ string ] string {
"foo" : "fooz" ,
"bar" : "barz" ,
} ,
expectedError : "" ,
} ,
}
for _ , e := range containers {
labels , err := getLabels ( e . container , [ ] string { "foo" , "bar" } )
if ! reflect . DeepEqual ( labels , e . expectedLabels ) {
t . Fatalf ( "expect %v, got %v" , e . expectedLabels , labels )
}
if e . expectedError != "" {
if err == nil || ! strings . Contains ( err . Error ( ) , e . expectedError ) {
t . Fatalf ( "expected an error with %q, got %v" , e . expectedError , err )
}
}
}
}
func TestDockerTraefikFilter ( t * testing . T ) {
2016-06-06 22:59:58 +03:00
provider := Docker { }
2015-11-13 13:50:32 +03:00
containers := [ ] struct {
2016-07-14 12:32:15 +03:00
container docker . ContainerJSON
exposedByDefault bool
expected bool
2015-11-13 13:50:32 +03:00
} {
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "container" ,
} ,
Config : & container . Config { } ,
2015-11-13 13:50:32 +03:00
NetworkSettings : & docker . NetworkSettings { } ,
} ,
2016-07-14 12:32:15 +03:00
exposedByDefault : true ,
expected : false ,
2015-11-13 13:50:32 +03:00
} ,
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "container" ,
} ,
Config : & container . Config {
2015-11-13 13:50:32 +03:00
Labels : map [ string ] string {
"traefik.enable" : "false" ,
} ,
} ,
NetworkSettings : & docker . NetworkSettings {
2016-04-08 15:20:54 +03:00
NetworkSettingsBase : docker . NetworkSettingsBase {
Ports : nat . PortMap {
"80/tcp" : { } ,
} ,
2015-11-13 13:50:32 +03:00
} ,
} ,
} ,
2016-07-14 12:32:15 +03:00
exposedByDefault : true ,
expected : false ,
2015-11-13 13:50:32 +03:00
} ,
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "container" ,
} ,
Config : & container . Config {
2015-11-13 13:50:32 +03:00
Labels : map [ string ] string {
2016-03-27 03:05:17 +03:00
"traefik.frontend.rule" : "Host:foo.bar" ,
2015-11-13 13:50:32 +03:00
} ,
} ,
NetworkSettings : & docker . NetworkSettings {
2016-04-08 15:20:54 +03:00
NetworkSettingsBase : docker . NetworkSettingsBase {
Ports : nat . PortMap {
"80/tcp" : { } ,
} ,
2015-11-13 13:50:32 +03:00
} ,
} ,
} ,
2016-07-14 12:32:15 +03:00
exposedByDefault : true ,
expected : true ,
2015-11-13 13:50:32 +03:00
} ,
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "container" ,
} ,
Config : & container . Config { } ,
2015-11-13 13:50:32 +03:00
NetworkSettings : & docker . NetworkSettings {
2016-04-08 15:20:54 +03:00
NetworkSettingsBase : docker . NetworkSettingsBase {
Ports : nat . PortMap {
"80/tcp" : { } ,
"443/tcp" : { } ,
} ,
2015-11-13 13:50:32 +03:00
} ,
} ,
} ,
2016-07-14 12:32:15 +03:00
exposedByDefault : true ,
expected : false ,
2015-11-13 13:50:32 +03:00
} ,
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "container" ,
} ,
Config : & container . Config { } ,
2015-11-13 13:50:32 +03:00
NetworkSettings : & docker . NetworkSettings {
2016-04-08 15:20:54 +03:00
NetworkSettingsBase : docker . NetworkSettingsBase {
Ports : nat . PortMap {
"80/tcp" : { } ,
} ,
2015-11-13 13:50:32 +03:00
} ,
} ,
} ,
2016-07-14 12:32:15 +03:00
exposedByDefault : true ,
expected : true ,
2015-11-13 13:50:32 +03:00
} ,
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "container" ,
} ,
Config : & container . Config {
2015-11-13 13:50:32 +03:00
Labels : map [ string ] string {
"traefik.port" : "80" ,
} ,
} ,
NetworkSettings : & docker . NetworkSettings {
2016-04-08 15:20:54 +03:00
NetworkSettingsBase : docker . NetworkSettingsBase {
Ports : nat . PortMap {
"80/tcp" : { } ,
"443/tcp" : { } ,
} ,
2015-11-13 13:50:32 +03:00
} ,
} ,
} ,
2016-07-14 12:32:15 +03:00
exposedByDefault : true ,
expected : true ,
2015-11-13 13:50:32 +03:00
} ,
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "container" ,
} ,
Config : & container . Config {
2015-11-13 13:50:32 +03:00
Labels : map [ string ] string {
"traefik.enable" : "true" ,
} ,
} ,
NetworkSettings : & docker . NetworkSettings {
2016-04-08 15:20:54 +03:00
NetworkSettingsBase : docker . NetworkSettingsBase {
Ports : nat . PortMap {
"80/tcp" : { } ,
} ,
2015-11-13 13:50:32 +03:00
} ,
} ,
} ,
2016-07-14 12:32:15 +03:00
exposedByDefault : true ,
expected : true ,
2015-11-13 13:50:32 +03:00
} ,
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "container" ,
} ,
Config : & container . Config {
2015-11-13 13:50:32 +03:00
Labels : map [ string ] string {
"traefik.enable" : "anything" ,
} ,
} ,
NetworkSettings : & docker . NetworkSettings {
2016-04-08 15:20:54 +03:00
NetworkSettingsBase : docker . NetworkSettingsBase {
Ports : nat . PortMap {
"80/tcp" : { } ,
} ,
2015-11-13 13:50:32 +03:00
} ,
} ,
} ,
2016-07-14 12:32:15 +03:00
exposedByDefault : true ,
expected : true ,
2015-11-13 13:50:32 +03:00
} ,
{
2016-04-08 15:20:54 +03:00
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "container" ,
} ,
Config : & container . Config {
2015-11-13 13:50:32 +03:00
Labels : map [ string ] string {
2016-03-27 03:05:17 +03:00
"traefik.frontend.rule" : "Host:foo.bar" ,
2015-11-13 13:50:32 +03:00
} ,
} ,
NetworkSettings : & docker . NetworkSettings {
2016-04-08 15:20:54 +03:00
NetworkSettingsBase : docker . NetworkSettingsBase {
Ports : nat . PortMap {
"80/tcp" : { } ,
} ,
2015-11-13 13:50:32 +03:00
} ,
} ,
} ,
2016-07-14 12:32:15 +03:00
exposedByDefault : true ,
expected : true ,
} ,
{
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "container" ,
} ,
Config : & container . Config { } ,
NetworkSettings : & docker . NetworkSettings {
NetworkSettingsBase : docker . NetworkSettingsBase {
Ports : nat . PortMap {
"80/tcp" : { } ,
} ,
} ,
} ,
} ,
exposedByDefault : false ,
expected : false ,
} ,
{
container : docker . ContainerJSON {
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "container" ,
} ,
Config : & container . Config {
Labels : map [ string ] string {
"traefik.enable" : "true" ,
} ,
} ,
NetworkSettings : & docker . NetworkSettings {
NetworkSettingsBase : docker . NetworkSettingsBase {
Ports : nat . PortMap {
"80/tcp" : { } ,
} ,
} ,
} ,
} ,
exposedByDefault : false ,
expected : true ,
2015-11-13 13:50:32 +03:00
} ,
}
for _ , e := range containers {
2016-07-14 12:32:15 +03:00
actual := provider . containerFilter ( e . container , e . exposedByDefault )
2015-11-13 13:50:32 +03:00
if actual != e . expected {
2016-03-27 03:05:17 +03:00
t . Fatalf ( "expected %v for %+v, got %+v" , e . expected , e , actual )
2015-11-13 13:50:32 +03:00
}
}
}
func TestDockerLoadDockerConfig ( t * testing . T ) {
cases := [ ] struct {
2016-04-08 15:20:54 +03:00
containers [ ] docker . ContainerJSON
2015-11-13 13:50:32 +03:00
expectedFrontends map [ string ] * types . Frontend
expectedBackends map [ string ] * types . Backend
} {
{
2016-04-08 15:20:54 +03:00
containers : [ ] docker . ContainerJSON { } ,
2015-11-13 13:50:32 +03:00
expectedFrontends : map [ string ] * types . Frontend { } ,
expectedBackends : map [ string ] * types . Backend { } ,
} ,
{
2016-04-08 15:20:54 +03:00
containers : [ ] docker . ContainerJSON {
2015-11-13 13:50:32 +03:00
{
2016-04-08 15:20:54 +03:00
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "test" ,
} ,
Config : & container . Config { } ,
2015-11-13 13:50:32 +03:00
NetworkSettings : & docker . NetworkSettings {
2016-04-08 15:20:54 +03:00
NetworkSettingsBase : docker . NetworkSettingsBase {
Ports : nat . PortMap {
"80/tcp" : { } ,
} ,
2015-11-13 13:50:32 +03:00
} ,
2016-04-08 15:20:54 +03:00
Networks : map [ string ] * network . EndpointSettings {
"bridge" : {
2016-02-27 15:04:33 +03:00
IPAddress : "127.0.0.1" ,
} ,
} ,
2015-11-13 13:50:32 +03:00
} ,
} ,
} ,
expectedFrontends : map [ string ] * types . Frontend {
2016-02-08 23:57:32 +03:00
"frontend-Host-test-docker-localhost" : {
2016-05-10 14:43:24 +03:00
Backend : "backend-test" ,
PassHostHeader : true ,
EntryPoints : [ ] string { } ,
2015-11-13 13:50:32 +03:00
Routes : map [ string ] types . Route {
2016-02-08 23:57:32 +03:00
"route-frontend-Host-test-docker-localhost" : {
2016-03-27 03:05:17 +03:00
Rule : "Host:test.docker.localhost" ,
2015-11-13 13:50:32 +03:00
} ,
} ,
} ,
} ,
expectedBackends : map [ string ] * types . Backend {
"backend-test" : {
Servers : map [ string ] types . Server {
"server-test" : {
2016-03-27 03:05:17 +03:00
URL : "http://127.0.0.1:80" ,
Weight : 1 ,
2015-11-13 13:50:32 +03:00
} ,
} ,
CircuitBreaker : nil ,
LoadBalancer : nil ,
} ,
} ,
} ,
{
2016-04-08 15:20:54 +03:00
containers : [ ] docker . ContainerJSON {
2015-11-13 13:50:32 +03:00
{
2016-04-08 15:20:54 +03:00
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "test1" ,
} ,
Config : & container . Config {
2015-11-13 13:50:32 +03:00
Labels : map [ string ] string {
2016-02-01 13:07:05 +03:00
"traefik.backend" : "foobar" ,
"traefik.frontend.entryPoints" : "http,https" ,
2015-11-13 13:50:32 +03:00
} ,
} ,
NetworkSettings : & docker . NetworkSettings {
2016-04-08 15:20:54 +03:00
NetworkSettingsBase : docker . NetworkSettingsBase {
Ports : nat . PortMap {
"80/tcp" : { } ,
} ,
2015-11-13 13:50:32 +03:00
} ,
2016-04-08 15:20:54 +03:00
Networks : map [ string ] * network . EndpointSettings {
"bridge" : {
2016-02-27 15:04:33 +03:00
IPAddress : "127.0.0.1" ,
} ,
} ,
2015-11-13 13:50:32 +03:00
} ,
} ,
{
2016-04-08 15:20:54 +03:00
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "test2" ,
} ,
Config : & container . Config {
2015-11-13 13:50:32 +03:00
Labels : map [ string ] string {
"traefik.backend" : "foobar" ,
} ,
} ,
NetworkSettings : & docker . NetworkSettings {
2016-04-08 15:20:54 +03:00
NetworkSettingsBase : docker . NetworkSettingsBase {
Ports : nat . PortMap {
"80/tcp" : { } ,
} ,
2015-11-13 13:50:32 +03:00
} ,
2016-04-08 15:20:54 +03:00
Networks : map [ string ] * network . EndpointSettings {
2016-03-27 03:05:17 +03:00
"bridge" : {
2016-02-27 15:04:33 +03:00
IPAddress : "127.0.0.1" ,
} ,
} ,
2015-11-13 13:50:32 +03:00
} ,
} ,
} ,
expectedFrontends : map [ string ] * types . Frontend {
2016-02-08 23:57:32 +03:00
"frontend-Host-test1-docker-localhost" : {
2016-05-10 14:43:24 +03:00
Backend : "backend-foobar" ,
PassHostHeader : true ,
EntryPoints : [ ] string { "http" , "https" } ,
2015-11-13 13:50:32 +03:00
Routes : map [ string ] types . Route {
2016-02-08 23:57:32 +03:00
"route-frontend-Host-test1-docker-localhost" : {
2016-03-27 03:05:17 +03:00
Rule : "Host:test1.docker.localhost" ,
2015-11-13 13:50:32 +03:00
} ,
} ,
} ,
2016-02-08 23:57:32 +03:00
"frontend-Host-test2-docker-localhost" : {
2016-05-10 14:43:24 +03:00
Backend : "backend-foobar" ,
PassHostHeader : true ,
EntryPoints : [ ] string { } ,
2015-11-13 13:50:32 +03:00
Routes : map [ string ] types . Route {
2016-02-08 23:57:32 +03:00
"route-frontend-Host-test2-docker-localhost" : {
2016-03-27 03:05:17 +03:00
Rule : "Host:test2.docker.localhost" ,
2015-11-13 13:50:32 +03:00
} ,
} ,
} ,
} ,
expectedBackends : map [ string ] * types . Backend {
"backend-foobar" : {
Servers : map [ string ] types . Server {
"server-test1" : {
2016-03-27 03:05:17 +03:00
URL : "http://127.0.0.1:80" ,
Weight : 1 ,
2015-11-13 13:50:32 +03:00
} ,
"server-test2" : {
2016-03-27 03:05:17 +03:00
URL : "http://127.0.0.1:80" ,
Weight : 1 ,
2015-11-13 13:50:32 +03:00
} ,
} ,
CircuitBreaker : nil ,
LoadBalancer : nil ,
} ,
} ,
} ,
2016-08-25 07:22:06 +03:00
{
containers : [ ] docker . ContainerJSON {
{
ContainerJSONBase : & docker . ContainerJSONBase {
Name : "test1" ,
} ,
Config : & container . Config {
Labels : map [ string ] string {
"traefik.backend" : "foobar" ,
"traefik.frontend.entryPoints" : "http,https" ,
"traefik.backend.maxconn.amount" : "1000" ,
"traefik.backend.maxconn.extractorfunc" : "somethingelse" ,
"traefik.backend.loadbalancer.method" : "drr" ,
"traefik.backend.circuitbreaker.expression" : "NetworkErrorRatio() > 0.5" ,
} ,
} ,
NetworkSettings : & docker . NetworkSettings {
NetworkSettingsBase : docker . NetworkSettingsBase {
Ports : nat . PortMap {
"80/tcp" : { } ,
} ,
} ,
Networks : map [ string ] * network . EndpointSettings {
"bridge" : {
IPAddress : "127.0.0.1" ,
} ,
} ,
} ,
} ,
} ,
expectedFrontends : map [ string ] * types . Frontend {
"frontend-Host-test1-docker-localhost" : {
Backend : "backend-foobar" ,
PassHostHeader : true ,
EntryPoints : [ ] string { "http" , "https" } ,
Routes : map [ string ] types . Route {
"route-frontend-Host-test1-docker-localhost" : {
Rule : "Host:test1.docker.localhost" ,
} ,
} ,
} ,
} ,
expectedBackends : map [ string ] * types . Backend {
"backend-foobar" : {
Servers : map [ string ] types . Server {
"server-test1" : {
URL : "http://127.0.0.1:80" ,
Weight : 1 ,
} ,
} ,
CircuitBreaker : & types . CircuitBreaker {
Expression : "NetworkErrorRatio() > 0.5" ,
} ,
LoadBalancer : & types . LoadBalancer {
Method : "drr" ,
} ,
MaxConn : & types . MaxConn {
Amount : 1000 ,
ExtractorFunc : "somethingelse" ,
} ,
} ,
} ,
} ,
2015-11-13 13:50:32 +03:00
}
provider := & Docker {
2016-07-14 12:32:15 +03:00
Domain : "docker.localhost" ,
ExposedByDefault : true ,
2015-11-13 13:50:32 +03:00
}
for _ , c := range cases {
actualConfig := provider . loadDockerConfig ( c . containers )
// Compare backends
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 )
}
}
}