2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2022-08-15 13:20:25 +00:00
|
|
|
//go:build !consulent
|
|
|
|
// +build !consulent
|
|
|
|
|
|
|
|
package structs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2023-08-22 14:46:03 +00:00
|
|
|
func TestServiceResolverConfigEntry_CE(t *testing.T) {
|
2022-08-15 13:20:25 +00:00
|
|
|
type testcase struct {
|
|
|
|
name string
|
|
|
|
entry *ServiceResolverConfigEntry
|
|
|
|
normalizeErr string
|
|
|
|
validateErr string
|
|
|
|
// check is called between normalize and validate
|
|
|
|
check func(t *testing.T, entry *ServiceResolverConfigEntry)
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []testcase{
|
2023-03-17 14:48:06 +00:00
|
|
|
{
|
2023-08-22 14:46:03 +00:00
|
|
|
name: "failover with a sameness group on CE",
|
2023-03-17 14:48:06 +00:00
|
|
|
entry: &ServiceResolverConfigEntry{
|
|
|
|
Kind: ServiceResolver,
|
|
|
|
Name: "test",
|
|
|
|
Failover: map[string]ServiceResolverFailover{
|
|
|
|
"*": {
|
|
|
|
SamenessGroup: "ns1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
validateErr: `Bad Failover["*"]: Setting SamenessGroup requires Consul Enterprise`,
|
|
|
|
},
|
2022-08-15 13:20:25 +00:00
|
|
|
{
|
2023-08-22 14:46:03 +00:00
|
|
|
name: "failover with a namespace on CE",
|
2022-08-15 13:20:25 +00:00
|
|
|
entry: &ServiceResolverConfigEntry{
|
|
|
|
Kind: ServiceResolver,
|
|
|
|
Name: "test",
|
|
|
|
Failover: map[string]ServiceResolverFailover{
|
|
|
|
"*": {
|
|
|
|
Service: "backup",
|
|
|
|
Namespace: "ns1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
validateErr: `Bad Failover["*"]: Setting Namespace requires Consul Enterprise`,
|
|
|
|
},
|
|
|
|
{
|
2023-08-22 14:46:03 +00:00
|
|
|
name: "failover Targets cannot set Namespace on CE",
|
2022-08-15 13:20:25 +00:00
|
|
|
entry: &ServiceResolverConfigEntry{
|
|
|
|
Kind: ServiceResolver,
|
|
|
|
Name: "test",
|
|
|
|
Failover: map[string]ServiceResolverFailover{
|
|
|
|
"*": {
|
|
|
|
Targets: []ServiceResolverFailoverTarget{{Namespace: "ns1"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
validateErr: `Bad Failover["*"].Targets[0]: Setting Namespace requires Consul Enterprise`,
|
|
|
|
},
|
|
|
|
{
|
2023-08-22 14:46:03 +00:00
|
|
|
name: "failover Targets cannot set Partition on CE",
|
2022-08-15 13:20:25 +00:00
|
|
|
entry: &ServiceResolverConfigEntry{
|
|
|
|
Kind: ServiceResolver,
|
|
|
|
Name: "test",
|
|
|
|
Failover: map[string]ServiceResolverFailover{
|
|
|
|
"*": {
|
|
|
|
Targets: []ServiceResolverFailoverTarget{{Partition: "ap1"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
validateErr: `Bad Failover["*"].Targets[0]: Setting Partition requires Consul Enterprise`,
|
|
|
|
},
|
|
|
|
{
|
2023-08-22 14:46:03 +00:00
|
|
|
name: "setting failover Namespace on CE",
|
2022-08-15 13:20:25 +00:00
|
|
|
entry: &ServiceResolverConfigEntry{
|
|
|
|
Kind: ServiceResolver,
|
|
|
|
Name: "test",
|
|
|
|
Failover: map[string]ServiceResolverFailover{
|
|
|
|
"*": {Namespace: "ns1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
validateErr: `Bad Failover["*"]: Setting Namespace requires Consul Enterprise`,
|
|
|
|
},
|
2023-03-03 16:12:38 +00:00
|
|
|
{
|
2023-08-22 14:46:03 +00:00
|
|
|
name: "setting failover Namespace on CE",
|
2023-03-03 16:12:38 +00:00
|
|
|
entry: &ServiceResolverConfigEntry{
|
|
|
|
Kind: ServiceResolver,
|
|
|
|
Name: "test",
|
|
|
|
Failover: map[string]ServiceResolverFailover{
|
|
|
|
"*": {Service: "s1", Policy: &ServiceResolverFailoverPolicy{Mode: "something"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
validateErr: `Bad Failover["*"]: Setting failover policies requires Consul Enterprise`,
|
|
|
|
},
|
2023-03-17 14:48:06 +00:00
|
|
|
{
|
2023-08-22 14:46:03 +00:00
|
|
|
name: "setting redirect SamenessGroup on CE",
|
2023-03-17 14:48:06 +00:00
|
|
|
entry: &ServiceResolverConfigEntry{
|
|
|
|
Kind: ServiceResolver,
|
|
|
|
Name: "test",
|
|
|
|
Redirect: &ServiceResolverRedirect{
|
|
|
|
SamenessGroup: "group",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
validateErr: `Redirect: Setting SamenessGroup requires Consul Enterprise`,
|
|
|
|
},
|
2022-08-29 13:51:32 +00:00
|
|
|
{
|
2023-08-22 14:46:03 +00:00
|
|
|
name: "setting redirect Namespace on CE",
|
2022-08-29 13:51:32 +00:00
|
|
|
entry: &ServiceResolverConfigEntry{
|
|
|
|
Kind: ServiceResolver,
|
|
|
|
Name: "test",
|
|
|
|
Redirect: &ServiceResolverRedirect{
|
|
|
|
Namespace: "ns1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
validateErr: `Redirect: Setting Namespace requires Consul Enterprise`,
|
|
|
|
},
|
|
|
|
{
|
2023-08-22 14:46:03 +00:00
|
|
|
name: "setting redirect Partition on CE",
|
2022-08-29 13:51:32 +00:00
|
|
|
entry: &ServiceResolverConfigEntry{
|
|
|
|
Kind: ServiceResolver,
|
|
|
|
Name: "test",
|
|
|
|
Redirect: &ServiceResolverRedirect{
|
|
|
|
Partition: "ap1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
validateErr: `Redirect: Setting Partition requires Consul Enterprise`,
|
|
|
|
},
|
2022-08-15 13:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bulk add a bunch of similar validation cases.
|
|
|
|
for _, invalidSubset := range invalidSubsetNames {
|
|
|
|
tc := testcase{
|
|
|
|
name: "invalid subset name: " + invalidSubset,
|
|
|
|
entry: &ServiceResolverConfigEntry{
|
|
|
|
Kind: ServiceResolver,
|
|
|
|
Name: "test",
|
|
|
|
Subsets: map[string]ServiceResolverSubset{
|
|
|
|
invalidSubset: {OnlyPassing: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
validateErr: fmt.Sprintf("Subset %q is invalid", invalidSubset),
|
|
|
|
}
|
|
|
|
cases = append(cases, tc)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, goodSubset := range validSubsetNames {
|
|
|
|
tc := testcase{
|
|
|
|
name: "valid subset name: " + goodSubset,
|
|
|
|
entry: &ServiceResolverConfigEntry{
|
|
|
|
Kind: ServiceResolver,
|
|
|
|
Name: "test",
|
|
|
|
Subsets: map[string]ServiceResolverSubset{
|
|
|
|
goodSubset: {OnlyPassing: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cases = append(cases, tc)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
err := tc.entry.Normalize()
|
|
|
|
if tc.normalizeErr != "" {
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), tc.normalizeErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
if tc.check != nil {
|
|
|
|
tc.check(t, tc.entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tc.entry.Validate()
|
|
|
|
if tc.validateErr != "" {
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), tc.validateErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|