-
Notifications
You must be signed in to change notification settings - Fork 1
/
gocker.go
179 lines (158 loc) · 4.66 KB
/
gocker.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
Copyright 2014 The Camlistore Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Package dockertest contains helper functions for setting up and tearing down docker containers to aid in testing.
*/
package gocker
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"log"
"os/exec"
"strings"
"testing"
"time"
"camlistore.org/pkg/netutil"
"strconv"
)
/// runLongTest checks all the conditions for running a docker container
// based on image.
func runLongTest(t *testing.T, image string) {
if testing.Short() {
t.Skip("skipping in short mode")
}
if !haveDocker() {
t.Error("'docker' command not found")
}
if ok, err := haveImage(image); !ok || err != nil {
if err != nil {
t.Errorf("Error running docker to check for %s: %v", image, err)
}
log.Printf("Pulling docker image %s ...", image)
if err := Pull(image); err != nil {
t.Errorf("Error pulling %s: %v", image, err)
}
}
}
// haveDocker returns whether the "docker" command was found.
func haveDocker() bool {
_, err := exec.LookPath("docker")
return err == nil
}
func haveImage(name string) (ok bool, err error) {
out, err := exec.Command("docker", "images", "--no-trunc").Output()
if err != nil {
return
}
return bytes.Contains(out, []byte(name)), nil
}
func Run(args ...string) (containerID string, err error) {
cmd := exec.Command("docker", append([]string{"run"}, args...)...)
var stdout, stderr bytes.Buffer
cmd.Stdout, cmd.Stderr = &stdout, &stderr
if err = cmd.Run(); err != nil {
err = fmt.Errorf("%v%v", stderr.String(), err)
return
}
containerID = strings.TrimSpace(stdout.String())
if containerID == "" {
return "", errors.New("unexpected empty output from `docker run`")
}
return
}
func KillContainer(container string) error {
return exec.Command("docker", "kill", container).Run()
}
// Pull retrieves the docker image with 'docker pull'.
func Pull(image string) error {
out, err := exec.Command("docker", "pull", image).CombinedOutput()
if err != nil {
err = fmt.Errorf("%v: %s", err, out)
}
return err
}
// IP returns the IP address of the container.
func IP(containerID string) (string, error) {
out, err := exec.Command("docker", "inspect", containerID).Output()
if err != nil {
return "", err
}
type networkSettings struct {
IPAddress string
}
type container struct {
NetworkSettings networkSettings
}
var c []container
if err := json.NewDecoder(bytes.NewReader(out)).Decode(&c); err != nil {
return "", err
}
if len(c) == 0 {
return "", errors.New("no output from docker inspect")
}
if ip := c[0].NetworkSettings.IPAddress; ip != "" {
return ip, nil
}
return "", errors.New("could not find an IP. Not running?")
}
type ContainerID string
func (c ContainerID) IP() (string, error) {
return IP(string(c))
}
func (c ContainerID) Kill() error {
return KillContainer(string(c))
}
// Remove runs "docker rm" on the container
func (c ContainerID) Remove() error {
return exec.Command("docker", "rm", string(c)).Run()
}
// KillRemove calls Kill on the container, and then Remove if there was
// no error. It logs any error to t.
func (c ContainerID) KillRemove(t *testing.T) {
if err := c.Kill(); err != nil {
t.Log(err)
return
}
if err := c.Remove(); err != nil {
t.Log(err)
}
}
// lookup retrieves the ip address of the container, and tries to reach
// before timeout the tcp address at this ip and given port.
func (c ContainerID) lookup(port int, timeout time.Duration) (ip string, err error) {
ip, err = c.IP()
if err != nil {
err = fmt.Errorf("error getting IP: %v", err)
return
}
addr := fmt.Sprintf("%s:%d", ip, port)
err = netutil.AwaitReachable(addr, timeout)
return
}
// setupContainer sets up a container, using the start function to run the given image.
// It also looks up the IP address of the container, and tests this address with the given
// port and timeout. It returns the container ID and its IP address, or makes the test
// fail on error.
func SetupContainer(t *testing.T, image string, port int, timeout time.Duration,
start func() (string, error)) (c ContainerID, ip string) {
runLongTest(t, image)
containerID, err := start()
if err != nil {
t.Fatalf("docker run: %v", err)
}
c = ContainerID(containerID)
ip = "0.0.0.0:" + strconv.Itoa(port)
return
}