forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer.go
146 lines (119 loc) · 3.2 KB
/
container.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
//go:build !freebsd
package testutil
import (
"context"
"fmt"
"io"
"strings"
"github.com/docker/go-connections/nat"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
type TestLogConsumer struct {
Msgs []string
}
func (g *TestLogConsumer) Accept(l testcontainers.Log) {
g.Msgs = append(g.Msgs, string(l.Content))
}
type Container struct {
BindMounts map[string]string
Entrypoint []string
Env map[string]string
ExposedPorts []string
Image string
Name string
Networks []string
WaitingFor wait.Strategy
Address string
Ports map[string]string
Logs TestLogConsumer
container testcontainers.Container
ctx context.Context
}
func (c *Container) Start() error {
c.ctx = context.Background()
containerMounts := make([]testcontainers.ContainerMount, 0, len(c.BindMounts))
for k, v := range c.BindMounts {
containerMounts = append(containerMounts, testcontainers.BindMount(v, testcontainers.ContainerMountTarget(k)))
}
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Mounts: testcontainers.Mounts(containerMounts...),
Entrypoint: c.Entrypoint,
Env: c.Env,
ExposedPorts: c.ExposedPorts,
Image: c.Image,
Name: c.Name,
Networks: c.Networks,
WaitingFor: c.WaitingFor,
},
Started: true,
}
container, err := testcontainers.GenericContainer(c.ctx, req)
if err != nil {
return fmt.Errorf("container failed to start: %s", err)
}
c.container = container
c.Logs = TestLogConsumer{
Msgs: []string{},
}
c.container.FollowOutput(&c.Logs)
err = c.container.StartLogProducer(c.ctx)
if err != nil {
return fmt.Errorf("log producer failed: %s", err)
}
c.Address = "localhost"
err = c.LookupMappedPorts()
if err != nil {
c.Terminate()
return fmt.Errorf("port lookup failed: %s", err)
}
return nil
}
// LookupMappedPorts creates a lookup table of exposed ports to mapped ports
func (c *Container) LookupMappedPorts() error {
if len(c.ExposedPorts) == 0 {
return nil
}
if len(c.Ports) == 0 {
c.Ports = make(map[string]string)
}
for _, port := range c.ExposedPorts {
// strip off leading host port: 80:8080 -> 8080
if strings.Contains(port, ":") {
port = strings.Split(port, ":")[1]
}
// strip off the transport: 80/tcp -> 80
if strings.Contains(port, "/") {
port = strings.Split(port, "/")[0]
}
p, err := c.container.MappedPort(c.ctx, nat.Port(port))
if err != nil {
return fmt.Errorf("failed to find '%s' - %s", port, err)
}
fmt.Printf("mapped container port '%s' to host port '%s'\n", port, p.Port())
c.Ports[port] = p.Port()
}
return nil
}
func (c *Container) Exec(cmds []string) (int, io.Reader, error) {
return c.container.Exec(c.ctx, cmds)
}
func (c *Container) PrintLogs() {
fmt.Println("--- Container Logs Start ---")
for _, msg := range c.Logs.Msgs {
fmt.Print(msg)
}
fmt.Println("--- Container Logs End ---")
}
func (c *Container) Terminate() {
err := c.container.StopLogProducer()
if err != nil {
fmt.Println(err)
}
err = c.container.Terminate(c.ctx)
if err != nil {
fmt.Printf("failed to terminate the container: %s", err)
}
c.PrintLogs()
}