Skip to content

This is an addon to be used with Testcontainers package and with GoDog

License

Notifications You must be signed in to change notification settings

jfelipearaujo/testcontainers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tests version go.dev reference license

testcontainers

This is an addon to be used with Testcontainers package and with GoDog

Installation

go get github.com/jfelipearaujo/testcontainers@latest

How to use

To use this addon, you need to import the packages in your project:

import "github.com/jfelipearaujo/testcontainers/pkg/container"
import "github.com/jfelipearaujo/testcontainers/pkg/network"
import "github.com/jfelipearaujo/testcontainers/pkg/state"
import "github.com/jfelipearaujo/testcontainers/pkg/testsuite"

Examples

Example Description
Example 01 Simple BDD test.
Example 02 Using a Postgres container.
Example 03 Using aLocalStack container.
Example 04 Using a MongoDB container.
Example 05 Custom API running on a container via Dockerfile.
Example 06 Two containers interacting with each other using a Network.

Full documentation

container

import "github.com/jfelipearaujo/testcontainers/pkg/container"

Index

func DestroyGroup(ctx context.Context, group GroupContainer) (context.Context, error)

DestroyGroup destroys the given group of containers and the network (if exists)

func GetMappedPort(ctx context.Context, container testcontainers.Container, exposedPort nat.Port) (nat.Port, error)

func NewGroup() map[string]GroupContainer

NewGroup creates a new map of test contexts to store a group of containers

Container is a type that represents a container that will be created

type Container struct {
    ContainerRequest  testcontainers.ContainerRequest
    ForceWaitDuration *time.Duration
}

func NewContainerDefinition(opts ...ContainerOption) *Container

NewContainerDefinition creates a new container definition that will be used to create a container

func (*Container) BuildContainer

func (c *Container) BuildContainer(ctx context.Context) (testcontainers.Container, error)

BuildContainer creates a new container following the container definition

ContainerOption is a type that represents a container option

type ContainerOption func(*Container)

func WithDockerfile(fromDockerFile testcontainers.FromDockerfile) ContainerOption

WithDockerfile is a ContainerOption that sets the Dockerfile data of the container

Default: nil

func WithEnvVars(envVars map[string]string) ContainerOption

WithEnvVars is a ContainerOption that sets the environment variables of the container

Default:

POSTGRES_DB: postgres_db
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres

func WithExecutableFiles(basePath string, files ...string) ContainerOption

WithExecutableFiles is a ContainerOption that sets the executable files of the container that will be copied to the container

Default: nil

func WithExposedPorts(ports ...string) ContainerOption

WithExposedPorts is a ContainerOption that sets the exposed ports of the container

Default: 5432

func WithFiles(basePath string, files ...string) ContainerOption

WithFiles is a ContainerOption that sets the startup files of the container that will be copied to the container

Default: nil

func WithForceWaitDuration(duration time.Duration) ContainerOption

WithForceWaitDuration is a ContainerOption that sets the duration to wait for the container to be ready

Default: nil

func WithImage(image string) ContainerOption

WithImage is a ContainerOption that sets the image of the container

Default: postgres:latest

func WithNetwork(alias string, network *testcontainers.DockerNetwork) ContainerOption

WithNetwork is a ContainerOption that sets the network of the container

Default: nil

func WithWaitingForLog(log string, startupTimeout time.Duration) ContainerOption

WithWaitingForLog is a ContainerOption that sets the log to wait for

Default: ready for start up

func WithWaitingForPort(port string, startupTimeout time.Duration) ContainerOption

WithWaitingForPort is a ContainerOption that sets the port to wait for

Example: "8080" for 30 seconds

GroupContainer is a type that represents a test context

type GroupContainer struct {
    Network    *testcontainers.DockerNetwork
    Containers []testcontainers.Container
}

func BuildGroupContainer(opts ...TestContainersOption) GroupContainer

BuildGroupContainer creates a new test context with the given options

TestContainersOption is a type that represents a test context option

type TestContainersOption func(*GroupContainer)

func WithDockerContainer(container ...testcontainers.Container) TestContainersOption

WithDockerContainer is a TestContainersOption that sets the containers of the test context

func WithDockerNetwork(network *testcontainers.DockerNetwork) TestContainersOption

WithDockerNetwork is a TestContainersOption that sets the network of the test context

network

import "github.com/jfelipearaujo/testcontainers/pkg/network"

Index

type Network

Network is a type that represents a network

type Network struct {
    Alias string
    Type  NetworkType
    // contains filtered or unexported fields
}

func NewNetwork(opts ...NetworkOption) *Network

NewNetwork creates a new Network

func (*Network) Build

func (ntw *Network) Build(ctx context.Context) (*testcontainers.DockerNetwork, error)

Build creates a new DockerNetwork

NetworkOption is a type that represents a network option

type NetworkOption func(*Network)

func WithAlias(alias string) NetworkOption

WithAlias is a NetworkOption that sets the alias of the network

Default: network

func WithType(typeName NetworkType) NetworkOption

WithType is a NetworkOption that sets the type of the network

Default: bridge

NetworkType is a type that represents the type of network

type NetworkType string

var (
    // NetworkTypeBridge is a network type that represents a bridge network
    NetworkTypeBridge NetworkType = "bridge"
)

state

import "github.com/jfelipearaujo/testcontainers/pkg/state"

Index

CtxKeyType is a type that can be used as a key for a context.Context

type CtxKeyType string

type State

State is a type that can be used to store data in a context.Context It is useful for storing data that needs to be shared between tests

type State[T any] struct {
    CtxKey CtxKeyType
}

func NewState[T any](opts ...StateOption[T]) *State[T]

NewState creates a new State

Example:

type test struct {
	Name string
}

state := state.NewState[test]()

func (*State[T]) Enrich

func (state *State[T]) Enrich(ctx context.Context, data *T) context.Context

Enrich enriches the context with the data

Example:

type test struct {
	Name string
}

state := state.NewState[test]()
ctx := state.Enrich(ctx, &test{
	Name: "John",
})

func (*State[T]) Retrieve

func (state *State[T]) Retrieve(ctx context.Context) *T

Retrieve retrieves the data from the context

Example:

type test struct {
	Name string
}

state := state.NewState[test]()
ctx := state.Enrich(ctx, &test{
	Name: "John",
})
data := state.Retrieve(ctx)

fmt.Println(data.Name) // John

StateOption is a type that can be used to configure a State

type StateOption[T any] func(*State[T])

func WithCtxKey[T any](ctxKey CtxKeyType) StateOption[T]

WithCtxKey is a StateOption that sets the key for the context.Context

Default: default

Example:

type test struct {
	Name string
}

state := state.NewState[test](
	state.WithCtxKey("test"),
)

testsuite

import "github.com/jfelipearaujo/testcontainers/pkg/testsuite"

Index

func NewTestSuite(t *testing.T, scenarioInitializer func(ctx *godog.ScenarioContext), opts ...TestSuiteOption)

NewTestSuite creates a new test suite

TestSuiteOption is a type that represents a test suite option

type TestSuiteOption func(*godog.Options)

func WithConcurrency(concurrency int) TestSuiteOption

WithConcurrency is a TestSuiteOption that sets the concurrency of the test suite. If the concurrency is set to 0, the test suite will NOT run in parallel

Default: 4

func WithPaths(paths ...string) TestSuiteOption

WithPaths is a TestSuiteOption that sets the paths of the test suite

Default: "features"

localstack

import "github.com/jfelipearaujo/testcontainers/pkg/container/localstack"

Index

Constants

const (
    BasePath      string = "/etc/localstack/init/ready.d"
    ExposedPort   string = "4566"
    Debug         string = "false"
    DockerHost    string = "unix:///var/run/docker.sock"
    DefaultRegion string = "us-east-1"
)

func BuildEndpoint(ctx context.Context, container testcontainers.Container, opts ...LocalStackOption) (string, error)

BuildEndpoint returns the endpoint of the LocalStack container

Example: "http://localhost:4566"

func WithLocalStackContainer() container.ContainerOption

Return a new container definition for a LocalStack container with default options:

DockerImage: "localstack/localstack:3.4"
ExposedPort: "4566"

Environment variables:
	DEBUG: false
	DOCKER_HOST: "unix:///var/run/docker.sock"
	DEFAULT_REGION: "us-east-1"

BasePath: "/etc/localstack/init/ready.d"
WaitingForLog: "Initialization complete!"
StartupTimeout: "30 seconds"

LocalStackOption is a type that represents a LocalStack option

type LocalStackOption func(*Options)

func WithDebug(debug string) LocalStackOption

WithDebug is a LocalStackOption that sets the debug of the LocalStack container

Default: false

func WithDefaultRegion(defaultRegion string) LocalStackOption

WithDefaultRegion is a LocalStackOption that sets the default region of the LocalStack container

Default: "us-east-1"

func WithDockerHost(dockerHost string) LocalStackOption

WithDockerHost is a LocalStackOption that sets the Docker host of the LocalStack container

Default: "unix:///var/run/docker.sock"

func WithExposedPort(exposedPort string) LocalStackOption

WithExposedPort is a LocalStackOption that sets the exposed port of the LocalStack container

Default: "4566"

type Options

Options is a type that represents the options for a LocalStack container

Default options:
	ExposedPort: "4566"
	Debug: false
	DockerHost: "unix:///var/run/docker.sock"
	DefaultRegion: "us-east-1"
type Options struct {
    ExposedPort   string
    Debug         string
    DockerHost    string
    DefaultRegion string
}

mongodb

import "github.com/jfelipearaujo/testcontainers/pkg/container/mongodb"

Index

Constants

const (
    ExposedPort string = "27017"
    User        string = "mongo"
    Pass        string = "mongo"
)

func BuildExternalConnectionString(ctx context.Context, container testcontainers.Container, opts ...MongoOption) (string, error)

Return a MongoDB connection string for the given container with default options when the container is NOT in a network

Example: "mongodb://mongo:mongo@localhost:27017/"

func BuildInternalConnectionString(ctx context.Context, container testcontainers.Container, opts ...MongoOption) (string, error)

Return a MongoDB connection string for the given container with default options when the container is in a network

Example: "mongodb://mongo:mongo@network_alias:27017/"

func WithMongoContainer() container.ContainerOption

Return a new container definition for a MongoDB container with default options

DockerImage: "mongo:7"
Exposed ports: "27017"
Environment variables:
	MONGO_INITDB_ROOT_USERNAME: "mongo"
	MONGO_INITDB_ROOT_PASSWORD: "mongo"

WaitingForLog: "Waiting for connections"
StartupTimeout: "30 seconds"

MongoOption is a type that represents a MongoDB option

type MongoOption func(*Options)

func WithExposedPort(exposedPort string) MongoOption

WithExposedPort is a MongoOption that sets the exposed port of the MongoDB container

Default: "27017"

func WithNetwork(network *network.Network) MongoOption

WithNetwork is a MongoOption that sets the network alias of the MongoDB container

Default: nil

func WithPass(pass string) MongoOption

WithPass is a MongoOption that sets the password of the MongoDB container

Default: "test"

func WithUser(user string) MongoOption

WithUser is a MongoOption that sets the user of the MongoDB container

Default: "test"

type Options

Options is a type that represents the options for a MongoDB container

Default options:
	ExposedPort: "27017"
	User: "mongo"
	Pass: "mongo"
type Options struct {
    ExposedPort  string
    User         string
    Pass         string
    NetworkAlias *string
}

postgres

import "github.com/jfelipearaujo/testcontainers/pkg/container/postgres"

Index

Constants

const (
    BasePath    string = "/docker-entrypoint-initdb.d"
    ExposedPort string = "5432"
    Database    string = "postgres_db"
    User        string = "postgres"
    Pass        string = "postgres"
)

func BuildExternalConnectionString(ctx context.Context, container testcontainers.Container, opts ...PostgresOption) (string, error)

Return a PostgreSQL connection string for the given container with default options when the container is NOT in a network

Example: "postgres://postgres:postgres@localhost:5432/postgres_db?sslmode=disable"

func BuildInternalConnectionString(ctx context.Context, container testcontainers.Container, opts ...PostgresOption) (string, error)

Return a PostgreSQL connection string for the given container with default options when the container is in a network

Example: "postgres://postgres:postgres@network_alias:5432/postgres_db?sslmode=disable"

func WithPostgresContainer() container.ContainerOption

Return a new container definition for a PostgreSQL container with default options

DockerImage: "postgres:16"
Exposed ports: "5432"
Environment variables:
	POSTGRES_DB: "postgres_db"
	POSTGRES_USER: "postgres"
	POSTGRES_PASSWORD: "postgres"

BasePath: "/docker-entrypoint-initdb.d"
WaitingForLog: "database system is ready to accept connections"
StartupTimeout: "30 seconds"

type Options

Options is a type that represents the options for a PostgreSQL container

Default options:
	ExposedPort: "5432"
	Database: "postgres_db"
	User: "postgres"
	Pass: "postgres"

Default network alias: nil
type Options struct {
    ExposedPort  string
    Database     string
    User         string
    Pass         string
    NetworkAlias *string
}

PostgresOption is a type that represents a PostgreSQL option

type PostgresOption func(*Options)

func WithDatabase(database string) PostgresOption

WithDatabase is a PostgresOption that sets the database of the PostgreSQL container

Default: "postgres_db"

func WithExposedPort(exposedPort string) PostgresOption

WithExposedPort is a PostgresOption that sets the exposed port of the PostgreSQL container

Default: "5432"

func WithNetwork(network *network.Network) PostgresOption

WithNetwork is a PostgresOption that sets the network alias of the PostgreSQL container

Default: nil

func WithPass(pass string) PostgresOption

WithPass is a PostgresOption that sets the password of the PostgreSQL container

Default: "postgres"

func WithUser(user string) PostgresOption

WithUser is a PostgresOption that sets the user of the PostgreSQL container

Default: "postgres"

Generated by gomarkdoc