testserver

package
v2.0.1+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 23, 2020 License: Apache-2.0 Imports: 26 Imported by: 3

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/testserver"
import "testing"
import "time"

func TestRunServer(t *testing.T) {
   ts, err := testserver.NewTestServer()
   if err != nil {
     t.Fatal(err)
   }
   if err := ts.Start(); err != nil {
     t.Fatal(err)
   }
   defer ts.Stop()

   url := ts.PGURL()
   if url == nil {
     t.Fatalf("url not found")
   }
   t.Logf("URL: %s", url.String())

   db, err := sql.Open("postgres", url.String())
   if err != nil {
     t.Fatal(err)
   }
 }

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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.

func SecureOpt

func SecureOpt() testServerOpt

SecureOpt is a TestServer option that can be passed to NewTestServer to enable secure mode.

Types

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(db *sql.DB) error
}

TestServer is a helper to run a real cockroach node.

func NewTestServer

func NewTestServer(opts ...testServerOpt) (TestServer, error)

NewTestServer creates a new TestServer, but does not start it. 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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL