package dktest
import "github.com/dhui/dktest"
Package dktest provides an easy way to write integration tests using Docker
dktest is short for dockertest
Code:play
Code:play
Example (Nginx)¶
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 {
ip, port, err := c.FirstPort()
if err != nil {
return false
}
u := url.URL{Scheme: "http", Host: ip + ":" + 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"
readyFunc := func(c dktest.ContainerInfo) bool {
ip, port, err := c.FirstPort()
if err != nil {
return false
}
connStr := fmt.Sprintf("host=%s port=%s user=postgres dbname=postgres sslmode=disable", ip, port)
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) {
ip, port, err := c.FirstPort()
if err != nil {
t.Fatal(err)
}
connStr := fmt.Sprintf("host=%s port=%s user=postgres dbname=postgres sslmode=disable", ip, port)
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 ¶
- Variables
- func Run(t *testing.T, imgName string, opts Options, testFunc func(*testing.T, ContainerInfo))
- type ContainerInfo
- func (c ContainerInfo) FirstPort() (hostIP string, hostPort string, err error)
- func (c ContainerInfo) FirstUDPPort() (hostIP string, hostPort string, err error)
- func (c ContainerInfo) Port(containerPort uint16) (hostIP string, hostPort string, err error)
- func (c ContainerInfo) String() string
- func (c ContainerInfo) UDPPort(containerPort uint16) (hostIP string, hostPort string, err error)
- type Options
Examples ¶
Variables ¶
Functions ¶
func Run ¶
Run runs the given test function once the specified Docker image is running in a container
Types ¶
type ContainerInfo ¶
ContainerInfo holds information about a running Docker container
func (ContainerInfo) FirstPort ¶
func (c ContainerInfo) FirstPort() (hostIP string, hostPort string, err error)
FirstPort gets the first published/bound/mapped TCP port. It is always safer to use Port(). This provided as a convenience method and should only be used with Docker images that only expose a single port. If the Docker image exposes multiple ports, then the "first" port will not always be the same.
func (ContainerInfo) FirstUDPPort ¶
func (c ContainerInfo) FirstUDPPort() (hostIP string, hostPort string, err error)
FirstUDPPort gets the first published/bound/mapped UDP port. It is always safer to use UDPPort(). This provided as a convenience method and should only be used with Docker images that only expose a single port. If the Docker image exposes multiple ports, then the "first" port will not always be the same.
func (ContainerInfo) Port ¶
func (c ContainerInfo) Port(containerPort uint16) (hostIP string, hostPort string, err error)
Port gets the specified published/bound/mapped TCP port
func (ContainerInfo) String ¶
func (c ContainerInfo) String() string
String gets the string representation for the ContainerInfo. This is intended for debugging purposes.
func (ContainerInfo) UDPPort ¶
func (c ContainerInfo) UDPPort(containerPort uint16) (hostIP string, hostPort string, err error)
UDPPort gets the specified published/bound/mapped UDP port
type Options ¶
type Options struct { Timeout time.Duration ReadyFunc func(ContainerInfo) bool Env map[string]string Entrypoint []string Cmd []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
Source Files ¶
container_info.go dktest.go doc.go errors.go logger.go math.go options.go rand.go
Directories ¶
Path | Synopsis |
---|---|
mockdockerclient |
- Version
- v0.2.0
- Published
- Jan 8, 2019
- Platform
- js/wasm
- Imports
- 14 packages
- Last checked
- now –
Tools for package owners.