From 8553bfe066e5dada662d2702981d94175e7e42b1 Mon Sep 17 00:00:00 2001 From: David Jumani Date: Mon, 26 Jun 2023 09:06:40 -0400 Subject: [PATCH 1/3] Add unit tests for xds package --- .../v1.15.0-beta15/add-xds-unit-tests.yaml | 7 ++++ projects/gloo/pkg/xds/cache_test.go | 42 +++++++++++++++++++ 2 files changed, 49 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..18fff3b6d61 --- /dev/null +++ b/projects/gloo/pkg/xds/cache_test.go @@ -0,0 +1,42 @@ +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{} + // Ensure it returns the fallback key if the role field in the node metadata is not present + Expect(nodeRoleHasher.ID(node)).To(Equal(xds.FallbackNodeCacheKey)) + + role := "role" + node.Metadata = &structpb.Struct{ + Fields: map[string]*structpb.Value{ + role: structpb.NewStringValue(role), + }, + } + // Ensure it returns the role field in the node metadata + Expect(nodeRoleHasher.ID(node)).To(Equal(role)) + }) + + 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)) + }) +}) From 59c20107df165fe822f4d1a4548a7b2e1d624a7a Mon Sep 17 00:00:00 2001 From: David Jumani Date: Mon, 26 Jun 2023 12:12:16 -0400 Subject: [PATCH 2/3] use optional desscription instead of comments to describe tests --- projects/gloo/pkg/xds/cache_test.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/projects/gloo/pkg/xds/cache_test.go b/projects/gloo/pkg/xds/cache_test.go index 18fff3b6d61..0c7e79cd7ae 100644 --- a/projects/gloo/pkg/xds/cache_test.go +++ b/projects/gloo/pkg/xds/cache_test.go @@ -16,8 +16,8 @@ var _ = Describe("Cache", func() { It("NodeRoleHasher generates the correct ID", func() { nodeRoleHasher := xds.NewNodeRoleHasher() node := &envoy_config_core_v3.Node{} - // Ensure it returns the fallback key if the role field in the node metadata is not present - Expect(nodeRoleHasher.ID(node)).To(Equal(xds.FallbackNodeCacheKey)) + Expect(nodeRoleHasher.ID(node)).To(Equal(xds.FallbackNodeCacheKey), + fmt.Sprintf("Should return %s if the role field in the node metadata is not present", xds.FallbackNodeCacheKey)) role := "role" node.Metadata = &structpb.Struct{ @@ -25,8 +25,7 @@ var _ = Describe("Cache", func() { role: structpb.NewStringValue(role), }, } - // Ensure it returns the role field in the node metadata - Expect(nodeRoleHasher.ID(node)).To(Equal(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() { From 8c248d5ca288bdd67f6aaa46e678fb545f890650 Mon Sep 17 00:00:00 2001 From: David Jumani Date: Mon, 26 Jun 2023 12:20:46 -0400 Subject: [PATCH 3/3] Remove redundant formatting --- projects/gloo/pkg/xds/cache_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/gloo/pkg/xds/cache_test.go b/projects/gloo/pkg/xds/cache_test.go index 0c7e79cd7ae..3578eb8fbeb 100644 --- a/projects/gloo/pkg/xds/cache_test.go +++ b/projects/gloo/pkg/xds/cache_test.go @@ -17,7 +17,7 @@ var _ = Describe("Cache", func() { nodeRoleHasher := xds.NewNodeRoleHasher() node := &envoy_config_core_v3.Node{} Expect(nodeRoleHasher.ID(node)).To(Equal(xds.FallbackNodeCacheKey), - fmt.Sprintf("Should return %s if the role field in the node metadata is not present", xds.FallbackNodeCacheKey)) + "Should return %s if the role field in the node metadata is not present", xds.FallbackNodeCacheKey) role := "role" node.Metadata = &structpb.Struct{