/
test_docker.go
156 lines (149 loc) · 3.41 KB
/
test_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
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
package kb
import (
"bufio"
"context"
"fmt"
"io"
"path/filepath"
"time"
"github.com/docker/docker/api/types/mount"
"github.com/midcontinentcontrols/kb/pkg/logger"
"github.com/docker/docker/api/types/strslice"
"github.com/docker/docker/api/types"
containertypes "github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"go.uber.org/zap"
)
func (t *TestSpec) runDocker(rootPath string, log logger.Logger) error {
cli, err := client.NewClientWithOpts()
if err != nil {
return err
}
var env []string
for _, v := range t.Variables {
env = append(env, v.Name+"="+v.Value)
}
var mounts []mount.Mount
for _, volume := range t.Env.Docker.Volumes {
source := volume.Source
if !filepath.IsAbs(source) {
source = filepath.Clean(filepath.Join(rootPath, source))
source, err = filepath.Abs(source)
if err != nil {
return err
}
}
ty := volume.Type
if ty == "" {
ty = "bind"
}
mounts = append(mounts, mount.Mount{
Type: mount.Type(ty),
Source: source,
Target: volume.Target,
ReadOnly: volume.ReadOnly,
Consistency: mount.Consistency(volume.Consistency),
})
}
var resp containertypes.ContainerCreateCreatedBody
resp, err = cli.ContainerCreate(
context.TODO(),
&containertypes.Config{
Image: t.Build.Name + ":latest",
Env: env,
Cmd: strslice.StrSlice(t.Build.Command),
},
&containertypes.HostConfig{
Mounts: mounts,
},
nil,
nil,
"",
)
if err != nil {
return fmt.Errorf("error creating container: %v", err)
}
container := resp.ID
log = log.With(zap.String("test.Name", t.Name))
defer func() {
if rmerr := cli.ContainerRemove(
context.TODO(),
container,
types.ContainerRemoveOptions{},
); rmerr != nil {
if err == nil {
err = rmerr
} else {
log.Error("error removing container",
zap.String("id", container),
zap.String("message", rmerr.Error()))
}
}
}()
for _, warning := range resp.Warnings {
log.Debug("Docker", zap.String("warning", warning))
}
if err := cli.ContainerStart(
context.TODO(),
container,
types.ContainerStartOptions{},
); err != nil {
return fmt.Errorf("error starting container: %v", err)
}
logs, err := cli.ContainerLogs(
context.TODO(),
container,
types.ContainerLogsOptions{
Follow: true,
ShowStdout: true,
ShowStderr: true,
},
)
if err != nil {
return fmt.Errorf("error getting logs: %v", err)
}
rd := bufio.NewReader(logs)
for {
message, err := rd.ReadString('\n')
if err != nil {
if err == io.EOF {
break
}
return err
}
log.Info(message)
}
ch, e := cli.ContainerWait(
context.TODO(),
container,
containertypes.WaitConditionNotRunning,
)
done := make(chan int, 1)
go func() {
start := time.Now()
for {
select {
case <-time.After(3 * time.Second):
log.Info("Still waiting on container", zap.String("elapsed", time.Since(start).String()))
case <-done:
return
}
}
}()
defer func() {
done <- 0
close(done)
}()
select {
case v := <-ch:
if v.Error != nil {
return fmt.Errorf("error waiting for container: %v", v.Error.Message)
}
if v.StatusCode != 0 {
return fmt.Errorf("exit code %d", v.StatusCode)
}
return nil
case err := <-e:
return fmt.Errorf("error waiting for container: %v", err)
}
}