Skip to content

Commit 65848d0

Browse files
craig[bot]Nukitt
andcommitted
Merge #158276
158276: testserver: consolidate testserver config to InitTestServerFactory r=yuzefovich,cthumuluru-crdb a=Nukitt Currently, we use global overrides to configure testserver's tenant and DRPC options. These were also using defer statements, [but it was concluded that ](#157192 (review) is not much value in actually performing the cleanup function to reset the global state option in a defer, since the test process exits immediately after finishing the tests. This change introduces the framework changes required to consolidate the configuration into InitTestServerFactory. For now, we also have the global overrides present to maintain backward compatibility until we can have all the usages of the `TestingSetDefaultTenantSelectionOverride` and `TestingGlobalDRPCOption` removed. We also change the priority order of changing these configs, highest priority is given to the config set in the test itself, then the global override, then factory default and then the enviorment variable change through the cli. After this changes, we can simply add the configuration we want for the package into `InitTestServerFactory`. Example usage of it would look like: ```go serverutils.InitTestServerFactory(server.TestServerFactory, serverutils.WithDRPCOption(base.TestDRPCEnabled), serverutils.WithTenantOption(base.TestTenantProbabilisticOnly, ) ``` Epic: None Fixes: #158004 Release note: None Co-authored-by: Nukitt <[email protected]>
2 parents b276200 + dd1d13d commit 65848d0

File tree

1 file changed

+85
-32
lines changed

1 file changed

+85
-32
lines changed

pkg/testutils/serverutils/test_server_shim.go

Lines changed: 85 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ func ShouldStartDefaultTestTenant(
8888
// Explicit case for disabling the default test tenant.
8989
if baseArg.TestTenantAlwaysDisabled() {
9090
if issueNum, label := baseArg.IssueRef(); issueNum != 0 {
91-
t.Logf("cluster virtualization disabled due to issue: #%d (expected label: %s)", issueNum, label)
91+
if t != nil {
92+
t.Logf("cluster virtualization disabled due to issue: #%d (expected label: %s)", issueNum, label)
93+
}
9294
}
9395
return baseArg
9496
}
@@ -103,9 +105,45 @@ func ShouldStartDefaultTestTenant(
103105
// If the test tenant is explicitly enabled and a process mode selected, then
104106
// we are done.
105107
if !baseArg.TestTenantNoDecisionMade() {
108+
if t != nil {
109+
t.Log("using test tenant configuration from test explicit setting")
110+
}
106111
return baseArg
107112
}
108113

114+
if globalDefaultSelectionOverride.isSet {
115+
override := globalDefaultSelectionOverride.value
116+
if override.TestTenantNoDecisionMade() {
117+
panic("programming error: global override does not contain a final decision")
118+
}
119+
if override.TestTenantAlwaysDisabled() {
120+
if issueNum, label := override.IssueRef(); issueNum != 0 && t != nil {
121+
t.Logf("cluster virtualization disabled in global scope due to issue: #%d (expected label: %s)", issueNum, label)
122+
}
123+
} else {
124+
if t != nil {
125+
t.Log(defaultTestTenantMessage(override.SharedProcessMode()) + "\n(via override from TestingSetDefaultTenantSelectionOverride)")
126+
}
127+
}
128+
return override
129+
}
130+
131+
if factoryDefaultTenant != nil {
132+
defaultArg := *factoryDefaultTenant
133+
// If factory default made a decision, return it.
134+
if !defaultArg.TestTenantNoDecisionMade() {
135+
if t != nil {
136+
t.Log("using test tenant configuration from testserver factory defaults")
137+
}
138+
if defaultArg.TestTenantAlwaysDisabled() {
139+
if issueNum, label := defaultArg.IssueRef(); issueNum != 0 && t != nil {
140+
t.Logf("cluster virtualization disabled due to issue: #%d (expected label: %s)", issueNum, label)
141+
}
142+
}
143+
return defaultArg
144+
}
145+
}
146+
109147
// Determine if the default test tenant should be run as a shared process.
110148
var shared bool
111149
switch {
@@ -127,24 +165,11 @@ func ShouldStartDefaultTestTenant(
127165

128166
if decision, override := testTenantDecisionFromEnvironment(baseArg, shared); override {
129167
if decision.TestTenantAlwaysEnabled() {
130-
t.Log(defaultTestTenantMessage(decision.SharedProcessMode()) + "\n(override via COCKROACH_TEST_TENANT)")
131-
}
132-
return decision
133-
}
134-
135-
if globalDefaultSelectionOverride.isSet {
136-
override := globalDefaultSelectionOverride.value
137-
if override.TestTenantNoDecisionMade() {
138-
panic("programming error: global override does not contain a final decision")
139-
}
140-
if override.TestTenantAlwaysDisabled() {
141-
if issueNum, label := override.IssueRef(); issueNum != 0 {
142-
t.Logf("cluster virtualization disabled in global scope due to issue: #%d (expected label: %s)", issueNum, label)
168+
if t != nil {
169+
t.Log(defaultTestTenantMessage(decision.SharedProcessMode()) + "\n(override via COCKROACH_TEST_TENANT)")
143170
}
144-
} else {
145-
t.Log(defaultTestTenantMessage(override.SharedProcessMode()) + "\n(override via TestingSetDefaultTenantSelectionOverride)")
146171
}
147-
return override
172+
return decision
148173
}
149174

150175
// Note: we ask the metamorphic framework for a "disable" value, instead
@@ -217,9 +242,6 @@ var globalDefaultDRPCOptionOverride struct {
217242
}
218243

219244
// TestingGlobalDRPCOption sets the package-level DefaultTestDRPCOption.
220-
//
221-
// Note: This override will be superseded by any more specific options provided
222-
// when starting the server or cluster.
223245
func TestingGlobalDRPCOption(v base.DefaultTestDRPCOption) func() {
224246
globalDefaultDRPCOptionOverride.isSet = true
225247
globalDefaultDRPCOptionOverride.value = v
@@ -245,12 +267,32 @@ func TestingSetDefaultTenantSelectionOverride(v base.DefaultTestTenantOptions) f
245267
}
246268

247269
var srvFactoryImpl TestServerFactory
270+
var factoryDefaultDRPC *base.DefaultTestDRPCOption
271+
var factoryDefaultTenant *base.DefaultTestTenantOptions
272+
273+
type TestServerFactoryOption func()
274+
275+
func WithTenantOption(opt base.DefaultTestTenantOptions) TestServerFactoryOption {
276+
return func() {
277+
factoryDefaultTenant = &opt
278+
}
279+
}
280+
func WithDRPCOption(opt base.DefaultTestDRPCOption) TestServerFactoryOption {
281+
return func() {
282+
factoryDefaultDRPC = &opt
283+
}
284+
}
248285

249286
// InitTestServerFactory should be called once to provide the implementation
250287
// of the service. It will be called from a xx_test package that can import the
251-
// server package.
252-
func InitTestServerFactory(impl TestServerFactory) {
288+
// server package. Optional parameters can be passed to set default options:
289+
// - WithDRPCOption: Sets the default DRPC mode for test servers
290+
// - WithTenantOption: Sets the default tenant configuration for test servers
291+
func InitTestServerFactory(impl TestServerFactory, opts ...TestServerFactoryOption) {
253292
srvFactoryImpl = impl
293+
for _, opt := range opts {
294+
opt()
295+
}
254296
}
255297

256298
// TestLogger is the minimal interface of testing.T that is used by
@@ -281,6 +323,8 @@ func StartServerOnlyE(t TestLogger, params base.TestServerArgs) (TestServerInter
281323
allowAdditionalTenants := params.DefaultTestTenant.AllowAdditionalTenants()
282324

283325
// Update the flags with the actual decisions for test configuration.
326+
// Priority of these Should* functions:
327+
// Test explicit value > global override > factory defaults > env vars > metamorphic.
284328
params.DefaultTestTenant = ShouldStartDefaultTestTenant(t, params.DefaultTestTenant)
285329
params.DefaultDRPCOption = ShouldEnableDRPC(ctx, t, params.DefaultDRPCOption)
286330

@@ -579,20 +623,27 @@ func parseDefaultTestDRPCOptionFromEnv() base.DefaultTestDRPCOption {
579623
}
580624

581625
// ShouldEnableDRPC determines the final DRPC option based on the input
582-
// option and any global overrides, resolving random choices to a concrete
583-
// enabled/disabled state.
626+
// option, resolving random choices to a concrete enabled/disabled state.
584627
func ShouldEnableDRPC(
585628
ctx context.Context, t TestLogger, option base.DefaultTestDRPCOption,
586629
) base.DefaultTestDRPCOption {
587630
var logSuffix string
588631

589-
// Check environment variable first
590-
if envOption := parseDefaultTestDRPCOptionFromEnv(); envOption != base.TestDRPCUnset {
591-
option = envOption
592-
logSuffix = " (override by COCKROACH_TEST_DRPC environment variable)"
593-
} else if option == base.TestDRPCUnset && globalDefaultDRPCOptionOverride.isSet {
594-
option = globalDefaultDRPCOptionOverride.value
595-
logSuffix = " (override by TestingGlobalDRPCOption)"
632+
if option == base.TestDRPCUnset {
633+
if globalDefaultDRPCOptionOverride.isSet {
634+
option = globalDefaultDRPCOptionOverride.value
635+
logSuffix = " (via override by TestingGlobalDRPCOption)"
636+
} else if factoryDefaultDRPC != nil {
637+
option = *factoryDefaultDRPC
638+
logSuffix = " (via testserver factory defaults)"
639+
} else if envOption := parseDefaultTestDRPCOptionFromEnv(); envOption != base.TestDRPCUnset {
640+
option = envOption
641+
logSuffix = " (via COCKROACH_TEST_DRPC environment variable)"
642+
} else {
643+
return base.TestDRPCUnset
644+
}
645+
} else {
646+
logSuffix = " (via test explicit setting)"
596647
}
597648

598649
enableDRPC := false
@@ -607,7 +658,9 @@ func ShouldEnableDRPC(
607658
}
608659

609660
if enableDRPC {
610-
t.Log("DRPC is enabled" + logSuffix)
661+
if t != nil {
662+
t.Log("DRPC is enabled" + logSuffix)
663+
}
611664
return base.TestDRPCEnabled
612665
}
613666

0 commit comments

Comments
 (0)