traefik/provider/eureka/config_test.go

183 lines
3.4 KiB
Go
Raw Normal View History

package eureka
2016-08-31 20:43:05 +00:00
import (
2018-02-13 08:20:04 +00:00
"strconv"
2016-08-31 20:43:05 +00:00
"testing"
"github.com/ArthurHlt/go-eureka-client/eureka"
"github.com/containous/traefik/provider/label"
2018-02-13 08:20:04 +00:00
"github.com/stretchr/testify/assert"
2016-08-31 20:43:05 +00:00
)
func TestGetPort(t *testing.T) {
2018-02-13 08:20:04 +00:00
testCases := []struct {
2016-08-31 20:43:05 +00:00
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,
},
},
},
}
2018-02-13 08:20:04 +00:00
for i, test := range testCases {
test := test
t.Run(strconv.Itoa(i), func(t *testing.T) {
t.Parallel()
port := getPort(test.instanceInfo)
assert.Equal(t, test.expectedPort, port)
})
2016-08-31 20:43:05 +00:00
}
}
func TestGetProtocol(t *testing.T) {
2018-02-13 08:20:04 +00:00
testCases := []struct {
2016-08-31 20:43:05 +00:00
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,
},
},
},
}
2018-02-13 08:20:04 +00:00
for i, test := range testCases {
test := test
t.Run(strconv.Itoa(i), func(t *testing.T) {
t.Parallel()
protocol := getProtocol(test.instanceInfo)
assert.Equal(t, test.expectedProtocol, protocol)
})
2016-08-31 20:43:05 +00:00
}
}
func TestGetWeight(t *testing.T) {
2018-02-13 08:20:04 +00:00
testCases := []struct {
2016-08-31 20:43:05 +00:00
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
},
},
},
},
}
2018-02-13 08:20:04 +00:00
for i, test := range testCases {
test := test
t.Run(strconv.Itoa(i), func(t *testing.T) {
t.Parallel()
weight := getWeight(test.instanceInfo)
assert.Equal(t, test.expectedWeight, weight)
})
2016-08-31 20:43:05 +00:00
}
}
func TestGetInstanceId(t *testing.T) {
2018-02-13 08:20:04 +00:00
testCases := []struct {
2016-08-31 20:43:05 +00:00
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{},
},
},
},
}
2018-02-13 08:20:04 +00:00
for i, test := range testCases {
test := test
t.Run(strconv.Itoa(i), func(t *testing.T) {
t.Parallel()
id := getInstanceID(test.instanceInfo)
assert.Equal(t, test.expectedID, id)
})
2016-08-31 20:43:05 +00:00
}
}