docker

package module
v1.0.1 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MainWithServices

func MainWithServices(m *testing.M, services ...Service)

MainWithServices is a helper function for running tests with multiples docker services. It will start the services in Docker containers and then run the tests. The services will be closed after the tests are run. The main function is invoked once per test package.

var (
	pgCfg = docker.PostgresServiceConfig{Repository: "postgres", Tag: "15", Database: "test"}
	pgSvc = docker.NewPostgresService(pgCfg)
)

func TestPostgresService(t *testing.T) {
	db, err := pgSvc.Open()
	require.NoError(t, err)
}

func TestMain(m *testing.M) {
	docker.MainWithServices(m, pgSvc)
}

Types

type PostgresService

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

PostgresService is a helper struct for running tests with a Postgres database. It will start a Postgres database in a Docker container and provide the connection parameters for the database.

func NewPostgresService

func NewPostgresService(c PostgresServiceConfig) *PostgresService

NewPostgresService creates a new PostgresService.

func (*PostgresService) Close

func (s *PostgresService) Close() error

Close closes the PostgresService and removes the Docker container.

func (*PostgresService) DSN

func (s *PostgresService) DSN() string

DSN returns the connection string for the Postgres database.

func (*PostgresService) Init

func (s *PostgresService) Init(p *dockertest.Pool) error

func (*PostgresService) Open

func (s *PostgresService) Open() (*sql.DB, error)

Open opens an sql.DB to the Postgres database. The caller must provide the driver and close the database when done.

type PostgresServiceConfig

type PostgresServiceConfig struct {
	// Docker repository and tag to use for the Postgres image.
	Repository, Tag string
	// Database name to create.
	Database string
	// Driver string
	Driver string
}

type RedisService

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

RedisService is a helper struct for running tests with a Redis database.

func NewRedisService

func NewRedisService(c RedisServiceConfig) *RedisService

NewRedisService creates a RedisService.

func (*RedisService) Addr

func (s *RedisService) Addr() string

Addr returns the address of the service, pass this to go-redis client.

func (*RedisService) Close

func (s *RedisService) Close() error

Close implements Service.Close.

func (*RedisService) Init

func (s *RedisService) Init(p *dockertest.Pool) error

Init implements Service.Init.

type RedisServiceConfig

type RedisServiceConfig struct {
	// Docker repository and tag to use for the Redis image.
	Repository, Tag string
}

type Service

type Service interface {
	// Init starts the service in a Docker container.
	Init(*dockertest.Pool) error
	// Close stops the service.
	Close() error
}

Service is a service that can be started in a Docker container.

Jump to

Keyboard shortcuts

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