-
Notifications
You must be signed in to change notification settings - Fork 0
/
harness.go
205 lines (169 loc) · 4.41 KB
/
harness.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
package ezdc
import (
"bytes"
"context"
"fmt"
"io"
"log"
"os"
"os/exec"
"os/signal"
"time"
)
func infoLog(msg string) {
log.Println("#### EZDC #### INFO " + msg)
}
func errorLog(msg string) {
log.Println("#### EZDC #### ERROR " + msg)
}
// Service configures options for a service defined in the docker compose file
type Service struct {
Name string
Pull bool // pull before starting tests
Waiter Waiter // optional, how to wait for service to be ready
}
// FileLogWriter utility to open a file for logging the docker compose output
func FileLogWriter(fileName string) *os.File {
logFile, err := os.OpenFile(fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(fmt.Errorf("failed to open log file %s: %w", fileName, err))
}
_ = logFile.Truncate(0)
_, _ = logFile.Seek(0, 0)
return logFile
}
type crasher interface {
Crash(exitCode int)
}
type exitCrasher struct{}
func (e exitCrasher) Crash(exitCode int) {
os.Exit(exitCode)
}
type Harness struct {
ProjectName string // name for the compose project
File string // path to the docker compose file
Services []Service // configuration for services
Logs io.Writer // where to send the docker compose logs
termSig chan os.Signal
cc ComposeFace
cleanerUppers []func(context.Context)
crasher crasher
}
// Run is the entrypoint for running your testing.M.
//
// func TestMain(m *testing.M) {
// h := Harness{.....} // configure
//
// exitCode, err := h.Run(context.Background(), m.Run)
// if err != nil {
// panic(err)
// }
// os.Exit(exitCode)
// }
func (h *Harness) Run(ctx context.Context, f func() int) (code int, err error) {
ctx, cncl := context.WithCancel(ctx)
defer cncl()
h.termSig = make(chan os.Signal)
if h.cc == nil {
h.cc = DefaultComposeCmd{
project: h.ProjectName,
file: h.File,
}
}
if h.crasher == nil {
h.crasher = exitCrasher{}
}
go func() {
<-h.termSig
infoLog("got os Signal")
cncl()
h.cleanup(10 * time.Second)
h.crasher.Crash(1)
}()
signal.Notify(h.termSig, os.Interrupt)
if err := h.startDcServices(ctx); err != nil {
return 1, err
}
if err := h.waitForServices(ctx); err != nil {
return 1, err
}
infoLog("services ready")
defer func() {
if panicErr := recover(); panicErr != nil {
err = fmt.Errorf("panic: %s", panicErr)
}
h.cleanup(10 * time.Second)
}()
return f(), nil
}
func (h Harness) withLogs(cmd *exec.Cmd) (*exec.Cmd, *bytes.Buffer) {
if h.Logs == nil {
h.Logs = os.Stdout
}
buf := &bytes.Buffer{}
cmd.Stdout = h.Logs
cmd.Stderr = io.MultiWriter(h.Logs, buf)
return cmd, buf
}
func (h Harness) startDcServices(ctx context.Context) error {
infoLog("cleaning up lingering resources")
cmd, _ := h.withLogs(h.cc.Down(ctx))
_ = cmd.Run()
toPull := gmap(
filter(h.Services, func(s Service) bool {
return s.Pull
},
), func(s Service) string {
return s.Name
})
if len(toPull) > 0 {
cmd, errBuf := h.withLogs(h.cc.Pull(ctx, toPull...))
infoLog("pulling")
if err := cmd.Run(); err != nil {
_, _ = fmt.Fprintln(os.Stderr, errBuf.String())
return fmt.Errorf("error pulling: %w", err)
}
}
cmd, errBuf := h.withLogs(h.cc.Build(ctx))
infoLog("building")
if err := cmd.Run(); err != nil {
_, _ = fmt.Fprintln(os.Stderr, errBuf.String())
return fmt.Errorf("error building: %w", err)
}
cmd, errBuf = h.withLogs(h.cc.Up(ctx))
infoLog("starting")
if err := cmd.Start(); err != nil {
_, _ = fmt.Fprintln(os.Stderr, errBuf.String())
return err
}
return nil
}
func (h Harness) waitForServices(ctx context.Context) error {
toWaitFor := filter(h.Services, func(s Service) bool {
return s.Waiter != nil
})
for _, svc := range toWaitFor {
infoLog(fmt.Sprintf("waiting for '%s'...\n", svc.Name))
if err := svc.Waiter.Wait(ctx); err != nil {
return err
}
}
return nil
}
// CleanupFunc registers a function to be run before stopping the docker compose services
func (h *Harness) CleanupFunc(f func(context.Context)) {
h.cleanerUppers = append(h.cleanerUppers, f)
}
func (h Harness) cleanup(timeout time.Duration) {
infoLog("cleaning up")
ctx, cncl := context.WithTimeout(context.Background(), timeout)
defer cncl()
for _, f := range h.cleanerUppers {
f(ctx)
}
cmd, errBuf := h.withLogs(h.cc.Down(ctx))
if err := cmd.Run(); err != nil {
_, _ = fmt.Fprintln(os.Stderr, errBuf.String())
errorLog(fmt.Sprintf("failed to run 'down': %s", err))
}
}