-
Notifications
You must be signed in to change notification settings - Fork 1
/
examples_test.go
117 lines (96 loc) · 2.52 KB
/
examples_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package dockertest_test
import (
"context"
"fmt"
"log"
"net"
"time"
"github.com/opalmer/dockertest"
. "gopkg.in/check.v1"
)
type ExamplesTest struct{}
var _ = Suite(&ExamplesTest{})
func (s *ExamplesTest) TestExampleNewClient(c *C) {
ExampleNewClient()
}
func (s *ExamplesTest) TestExampleDockerClient_Service(c *C) {
ExampleDockerClient_Service()
}
//
// NOTE:
// If you modify the example functions below, please make sure to
// update README.md too.
//
func ExampleNewClient() {
client, err := dockertest.NewClient()
if err != nil {
log.Fatal(err)
}
// Construct information about the container to start.
input := dockertest.NewClientInput("nginx:mainline-alpine")
input.Ports.Add(&dockertest.Port{
Private: 80,
Public: dockertest.RandomPort,
Protocol: dockertest.ProtocolTCP,
})
// Start the container
container, err := client.RunContainer(context.Background(), input)
if err != nil {
log.Fatal(err)
}
// Extract information about the started container.
port, err := container.Port(80)
if err != nil {
log.Fatal(err)
}
fmt.Println(port.Public, port.Address)
if err := client.RemoveContainer(context.Background(), container.ID()); err != nil {
log.Fatal(err)
}
}
func ExampleDockerClient_Service() {
client, err := dockertest.NewClient()
if err != nil {
log.Fatal(err)
}
// Construct information about the container to start.
input := dockertest.NewClientInput("nginx:mainline-alpine")
input.Ports.Add(&dockertest.Port{
Private: 80,
Public: dockertest.RandomPort,
Protocol: dockertest.ProtocolTCP,
})
// Construct the service and tell it how to handle waiting
// for the container to start.
service := client.Service(input)
service.Ping = func(input *dockertest.PingInput) error {
port, err := input.Container.Port(80)
if err != nil {
return err // Will cause Run() to call Terminate()
}
for {
_, err := net.Dial(string(port.Protocol), fmt.Sprintf("%s:%d", port.Address, port.Public))
if err != nil {
time.Sleep(time.Millisecond * 100)
continue
}
break
}
return nil
}
// Starts the container, runs Ping() and waits for it to return. If Ping()
// fails the container will be terminated and Run() will return an error.
if err := service.Run(); err != nil {
log.Fatal(err)
}
// Container has started, get information information
// about the exposed port.
port, err := service.Container.Port(80)
if err != nil {
log.Fatal(err)
}
fmt.Println(port.Public, port.Address)
if err := service.Terminate(); err != nil {
log.Fatal(err)
}
}