2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-01-27 19:43:16 +00:00
|
|
|
package validate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
envoy_admin_v3 "github.com/envoyproxy/go-control-plane/envoy/admin/v3"
|
|
|
|
envoy_cluster_v3 "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
|
|
|
|
envoy_aggregate_cluster_v3 "github.com/envoyproxy/go-control-plane/envoy/extensions/clusters/aggregate/v3"
|
|
|
|
"github.com/hashicorp/consul/api"
|
2023-02-06 17:14:35 +00:00
|
|
|
"github.com/hashicorp/consul/envoyextensions/extensioncommon"
|
2023-01-27 19:43:16 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"google.golang.org/protobuf/types/known/anypb"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestErrors(t *testing.T) {
|
|
|
|
cases := map[string]struct {
|
|
|
|
validate func() *Validate
|
|
|
|
endpointValidator EndpointValidator
|
|
|
|
err string
|
|
|
|
}{
|
|
|
|
"success": {
|
|
|
|
validate: func() *Validate {
|
|
|
|
return &Validate{
|
|
|
|
envoyID: "db",
|
|
|
|
snis: map[string]struct{}{
|
|
|
|
"db-sni": {},
|
|
|
|
},
|
|
|
|
listener: true,
|
|
|
|
usesRDS: true,
|
|
|
|
route: true,
|
|
|
|
resources: map[string]*resource{
|
|
|
|
"db-sni": {
|
|
|
|
required: true,
|
|
|
|
cluster: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
endpointValidator: func(r *resource, s string, clusters *envoy_admin_v3.Clusters) {
|
|
|
|
r.loadAssignment = true
|
|
|
|
r.endpoints = 1
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"no clusters for listener or route": {
|
|
|
|
validate: func() *Validate {
|
|
|
|
return &Validate{
|
|
|
|
envoyID: "db",
|
|
|
|
snis: map[string]struct{}{
|
|
|
|
"db-sni": {},
|
|
|
|
},
|
|
|
|
listener: true,
|
|
|
|
usesRDS: true,
|
|
|
|
route: true,
|
|
|
|
resources: map[string]*resource{
|
|
|
|
"db-sni": {
|
|
|
|
required: false,
|
|
|
|
cluster: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
endpointValidator: func(r *resource, s string, clusters *envoy_admin_v3.Clusters) {
|
|
|
|
r.loadAssignment = true
|
|
|
|
r.endpoints = 1
|
|
|
|
},
|
2023-02-17 15:43:05 +00:00
|
|
|
err: "No clusters found on route or listener",
|
2023-01-27 19:43:16 +00:00
|
|
|
},
|
|
|
|
"no healthy endpoints": {
|
|
|
|
validate: func() *Validate {
|
|
|
|
return &Validate{
|
|
|
|
envoyID: "db",
|
|
|
|
snis: map[string]struct{}{
|
|
|
|
"db-sni": {},
|
|
|
|
},
|
|
|
|
listener: true,
|
|
|
|
usesRDS: true,
|
|
|
|
route: true,
|
|
|
|
resources: map[string]*resource{
|
|
|
|
"db-sni": {
|
|
|
|
required: true,
|
|
|
|
cluster: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
endpointValidator: func(r *resource, s string, clusters *envoy_admin_v3.Clusters) {
|
|
|
|
r.loadAssignment = true
|
|
|
|
},
|
2023-02-17 15:43:05 +00:00
|
|
|
err: "No healthy endpoints for cluster \"db-sni\" for upstream \"db\"",
|
2023-01-27 19:43:16 +00:00
|
|
|
},
|
|
|
|
"success: aggregate cluster with one target with endpoints": {
|
|
|
|
validate: func() *Validate {
|
|
|
|
return &Validate{
|
|
|
|
envoyID: "db",
|
|
|
|
snis: map[string]struct{}{
|
|
|
|
"db-sni": {},
|
|
|
|
"db-fail-1-sni": {},
|
|
|
|
"db-fail-2-sni": {},
|
|
|
|
},
|
|
|
|
listener: true,
|
|
|
|
usesRDS: true,
|
|
|
|
route: true,
|
|
|
|
resources: map[string]*resource{
|
|
|
|
"db-sni": {
|
|
|
|
required: true,
|
|
|
|
cluster: true,
|
|
|
|
aggregateCluster: true,
|
|
|
|
aggregateClusterChildren: []string{
|
|
|
|
"db-fail-1-sni",
|
|
|
|
"db-fail-2-sni",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"db-fail-1-sni": {
|
|
|
|
required: true,
|
|
|
|
cluster: true,
|
|
|
|
parentCluster: "db-sni",
|
|
|
|
// This doesn't usually get set here, but this tests that at least one child cluster has
|
|
|
|
// healthy endpoints case.
|
|
|
|
endpoints: 1,
|
|
|
|
},
|
|
|
|
"db-fail-2-sni": {
|
|
|
|
required: true,
|
|
|
|
cluster: true,
|
|
|
|
parentCluster: "db-sni",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
endpointValidator: func(r *resource, s string, clusters *envoy_admin_v3.Clusters) {
|
|
|
|
r.loadAssignment = true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"aggregate cluster no healthy endpoints": {
|
|
|
|
validate: func() *Validate {
|
|
|
|
return &Validate{
|
|
|
|
envoyID: "db",
|
|
|
|
snis: map[string]struct{}{
|
|
|
|
"db-sni": {},
|
|
|
|
"db-fail-1-sni": {},
|
|
|
|
"db-fail-2-sni": {},
|
|
|
|
},
|
|
|
|
listener: true,
|
|
|
|
usesRDS: true,
|
|
|
|
route: true,
|
|
|
|
resources: map[string]*resource{
|
|
|
|
"db-sni": {
|
|
|
|
required: true,
|
|
|
|
cluster: true,
|
|
|
|
aggregateCluster: true,
|
|
|
|
aggregateClusterChildren: []string{
|
|
|
|
"db-fail-1-sni",
|
|
|
|
"db-fail-2-sni",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"db-fail-1-sni": {
|
|
|
|
required: true,
|
|
|
|
cluster: true,
|
|
|
|
parentCluster: "db-sni",
|
|
|
|
},
|
|
|
|
"db-fail-2-sni": {
|
|
|
|
required: true,
|
|
|
|
cluster: true,
|
|
|
|
parentCluster: "db-sni",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
endpointValidator: func(r *resource, s string, clusters *envoy_admin_v3.Clusters) {
|
|
|
|
r.loadAssignment = true
|
|
|
|
r.endpoints = 0
|
|
|
|
},
|
2023-02-17 15:43:05 +00:00
|
|
|
err: "No healthy endpoints for aggregate cluster \"db-sni\" for upstream \"db\"",
|
2023-01-27 19:43:16 +00:00
|
|
|
},
|
2023-02-08 22:49:38 +00:00
|
|
|
"success: passthrough cluster doesn't error even though there are zero endpoints": {
|
|
|
|
validate: func() *Validate {
|
|
|
|
return &Validate{
|
|
|
|
envoyID: "db",
|
|
|
|
snis: map[string]struct{}{
|
|
|
|
"passthrough~db-sni": {},
|
|
|
|
},
|
|
|
|
listener: true,
|
|
|
|
usesRDS: true,
|
|
|
|
route: true,
|
|
|
|
resources: map[string]*resource{
|
|
|
|
"passthrough~db-sni": {
|
|
|
|
required: true,
|
|
|
|
cluster: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
endpointValidator: func(r *resource, s string, clusters *envoy_admin_v3.Clusters) {
|
|
|
|
r.loadAssignment = true
|
|
|
|
},
|
|
|
|
},
|
2023-01-27 19:43:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for n, tc := range cases {
|
|
|
|
t.Run(n, func(t *testing.T) {
|
|
|
|
v := tc.validate()
|
2023-02-08 21:03:15 +00:00
|
|
|
messages := v.GetMessages(true, tc.endpointValidator, nil)
|
2023-01-27 19:43:16 +00:00
|
|
|
|
2023-02-08 21:03:15 +00:00
|
|
|
var outputErrors string
|
|
|
|
for _, msgError := range messages.Errors() {
|
|
|
|
outputErrors += msgError.Message
|
2023-02-17 15:43:05 +00:00
|
|
|
for _, action := range msgError.PossibleActions {
|
|
|
|
outputErrors += action
|
|
|
|
}
|
2023-02-08 21:03:15 +00:00
|
|
|
}
|
|
|
|
if tc.err == "" {
|
|
|
|
require.True(t, messages.Success())
|
2023-01-27 19:43:16 +00:00
|
|
|
} else {
|
2023-02-08 21:03:15 +00:00
|
|
|
require.Contains(t, outputErrors, tc.err)
|
2023-01-27 19:43:16 +00:00
|
|
|
}
|
2023-02-08 21:03:15 +00:00
|
|
|
|
2023-01-27 19:43:16 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsAggregateCluster(t *testing.T) {
|
|
|
|
aggregateClusterConfig, err := anypb.New(&envoy_aggregate_cluster_v3.ClusterConfig{
|
|
|
|
Clusters: []string{"c1", "c2"},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
|
|
|
input *envoy_cluster_v3.Cluster
|
|
|
|
expectedAggregateCluster *envoy_aggregate_cluster_v3.ClusterConfig
|
|
|
|
expectedOk bool
|
|
|
|
}{
|
|
|
|
"non-aggregate cluster": {
|
|
|
|
input: &envoy_cluster_v3.Cluster{
|
|
|
|
Name: "foo",
|
|
|
|
ClusterDiscoveryType: &envoy_cluster_v3.Cluster_Type{Type: envoy_cluster_v3.Cluster_LOGICAL_DNS},
|
|
|
|
},
|
|
|
|
expectedOk: false,
|
|
|
|
},
|
|
|
|
"valid aggregate cluster": {
|
|
|
|
input: &envoy_cluster_v3.Cluster{
|
|
|
|
Name: "foo",
|
|
|
|
ClusterDiscoveryType: &envoy_cluster_v3.Cluster_ClusterType{
|
|
|
|
ClusterType: &envoy_cluster_v3.Cluster_CustomClusterType{
|
|
|
|
Name: "foo",
|
|
|
|
TypedConfig: aggregateClusterConfig,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedOk: true,
|
|
|
|
expectedAggregateCluster: &envoy_aggregate_cluster_v3.ClusterConfig{Clusters: []string{"c1", "c2"}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for n, tc := range cases {
|
|
|
|
t.Run(n, func(t *testing.T) {
|
|
|
|
ac, ok := isAggregateCluster(tc.input)
|
|
|
|
require.Equal(t, tc.expectedOk, ok)
|
|
|
|
|
|
|
|
if tc.expectedOk {
|
|
|
|
require.Equal(t, tc.expectedAggregateCluster.Clusters, ac.Clusters)
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakeValidate(t *testing.T) {
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
|
|
|
extensionName string
|
|
|
|
arguments map[string]interface{}
|
|
|
|
expected *Validate
|
|
|
|
snis map[string]struct{}
|
|
|
|
ok bool
|
|
|
|
}{
|
|
|
|
"with no arguments": {
|
|
|
|
arguments: nil,
|
|
|
|
ok: false,
|
|
|
|
},
|
|
|
|
"with an invalid name": {
|
|
|
|
arguments: map[string]interface{}{
|
|
|
|
"envoyID": "id",
|
|
|
|
},
|
2023-02-06 17:14:35 +00:00
|
|
|
snis: map[string]struct{}{
|
|
|
|
"sni1": {},
|
|
|
|
"sni2": {},
|
|
|
|
},
|
2023-01-27 19:43:16 +00:00
|
|
|
extensionName: "bad",
|
|
|
|
ok: false,
|
|
|
|
},
|
|
|
|
"empty envoy ID": {
|
|
|
|
arguments: map[string]interface{}{"envoyID": ""},
|
2023-02-06 17:14:35 +00:00
|
|
|
snis: map[string]struct{}{
|
|
|
|
"sni1": {},
|
|
|
|
"sni2": {},
|
|
|
|
},
|
|
|
|
ok: false,
|
|
|
|
},
|
|
|
|
"missing snis": {
|
|
|
|
arguments: map[string]interface{}{
|
|
|
|
"envoyID": "id",
|
|
|
|
},
|
|
|
|
expected: &Validate{
|
|
|
|
envoyID: "id",
|
|
|
|
resources: map[string]*resource{},
|
|
|
|
snis: map[string]struct{}{
|
|
|
|
"sni1": {},
|
|
|
|
"sni2": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ok: false,
|
2023-01-27 19:43:16 +00:00
|
|
|
},
|
|
|
|
"valid everything": {
|
|
|
|
arguments: map[string]interface{}{
|
|
|
|
"envoyID": "id",
|
|
|
|
},
|
|
|
|
snis: map[string]struct{}{
|
|
|
|
"sni1": {},
|
|
|
|
"sni2": {},
|
|
|
|
},
|
|
|
|
expected: &Validate{
|
|
|
|
envoyID: "id",
|
|
|
|
resources: map[string]*resource{},
|
2023-02-06 17:14:35 +00:00
|
|
|
snis: map[string]struct{}{
|
|
|
|
"sni1": {},
|
|
|
|
"sni2": {},
|
|
|
|
},
|
2023-01-27 19:43:16 +00:00
|
|
|
},
|
|
|
|
ok: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for n, tc := range cases {
|
|
|
|
t.Run(n, func(t *testing.T) {
|
|
|
|
|
2023-05-23 11:55:06 +00:00
|
|
|
extensionName := api.BuiltinValidateExtension
|
2023-01-27 19:43:16 +00:00
|
|
|
if tc.extensionName != "" {
|
|
|
|
extensionName = tc.extensionName
|
|
|
|
}
|
|
|
|
|
|
|
|
svc := api.CompoundServiceName{Name: "svc"}
|
2023-01-30 21:35:26 +00:00
|
|
|
ext := extensioncommon.RuntimeConfig{
|
2023-01-27 19:43:16 +00:00
|
|
|
ServiceName: svc,
|
|
|
|
EnvoyExtension: api.EnvoyExtension{
|
|
|
|
Name: extensionName,
|
|
|
|
Arguments: tc.arguments,
|
|
|
|
},
|
2023-02-06 17:14:35 +00:00
|
|
|
Upstreams: map[api.CompoundServiceName]*extensioncommon.UpstreamData{
|
|
|
|
svc: {
|
2023-05-30 18:53:42 +00:00
|
|
|
SNIs: tc.snis,
|
2023-02-06 17:14:35 +00:00
|
|
|
},
|
|
|
|
},
|
2023-01-27 19:43:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
patcher, err := MakeValidate(ext)
|
|
|
|
|
|
|
|
if tc.ok {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tc.expected, patcher)
|
|
|
|
} else {
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|