diff --git a/go.mod b/go.mod index 5a3cdd5..945846d 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/lib/pq v1.10.6 github.com/pkg/errors v0.9.1 github.com/shopspring/decimal v1.3.1 // indirect + github.com/stretchr/testify v1.7.0 golang.org/x/crypto v0.0.0-20220517005047-85d78b3ac167 // indirect golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a // indirect gorm.io/driver/postgres v1.3.5 diff --git a/testserver/testserver.go b/testserver/testserver.go index fda4c1e..0ad4ced 100644 --- a/testserver/testserver.go +++ b/testserver/testserver.go @@ -18,22 +18,23 @@ // from your PATH. // // To use, run as follows: -// import "github.com/cockroachdb/cockroach-go/v2/testserver" -// import "testing" -// import "time" // -// func TestRunServer(t *testing.T) { -// ts, err := testserver.NewTestServer() -// if err != nil { -// t.Fatal(err) -// } -// defer ts.Stop() +// import "github.com/cockroachdb/cockroach-go/v2/testserver" +// import "testing" +// import "time" // -// db, err := sql.Open("postgres", ts.PGURL().String()) -// if err != nil { -// t.Fatal(err) -// } -// } +// func TestRunServer(t *testing.T) { +// ts, err := testserver.NewTestServer() +// if err != nil { +// t.Fatal(err) +// } +// defer ts.Stop() +// +// db, err := sql.Open("postgres", ts.PGURL().String()) +// if err != nil { +// t.Fatal(err) +// } +// } package testserver import ( @@ -222,6 +223,7 @@ type testServerArgs struct { cockroachBinary string // path to cockroach executable file upgradeCockroachBinary string // path to cockroach binary for upgrade numNodes int + externalIODir string } // CockroachBinaryPathOpt is a TestServer option that can be passed to @@ -330,6 +332,14 @@ func ThreeNodeOpt() TestServerOpt { } } +// ExternalIODirOpt is a TestServer option that can be passed to NewTestServer to +// specify the external IO directory to be used for the cluster. +func ExternalIODirOpt(ioDir string) TestServerOpt { + return func(args *testServerArgs) { + args.externalIODir = ioDir + } +} + const ( logsDirName = "logs" certsDirName = "certs" @@ -489,6 +499,10 @@ func NewTestServer(opts ...TestServerOpt) (TestServer, error) { serverArgs.httpPorts = make([]int, serverArgs.numNodes) } + if serverArgs.externalIODir == "" { + serverArgs.externalIODir = "disabled" + } + for i := 0; i < serverArgs.numNodes; i++ { nodes[i].state = stateNew nodes[i].listeningURLFile = filepath.Join(baseDir, fmt.Sprintf("listen-url%d", i)) @@ -503,6 +517,7 @@ func NewTestServer(opts ...TestServerOpt) (TestServer, error) { fmt.Sprintf("--http-addr=localhost:%d", serverArgs.httpPorts[i]), "--listening-url-file=" + nodes[i].listeningURLFile, joinArg, + "--external-io-dir=" + serverArgs.externalIODir, } } else { nodes[0].startCmdArgs = []string{ @@ -515,6 +530,7 @@ func NewTestServer(opts ...TestServerOpt) (TestServer, error) { "--http-port=" + strconv.Itoa(serverArgs.httpPorts[0]), storeArg, "--listening-url-file=" + nodes[i].listeningURLFile, + "--external-io-dir=" + serverArgs.externalIODir, } } } diff --git a/testserver/testserver_test.go b/testserver/testserver_test.go index 583706c..e69ad86 100644 --- a/testserver/testserver_test.go +++ b/testserver/testserver_test.go @@ -252,6 +252,29 @@ func TestCockroachBinaryPathOpt(t *testing.T) { } } +func TestCockroachExternalIODirOpt(t *testing.T) { + externalDir, err := os.MkdirTemp("/tmp", "cockroach-testserver") + require.NoError(t, err) + defer func() { + err := os.RemoveAll(externalDir) + require.NoError(t, err) + }() + + db, cleanup := testserver.NewDBForTest(t, testserver.ExternalIODirOpt(externalDir)) + defer cleanup() + + // test that we can use external dir + _, err = db.Exec("BACKUP INTO 'nodelocal://self/backup'") + require.NoError(t, err) + + // test that external dir has files + f, err := os.Open(externalDir) + require.NoError(t, err) + defer f.Close() + _, err = f.Readdirnames(1) + require.NoError(t, err) +} + func TestPGURLWhitespace(t *testing.T) { ts, err := testserver.NewTestServer() if err != nil {