@@ -152,7 +152,6 @@ type testServerImpl struct {
152152 version * version.Version
153153 serverArgs testServerArgs
154154 serverState int
155- baseDir string
156155 pgURL []pgURLChan
157156 initCmd * exec.Cmd
158157 initCmdArgs []string
@@ -241,6 +240,7 @@ type testServerArgs struct {
241240 localityFlags []string
242241 cockroachLogsDir string
243242 noFileCleanup bool // do not clean files at `Stop`
243+ baseDir string
244244}
245245
246246// CockroachBinaryPathOpt is a TestServer option that can be passed to
@@ -283,6 +283,14 @@ func NoFileCleanup() TestServerOpt {
283283 }
284284}
285285
286+ // NoFileCleanup is a TestServer option that can be passed to NewTestServer
287+ // to skip cleanup of files when Testserver is stopped
288+ func BasedirOverride (baseDirPath string ) TestServerOpt {
289+ return func (args * testServerArgs ) {
290+ args .baseDir = baseDirPath
291+ }
292+ }
293+
286294// SetStoreMemSizeOpt is a TestServer option that can be passed to NewTestServer
287295// to set the proportion of available memory that is allocated
288296// to the test server.
@@ -437,20 +445,26 @@ var errStoppedInMiddle = errors.New("download stopped in middle")
437445// If the download fails, we attempt just call "cockroach", hoping it is
438446// found in your path.
439447func NewTestServer (opts ... TestServerOpt ) (TestServer , error ) {
440- baseDir , err := os .MkdirTemp ("" , "cockroach-testserver" )
441- if err != nil {
442- return nil , fmt .Errorf ("%s: could not create temp directory: %w" , testserverMessagePrefix , err )
443- }
444-
445448 serverArgs := & testServerArgs {numNodes : 1 }
446449 serverArgs .storeMemSize = defaultStoreMemSize
447450 serverArgs .initTimeoutSeconds = defaultInitTimeout
448451 serverArgs .pollListenURLTimeoutSeconds = defaultPollListenURLTimeout
449452 serverArgs .listenAddrHost = defaultListenAddrHost
450- serverArgs .cockroachLogsDir = baseDir
451453 for _ , applyOptToArgs := range opts {
452454 applyOptToArgs (serverArgs )
453455 }
456+ var err error
457+ if serverArgs .baseDir == "" {
458+ baseDir , err := os .MkdirTemp ("" , "cockroach-testserver" )
459+ if err != nil {
460+ return nil , fmt .Errorf ("%s: could not create temp directory: %w" , testserverMessagePrefix , err )
461+ }
462+ serverArgs .baseDir = baseDir
463+ if serverArgs .cockroachLogsDir == "" {
464+ serverArgs .cockroachLogsDir = baseDir
465+ }
466+ }
467+
454468 log .Printf ("cockroach logs directory: %s" , serverArgs .cockroachLogsDir )
455469
456470 if serverArgs .cockroachBinary != "" {
@@ -496,7 +510,7 @@ func NewTestServer(opts ...TestServerOpt) (TestServer, error) {
496510 }
497511
498512 mkDir := func (name string ) (string , error ) {
499- path := filepath .Join (baseDir , name )
513+ path := filepath .Join (serverArgs . baseDir , name )
500514 if err := os .MkdirAll (path , 0755 ); err != nil {
501515 return "" , fmt .Errorf ("%s: could not create %s directory: %s: %w" ,
502516 testserverMessagePrefix , name , path , err )
@@ -638,7 +652,6 @@ func NewTestServer(opts ...TestServerOpt) (TestServer, error) {
638652 serverArgs : * serverArgs ,
639653 version : v ,
640654 serverState : stateNew ,
641- baseDir : baseDir ,
642655 initCmdArgs : initArgs ,
643656 curTenantID : firstTenantID ,
644657 nodes : nodes ,
@@ -686,7 +699,7 @@ func (ts *testServerImpl) StderrForNode(i int) string {
686699
687700// BaseDir returns directory StoreOnDiskOpt writes to if used.
688701func (ts * testServerImpl ) BaseDir () string {
689- return ts .baseDir
702+ return ts .serverArgs . baseDir
690703}
691704
692705// PGURL returns the postgres connection URL to reach the started
@@ -887,7 +900,7 @@ func (ts *testServerImpl) Stop() {
887900 }
888901 ts .mu .RLock ()
889902
890- nodeDir := filepath .Join (ts .baseDir , strconv .Itoa (i ))
903+ nodeDir := filepath .Join (ts .BaseDir () , strconv .Itoa (i ))
891904 if ts .serverArgs .noFileCleanup {
892905 log .Printf ("%s: skipping file cleanup of node-dir %s" , testserverMessagePrefix , nodeDir )
893906 } else if err := os .RemoveAll (nodeDir ); err != nil {
@@ -903,9 +916,9 @@ func (ts *testServerImpl) Stop() {
903916
904917 // Only cleanup on intentional stops.
905918 if ts .serverArgs .noFileCleanup {
906- log .Printf ("%s: skipping file cleanup of base-dir %s" , testserverMessagePrefix , ts .baseDir )
919+ log .Printf ("%s: skipping file cleanup of base-dir %s" , testserverMessagePrefix , ts .BaseDir () )
907920 } else {
908- _ = os .RemoveAll (ts .baseDir )
921+ _ = os .RemoveAll (ts .BaseDir () )
909922 }
910923}
911924
@@ -922,7 +935,7 @@ func (ts *testServerImpl) CockroachInit() error {
922935 // Set the working directory of the cockroach process to our temp folder.
923936 // This stops cockroach from polluting the project directory with _dump
924937 // folders.
925- ts .initCmd .Dir = ts .baseDir
938+ ts .initCmd .Dir = ts .serverArgs . baseDir
926939
927940 err := ts .initCmd .Start ()
928941 if ts .initCmd .Process != nil {
0 commit comments