fixtures

package module
v2.3.3 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2022 License: MIT Imports: 27 Imported by: 1

README

go-fixtures

Import github.com/charlieparkes/go-fixtures/v2.

Inspired by pytest, go-fixtures provides a collection of fixtures which automagically setup/teardown services.

Supported Fixtures
  • docker: Docker
  • postgres: Postgres, PostgresWithSchema, Psql
  • aws (localstack): coming soon

Documentation

Index

Constants

View Source
const (
	DEFAULT_POSTGRES_REPO    = "postgres"
	DEFAULT_POSTGRES_VERSION = "13-alpine"
)

Variables

This section is empty.

Functions

func CastUnicodeToSymbol

func CastUnicodeToSymbol(hex int) string

func ContainerAddress

func ContainerAddress(resource *dockertest.Resource, network *dockertest.Network) string

GetContainerAddress returns the address at which requests from the tests can be made into the container. When running inside a host container and connected to a bridge network, this returns the address of the container as known by the container network. When running inside a host container and not connected to a bridge network, this returns the network gateway. When not running inside a host container it returns localhost.

func ContainerTcpPort

func ContainerTcpPort(resource *dockertest.Resource, network *dockertest.Network, port string) string

GetContainerTcpPort returns the port which can be used to connect into the container from the test. When connected to a bridge network, the container exposed port can be used. Otherwise, we'll need to use the mapped port.

func FindPath

func FindPath(containing string) string

func GenerateString

func GenerateString() string

func GetContainerAddress deprecated

func GetContainerAddress(resource *dockertest.Resource, network *dockertest.Network) string

Deprecated: use ContainerAddress()

func GetContainerTcpPort deprecated

func GetContainerTcpPort(resource *dockertest.Resource, network *dockertest.Network, port string) string

Deprecated: use ContainerTcpPort()

func GetHostIP deprecated

func GetHostIP(resource *dockertest.Resource, network *dockertest.Network) string

Deprecated: use HostIP()

func GetHostName deprecated

func GetHostName(resource *dockertest.Resource) string

Deprecated: use HostName()

func GetRandomName added in v2.3.3

func GetRandomName(retry int) string

func GetStatusSymbol

func GetStatusSymbol(exitCode int) string

func GetSymbol

func GetSymbol(name string) string

func HostIP

func HostIP(resource *dockertest.Resource, network *dockertest.Network) string

func HostName

func HostName(resource *dockertest.Resource) string

func IsDockerRunning

func IsDockerRunning() bool

func IsRunningInContainer

func IsRunningInContainer() bool

IsRunningInContainer checks if the current executable is running inside a container. This implementation is currently docker-specific and won't work on other container engines, such as podman. A more portable solution is probably more ideal.

func MemoryMB added in v2.3.1

func MemoryMB() int64

func Retry

func Retry(d time.Duration, op func() error) error

func UseBridgeNetwork

func UseBridgeNetwork(network *dockertest.Network) bool

func WaitForContainer

func WaitForContainer(pool *dockertest.Pool, resource *dockertest.Resource) (int, error)

Types

type BaseFixture

type BaseFixture struct{}

func (*BaseFixture) Type

func (f *BaseFixture) Type() string

type ConnectionSettings

type ConnectionSettings struct {
	Driver       string
	Host         string
	Port         string
	User         string
	Password     string
	Database     string
	DisableSSL   bool
	MaxOpenConns int
}

func (*ConnectionSettings) Connect

func (cs *ConnectionSettings) Connect(ctx context.Context) (*pgx.Conn, error)

func (*ConnectionSettings) Copy

func (*ConnectionSettings) String

func (cs *ConnectionSettings) String() string

type Docker

type Docker struct {
	BaseFixture
	// contains filtered or unexported fields
}

func NewDocker

func NewDocker(opts ...DockerOpt) *Docker

func (*Docker) GetName deprecated

func (f *Docker) GetName() string

Deprecated: use Name()

func (*Docker) GetNamePrefix deprecated

func (f *Docker) GetNamePrefix() string

Deprecated: use NamePrefix()

func (*Docker) GetNetwork deprecated

func (f *Docker) GetNetwork() *dockertest.Network

Deprecated: use Network()

func (*Docker) GetNetworkName deprecated

func (f *Docker) GetNetworkName() string

Deprecated: use NetworkName()

func (*Docker) GetPool deprecated

func (f *Docker) GetPool() *dockertest.Pool

Deprecated: use Pool()

func (*Docker) Name

func (f *Docker) Name() string

func (*Docker) NamePrefix

func (f *Docker) NamePrefix() string

func (*Docker) Network

func (f *Docker) Network() *dockertest.Network

func (*Docker) NetworkName

func (f *Docker) NetworkName() string

func (*Docker) Pool

func (f *Docker) Pool() *dockertest.Pool

func (*Docker) Purge added in v2.3.2

func (f *Docker) Purge(r *dockertest.Resource)

func (*Docker) SetUp

func (f *Docker) SetUp(ctx context.Context) error

func (*Docker) TearDown

func (f *Docker) TearDown(context.Context) error

type DockerOpt

type DockerOpt func(*Docker)

func DockerLogger

func DockerLogger(logger *zap.Logger) DockerOpt

func DockerName

func DockerName(name string) DockerOpt

func DockerNamePrefix

func DockerNamePrefix(namePrefix string) DockerOpt

func DockerNetworkName

func DockerNetworkName(networkName string) DockerOpt

type Fixture

type Fixture interface {
	Type() string
	SetUp(context.Context) error
	TearDown(context.Context) error
}

type Fixtures

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

func NewFixtures

func NewFixtures(opts ...FixturesOpt) *Fixtures

func (*Fixtures) Add

func (f *Fixtures) Add(ctx context.Context, fixtures ...Fixture) error

func (*Fixtures) AddByName

func (f *Fixtures) AddByName(ctx context.Context, name string, fixture Fixture) error

func (*Fixtures) Docker

func (f *Fixtures) Docker() *Docker

Docker() returns the first Docker fixture. If none exists, panic.

func (*Fixtures) Get

func (f *Fixtures) Get(name string) Fixture

func (*Fixtures) Postgres

func (f *Fixtures) Postgres() *Postgres

Postgres() returns the first Postgres fixture. If none exists, panic.

func (*Fixtures) RecoverTearDown

func (f *Fixtures) RecoverTearDown(ctx context.Context)

RecoverTearDown returns a deferrable function that will teardown in the event of a panic.

func (*Fixtures) SetUp

func (f *Fixtures) SetUp(ctx context.Context) error

func (*Fixtures) TearDown

func (f *Fixtures) TearDown(ctx context.Context) error

type FixturesOpt

type FixturesOpt func(*Fixtures)

func FixturesLogger

func FixturesLogger(logger *zap.Logger) FixturesOpt

type Postgres

type Postgres struct {
	BaseFixture
	// contains filtered or unexported fields
}

func NewPostgres

func NewPostgres(d *Docker, opts ...PostgresOpt) *Postgres

func (*Postgres) ConnConfig

func (f *Postgres) ConnConfig() (*pgxpool.Config, error)

func (*Postgres) Connect

func (f *Postgres) Connect(ctx context.Context, opts ...PostgresConnOpt) (*pgxpool.Pool, error)

func (*Postgres) CopyDatabase

func (f *Postgres) CopyDatabase(ctx context.Context, source string, target string) error

CopyDatabase creates a copy of an existing postgres database using `createdb --template={source} {target}` source will default to the primary database

func (*Postgres) CreateDatabase

func (f *Postgres) CreateDatabase(ctx context.Context, name string) error

func (*Postgres) DropDatabase

func (f *Postgres) DropDatabase(ctx context.Context, name string) error

func (*Postgres) Dump

func (f *Postgres) Dump(ctx context.Context, dir string, filename string) error

func (*Postgres) GetConnConfig deprecated

func (f *Postgres) GetConnConfig() (*pgxpool.Config, error)

Deprecated: use ConnConfig()

func (*Postgres) GetConnection deprecated

func (f *Postgres) GetConnection(ctx context.Context, database string) (*pgx.Conn, error)

Deprecated: use Connect(ctx, PostgresConnDatabase("database_name"))

func (*Postgres) GetHostName deprecated

func (f *Postgres) GetHostName() string

Deprecated: use HostName()

func (*Postgres) GetSettings deprecated

func (f *Postgres) GetSettings() *ConnectionSettings

Deprecated: use Settings()

func (*Postgres) GetTableColumns deprecated

func (f *Postgres) GetTableColumns(ctx context.Context, database, schema, table string) ([]string, error)

Deprecated: use TableColumns()

func (*Postgres) GetTables deprecated

func (f *Postgres) GetTables(ctx context.Context, database string) ([]string, error)

Deprecated: use Tables()

func (*Postgres) HostName

func (f *Postgres) HostName() string

func (*Postgres) LoadSql

func (f *Postgres) LoadSql(ctx context.Context, path string) error

LoadSql runs a file or directory of *.sql files against the default postgres database.

func (*Postgres) LoadSqlPattern

func (f *Postgres) LoadSqlPattern(ctx context.Context, pattern string) error

LoadSqlPattern finds files matching a custom pattern and runs them against the default database.

func (*Postgres) MustConnect

func (f *Postgres) MustConnect(ctx context.Context, opts ...PostgresConnOpt) *pgxpool.Pool

func (*Postgres) Ping

func (f *Postgres) Ping(ctx context.Context) error

func (*Postgres) PingPsql

func (f *Postgres) PingPsql(ctx context.Context) error

func (*Postgres) Psql

func (f *Postgres) Psql(ctx context.Context, cmd []string, mounts []string, quiet bool) (int, error)

func (*Postgres) Restore

func (f *Postgres) Restore(ctx context.Context, dir string, filename string) error

func (*Postgres) SetUp

func (f *Postgres) SetUp(ctx context.Context) error

func (*Postgres) Settings

func (f *Postgres) Settings() *ConnectionSettings

func (*Postgres) TableColumns

func (f *Postgres) TableColumns(ctx context.Context, database, schema, table string) ([]string, error)

func (*Postgres) TableExists

func (f *Postgres) TableExists(ctx context.Context, database, schema, table string) (bool, error)

func (*Postgres) Tables

func (f *Postgres) Tables(ctx context.Context, database string) ([]string, error)

func (*Postgres) TearDown

func (f *Postgres) TearDown(ctx context.Context) error

func (*Postgres) ValidateModel

func (f *Postgres) ValidateModel(ctx context.Context, databaseName string, i interface{}) error

func (*Postgres) ValidateModels

func (f *Postgres) ValidateModels(ctx context.Context, databaseName string, i ...interface{}) error

type PostgresConnConfig

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

type PostgresConnOpt

type PostgresConnOpt func(*PostgresConnConfig)

func PostgresConnCreateCopy

func PostgresConnCreateCopy() PostgresConnOpt

func PostgresConnDatabase

func PostgresConnDatabase(database string) PostgresConnOpt

func PostgresConnRole

func PostgresConnRole(role string) PostgresConnOpt

type PostgresOpt

type PostgresOpt func(*Postgres)

func PostgresDocker

func PostgresDocker(d *Docker) PostgresOpt

func PostgresExpireAfter

func PostgresExpireAfter(expireAfter uint) PostgresOpt

Tell docker to kill the container after an unreasonable amount of test time to prevent orphans. Defaults to 600 seconds.

func PostgresLogger

func PostgresLogger(logger *zap.Logger) PostgresOpt

func PostgresMounts

func PostgresMounts(mounts []string) PostgresOpt

func PostgresRepo

func PostgresRepo(repo string) PostgresOpt

func PostgresSettings

func PostgresSettings(settings *ConnectionSettings) PostgresOpt

func PostgresSkipTearDown

func PostgresSkipTearDown() PostgresOpt

func PostgresTimeoutAfter

func PostgresTimeoutAfter(timeoutAfter uint) PostgresOpt

Wait this long for operations to execute. Defaults to 30 seconds.

func PostgresVersion

func PostgresVersion(version string) PostgresOpt

Jump to

Keyboard shortcuts

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