Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testserver: expose NoFileCleanup option to allow retention of files after Stop #172

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions testserver/testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ type testServerArgs struct {
envVars []string // to be passed to cmd.Env
localityFlags []string
cockroachLogsDir string
noFileCleanup bool // do not clean files at `Stop`
}

// CockroachBinaryPathOpt is a TestServer option that can be passed to
Expand Down Expand Up @@ -274,6 +275,14 @@ func StoreOnDiskOpt() TestServerOpt {
}
}

// NoFileCleanup is a TestServer option that can be passed to NewTestServer
// to skip cleanup of files when Testserver is stopped
func NoFileCleanup() TestServerOpt {
return func(args *testServerArgs) {
args.noFileCleanup = true
}
}

// SetStoreMemSizeOpt is a TestServer option that can be passed to NewTestServer
// to set the proportion of available memory that is allocated
// to the test server.
Expand Down Expand Up @@ -879,7 +888,9 @@ func (ts *testServerImpl) Stop() {
ts.mu.RLock()

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

// Only cleanup on intentional stops.
_ = os.RemoveAll(ts.baseDir)
if ts.serverArgs.noFileCleanup {
log.Printf("%s: skipping file cleanup of base-dir %s", testserverMessagePrefix, ts.baseDir)
} else {
_ = os.RemoveAll(ts.baseDir)
}
}

func (ts *testServerImpl) CockroachInit() error {
Expand Down
6 changes: 6 additions & 0 deletions testserver/testserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,12 @@ func TestRunServer(t *testing.T) {
)
},
},
{
name: "No File Cleanup",
instantiation: func(t *testing.T) (*sql.DB, func()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any way for this test to assert that the directory still exists at the end?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a dedicated test TestBaseDirIsPreservedWhenNoFileCleanupRequested (but also had to add some additional machinery to power it).

return testserver.NewDBForTest(t, testserver.NoFileCleanup())
},
},
} {
t.Run(tc.name, func(t *testing.T) {
db, stop := tc.instantiation(t)
Expand Down