testcontainers

package module
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: May 8, 2020 License: MIT Imports: 9 Imported by: 0

README

testcontainers

Build Status GitHub GoDoc Test Coverage Release

A collection of pre-configured testcontainers for your golang integration tests.

Available containers (feel free to contribute):

Usage
import (
	"context"
	"testing"
	"time"

	tc "github.com/romnnn/testcontainers"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

// TestDatabaseIntegration tests datababase logic
func TestDatabaseIntegration(t *testing.T) {
	// Start the container
	mongoC, Config, err := tc.StartMongoContainer(tc.ContainerOptions{})
	if err != nil {
		t.Fatalf("Failed to start mongoDB container: %v", err)
	}
	defer mongoC.Terminate(context.Background())

	// Connect to the container
	client, err := mongo.NewClient(options.Client().ApplyURI(Config.ConnectionURI()))
	if err != nil {
		t.Fatalf("Failed to create mongo client: %v", err)
	}
	ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
	defer cancel()
	client.Connect(ctx)

	// TODO: Connect to the database using the URI and implement testDatabaseFunction!
	if err := testDatabaseFunction(client); err != nil {
		t.Fatalf("myDatabaseFunction failed with error: %v", err)
	}
}

For full examples of all available containers, see examples/.

Development
Prerequisites

Before you get started, make sure you have installed the following tools::

$ python3 -m pip install -U cookiecutter>=1.4.0
$ python3 -m pip install pre-commit bump2version invoke ruamel.yaml halo
$ go get -u golang.org/x/tools/cmd/goimports
$ go get -u golang.org/x/lint/golint
$ go get -u github.com/fzipp/gocyclo
$ go get -u github.com/mitchellh/gox  # if you want to test building on different architectures

Remember: To be able to excecute the tools downloaded with go get, make sure to include $GOPATH/bin in your $PATH. If echo $GOPATH does not give you a path make sure to run (export GOPATH="$HOME/go" to set it). In order for your changes to persist, do not forget to add these to your shells .bashrc.

With the tools in place, it is strongly advised to install the git commit hooks to make sure checks are passing in CI:

invoke install-hooks

You can check if all checks pass at any time:

invoke pre-commit

Note for Maintainers: After merging changes, tag your commits with a new version and push to GitHub to create a release:

bump2version (major | minor | patch)
git push --follow-tags
Note

This project is still in the alpha stage and should not be considered production ready.

Documentation

Index

Constants

View Source
const Version = "0.1.7"

Version is incremented using bump2version

Variables

View Source
var ClientMux sync.Mutex

ClientMux ...

Functions

func CreateNetwork added in v0.1.2

func CreateNetwork(request testcontainers.NetworkRequest, timeoutMin time.Duration) (net testcontainers.Network, err error)

CreateNetwork creates a docker container network

func EnableLogger added in v0.1.5

func EnableLogger(container testcontainers.Container, logger *LogCollector)

EnableLogger ...

func MergeRequest added in v0.1.5

func MergeRequest(c *testcontainers.ContainerRequest, override *testcontainers.ContainerRequest)

MergeRequest ...

func UniqueID added in v0.1.1

func UniqueID() string

UniqueID generates a unique uuid

Types

type ContainerConfig

type ContainerConfig struct {
	Log *LogCollector
}

ContainerConfig ...

type ContainerOptions

type ContainerOptions struct {
	testcontainers.ContainerRequest
	CollectLogs    bool
	StartupTimeout time.Duration
}

ContainerOptions ...

type LogCollector

type LogCollector struct {
	MessageChan chan string
}

LogCollector ...

func (*LogCollector) Accept

func (c *LogCollector) Accept(l testcontainers.Log)

Accept ...

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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