integration

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2023 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Overview

Package integration is a helper for running integration tests.

Index

Examples

Constants

View Source
const (
	// EnvPGConnString is the environment variable examined for a DSN for a
	// pre-existing database engine. If unset, an appropriate database will
	// attempt to be downloaded and run.
	EnvPGConnString = "POSTGRES_CONNECTION_STRING"

	// EnvPGVersion is the environment variable examined for the version of
	// PostgreSQL used if an embedded binary would be used.
	EnvPGVersion = `PGVERSION`
)

Variables

This section is empty.

Functions

func CacheDir

func CacheDir(t testing.TB) string

CacheDir reports a directory for caching test data and creates it if necessary.

func DBSetup

func DBSetup() func()

DBSetup queues setup and teardown for a postgres engine instance. If the "integration" build tag is not provided, then nothing is done. If the environment variable at EnvPGConnString is populated and the "integration" build tag is provided, then the value of that environment variable is used instead of an embedded postgres binary.

See the example for usage.

Example
package main

import (
	"os"
	"testing"

	"github.com/vishnuchalla/claircore/test/integration"
)

func main() {
	var m *testing.M // This should come from TestMain's argument.
	var c int
	defer func() { os.Exit(c) }()
	defer integration.DBSetup()()
	c = m.Run()
}
Output:

func NeedDB

func NeedDB(t testing.TB)

NeedDB is like Skip, except that the test will run if the needed binaries have been fetched.

See the example for usage.

Example
package main

import (
	"context"
	"testing"

	"github.com/vishnuchalla/claircore/test/integration"
)

func main() {
	var t *testing.T // This should come from the test function's argument.
	integration.NeedDB(t)

	ctx := context.Background()
	db, err := integration.NewDB(ctx, t)
	if err != nil {
		t.Fatal(err)
	}
	defer db.Close(ctx, t)

	t.Log("OK") // Do some test that needs a database.
}
Output:

func PackageCacheDir

func PackageCacheDir(t testing.TB) string

PackageCacheDir reports a directory for caching per-package test data and creates it if necessary.

func Skip

func Skip(t testing.TB)

Skip will skip the current test or benchmark if this package was built without the "integration" build tag unless the "CI" environment variable is defined and the "short" flag is not provided.

This should be used as an annotation at the top of the function, like (*testing.T).Parallel().

See the example for usage.

Example
package main

import (
	"testing"

	"github.com/vishnuchalla/claircore/test/integration"
)

func main() {
	var t *testing.T // This should come from the test function's argument.
	t.Parallel()
	integration.Skip(t)
	t.Log("OK") // Do some test that needs external setup.
}
Output:

Types

type DB

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

DB is a handle for connecting to and cleaning up a test database.

If testing.Verbose reports true, the database engine will be run with the "auto_explain" module enabled. See the auto_explain documentation for more information. Setting the environment variable "PGEXPLAIN_FORMAT" will control the output format. This does not apply when the test harness is not controlling the database.

func NewDB

func NewDB(ctx context.Context, t testing.TB) (*DB, error)

NewDB generates a unique database for use in integration tests.

The returned database has a random name and a dedicated owner role configured. The "uuid-ossp" extension is already loaded.

DBSetup and NeedDB are expected to have been called correctly.

func NewPersistentDB

func NewPersistentDB(ctx context.Context, t testing.TB, id string) *DB

NewPersistentDB creates a database for use in integration tests.

Unlike the NewDB function, this function uses a deterministic role and database name, and does not drop the database during cleanup. The "uuid-ossp" extension is already loaded.

DBSetup and NeedDB are expected to have been called correctly.

func (*DB) Close

func (db *DB) Close(ctx context.Context, t testing.TB)

Close tears down the created database.

func (*DB) Config

func (db *DB) Config() *pgxpool.Config

Config returns a pgxpool.Config for the test database.

func (*DB) String

func (db *DB) String() string

type Engine

type Engine struct {
	DSN string
	// contains filtered or unexported fields
}

Engine is a helper for managing a postgres engine.

func (*Engine) Start

func (e *Engine) Start(t testing.TB) error

Start configures and starts the database engine.

This should not be called multiple times.

func (*Engine) Stop

func (e *Engine) Stop() error

Stop stops the database engine.

It's an error to call Stop before a successful Start.

Jump to

Keyboard shortcuts

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