From 9ec6aa06665e6255bb90f6cdb45dbe1a3545025b Mon Sep 17 00:00:00 2001 From: David Jumani Date: Mon, 26 Jun 2023 12:47:23 -0400 Subject: [PATCH] Add unit tests for xds package (#8419) * Add unit tests for xds package * use optional desscription instead of comments to describe tests * Remove redundant formatting --------- Co-authored-by: David Jumani Co-authored-by: soloio-bulldozer[bot] <48420018+soloio-bulldozer[bot]@users.noreply.github.com> --- .../v1.15.0-beta15/add-xds-unit-tests.yaml | 7 ++++ projects/gloo/pkg/xds/cache_test.go | 41 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 changelog/v1.15.0-beta15/add-xds-unit-tests.yaml create mode 100644 projects/gloo/pkg/xds/cache_test.go diff --git a/changelog/v1.15.0-beta15/add-xds-unit-tests.yaml b/changelog/v1.15.0-beta15/add-xds-unit-tests.yaml new file mode 100644 index 00000000000..0ab5e1f02f4 --- /dev/null +++ b/changelog/v1.15.0-beta15/add-xds-unit-tests.yaml @@ -0,0 +1,7 @@ +changelog: +- type: NON_USER_FACING + issueLink: https://github.com/solo-io/solo-projects/issues/5113 + resolvesIssue: true + description: >- + Adds new unit tests to improve coverage of the xds package. + diff --git a/projects/gloo/pkg/xds/cache_test.go b/projects/gloo/pkg/xds/cache_test.go new file mode 100644 index 00000000000..3578eb8fbeb --- /dev/null +++ b/projects/gloo/pkg/xds/cache_test.go @@ -0,0 +1,41 @@ +package xds_test + +import ( + "fmt" + + envoy_config_core_v3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + v1 "github.com/solo-io/gloo/projects/gloo/pkg/api/v1" + "github.com/solo-io/gloo/projects/gloo/pkg/xds" + "google.golang.org/protobuf/types/known/structpb" +) + +var _ = Describe("Cache", func() { + + It("NodeRoleHasher generates the correct ID", func() { + nodeRoleHasher := xds.NewNodeRoleHasher() + node := &envoy_config_core_v3.Node{} + Expect(nodeRoleHasher.ID(node)).To(Equal(xds.FallbackNodeCacheKey), + "Should return %s if the role field in the node metadata is not present", xds.FallbackNodeCacheKey) + + role := "role" + node.Metadata = &structpb.Struct{ + Fields: map[string]*structpb.Value{ + role: structpb.NewStringValue(role), + }, + } + Expect(nodeRoleHasher.ID(node)).To(Equal(role), "Should return the role field in the node metadata") + }) + + It("SnapshotCacheKeys returns the keys formatted correctly", func() { + namespace1, namespace2, name1, name2 := "namespace1", "namespace2", "name1", "name2" + proxies := []*v1.Proxy{ + v1.NewProxy(namespace1, name1), + v1.NewProxy(namespace2, name2), + } + expectedKeys := []string{fmt.Sprintf("%v~%v", namespace1, name1), fmt.Sprintf("%v~%v", namespace2, name2)} + actualKeys := xds.SnapshotCacheKeys(proxies) + Expect(actualKeys).To(BeEquivalentTo(expectedKeys)) + }) +})