dktest

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2019 License: Apache-2.0 Imports: 13 Imported by: 51

README

dktest

Build Status Code Coverage GoDoc Go Report Card GitHub Release Supported Go versions

dktest makes it stupidly easy to write integration tests in Go using Docker. Pulling images, starting containers, and cleaning up (even if your tests panic) is handled for you automatically!

API

Run() is the workhorse

type ContainerInfo struct {
    ID        string
    Name      string
    ImageName string
    IP        string
    Port      string
}

type Options struct {
    Timeout   time.Duration
    ReadyFunc func(ContainerInfo) bool
    Env       map[string]string
    // If you prefer to specify your port bindings as a string, use nat.ParsePortSpecs()
    PortBindings nat.PortMap
    PortRequired bool
}

func Run(t *testing.T, imgName string, opts Options, testFunc func(*testing.T, ContainerInfo))

Example Usage

import "testing"

import (
    "github.com/dhui/dktest"
    _ "github.com/lib/pq"
)

func pgReady(c dktest.ContainerInfo) bool {
    connStr := fmt.Sprintf("host=%s port=%s user=postgres password=%s", c.IP, c.Port, password)
    db, err := sql.Open("postgres", connStr)
    if err != nil {
        return false
    }
    defer db.Close()
    return db.Ping() == nil
}

func Test(t *testing.T) {
    dktest.Run(t, "postgres:alpine", dktest.Options{PortRequired: true, ReadyFunc: pgReady},
        func(t *testing.T, c dktest.ContainerInfo) {
        connStr := fmt.Sprintf("host=%s port=%s user=postgres password=%s", c.IP, c.Port, password)
        db, err := sql.Open("postgres", connStr)
        if err != nil {
            t.Fatal(err)
        }
        defer db.Close()
        if err := db.Ping(); err != nil {
            t.Fatal(err)
        }
        // Test using db
    })
}

For more examples, see the docs.

Cleaning up dangling containers

In the unlikely scenario where dktest leaves dangling containers, you can find and removing them by using the dktest label:

# find dangling containers
$ docker ps -a -f label=dktest
# remove dangling containers
$ docker container prune -f label=dktest

Roadmap

  • Support multiple ports in ContainerInfo
  • Use non-default network
  • Add more Options
    • Volume mounts
    • Network config

Comparison to dockertest

Why dktest is better
Why dockertest is better
  • Has been around longer and API is more stable
  • More options for configuring Docker containers
  • Has more Github stars and contributors

Documentation

Overview

Example
package main

import (
	"net/http"
	"net/url"
	"testing"

	"github.com/dhui/dktest"
	_ "github.com/lib/pq"
)

func main() {
	dockerImageName := "nginx:alpine"
	readyFunc := func(c dktest.ContainerInfo) bool {
		u := url.URL{Scheme: "http", Host: c.IP + ":" + c.Port}
		if resp, err := http.Get(u.String()); err != nil {
			return false
		} else if resp.StatusCode != 200 {
			return false
		}
		return true
	}

	// dktest.Run() should be used within a test
	dktest.Run(&testing.T{}, dockerImageName, dktest.Options{PortRequired: true, ReadyFunc: readyFunc},
		func(t *testing.T, c dktest.ContainerInfo) {
			// test code here
		})
}
Example (Postgres)
package main

import (
	"database/sql"
	"fmt"
	"testing"

	"github.com/dhui/dktest"
	_ "github.com/lib/pq"
)

func main() {
	dockerImageName := "postgres:alpine"
	password := "insecurepassword"
	readyFunc := func(c dktest.ContainerInfo) bool {
		connStr := fmt.Sprintf("host=%s port=%s user=postgres password=%s", c.IP, c.Port, password)
		db, err := sql.Open("postgres", connStr)
		if err != nil {
			return false
		}
		defer db.Close() // nolint:errcheck
		return db.Ping() == nil
	}

	// dktest.Run() should be used within a test
	dktest.Run(&testing.T{}, dockerImageName, dktest.Options{PortRequired: true, ReadyFunc: readyFunc},
		func(t *testing.T, c dktest.ContainerInfo) {
			connStr := fmt.Sprintf("host=%s port=%s user=postgres password=%s", c.IP, c.Port, password)
			db, err := sql.Open("postgres", connStr)
			if err != nil {
				t.Fatal(err)
			}
			defer db.Close() // nolint:errcheck
			if err := db.Ping(); err != nil {
				t.Fatal(err)
			}
			// Test using db
		})
}

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// DefaultTimeout is the default timeout to use
	DefaultTimeout = time.Minute
)

Functions

func Run

func Run(t *testing.T, imgName string, opts Options, testFunc func(*testing.T, ContainerInfo))

Run runs the given test function once the specified Docker image is running in a container

Types

type ContainerInfo

type ContainerInfo struct {
	ID        string
	Name      string
	ImageName string
	IP        string
	Port      string
}

ContainerInfo holds information about a running Docker container

func (ContainerInfo) String

func (c ContainerInfo) String() string

type Options

type Options struct {
	Timeout   time.Duration
	ReadyFunc func(ContainerInfo) bool
	Env       map[string]string
	// If you prefer to specify your port bindings as a string, use nat.ParsePortSpecs()
	PortBindings nat.PortMap
	PortRequired bool
}

Options contains the configurable options for running tests in the docker image

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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