Skip to content

Commit 0276345

Browse files
committed
testserver: expose NoFileCleanup option to allow retention of files after Stop
Retaining files after `Stop` can help debug some test-failures because one can start the server back up (off the store-dir left behind after failure). Looking at the data the failing test left behind can help hypothesize better.
1 parent 687b669 commit 0276345

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

testserver/testserver.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ type testServerArgs struct {
240240
envVars []string // to be passed to cmd.Env
241241
localityFlags []string
242242
cockroachLogsDir string
243+
noFileCleanup bool // do not clean files at `Stop`
243244
}
244245

245246
// CockroachBinaryPathOpt is a TestServer option that can be passed to
@@ -274,6 +275,14 @@ func StoreOnDiskOpt() TestServerOpt {
274275
}
275276
}
276277

278+
// NoFileCleanup is a TestServer option that can be passed to NewTestServer
279+
// to skip cleanup of files when Testserver is stopped
280+
func NoFileCleanup() TestServerOpt {
281+
return func(args *testServerArgs) {
282+
args.noFileCleanup = true
283+
}
284+
}
285+
277286
// SetStoreMemSizeOpt is a TestServer option that can be passed to NewTestServer
278287
// to set the proportion of available memory that is allocated
279288
// to the test server.
@@ -879,7 +888,9 @@ func (ts *testServerImpl) Stop() {
879888
ts.mu.RLock()
880889

881890
nodeDir := filepath.Join(ts.baseDir, strconv.Itoa(i))
882-
if err := os.RemoveAll(nodeDir); err != nil {
891+
if ts.serverArgs.noFileCleanup {
892+
log.Printf("%s: skipping file cleanup of node-dir %s", testserverMessagePrefix, nodeDir)
893+
} else if err := os.RemoveAll(nodeDir); err != nil {
883894
log.Printf("error deleting tmp directory %s for node: %s", nodeDir, err)
884895
}
885896
if closeErr := ts.nodes[i].stdoutBuf.Close(); closeErr != nil {
@@ -891,7 +902,11 @@ func (ts *testServerImpl) Stop() {
891902
}
892903

893904
// Only cleanup on intentional stops.
894-
_ = os.RemoveAll(ts.baseDir)
905+
if ts.serverArgs.noFileCleanup {
906+
log.Printf("%s: skipping file cleanup of base-dir %s", testserverMessagePrefix, ts.baseDir)
907+
} else {
908+
_ = os.RemoveAll(ts.baseDir)
909+
}
895910
}
896911

897912
func (ts *testServerImpl) CockroachInit() error {

testserver/testserver_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,12 @@ func TestRunServer(t *testing.T) {
234234
)
235235
},
236236
},
237+
{
238+
name: "No File Cleanup",
239+
instantiation: func(t *testing.T) (*sql.DB, func()) {
240+
return testserver.NewDBForTest(t, testserver.NoFileCleanup())
241+
},
242+
},
237243
} {
238244
t.Run(tc.name, func(t *testing.T) {
239245
db, stop := tc.instantiation(t)

0 commit comments

Comments
 (0)