-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
66 lines (54 loc) · 1.91 KB
/
client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package docker
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestCanStartContainer(t *testing.T) {
RequireDocker(t)
container, err := StartContainer(NewContainerRequest{
Image: "redis",
Ports: []int{6379},
}, t.Name())
assert.NoError(t, err)
defer container.Close()
err = container.WaitForPortToOpen(container.PortMapping(6379), 10*time.Second)
assert.NoError(t, err)
}
func ExampleStartContainer() {
// In a "real" test, you'd first want to call RequireDocker(t), to ensure
// that the test is running on a Docker-able machine:
//
// RequireDocker(t)
// Specify which container you want to start and which ports/env var you need to use.
// Note that the image name can, and SHOULD contain the version tag (4.0.11 in this case),
// so that a known image is used, and that tests are reproducible over time.
//
// This request specifies that we want to map the standard redis port (6379)
// to a random open port on the host machine.
cr := NewContainerRequest{
Image: "redis:4.0.11",
EnvVars: map[string]string{
"FOO": "BAR",
},
Ports: []int{6379},
}
// start up the container, using a prefix that helps us distinguish this
// container from the other that might be running on the machine.
container, err := StartContainer(cr, "example-redis")
if err != nil {
panic(fmt.Errorf("failed to start the prerequisite redis Docker container: %s", err.Error()))
}
// Wait for the message that redis prints when the server is up.
err = container.WaitForLogLine("Ready to accept connections", 10*time.Second)
if err != nil {
panic(err)
}
// Don't forget to close the container when we're done with the test!
defer container.Close()
// Find out which local port the container's port 6379 got mapped to
localPort := container.PortMapping(6379)
fmt.Printf("so, apparently redis is up on local port: %d\n", localPort)
// connect to the above port, and do stuff with redis!
}