2023-10-13 16:31:56 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
|
|
package resourcetest
|
|
|
|
|
|
|
|
import (
|
2023-11-15 06:11:56 +00:00
|
|
|
"fmt"
|
2023-10-13 16:31:56 +00:00
|
|
|
"strings"
|
2023-10-13 23:16:26 +00:00
|
|
|
"testing"
|
2023-10-13 16:31:56 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/internal/resource"
|
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
2023-11-13 16:30:07 +00:00
|
|
|
"github.com/hashicorp/consul/version/versiontest"
|
2023-10-13 16:31:56 +00:00
|
|
|
)
|
|
|
|
|
2023-11-07 03:39:15 +00:00
|
|
|
// TestTenancies returns a list of tenancies which represent
|
|
|
|
// the namespace and partition combinations that can be used in unit tests
|
|
|
|
func TestTenancies() []*pbresource.Tenancy {
|
2023-11-13 16:30:07 +00:00
|
|
|
isEnterprise := versiontest.IsEnterprise()
|
2023-11-07 03:39:15 +00:00
|
|
|
|
|
|
|
tenancies := []*pbresource.Tenancy{Tenancy("default.default")}
|
|
|
|
if isEnterprise {
|
2024-01-29 20:08:31 +00:00
|
|
|
// TODO(namespaces/v2) move the default partition + non-default namespace test to run even for CE.
|
2023-11-07 03:39:15 +00:00
|
|
|
tenancies = append(tenancies, Tenancy("default.bar"), Tenancy("foo.default"), Tenancy("foo.bar"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return tenancies
|
|
|
|
}
|
|
|
|
|
2024-02-07 22:01:10 +00:00
|
|
|
func ToPartitionScoped(t *pbresource.Tenancy) *pbresource.Tenancy {
|
|
|
|
return &pbresource.Tenancy{
|
|
|
|
Partition: t.Partition,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-13 16:31:56 +00:00
|
|
|
// Tenancy constructs a pbresource.Tenancy from a concise string representation
|
|
|
|
// suitable for use in unit tests.
|
|
|
|
//
|
2024-01-29 20:08:31 +00:00
|
|
|
// - "" : partition="" namespace=""
|
|
|
|
// - "foo" : partition="foo" namespace=""
|
|
|
|
// - "foo.bar" : partition="foo" namespace="bar"
|
|
|
|
// - <others> : partition="BAD" namespace="BAD"
|
2023-10-13 16:31:56 +00:00
|
|
|
func Tenancy(s string) *pbresource.Tenancy {
|
|
|
|
parts := strings.Split(s, ".")
|
|
|
|
switch len(parts) {
|
|
|
|
case 0:
|
|
|
|
return resource.DefaultClusteredTenancy()
|
|
|
|
case 1:
|
|
|
|
v := resource.DefaultPartitionedTenancy()
|
|
|
|
v.Partition = parts[0]
|
|
|
|
return v
|
|
|
|
case 2:
|
|
|
|
v := resource.DefaultNamespacedTenancy()
|
|
|
|
v.Partition = parts[0]
|
|
|
|
v.Namespace = parts[1]
|
|
|
|
return v
|
|
|
|
default:
|
2024-01-29 20:08:31 +00:00
|
|
|
return &pbresource.Tenancy{Partition: "BAD", Namespace: "BAD"}
|
2023-10-13 16:31:56 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-13 23:16:26 +00:00
|
|
|
|
|
|
|
func DefaultTenancyForType(t *testing.T, reg resource.Registration) *pbresource.Tenancy {
|
|
|
|
switch reg.Scope {
|
|
|
|
case resource.ScopeNamespace:
|
|
|
|
return resource.DefaultNamespacedTenancy()
|
|
|
|
case resource.ScopePartition:
|
|
|
|
return resource.DefaultPartitionedTenancy()
|
|
|
|
case resource.ScopeCluster:
|
|
|
|
return resource.DefaultClusteredTenancy()
|
|
|
|
default:
|
|
|
|
t.Fatalf("unsupported resource scope: %v", reg.Scope)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2023-11-15 06:11:56 +00:00
|
|
|
|
2023-11-15 08:19:40 +00:00
|
|
|
func AppendTenancyInfoSubtest(name string, subtestName string, tenancy *pbresource.Tenancy) string {
|
|
|
|
return fmt.Sprintf("%s_%s_Namespace_%s_Partition/%s", name, tenancy.Namespace, tenancy.Partition, subtestName)
|
|
|
|
}
|
|
|
|
|
2023-11-15 06:11:56 +00:00
|
|
|
func AppendTenancyInfo(name string, tenancy *pbresource.Tenancy) string {
|
|
|
|
return fmt.Sprintf("%s_%s_Namespace_%s_Partition", name, tenancy.Namespace, tenancy.Partition)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RunWithTenancies(testFunc func(tenancy *pbresource.Tenancy), t *testing.T) {
|
|
|
|
for _, tenancy := range TestTenancies() {
|
|
|
|
t.Run(AppendTenancyInfo(t.Name(), tenancy), func(t *testing.T) {
|
|
|
|
testFunc(tenancy)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|