test: add test helper to detect if the current build is enterprise (#19201)

This can help conditionally change test behavior if the test is executing in enterprise without the need to split the test with build tags.
This commit is contained in:
R.B. Boyer 2023-11-13 10:30:07 -06:00 committed by GitHub
parent af2086fbeb
commit b21851c903
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 60 additions and 38 deletions

View File

@ -6,7 +6,6 @@ package nodehealth
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/hashicorp/consul/agent/structs"
"testing" "testing"
"github.com/oklog/ulid/v2" "github.com/oklog/ulid/v2"
@ -25,6 +24,7 @@ import (
"github.com/hashicorp/consul/proto/private/prototest" "github.com/hashicorp/consul/proto/private/prototest"
"github.com/hashicorp/consul/sdk/testutil" "github.com/hashicorp/consul/sdk/testutil"
"github.com/hashicorp/consul/sdk/testutil/retry" "github.com/hashicorp/consul/sdk/testutil/retry"
"github.com/hashicorp/consul/version/versiontest"
) )
var ( var (
@ -94,7 +94,7 @@ func (suite *nodeHealthControllerTestSuite) SetupTest() {
client := svctest.RunResourceServiceWithConfig(suite.T(), cfg, types.Register, types.RegisterDNSPolicy) client := svctest.RunResourceServiceWithConfig(suite.T(), cfg, types.Register, types.RegisterDNSPolicy)
suite.resourceClient = resourcetest.NewClient(client) suite.resourceClient = resourcetest.NewClient(client)
suite.runtime = controller.Runtime{Client: suite.resourceClient, Logger: testutil.Logger(suite.T())} suite.runtime = controller.Runtime{Client: suite.resourceClient, Logger: testutil.Logger(suite.T())}
suite.isEnterprise = structs.NodeEnterpriseMetaInDefaultPartition().PartitionOrEmpty() == "default" suite.isEnterprise = versiontest.IsEnterprise()
} }
func (suite *nodeHealthControllerTestSuite) TestGetNodeHealthListError() { func (suite *nodeHealthControllerTestSuite) TestGetNodeHealthListError() {
@ -320,6 +320,7 @@ func (suite *nodeHealthControllerTestSuite) waitForReconciliation(id *pbresource
require.Equal(r, reason, nodeHealthStatus.Conditions[0].Reason) require.Equal(r, reason, nodeHealthStatus.Conditions[0].Reason)
}) })
} }
func (suite *nodeHealthControllerTestSuite) TestController() { func (suite *nodeHealthControllerTestSuite) TestController() {
suite.runTestCaseWithTenancies(func(tenancy *pbresource.Tenancy) { suite.runTestCaseWithTenancies(func(tenancy *pbresource.Tenancy) {

View File

@ -9,27 +9,26 @@ import (
"testing" "testing"
"time" "time"
"github.com/hashicorp/consul/internal/resource" "github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
"google.golang.org/protobuf/testing/protocmp" "google.golang.org/protobuf/testing/protocmp"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
svc "github.com/hashicorp/consul/agent/grpc-external/services/resource" svc "github.com/hashicorp/consul/agent/grpc-external/services/resource"
svctest "github.com/hashicorp/consul/agent/grpc-external/services/resource/testing" svctest "github.com/hashicorp/consul/agent/grpc-external/services/resource/testing"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/internal/catalog/internal/controllers/nodehealth" "github.com/hashicorp/consul/internal/catalog/internal/controllers/nodehealth"
"github.com/hashicorp/consul/internal/catalog/internal/mappers/nodemapper" "github.com/hashicorp/consul/internal/catalog/internal/mappers/nodemapper"
"github.com/hashicorp/consul/internal/catalog/internal/types" "github.com/hashicorp/consul/internal/catalog/internal/types"
"github.com/hashicorp/consul/internal/controller" "github.com/hashicorp/consul/internal/controller"
"github.com/hashicorp/consul/internal/resource"
"github.com/hashicorp/consul/internal/resource/resourcetest" "github.com/hashicorp/consul/internal/resource/resourcetest"
pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v2beta1" pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v2beta1"
"github.com/hashicorp/consul/proto-public/pbresource" "github.com/hashicorp/consul/proto-public/pbresource"
"github.com/hashicorp/consul/proto/private/prototest" "github.com/hashicorp/consul/proto/private/prototest"
"github.com/hashicorp/consul/sdk/testutil" "github.com/hashicorp/consul/sdk/testutil"
"github.com/hashicorp/consul/sdk/testutil/retry" "github.com/hashicorp/consul/sdk/testutil/retry"
"github.com/hashicorp/consul/version/versiontest"
) )
var ( var (
@ -104,7 +103,7 @@ func (suite *controllerSuite) SetupTest() {
suite.client = svctest.RunResourceServiceWithConfig(suite.T(), svc.Config{TenancyBridge: mockTenancyBridge}, types.Register) suite.client = svctest.RunResourceServiceWithConfig(suite.T(), svc.Config{TenancyBridge: mockTenancyBridge}, types.Register)
suite.runtime = controller.Runtime{Client: suite.client, Logger: testutil.Logger(suite.T())} suite.runtime = controller.Runtime{Client: suite.client, Logger: testutil.Logger(suite.T())}
suite.isEnterprise = (structs.NodeEnterpriseMetaInDefaultPartition().PartitionOrEmpty() == "default") suite.isEnterprise = versiontest.IsEnterprise()
} }
// injectNodeWithStatus is a helper method to write a Node resource and synthesize its status // injectNodeWithStatus is a helper method to write a Node resource and synthesize its status

View File

@ -11,13 +11,13 @@ import (
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
"github.com/hashicorp/consul/acl" "github.com/hashicorp/consul/acl"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/internal/resource" "github.com/hashicorp/consul/internal/resource"
"github.com/hashicorp/consul/internal/resource/resourcetest" "github.com/hashicorp/consul/internal/resource/resourcetest"
pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v2beta1" pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v2beta1"
"github.com/hashicorp/consul/proto-public/pbresource" "github.com/hashicorp/consul/proto-public/pbresource"
"github.com/hashicorp/consul/proto/private/prototest" "github.com/hashicorp/consul/proto/private/prototest"
"github.com/hashicorp/consul/sdk/testutil" "github.com/hashicorp/consul/sdk/testutil"
"github.com/hashicorp/consul/version/versiontest"
) )
func TestMutateFailoverPolicy(t *testing.T) { func TestMutateFailoverPolicy(t *testing.T) {
@ -749,7 +749,7 @@ func TestFailoverPolicyACLs(t *testing.T) {
}) })
} }
isEnterprise := (structs.NodeEnterpriseMetaInDefaultPartition().PartitionOrEmpty() == "default") isEnterprise := versiontest.IsEnterprise()
serviceRead := func(partition, namespace, name string) string { serviceRead := func(partition, namespace, name string) string {
if isEnterprise { if isEnterprise {

View File

@ -11,13 +11,13 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/durationpb" "google.golang.org/protobuf/types/known/durationpb"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/internal/resource" "github.com/hashicorp/consul/internal/resource"
"github.com/hashicorp/consul/internal/resource/resourcetest" "github.com/hashicorp/consul/internal/resource/resourcetest"
pbmesh "github.com/hashicorp/consul/proto-public/pbmesh/v2beta1" pbmesh "github.com/hashicorp/consul/proto-public/pbmesh/v2beta1"
"github.com/hashicorp/consul/proto-public/pbresource" "github.com/hashicorp/consul/proto-public/pbresource"
"github.com/hashicorp/consul/proto/private/prototest" "github.com/hashicorp/consul/proto/private/prototest"
"github.com/hashicorp/consul/sdk/testutil" "github.com/hashicorp/consul/sdk/testutil"
"github.com/hashicorp/consul/version/versiontest"
) )
func TestValidateDestinationPolicy(t *testing.T) { func TestValidateDestinationPolicy(t *testing.T) {
@ -545,7 +545,7 @@ func TestDestinationPolicyACLs(t *testing.T) {
}) })
} }
isEnterprise := (structs.NodeEnterpriseMetaInDefaultPartition().PartitionOrEmpty() == "default") isEnterprise := versiontest.IsEnterprise()
serviceRead := func(partition, namespace, name string) string { serviceRead := func(partition, namespace, name string) string {
if isEnterprise { if isEnterprise {

View File

@ -12,12 +12,12 @@ import (
"google.golang.org/protobuf/types/known/durationpb" "google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/wrapperspb" "google.golang.org/protobuf/types/known/wrapperspb"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/internal/resource" "github.com/hashicorp/consul/internal/resource"
"github.com/hashicorp/consul/internal/resource/resourcetest" "github.com/hashicorp/consul/internal/resource/resourcetest"
pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v2beta1" pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v2beta1"
pbmesh "github.com/hashicorp/consul/proto-public/pbmesh/v2beta1" pbmesh "github.com/hashicorp/consul/proto-public/pbmesh/v2beta1"
"github.com/hashicorp/consul/proto-public/pbresource" "github.com/hashicorp/consul/proto-public/pbresource"
"github.com/hashicorp/consul/version/versiontest"
) )
type xRouteParentRefMutateTestcase struct { type xRouteParentRefMutateTestcase struct {
@ -441,7 +441,7 @@ func testXRouteACLs[R XRouteData](t *testing.T, newRoute func(t *testing.T, pare
}) })
} }
isEnterprise := (structs.NodeEnterpriseMetaInDefaultPartition().PartitionOrEmpty() == "default") isEnterprise := versiontest.IsEnterprise()
serviceRead := func(partition, namespace, name string) string { serviceRead := func(partition, namespace, name string) string {
if isEnterprise { if isEnterprise {

View File

@ -8,9 +8,12 @@ import (
"fmt" "fmt"
"testing" "testing"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"google.golang.org/protobuf/proto"
svc "github.com/hashicorp/consul/agent/grpc-external/services/resource" svc "github.com/hashicorp/consul/agent/grpc-external/services/resource"
svctest "github.com/hashicorp/consul/agent/grpc-external/services/resource/testing" svctest "github.com/hashicorp/consul/agent/grpc-external/services/resource/testing"
"github.com/hashicorp/consul/agent/structs"
cat "github.com/hashicorp/consul/internal/catalog" cat "github.com/hashicorp/consul/internal/catalog"
"github.com/hashicorp/consul/internal/controller" "github.com/hashicorp/consul/internal/controller"
"github.com/hashicorp/consul/internal/multicluster/internal/types" "github.com/hashicorp/consul/internal/multicluster/internal/types"
@ -20,11 +23,8 @@ import (
pbmulticluster "github.com/hashicorp/consul/proto-public/pbmulticluster/v2beta1" pbmulticluster "github.com/hashicorp/consul/proto-public/pbmulticluster/v2beta1"
"github.com/hashicorp/consul/proto-public/pbresource" "github.com/hashicorp/consul/proto-public/pbresource"
"github.com/hashicorp/consul/proto/private/prototest" "github.com/hashicorp/consul/proto/private/prototest"
"google.golang.org/protobuf/proto"
"github.com/hashicorp/consul/sdk/testutil" "github.com/hashicorp/consul/sdk/testutil"
"github.com/stretchr/testify/require" "github.com/hashicorp/consul/version/versiontest"
"github.com/stretchr/testify/suite"
) )
type controllerSuite struct { type controllerSuite struct {
@ -60,7 +60,7 @@ func (suite *controllerSuite) SetupTest() {
Logger: testutil.Logger(suite.T()), Logger: testutil.Logger(suite.T()),
} }
suite.reconciler = &reconciler{} suite.reconciler = &reconciler{}
suite.isEnterprise = (structs.NodeEnterpriseMetaInDefaultPartition().PartitionOrEmpty() == "default") suite.isEnterprise = versiontest.IsEnterprise()
} }
func (suite *controllerSuite) TestReconcile() { func (suite *controllerSuite) TestReconcile() {

View File

@ -5,13 +5,15 @@ package types
import ( import (
"errors" "errors"
"github.com/hashicorp/consul/agent/structs" "testing"
"github.com/stretchr/testify/require"
"github.com/hashicorp/consul/internal/resource" "github.com/hashicorp/consul/internal/resource"
"github.com/hashicorp/consul/internal/resource/resourcetest" "github.com/hashicorp/consul/internal/resource/resourcetest"
pbmulticluster "github.com/hashicorp/consul/proto-public/pbmulticluster/v2beta1" pbmulticluster "github.com/hashicorp/consul/proto-public/pbmulticluster/v2beta1"
"github.com/hashicorp/consul/proto-public/pbresource" "github.com/hashicorp/consul/proto-public/pbresource"
"github.com/stretchr/testify/require" "github.com/hashicorp/consul/version/versiontest"
"testing"
) )
func computedExportedServicesWithPartition(partitionName string) *pbmulticluster.ComputedExportedServices { func computedExportedServicesWithPartition(partitionName string) *pbmulticluster.ComputedExportedServices {
@ -123,7 +125,7 @@ func TestComputedExportedServicesValidations(t *testing.T) {
expectErrorENT []string expectErrorENT []string
} }
isEnterprise := structs.NodeEnterpriseMetaInDefaultPartition().PartitionOrEmpty() == "default" isEnterprise := versiontest.IsEnterprise()
run := func(t *testing.T, tc testcase) { run := func(t *testing.T, tc testcase) {
expectError := tc.expectErrorCE expectError := tc.expectErrorCE

View File

@ -5,13 +5,15 @@ package types
import ( import (
"errors" "errors"
"github.com/hashicorp/consul/agent/structs" "testing"
"github.com/stretchr/testify/require"
"github.com/hashicorp/consul/internal/resource" "github.com/hashicorp/consul/internal/resource"
"github.com/hashicorp/consul/internal/resource/resourcetest" "github.com/hashicorp/consul/internal/resource/resourcetest"
pbmulticluster "github.com/hashicorp/consul/proto-public/pbmulticluster/v2beta1" pbmulticluster "github.com/hashicorp/consul/proto-public/pbmulticluster/v2beta1"
"github.com/hashicorp/consul/proto-public/pbresource" "github.com/hashicorp/consul/proto-public/pbresource"
"github.com/stretchr/testify/require" "github.com/hashicorp/consul/version/versiontest"
"testing"
) )
func inValidExportedServices() *pbmulticluster.ExportedServices { func inValidExportedServices() *pbmulticluster.ExportedServices {
@ -149,7 +151,7 @@ func TestExportedServicesValidation(t *testing.T) {
expectErrorENT []string expectErrorENT []string
} }
isEnterprise := structs.NodeEnterpriseMetaInDefaultPartition().PartitionOrEmpty() == "default" isEnterprise := versiontest.IsEnterprise()
run := func(t *testing.T, tc testcase) { run := func(t *testing.T, tc testcase) {
expectError := tc.expectErrorCE expectError := tc.expectErrorCE

View File

@ -4,13 +4,15 @@
package types package types
import ( import (
"github.com/hashicorp/consul/agent/structs" "testing"
"github.com/stretchr/testify/require"
"github.com/hashicorp/consul/internal/resource" "github.com/hashicorp/consul/internal/resource"
"github.com/hashicorp/consul/internal/resource/resourcetest" "github.com/hashicorp/consul/internal/resource/resourcetest"
pbmulticluster "github.com/hashicorp/consul/proto-public/pbmulticluster/v2beta1" pbmulticluster "github.com/hashicorp/consul/proto-public/pbmulticluster/v2beta1"
"github.com/hashicorp/consul/proto-public/pbresource" "github.com/hashicorp/consul/proto-public/pbresource"
"github.com/stretchr/testify/require" "github.com/hashicorp/consul/version/versiontest"
"testing"
) )
func validNamespaceExportedServicesWithPeer(peerName string) *pbmulticluster.NamespaceExportedServices { func validNamespaceExportedServicesWithPeer(peerName string) *pbmulticluster.NamespaceExportedServices {
@ -51,6 +53,7 @@ func validNamespaceExportedServicesWithSamenessGroup(samenessGroupName string) *
Consumers: consumers, Consumers: consumers,
} }
} }
func TestNamespaceExportedServicesACLs(t *testing.T) { func TestNamespaceExportedServicesACLs(t *testing.T) {
// Wire up a registry to generically invoke hooks // Wire up a registry to generically invoke hooks
registry := resource.NewRegistry() registry := resource.NewRegistry()
@ -115,7 +118,7 @@ func TestNamespaceExportedServicesValidations(t *testing.T) {
expectErrorENT []string expectErrorENT []string
} }
isEnterprise := structs.NodeEnterpriseMetaInDefaultPartition().PartitionOrEmpty() == "default" isEnterprise := versiontest.IsEnterprise()
run := func(t *testing.T, tc testcase) { run := func(t *testing.T, tc testcase) {
expectError := tc.expectErrorCE expectError := tc.expectErrorCE

View File

@ -4,13 +4,15 @@
package types package types
import ( import (
"github.com/hashicorp/consul/agent/structs" "testing"
"github.com/stretchr/testify/require"
"github.com/hashicorp/consul/internal/resource" "github.com/hashicorp/consul/internal/resource"
"github.com/hashicorp/consul/internal/resource/resourcetest" "github.com/hashicorp/consul/internal/resource/resourcetest"
pbmulticluster "github.com/hashicorp/consul/proto-public/pbmulticluster/v2beta1" pbmulticluster "github.com/hashicorp/consul/proto-public/pbmulticluster/v2beta1"
"github.com/hashicorp/consul/proto-public/pbresource" "github.com/hashicorp/consul/proto-public/pbresource"
"github.com/stretchr/testify/require" "github.com/hashicorp/consul/version/versiontest"
"testing"
) )
func validPartitionExportedServicesWithPeer(peerName string) *pbmulticluster.PartitionExportedServices { func validPartitionExportedServicesWithPeer(peerName string) *pbmulticluster.PartitionExportedServices {
@ -116,7 +118,7 @@ func TestPartitionExportedServicesValidations(t *testing.T) {
expectErrorENT []string expectErrorENT []string
} }
isEnterprise := structs.NodeEnterpriseMetaInDefaultPartition().PartitionOrEmpty() == "default" isEnterprise := versiontest.IsEnterprise()
run := func(t *testing.T, tc testcase) { run := func(t *testing.T, tc testcase) {
expectError := tc.expectErrorCE expectError := tc.expectErrorCE

View File

@ -7,15 +7,15 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/internal/resource" "github.com/hashicorp/consul/internal/resource"
"github.com/hashicorp/consul/proto-public/pbresource" "github.com/hashicorp/consul/proto-public/pbresource"
"github.com/hashicorp/consul/version/versiontest"
) )
// TestTenancies returns a list of tenancies which represent // TestTenancies returns a list of tenancies which represent
// the namespace and partition combinations that can be used in unit tests // the namespace and partition combinations that can be used in unit tests
func TestTenancies() []*pbresource.Tenancy { func TestTenancies() []*pbresource.Tenancy {
isEnterprise := structs.NodeEnterpriseMetaInDefaultPartition().PartitionOrEmpty() == "default" isEnterprise := versiontest.IsEnterprise()
tenancies := []*pbresource.Tenancy{Tenancy("default.default")} tenancies := []*pbresource.Tenancy{Tenancy("default.default")}
if isEnterprise { if isEnterprise {

View File

@ -0,0 +1,13 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package versiontest
import "github.com/hashicorp/consul/version"
// IsEnterprise returns true if the current build is a Consul Enterprise build.
//
// This should only be called from test code.
func IsEnterprise() bool {
return version.VersionMetadata == "ent"
}