/
docker.go
97 lines (80 loc) 路 2.49 KB
/
docker.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
package test
import (
"bytes"
"encoding/json"
"fmt"
"net"
"os/exec"
"strings"
)
// Container tracks information about the docker container started for tests.
type Container struct {
ID string
Host string // IP:Port
}
// StartContainer starts the specified container for running tests.
func StartContainer(image string, port string, args ...string) (*Container, error) {
arg := []string{"run", "-P", "-d"}
arg = append(arg, args...)
arg = append(arg, image)
cmd := exec.Command("docker", arg...)
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("could not start container %s: %w", image, err)
}
id := out.String()[:12]
hostIP, hostPort, err := extractIPPort(id, port)
if err != nil {
return nil, fmt.Errorf("could not extract ip/port: %w", err)
}
c := Container{
ID: id,
Host: net.JoinHostPort(hostIP, hostPort),
}
return &c, nil
}
// StopContainer stops and removes the specified container.
func StopContainer(id string) error {
if err := exec.Command("docker", "stop", id).Run(); err != nil {
return fmt.Errorf("could not stop container: %w", err)
}
if err := exec.Command("docker", "rm", id, "-v").Run(); err != nil {
return fmt.Errorf("could not remove container: %w", err)
}
return nil
}
// DumpContainerLogs outputs logs from the running docker container.
func DumpContainerLogs(id string) []byte {
out, err := exec.Command("docker", "logs", id).CombinedOutput()
if err != nil {
return nil
}
return out
}
func extractIPPort(id string, port string) (hostIP string, hostPort string, err error) {
tmpl := fmt.Sprintf("[{{range $k,$v := (index .NetworkSettings.Ports \"%s/tcp\")}}{{json $v}}{{end}}]", port)
cmd := exec.Command("docker", "inspect", "-f", tmpl, id)
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
return "", "", fmt.Errorf("could not inspect container %s: %w", id, err)
}
// When IPv6 is turned on with Docker.
// Got [{"HostIp":"0.0.0.0","HostPort":"49190"}{"HostIp":"::","HostPort":"49190"}]
// Need [{"HostIp":"0.0.0.0","HostPort":"49190"},{"HostIp":"::","HostPort":"49190"}]
data := strings.ReplaceAll(out.String(), "}{", "},{")
var docs []struct {
HostIP string
HostPort string
}
if err := json.Unmarshal([]byte(data), &docs); err != nil {
return "", "", fmt.Errorf("could not decode json: %w", err)
}
for _, doc := range docs {
if doc.HostIP != "::" {
return doc.HostIP, doc.HostPort, nil
}
}
return "", "", fmt.Errorf("could not locate ip/port")
}