integration

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2022 License: Apache-2.0 Imports: 25 Imported by: 2

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 DBSetup added in v0.5.0

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/quay/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 added in v0.5.0

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/quay/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 Skip

func Skip(t testing.TB)

Skip will skip the current test or benchmark if this package was built without the "integration" build tag.

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/quay/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.

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 (*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.

type Engine added in v0.5.0

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

Engine is a helper for managing a postgres engine.

func (*Engine) Start added in v0.5.0

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 added in v0.5.0

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