Documentation ¶
Overview ¶
Package testserver provides helpers to run a cockroach binary within tests. It automatically downloads the latest cockroach binary for your platform (Linux-amd64 and Darwin-amd64 only for now), or attempts to run "cockroach" 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() db, err := sql.Open("postgres", ts.PGURL().String()) if err != nil { t.Fatal(err) } }
Index ¶
- func GetDownloadFilename(response *http.Response, nonStableDB bool, latestStableVersion string) (string, error)
- func GetDownloadResponse(nonStable bool) (*http.Response, string, error)
- func NewDBForTest(t *testing.T, opts ...TestServerOpt) (*sql.DB, func())
- func NewDBForTestWithDatabase(t *testing.T, database string, opts ...TestServerOpt) (*sql.DB, func())
- type TestConfig
- type TestServer
- type TestServerOpt
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetDownloadFilename ¶
func GetDownloadFilename( response *http.Response, nonStableDB bool, latestStableVersion string, ) (string, error)
GetDownloadFilename returns the local filename of the downloaded CRDB binary file.
func GetDownloadResponse ¶
GetDownloadResponse return the http response of a CRDB download. It creates the url for downloading a CRDB binary for current runtime OS, makes a request to this url, and return the response.
func NewDBForTest ¶
func NewDBForTest(t *testing.T, opts ...TestServerOpt) (*sql.DB, func())
NewDBForTest creates a new CockroachDB TestServer instance and opens a SQL database connection to it. Returns a sql *DB instance and a shutdown function. The caller is responsible for executing the returned shutdown function on exit.
func NewDBForTestWithDatabase ¶
func NewDBForTestWithDatabase( t *testing.T, database string, opts ...TestServerOpt, ) (*sql.DB, func())
NewDBForTestWithDatabase creates a new CockroachDB TestServer instance and opens a SQL database connection to it. If database is specified, the returned connection will explicitly connect to it. Returns a sql *DB instance a shutdown function. The caller is responsible for executing the returned shutdown function on exit.
Types ¶
type TestConfig ¶
type TestServer ¶
type TestServer interface { // Start starts the server. Start() error // Stop stops the server and cleans up any associated resources. Stop() // Stdout returns the entire contents of the process' stdout. Stdout() string // Stdout returns the entire contents of the process' stderr. Stderr() string // PGURL returns the postgres connection URL to this server. PGURL() *url.URL // WaitForInit retries until a SQL connection is successfully established to // this server. WaitForInit() error }
TestServer is a helper to run a real cockroach node.
func NewTestServer ¶
func NewTestServer(opts ...TestServerOpt) (TestServer, error)
NewTestServer creates a new TestServer and starts it. It also waits until the server is ready to accept clients, so it safe to connect to the server returned by this function right away. The cockroach binary for your OS and ARCH is downloaded automatically. If the download fails, we attempt just call "cockroach", hoping it is found in your path.
type TestServerOpt ¶
type TestServerOpt func(args *testServerArgs)
TestServerOpt is passed to NewTestServer.
func NonStableDbOpt ¶
func NonStableDbOpt() TestServerOpt
NonStableDbOpt is a TestServer option that can be passed to NewTestServer to download the latest beta version of CRDB, but not necessary a stable one.
func RootPasswordOpt ¶
func RootPasswordOpt(pw string) TestServerOpt
RootPasswordOpt is a TestServer option that, when passed to NewTestServer, sets the given password for the root user (and returns a URL using it from PGURL(). This avoids having to use client certs.
func SecureOpt ¶
func SecureOpt() TestServerOpt
SecureOpt is a TestServer option that can be passed to NewTestServer to enable secure mode.
func SetStoreMemSizeOpt ¶
func SetStoreMemSizeOpt(memSize float64) TestServerOpt
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.
func StopDownloadInMiddleOpt ¶
func StopDownloadInMiddleOpt() TestServerOpt
StopDownloadInMiddleOpt is a TestServer option used only in testing. It is used to test the flock over downloaded CRDB binary. It should not be used in production.
func StoreOnDiskOpt ¶
func StoreOnDiskOpt() TestServerOpt
StoreOnDiskOpt is a TestServer option that can be passed to NewTestServer to enable storing database in memory.