2023-08-11 13:12:13 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
2023-05-26 19:52:09 +00:00
|
|
|
package propertyoverride
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
clusterv3 "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
|
2023-05-30 18:53:42 +00:00
|
|
|
endpointv3 "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3"
|
|
|
|
listenerv3 "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3"
|
|
|
|
routev3 "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
|
2023-05-26 19:52:09 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/consul/envoyextensions/extensioncommon"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestConstructor tests raw input to the Constructor function called to initialize
|
|
|
|
// the property-override extension. This includes implicit validation of the deserialized
|
|
|
|
// input to the extension.
|
|
|
|
func TestConstructor(t *testing.T) {
|
|
|
|
// These helpers aid in constructing valid raw example input (map[string]any)
|
|
|
|
// for the Constructor, with optional overrides for fields under test.
|
|
|
|
applyOverrides := func(m map[string]any, overrides map[string]any) map[string]any {
|
|
|
|
for k, v := range overrides {
|
|
|
|
if v == nil {
|
|
|
|
delete(m, k)
|
|
|
|
} else {
|
|
|
|
m[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
makeResourceFilter := func(overrides map[string]any) map[string]any {
|
|
|
|
f := map[string]any{
|
|
|
|
"ResourceType": ResourceTypeRoute,
|
2023-05-30 18:53:42 +00:00
|
|
|
"TrafficDirection": extensioncommon.TrafficDirectionOutbound,
|
2023-05-26 19:52:09 +00:00
|
|
|
}
|
|
|
|
return applyOverrides(f, overrides)
|
|
|
|
}
|
|
|
|
makePatch := func(overrides map[string]any) map[string]any {
|
|
|
|
p := map[string]any{
|
|
|
|
"ResourceFilter": makeResourceFilter(map[string]any{}),
|
|
|
|
"Op": OpAdd,
|
|
|
|
"Path": "/name",
|
|
|
|
"Value": "foo",
|
|
|
|
}
|
|
|
|
return applyOverrides(p, overrides)
|
|
|
|
}
|
|
|
|
makeArguments := func(overrides map[string]any) map[string]any {
|
|
|
|
a := map[string]any{
|
|
|
|
"Patches": []map[string]any{
|
|
|
|
makePatch(map[string]any{}),
|
|
|
|
},
|
|
|
|
"Debug": true,
|
|
|
|
"ProxyType": api.ServiceKindConnectProxy,
|
|
|
|
}
|
|
|
|
return applyOverrides(a, overrides)
|
|
|
|
}
|
|
|
|
type testCase struct {
|
|
|
|
extensionName string
|
|
|
|
arguments map[string]any
|
|
|
|
expected propertyOverride
|
|
|
|
ok bool
|
|
|
|
errMsg string
|
2023-06-16 13:37:47 +00:00
|
|
|
errFunc func(*testing.T, error)
|
2023-05-26 19:52:09 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 18:53:42 +00:00
|
|
|
validTestCase := func(o Op, d extensioncommon.TrafficDirection, t ResourceType) testCase {
|
2023-05-26 19:52:09 +00:00
|
|
|
var v any = "foo"
|
|
|
|
if o != OpAdd {
|
|
|
|
v = nil
|
|
|
|
}
|
|
|
|
|
2023-05-29 13:42:35 +00:00
|
|
|
// Use a valid field for all resource types.
|
2023-05-26 19:52:09 +00:00
|
|
|
path := "/name"
|
2023-05-29 13:42:35 +00:00
|
|
|
if t == ResourceTypeClusterLoadAssignment {
|
|
|
|
path = "/cluster_name"
|
|
|
|
}
|
2023-05-26 19:52:09 +00:00
|
|
|
|
|
|
|
return testCase{
|
|
|
|
arguments: makeArguments(map[string]any{
|
|
|
|
"Patches": []map[string]any{
|
|
|
|
makePatch(map[string]any{
|
|
|
|
"ResourceFilter": makeResourceFilter(map[string]any{
|
|
|
|
"ResourceType": t,
|
|
|
|
"TrafficDirection": d,
|
|
|
|
}),
|
|
|
|
"Op": o,
|
|
|
|
"Path": path,
|
|
|
|
"Value": v,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
expected: propertyOverride{
|
|
|
|
Patches: []Patch{
|
|
|
|
{
|
|
|
|
ResourceFilter: ResourceFilter{
|
|
|
|
ResourceType: t,
|
|
|
|
TrafficDirection: d,
|
|
|
|
},
|
|
|
|
Op: o,
|
|
|
|
Path: path,
|
|
|
|
Value: v,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Debug: true,
|
|
|
|
ProxyType: api.ServiceKindConnectProxy,
|
|
|
|
},
|
|
|
|
ok: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cases := map[string]testCase{
|
|
|
|
"with no arguments": {
|
|
|
|
arguments: nil,
|
|
|
|
ok: false,
|
|
|
|
errMsg: "at least one patch is required",
|
|
|
|
},
|
|
|
|
"with an invalid name": {
|
|
|
|
arguments: makeArguments(map[string]any{}),
|
|
|
|
extensionName: "bad",
|
|
|
|
ok: false,
|
|
|
|
errMsg: "expected extension name \"builtin/property-override\" but got \"bad\"",
|
|
|
|
},
|
|
|
|
"empty Patches": {
|
|
|
|
arguments: makeArguments(map[string]any{"Patches": []map[string]any{}}),
|
|
|
|
ok: false,
|
|
|
|
errMsg: "at least one patch is required",
|
|
|
|
},
|
|
|
|
"patch with no ResourceFilter": {
|
|
|
|
arguments: makeArguments(map[string]any{"Patches": []map[string]any{
|
|
|
|
makePatch(map[string]any{
|
|
|
|
"ResourceFilter": nil,
|
|
|
|
}),
|
|
|
|
}}),
|
|
|
|
ok: false,
|
|
|
|
errMsg: "field ResourceFilter is required",
|
|
|
|
},
|
|
|
|
"patch with no ResourceType": {
|
|
|
|
arguments: makeArguments(map[string]any{"Patches": []map[string]any{
|
|
|
|
makePatch(map[string]any{
|
|
|
|
"ResourceFilter": makeResourceFilter(map[string]any{
|
|
|
|
"ResourceType": nil,
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
}}),
|
|
|
|
ok: false,
|
|
|
|
errMsg: "field ResourceType is required",
|
|
|
|
},
|
|
|
|
"patch with invalid ResourceType": {
|
|
|
|
arguments: makeArguments(map[string]any{"Patches": []map[string]any{
|
|
|
|
makePatch(map[string]any{
|
|
|
|
"ResourceFilter": makeResourceFilter(map[string]any{
|
|
|
|
"ResourceType": "foo",
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
}}),
|
|
|
|
ok: false,
|
|
|
|
errMsg: "invalid ResourceType",
|
|
|
|
},
|
|
|
|
"patch with no TrafficDirection": {
|
|
|
|
arguments: makeArguments(map[string]any{"Patches": []map[string]any{
|
|
|
|
makePatch(map[string]any{
|
|
|
|
"ResourceFilter": makeResourceFilter(map[string]any{
|
|
|
|
"TrafficDirection": nil,
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
}}),
|
|
|
|
ok: false,
|
|
|
|
errMsg: "field TrafficDirection is required",
|
|
|
|
},
|
|
|
|
"patch with invalid TrafficDirection": {
|
|
|
|
arguments: makeArguments(map[string]any{"Patches": []map[string]any{
|
|
|
|
makePatch(map[string]any{
|
|
|
|
"ResourceFilter": makeResourceFilter(map[string]any{
|
|
|
|
"TrafficDirection": "foo",
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
}}),
|
|
|
|
ok: false,
|
|
|
|
errMsg: "invalid TrafficDirection",
|
|
|
|
},
|
|
|
|
"patch with no Op": {
|
|
|
|
arguments: makeArguments(map[string]any{"Patches": []map[string]any{
|
|
|
|
makePatch(map[string]any{
|
|
|
|
"Op": nil,
|
|
|
|
}),
|
|
|
|
}}),
|
|
|
|
ok: false,
|
|
|
|
errMsg: "field Op is required",
|
|
|
|
},
|
|
|
|
"patch with invalid Op": {
|
|
|
|
arguments: makeArguments(map[string]any{"Patches": []map[string]any{
|
|
|
|
makePatch(map[string]any{
|
|
|
|
"Op": "foo",
|
|
|
|
}),
|
|
|
|
}}),
|
|
|
|
ok: false,
|
|
|
|
errMsg: "invalid Op",
|
|
|
|
},
|
|
|
|
"patch with invalid Envoy resource Path": {
|
|
|
|
arguments: makeArguments(map[string]any{"Patches": []map[string]any{
|
|
|
|
makePatch(map[string]any{
|
|
|
|
"Path": "/invalid",
|
|
|
|
}),
|
|
|
|
}}),
|
|
|
|
ok: false,
|
|
|
|
errMsg: "no match for field", // this error comes from the patcher dry-run attempt
|
|
|
|
},
|
|
|
|
"non-Add patch with Value": {
|
|
|
|
arguments: makeArguments(map[string]any{"Patches": []map[string]any{
|
|
|
|
makePatch(map[string]any{
|
|
|
|
"Op": OpRemove,
|
|
|
|
"Value": 1,
|
|
|
|
}),
|
|
|
|
}}),
|
|
|
|
ok: false,
|
|
|
|
errMsg: fmt.Sprintf("field Value is not supported for %s operation", OpRemove),
|
|
|
|
},
|
2023-06-16 13:37:47 +00:00
|
|
|
"multiple patches includes indexed errors": {
|
|
|
|
arguments: makeArguments(map[string]any{"Patches": []map[string]any{
|
|
|
|
makePatch(map[string]any{
|
|
|
|
"Op": OpRemove,
|
|
|
|
"Value": 0,
|
|
|
|
}),
|
|
|
|
makePatch(map[string]any{
|
|
|
|
"Op": OpAdd,
|
|
|
|
"Value": nil,
|
|
|
|
}),
|
|
|
|
makePatch(map[string]any{
|
|
|
|
"Op": OpAdd,
|
|
|
|
"Path": "/foo",
|
|
|
|
}),
|
|
|
|
}}),
|
|
|
|
ok: false,
|
|
|
|
errFunc: func(t *testing.T, err error) {
|
|
|
|
require.ErrorContains(t, err, "invalid Patches[0]: field Value is not supported for remove operation")
|
|
|
|
require.ErrorContains(t, err, "invalid Patches[1]: non-nil Value is required")
|
|
|
|
require.ErrorContains(t, err, "invalid Patches[2]: no match for field 'foo'")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"multiple patches single error contains correct index": {
|
|
|
|
arguments: makeArguments(map[string]any{"Patches": []map[string]any{
|
|
|
|
makePatch(map[string]any{
|
|
|
|
"Op": OpAdd,
|
|
|
|
"Value": "foo",
|
|
|
|
}),
|
|
|
|
makePatch(map[string]any{
|
|
|
|
"Op": OpRemove,
|
|
|
|
"Value": 1,
|
|
|
|
}),
|
|
|
|
makePatch(map[string]any{
|
|
|
|
"Op": OpAdd,
|
|
|
|
"Value": "bar",
|
|
|
|
}),
|
|
|
|
}}),
|
|
|
|
ok: false,
|
|
|
|
errFunc: func(t *testing.T, err error) {
|
|
|
|
require.ErrorContains(t, err, "invalid Patches[1]: field Value is not supported for remove operation")
|
|
|
|
require.NotContains(t, err.Error(), "invalid Patches[0]")
|
|
|
|
require.NotContains(t, err.Error(), "invalid Patches[2]")
|
|
|
|
},
|
|
|
|
},
|
2023-05-30 18:53:42 +00:00
|
|
|
"empty service name": {
|
|
|
|
arguments: makeArguments(map[string]any{"Patches": []map[string]any{
|
|
|
|
makePatch(map[string]any{
|
|
|
|
"ResourceFilter": makeResourceFilter(map[string]any{
|
2023-06-06 19:40:37 +00:00
|
|
|
"Services": []map[string]any{
|
|
|
|
{},
|
2023-05-30 18:53:42 +00:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
}}),
|
|
|
|
ok: false,
|
|
|
|
errMsg: "service name is required",
|
|
|
|
},
|
2023-06-15 17:51:47 +00:00
|
|
|
"non-empty services with invalid traffic direction": {
|
|
|
|
arguments: makeArguments(map[string]any{"Patches": []map[string]any{
|
|
|
|
makePatch(map[string]any{
|
|
|
|
"ResourceFilter": makeResourceFilter(map[string]any{
|
|
|
|
"TrafficDirection": extensioncommon.TrafficDirectionInbound,
|
|
|
|
"Services": []map[string]any{
|
|
|
|
{"Name:": "foo"},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
}}),
|
|
|
|
ok: false,
|
|
|
|
errMsg: "patch contains non-empty ResourceFilter.Services but ResourceFilter.TrafficDirection is not \"outbound\"",
|
|
|
|
},
|
2023-05-26 19:52:09 +00:00
|
|
|
// See decode.HookWeakDecodeFromSlice for more details. In practice, we can end up
|
|
|
|
// with a "Patches" field decoded to the single "Patch" value contained in the
|
|
|
|
// serialized slice (raised from the containing slice). Using WeakDecode solves
|
|
|
|
// for this. Ideally, we would kill that decoding hook entirely, but this test
|
|
|
|
// enforces expected behavior until we do. Multi-member slices should be unaffected
|
|
|
|
// by WeakDecode as it is a more-permissive version of the default behavior.
|
|
|
|
"single value Patches decoded as map construction succeeds": {
|
2023-06-12 17:47:31 +00:00
|
|
|
arguments: makeArguments(map[string]any{"Patches": makePatch(map[string]any{}), "ProxyType": nil}),
|
2023-05-30 18:53:42 +00:00
|
|
|
expected: validTestCase(OpAdd, extensioncommon.TrafficDirectionOutbound, ResourceTypeRoute).expected,
|
2023-05-26 19:52:09 +00:00
|
|
|
ok: true,
|
|
|
|
},
|
2023-06-06 19:40:37 +00:00
|
|
|
// Ensure that embedded api struct used for Services is parsed correctly.
|
|
|
|
// See also above comment on decode.HookWeakDecodeFromSlice.
|
|
|
|
"single value Services decoded as map construction succeeds": {
|
|
|
|
arguments: makeArguments(map[string]any{"Patches": []map[string]any{
|
|
|
|
makePatch(map[string]any{
|
|
|
|
"ResourceFilter": makeResourceFilter(map[string]any{
|
|
|
|
"Services": []map[string]any{
|
|
|
|
{"Name": "foo"},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
}}),
|
|
|
|
expected: propertyOverride{
|
|
|
|
Patches: []Patch{
|
|
|
|
{
|
|
|
|
ResourceFilter: ResourceFilter{
|
|
|
|
ResourceType: ResourceTypeRoute,
|
|
|
|
TrafficDirection: extensioncommon.TrafficDirectionOutbound,
|
|
|
|
Services: []*ServiceName{
|
|
|
|
{CompoundServiceName: api.CompoundServiceName{
|
|
|
|
Name: "foo",
|
|
|
|
Namespace: "default",
|
|
|
|
Partition: "default",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Op: OpAdd,
|
|
|
|
Path: "/name",
|
|
|
|
Value: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Debug: true,
|
|
|
|
ProxyType: api.ServiceKindConnectProxy,
|
|
|
|
},
|
|
|
|
ok: true,
|
|
|
|
},
|
2023-05-26 19:52:09 +00:00
|
|
|
"invalid ProxyType": {
|
|
|
|
arguments: makeArguments(map[string]any{
|
|
|
|
"Patches": []map[string]any{
|
|
|
|
makePatch(map[string]any{}),
|
|
|
|
},
|
|
|
|
"ProxyType": "invalid",
|
|
|
|
}),
|
|
|
|
ok: false,
|
|
|
|
errMsg: "invalid ProxyType",
|
|
|
|
},
|
|
|
|
"unsupported ProxyType": {
|
|
|
|
arguments: makeArguments(map[string]any{
|
|
|
|
"Patches": []map[string]any{
|
|
|
|
makePatch(map[string]any{}),
|
|
|
|
},
|
|
|
|
"ProxyType": api.ServiceKindMeshGateway,
|
|
|
|
}),
|
|
|
|
ok: false,
|
|
|
|
errMsg: "invalid ProxyType",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for o := range Ops {
|
2023-05-30 18:53:42 +00:00
|
|
|
for d := range extensioncommon.TrafficDirections {
|
2023-05-26 19:52:09 +00:00
|
|
|
for t := range ResourceTypes {
|
|
|
|
cases["valid everything: "+strings.Join([]string{o, d, t}, ",")] =
|
2023-05-30 18:53:42 +00:00
|
|
|
validTestCase(Op(o), extensioncommon.TrafficDirection(d), ResourceType(t))
|
2023-05-26 19:52:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for n, tc := range cases {
|
|
|
|
t.Run(n, func(t *testing.T) {
|
|
|
|
|
|
|
|
extensionName := api.BuiltinPropertyOverrideExtension
|
|
|
|
if tc.extensionName != "" {
|
|
|
|
extensionName = tc.extensionName
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the wrapping RuntimeConfig struct, which contains the serialized
|
|
|
|
// arguments for constructing the property-override extension.
|
|
|
|
svc := api.CompoundServiceName{Name: "svc"}
|
|
|
|
ext := extensioncommon.RuntimeConfig{
|
|
|
|
ServiceName: svc,
|
|
|
|
EnvoyExtension: api.EnvoyExtension{
|
|
|
|
Name: extensionName,
|
|
|
|
Arguments: tc.arguments,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct the actual extension
|
|
|
|
e, err := Constructor(ext.EnvoyExtension)
|
|
|
|
|
|
|
|
if tc.ok {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, &extensioncommon.BasicEnvoyExtender{Extension: &tc.expected}, e)
|
|
|
|
} else {
|
2023-06-16 13:37:47 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
if tc.errMsg != "" {
|
|
|
|
require.ErrorContains(t, err, tc.errMsg)
|
|
|
|
}
|
|
|
|
if tc.errFunc != nil {
|
|
|
|
tc.errFunc(t, err)
|
|
|
|
}
|
2023-05-26 19:52:09 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_patchResourceType(t *testing.T) {
|
|
|
|
makeExtension := func(patches ...Patch) *propertyOverride {
|
|
|
|
return &propertyOverride{
|
|
|
|
Patches: patches,
|
|
|
|
}
|
|
|
|
}
|
2023-05-30 18:53:42 +00:00
|
|
|
makePatchWithPath := func(filter ResourceFilter, p string) Patch {
|
2023-05-26 19:52:09 +00:00
|
|
|
return Patch{
|
2023-05-30 18:53:42 +00:00
|
|
|
ResourceFilter: filter,
|
|
|
|
Op: OpAdd,
|
|
|
|
Path: p,
|
|
|
|
Value: 1,
|
2023-05-26 19:52:09 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-30 18:53:42 +00:00
|
|
|
makePatch := func(filter ResourceFilter) Patch {
|
|
|
|
return makePatchWithPath(filter, "/foo")
|
2023-05-26 19:52:09 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 18:53:42 +00:00
|
|
|
svc1 := ServiceName{
|
|
|
|
CompoundServiceName: api.CompoundServiceName{Name: "svc1"},
|
|
|
|
}
|
|
|
|
svc2 := ServiceName{
|
|
|
|
CompoundServiceName: api.CompoundServiceName{Name: "svc2"},
|
|
|
|
}
|
|
|
|
|
|
|
|
clusterOutbound := makePatch(ResourceFilter{
|
|
|
|
ResourceType: ResourceTypeCluster,
|
|
|
|
TrafficDirection: extensioncommon.TrafficDirectionOutbound,
|
|
|
|
})
|
|
|
|
clusterInbound := makePatch(ResourceFilter{
|
|
|
|
ResourceType: ResourceTypeCluster,
|
|
|
|
TrafficDirection: extensioncommon.TrafficDirectionInbound,
|
|
|
|
})
|
|
|
|
listenerOutbound := makePatch(ResourceFilter{
|
|
|
|
ResourceType: ResourceTypeListener,
|
|
|
|
TrafficDirection: extensioncommon.TrafficDirectionOutbound,
|
|
|
|
})
|
|
|
|
listenerOutbound2 := makePatchWithPath(ResourceFilter{
|
|
|
|
ResourceType: ResourceTypeListener,
|
|
|
|
TrafficDirection: extensioncommon.TrafficDirectionOutbound,
|
|
|
|
}, "/bar")
|
|
|
|
listenerInbound := makePatch(ResourceFilter{
|
|
|
|
ResourceType: ResourceTypeListener,
|
|
|
|
TrafficDirection: extensioncommon.TrafficDirectionInbound,
|
|
|
|
})
|
|
|
|
routeOutbound := makePatch(ResourceFilter{
|
|
|
|
ResourceType: ResourceTypeRoute,
|
|
|
|
TrafficDirection: extensioncommon.TrafficDirectionOutbound,
|
|
|
|
})
|
|
|
|
routeOutbound2 := makePatchWithPath(ResourceFilter{
|
|
|
|
ResourceType: ResourceTypeRoute,
|
|
|
|
TrafficDirection: extensioncommon.TrafficDirectionOutbound,
|
|
|
|
}, "/bar")
|
|
|
|
routeInbound := makePatch(ResourceFilter{
|
|
|
|
ResourceType: ResourceTypeRoute,
|
|
|
|
TrafficDirection: extensioncommon.TrafficDirectionInbound,
|
|
|
|
})
|
2023-05-26 19:52:09 +00:00
|
|
|
|
|
|
|
type args struct {
|
2023-05-30 18:53:42 +00:00
|
|
|
resourceType ResourceType
|
|
|
|
payload extensioncommon.Payload[proto.Message]
|
|
|
|
p *propertyOverride
|
2023-05-26 19:52:09 +00:00
|
|
|
}
|
|
|
|
type testCase struct {
|
|
|
|
args args
|
|
|
|
expectPatched bool
|
|
|
|
wantApplied []Patch
|
|
|
|
}
|
|
|
|
cases := map[string]testCase{
|
|
|
|
"outbound gets matching patch": {
|
|
|
|
args: args{
|
2023-05-30 18:53:42 +00:00
|
|
|
resourceType: ResourceTypeCluster,
|
|
|
|
payload: extensioncommon.Payload[proto.Message]{
|
|
|
|
TrafficDirection: extensioncommon.TrafficDirectionOutbound,
|
|
|
|
Message: &clusterv3.Cluster{},
|
|
|
|
},
|
2023-05-26 19:52:09 +00:00
|
|
|
p: makeExtension(clusterOutbound),
|
|
|
|
},
|
|
|
|
expectPatched: true,
|
|
|
|
wantApplied: []Patch{clusterOutbound},
|
|
|
|
},
|
|
|
|
"inbound gets matching patch": {
|
|
|
|
args: args{
|
2023-05-30 18:53:42 +00:00
|
|
|
resourceType: ResourceTypeCluster,
|
|
|
|
payload: extensioncommon.Payload[proto.Message]{
|
|
|
|
TrafficDirection: extensioncommon.TrafficDirectionInbound,
|
|
|
|
Message: &clusterv3.Cluster{},
|
|
|
|
},
|
2023-05-26 19:52:09 +00:00
|
|
|
p: makeExtension(clusterInbound),
|
|
|
|
},
|
|
|
|
expectPatched: true,
|
|
|
|
wantApplied: []Patch{clusterInbound},
|
|
|
|
},
|
|
|
|
"multiple resources same direction only gets matching resource": {
|
|
|
|
args: args{
|
2023-05-30 18:53:42 +00:00
|
|
|
resourceType: ResourceTypeCluster,
|
|
|
|
payload: extensioncommon.Payload[proto.Message]{
|
|
|
|
TrafficDirection: extensioncommon.TrafficDirectionOutbound,
|
|
|
|
Message: &clusterv3.Cluster{},
|
|
|
|
},
|
|
|
|
p: makeExtension(clusterOutbound, listenerOutbound),
|
2023-05-26 19:52:09 +00:00
|
|
|
},
|
|
|
|
expectPatched: true,
|
|
|
|
wantApplied: []Patch{clusterOutbound},
|
|
|
|
},
|
|
|
|
"multiple directions same resource only gets matching direction": {
|
|
|
|
args: args{
|
2023-05-30 18:53:42 +00:00
|
|
|
resourceType: ResourceTypeCluster,
|
|
|
|
payload: extensioncommon.Payload[proto.Message]{
|
|
|
|
TrafficDirection: extensioncommon.TrafficDirectionOutbound,
|
|
|
|
Message: &clusterv3.Cluster{},
|
|
|
|
},
|
2023-05-26 19:52:09 +00:00
|
|
|
p: makeExtension(clusterOutbound, clusterInbound),
|
|
|
|
},
|
|
|
|
expectPatched: true,
|
|
|
|
wantApplied: []Patch{clusterOutbound},
|
|
|
|
},
|
|
|
|
"multiple directions and resources only gets matching patch": {
|
|
|
|
args: args{
|
2023-05-30 18:53:42 +00:00
|
|
|
resourceType: ResourceTypeRoute,
|
|
|
|
payload: extensioncommon.Payload[proto.Message]{
|
|
|
|
TrafficDirection: extensioncommon.TrafficDirectionInbound,
|
|
|
|
Message: &routev3.RouteConfiguration{},
|
|
|
|
},
|
|
|
|
p: makeExtension(clusterOutbound, clusterInbound, listenerOutbound, listenerInbound, routeOutbound, routeOutbound2, routeInbound),
|
2023-05-26 19:52:09 +00:00
|
|
|
},
|
|
|
|
expectPatched: true,
|
|
|
|
wantApplied: []Patch{routeInbound},
|
|
|
|
},
|
|
|
|
"multiple directions and resources multiple matches gets all matching patches": {
|
|
|
|
args: args{
|
2023-05-30 18:53:42 +00:00
|
|
|
resourceType: ResourceTypeRoute,
|
|
|
|
payload: extensioncommon.Payload[proto.Message]{
|
|
|
|
TrafficDirection: extensioncommon.TrafficDirectionOutbound,
|
|
|
|
Message: &routev3.RouteConfiguration{},
|
|
|
|
},
|
|
|
|
p: makeExtension(clusterOutbound, clusterInbound, listenerOutbound, listenerInbound, listenerOutbound2, routeOutbound, routeOutbound2, routeInbound),
|
2023-05-26 19:52:09 +00:00
|
|
|
},
|
|
|
|
expectPatched: true,
|
|
|
|
wantApplied: []Patch{routeOutbound, routeOutbound2},
|
|
|
|
},
|
|
|
|
"multiple directions and resources no matches gets no patches": {
|
|
|
|
args: args{
|
2023-05-30 18:53:42 +00:00
|
|
|
resourceType: ResourceTypeCluster,
|
|
|
|
payload: extensioncommon.Payload[proto.Message]{
|
|
|
|
TrafficDirection: extensioncommon.TrafficDirectionOutbound,
|
|
|
|
Message: &clusterv3.Cluster{},
|
|
|
|
},
|
|
|
|
p: makeExtension(clusterInbound, listenerOutbound, listenerInbound, listenerOutbound2, routeInbound, routeOutbound),
|
2023-05-26 19:52:09 +00:00
|
|
|
},
|
|
|
|
expectPatched: false,
|
|
|
|
wantApplied: nil,
|
|
|
|
},
|
|
|
|
}
|
2023-05-30 18:53:42 +00:00
|
|
|
|
|
|
|
type resourceTypeServiceMatch struct {
|
|
|
|
resourceType ResourceType
|
|
|
|
message proto.Message
|
|
|
|
}
|
|
|
|
|
|
|
|
resourceTypeCases := []resourceTypeServiceMatch{
|
|
|
|
{
|
|
|
|
resourceType: ResourceTypeCluster,
|
|
|
|
message: &clusterv3.Cluster{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
resourceType: ResourceTypeListener,
|
|
|
|
message: &listenerv3.Listener{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
resourceType: ResourceTypeRoute,
|
|
|
|
message: &routev3.RouteConfiguration{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
resourceType: ResourceTypeClusterLoadAssignment,
|
|
|
|
message: &endpointv3.ClusterLoadAssignment{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range resourceTypeCases {
|
|
|
|
{
|
|
|
|
patch := makePatch(ResourceFilter{
|
|
|
|
ResourceType: tc.resourceType,
|
|
|
|
TrafficDirection: extensioncommon.TrafficDirectionOutbound,
|
|
|
|
Services: []*ServiceName{
|
|
|
|
{CompoundServiceName: svc2.CompoundServiceName},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
cases[fmt.Sprintf("%s - no match", tc.resourceType)] = testCase{
|
|
|
|
args: args{
|
|
|
|
resourceType: tc.resourceType,
|
|
|
|
payload: extensioncommon.Payload[proto.Message]{
|
|
|
|
TrafficDirection: extensioncommon.TrafficDirectionOutbound,
|
|
|
|
ServiceName: &svc1.CompoundServiceName,
|
|
|
|
Message: tc.message,
|
|
|
|
RuntimeConfig: &extensioncommon.RuntimeConfig{
|
|
|
|
Upstreams: map[api.CompoundServiceName]*extensioncommon.UpstreamData{
|
|
|
|
svc1.CompoundServiceName: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
p: makeExtension(patch),
|
|
|
|
},
|
|
|
|
expectPatched: false,
|
|
|
|
wantApplied: nil,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
patch := makePatch(ResourceFilter{
|
|
|
|
ResourceType: tc.resourceType,
|
|
|
|
TrafficDirection: extensioncommon.TrafficDirectionOutbound,
|
|
|
|
Services: []*ServiceName{
|
|
|
|
{CompoundServiceName: svc2.CompoundServiceName},
|
|
|
|
{CompoundServiceName: svc1.CompoundServiceName},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
cases[fmt.Sprintf("%s - match", tc.resourceType)] = testCase{
|
|
|
|
args: args{
|
|
|
|
resourceType: tc.resourceType,
|
|
|
|
payload: extensioncommon.Payload[proto.Message]{
|
|
|
|
TrafficDirection: extensioncommon.TrafficDirectionOutbound,
|
|
|
|
ServiceName: &svc1.CompoundServiceName,
|
|
|
|
Message: tc.message,
|
|
|
|
RuntimeConfig: &extensioncommon.RuntimeConfig{
|
|
|
|
Upstreams: map[api.CompoundServiceName]*extensioncommon.UpstreamData{
|
|
|
|
svc1.CompoundServiceName: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
p: makeExtension(patch),
|
|
|
|
},
|
|
|
|
expectPatched: true,
|
|
|
|
wantApplied: []Patch{patch},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-26 19:52:09 +00:00
|
|
|
for n, tc := range cases {
|
|
|
|
t.Run(n, func(t *testing.T) {
|
|
|
|
mockPatcher := MockPatcher[proto.Message]{}
|
2023-05-30 18:53:42 +00:00
|
|
|
_, patched, err := patchResourceType[proto.Message](tc.args.p, tc.args.resourceType, tc.args.payload, &mockPatcher)
|
2023-05-26 19:52:09 +00:00
|
|
|
|
|
|
|
require.NoError(t, err, "unexpected error from mock")
|
|
|
|
require.Equal(t, tc.expectPatched, patched)
|
|
|
|
require.Equal(t, tc.wantApplied, mockPatcher.appliedPatches)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type MockPatcher[K proto.Message] struct {
|
|
|
|
appliedPatches []Patch
|
|
|
|
}
|
|
|
|
|
2023-06-05 20:08:39 +00:00
|
|
|
//nolint:unparam
|
2023-05-26 19:52:09 +00:00
|
|
|
func (m *MockPatcher[K]) applyPatch(k K, p Patch, _ bool) (result K, e error) {
|
|
|
|
m.appliedPatches = append(m.appliedPatches, p)
|
|
|
|
return k, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCanApply(t *testing.T) {
|
|
|
|
cases := map[string]struct {
|
|
|
|
ext *propertyOverride
|
|
|
|
conf *extensioncommon.RuntimeConfig
|
|
|
|
canApply bool
|
|
|
|
}{
|
|
|
|
"valid proxy type": {
|
|
|
|
ext: &propertyOverride{
|
|
|
|
ProxyType: api.ServiceKindConnectProxy,
|
|
|
|
},
|
|
|
|
conf: &extensioncommon.RuntimeConfig{
|
|
|
|
Kind: api.ServiceKindConnectProxy,
|
|
|
|
},
|
|
|
|
canApply: true,
|
|
|
|
},
|
|
|
|
"invalid proxy type": {
|
|
|
|
ext: &propertyOverride{
|
2023-06-07 19:39:25 +00:00
|
|
|
ProxyType: api.ServiceKindConnectProxy,
|
2023-05-26 19:52:09 +00:00
|
|
|
},
|
|
|
|
conf: &extensioncommon.RuntimeConfig{
|
|
|
|
Kind: api.ServiceKindMeshGateway,
|
|
|
|
},
|
|
|
|
canApply: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for n, tc := range cases {
|
|
|
|
t.Run(n, func(t *testing.T) {
|
|
|
|
require.Equal(t, tc.canApply, tc.ext.CanApply(tc.conf))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|