dockerinitiator

package module
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2021 License: MIT Imports: 17 Imported by: 0

README

go-docker-initiator

Build Status codecov Go Report Card

Utility for starting docker containers from Go code. Useful for testing.

Image Support

This library currently supports the following services out of the box:

Installation

Install the package with:

go get github.com/Storytel/go-docker-initiator

Once installed import it to your code:

import dockerinitiator github.com/Storytel/go-docker-initiator

Examples

This package is especially useful for testing. With go-docker-initiator the configuration for your external integrations lives in the code and not in 3rd party configuration files.

Below is a typical and simple example using go-docker-initiator in a test.

package example_test

import (
	"log"
	"testing"

	dockerinitiator "github.com/Storytel/go-docker-initiator"
	mysqlinitiator "github.com/Storytel/go-docker-initiator/mysql"
	"github.com/stretchr/testify/assert"
)

// WithMySQL will clear obsolete containers an spin up a mysql container for use
func WithMySQL() *mysqlinitiator.MysqlInstance {
	if err := dockerinitiator.ClearObsolete(); err != nil {
		log.Panic(err)
	}

	instance, err := mysqlinitiator.Mysql(mysqlinitiator.MysqlConfig{
		Password: "",
		DbName:   "testdb",
	})
	if err != nil {
		log.Panic(err)
	}

	// Set the needed environment variables
	// MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE
	if err = instance.Setenv(); err != nil {
		log.Panic(err)
	}

	return instance
}

func TestDatabaseIntegration(t *testing.T) {
	mysqlInstance := WithMySQL()
	defer mysqlInstance.Stop()

	// Establish a database connection to the exposed environment variables
	db, err := InitAndCreateDatabase()
	assert.NoError(t, err)
	defer db.Close()

	// Run any DB seeds here

	// Setup your service and inject the database
	exampleService := ExampleService{
		Db: db,
	}

	// Test your integration
	_, err = exmapleService.Create()
	assert.NoError(t, err)
}

Notes

A single image can be easily shared between tests using techniques such as TableDrivenTests.

As is often the case, if you have tests set to automatically run on each file-save you might do best to tag the tests running with go-docker-initiator to avoid running them on-save.

Tag a file like so:

// +build <TAG>

Since the tests with the tag wont run automatically you have to manually invoke it with:

go test -tags=<TAG> ./...

Storytel Go

https://github.com/Storytel/go-mysql-seed - Simple MySQL seeding package

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ObsoleteAfter = 10 * time.Minute

Functions

func ClearObsolete

func ClearObsolete() error

ClearObsolete will delete all obsolete containers, created by this program

Types

type ContainerConfig

type ContainerConfig struct {
	Image         string
	Cmd           []string
	Env           []string
	ContainerPort string
	Tmpfs         map[string]string
}

ContainerConfig holds a subset of all of the configuration options available for the docker instance

type HTTPProbe

type HTTPProbe struct {
}

HTTPProbe implements the IProbe interface for HTTP connections

func (HTTPProbe) DoProbe

func (i HTTPProbe) DoProbe(instance *Instance) error

DoProbe will probe using HTTP

type Instance

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

Instance contains the setup for the docker instance

func CreateContainer

func CreateContainer(config ContainerConfig, prober Probe) (*Instance, error)

CreateContainer will create a new container with the specified Probe to check if it is up

func CreateContainerWithPlatform added in v1.3.1

func CreateContainerWithPlatform(config ContainerConfig, platform *v1.Platform, prober Probe) (*Instance, error)

CreateContainerWithPlatform applies the config and creates the container config specifices configuration for the container platform if you want to specificy a certain platform. nil if you want to use default.

func (*Instance) Container

func (i *Instance) Container() types.ContainerJSON

func (*Instance) GetHost

func (i *Instance) GetHost() string

GetHost will fetch the host of the instance container

func (*Instance) Probe

func (i *Instance) Probe(timeout time.Duration) error

Probe will periodically, during a timeout, check for an active connection in the instance container

func (*Instance) Stop

func (i *Instance) Stop() error

Stop will remove the instance container

type Probe

type Probe interface {
	DoProbe(instance *Instance) error
}

Probe provides an interface for the probing mechanism

type TCPProbe

type TCPProbe struct {
}

TCPProbe implements the IProbe interface for TPC connections

func (TCPProbe) DoProbe

func (i TCPProbe) DoProbe(instance *Instance) error

DoProbe will probe using TCP

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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