[OSS] Pull split ns/partition var out of testing file (#13337)

The api module previously had defaultPartition and defaultNamespace vars
for when we need default/empty split usage between ent/oss respectively.

This commit moves those two variables out of test code so that they can
be used for the service exports config entry's `GetNamespace()` method.

Previously `GetNamespace()` would return "default" in both OSS and enterprise,
which can trip up automation that passes the result of this method as the
namespace to write a config entry.

The split vars are kept private to prevent external usage, and prefixed with
`split` for more clarity about their behavior.
This commit is contained in:
Freddy 2022-06-01 14:42:33 -06:00 committed by GitHub
parent 1b69366a39
commit 143dc75e0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 65 additions and 62 deletions

View File

@ -363,7 +363,7 @@ func TestAPI_AgentServicesWithFilterOpts(t *testing.T) {
} }
require.NoError(t, agent.ServiceRegister(reg)) require.NoError(t, agent.ServiceRegister(reg))
opts := &QueryOptions{Namespace: defaultNamespace} opts := &QueryOptions{Namespace: splitDefaultNamespace}
services, err := agent.ServicesWithFilterOpts("foo in Tags", opts) services, err := agent.ServicesWithFilterOpts("foo in Tags", opts)
require.NoError(t, err) require.NoError(t, err)
require.Len(t, services, 1) require.Len(t, services, 1)
@ -791,8 +791,8 @@ func TestAPI_AgentService(t *testing.T) {
Warning: 1, Warning: 1,
}, },
Meta: map[string]string{}, Meta: map[string]string{},
Namespace: defaultNamespace, Namespace: splitDefaultNamespace,
Partition: defaultPartition, Partition: splitDefaultPartition,
Datacenter: "dc1", Datacenter: "dc1",
} }
require.Equal(t, expect, got) require.Equal(t, expect, got)
@ -932,7 +932,7 @@ func TestAPI_AgentUpdateTTLOpts(t *testing.T) {
} }
} }
opts := &QueryOptions{Namespace: defaultNamespace} opts := &QueryOptions{Namespace: splitDefaultNamespace}
if err := agent.UpdateTTLOpts("service:foo", "foo", HealthWarning, opts); err != nil { if err := agent.UpdateTTLOpts("service:foo", "foo", HealthWarning, opts); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
@ -1007,7 +1007,7 @@ func TestAPI_AgentChecksWithFilterOpts(t *testing.T) {
reg.TTL = "15s" reg.TTL = "15s"
require.NoError(t, agent.CheckRegister(reg)) require.NoError(t, agent.CheckRegister(reg))
opts := &QueryOptions{Namespace: defaultNamespace} opts := &QueryOptions{Namespace: splitDefaultNamespace}
checks, err := agent.ChecksWithFilterOpts("Name == foo", opts) checks, err := agent.ChecksWithFilterOpts("Name == foo", opts)
require.NoError(t, err) require.NoError(t, err)
require.Len(t, checks, 1) require.Len(t, checks, 1)
@ -1382,7 +1382,7 @@ func TestAPI_ServiceMaintenanceOpts(t *testing.T) {
} }
// Specify namespace in query option // Specify namespace in query option
opts := &QueryOptions{Namespace: defaultNamespace} opts := &QueryOptions{Namespace: splitDefaultNamespace}
// Enable maintenance mode // Enable maintenance mode
if err := agent.EnableServiceMaintenanceOpts("redis", "broken", opts); err != nil { if err := agent.EnableServiceMaintenanceOpts("redis", "broken", opts); err != nil {
@ -1701,7 +1701,7 @@ func TestAPI_AgentHealthServiceOpts(t *testing.T) {
requireServiceHealthID := func(t *testing.T, serviceID, expected string, shouldExist bool) { requireServiceHealthID := func(t *testing.T, serviceID, expected string, shouldExist bool) {
msg := fmt.Sprintf("service id:%s, shouldExist:%v, expectedStatus:%s : bad %%s", serviceID, shouldExist, expected) msg := fmt.Sprintf("service id:%s, shouldExist:%v, expectedStatus:%s : bad %%s", serviceID, shouldExist, expected)
opts := &QueryOptions{Namespace: defaultNamespace} opts := &QueryOptions{Namespace: splitDefaultNamespace}
state, out, err := agent.AgentHealthServiceByIDOpts(serviceID, opts) state, out, err := agent.AgentHealthServiceByIDOpts(serviceID, opts)
require.Nil(t, err, msg, "err") require.Nil(t, err, msg, "err")
require.Equal(t, expected, state, msg, "state") require.Equal(t, expected, state, msg, "state")
@ -1715,7 +1715,7 @@ func TestAPI_AgentHealthServiceOpts(t *testing.T) {
requireServiceHealthName := func(t *testing.T, serviceName, expected string, shouldExist bool) { requireServiceHealthName := func(t *testing.T, serviceName, expected string, shouldExist bool) {
msg := fmt.Sprintf("service name:%s, shouldExist:%v, expectedStatus:%s : bad %%s", serviceName, shouldExist, expected) msg := fmt.Sprintf("service name:%s, shouldExist:%v, expectedStatus:%s : bad %%s", serviceName, shouldExist, expected)
opts := &QueryOptions{Namespace: defaultNamespace} opts := &QueryOptions{Namespace: splitDefaultNamespace}
state, outs, err := agent.AgentHealthServiceByNameOpts(serviceName, opts) state, outs, err := agent.AgentHealthServiceByNameOpts(serviceName, opts)
require.Nil(t, err, msg, "err") require.Nil(t, err, msg, "err")
require.Equal(t, expected, state, msg, "state") require.Equal(t, expected, state, msg, "state")

View File

@ -51,7 +51,7 @@ func TestAPI_CatalogNodes(t *testing.T) {
{ {
ID: s.Config.NodeID, ID: s.Config.NodeID,
Node: s.Config.NodeName, Node: s.Config.NodeName,
Partition: defaultPartition, Partition: splitDefaultPartition,
Address: "127.0.0.1", Address: "127.0.0.1",
Datacenter: "dc1", Datacenter: "dc1",
TaggedAddresses: map[string]string{ TaggedAddresses: map[string]string{
@ -1152,8 +1152,8 @@ func TestAPI_CatalogGatewayServices_Terminating(t *testing.T) {
expect := []*GatewayService{ expect := []*GatewayService{
{ {
Service: CompoundServiceName{Name: "api", Namespace: defaultNamespace, Partition: defaultPartition}, Service: CompoundServiceName{Name: "api", Namespace: splitDefaultNamespace, Partition: splitDefaultPartition},
Gateway: CompoundServiceName{Name: "terminating", Namespace: defaultNamespace, Partition: defaultPartition}, Gateway: CompoundServiceName{Name: "terminating", Namespace: splitDefaultNamespace, Partition: splitDefaultPartition},
GatewayKind: ServiceKindTerminatingGateway, GatewayKind: ServiceKindTerminatingGateway,
CAFile: "api/ca.crt", CAFile: "api/ca.crt",
CertFile: "api/client.crt", CertFile: "api/client.crt",
@ -1161,8 +1161,8 @@ func TestAPI_CatalogGatewayServices_Terminating(t *testing.T) {
SNI: "my-domain", SNI: "my-domain",
}, },
{ {
Service: CompoundServiceName{Name: "redis", Namespace: defaultNamespace, Partition: defaultPartition}, Service: CompoundServiceName{Name: "redis", Namespace: splitDefaultNamespace, Partition: splitDefaultPartition},
Gateway: CompoundServiceName{Name: "terminating", Namespace: defaultNamespace, Partition: defaultPartition}, Gateway: CompoundServiceName{Name: "terminating", Namespace: splitDefaultNamespace, Partition: splitDefaultPartition},
GatewayKind: ServiceKindTerminatingGateway, GatewayKind: ServiceKindTerminatingGateway,
CAFile: "ca.crt", CAFile: "ca.crt",
CertFile: "client.crt", CertFile: "client.crt",
@ -1220,15 +1220,15 @@ func TestAPI_CatalogGatewayServices_Ingress(t *testing.T) {
expect := []*GatewayService{ expect := []*GatewayService{
{ {
Service: CompoundServiceName{Name: "api", Namespace: defaultNamespace, Partition: defaultPartition}, Service: CompoundServiceName{Name: "api", Namespace: splitDefaultNamespace, Partition: splitDefaultPartition},
Gateway: CompoundServiceName{Name: "ingress", Namespace: defaultNamespace, Partition: defaultPartition}, Gateway: CompoundServiceName{Name: "ingress", Namespace: splitDefaultNamespace, Partition: splitDefaultPartition},
GatewayKind: ServiceKindIngressGateway, GatewayKind: ServiceKindIngressGateway,
Protocol: "tcp", Protocol: "tcp",
Port: 8888, Port: 8888,
}, },
{ {
Service: CompoundServiceName{Name: "redis", Namespace: defaultNamespace, Partition: defaultPartition}, Service: CompoundServiceName{Name: "redis", Namespace: splitDefaultNamespace, Partition: splitDefaultPartition},
Gateway: CompoundServiceName{Name: "ingress", Namespace: defaultNamespace, Partition: defaultPartition}, Gateway: CompoundServiceName{Name: "ingress", Namespace: splitDefaultNamespace, Partition: splitDefaultPartition},
GatewayKind: ServiceKindIngressGateway, GatewayKind: ServiceKindIngressGateway,
Protocol: "tcp", Protocol: "tcp",
Port: 9999, Port: 9999,

View File

@ -139,8 +139,8 @@ func TestAPI_ConfigEntry_DiscoveryChain(t *testing.T) {
entry: &ServiceResolverConfigEntry{ entry: &ServiceResolverConfigEntry{
Kind: ServiceResolver, Kind: ServiceResolver,
Name: "test-failover", Name: "test-failover",
Partition: defaultPartition, Partition: splitDefaultPartition,
Namespace: defaultNamespace, Namespace: splitDefaultNamespace,
DefaultSubset: "v1", DefaultSubset: "v1",
Subsets: map[string]ServiceResolverSubset{ Subsets: map[string]ServiceResolverSubset{
"v1": { "v1": {
@ -156,7 +156,7 @@ func TestAPI_ConfigEntry_DiscoveryChain(t *testing.T) {
}, },
"v1": { "v1": {
Service: "alternate", Service: "alternate",
Namespace: defaultNamespace, Namespace: splitDefaultNamespace,
}, },
}, },
ConnectTimeout: 5 * time.Second, ConnectTimeout: 5 * time.Second,
@ -172,12 +172,12 @@ func TestAPI_ConfigEntry_DiscoveryChain(t *testing.T) {
entry: &ServiceResolverConfigEntry{ entry: &ServiceResolverConfigEntry{
Kind: ServiceResolver, Kind: ServiceResolver,
Name: "test-redirect", Name: "test-redirect",
Partition: defaultPartition, Partition: splitDefaultPartition,
Namespace: defaultNamespace, Namespace: splitDefaultNamespace,
Redirect: &ServiceResolverRedirect{ Redirect: &ServiceResolverRedirect{
Service: "test-failover", Service: "test-failover",
ServiceSubset: "v2", ServiceSubset: "v2",
Namespace: defaultNamespace, Namespace: splitDefaultNamespace,
Datacenter: "d", Datacenter: "d",
}, },
}, },
@ -188,14 +188,14 @@ func TestAPI_ConfigEntry_DiscoveryChain(t *testing.T) {
entry: &ServiceSplitterConfigEntry{ entry: &ServiceSplitterConfigEntry{
Kind: ServiceSplitter, Kind: ServiceSplitter,
Name: "test-split", Name: "test-split",
Partition: defaultPartition, Partition: splitDefaultPartition,
Namespace: defaultNamespace, Namespace: splitDefaultNamespace,
Splits: []ServiceSplit{ Splits: []ServiceSplit{
{ {
Weight: 90, Weight: 90,
Service: "test-failover", Service: "test-failover",
ServiceSubset: "v1", ServiceSubset: "v1",
Namespace: defaultNamespace, Namespace: splitDefaultNamespace,
RequestHeaders: &HTTPHeaderModifiers{ RequestHeaders: &HTTPHeaderModifiers{
Set: map[string]string{ Set: map[string]string{
"x-foo": "bar", "x-foo": "bar",
@ -208,7 +208,7 @@ func TestAPI_ConfigEntry_DiscoveryChain(t *testing.T) {
{ {
Weight: 10, Weight: 10,
Service: "test-redirect", Service: "test-redirect",
Namespace: defaultNamespace, Namespace: splitDefaultNamespace,
}, },
}, },
Meta: map[string]string{ Meta: map[string]string{
@ -223,8 +223,8 @@ func TestAPI_ConfigEntry_DiscoveryChain(t *testing.T) {
entry: &ServiceRouterConfigEntry{ entry: &ServiceRouterConfigEntry{
Kind: ServiceRouter, Kind: ServiceRouter,
Name: "test-route", Name: "test-route",
Partition: defaultPartition, Partition: splitDefaultPartition,
Namespace: defaultNamespace, Namespace: splitDefaultNamespace,
Routes: []ServiceRoute{ Routes: []ServiceRoute{
{ {
Match: &ServiceRouteMatch{ Match: &ServiceRouteMatch{
@ -241,8 +241,8 @@ func TestAPI_ConfigEntry_DiscoveryChain(t *testing.T) {
Destination: &ServiceRouteDestination{ Destination: &ServiceRouteDestination{
Service: "test-failover", Service: "test-failover",
ServiceSubset: "v2", ServiceSubset: "v2",
Namespace: defaultNamespace, Namespace: splitDefaultNamespace,
Partition: defaultPartition, Partition: splitDefaultPartition,
PrefixRewrite: "/", PrefixRewrite: "/",
RequestTimeout: 5 * time.Second, RequestTimeout: 5 * time.Second,
NumRetries: 5, NumRetries: 5,
@ -334,8 +334,8 @@ func TestAPI_ConfigEntry_ServiceResolver_LoadBalancer(t *testing.T) {
entry: &ServiceResolverConfigEntry{ entry: &ServiceResolverConfigEntry{
Kind: ServiceResolver, Kind: ServiceResolver,
Name: "test-least-req", Name: "test-least-req",
Partition: defaultPartition, Partition: splitDefaultPartition,
Namespace: defaultNamespace, Namespace: splitDefaultNamespace,
LoadBalancer: &LoadBalancer{ LoadBalancer: &LoadBalancer{
Policy: "least_request", Policy: "least_request",
LeastRequestConfig: &LeastRequestConfig{ChoiceCount: 10}, LeastRequestConfig: &LeastRequestConfig{ChoiceCount: 10},
@ -348,8 +348,8 @@ func TestAPI_ConfigEntry_ServiceResolver_LoadBalancer(t *testing.T) {
entry: &ServiceResolverConfigEntry{ entry: &ServiceResolverConfigEntry{
Kind: ServiceResolver, Kind: ServiceResolver,
Name: "test-ring-hash", Name: "test-ring-hash",
Namespace: defaultNamespace, Namespace: splitDefaultNamespace,
Partition: defaultPartition, Partition: splitDefaultPartition,
LoadBalancer: &LoadBalancer{ LoadBalancer: &LoadBalancer{
Policy: "ring_hash", Policy: "ring_hash",
RingHashConfig: &RingHashConfig{ RingHashConfig: &RingHashConfig{

View File

@ -57,7 +57,7 @@ type ServiceConsumer struct {
func (e *ExportedServicesConfigEntry) GetKind() string { return ExportedServices } func (e *ExportedServicesConfigEntry) GetKind() string { return ExportedServices }
func (e *ExportedServicesConfigEntry) GetName() string { return e.Name } func (e *ExportedServicesConfigEntry) GetName() string { return e.Name }
func (e *ExportedServicesConfigEntry) GetPartition() string { return e.Name } func (e *ExportedServicesConfigEntry) GetPartition() string { return e.Name }
func (e *ExportedServicesConfigEntry) GetNamespace() string { return IntentionDefaultNamespace } func (e *ExportedServicesConfigEntry) GetNamespace() string { return splitDefaultNamespace }
func (e *ExportedServicesConfigEntry) GetMeta() map[string]string { return e.Meta } func (e *ExportedServicesConfigEntry) GetMeta() map[string]string { return e.Meta }
func (e *ExportedServicesConfigEntry) GetCreateIndex() uint64 { return e.CreateIndex } func (e *ExportedServicesConfigEntry) GetCreateIndex() uint64 { return e.CreateIndex }
func (e *ExportedServicesConfigEntry) GetModifyIndex() uint64 { return e.ModifyIndex } func (e *ExportedServicesConfigEntry) GetModifyIndex() uint64 { return e.ModifyIndex }

View File

@ -17,7 +17,7 @@ func TestAPI_ConfigEntries_ExportedServices(t *testing.T) {
testutil.RunStep(t, "set and get", func(t *testing.T) { testutil.RunStep(t, "set and get", func(t *testing.T) {
exports := &ExportedServicesConfigEntry{ exports := &ExportedServicesConfigEntry{
Name: PartitionDefaultName, Name: PartitionDefaultName,
Partition: defaultPartition, Partition: splitDefaultPartition,
Meta: map[string]string{ Meta: map[string]string{
"gir": "zim", "gir": "zim",
}, },
@ -48,7 +48,7 @@ func TestAPI_ConfigEntries_ExportedServices(t *testing.T) {
Services: []ExportedService{ Services: []ExportedService{
{ {
Name: "db", Name: "db",
Namespace: defaultNamespace, Namespace: splitDefaultNamespace,
Consumers: []ServiceConsumer{ Consumers: []ServiceConsumer{
{ {
PeerName: "alpha", PeerName: "alpha",
@ -60,7 +60,7 @@ func TestAPI_ConfigEntries_ExportedServices(t *testing.T) {
"foo": "bar", "foo": "bar",
"gir": "zim", "gir": "zim",
}, },
Partition: defaultPartition, Partition: splitDefaultPartition,
} }
_, wm, err := entries.Set(updated, nil) _, wm, err := entries.Set(updated, nil)

View File

@ -213,8 +213,8 @@ func TestAPI_ConfigEntries(t *testing.T) {
"foo": "bar", "foo": "bar",
"gir": "zim", "gir": "zim",
}, },
Partition: defaultPartition, Partition: splitDefaultPartition,
Namespace: defaultNamespace, Namespace: splitDefaultNamespace,
} }
ce := c.ConfigEntries() ce := c.ConfigEntries()

View File

@ -87,7 +87,7 @@ func TestAPI_CoordinateUpdate(t *testing.T) {
newCoord.Height = 0.5 newCoord.Height = 0.5
entry := &CoordinateEntry{ entry := &CoordinateEntry{
Node: node, Node: node,
Partition: defaultPartition, Partition: splitDefaultPartition,
Coord: newCoord, Coord: newCoord,
} }
_, err = coord.Update(entry, nil) _, err = coord.Update(entry, nil)

View File

@ -223,8 +223,8 @@ func TestAPI_HealthChecks(t *testing.T) {
ServiceName: "foo", ServiceName: "foo",
ServiceTags: []string{"bar"}, ServiceTags: []string{"bar"},
Type: "ttl", Type: "ttl",
Partition: defaultPartition, Partition: splitDefaultPartition,
Namespace: defaultNamespace, Namespace: splitDefaultNamespace,
}, },
} }

10
api/oss.go Normal file
View File

@ -0,0 +1,10 @@
//go:build !consulent
// +build !consulent
package api
// The following defaults return "default" in enterprise and "" in OSS.
// This constant is useful when a default value is needed for an
// operation that will reject non-empty values in OSS.
const splitDefaultNamespace = ""
const splitDefaultPartition = ""

View File

@ -1,7 +0,0 @@
//go:build !consulent
// +build !consulent
package api
var defaultNamespace = ""
var defaultPartition = ""

View File

@ -153,7 +153,7 @@ func TestAPI_ClientTxn(t *testing.T) {
CreateIndex: ret.Results[0].KV.CreateIndex, CreateIndex: ret.Results[0].KV.CreateIndex,
ModifyIndex: ret.Results[0].KV.ModifyIndex, ModifyIndex: ret.Results[0].KV.ModifyIndex,
Namespace: ret.Results[0].KV.Namespace, Namespace: ret.Results[0].KV.Namespace,
Partition: defaultPartition, Partition: splitDefaultPartition,
}, },
}, },
&TxnResult{ &TxnResult{
@ -165,14 +165,14 @@ func TestAPI_ClientTxn(t *testing.T) {
CreateIndex: ret.Results[1].KV.CreateIndex, CreateIndex: ret.Results[1].KV.CreateIndex,
ModifyIndex: ret.Results[1].KV.ModifyIndex, ModifyIndex: ret.Results[1].KV.ModifyIndex,
Namespace: ret.Results[0].KV.Namespace, Namespace: ret.Results[0].KV.Namespace,
Partition: defaultPartition, Partition: splitDefaultPartition,
}, },
}, },
&TxnResult{ &TxnResult{
Node: &Node{ Node: &Node{
ID: nodeID, ID: nodeID,
Node: "foo", Node: "foo",
Partition: defaultPartition, Partition: splitDefaultPartition,
Address: "2.2.2.2", Address: "2.2.2.2",
Datacenter: "dc1", Datacenter: "dc1",
CreateIndex: ret.Results[2].Node.CreateIndex, CreateIndex: ret.Results[2].Node.CreateIndex,
@ -184,8 +184,8 @@ func TestAPI_ClientTxn(t *testing.T) {
ID: "foo1", ID: "foo1",
CreateIndex: ret.Results[3].Service.CreateIndex, CreateIndex: ret.Results[3].Service.CreateIndex,
ModifyIndex: ret.Results[3].Service.CreateIndex, ModifyIndex: ret.Results[3].Service.CreateIndex,
Partition: defaultPartition, Partition: splitDefaultPartition,
Namespace: defaultNamespace, Namespace: splitDefaultNamespace,
}, },
}, },
&TxnResult{ &TxnResult{
@ -203,8 +203,8 @@ func TestAPI_ClientTxn(t *testing.T) {
DeregisterCriticalServiceAfterDuration: 20 * time.Second, DeregisterCriticalServiceAfterDuration: 20 * time.Second,
}, },
Type: "tcp", Type: "tcp",
Partition: defaultPartition, Partition: splitDefaultPartition,
Namespace: defaultNamespace, Namespace: splitDefaultNamespace,
CreateIndex: ret.Results[4].Check.CreateIndex, CreateIndex: ret.Results[4].Check.CreateIndex,
ModifyIndex: ret.Results[4].Check.CreateIndex, ModifyIndex: ret.Results[4].Check.CreateIndex,
}, },
@ -224,8 +224,8 @@ func TestAPI_ClientTxn(t *testing.T) {
DeregisterCriticalServiceAfterDuration: 160 * time.Second, DeregisterCriticalServiceAfterDuration: 160 * time.Second,
}, },
Type: "tcp", Type: "tcp",
Partition: defaultPartition, Partition: splitDefaultPartition,
Namespace: defaultNamespace, Namespace: splitDefaultNamespace,
CreateIndex: ret.Results[4].Check.CreateIndex, CreateIndex: ret.Results[4].Check.CreateIndex,
ModifyIndex: ret.Results[4].Check.CreateIndex, ModifyIndex: ret.Results[4].Check.CreateIndex,
}, },
@ -266,14 +266,14 @@ func TestAPI_ClientTxn(t *testing.T) {
CreateIndex: ret.Results[0].KV.CreateIndex, CreateIndex: ret.Results[0].KV.CreateIndex,
ModifyIndex: ret.Results[0].KV.ModifyIndex, ModifyIndex: ret.Results[0].KV.ModifyIndex,
Namespace: ret.Results[0].KV.Namespace, Namespace: ret.Results[0].KV.Namespace,
Partition: defaultPartition, Partition: splitDefaultPartition,
}, },
}, },
&TxnResult{ &TxnResult{
Node: &Node{ Node: &Node{
ID: s.Config.NodeID, ID: s.Config.NodeID,
Node: s.Config.NodeName, Node: s.Config.NodeName,
Partition: defaultPartition, Partition: splitDefaultPartition,
Address: "127.0.0.1", Address: "127.0.0.1",
Datacenter: "dc1", Datacenter: "dc1",
TaggedAddresses: map[string]string{ TaggedAddresses: map[string]string{