-
Notifications
You must be signed in to change notification settings - Fork 630
/
Copy pathexamples_test.go
271 lines (229 loc) · 6.1 KB
/
examples_test.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//go:build examples
package examples
import (
"bufio"
"bytes"
"context"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"syscall"
"testing"
"time"
"os/exec"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)
const (
timeoutPerExample = 10 * time.Minute
durationToStayRunning = 5 * time.Second
)
type env struct {
dir string // project dir of docker-compose
path string // path to docker-compose file
}
type status struct {
Name string `json:"Name"`
State string `json:"State"`
}
func (e *env) projectName() string {
h := sha256.New()
_, _ = h.Write([]byte(e.dir))
return fmt.Sprintf("%s_%x", filepath.Base(e.dir), h.Sum(nil)[0:2])
}
func (e *env) newCmd(ctx context.Context, args ...string) *exec.Cmd {
c := exec.CommandContext(
ctx,
"docker",
append([]string{
"compose",
"--file", e.path,
"--project-directory", e.dir,
"--project-name", e.projectName(),
}, args...)...)
return c
}
func (e *env) newCmdWithOutputCapture(t testing.TB, ctx context.Context, args ...string) *exec.Cmd {
c := e.newCmd(ctx, args...)
stdout, err := c.StdoutPipe()
require.NoError(t, err)
go func() {
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
t.Log(scanner.Text())
}
}()
stderr, err := c.StderrPipe()
require.NoError(t, err)
go func() {
scanner := bufio.NewScanner(stderr)
for scanner.Scan() {
t.Log("STDERR: " + scanner.Text())
}
}()
return c
}
func (e *env) containerStatus(ctx context.Context) ([]status, error) {
data, err := e.newCmd(ctx, "ps", "--all", "--format", "json").Output()
if err != nil {
return nil, err
}
var stats []status
dec := json.NewDecoder(bytes.NewReader(data))
for {
var s status
err := dec.Decode(&s)
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return nil, err
}
stats = append(stats, s)
}
return stats, nil
}
func (e *env) containersAllRunning(ctx context.Context) error {
status, err := e.containerStatus(ctx)
if err != nil {
return err
}
var errs []error
for _, s := range status {
if s.State != "running" {
errs = append(errs, fmt.Errorf("container %s is not running", s.Name))
}
}
return errors.Join(errs...)
}
// removeExposedPorts removes ports from services which expose fixed ports. This will break once there is an overlap of ports. This will instead use random ports allocated by docker-compose.
func (e *env) removeExposedPorts(t testing.TB) *env {
var obj map[interface{}]interface{}
body, err := os.ReadFile(e.path)
if err != nil {
require.NoError(t, err)
}
if err := yaml.Unmarshal(body, &obj); err != nil {
require.NoError(t, err)
}
changed := false
for key, value := range obj {
if key.(string) == "services" {
services, ok := value.(map[string]interface{})
if !ok {
require.NoError(t, fmt.Errorf("services is not a map[string]interface{}"))
}
for serviceName, service := range services {
params, ok := service.(map[string]interface{})
if !ok {
require.NoError(t, fmt.Errorf("service '%s' is not a map[string]interface{}", serviceName))
}
// check for ports
ports, ok := params["ports"]
if !ok {
continue
}
portsSlice, ok := ports.([]interface{})
if !ok {
continue
}
for i := range portsSlice {
port, ok := portsSlice[i].(string)
if !ok {
continue
}
portSplitted := strings.Split(port, ":")
if len(portSplitted) < 2 {
continue
}
portsSlice[i] = portSplitted[len(portSplitted)-1]
changed = true
}
}
}
}
if !changed {
return e
}
path := filepath.Join(t.TempDir(), "docker-compose.yml")
data, err := yaml.Marshal(obj)
if err != nil {
require.NoError(t, err)
}
require.NoError(t, os.WriteFile(path, data, 0644))
return &env{
dir: e.dir,
path: path,
}
}
// This test is meant to catch very fundamental errors in the examples. It could be extened to be more comprehensive. For now it will just run the examples and check that they don't crash, within 5 seconds.
func TestDockerComposeBuildRun(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
ctx := context.Background()
// find docker compose files
out, err := exec.Command("git", "ls-files", "**/docker-compose.yml").Output()
require.NoError(t, err)
var envs []*env
for _, path := range strings.Split(strings.TrimSpace(string(out)), "\n") {
e := &env{dir: filepath.Dir(path), path: path}
envs = append(envs, e)
}
for i := range envs {
t.Run(envs[i].dir, func(t *testing.T) {
e := envs[i]
t.Parallel()
ctx, cancel := context.WithTimeout(ctx, timeoutPerExample)
defer cancel()
t.Run("build", func(t *testing.T) {
cmd := e.newCmdWithOutputCapture(t, ctx, "build")
require.NoError(t, cmd.Run())
})
// run pull first so lcontainers can start immediately
t.Run("pull", func(t *testing.T) {
cmd := e.newCmdWithOutputCapture(t, ctx, "pull")
require.NoError(t, cmd.Run())
})
// now run the docker-compose containers, run them for 5 seconds, it would abort if one of the containers exits
t.Run("run", func(t *testing.T) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
e = e.removeExposedPorts(t)
cmd := e.newCmdWithOutputCapture(t, ctx, "up", "--abort-on-container-exit")
require.NoError(t, cmd.Start())
// cleanup what ever happens
defer func() {
err := e.newCmdWithOutputCapture(t, context.Background(), "down", "--volumes").Run()
if err != nil {
t.Logf("cleanup error=%v\n", err)
}
}()
// check if all containers are still running after 5 seconds
go func() {
<-time.After(durationToStayRunning)
err := e.containersAllRunning(ctx)
if err != nil {
t.Logf("do nothing, as not all containers are running: %v\n", err)
return
}
t.Log("all healthy, start graceful shutdown")
err = cmd.Process.Signal(syscall.SIGTERM)
if err != nil {
t.Log("error sending terminate signal", err)
}
}()
err := cmd.Wait()
var exitError *exec.ExitError
if !errors.As(err, &exitError) || exitError.ExitCode() != 130 {
require.NoError(t, err)
}
})
})
}
}