From 9eba234ddded7cc67c3e1617b32c3ee1b7fe624b Mon Sep 17 00:00:00 2001 From: Arko Dasgupta Date: Mon, 22 Aug 2022 14:01:13 -0700 Subject: [PATCH 1/3] tests for message * specify len and capacity seperately in `make` api * gen DeepCopy for infra ir structs Signed-off-by: Arko Dasgupta --- internal/ir/infra.go | 1 + internal/ir/zz_generated.deepcopy.go | 15 ++++ internal/message/types.go | 6 +- internal/message/types_test.go | 118 +++++++++++++++++++++++++++ 4 files changed, 137 insertions(+), 3 deletions(-) create mode 100644 internal/message/types_test.go diff --git a/internal/ir/infra.go b/internal/ir/infra.go index 7d1c1396bb..81fbbcbdb9 100644 --- a/internal/ir/infra.go +++ b/internal/ir/infra.go @@ -48,6 +48,7 @@ type ProxyListener struct { } // ListenerPort defines a network port of a listener. +// +k8s:deepcopy-gen=true type ListenerPort struct { // Name is the name of the listener port. Name string diff --git a/internal/ir/zz_generated.deepcopy.go b/internal/ir/zz_generated.deepcopy.go index f37cd803ca..636ae75813 100644 --- a/internal/ir/zz_generated.deepcopy.go +++ b/internal/ir/zz_generated.deepcopy.go @@ -118,6 +118,21 @@ func (in *Infra) DeepCopy() *Infra { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ListenerPort) DeepCopyInto(out *ListenerPort) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ListenerPort. +func (in *ListenerPort) DeepCopy() *ListenerPort { + if in == nil { + return nil + } + out := new(ListenerPort) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *ProxyInfra) DeepCopyInto(out *ProxyInfra) { *out = *in diff --git a/internal/message/types.go b/internal/message/types.go index 1bd455cd1d..eb16eddb92 100644 --- a/internal/message/types.go +++ b/internal/message/types.go @@ -22,7 +22,7 @@ type ProviderResources struct { } func (p *ProviderResources) GetGatewayClasses() []*gwapiv1b1.GatewayClass { - res := make([]*gwapiv1b1.GatewayClass, p.GatewayClasses.Len()) + res := make([]*gwapiv1b1.GatewayClass, 0, p.GatewayClasses.Len()) for _, v := range p.GatewayClasses.LoadAll() { res = append(res, v) } @@ -30,7 +30,7 @@ func (p *ProviderResources) GetGatewayClasses() []*gwapiv1b1.GatewayClass { } func (p *ProviderResources) GetGateways() []*gwapiv1b1.Gateway { - res := make([]*gwapiv1b1.Gateway, p.Gateways.Len()) + res := make([]*gwapiv1b1.Gateway, 0, p.Gateways.Len()) for _, v := range p.Gateways.LoadAll() { res = append(res, v) } @@ -38,7 +38,7 @@ func (p *ProviderResources) GetGateways() []*gwapiv1b1.Gateway { } func (p *ProviderResources) GetHTTPRoutes() []*gwapiv1b1.HTTPRoute { - res := make([]*gwapiv1b1.HTTPRoute, p.HTTPRoutes.Len()) + res := make([]*gwapiv1b1.HTTPRoute, 0, p.HTTPRoutes.Len()) for _, v := range p.HTTPRoutes.LoadAll() { res = append(res, v) } diff --git a/internal/message/types_test.go b/internal/message/types_test.go new file mode 100644 index 0000000000..4de6850fa8 --- /dev/null +++ b/internal/message/types_test.go @@ -0,0 +1,118 @@ +package message + +import ( + "testing" + + "github.com/stretchr/testify/assert" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" + gwapiv1b1 "sigs.k8s.io/gateway-api/apis/v1beta1" + + "github.com/envoyproxy/gateway/internal/ir" +) + +func TestProviderResources(t *testing.T) { + resources := new(ProviderResources) + gc1 := &gwapiv1b1.GatewayClass{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-gc1", + }, + } + gw1 := &gwapiv1b1.Gateway{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test1", + Namespace: "test", + }, + } + r1 := &gwapiv1b1.HTTPRoute{ + ObjectMeta: metav1.ObjectMeta{ + Name: "route1", + Namespace: "test", + }, + } + + // Add resources + resources.GatewayClasses.Store("test-gc1", gc1) + + gw1Key := types.NamespacedName{ + Namespace: gw1.GetNamespace(), + Name: gw1.GetName(), + } + resources.Gateways.Store(gw1Key, gw1) + + r1Key := types.NamespacedName{ + Namespace: r1.GetNamespace(), + Name: r1.GetName(), + } + resources.HTTPRoutes.Store(r1Key, r1) + + // Test + gcs := resources.GetGatewayClasses() + assert.Equal(t, len(gcs), 1) + + gws := resources.GetGateways() + assert.Equal(t, len(gws), 1) + + hrs := resources.GetHTTPRoutes() + assert.Equal(t, len(hrs), 1) + + // Add more resources + gc2 := &gwapiv1b1.GatewayClass{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-gc2", + }, + } + gw2 := &gwapiv1b1.Gateway{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test2", + Namespace: "test", + }, + } + r2 := &gwapiv1b1.HTTPRoute{ + ObjectMeta: metav1.ObjectMeta{ + Name: "route2", + Namespace: "test", + }, + } + + resources.GatewayClasses.Store("test-gc2", gc2) + gw2Key := types.NamespacedName{ + Namespace: gw2.GetNamespace(), + Name: gw2.GetName(), + } + resources.Gateways.Store(gw2Key, gw2) + + r2Key := types.NamespacedName{ + Namespace: r2.GetNamespace(), + Name: r2.GetName(), + } + resources.HTTPRoutes.Store(r2Key, r2) + + // Test contents + + gcs = resources.GetGatewayClasses() + assert.ElementsMatch(t, gcs, []*gwapiv1b1.GatewayClass{gc1, gc2}) + + gws = resources.GetGateways() + assert.ElementsMatch(t, gws, []*gwapiv1b1.Gateway{gw1, gw2}) + + hrs = resources.GetHTTPRoutes() + assert.ElementsMatch(t, hrs, []*gwapiv1b1.HTTPRoute{r1, r2}) +} + +func TestXdsIR(t *testing.T) { + msg := new(XdsIR) + in := &ir.Xds{} + msg.Store("xds-ir", in) + out := msg.Get() + assert.Equal(t, out, in) +} + +func TestInfraIR(t *testing.T) { + msg := new(InfraIR) + in := &ir.Infra{} + msg.Store("infra-ir", in) + out := msg.Get() + assert.Equal(t, out, in) + +} From 038e39b53acf6a0da2e0966ff140296679ef0962 Mon Sep 17 00:00:00 2001 From: Arko Dasgupta Date: Mon, 22 Aug 2022 14:15:22 -0700 Subject: [PATCH 2/3] add some data Signed-off-by: Arko Dasgupta --- internal/message/types_test.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/internal/message/types_test.go b/internal/message/types_test.go index 4de6850fa8..5a8565a69a 100644 --- a/internal/message/types_test.go +++ b/internal/message/types_test.go @@ -102,7 +102,9 @@ func TestProviderResources(t *testing.T) { func TestXdsIR(t *testing.T) { msg := new(XdsIR) - in := &ir.Xds{} + in := &ir.Xds{ + HTTP: []*ir.HTTPListener{{Name: "test"}}, + } msg.Store("xds-ir", in) out := msg.Get() assert.Equal(t, out, in) @@ -110,7 +112,9 @@ func TestXdsIR(t *testing.T) { func TestInfraIR(t *testing.T) { msg := new(InfraIR) - in := &ir.Infra{} + in := &ir.Infra{ + Proxy: &ir.ProxyInfra{Name: "test"}, + } msg.Store("infra-ir", in) out := msg.Get() assert.Equal(t, out, in) From 76a14968eb386109a42a3bfbe1a056a4c7fc780b Mon Sep 17 00:00:00 2001 From: Arko Dasgupta Date: Mon, 22 Aug 2022 14:53:14 -0700 Subject: [PATCH 3/3] return nil when empty Signed-off-by: Arko Dasgupta --- internal/message/types.go | 10 ++++++++++ internal/message/types_test.go | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/internal/message/types.go b/internal/message/types.go index eb16eddb92..27148205fb 100644 --- a/internal/message/types.go +++ b/internal/message/types.go @@ -22,6 +22,10 @@ type ProviderResources struct { } func (p *ProviderResources) GetGatewayClasses() []*gwapiv1b1.GatewayClass { + if p.GatewayClasses.Len() == 0 { + return nil + } + res := make([]*gwapiv1b1.GatewayClass, 0, p.GatewayClasses.Len()) for _, v := range p.GatewayClasses.LoadAll() { res = append(res, v) @@ -30,6 +34,9 @@ func (p *ProviderResources) GetGatewayClasses() []*gwapiv1b1.GatewayClass { } func (p *ProviderResources) GetGateways() []*gwapiv1b1.Gateway { + if p.Gateways.Len() == 0 { + return nil + } res := make([]*gwapiv1b1.Gateway, 0, p.Gateways.Len()) for _, v := range p.Gateways.LoadAll() { res = append(res, v) @@ -38,6 +45,9 @@ func (p *ProviderResources) GetGateways() []*gwapiv1b1.Gateway { } func (p *ProviderResources) GetHTTPRoutes() []*gwapiv1b1.HTTPRoute { + if p.HTTPRoutes.Len() == 0 { + return nil + } res := make([]*gwapiv1b1.HTTPRoute, 0, p.HTTPRoutes.Len()) for _, v := range p.HTTPRoutes.LoadAll() { res = append(res, v) diff --git a/internal/message/types_test.go b/internal/message/types_test.go index 5a8565a69a..386dc4719f 100644 --- a/internal/message/types_test.go +++ b/internal/message/types_test.go @@ -31,6 +31,11 @@ func TestProviderResources(t *testing.T) { }, } + // Check init state + assert.Nil(t, resources.GetGatewayClasses()) + assert.Nil(t, resources.GetGateways()) + assert.Nil(t, resources.GetHTTPRoutes()) + // Add resources resources.GatewayClasses.Store("test-gc1", gc1)