spantest

package
v0.0.0-...-f0b372e Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Overview

Package spantest implements: * start/stop the Cloud Spanner Emulator, * creation/removal of a temporary gcloud config, * creation of a temporary Spanner instance, * creation/destruction of a temporary Spanner database.

Index

Constants

View Source
const (

	// IntegrationTestEnvVar is the name of the environment variable which controls
	// whether spanner tests are executed.
	// The value must be "1" for integration tests to run.
	IntegrationTestEnvVar = "INTEGRATION_TESTS"
)

Variables

This section is empty.

Functions

func SanitizeDBName

func SanitizeDBName(name string) string

SanitizeDBName tranforms name to a valid one. If name is already valid, returns it without changes.

func SpannerTestContext

func SpannerTestContext(tb testing.TB, cleanupDatabase CleanupDatabase) context.Context

SpannerTestContext returns a context for testing code that talks to Spanner. Skips the test if integration tests are not enabled.

Tests that use Spanner MUST NOT call t.Parallel().

func SpannerTestMain

func SpannerTestMain(m *testing.M, findInitScript FindInitScript)

SpannerTestMain is a test main function for packages that have tests that talk to spanner. It creates/destroys a temporary spanner database before/after running tests.

This function never returns. Instead it calls os.Exit with the value returned by m.Run().

Types

type CleanupDatabase

type CleanupDatabase func(ctx context.Context, client *spanner.Client) error

CleanupDatabase deletes all data from all tables.

type Emulator

type Emulator struct {
	// contains filtered or unexported fields
}

Emulator is for starting and stopping a Cloud Spanner Emulator process. TODO(crbug.com/1066993): Make Emulator an interfact with two implementations (*nativeEmulator and *dockerEmulator).

func StartEmulator

func StartEmulator(ctx context.Context) (*Emulator, error)

StartEmulator starts a Cloud Spanner Emulator instance.

func (*Emulator) NewInstance

func (e *Emulator) NewInstance(ctx context.Context, projectName string) (string, error)

NewInstance creates a temporary instance using Cloud Spanner Emulator.

func (*Emulator) Stop

func (e *Emulator) Stop() error

Stop kills the emulator process and removes the temporary gcloud config directory.

type FindInitScript

type FindInitScript func() (string, error)

FindInitScript returns path to a .sql file which contains the schema of the tested Spanner database.

type TempDB

type TempDB struct {
	Name string
	// contains filtered or unexported fields
}

TempDB is a temporary Spanner database.

func NewTempDB

func NewTempDB(ctx context.Context, cfg TempDBConfig, e *Emulator) (*TempDB, error)

NewTempDB creates a temporary database with a random name. The caller is responsible for calling Drop on the returned TempDB to cleanup resources after usage. Unless it uses Cloud Spanner Emulator, then the database will be dropped when emulator stops.

func (*TempDB) Client

func (db *TempDB) Client(ctx context.Context) (*spanner.Client, error)

Client returns a spanner client connected to the database.

func (*TempDB) Drop

func (db *TempDB) Drop(ctx context.Context) error

Drop deletes the database.

type TempDBConfig

type TempDBConfig struct {
	// InstanceName is the name of Spannner instance where to create the
	// temporary database.
	// Format: projects/{project}/instances/{instance}.
	// Defaults to chromeinfra.TestSpannerInstance.
	InstanceName string

	// InitScriptPath is a path to a DDL script to initialize the database.
	//
	// In lieu of a proper DDL parser, it is parsed using regexes.
	// Therefore the script MUST:
	//   - Use `#“ and/or `--“ for comments. No block comments.
	//   - Separate DDL statements with `;\n`.
	//
	// If empty, the database is created with no tables.
	InitScriptPath string
}

TempDBConfig specifies how to create a temporary database.

Jump to

Keyboard shortcuts

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