test: fix oss/ent drift in gateway container tests (#17365)

This commit is contained in:
R.B. Boyer 2023-05-16 11:49:27 -05:00 committed by GitHub
parent 5d07624e80
commit a20102560e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 64 deletions

View File

@ -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

View File

@ -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{

View File

@ -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 ""
}

View File

@ -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 ""
}