2016-04-19 17:23:08 +00:00
|
|
|
package provider
|
2016-04-20 11:26:51 +00:00
|
|
|
|
|
|
|
import (
|
2016-04-26 20:26:25 +00:00
|
|
|
"encoding/json"
|
2016-04-20 11:26:51 +00:00
|
|
|
"github.com/containous/traefik/provider/k8s"
|
|
|
|
"github.com/containous/traefik/types"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLoadIngresses(t *testing.T) {
|
|
|
|
ingresses := []k8s.Ingress{{
|
|
|
|
Spec: k8s.IngressSpec{
|
|
|
|
Rules: []k8s.IngressRule{
|
|
|
|
{
|
|
|
|
Host: "foo",
|
|
|
|
IngressRuleValue: k8s.IngressRuleValue{
|
|
|
|
HTTP: &k8s.HTTPIngressRuleValue{
|
|
|
|
Paths: []k8s.HTTPIngressPath{
|
|
|
|
{
|
|
|
|
Path: "/bar",
|
|
|
|
Backend: k8s.IngressBackend{
|
|
|
|
ServiceName: "service1",
|
|
|
|
ServicePort: k8s.FromInt(801),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Host: "bar",
|
|
|
|
IngressRuleValue: k8s.IngressRuleValue{
|
|
|
|
HTTP: &k8s.HTTPIngressRuleValue{
|
|
|
|
Paths: []k8s.HTTPIngressPath{
|
|
|
|
{
|
|
|
|
Backend: k8s.IngressBackend{
|
|
|
|
ServiceName: "service3",
|
2016-04-26 20:26:25 +00:00
|
|
|
ServicePort: k8s.FromInt(443),
|
2016-04-20 11:26:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Backend: k8s.IngressBackend{
|
|
|
|
ServiceName: "service2",
|
|
|
|
ServicePort: k8s.FromInt(802),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
services := []k8s.Service{
|
|
|
|
{
|
|
|
|
ObjectMeta: k8s.ObjectMeta{
|
|
|
|
Name: "service1",
|
|
|
|
UID: "1",
|
|
|
|
},
|
|
|
|
Spec: k8s.ServiceSpec{
|
|
|
|
ClusterIP: "10.0.0.1",
|
|
|
|
Ports: []k8s.ServicePort{
|
|
|
|
{
|
|
|
|
Name: "http",
|
|
|
|
Port: 801,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ObjectMeta: k8s.ObjectMeta{
|
|
|
|
Name: "service2",
|
|
|
|
UID: "2",
|
|
|
|
},
|
|
|
|
Spec: k8s.ServiceSpec{
|
|
|
|
ClusterIP: "10.0.0.2",
|
|
|
|
Ports: []k8s.ServicePort{
|
|
|
|
{
|
|
|
|
Port: 802,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ObjectMeta: k8s.ObjectMeta{
|
|
|
|
Name: "service3",
|
|
|
|
UID: "3",
|
|
|
|
},
|
|
|
|
Spec: k8s.ServiceSpec{
|
|
|
|
ClusterIP: "10.0.0.3",
|
|
|
|
Ports: []k8s.ServicePort{
|
|
|
|
{
|
|
|
|
Name: "http",
|
2016-04-26 20:26:25 +00:00
|
|
|
Port: 443,
|
2016-04-20 11:26:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
watchChan := make(chan interface{})
|
|
|
|
client := clientMock{
|
|
|
|
ingresses: ingresses,
|
|
|
|
services: services,
|
|
|
|
watchChan: watchChan,
|
|
|
|
}
|
|
|
|
provider := Kubernetes{}
|
|
|
|
actual, err := provider.loadIngresses(client)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := &types.Configuration{
|
|
|
|
Backends: map[string]*types.Backend{
|
|
|
|
"foo/bar": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"1": {
|
|
|
|
URL: "http://10.0.0.1:801",
|
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CircuitBreaker: nil,
|
|
|
|
LoadBalancer: nil,
|
|
|
|
},
|
|
|
|
"bar": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"2": {
|
|
|
|
URL: "http://10.0.0.2:802",
|
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
"3": {
|
2016-04-26 20:26:25 +00:00
|
|
|
URL: "https://10.0.0.3:443",
|
2016-04-20 11:26:51 +00:00
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CircuitBreaker: nil,
|
|
|
|
LoadBalancer: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Frontends: map[string]*types.Frontend{
|
|
|
|
"foo/bar": {
|
|
|
|
Backend: "foo/bar",
|
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"/bar": {
|
2016-04-22 09:33:41 +00:00
|
|
|
Rule: "PathPrefixStrip:/bar",
|
2016-04-20 11:26:51 +00:00
|
|
|
},
|
|
|
|
"foo": {
|
|
|
|
Rule: "Host:foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"bar": {
|
|
|
|
Backend: "bar",
|
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"bar": {
|
|
|
|
Rule: "Host:bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-04-26 20:26:25 +00:00
|
|
|
actualJSON, _ := json.Marshal(actual)
|
|
|
|
expectedJSON, _ := json.Marshal(expected)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("expected %+v, got %+v", string(expectedJSON), string(actualJSON))
|
2016-04-20 11:26:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type clientMock struct {
|
|
|
|
ingresses []k8s.Ingress
|
|
|
|
services []k8s.Service
|
|
|
|
watchChan chan interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c clientMock) GetIngresses(predicate func(k8s.Ingress) bool) ([]k8s.Ingress, error) {
|
|
|
|
return c.ingresses, nil
|
|
|
|
}
|
|
|
|
func (c clientMock) WatchIngresses(predicate func(k8s.Ingress) bool, stopCh <-chan bool) (chan interface{}, chan error, error) {
|
|
|
|
return c.watchChan, make(chan error), nil
|
|
|
|
}
|
2016-04-25 14:56:06 +00:00
|
|
|
func (c clientMock) GetServices(predicate func(k8s.Service) bool) ([]k8s.Service, error) {
|
2016-04-20 11:26:51 +00:00
|
|
|
return c.services, nil
|
|
|
|
}
|
2016-04-25 14:56:06 +00:00
|
|
|
func (c clientMock) WatchAll(stopCh <-chan bool) (chan interface{}, chan error, error) {
|
|
|
|
return c.watchChan, make(chan error), nil
|
|
|
|
}
|