@@ -238,6 +238,7 @@ type testServerArgs struct {
238238 initTimeoutSeconds int
239239 pollListenURLTimeoutSeconds int
240240 envVars []string // to be passed to cmd.Env
241+ localityFlags []string
241242}
242243
243244// CockroachBinaryPathOpt is a TestServer option that can be passed to
@@ -383,6 +384,18 @@ func PollListenURLTimeoutOpt(timeout int) TestServerOpt {
383384 }
384385}
385386
387+ // LocalityFlagsOpt is used to specify the --locality flag for each node.
388+ //
389+ // Example Usage:
390+ //
391+ // localities := LocalityFlagsOpt("region=us-west", "region=us-east", "region=us-central")
392+ // server, err := NewTestServer(ThreeNodeOpt(), localities)
393+ func LocalityFlagsOpt (locality ... string ) TestServerOpt {
394+ return func (args * testServerArgs ) {
395+ args .localityFlags = locality
396+ }
397+ }
398+
386399// EnvVarOpt is a list of environment variables to be passed to the start
387400// command. Each entry in the slice should be in `key=value` format.
388401func EnvVarOpt (vars []string ) TestServerOpt {
@@ -430,6 +443,10 @@ func NewTestServer(opts ...TestServerOpt) (TestServer, error) {
430443 serverArgs .numNodes , len (serverArgs .listenAddrPorts )))
431444 }
432445
446+ if 0 < len (serverArgs .localityFlags ) && len (serverArgs .localityFlags ) != serverArgs .numNodes {
447+ panic (fmt .Sprintf ("got %d locality flags when %d are needed (one for each node)" , len (serverArgs .localityFlags ), serverArgs .numNodes ))
448+ }
449+
433450 var err error
434451 if serverArgs .cockroachBinary != "" {
435452 log .Printf ("Using custom cockroach binary: %s" , serverArgs .cockroachBinary )
@@ -574,6 +591,9 @@ func NewTestServer(opts ...TestServerOpt) (TestServer, error) {
574591 "--external-io-dir=" + serverArgs .externalIODir ,
575592 }
576593 }
594+ if 0 < len (serverArgs .localityFlags ) {
595+ nodes [i ].startCmdArgs = append (nodes [i ].startCmdArgs , fmt .Sprintf ("--locality=%s" , serverArgs .localityFlags [i ]))
596+ }
577597 }
578598
579599 // We only need initArgs if we're creating a testserver
0 commit comments