From 1b4c03536976c9c859b5ec48dbee881c2215a574 Mon Sep 17 00:00:00 2001 From: John Murret Date: Thu, 12 Oct 2023 10:16:12 -0600 Subject: [PATCH] NET-5397 - wire up golden tests from sidecar-proxy controller for xds controller and xdsv2 --- .../controllers/xds/controller_test.go | 72 ++++++++++++++++++- internal/testing/golden/golden.go | 10 ++- 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/internal/mesh/internal/controllers/xds/controller_test.go b/internal/mesh/internal/controllers/xds/controller_test.go index 6bb5f85c9908f..3167ae4c147c2 100644 --- a/internal/mesh/internal/controllers/xds/controller_test.go +++ b/internal/mesh/internal/controllers/xds/controller_test.go @@ -7,10 +7,12 @@ import ( "context" "crypto/x509" "encoding/pem" - "testing" - + "fmt" + "github.com/hashicorp/consul/internal/testing/golden" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" + "google.golang.org/protobuf/encoding/protojson" + "testing" svctest "github.com/hashicorp/consul/agent/grpc-external/services/resource/testing" "github.com/hashicorp/consul/agent/leafcert" @@ -995,3 +997,69 @@ func (suite *xdsControllerTestSuite) TestReconcile_prevWatchesToCancel() { func TestXdsController(t *testing.T) { suite.Run(t, new(xdsControllerTestSuite)) } + +func (suite *xdsControllerTestSuite) TestBuildExplicitDestinations() { + path := "../sidecarproxy/builder/testdata" + cases := []string{ + "destination/l4-single-destination-ip-port-bind-address", + "destination/l4-single-destination-unix-socket-bind-address", + "destination/l4-multi-destination", + "destination/mixed-multi-destination", + } + + for _, name := range cases { + suite.Run(name, func() { + // Create ProxyStateTemplate from the golden file. + pst := JSONToProxyTemplate(suite.T(), + golden.GetBytesAtFilePath(suite.T(), fmt.Sprintf("%s/%s.golden", path, name))) + + // Store the initial ProxyStateTemplate and track it in the mapper. + proxyStateTemplate := resourcetest.Resource(pbmesh.ProxyStateTemplateType, "test"). + WithData(suite.T(), pst). + Write(suite.T(), suite.client) + + suite.mapper.TrackItem(proxyStateTemplate.Id, []resource.ReferenceOrID{}) + pst.ProxyState.Endpoints + + // Run the reconcile, and since no ProxyStateTemplate is stored, this simulates a deletion. + err := suite.ctl.Reconcile(context.Background(), suite.runtime, controller.Request{ + ID: proxyStateTemplate.Id, + }) + require.NoError(suite.T(), err) + + require.NotNil(suite.T(), proxyStateTemplate) + //require.JSONEq(suite.T(), expected, actual) + }) + } +} + +func (suite *xdsControllerTestSuite) TestBuildImplicitDestinations() { + + cases := []string{ + "destination/l4-single-implicit-destination-tproxy", + "destination/l4-multiple-implicit-destinations-tproxy", + "destination/l4-implicit-and-explicit-destinations-tproxy", + } + + for _, name := range cases { + suite.Run(name, func() { + //proxyTmpl := New(testProxyStateTemplateID(), testIdentityRef(), "foo.consul", "dc1", false, proxyCfg). + // BuildDestinations(c.destinations). + // Build() + // + //actual := protoToJSON(t, proxyTmpl) + //expected := golden.Get(t, actual, name+".golden") + // + //require.JSONEq(t, expected, actual) + }) + } +} + +func JSONToProxyTemplate(t *testing.T, json []byte) *pbmesh.ProxyStateTemplate { + t.Helper() + proxyTemplate := &pbmesh.ProxyStateTemplate{} + m := protojson.UnmarshalOptions{} + err := m.Unmarshal(json, proxyTemplate) + require.NoError(t, err) + return proxyTemplate +} diff --git a/internal/testing/golden/golden.go b/internal/testing/golden/golden.go index 8145d16814bf8..a4d971d0d0960 100644 --- a/internal/testing/golden/golden.go +++ b/internal/testing/golden/golden.go @@ -42,7 +42,15 @@ func GetBytes(t *testing.T, actual, filename string) []byte { require.NoError(t, err) } - expected, err := os.ReadFile(path) + return GetBytesAtFilePath(t, path) +} + +// GetBytes reads the expected value from the file at filepath and returns the +// value as a byte array. filepath is relative to the ./testdata directory. +func GetBytesAtFilePath(t *testing.T, filepath string) []byte { + t.Helper() + + expected, err := os.ReadFile(filepath) require.NoError(t, err) return expected }