package dktest
import "github.com/dhui/dktest"
Example¶
Code:play
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)¶
Code:play
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 ¶
- Variables
- func Run(t *testing.T, imgName string, opts Options, testFunc func(*testing.T, ContainerInfo))
- type ContainerInfo
- 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) 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
Source Files ¶
dktest.go logger.go options.go rand.go
Directories ¶
Path | Synopsis |
---|---|
mockdockerclient |
- Version
- v0.1.0
- Published
- Jan 4, 2019
- Platform
- windows/amd64
- Imports
- 13 packages
- Last checked
- now –
Tools for package owners.