2019-06-27 17:37:43 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2019-07-01 20:23:36 +00:00
|
|
|
"fmt"
|
2019-06-27 17:37:43 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAPI_ConfigEntry_DiscoveryChain(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
config_entries := c.ConfigEntries()
|
|
|
|
|
2019-07-01 20:23:36 +00:00
|
|
|
verifyResolver := func(t *testing.T, initial ConfigEntry) {
|
|
|
|
t.Helper()
|
|
|
|
require.IsType(t, &ServiceResolverConfigEntry{}, initial)
|
|
|
|
testEntry := initial.(*ServiceResolverConfigEntry)
|
2019-06-27 17:37:43 +00:00
|
|
|
|
|
|
|
// set it
|
2019-07-01 20:23:36 +00:00
|
|
|
_, wm, err := config_entries.Set(testEntry, nil)
|
2019-06-27 17:37:43 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, wm)
|
|
|
|
require.NotEqual(t, 0, wm.RequestTime)
|
|
|
|
|
|
|
|
// get it
|
2019-07-01 20:23:36 +00:00
|
|
|
entry, qm, err := config_entries.Get(ServiceResolver, testEntry.Name, nil)
|
2019-06-27 17:37:43 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, qm)
|
|
|
|
require.NotEqual(t, 0, qm.RequestTime)
|
|
|
|
|
|
|
|
// verify it
|
2019-07-01 20:23:36 +00:00
|
|
|
readResolver, ok := entry.(*ServiceResolverConfigEntry)
|
2019-06-27 17:37:43 +00:00
|
|
|
require.True(t, ok)
|
2019-07-01 20:23:36 +00:00
|
|
|
readResolver.ModifyIndex = 0 // reset for Equals()
|
|
|
|
readResolver.CreateIndex = 0 // reset for Equals()
|
2019-06-27 17:37:43 +00:00
|
|
|
|
2019-07-01 20:23:36 +00:00
|
|
|
require.Equal(t, testEntry, readResolver)
|
2019-06-27 17:37:43 +00:00
|
|
|
|
|
|
|
// TODO(rb): cas?
|
|
|
|
// TODO(rb): list?
|
2019-07-01 20:23:36 +00:00
|
|
|
}
|
2019-06-27 17:37:43 +00:00
|
|
|
|
2019-07-01 20:23:36 +00:00
|
|
|
verifySplitter := func(t *testing.T, initial ConfigEntry) {
|
|
|
|
t.Helper()
|
|
|
|
require.IsType(t, &ServiceSplitterConfigEntry{}, initial)
|
|
|
|
testEntry := initial.(*ServiceSplitterConfigEntry)
|
2019-06-27 17:37:43 +00:00
|
|
|
|
|
|
|
// set it
|
2019-07-01 20:23:36 +00:00
|
|
|
_, wm, err := config_entries.Set(testEntry, nil)
|
2019-06-27 17:37:43 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, wm)
|
|
|
|
require.NotEqual(t, 0, wm.RequestTime)
|
|
|
|
|
|
|
|
// get it
|
2019-07-01 20:23:36 +00:00
|
|
|
entry, qm, err := config_entries.Get(ServiceSplitter, testEntry.Name, nil)
|
2019-06-27 17:37:43 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, qm)
|
|
|
|
require.NotEqual(t, 0, qm.RequestTime)
|
|
|
|
|
|
|
|
// verify it
|
|
|
|
readSplitter, ok := entry.(*ServiceSplitterConfigEntry)
|
|
|
|
require.True(t, ok)
|
|
|
|
readSplitter.ModifyIndex = 0 // reset for Equals()
|
|
|
|
readSplitter.CreateIndex = 0 // reset for Equals()
|
|
|
|
|
2019-07-01 20:23:36 +00:00
|
|
|
require.Equal(t, testEntry, readSplitter)
|
2019-06-27 17:37:43 +00:00
|
|
|
|
|
|
|
// TODO(rb): cas?
|
|
|
|
// TODO(rb): list?
|
2019-07-01 20:23:36 +00:00
|
|
|
}
|
2019-06-27 17:37:43 +00:00
|
|
|
|
2019-07-01 20:23:36 +00:00
|
|
|
verifyRouter := func(t *testing.T, initial ConfigEntry) {
|
|
|
|
t.Helper()
|
|
|
|
require.IsType(t, &ServiceRouterConfigEntry{}, initial)
|
|
|
|
testEntry := initial.(*ServiceRouterConfigEntry)
|
|
|
|
|
|
|
|
// set it
|
|
|
|
_, wm, err := config_entries.Set(testEntry, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, wm)
|
|
|
|
require.NotEqual(t, 0, wm.RequestTime)
|
|
|
|
|
|
|
|
// get it
|
|
|
|
entry, qm, err := config_entries.Get(ServiceRouter, testEntry.Name, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, qm)
|
|
|
|
require.NotEqual(t, 0, qm.RequestTime)
|
|
|
|
|
|
|
|
// verify it
|
|
|
|
readRouter, ok := entry.(*ServiceRouterConfigEntry)
|
|
|
|
require.True(t, ok)
|
|
|
|
readRouter.ModifyIndex = 0 // reset for Equals()
|
|
|
|
readRouter.CreateIndex = 0 // reset for Equals()
|
|
|
|
|
|
|
|
require.Equal(t, testEntry, readRouter)
|
|
|
|
|
|
|
|
// TODO(rb): cas?
|
|
|
|
// TODO(rb): list?
|
|
|
|
}
|
|
|
|
|
|
|
|
// First set the necessary protocols to allow advanced routing features.
|
|
|
|
for _, service := range []string{
|
|
|
|
"test-failover",
|
|
|
|
"test-redirect",
|
|
|
|
"alternate",
|
|
|
|
"test-split",
|
|
|
|
"test-route",
|
|
|
|
} {
|
|
|
|
serviceDefaults := &ServiceConfigEntry{
|
|
|
|
Kind: ServiceDefaults,
|
|
|
|
Name: service,
|
|
|
|
Protocol: "http",
|
|
|
|
}
|
|
|
|
_, _, err := config_entries.Set(serviceDefaults, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Due to service graph validation, these have to happen in a specific order.
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
entry ConfigEntry
|
|
|
|
verify func(t *testing.T, initial ConfigEntry)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "failover",
|
|
|
|
entry: &ServiceResolverConfigEntry{
|
2019-06-27 17:37:43 +00:00
|
|
|
Kind: ServiceResolver,
|
2019-07-01 20:23:36 +00:00
|
|
|
Name: "test-failover",
|
2020-01-24 15:04:58 +00:00
|
|
|
Namespace: defaultNamespace,
|
2019-06-27 17:37:43 +00:00
|
|
|
DefaultSubset: "v1",
|
|
|
|
Subsets: map[string]ServiceResolverSubset{
|
2020-06-16 17:19:31 +00:00
|
|
|
"v1": {
|
2019-07-01 20:23:36 +00:00
|
|
|
Filter: "Service.Meta.version == v1",
|
2019-06-27 17:37:43 +00:00
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"v2": {
|
2019-07-01 20:23:36 +00:00
|
|
|
Filter: "Service.Meta.version == v2",
|
2019-06-27 17:37:43 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Failover: map[string]ServiceResolverFailover{
|
2020-06-16 17:19:31 +00:00
|
|
|
"*": {
|
2019-06-27 17:37:43 +00:00
|
|
|
Datacenters: []string{"dc2"},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"v1": {
|
2020-01-24 15:04:58 +00:00
|
|
|
Service: "alternate",
|
|
|
|
Namespace: defaultNamespace,
|
2019-06-27 17:37:43 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
ConnectTimeout: 5 * time.Second,
|
2019-07-01 20:23:36 +00:00
|
|
|
},
|
|
|
|
verify: verifyResolver,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "redirect",
|
|
|
|
entry: &ServiceResolverConfigEntry{
|
2020-01-24 15:04:58 +00:00
|
|
|
Kind: ServiceResolver,
|
|
|
|
Name: "test-redirect",
|
|
|
|
Namespace: defaultNamespace,
|
2019-07-01 20:23:36 +00:00
|
|
|
Redirect: &ServiceResolverRedirect{
|
|
|
|
Service: "test-failover",
|
|
|
|
ServiceSubset: "v2",
|
2020-01-24 15:04:58 +00:00
|
|
|
Namespace: defaultNamespace,
|
2019-07-01 20:23:36 +00:00
|
|
|
Datacenter: "d",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
verify: verifyResolver,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "mega splitter", // use one mega object to avoid multiple trips
|
|
|
|
entry: &ServiceSplitterConfigEntry{
|
2020-01-24 15:04:58 +00:00
|
|
|
Kind: ServiceSplitter,
|
|
|
|
Name: "test-split",
|
|
|
|
Namespace: defaultNamespace,
|
2019-07-01 20:23:36 +00:00
|
|
|
Splits: []ServiceSplit{
|
|
|
|
{
|
|
|
|
Weight: 90,
|
|
|
|
Service: "test-failover",
|
|
|
|
ServiceSubset: "v1",
|
2020-01-24 15:04:58 +00:00
|
|
|
Namespace: defaultNamespace,
|
2019-07-01 20:23:36 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Weight: 10,
|
|
|
|
Service: "test-redirect",
|
2020-01-24 15:04:58 +00:00
|
|
|
Namespace: defaultNamespace,
|
2019-07-01 20:23:36 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
verify: verifySplitter,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "mega router", // use one mega object to avoid multiple trips
|
|
|
|
entry: &ServiceRouterConfigEntry{
|
2020-01-24 15:04:58 +00:00
|
|
|
Kind: ServiceRouter,
|
|
|
|
Name: "test-route",
|
|
|
|
Namespace: defaultNamespace,
|
2019-07-01 20:23:36 +00:00
|
|
|
Routes: []ServiceRoute{
|
|
|
|
{
|
|
|
|
Match: &ServiceRouteMatch{
|
|
|
|
HTTP: &ServiceRouteHTTPMatch{
|
|
|
|
PathPrefix: "/prefix",
|
|
|
|
Header: []ServiceRouteHTTPMatchHeader{
|
|
|
|
{Name: "x-debug", Exact: "1"},
|
|
|
|
},
|
|
|
|
QueryParam: []ServiceRouteHTTPMatchQueryParam{
|
2019-07-24 01:55:26 +00:00
|
|
|
{Name: "debug", Exact: "1"},
|
2019-07-01 20:23:36 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Destination: &ServiceRouteDestination{
|
|
|
|
Service: "test-failover",
|
|
|
|
ServiceSubset: "v2",
|
2020-01-24 15:04:58 +00:00
|
|
|
Namespace: defaultNamespace,
|
2019-07-01 20:23:36 +00:00
|
|
|
PrefixRewrite: "/",
|
|
|
|
RequestTimeout: 5 * time.Second,
|
|
|
|
NumRetries: 5,
|
|
|
|
RetryOnConnectFailure: true,
|
|
|
|
RetryOnStatusCodes: []uint32{500, 503, 401},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
verify: verifyRouter,
|
2019-06-27 17:37:43 +00:00
|
|
|
},
|
|
|
|
} {
|
2019-07-01 20:23:36 +00:00
|
|
|
tc := tc
|
|
|
|
name := fmt.Sprintf("%s:%s: %s", tc.entry.GetKind(), tc.entry.GetName(), tc.name)
|
|
|
|
ok := t.Run(name, func(t *testing.T) {
|
|
|
|
tc.verify(t, tc.entry)
|
2019-06-27 17:37:43 +00:00
|
|
|
})
|
2019-07-01 20:23:36 +00:00
|
|
|
require.True(t, ok, "subtest %q failed so aborting remainder", name)
|
2019-06-27 17:37:43 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-23 00:05:09 +00:00
|
|
|
|
|
|
|
func TestAPI_ConfigEntry_ServiceResolver_LoadBalancer(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
config_entries := c.ConfigEntries()
|
|
|
|
|
|
|
|
verifyResolver := func(t *testing.T, initial ConfigEntry) {
|
|
|
|
t.Helper()
|
|
|
|
require.IsType(t, &ServiceResolverConfigEntry{}, initial)
|
|
|
|
testEntry := initial.(*ServiceResolverConfigEntry)
|
|
|
|
|
|
|
|
// set it
|
|
|
|
_, wm, err := config_entries.Set(testEntry, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, wm)
|
|
|
|
require.NotEqual(t, 0, wm.RequestTime)
|
|
|
|
|
|
|
|
// get it
|
|
|
|
entry, qm, err := config_entries.Get(ServiceResolver, testEntry.Name, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, qm)
|
|
|
|
require.NotEqual(t, 0, qm.RequestTime)
|
|
|
|
|
|
|
|
// verify it
|
|
|
|
readResolver, ok := entry.(*ServiceResolverConfigEntry)
|
|
|
|
require.True(t, ok)
|
|
|
|
readResolver.ModifyIndex = 0 // reset for Equals()
|
|
|
|
readResolver.CreateIndex = 0 // reset for Equals()
|
|
|
|
|
|
|
|
require.Equal(t, testEntry, readResolver)
|
|
|
|
}
|
|
|
|
|
|
|
|
// First set the necessary protocols to allow advanced routing features.
|
|
|
|
for _, service := range []string{
|
|
|
|
"test-least-req",
|
|
|
|
"test-ring-hash",
|
|
|
|
} {
|
|
|
|
serviceDefaults := &ServiceConfigEntry{
|
|
|
|
Kind: ServiceDefaults,
|
|
|
|
Name: service,
|
|
|
|
Protocol: "http",
|
|
|
|
}
|
|
|
|
_, _, err := config_entries.Set(serviceDefaults, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Due to service graph validation, these have to happen in a specific order.
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
entry ConfigEntry
|
|
|
|
verify func(t *testing.T, initial ConfigEntry)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "least-req",
|
|
|
|
entry: &ServiceResolverConfigEntry{
|
|
|
|
Kind: ServiceResolver,
|
|
|
|
Name: "test-least-req",
|
|
|
|
Namespace: defaultNamespace,
|
2020-09-02 15:10:50 +00:00
|
|
|
LoadBalancer: &LoadBalancer{
|
|
|
|
EnvoyLBConfig: &EnvoyLBConfig{
|
|
|
|
Policy: "least_request",
|
|
|
|
LeastRequestConfig: &LeastRequestConfig{ChoiceCount: 10},
|
|
|
|
},
|
2020-08-23 00:05:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
verify: verifyResolver,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ring-hash-with-policies",
|
|
|
|
entry: &ServiceResolverConfigEntry{
|
|
|
|
Kind: ServiceResolver,
|
|
|
|
Name: "test-ring-hash",
|
|
|
|
Namespace: defaultNamespace,
|
2020-09-02 15:10:50 +00:00
|
|
|
LoadBalancer: &LoadBalancer{
|
|
|
|
EnvoyLBConfig: &EnvoyLBConfig{
|
|
|
|
Policy: "ring_hash",
|
|
|
|
RingHashConfig: &RingHashConfig{
|
|
|
|
MinimumRingSize: 1024 * 2,
|
|
|
|
MaximumRingSize: 1024 * 4,
|
2020-08-23 00:05:09 +00:00
|
|
|
},
|
2020-09-02 15:10:50 +00:00
|
|
|
HashPolicies: []HashPolicy{
|
|
|
|
{
|
|
|
|
Field: "header",
|
|
|
|
FieldValue: "my-session-header",
|
|
|
|
Terminal: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Field: "cookie",
|
|
|
|
FieldValue: "oreo",
|
|
|
|
CookieConfig: &CookieConfig{
|
|
|
|
Path: "/tray",
|
|
|
|
TTL: 20 * time.Millisecond,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SourceIP: true,
|
|
|
|
},
|
2020-08-23 00:05:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
verify: verifyResolver,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
tc := tc
|
|
|
|
name := fmt.Sprintf("%s:%s: %s", tc.entry.GetKind(), tc.entry.GetName(), tc.name)
|
|
|
|
ok := t.Run(name, func(t *testing.T) {
|
|
|
|
tc.verify(t, tc.entry)
|
|
|
|
})
|
|
|
|
require.True(t, ok, "subtest %q failed so aborting remainder", name)
|
|
|
|
}
|
|
|
|
}
|