2019-03-19 22:56:17 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2019-07-01 20:23:36 +00:00
|
|
|
"time"
|
2019-03-19 22:56:17 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2019-04-07 06:38:08 +00:00
|
|
|
memdb "github.com/hashicorp/go-memdb"
|
2019-03-27 23:52:38 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-03-19 22:56:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStore_ConfigEntry(t *testing.T) {
|
2019-03-27 23:52:38 +00:00
|
|
|
require := require.New(t)
|
2019-03-19 22:56:17 +00:00
|
|
|
s := testStateStore(t)
|
|
|
|
|
|
|
|
expected := &structs.ProxyConfigEntry{
|
|
|
|
Kind: structs.ProxyDefaults,
|
|
|
|
Name: "global",
|
2019-03-27 23:52:38 +00:00
|
|
|
Config: map[string]interface{}{
|
|
|
|
"DestinationServiceName": "foo",
|
2019-03-19 22:56:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create
|
2020-01-24 15:04:58 +00:00
|
|
|
require.NoError(s.EnsureConfigEntry(0, expected, nil))
|
2019-03-19 22:56:17 +00:00
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
idx, config, err := s.ConfigEntry(nil, structs.ProxyDefaults, "global", nil)
|
2019-03-27 23:52:38 +00:00
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(uint64(0), idx)
|
|
|
|
require.Equal(expected, config)
|
2019-03-19 22:56:17 +00:00
|
|
|
|
|
|
|
// Update
|
|
|
|
updated := &structs.ProxyConfigEntry{
|
|
|
|
Kind: structs.ProxyDefaults,
|
|
|
|
Name: "global",
|
2019-03-27 23:52:38 +00:00
|
|
|
Config: map[string]interface{}{
|
|
|
|
"DestinationServiceName": "bar",
|
2019-03-19 22:56:17 +00:00
|
|
|
},
|
|
|
|
}
|
2020-01-24 15:04:58 +00:00
|
|
|
require.NoError(s.EnsureConfigEntry(1, updated, nil))
|
2019-03-19 22:56:17 +00:00
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
idx, config, err = s.ConfigEntry(nil, structs.ProxyDefaults, "global", nil)
|
2019-03-27 23:52:38 +00:00
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(uint64(1), idx)
|
|
|
|
require.Equal(updated, config)
|
2019-03-19 22:56:17 +00:00
|
|
|
|
|
|
|
// Delete
|
2020-01-24 15:04:58 +00:00
|
|
|
require.NoError(s.DeleteConfigEntry(2, structs.ProxyDefaults, "global", nil))
|
2019-03-27 23:52:38 +00:00
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
idx, config, err = s.ConfigEntry(nil, structs.ProxyDefaults, "global", nil)
|
2019-03-27 23:52:38 +00:00
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(uint64(2), idx)
|
|
|
|
require.Nil(config)
|
2019-04-07 06:38:08 +00:00
|
|
|
|
|
|
|
// Set up a watch.
|
|
|
|
serviceConf := &structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "foo",
|
|
|
|
}
|
2020-01-24 15:04:58 +00:00
|
|
|
require.NoError(s.EnsureConfigEntry(3, serviceConf, nil))
|
2019-04-07 06:38:08 +00:00
|
|
|
|
|
|
|
ws := memdb.NewWatchSet()
|
2020-01-24 15:04:58 +00:00
|
|
|
_, _, err = s.ConfigEntry(ws, structs.ServiceDefaults, "foo", nil)
|
2019-04-07 06:38:08 +00:00
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// Make an unrelated modification and make sure the watch doesn't fire.
|
2020-01-24 15:04:58 +00:00
|
|
|
require.NoError(s.EnsureConfigEntry(4, updated, nil))
|
2019-04-07 06:38:08 +00:00
|
|
|
require.False(watchFired(ws))
|
|
|
|
|
|
|
|
// Update the watched config and make sure it fires.
|
|
|
|
serviceConf.Protocol = "http"
|
2020-01-24 15:04:58 +00:00
|
|
|
require.NoError(s.EnsureConfigEntry(5, serviceConf, nil))
|
2019-04-07 06:38:08 +00:00
|
|
|
require.True(watchFired(ws))
|
2019-03-27 23:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStore_ConfigEntryCAS(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
s := testStateStore(t)
|
|
|
|
|
|
|
|
expected := &structs.ProxyConfigEntry{
|
|
|
|
Kind: structs.ProxyDefaults,
|
|
|
|
Name: "global",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"DestinationServiceName": "foo",
|
|
|
|
},
|
2019-03-19 22:56:17 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 23:52:38 +00:00
|
|
|
// Create
|
2020-01-24 15:04:58 +00:00
|
|
|
require.NoError(s.EnsureConfigEntry(1, expected, nil))
|
2019-03-27 23:52:38 +00:00
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
idx, config, err := s.ConfigEntry(nil, structs.ProxyDefaults, "global", nil)
|
2019-03-27 23:52:38 +00:00
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(uint64(1), idx)
|
|
|
|
require.Equal(expected, config)
|
|
|
|
|
|
|
|
// Update with invalid index
|
|
|
|
updated := &structs.ProxyConfigEntry{
|
|
|
|
Kind: structs.ProxyDefaults,
|
|
|
|
Name: "global",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"DestinationServiceName": "bar",
|
|
|
|
},
|
|
|
|
}
|
2020-01-24 15:04:58 +00:00
|
|
|
ok, err := s.EnsureConfigEntryCAS(2, 99, updated, nil)
|
2019-03-27 23:52:38 +00:00
|
|
|
require.False(ok)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// Entry should not be changed
|
2020-01-24 15:04:58 +00:00
|
|
|
idx, config, err = s.ConfigEntry(nil, structs.ProxyDefaults, "global", nil)
|
2019-03-27 23:52:38 +00:00
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(uint64(1), idx)
|
|
|
|
require.Equal(expected, config)
|
|
|
|
|
|
|
|
// Update with a valid index
|
2020-01-24 15:04:58 +00:00
|
|
|
ok, err = s.EnsureConfigEntryCAS(2, 1, updated, nil)
|
2019-03-27 23:52:38 +00:00
|
|
|
require.True(ok)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// Entry should be updated
|
2020-01-24 15:04:58 +00:00
|
|
|
idx, config, err = s.ConfigEntry(nil, structs.ProxyDefaults, "global", nil)
|
2019-03-27 23:52:38 +00:00
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(uint64(2), idx)
|
|
|
|
require.Equal(updated, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStore_ConfigEntries(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
s := testStateStore(t)
|
|
|
|
|
|
|
|
// Create some config entries.
|
|
|
|
entry1 := &structs.ProxyConfigEntry{
|
|
|
|
Kind: structs.ProxyDefaults,
|
|
|
|
Name: "test1",
|
2019-03-19 22:56:17 +00:00
|
|
|
}
|
2019-03-27 23:52:38 +00:00
|
|
|
entry2 := &structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "test2",
|
2019-03-19 22:56:17 +00:00
|
|
|
}
|
2019-04-07 06:38:08 +00:00
|
|
|
entry3 := &structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "test3",
|
|
|
|
}
|
2019-03-27 23:52:38 +00:00
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
require.NoError(s.EnsureConfigEntry(0, entry1, nil))
|
|
|
|
require.NoError(s.EnsureConfigEntry(1, entry2, nil))
|
|
|
|
require.NoError(s.EnsureConfigEntry(2, entry3, nil))
|
2019-03-27 23:52:38 +00:00
|
|
|
|
|
|
|
// Get all entries
|
2020-01-24 15:04:58 +00:00
|
|
|
idx, entries, err := s.ConfigEntries(nil, nil)
|
2019-03-27 23:52:38 +00:00
|
|
|
require.NoError(err)
|
2019-04-07 06:38:08 +00:00
|
|
|
require.Equal(uint64(2), idx)
|
|
|
|
require.Equal([]structs.ConfigEntry{entry1, entry2, entry3}, entries)
|
2019-03-27 23:52:38 +00:00
|
|
|
|
|
|
|
// Get all proxy entries
|
2020-01-24 15:04:58 +00:00
|
|
|
idx, entries, err = s.ConfigEntriesByKind(nil, structs.ProxyDefaults, nil)
|
2019-03-27 23:52:38 +00:00
|
|
|
require.NoError(err)
|
2019-04-07 06:38:08 +00:00
|
|
|
require.Equal(uint64(2), idx)
|
2019-03-27 23:52:38 +00:00
|
|
|
require.Equal([]structs.ConfigEntry{entry1}, entries)
|
|
|
|
|
|
|
|
// Get all service entries
|
2019-04-07 06:38:08 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
2020-01-24 15:04:58 +00:00
|
|
|
idx, entries, err = s.ConfigEntriesByKind(ws, structs.ServiceDefaults, nil)
|
2019-03-27 23:52:38 +00:00
|
|
|
require.NoError(err)
|
2019-04-07 06:38:08 +00:00
|
|
|
require.Equal(uint64(2), idx)
|
|
|
|
require.Equal([]structs.ConfigEntry{entry2, entry3}, entries)
|
|
|
|
|
|
|
|
// Watch should not have fired
|
|
|
|
require.False(watchFired(ws))
|
|
|
|
|
|
|
|
// Now make an update and make sure the watch fires.
|
|
|
|
require.NoError(s.EnsureConfigEntry(3, &structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "test2",
|
|
|
|
Protocol: "tcp",
|
2020-01-24 15:04:58 +00:00
|
|
|
}, nil))
|
2019-04-07 06:38:08 +00:00
|
|
|
require.True(watchFired(ws))
|
2019-03-19 22:56:17 +00:00
|
|
|
}
|
2019-07-01 20:23:36 +00:00
|
|
|
|
|
|
|
func TestStore_ConfigEntry_GraphValidation(t *testing.T) {
|
2020-01-24 15:04:58 +00:00
|
|
|
type tcase struct {
|
2019-07-01 20:23:36 +00:00
|
|
|
entries []structs.ConfigEntry
|
|
|
|
op func(t *testing.T, s *Store) error
|
|
|
|
expectErr string
|
|
|
|
expectGraphErr bool
|
2020-01-24 15:04:58 +00:00
|
|
|
}
|
|
|
|
cases := map[string]tcase{
|
2020-06-16 17:19:31 +00:00
|
|
|
"splitter fails without default protocol": {
|
2019-07-01 20:23:36 +00:00
|
|
|
entries: []structs.ConfigEntry{},
|
|
|
|
op: func(t *testing.T, s *Store) error {
|
|
|
|
entry := &structs.ServiceSplitterConfigEntry{
|
|
|
|
Kind: structs.ServiceSplitter,
|
|
|
|
Name: "main",
|
|
|
|
Splits: []structs.ServiceSplit{
|
2020-02-06 15:52:25 +00:00
|
|
|
{Weight: 100},
|
2019-07-01 20:23:36 +00:00
|
|
|
},
|
|
|
|
}
|
2020-01-24 15:04:58 +00:00
|
|
|
return s.EnsureConfigEntry(0, entry, nil)
|
2019-07-01 20:23:36 +00:00
|
|
|
},
|
|
|
|
expectErr: "does not permit advanced routing or splitting behavior",
|
|
|
|
expectGraphErr: true,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"splitter fails with tcp protocol": {
|
2019-07-01 20:23:36 +00:00
|
|
|
entries: []structs.ConfigEntry{
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "main",
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
op: func(t *testing.T, s *Store) error {
|
|
|
|
entry := &structs.ServiceSplitterConfigEntry{
|
|
|
|
Kind: structs.ServiceSplitter,
|
|
|
|
Name: "main",
|
|
|
|
Splits: []structs.ServiceSplit{
|
2020-02-06 15:52:25 +00:00
|
|
|
{Weight: 100},
|
2019-07-01 20:23:36 +00:00
|
|
|
},
|
|
|
|
}
|
2020-01-24 15:04:58 +00:00
|
|
|
return s.EnsureConfigEntry(0, entry, nil)
|
2019-07-01 20:23:36 +00:00
|
|
|
},
|
|
|
|
expectErr: "does not permit advanced routing or splitting behavior",
|
|
|
|
expectGraphErr: true,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"splitter works with http protocol": {
|
2019-07-02 16:01:17 +00:00
|
|
|
entries: []structs.ConfigEntry{
|
|
|
|
&structs.ProxyConfigEntry{
|
|
|
|
Kind: structs.ProxyDefaults,
|
|
|
|
Name: structs.ProxyConfigGlobal,
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"protocol": "tcp", // loses
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.ServiceConfigEntry{
|
2020-01-24 15:04:58 +00:00
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "main",
|
|
|
|
Protocol: "http",
|
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMeta(),
|
|
|
|
},
|
|
|
|
&structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "main",
|
|
|
|
Subsets: map[string]structs.ServiceResolverSubset{
|
2020-06-16 17:19:31 +00:00
|
|
|
"v1": {
|
2020-01-24 15:04:58 +00:00
|
|
|
Filter: "Service.Meta.version == v1",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"v2": {
|
2020-01-24 15:04:58 +00:00
|
|
|
Filter: "Service.Meta.version == v2",
|
|
|
|
},
|
|
|
|
},
|
2019-07-02 16:01:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
op: func(t *testing.T, s *Store) error {
|
|
|
|
entry := &structs.ServiceSplitterConfigEntry{
|
|
|
|
Kind: structs.ServiceSplitter,
|
|
|
|
Name: "main",
|
|
|
|
Splits: []structs.ServiceSplit{
|
2020-01-24 15:04:58 +00:00
|
|
|
{Weight: 90, ServiceSubset: "v1"},
|
|
|
|
{Weight: 10, ServiceSubset: "v2"},
|
2019-07-02 16:01:17 +00:00
|
|
|
},
|
2020-01-24 15:04:58 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMeta(),
|
2019-07-02 16:01:17 +00:00
|
|
|
}
|
2020-01-24 15:04:58 +00:00
|
|
|
return s.EnsureConfigEntry(0, entry, nil)
|
2019-07-02 16:01:17 +00:00
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"splitter works with http protocol (from proxy-defaults)": {
|
2019-07-02 16:01:17 +00:00
|
|
|
entries: []structs.ConfigEntry{
|
|
|
|
&structs.ProxyConfigEntry{
|
|
|
|
Kind: structs.ProxyDefaults,
|
|
|
|
Name: structs.ProxyConfigGlobal,
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"protocol": "http",
|
|
|
|
},
|
|
|
|
},
|
2020-02-06 15:52:25 +00:00
|
|
|
&structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "main",
|
|
|
|
Subsets: map[string]structs.ServiceResolverSubset{
|
2020-06-16 17:19:31 +00:00
|
|
|
"v1": {
|
2020-02-06 15:52:25 +00:00
|
|
|
Filter: "Service.Meta.version == v1",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"v2": {
|
2020-02-06 15:52:25 +00:00
|
|
|
Filter: "Service.Meta.version == v2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-02 16:01:17 +00:00
|
|
|
},
|
|
|
|
op: func(t *testing.T, s *Store) error {
|
|
|
|
entry := &structs.ServiceSplitterConfigEntry{
|
|
|
|
Kind: structs.ServiceSplitter,
|
|
|
|
Name: "main",
|
|
|
|
Splits: []structs.ServiceSplit{
|
2020-02-06 15:52:25 +00:00
|
|
|
{Weight: 90, ServiceSubset: "v1"},
|
|
|
|
{Weight: 10, ServiceSubset: "v2"},
|
2019-07-02 16:01:17 +00:00
|
|
|
},
|
|
|
|
}
|
2020-01-24 15:04:58 +00:00
|
|
|
return s.EnsureConfigEntry(0, entry, nil)
|
2019-07-02 16:01:17 +00:00
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"router fails with tcp protocol": {
|
2019-07-01 20:23:36 +00:00
|
|
|
entries: []structs.ConfigEntry{
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "main",
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
2020-02-06 15:52:25 +00:00
|
|
|
&structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "main",
|
|
|
|
Subsets: map[string]structs.ServiceResolverSubset{
|
2020-06-16 17:19:31 +00:00
|
|
|
"other": {
|
2020-02-06 15:52:25 +00:00
|
|
|
Filter: "Service.Meta.version == other",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-01 20:23:36 +00:00
|
|
|
},
|
|
|
|
op: func(t *testing.T, s *Store) error {
|
|
|
|
entry := &structs.ServiceRouterConfigEntry{
|
|
|
|
Kind: structs.ServiceRouter,
|
|
|
|
Name: "main",
|
|
|
|
Routes: []structs.ServiceRoute{
|
|
|
|
{
|
|
|
|
Match: &structs.ServiceRouteMatch{
|
|
|
|
HTTP: &structs.ServiceRouteHTTPMatch{
|
|
|
|
PathExact: "/other",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Destination: &structs.ServiceRouteDestination{
|
2020-02-06 15:52:25 +00:00
|
|
|
ServiceSubset: "other",
|
2019-07-01 20:23:36 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-01-24 15:04:58 +00:00
|
|
|
return s.EnsureConfigEntry(0, entry, nil)
|
2019-07-01 20:23:36 +00:00
|
|
|
},
|
|
|
|
expectErr: "does not permit advanced routing or splitting behavior",
|
|
|
|
expectGraphErr: true,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"router fails without default protocol": {
|
2020-02-06 15:52:25 +00:00
|
|
|
entries: []structs.ConfigEntry{
|
|
|
|
&structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "main",
|
|
|
|
Subsets: map[string]structs.ServiceResolverSubset{
|
2020-06-16 17:19:31 +00:00
|
|
|
"other": {
|
2020-02-06 15:52:25 +00:00
|
|
|
Filter: "Service.Meta.version == other",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-01 20:23:36 +00:00
|
|
|
op: func(t *testing.T, s *Store) error {
|
|
|
|
entry := &structs.ServiceRouterConfigEntry{
|
|
|
|
Kind: structs.ServiceRouter,
|
|
|
|
Name: "main",
|
|
|
|
Routes: []structs.ServiceRoute{
|
|
|
|
{
|
|
|
|
Match: &structs.ServiceRouteMatch{
|
|
|
|
HTTP: &structs.ServiceRouteHTTPMatch{
|
|
|
|
PathExact: "/other",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Destination: &structs.ServiceRouteDestination{
|
2020-02-06 15:52:25 +00:00
|
|
|
ServiceSubset: "other",
|
2019-07-01 20:23:36 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-01-24 15:04:58 +00:00
|
|
|
return s.EnsureConfigEntry(0, entry, nil)
|
2019-07-01 20:23:36 +00:00
|
|
|
},
|
|
|
|
expectErr: "does not permit advanced routing or splitting behavior",
|
|
|
|
expectGraphErr: true,
|
|
|
|
},
|
|
|
|
/////////////////////////////////////////////////
|
2020-06-16 17:19:31 +00:00
|
|
|
"cannot remove default protocol after splitter created": {
|
2019-07-01 20:23:36 +00:00
|
|
|
entries: []structs.ConfigEntry{
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "main",
|
|
|
|
Protocol: "http",
|
|
|
|
},
|
2020-01-24 15:04:58 +00:00
|
|
|
&structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "main",
|
|
|
|
Subsets: map[string]structs.ServiceResolverSubset{
|
2020-06-16 17:19:31 +00:00
|
|
|
"v1": {
|
2020-01-24 15:04:58 +00:00
|
|
|
Filter: "Service.Meta.version == v1",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"v2": {
|
2020-01-24 15:04:58 +00:00
|
|
|
Filter: "Service.Meta.version == v2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-01 20:23:36 +00:00
|
|
|
&structs.ServiceSplitterConfigEntry{
|
|
|
|
Kind: structs.ServiceSplitter,
|
|
|
|
Name: "main",
|
|
|
|
Splits: []structs.ServiceSplit{
|
2020-01-24 15:04:58 +00:00
|
|
|
{Weight: 90, ServiceSubset: "v1"},
|
|
|
|
{Weight: 10, ServiceSubset: "v2"},
|
2019-07-01 20:23:36 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
op: func(t *testing.T, s *Store) error {
|
2020-01-24 15:04:58 +00:00
|
|
|
return s.DeleteConfigEntry(0, structs.ServiceDefaults, "main", nil)
|
2019-07-01 20:23:36 +00:00
|
|
|
},
|
|
|
|
expectErr: "does not permit advanced routing or splitting behavior",
|
|
|
|
expectGraphErr: true,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"cannot remove global default protocol after splitter created": {
|
2019-07-02 16:01:17 +00:00
|
|
|
entries: []structs.ConfigEntry{
|
|
|
|
&structs.ProxyConfigEntry{
|
|
|
|
Kind: structs.ProxyDefaults,
|
|
|
|
Name: structs.ProxyConfigGlobal,
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"protocol": "http",
|
|
|
|
},
|
|
|
|
},
|
2020-02-06 15:52:25 +00:00
|
|
|
&structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "main",
|
|
|
|
Subsets: map[string]structs.ServiceResolverSubset{
|
2020-06-16 17:19:31 +00:00
|
|
|
"v1": {
|
2020-02-06 15:52:25 +00:00
|
|
|
Filter: "Service.Meta.version == v1",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"v2": {
|
2020-02-06 15:52:25 +00:00
|
|
|
Filter: "Service.Meta.version == v2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-02 16:01:17 +00:00
|
|
|
&structs.ServiceSplitterConfigEntry{
|
|
|
|
Kind: structs.ServiceSplitter,
|
|
|
|
Name: "main",
|
|
|
|
Splits: []structs.ServiceSplit{
|
2020-02-06 15:52:25 +00:00
|
|
|
{Weight: 90, ServiceSubset: "v1"},
|
|
|
|
{Weight: 10, ServiceSubset: "v2"},
|
2019-07-02 16:01:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
op: func(t *testing.T, s *Store) error {
|
2020-01-24 15:04:58 +00:00
|
|
|
return s.DeleteConfigEntry(0, structs.ProxyDefaults, structs.ProxyConfigGlobal, nil)
|
2019-07-02 16:01:17 +00:00
|
|
|
},
|
|
|
|
expectErr: "does not permit advanced routing or splitting behavior",
|
|
|
|
expectGraphErr: true,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"can remove global default protocol after splitter created if service default overrides it": {
|
2019-07-02 16:01:17 +00:00
|
|
|
entries: []structs.ConfigEntry{
|
|
|
|
&structs.ProxyConfigEntry{
|
|
|
|
Kind: structs.ProxyDefaults,
|
|
|
|
Name: structs.ProxyConfigGlobal,
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"protocol": "http",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "main",
|
|
|
|
Protocol: "http",
|
|
|
|
},
|
2020-01-24 15:04:58 +00:00
|
|
|
&structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "main",
|
|
|
|
Subsets: map[string]structs.ServiceResolverSubset{
|
2020-06-16 17:19:31 +00:00
|
|
|
"v1": {
|
2020-01-24 15:04:58 +00:00
|
|
|
Filter: "Service.Meta.version == v1",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"v2": {
|
2020-01-24 15:04:58 +00:00
|
|
|
Filter: "Service.Meta.version == v2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-02 16:01:17 +00:00
|
|
|
&structs.ServiceSplitterConfigEntry{
|
|
|
|
Kind: structs.ServiceSplitter,
|
|
|
|
Name: "main",
|
|
|
|
Splits: []structs.ServiceSplit{
|
2020-01-24 15:04:58 +00:00
|
|
|
{Weight: 90, ServiceSubset: "v1"},
|
|
|
|
{Weight: 10, ServiceSubset: "v2"},
|
2019-07-02 16:01:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
op: func(t *testing.T, s *Store) error {
|
2020-01-24 15:04:58 +00:00
|
|
|
return s.DeleteConfigEntry(0, structs.ProxyDefaults, structs.ProxyConfigGlobal, nil)
|
2019-07-02 16:01:17 +00:00
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"cannot change to tcp protocol after splitter created": {
|
2019-07-01 20:23:36 +00:00
|
|
|
entries: []structs.ConfigEntry{
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "main",
|
|
|
|
Protocol: "http",
|
|
|
|
},
|
2020-01-24 15:04:58 +00:00
|
|
|
&structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "main",
|
|
|
|
Subsets: map[string]structs.ServiceResolverSubset{
|
2020-06-16 17:19:31 +00:00
|
|
|
"v1": {
|
2020-01-24 15:04:58 +00:00
|
|
|
Filter: "Service.Meta.version == v1",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"v2": {
|
2020-01-24 15:04:58 +00:00
|
|
|
Filter: "Service.Meta.version == v2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-01 20:23:36 +00:00
|
|
|
&structs.ServiceSplitterConfigEntry{
|
|
|
|
Kind: structs.ServiceSplitter,
|
|
|
|
Name: "main",
|
|
|
|
Splits: []structs.ServiceSplit{
|
2020-01-24 15:04:58 +00:00
|
|
|
{Weight: 90, ServiceSubset: "v1"},
|
|
|
|
{Weight: 10, ServiceSubset: "v2"},
|
2019-07-01 20:23:36 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
op: func(t *testing.T, s *Store) error {
|
|
|
|
entry := &structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "main",
|
|
|
|
Protocol: "tcp",
|
|
|
|
}
|
2020-01-24 15:04:58 +00:00
|
|
|
return s.EnsureConfigEntry(0, entry, nil)
|
2019-07-01 20:23:36 +00:00
|
|
|
},
|
|
|
|
expectErr: "does not permit advanced routing or splitting behavior",
|
|
|
|
expectGraphErr: true,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"cannot remove default protocol after router created": {
|
2019-07-01 20:23:36 +00:00
|
|
|
entries: []structs.ConfigEntry{
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "main",
|
|
|
|
Protocol: "http",
|
|
|
|
},
|
2020-01-24 15:04:58 +00:00
|
|
|
&structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "main",
|
|
|
|
Subsets: map[string]structs.ServiceResolverSubset{
|
2020-06-16 17:19:31 +00:00
|
|
|
"other": {
|
2020-01-24 15:04:58 +00:00
|
|
|
Filter: "Service.Meta.version == other",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-01 20:23:36 +00:00
|
|
|
&structs.ServiceRouterConfigEntry{
|
|
|
|
Kind: structs.ServiceRouter,
|
|
|
|
Name: "main",
|
|
|
|
Routes: []structs.ServiceRoute{
|
|
|
|
{
|
|
|
|
Match: &structs.ServiceRouteMatch{
|
|
|
|
HTTP: &structs.ServiceRouteHTTPMatch{
|
|
|
|
PathExact: "/other",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Destination: &structs.ServiceRouteDestination{
|
2020-01-24 15:04:58 +00:00
|
|
|
ServiceSubset: "other",
|
2019-07-01 20:23:36 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
op: func(t *testing.T, s *Store) error {
|
2020-01-24 15:04:58 +00:00
|
|
|
return s.DeleteConfigEntry(0, structs.ServiceDefaults, "main", nil)
|
2019-07-01 20:23:36 +00:00
|
|
|
},
|
|
|
|
expectErr: "does not permit advanced routing or splitting behavior",
|
|
|
|
expectGraphErr: true,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"cannot change to tcp protocol after router created": {
|
2019-07-01 20:23:36 +00:00
|
|
|
entries: []structs.ConfigEntry{
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "main",
|
|
|
|
Protocol: "http",
|
|
|
|
},
|
2020-01-24 15:04:58 +00:00
|
|
|
&structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "main",
|
|
|
|
Subsets: map[string]structs.ServiceResolverSubset{
|
2020-06-16 17:19:31 +00:00
|
|
|
"other": {
|
2020-01-24 15:04:58 +00:00
|
|
|
Filter: "Service.Meta.version == other",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-01 20:23:36 +00:00
|
|
|
&structs.ServiceRouterConfigEntry{
|
|
|
|
Kind: structs.ServiceRouter,
|
|
|
|
Name: "main",
|
|
|
|
Routes: []structs.ServiceRoute{
|
|
|
|
{
|
|
|
|
Match: &structs.ServiceRouteMatch{
|
|
|
|
HTTP: &structs.ServiceRouteHTTPMatch{
|
|
|
|
PathExact: "/other",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Destination: &structs.ServiceRouteDestination{
|
2020-01-24 15:04:58 +00:00
|
|
|
ServiceSubset: "other",
|
2019-07-01 20:23:36 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
op: func(t *testing.T, s *Store) error {
|
|
|
|
entry := &structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "main",
|
|
|
|
Protocol: "tcp",
|
|
|
|
}
|
2020-01-24 15:04:58 +00:00
|
|
|
return s.EnsureConfigEntry(0, entry, nil)
|
2019-07-01 20:23:36 +00:00
|
|
|
},
|
|
|
|
expectErr: "does not permit advanced routing or splitting behavior",
|
|
|
|
expectGraphErr: true,
|
|
|
|
},
|
|
|
|
/////////////////////////////////////////////////
|
2020-06-16 17:19:31 +00:00
|
|
|
"cannot split to a service using tcp": {
|
2019-07-01 20:23:36 +00:00
|
|
|
entries: []structs.ConfigEntry{
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "main",
|
|
|
|
Protocol: "http",
|
|
|
|
},
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "other",
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
op: func(t *testing.T, s *Store) error {
|
|
|
|
entry := &structs.ServiceSplitterConfigEntry{
|
|
|
|
Kind: structs.ServiceSplitter,
|
|
|
|
Name: "main",
|
|
|
|
Splits: []structs.ServiceSplit{
|
|
|
|
{Weight: 90},
|
|
|
|
{Weight: 10, Service: "other"},
|
|
|
|
},
|
|
|
|
}
|
2020-01-24 15:04:58 +00:00
|
|
|
return s.EnsureConfigEntry(0, entry, nil)
|
2019-07-01 20:23:36 +00:00
|
|
|
},
|
|
|
|
expectErr: "uses inconsistent protocols",
|
|
|
|
expectGraphErr: true,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"cannot route to a service using tcp": {
|
2019-07-01 20:23:36 +00:00
|
|
|
entries: []structs.ConfigEntry{
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "main",
|
|
|
|
Protocol: "http",
|
|
|
|
},
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "other",
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
op: func(t *testing.T, s *Store) error {
|
|
|
|
entry := &structs.ServiceRouterConfigEntry{
|
|
|
|
Kind: structs.ServiceRouter,
|
|
|
|
Name: "main",
|
|
|
|
Routes: []structs.ServiceRoute{
|
|
|
|
{
|
|
|
|
Match: &structs.ServiceRouteMatch{
|
|
|
|
HTTP: &structs.ServiceRouteHTTPMatch{
|
|
|
|
PathExact: "/other",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Destination: &structs.ServiceRouteDestination{
|
|
|
|
Service: "other",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-01-24 15:04:58 +00:00
|
|
|
return s.EnsureConfigEntry(0, entry, nil)
|
2019-07-01 20:23:36 +00:00
|
|
|
},
|
|
|
|
expectErr: "uses inconsistent protocols",
|
|
|
|
expectGraphErr: true,
|
|
|
|
},
|
|
|
|
/////////////////////////////////////////////////
|
2020-06-16 17:19:31 +00:00
|
|
|
"cannot failover to a service using a different protocol": {
|
2019-07-01 20:23:36 +00:00
|
|
|
entries: []structs.ConfigEntry{
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "main",
|
|
|
|
Protocol: "grpc",
|
|
|
|
},
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "other",
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
&structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "main",
|
|
|
|
ConnectTimeout: 33 * time.Second,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
op: func(t *testing.T, s *Store) error {
|
|
|
|
entry := &structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "main",
|
|
|
|
Failover: map[string]structs.ServiceResolverFailover{
|
2020-06-16 17:19:31 +00:00
|
|
|
"*": {
|
2019-07-01 20:23:36 +00:00
|
|
|
Service: "other",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-01-24 15:04:58 +00:00
|
|
|
return s.EnsureConfigEntry(0, entry, nil)
|
2019-07-01 20:23:36 +00:00
|
|
|
},
|
|
|
|
expectErr: "uses inconsistent protocols",
|
|
|
|
expectGraphErr: true,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"cannot redirect to a service using a different protocol": {
|
2019-07-01 20:23:36 +00:00
|
|
|
entries: []structs.ConfigEntry{
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "main",
|
|
|
|
Protocol: "grpc",
|
|
|
|
},
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "other",
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
&structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "main",
|
|
|
|
ConnectTimeout: 33 * time.Second,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
op: func(t *testing.T, s *Store) error {
|
|
|
|
entry := &structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "main",
|
|
|
|
Redirect: &structs.ServiceResolverRedirect{
|
|
|
|
Service: "other",
|
|
|
|
},
|
|
|
|
}
|
2020-01-24 15:04:58 +00:00
|
|
|
return s.EnsureConfigEntry(0, entry, nil)
|
2019-07-01 20:23:36 +00:00
|
|
|
},
|
|
|
|
expectErr: "uses inconsistent protocols",
|
|
|
|
expectGraphErr: true,
|
|
|
|
},
|
|
|
|
/////////////////////////////////////////////////
|
2020-06-16 17:19:31 +00:00
|
|
|
"redirect to a subset that does exist is fine": {
|
2019-07-01 20:23:36 +00:00
|
|
|
entries: []structs.ConfigEntry{
|
|
|
|
&structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "other",
|
|
|
|
ConnectTimeout: 33 * time.Second,
|
|
|
|
Subsets: map[string]structs.ServiceResolverSubset{
|
2020-06-16 17:19:31 +00:00
|
|
|
"v1": {
|
2019-07-01 20:23:36 +00:00
|
|
|
Filter: "Service.Meta.version == v1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
op: func(t *testing.T, s *Store) error {
|
|
|
|
entry := &structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "main",
|
|
|
|
Redirect: &structs.ServiceResolverRedirect{
|
|
|
|
Service: "other",
|
|
|
|
ServiceSubset: "v1",
|
|
|
|
},
|
|
|
|
}
|
2020-01-24 15:04:58 +00:00
|
|
|
return s.EnsureConfigEntry(0, entry, nil)
|
2019-07-01 20:23:36 +00:00
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"cannot redirect to a subset that does not exist": {
|
2019-07-01 20:23:36 +00:00
|
|
|
entries: []structs.ConfigEntry{
|
|
|
|
&structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "other",
|
|
|
|
ConnectTimeout: 33 * time.Second,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
op: func(t *testing.T, s *Store) error {
|
|
|
|
entry := &structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "main",
|
|
|
|
Redirect: &structs.ServiceResolverRedirect{
|
|
|
|
Service: "other",
|
|
|
|
ServiceSubset: "v1",
|
|
|
|
},
|
|
|
|
}
|
2020-01-24 15:04:58 +00:00
|
|
|
return s.EnsureConfigEntry(0, entry, nil)
|
2019-07-01 20:23:36 +00:00
|
|
|
},
|
|
|
|
expectErr: `does not have a subset named "v1"`,
|
|
|
|
expectGraphErr: true,
|
|
|
|
},
|
2019-08-02 14:18:45 +00:00
|
|
|
/////////////////////////////////////////////////
|
2020-06-16 17:19:31 +00:00
|
|
|
"cannot introduce circular resolver redirect": {
|
2019-08-02 14:18:45 +00:00
|
|
|
entries: []structs.ConfigEntry{
|
|
|
|
&structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "other",
|
|
|
|
Redirect: &structs.ServiceResolverRedirect{
|
|
|
|
Service: "main",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
op: func(t *testing.T, s *Store) error {
|
|
|
|
entry := &structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "main",
|
|
|
|
Redirect: &structs.ServiceResolverRedirect{
|
|
|
|
Service: "other",
|
|
|
|
},
|
|
|
|
}
|
2020-01-24 15:04:58 +00:00
|
|
|
return s.EnsureConfigEntry(0, entry, nil)
|
2019-08-02 14:18:45 +00:00
|
|
|
},
|
|
|
|
expectErr: `detected circular resolver redirect`,
|
|
|
|
expectGraphErr: true,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"cannot introduce circular split": {
|
2019-08-02 14:18:45 +00:00
|
|
|
entries: []structs.ConfigEntry{
|
|
|
|
&structs.ProxyConfigEntry{
|
|
|
|
Kind: structs.ProxyDefaults,
|
|
|
|
Name: structs.ProxyConfigGlobal,
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"protocol": "http",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.ServiceSplitterConfigEntry{
|
|
|
|
Kind: "service-splitter",
|
|
|
|
Name: "other",
|
|
|
|
Splits: []structs.ServiceSplit{
|
|
|
|
{Weight: 100, Service: "main"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
op: func(t *testing.T, s *Store) error {
|
|
|
|
entry := &structs.ServiceSplitterConfigEntry{
|
|
|
|
Kind: "service-splitter",
|
|
|
|
Name: "main",
|
|
|
|
Splits: []structs.ServiceSplit{
|
|
|
|
{Weight: 100, Service: "other"},
|
|
|
|
},
|
|
|
|
}
|
2020-01-24 15:04:58 +00:00
|
|
|
return s.EnsureConfigEntry(0, entry, nil)
|
2019-08-02 14:18:45 +00:00
|
|
|
},
|
|
|
|
expectErr: `detected circular reference`,
|
|
|
|
expectGraphErr: true,
|
|
|
|
},
|
2020-01-24 15:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range cases {
|
|
|
|
name := name
|
2019-07-01 20:23:36 +00:00
|
|
|
tc := tc
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
t.Run(name, func(t *testing.T) {
|
2019-07-01 20:23:36 +00:00
|
|
|
s := testStateStore(t)
|
|
|
|
for _, entry := range tc.entries {
|
2020-02-06 15:52:25 +00:00
|
|
|
require.NoError(t, entry.Normalize())
|
2020-01-24 15:04:58 +00:00
|
|
|
require.NoError(t, s.EnsureConfigEntry(0, entry, nil))
|
2019-07-01 20:23:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := tc.op(t, s)
|
|
|
|
if tc.expectErr != "" {
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), tc.expectErr)
|
|
|
|
_, ok := err.(*structs.ConfigEntryGraphError)
|
|
|
|
if tc.expectGraphErr {
|
|
|
|
require.True(t, ok, "%T is not a *ConfigEntryGraphError", err)
|
|
|
|
} else {
|
|
|
|
require.False(t, ok, "did not expect a *ConfigEntryGraphError here: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStore_ReadDiscoveryChainConfigEntries_Overrides(t *testing.T) {
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
entries []structs.ConfigEntry
|
|
|
|
expectBefore []structs.ConfigEntryKindName
|
|
|
|
overrides map[structs.ConfigEntryKindName]structs.ConfigEntry
|
|
|
|
expectAfter []structs.ConfigEntryKindName
|
|
|
|
expectAfterErr string
|
|
|
|
checkAfter func(t *testing.T, entrySet *structs.DiscoveryChainConfigEntries)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "mask service-defaults",
|
|
|
|
entries: []structs.ConfigEntry{
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "main",
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectBefore: []structs.ConfigEntryKindName{
|
|
|
|
{Kind: structs.ServiceDefaults, Name: "main"},
|
|
|
|
},
|
|
|
|
overrides: map[structs.ConfigEntryKindName]structs.ConfigEntry{
|
|
|
|
{Kind: structs.ServiceDefaults, Name: "main"}: nil,
|
|
|
|
},
|
|
|
|
expectAfter: []structs.ConfigEntryKindName{
|
|
|
|
// nothing
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "edit service-defaults",
|
|
|
|
entries: []structs.ConfigEntry{
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "main",
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectBefore: []structs.ConfigEntryKindName{
|
|
|
|
{Kind: structs.ServiceDefaults, Name: "main"},
|
|
|
|
},
|
|
|
|
overrides: map[structs.ConfigEntryKindName]structs.ConfigEntry{
|
|
|
|
{Kind: structs.ServiceDefaults, Name: "main"}: &structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "main",
|
|
|
|
Protocol: "grpc",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectAfter: []structs.ConfigEntryKindName{
|
|
|
|
{Kind: structs.ServiceDefaults, Name: "main"},
|
|
|
|
},
|
|
|
|
checkAfter: func(t *testing.T, entrySet *structs.DiscoveryChainConfigEntries) {
|
2020-01-24 15:04:58 +00:00
|
|
|
defaults := entrySet.GetService(structs.NewServiceID("main", nil))
|
2019-07-01 20:23:36 +00:00
|
|
|
require.NotNil(t, defaults)
|
|
|
|
require.Equal(t, "grpc", defaults.Protocol)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "mask service-router",
|
|
|
|
entries: []structs.ConfigEntry{
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "main",
|
|
|
|
Protocol: "http",
|
|
|
|
},
|
|
|
|
&structs.ServiceRouterConfigEntry{
|
|
|
|
Kind: structs.ServiceRouter,
|
|
|
|
Name: "main",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectBefore: []structs.ConfigEntryKindName{
|
|
|
|
{Kind: structs.ServiceDefaults, Name: "main"},
|
|
|
|
{Kind: structs.ServiceRouter, Name: "main"},
|
|
|
|
},
|
|
|
|
overrides: map[structs.ConfigEntryKindName]structs.ConfigEntry{
|
|
|
|
{Kind: structs.ServiceRouter, Name: "main"}: nil,
|
|
|
|
},
|
|
|
|
expectAfter: []structs.ConfigEntryKindName{
|
|
|
|
{Kind: structs.ServiceDefaults, Name: "main"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "edit service-router",
|
|
|
|
entries: []structs.ConfigEntry{
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "main",
|
|
|
|
Protocol: "http",
|
|
|
|
},
|
|
|
|
&structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "main",
|
|
|
|
Subsets: map[string]structs.ServiceResolverSubset{
|
|
|
|
"v1": {Filter: "Service.Meta.version == v1"},
|
|
|
|
"v2": {Filter: "Service.Meta.version == v2"},
|
|
|
|
"v3": {Filter: "Service.Meta.version == v3"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.ServiceRouterConfigEntry{
|
|
|
|
Kind: structs.ServiceRouter,
|
|
|
|
Name: "main",
|
|
|
|
Routes: []structs.ServiceRoute{
|
|
|
|
{
|
|
|
|
Match: &structs.ServiceRouteMatch{
|
|
|
|
HTTP: &structs.ServiceRouteHTTPMatch{
|
|
|
|
PathExact: "/admin",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Destination: &structs.ServiceRouteDestination{
|
|
|
|
ServiceSubset: "v2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectBefore: []structs.ConfigEntryKindName{
|
|
|
|
{Kind: structs.ServiceDefaults, Name: "main"},
|
|
|
|
{Kind: structs.ServiceResolver, Name: "main"},
|
|
|
|
{Kind: structs.ServiceRouter, Name: "main"},
|
|
|
|
},
|
|
|
|
overrides: map[structs.ConfigEntryKindName]structs.ConfigEntry{
|
|
|
|
{Kind: structs.ServiceRouter, Name: "main"}: &structs.ServiceRouterConfigEntry{
|
|
|
|
Kind: structs.ServiceRouter,
|
|
|
|
Name: "main",
|
|
|
|
Routes: []structs.ServiceRoute{
|
|
|
|
{
|
|
|
|
Match: &structs.ServiceRouteMatch{
|
|
|
|
HTTP: &structs.ServiceRouteHTTPMatch{
|
|
|
|
PathExact: "/admin",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Destination: &structs.ServiceRouteDestination{
|
|
|
|
ServiceSubset: "v3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectAfter: []structs.ConfigEntryKindName{
|
|
|
|
{Kind: structs.ServiceDefaults, Name: "main"},
|
|
|
|
{Kind: structs.ServiceResolver, Name: "main"},
|
|
|
|
{Kind: structs.ServiceRouter, Name: "main"},
|
|
|
|
},
|
|
|
|
checkAfter: func(t *testing.T, entrySet *structs.DiscoveryChainConfigEntries) {
|
2020-01-24 15:04:58 +00:00
|
|
|
router := entrySet.GetRouter(structs.NewServiceID("main", nil))
|
2019-07-01 20:23:36 +00:00
|
|
|
require.NotNil(t, router)
|
|
|
|
require.Len(t, router.Routes, 1)
|
|
|
|
|
|
|
|
expect := structs.ServiceRoute{
|
|
|
|
Match: &structs.ServiceRouteMatch{
|
|
|
|
HTTP: &structs.ServiceRouteHTTPMatch{
|
|
|
|
PathExact: "/admin",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Destination: &structs.ServiceRouteDestination{
|
|
|
|
ServiceSubset: "v3",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
require.Equal(t, expect, router.Routes[0])
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "mask service-splitter",
|
|
|
|
entries: []structs.ConfigEntry{
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "main",
|
|
|
|
Protocol: "http",
|
|
|
|
},
|
|
|
|
&structs.ServiceSplitterConfigEntry{
|
|
|
|
Kind: structs.ServiceSplitter,
|
|
|
|
Name: "main",
|
|
|
|
Splits: []structs.ServiceSplit{
|
|
|
|
{Weight: 100},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectBefore: []structs.ConfigEntryKindName{
|
|
|
|
{Kind: structs.ServiceDefaults, Name: "main"},
|
|
|
|
{Kind: structs.ServiceSplitter, Name: "main"},
|
|
|
|
},
|
|
|
|
overrides: map[structs.ConfigEntryKindName]structs.ConfigEntry{
|
|
|
|
{Kind: structs.ServiceSplitter, Name: "main"}: nil,
|
|
|
|
},
|
|
|
|
expectAfter: []structs.ConfigEntryKindName{
|
|
|
|
{Kind: structs.ServiceDefaults, Name: "main"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "edit service-splitter",
|
|
|
|
entries: []structs.ConfigEntry{
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "main",
|
|
|
|
Protocol: "http",
|
|
|
|
},
|
|
|
|
&structs.ServiceSplitterConfigEntry{
|
|
|
|
Kind: structs.ServiceSplitter,
|
|
|
|
Name: "main",
|
|
|
|
Splits: []structs.ServiceSplit{
|
|
|
|
{Weight: 100},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectBefore: []structs.ConfigEntryKindName{
|
|
|
|
{Kind: structs.ServiceDefaults, Name: "main"},
|
|
|
|
{Kind: structs.ServiceSplitter, Name: "main"},
|
|
|
|
},
|
|
|
|
overrides: map[structs.ConfigEntryKindName]structs.ConfigEntry{
|
|
|
|
{Kind: structs.ServiceSplitter, Name: "main"}: &structs.ServiceSplitterConfigEntry{
|
|
|
|
Kind: structs.ServiceSplitter,
|
|
|
|
Name: "main",
|
|
|
|
Splits: []structs.ServiceSplit{
|
|
|
|
{Weight: 85, ServiceSubset: "v1"},
|
|
|
|
{Weight: 15, ServiceSubset: "v2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectAfter: []structs.ConfigEntryKindName{
|
|
|
|
{Kind: structs.ServiceDefaults, Name: "main"},
|
|
|
|
{Kind: structs.ServiceSplitter, Name: "main"},
|
|
|
|
},
|
|
|
|
checkAfter: func(t *testing.T, entrySet *structs.DiscoveryChainConfigEntries) {
|
2020-01-24 15:04:58 +00:00
|
|
|
splitter := entrySet.GetSplitter(structs.NewServiceID("main", nil))
|
2019-07-01 20:23:36 +00:00
|
|
|
require.NotNil(t, splitter)
|
|
|
|
require.Len(t, splitter.Splits, 2)
|
|
|
|
|
|
|
|
expect := []structs.ServiceSplit{
|
|
|
|
{Weight: 85, ServiceSubset: "v1"},
|
|
|
|
{Weight: 15, ServiceSubset: "v2"},
|
|
|
|
}
|
|
|
|
require.Equal(t, expect, splitter.Splits)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "mask service-resolver",
|
|
|
|
entries: []structs.ConfigEntry{
|
|
|
|
&structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "main",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectBefore: []structs.ConfigEntryKindName{
|
|
|
|
{Kind: structs.ServiceResolver, Name: "main"},
|
|
|
|
},
|
|
|
|
overrides: map[structs.ConfigEntryKindName]structs.ConfigEntry{
|
|
|
|
{Kind: structs.ServiceResolver, Name: "main"}: nil,
|
|
|
|
},
|
|
|
|
expectAfter: []structs.ConfigEntryKindName{
|
|
|
|
// nothing
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "edit service-resolver",
|
|
|
|
entries: []structs.ConfigEntry{
|
|
|
|
&structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "main",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectBefore: []structs.ConfigEntryKindName{
|
|
|
|
{Kind: structs.ServiceResolver, Name: "main"},
|
|
|
|
},
|
|
|
|
overrides: map[structs.ConfigEntryKindName]structs.ConfigEntry{
|
|
|
|
{Kind: structs.ServiceResolver, Name: "main"}: &structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "main",
|
|
|
|
ConnectTimeout: 33 * time.Second,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectAfter: []structs.ConfigEntryKindName{
|
|
|
|
{Kind: structs.ServiceResolver, Name: "main"},
|
|
|
|
},
|
|
|
|
checkAfter: func(t *testing.T, entrySet *structs.DiscoveryChainConfigEntries) {
|
2020-01-24 15:04:58 +00:00
|
|
|
resolver := entrySet.GetResolver(structs.NewServiceID("main", nil))
|
2019-07-01 20:23:36 +00:00
|
|
|
require.NotNil(t, resolver)
|
|
|
|
require.Equal(t, 33*time.Second, resolver.ConnectTimeout)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
tc := tc
|
|
|
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
s := testStateStore(t)
|
|
|
|
for _, entry := range tc.entries {
|
2020-01-24 15:04:58 +00:00
|
|
|
require.NoError(t, s.EnsureConfigEntry(0, entry, nil))
|
2019-07-01 20:23:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("without override", func(t *testing.T) {
|
2020-01-24 15:04:58 +00:00
|
|
|
_, entrySet, err := s.readDiscoveryChainConfigEntries(nil, "main", nil, nil)
|
2019-07-01 20:23:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
got := entrySetToKindNames(entrySet)
|
|
|
|
require.ElementsMatch(t, tc.expectBefore, got)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("with override", func(t *testing.T) {
|
2020-01-24 15:04:58 +00:00
|
|
|
_, entrySet, err := s.readDiscoveryChainConfigEntries(nil, "main", tc.overrides, nil)
|
2019-07-01 20:23:36 +00:00
|
|
|
|
|
|
|
if tc.expectAfterErr != "" {
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), tc.expectAfterErr)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
got := entrySetToKindNames(entrySet)
|
|
|
|
require.ElementsMatch(t, tc.expectAfter, got)
|
|
|
|
|
|
|
|
if tc.checkAfter != nil {
|
|
|
|
tc.checkAfter(t, entrySet)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func entrySetToKindNames(entrySet *structs.DiscoveryChainConfigEntries) []structs.ConfigEntryKindName {
|
|
|
|
var out []structs.ConfigEntryKindName
|
|
|
|
for _, entry := range entrySet.Routers {
|
|
|
|
out = append(out, structs.ConfigEntryKindName{
|
|
|
|
Kind: entry.Kind,
|
|
|
|
Name: entry.Name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, entry := range entrySet.Splitters {
|
|
|
|
out = append(out, structs.ConfigEntryKindName{
|
|
|
|
Kind: entry.Kind,
|
|
|
|
Name: entry.Name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, entry := range entrySet.Resolvers {
|
|
|
|
out = append(out, structs.ConfigEntryKindName{
|
|
|
|
Kind: entry.Kind,
|
|
|
|
Name: entry.Name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, entry := range entrySet.Services {
|
|
|
|
out = append(out, structs.ConfigEntryKindName{
|
|
|
|
Kind: entry.Kind,
|
|
|
|
Name: entry.Name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStore_ReadDiscoveryChainConfigEntries_SubsetSplit(t *testing.T) {
|
|
|
|
s := testStateStore(t)
|
|
|
|
|
|
|
|
entries := []structs.ConfigEntry{
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "main",
|
|
|
|
Protocol: "http",
|
|
|
|
},
|
|
|
|
&structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "main",
|
|
|
|
Subsets: map[string]structs.ServiceResolverSubset{
|
2020-06-16 17:19:31 +00:00
|
|
|
"v1": {
|
2019-07-01 20:23:36 +00:00
|
|
|
Filter: "Service.Meta.version == v1",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"v2": {
|
2019-07-01 20:23:36 +00:00
|
|
|
Filter: "Service.Meta.version == v2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.ServiceSplitterConfigEntry{
|
|
|
|
Kind: structs.ServiceSplitter,
|
|
|
|
Name: "main",
|
|
|
|
Splits: []structs.ServiceSplit{
|
|
|
|
{Weight: 90, ServiceSubset: "v1"},
|
|
|
|
{Weight: 10, ServiceSubset: "v2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range entries {
|
2020-01-24 15:04:58 +00:00
|
|
|
require.NoError(t, s.EnsureConfigEntry(0, entry, nil))
|
2019-07-01 20:23:36 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
_, entrySet, err := s.ReadDiscoveryChainConfigEntries(nil, "main", nil)
|
2019-07-01 20:23:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Len(t, entrySet.Routers, 0)
|
|
|
|
require.Len(t, entrySet.Splitters, 1)
|
|
|
|
require.Len(t, entrySet.Resolvers, 1)
|
|
|
|
require.Len(t, entrySet.Services, 1)
|
|
|
|
}
|
2020-04-16 21:00:48 +00:00
|
|
|
|
|
|
|
func TestStore_ValidateGatewayNamesCannotBeShared(t *testing.T) {
|
|
|
|
s := testStateStore(t)
|
|
|
|
|
|
|
|
ingress := &structs.IngressGatewayConfigEntry{
|
|
|
|
Kind: structs.IngressGateway,
|
|
|
|
Name: "gateway",
|
|
|
|
}
|
|
|
|
require.NoError(t, s.EnsureConfigEntry(0, ingress, nil))
|
|
|
|
|
|
|
|
terminating := &structs.TerminatingGatewayConfigEntry{
|
|
|
|
Kind: structs.TerminatingGateway,
|
|
|
|
Name: "gateway",
|
|
|
|
}
|
|
|
|
// Cannot have 2 gateways with same service name
|
|
|
|
require.Error(t, s.EnsureConfigEntry(1, terminating, nil))
|
|
|
|
|
|
|
|
ingress = &structs.IngressGatewayConfigEntry{
|
|
|
|
Kind: structs.IngressGateway,
|
|
|
|
Name: "gateway",
|
|
|
|
Listeners: []structs.IngressListener{
|
|
|
|
{Port: 8080},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
require.NoError(t, s.EnsureConfigEntry(2, ingress, nil))
|
|
|
|
require.NoError(t, s.DeleteConfigEntry(3, structs.IngressGateway, "gateway", nil))
|
|
|
|
|
|
|
|
// Adding the terminating gateway with same name should now work
|
|
|
|
require.NoError(t, s.EnsureConfigEntry(4, terminating, nil))
|
|
|
|
|
|
|
|
// Cannot have 2 gateways with same service name
|
|
|
|
require.Error(t, s.EnsureConfigEntry(5, ingress, nil))
|
|
|
|
}
|
2020-05-06 21:09:24 +00:00
|
|
|
|
|
|
|
func TestStore_ValidateIngressGatewayErrorOnMismatchedProtocols(t *testing.T) {
|
2020-08-12 16:19:20 +00:00
|
|
|
newIngress := func(protocol, name string) *structs.IngressGatewayConfigEntry {
|
|
|
|
return &structs.IngressGatewayConfigEntry{
|
|
|
|
Kind: structs.IngressGateway,
|
|
|
|
Name: "gateway",
|
|
|
|
Listeners: []structs.IngressListener{
|
|
|
|
{
|
|
|
|
Port: 8080,
|
|
|
|
Protocol: protocol,
|
|
|
|
Services: []structs.IngressService{
|
|
|
|
{Name: name},
|
|
|
|
},
|
2020-05-06 21:09:24 +00:00
|
|
|
},
|
|
|
|
},
|
2020-08-12 16:19:20 +00:00
|
|
|
}
|
2020-05-06 21:09:24 +00:00
|
|
|
}
|
|
|
|
|
2020-08-12 16:19:20 +00:00
|
|
|
t.Run("http ingress fails with http upstream later changed to tcp", func(t *testing.T) {
|
|
|
|
s := testStateStore(t)
|
|
|
|
|
|
|
|
// First set the target service as http
|
|
|
|
expected := &structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "web",
|
|
|
|
Protocol: "http",
|
|
|
|
}
|
|
|
|
require.NoError(t, s.EnsureConfigEntry(0, expected, nil))
|
|
|
|
|
|
|
|
// Next configure http ingress to route to the http service
|
|
|
|
require.NoError(t, s.EnsureConfigEntry(1, newIngress("http", "web"), nil))
|
|
|
|
|
|
|
|
t.Run("via modification", func(t *testing.T) {
|
|
|
|
// Now redefine the target service as tcp
|
|
|
|
expected = &structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "web",
|
|
|
|
Protocol: "tcp",
|
|
|
|
}
|
|
|
|
|
|
|
|
err := s.EnsureConfigEntry(2, expected, nil)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), `has protocol "tcp"`)
|
|
|
|
})
|
|
|
|
t.Run("via deletion", func(t *testing.T) {
|
|
|
|
// This will fall back to the default tcp.
|
|
|
|
err := s.DeleteConfigEntry(2, structs.ServiceDefaults, "web", nil)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), `has protocol "tcp"`)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("tcp ingress ok with tcp upstream (defaulted) later changed to http", func(t *testing.T) {
|
|
|
|
s := testStateStore(t)
|
|
|
|
|
|
|
|
// First configure tcp ingress to route to a defaulted tcp service
|
|
|
|
require.NoError(t, s.EnsureConfigEntry(0, newIngress("tcp", "web"), nil))
|
|
|
|
|
|
|
|
// Now redefine the target service as http
|
|
|
|
expected := &structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "web",
|
|
|
|
Protocol: "http",
|
|
|
|
}
|
|
|
|
require.NoError(t, s.EnsureConfigEntry(1, expected, nil))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("tcp ingress fails with tcp upstream (defaulted) later changed to http", func(t *testing.T) {
|
|
|
|
s := testStateStore(t)
|
|
|
|
|
|
|
|
// First configure tcp ingress to route to a defaulted tcp service
|
|
|
|
require.NoError(t, s.EnsureConfigEntry(0, newIngress("tcp", "web"), nil))
|
|
|
|
|
|
|
|
// Now redefine the target service as http
|
|
|
|
expected := &structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "web",
|
|
|
|
Protocol: "http",
|
|
|
|
}
|
|
|
|
require.NoError(t, s.EnsureConfigEntry(1, expected, nil))
|
|
|
|
|
|
|
|
t.Run("and a router defined", func(t *testing.T) {
|
|
|
|
// This part should fail.
|
|
|
|
expected2 := &structs.ServiceRouterConfigEntry{
|
|
|
|
Kind: structs.ServiceRouter,
|
|
|
|
Name: "web",
|
|
|
|
}
|
|
|
|
err := s.EnsureConfigEntry(2, expected2, nil)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), `has protocol "http"`)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("and a splitter defined", func(t *testing.T) {
|
|
|
|
// This part should fail.
|
|
|
|
expected2 := &structs.ServiceSplitterConfigEntry{
|
|
|
|
Kind: structs.ServiceSplitter,
|
|
|
|
Name: "web",
|
|
|
|
Splits: []structs.ServiceSplit{
|
|
|
|
{Weight: 100},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := s.EnsureConfigEntry(2, expected2, nil)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), `has protocol "http"`)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("http ingress fails with tcp upstream (defaulted)", func(t *testing.T) {
|
|
|
|
s := testStateStore(t)
|
|
|
|
err := s.EnsureConfigEntry(0, newIngress("http", "web"), nil)
|
2020-05-06 21:09:24 +00:00
|
|
|
require.Error(t, err)
|
2020-05-08 18:24:33 +00:00
|
|
|
require.Contains(t, err.Error(), `has protocol "tcp"`)
|
2020-05-06 21:09:24 +00:00
|
|
|
})
|
|
|
|
|
2020-08-12 16:19:20 +00:00
|
|
|
t.Run("http ingress fails with http2 upstream (via proxy-defaults)", func(t *testing.T) {
|
|
|
|
s := testStateStore(t)
|
2020-05-06 21:09:24 +00:00
|
|
|
expected := &structs.ProxyConfigEntry{
|
|
|
|
Kind: structs.ProxyDefaults,
|
|
|
|
Name: "global",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"protocol": "http2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
require.NoError(t, s.EnsureConfigEntry(0, expected, nil))
|
|
|
|
|
2020-08-12 16:19:20 +00:00
|
|
|
err := s.EnsureConfigEntry(1, newIngress("http", "web"), nil)
|
2020-05-06 21:09:24 +00:00
|
|
|
require.Error(t, err)
|
2020-05-08 18:24:33 +00:00
|
|
|
require.Contains(t, err.Error(), `has protocol "http2"`)
|
2020-05-06 21:09:24 +00:00
|
|
|
})
|
|
|
|
|
2020-08-12 16:19:20 +00:00
|
|
|
t.Run("http ingress fails with grpc upstream (via service-defaults)", func(t *testing.T) {
|
|
|
|
s := testStateStore(t)
|
2020-05-06 21:09:24 +00:00
|
|
|
expected := &structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "web",
|
|
|
|
Protocol: "grpc",
|
|
|
|
}
|
|
|
|
require.NoError(t, s.EnsureConfigEntry(1, expected, nil))
|
2020-08-12 16:19:20 +00:00
|
|
|
err := s.EnsureConfigEntry(2, newIngress("http", "web"), nil)
|
2020-05-06 21:09:24 +00:00
|
|
|
require.Error(t, err)
|
2020-05-08 18:24:33 +00:00
|
|
|
require.Contains(t, err.Error(), `has protocol "grpc"`)
|
2020-05-06 21:09:24 +00:00
|
|
|
})
|
|
|
|
|
2020-08-12 16:19:20 +00:00
|
|
|
t.Run("http ingress ok with http upstream (via service-defaults)", func(t *testing.T) {
|
|
|
|
s := testStateStore(t)
|
2020-05-06 21:09:24 +00:00
|
|
|
expected := &structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "web",
|
|
|
|
Protocol: "http",
|
|
|
|
}
|
|
|
|
require.NoError(t, s.EnsureConfigEntry(2, expected, nil))
|
2020-08-12 16:19:20 +00:00
|
|
|
require.NoError(t, s.EnsureConfigEntry(3, newIngress("http", "web"), nil))
|
2020-05-06 21:09:24 +00:00
|
|
|
})
|
|
|
|
|
2020-08-12 16:19:20 +00:00
|
|
|
t.Run("http ingress ignores wildcard specifier", func(t *testing.T) {
|
|
|
|
s := testStateStore(t)
|
|
|
|
require.NoError(t, s.EnsureConfigEntry(4, newIngress("http", "*"), nil))
|
2020-05-06 21:09:24 +00:00
|
|
|
})
|
|
|
|
|
2020-08-12 16:19:20 +00:00
|
|
|
t.Run("deleting ingress config entry ok", func(t *testing.T) {
|
|
|
|
s := testStateStore(t)
|
|
|
|
require.NoError(t, s.EnsureConfigEntry(1, newIngress("tcp", "web"), nil))
|
2020-05-06 21:09:24 +00:00
|
|
|
require.NoError(t, s.DeleteConfigEntry(5, structs.IngressGateway, "gateway", nil))
|
|
|
|
})
|
|
|
|
}
|