From ec34e3f5e82b5338a00a7b3b85c47778e179cb1b Mon Sep 17 00:00:00 2001 From: jm96441n Date: Fri, 7 Apr 2023 15:19:13 -0400 Subject: [PATCH] Added unit tests --- agent/structs/config_entry_routes_test.go | 151 +++++++++++++++++++++- 1 file changed, 149 insertions(+), 2 deletions(-) diff --git a/agent/structs/config_entry_routes_test.go b/agent/structs/config_entry_routes_test.go index d33b5d06eb53..476ce46eed05 100644 --- a/agent/structs/config_entry_routes_test.go +++ b/agent/structs/config_entry_routes_test.go @@ -6,8 +6,9 @@ package structs import ( "testing" - "github.com/hashicorp/consul/acl" "github.com/stretchr/testify/require" + + "github.com/hashicorp/consul/acl" ) func TestTCPRoute(t *testing.T) { @@ -60,6 +61,78 @@ func TestTCPRoute(t *testing.T) { }, validateErr: "unsupported parent kind", }, + "duplicate parents with no listener specified": { + entry: &TCPRouteConfigEntry{ + Kind: TCPRoute, + Name: "route-two", + Parents: []ResourceReference{ + { + Kind: "api-gateway", + Name: "gateway", + }, + { + Kind: "api-gateway", + Name: "gateway", + }, + }, + }, + validateErr: "route parents must be unique", + }, + "duplicate parents with listener specified": { + entry: &TCPRouteConfigEntry{ + Kind: TCPRoute, + Name: "route-two", + Parents: []ResourceReference{ + { + Kind: "api-gateway", + Name: "gateway", + SectionName: "same", + }, + { + Kind: "api-gateway", + Name: "gateway", + SectionName: "same", + }, + }, + }, + validateErr: "route parents must be unique", + }, + "almost duplicate parents with one not specifying a listener": { + entry: &TCPRouteConfigEntry{ + Kind: TCPRoute, + Name: "route-two", + Parents: []ResourceReference{ + { + Kind: "api-gateway", + Name: "gateway", + }, + { + Kind: "api-gateway", + Name: "gateway", + SectionName: "same", + }, + }, + }, + check: func(t *testing.T, entry ConfigEntry) { + expectedParents := []ResourceReference{ + { + Kind: APIGateway, + Name: "gateway", + EnterpriseMeta: *acl.DefaultEnterpriseMeta(), + }, + { + Kind: APIGateway, + Name: "gateway", + SectionName: "same", + EnterpriseMeta: *acl.DefaultEnterpriseMeta(), + }, + } + route := entry.(*TCPRouteConfigEntry) + require.Len(t, route.Parents, 2) + require.Equal(t, expectedParents[0], route.Parents[0]) + require.Equal(t, expectedParents[1], route.Parents[1]) + }, + }, } testConfigEntryNormalizeAndValidate(t, cases) } @@ -278,7 +351,8 @@ func TestHTTPRoute(t *testing.T) { { Name: "test2", Weight: -1, - }}, + }, + }, }}, }, check: func(t *testing.T, entry ConfigEntry) { @@ -287,6 +361,79 @@ func TestHTTPRoute(t *testing.T) { require.Equal(t, 1, route.Rules[0].Services[1].Weight) }, }, + + "duplicate parents with no listener specified": { + entry: &HTTPRouteConfigEntry{ + Kind: HTTPRoute, + Name: "route-two", + Parents: []ResourceReference{ + { + Kind: "api-gateway", + Name: "gateway", + }, + { + Kind: "api-gateway", + Name: "gateway", + }, + }, + }, + validateErr: "route parents must be unique", + }, + "duplicate parents with listener specified": { + entry: &HTTPRouteConfigEntry{ + Kind: HTTPRoute, + Name: "route-two", + Parents: []ResourceReference{ + { + Kind: "api-gateway", + Name: "gateway", + SectionName: "same", + }, + { + Kind: "api-gateway", + Name: "gateway", + SectionName: "same", + }, + }, + }, + validateErr: "route parents must be unique", + }, + "almost duplicate parents with one not specifying a listener": { + entry: &HTTPRouteConfigEntry{ + Kind: HTTPRoute, + Name: "route-two", + Parents: []ResourceReference{ + { + Kind: "api-gateway", + Name: "gateway", + }, + { + Kind: "api-gateway", + Name: "gateway", + SectionName: "same", + }, + }, + }, + check: func(t *testing.T, entry ConfigEntry) { + expectedParents := []ResourceReference{ + { + Kind: APIGateway, + Name: "gateway", + EnterpriseMeta: *acl.DefaultEnterpriseMeta(), + }, + { + Kind: APIGateway, + Name: "gateway", + SectionName: "same", + EnterpriseMeta: *acl.DefaultEnterpriseMeta(), + }, + } + route := entry.(*HTTPRouteConfigEntry) + require.Len(t, route.Parents, 2) + require.Equal(t, expectedParents[0], route.Parents[0]) + require.Equal(t, expectedParents[1], route.Parents[1]) + }, + }, } testConfigEntryNormalizeAndValidate(t, cases) }