Skip to content

Commit

Permalink
Merge pull request #146 from pawalt/external-io
Browse files Browse the repository at this point in the history
Add External IO Dir TestServerOpt
  • Loading branch information
pawalt committed Sep 12, 2022
2 parents c8da6e3 + dacb81a commit de6be8c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 30 additions & 14 deletions testserver/testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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))
Expand All @@ -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{
Expand All @@ -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,
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions testserver/testserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit de6be8c

Please sign in to comment.