Nifty Go Pkg: ory/dockertest

A package I often use and find extremely useful for intergation testing is dockertest

Use Case

You want to setup and tear down a real database for each integration test run.

Example

An example from the dockertest repository

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
var db *redis.Client
var err error
pool, err = dockertest.NewPool("")
if err != nil {
    log.Fatalf("Could not connect to docker: %s", err)
}

resource, err := pool.Run("redis", "3.2", nil)
if err != nil {
    log.Fatalf("Could not start resource: %s", err)
}

if err = pool.Retry(func() error {
    db = redis.NewClient(&redis.Options{
        Addr: fmt.Sprintf("localhost:%s", resource.GetPort("6379/tcp")),
    })

    return db.Ping().Err()
}); err != nil {
    log.Fatalf("Could not connect to docker: %s", err)
}

// When you're done, kill and remove the container
err = pool.Purge(resource)