-
Notifications
You must be signed in to change notification settings - Fork 82
/
docker.go
107 lines (88 loc) · 2.86 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
98
99
100
101
102
103
104
105
106
107
// Package docker provides support for starting and stopping docker containers
// for running tests.
package docker
import (
"bytes"
"fmt"
"net"
"os/exec"
"strings"
"testing"
jsoniter "github.com/json-iterator/go"
)
// 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),
}
fmt.Printf("Image: %s\n", image)
fmt.Printf("ContainerID: %s\n", c.ID)
fmt.Printf("Host: %s\n", c.Host)
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)
}
fmt.Println("Stopped:", id)
if err := exec.Command("docker", "rm", id, "-v").Run(); err != nil {
return fmt.Errorf("could not remove container: %w", err)
}
fmt.Println("Removed:", id)
return nil
}
// DumpContainerLogs outputs logs from the running docker container.
func DumpContainerLogs(t *testing.T, id string) {
out, err := exec.Command("docker", "logs", id).CombinedOutput()
if err != nil {
t.Fatalf("could not log container: %v", err)
}
t.Logf("Logs for %s\n%s:", id, 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 := jsoniter.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")
}