2023-09-12 01:17:56 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
|
|
package xdsv2
|
|
|
|
|
|
|
|
import (
|
2023-10-24 16:23:27 +00:00
|
|
|
"fmt"
|
|
|
|
envoy_endpoint_v3 "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3"
|
|
|
|
envoy_route_v3 "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
|
|
|
|
envoy_tls_v3 "github.com/envoyproxy/go-control-plane/envoy/extensions/transport_sockets/tls/v3"
|
|
|
|
"github.com/hashicorp/consul/internal/testing/golden"
|
2023-09-20 00:02:01 +00:00
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
|
2023-09-12 01:17:56 +00:00
|
|
|
envoy_cluster_v3 "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
|
|
|
|
envoy_listener_v3 "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3"
|
2023-09-20 00:02:01 +00:00
|
|
|
|
2023-09-12 01:17:56 +00:00
|
|
|
"github.com/hashicorp/consul/agent/xds/response"
|
|
|
|
"github.com/hashicorp/consul/envoyextensions/xdscommon"
|
|
|
|
proxytracker "github.com/hashicorp/consul/internal/mesh/proxy-tracker"
|
2023-09-22 16:51:15 +00:00
|
|
|
meshv2beta1 "github.com/hashicorp/consul/proto-public/pbmesh/v2beta1"
|
2023-09-12 01:17:56 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
)
|
|
|
|
|
2023-10-24 16:23:27 +00:00
|
|
|
var testTypeUrlToPrettyName = map[string]string{
|
|
|
|
xdscommon.ListenerType: "listeners",
|
|
|
|
xdscommon.RouteType: "routes",
|
|
|
|
xdscommon.ClusterType: "clusters",
|
|
|
|
xdscommon.EndpointType: "endpoints",
|
|
|
|
xdscommon.SecretType: "secrets",
|
|
|
|
}
|
2023-09-12 01:17:56 +00:00
|
|
|
|
2023-10-24 16:23:27 +00:00
|
|
|
// TestAllResourcesFromIR_XDSGoldenFileInputs tests the AllResourcesFromIR() by
|
|
|
|
// using the golden test output/expected files from the XDS controller tests as
|
|
|
|
// inputs to the XDSV2 resources generation.
|
|
|
|
func TestAllResourcesFromIR_XDSGoldenFileInputs(t *testing.T) {
|
|
|
|
inputPath := "../../internal/mesh/internal/controllers/xds"
|
|
|
|
|
|
|
|
cases := []string{
|
|
|
|
// destinations - please add in alphabetical order
|
|
|
|
"destination/l4-single-destination-ip-port-bind-address",
|
|
|
|
"destination/l4-single-destination-unix-socket-bind-address",
|
|
|
|
"destination/l4-single-implicit-destination-tproxy",
|
|
|
|
"destination/l4-multi-destination",
|
|
|
|
"destination/l4-multiple-implicit-destinations-tproxy",
|
|
|
|
"destination/l4-implicit-and-explicit-destinations-tproxy",
|
|
|
|
"destination/mixed-multi-destination",
|
|
|
|
"destination/multiport-l4-and-l7-multiple-implicit-destinations-tproxy",
|
|
|
|
"destination/multiport-l4-and-l7-single-implicit-destination-tproxy",
|
|
|
|
"destination/multiport-l4-and-l7-single-implicit-destination-with-multiple-workloads-tproxy",
|
|
|
|
|
|
|
|
//sources - please add in alphabetical order
|
2023-10-24 17:00:30 +00:00
|
|
|
"source/l7-expose-paths",
|
|
|
|
"source/local-and-inbound-connections",
|
2023-11-02 18:31:06 +00:00
|
|
|
"source/multiple-workload-addresses-with-specific-ports",
|
|
|
|
"source/multiple-workload-addresses-without-ports",
|
2023-10-24 17:00:30 +00:00
|
|
|
"source/multiport-l4-multiple-workload-addresses-with-specific-ports",
|
|
|
|
"source/multiport-l4-multiple-workload-addresses-without-ports",
|
|
|
|
"source/multiport-l4-workload-with-only-mesh-port",
|
|
|
|
"source/multiport-l7-multiple-workload-addresses-with-specific-ports",
|
|
|
|
"source/multiport-l7-multiple-workload-addresses-without-ports",
|
2023-11-02 18:31:06 +00:00
|
|
|
"source/single-workload-address-without-ports",
|
2023-09-12 01:17:56 +00:00
|
|
|
}
|
|
|
|
|
2023-10-24 16:23:27 +00:00
|
|
|
for _, name := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
// Arrange - paths to input and output golden files.
|
|
|
|
testFile := fmt.Sprintf("%s.golden", name)
|
|
|
|
inputFilePath := fmt.Sprintf("%s/testdata/%s", inputPath, testFile)
|
|
|
|
inputValueInput := golden.GetBytesAtFilePath(t, inputFilePath)
|
|
|
|
|
|
|
|
// Act.
|
|
|
|
ps := jsonToProxyState(t, inputValueInput)
|
|
|
|
generator := NewResourceGenerator(testutil.Logger(t))
|
|
|
|
resources, err := generator.AllResourcesFromIR(&proxytracker.ProxyState{ProxyState: ps})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Assert.
|
|
|
|
// Assert all resources were generated.
|
|
|
|
typeUrls := []string{
|
|
|
|
xdscommon.ListenerType,
|
|
|
|
xdscommon.RouteType,
|
|
|
|
xdscommon.ClusterType,
|
|
|
|
xdscommon.EndpointType,
|
|
|
|
// TODO(proxystate): add in future
|
|
|
|
//xdscommon.SecretType,
|
|
|
|
}
|
|
|
|
require.Len(t, resources, len(typeUrls))
|
|
|
|
|
|
|
|
// Assert each resource type has actual XDS matching expected XDS.
|
|
|
|
for _, typeUrl := range typeUrls {
|
|
|
|
prettyName := testTypeUrlToPrettyName[typeUrl]
|
|
|
|
t.Run(prettyName, func(t *testing.T) {
|
|
|
|
items, ok := resources[typeUrl]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
// sort resources so they don't show up as flakey tests as
|
|
|
|
// ordering in JSON is not guaranteed.
|
|
|
|
sort.Slice(items, func(i, j int) bool {
|
|
|
|
switch typeUrl {
|
|
|
|
case xdscommon.ListenerType:
|
|
|
|
return items[i].(*envoy_listener_v3.Listener).Name < items[j].(*envoy_listener_v3.Listener).Name
|
|
|
|
case xdscommon.RouteType:
|
|
|
|
return items[i].(*envoy_route_v3.RouteConfiguration).Name < items[j].(*envoy_route_v3.RouteConfiguration).Name
|
|
|
|
case xdscommon.ClusterType:
|
|
|
|
return items[i].(*envoy_cluster_v3.Cluster).Name < items[j].(*envoy_cluster_v3.Cluster).Name
|
|
|
|
case xdscommon.EndpointType:
|
|
|
|
return items[i].(*envoy_endpoint_v3.ClusterLoadAssignment).ClusterName < items[j].(*envoy_endpoint_v3.ClusterLoadAssignment).ClusterName
|
|
|
|
case xdscommon.SecretType:
|
|
|
|
return items[i].(*envoy_tls_v3.Secret).Name < items[j].(*envoy_tls_v3.Secret).Name
|
|
|
|
default:
|
|
|
|
panic("not possible")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Compare actual to expected.
|
|
|
|
resp, err := response.CreateResponse(typeUrl, "00000001", "00000001", items)
|
|
|
|
require.NoError(t, err)
|
|
|
|
gotJSON := protoToJSON(t, resp)
|
|
|
|
|
|
|
|
expectedJSON := golden.Get(t, gotJSON, fmt.Sprintf("%s/%s", prettyName, testFile))
|
|
|
|
require.JSONEq(t, expectedJSON, gotJSON)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2023-09-12 01:17:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func protoToJSON(t *testing.T, pb proto.Message) string {
|
|
|
|
t.Helper()
|
|
|
|
m := protojson.MarshalOptions{
|
|
|
|
Indent: " ",
|
|
|
|
}
|
|
|
|
gotJSON, err := m.Marshal(pb)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return string(gotJSON)
|
|
|
|
}
|
|
|
|
|
2023-10-24 16:23:27 +00:00
|
|
|
func jsonToProxyState(t *testing.T, json []byte) *meshv2beta1.ProxyState {
|
2023-09-12 01:17:56 +00:00
|
|
|
t.Helper()
|
|
|
|
um := protojson.UnmarshalOptions{}
|
2023-10-24 16:23:27 +00:00
|
|
|
ps := &meshv2beta1.ProxyState{}
|
|
|
|
err := um.Unmarshal(json, ps)
|
2023-09-12 01:17:56 +00:00
|
|
|
require.NoError(t, err)
|
2023-10-24 16:23:27 +00:00
|
|
|
return ps
|
2023-09-12 01:17:56 +00:00
|
|
|
}
|