From a20102560e44ccb30d84e93281814ea11dca18f4 Mon Sep 17 00:00:00 2001 From: "R.B. Boyer" <4903+rboyer@users.noreply.github.com> Date: Tue, 16 May 2023 11:49:27 -0500 Subject: [PATCH] test: fix oss/ent drift in gateway container tests (#17365) --- .../test/gateways/gateway_endpoint_test.go | 20 +++---- .../test/gateways/http_route_test.go | 55 +++++-------------- .../test/gateways/namespace_oss.go | 11 ---- .../test/gateways/tenancy_oss.go | 14 +++++ 4 files changed, 36 insertions(+), 64 deletions(-) delete mode 100644 test/integration/consul-container/test/gateways/namespace_oss.go create mode 100644 test/integration/consul-container/test/gateways/tenancy_oss.go diff --git a/test/integration/consul-container/test/gateways/gateway_endpoint_test.go b/test/integration/consul-container/test/gateways/gateway_endpoint_test.go index 309538e89..1f7a00e68 100644 --- a/test/integration/consul-container/test/gateways/gateway_endpoint_test.go +++ b/test/integration/consul-container/test/gateways/gateway_endpoint_test.go @@ -15,11 +15,12 @@ import ( "github.com/stretchr/testify/require" "github.com/hashicorp/consul/api" + "github.com/hashicorp/go-cleanhttp" + libassert "github.com/hashicorp/consul/test/integration/consul-container/libs/assert" libcluster "github.com/hashicorp/consul/test/integration/consul-container/libs/cluster" libservice "github.com/hashicorp/consul/test/integration/consul-container/libs/service" libtopology "github.com/hashicorp/consul/test/integration/consul-container/libs/topology" - "github.com/hashicorp/go-cleanhttp" ) // Creates a gateway service and tests to see if it is routable @@ -56,12 +57,7 @@ func TestAPIGatewayCreate(t *testing.T) { cluster, _, _ := libtopology.NewCluster(t, clusterConfig) client := cluster.APIClient(0) - namespace := getNamespace() - if namespace != "" { - ns := &api.Namespace{Name: namespace} - _, _, err := client.Namespaces().Create(ns, nil) - require.NoError(t, err) - } + namespace := getOrCreateNamespace(t, client) // add api gateway config apiGateway := &api.APIGatewayConfigEntry{ @@ -119,7 +115,7 @@ func TestAPIGatewayCreate(t *testing.T) { // make sure the gateway/route come online // make sure config entries have been properly created - checkGatewayConfigEntry(t, client, gatewayName, namespace) + checkGatewayConfigEntry(t, client, gatewayName, &api.QueryOptions{Namespace: namespace}) checkTCPRouteConfigEntry(t, client, routeName, namespace) port, err := gatewayService.GetPort(listenerPortOne) @@ -144,11 +140,11 @@ func conditionStatusIsValue(typeName string, statusValue string, conditions []ap return false } -func checkGatewayConfigEntry(t *testing.T, client *api.Client, gatewayName string, namespace string) { +func checkGatewayConfigEntry(t *testing.T, client *api.Client, gatewayName string, opts *api.QueryOptions) { t.Helper() require.Eventually(t, func() bool { - entry, _, err := client.ConfigEntries().Get(api.APIGateway, gatewayName, &api.QueryOptions{Namespace: namespace}) + entry, _, err := client.ConfigEntries().Get(api.APIGateway, gatewayName, opts) if err != nil { t.Log("error constructing request", err) return false @@ -163,11 +159,11 @@ func checkGatewayConfigEntry(t *testing.T, client *api.Client, gatewayName strin }, time.Second*10, time.Second*1) } -func checkHTTPRouteConfigEntry(t *testing.T, client *api.Client, routeName string, namespace string) { +func checkHTTPRouteConfigEntry(t *testing.T, client *api.Client, routeName string, opts *api.QueryOptions) { t.Helper() require.Eventually(t, func() bool { - entry, _, err := client.ConfigEntries().Get(api.HTTPRoute, routeName, &api.QueryOptions{Namespace: namespace}) + entry, _, err := client.ConfigEntries().Get(api.HTTPRoute, routeName, opts) if err != nil { t.Log("error constructing request", err) return false diff --git a/test/integration/consul-container/test/gateways/http_route_test.go b/test/integration/consul-container/test/gateways/http_route_test.go index 97f8c8dc6..c8236294b 100644 --- a/test/integration/consul-container/test/gateways/http_route_test.go +++ b/test/integration/consul-container/test/gateways/http_route_test.go @@ -15,6 +15,7 @@ import ( "github.com/stretchr/testify/require" "github.com/hashicorp/consul/api" + libassert "github.com/hashicorp/consul/test/integration/consul-container/libs/assert" libcluster "github.com/hashicorp/consul/test/integration/consul-container/libs/cluster" libservice "github.com/hashicorp/consul/test/integration/consul-container/libs/service" @@ -82,26 +83,9 @@ func TestHTTPRouteFlattening(t *testing.T) { cluster, _, _ := libtopology.NewCluster(t, clusterConfig) client := cluster.Agents[0].GetClient() - gwNamespace := getNamespace() - if gwNamespace != "" { - ns := &api.Namespace{Name: gwNamespace} - _, _, err := client.Namespaces().Create(ns, nil) - require.NoError(t, err) - } - - serviceOneNamespace := getNamespace() - if serviceOneNamespace != "" { - ns := &api.Namespace{Name: serviceOneNamespace} - _, _, err := client.Namespaces().Create(ns, nil) - require.NoError(t, err) - } - - serviceTwoNamespace := getNamespace() - if serviceTwoNamespace != "" { - ns := &api.Namespace{Name: serviceTwoNamespace} - _, _, err := client.Namespaces().Create(ns, nil) - require.NoError(t, err) - } + gwNamespace := getOrCreateNamespace(t, client) + serviceOneNamespace := getOrCreateNamespace(t, client) + serviceTwoNamespace := getOrCreateNamespace(t, client) _, _, err := libservice.CreateAndRegisterStaticServerAndSidecar(cluster.Agents[0], &libservice.ServiceOpts{ Name: serviceOneName, @@ -244,9 +228,9 @@ func TestHTTPRouteFlattening(t *testing.T) { libassert.CatalogServiceExists(t, client, gatewayName, &api.QueryOptions{Namespace: gwNamespace}) // make sure config entries have been properly created - checkGatewayConfigEntry(t, client, gatewayName, gwNamespace) - checkHTTPRouteConfigEntry(t, client, routeOneName, gwNamespace) - checkHTTPRouteConfigEntry(t, client, routeTwoName, gwNamespace) + checkGatewayConfigEntry(t, client, gatewayName, &api.QueryOptions{Namespace: gwNamespace}) + checkHTTPRouteConfigEntry(t, client, routeOneName, &api.QueryOptions{Namespace: gwNamespace}) + checkHTTPRouteConfigEntry(t, client, routeTwoName, &api.QueryOptions{Namespace: gwNamespace}) // gateway resolves routes gatewayPort, err := gatewayService.GetPort(listenerPort) @@ -332,12 +316,7 @@ func TestHTTPRoutePathRewrite(t *testing.T) { fooPath := "/v1/foo" barPath := "/v1/bar" - namespace := getNamespace() - if namespace != "" { - ns := &api.Namespace{Name: namespace} - _, _, err := client.Namespaces().Create(ns, nil) - require.NoError(t, err) - } + namespace := getOrCreateNamespace(t, client) _, _, err := libservice.CreateAndRegisterStaticServerAndSidecar(cluster.Agents[0], &libservice.ServiceOpts{ ID: fooName, @@ -486,9 +465,9 @@ func TestHTTPRoutePathRewrite(t *testing.T) { libassert.CatalogServiceExists(t, client, gatewayName, &api.QueryOptions{Namespace: namespace}) // make sure config entries have been properly created - checkGatewayConfigEntry(t, client, gatewayName, namespace) - checkHTTPRouteConfigEntry(t, client, invalidRouteName, namespace) - checkHTTPRouteConfigEntry(t, client, validRouteName, namespace) + checkGatewayConfigEntry(t, client, gatewayName, &api.QueryOptions{Namespace: namespace}) + checkHTTPRouteConfigEntry(t, client, invalidRouteName, &api.QueryOptions{Namespace: namespace}) + checkHTTPRouteConfigEntry(t, client, validRouteName, &api.QueryOptions{Namespace: namespace}) gatewayPort, err := gatewayService.GetPort(listenerPort) require.NoError(t, err) @@ -560,13 +539,7 @@ func TestHTTPRouteParentRefChange(t *testing.T) { cluster, _, _ := libtopology.NewCluster(t, clusterConfig) client := cluster.APIClient(0) - // getNamespace() should always return an empty string in Consul OSS - namespace := getNamespace() - if namespace != "" { - ns := &api.Namespace{Name: namespace} - _, _, err := client.Namespaces().Create(ns, nil) - require.NoError(t, err) - } + namespace := getOrCreateNamespace(t, client) _, _, err := libservice.CreateAndRegisterStaticServerAndSidecar(cluster.Agents[0], &libservice.ServiceOpts{ ID: serviceName, @@ -603,7 +576,7 @@ func TestHTTPRouteParentRefChange(t *testing.T) { Namespace: namespace, } require.NoError(t, cluster.ConfigEntryWrite(gatewayOne)) - checkGatewayConfigEntry(t, client, gatewayOneName, namespace) + checkGatewayConfigEntry(t, client, gatewayOneName, &api.QueryOptions{Namespace: namespace}) // create gateway service gwOneCfg := libservice.GatewayConfig{ @@ -630,7 +603,7 @@ func TestHTTPRouteParentRefChange(t *testing.T) { Namespace: namespace, } require.NoError(t, cluster.ConfigEntryWrite(gatewayTwo)) - checkGatewayConfigEntry(t, client, gatewayTwoName, namespace) + checkGatewayConfigEntry(t, client, gatewayTwoName, &api.QueryOptions{Namespace: namespace}) // create gateway service gwTwoCfg := libservice.GatewayConfig{ diff --git a/test/integration/consul-container/test/gateways/namespace_oss.go b/test/integration/consul-container/test/gateways/namespace_oss.go deleted file mode 100644 index b9c21d4a9..000000000 --- a/test/integration/consul-container/test/gateways/namespace_oss.go +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -//go:build !consulent -// +build !consulent - -package gateways - -func getNamespace() string { - return "" -} diff --git a/test/integration/consul-container/test/gateways/tenancy_oss.go b/test/integration/consul-container/test/gateways/tenancy_oss.go new file mode 100644 index 000000000..19d82d39d --- /dev/null +++ b/test/integration/consul-container/test/gateways/tenancy_oss.go @@ -0,0 +1,14 @@ +//go:build !consulent +// +build !consulent + +package gateways + +import ( + "testing" + + "github.com/hashicorp/consul/api" +) + +func getOrCreateNamespace(_ *testing.T, _ *api.Client) string { + return "" +}