traefik/provider/eureka/config_test.go

168 lines
3.2 KiB
Go
Raw Normal View History

package eureka
2016-08-31 20:43:05 +00:00
import (
"testing"
"github.com/ArthurHlt/go-eureka-client/eureka"
"github.com/containous/traefik/provider/label"
2016-08-31 20:43:05 +00:00
)
func TestGetPort(t *testing.T) {
2016-08-31 20:43:05 +00:00
cases := []struct {
expectedPort string
instanceInfo eureka.InstanceInfo
}{
{
expectedPort: "80",
instanceInfo: eureka.InstanceInfo{
SecurePort: &eureka.Port{
Port: 443, Enabled: false,
},
Port: &eureka.Port{
Port: 80, Enabled: true,
},
},
},
{
expectedPort: "443",
instanceInfo: eureka.InstanceInfo{
SecurePort: &eureka.Port{
Port: 443, Enabled: true,
},
Port: &eureka.Port{
Port: 80, Enabled: false,
},
},
},
}
for _, c := range cases {
port := getPort(c.instanceInfo)
2016-08-31 20:43:05 +00:00
if port != c.expectedPort {
t.Fatalf("Should have been %s, got %s", c.expectedPort, port)
}
}
}
func TestGetProtocol(t *testing.T) {
2016-08-31 20:43:05 +00:00
cases := []struct {
expectedProtocol string
instanceInfo eureka.InstanceInfo
}{
{
expectedProtocol: "http",
instanceInfo: eureka.InstanceInfo{
SecurePort: &eureka.Port{
Port: 443, Enabled: false,
},
Port: &eureka.Port{
Port: 80, Enabled: true,
},
},
},
{
expectedProtocol: "https",
instanceInfo: eureka.InstanceInfo{
SecurePort: &eureka.Port{
Port: 443, Enabled: true,
},
Port: &eureka.Port{
Port: 80, Enabled: false,
},
},
},
}
for _, c := range cases {
protocol := getProtocol(c.instanceInfo)
2016-08-31 20:43:05 +00:00
if protocol != c.expectedProtocol {
t.Fatalf("Should have been %s, got %s", c.expectedProtocol, protocol)
}
}
}
func TestGetWeight(t *testing.T) {
2016-08-31 20:43:05 +00:00
cases := []struct {
expectedWeight string
instanceInfo eureka.InstanceInfo
}{
{
expectedWeight: "0",
instanceInfo: eureka.InstanceInfo{
Port: &eureka.Port{
Port: 80, Enabled: true,
},
Metadata: &eureka.MetaData{
Map: map[string]string{},
},
},
},
{
expectedWeight: "10",
instanceInfo: eureka.InstanceInfo{
Port: &eureka.Port{
Port: 80, Enabled: true,
},
Metadata: &eureka.MetaData{
Map: map[string]string{
label.TraefikWeight: "10",
2016-08-31 20:43:05 +00:00
},
},
},
},
}
for _, c := range cases {
weight := getWeight(c.instanceInfo)
2016-08-31 20:43:05 +00:00
if weight != c.expectedWeight {
t.Fatalf("Should have been %s, got %s", c.expectedWeight, weight)
}
}
}
func TestGetInstanceId(t *testing.T) {
2016-08-31 20:43:05 +00:00
cases := []struct {
expectedID string
instanceInfo eureka.InstanceInfo
}{
{
expectedID: "MyInstanceId",
instanceInfo: eureka.InstanceInfo{
IpAddr: "10.11.12.13",
SecurePort: &eureka.Port{
Port: 443, Enabled: false,
},
Port: &eureka.Port{
Port: 80, Enabled: true,
},
Metadata: &eureka.MetaData{
Map: map[string]string{
label.TraefikBackendID: "MyInstanceId",
2016-08-31 20:43:05 +00:00
},
},
},
},
{
expectedID: "10-11-12-13-80",
instanceInfo: eureka.InstanceInfo{
IpAddr: "10.11.12.13",
SecurePort: &eureka.Port{
Port: 443, Enabled: false,
},
Port: &eureka.Port{
Port: 80, Enabled: true,
},
Metadata: &eureka.MetaData{
Map: map[string]string{},
},
},
},
}
for _, c := range cases {
id := getInstanceID(c.instanceInfo)
2016-08-31 20:43:05 +00:00
if id != c.expectedID {
t.Fatalf("Should have been %s, got %s", c.expectedID, id)
}
}
}