-
Notifications
You must be signed in to change notification settings - Fork 0
/
harness_test.go
191 lines (157 loc) · 3.37 KB
/
harness_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
package ezdc
import (
"context"
"os"
"os/exec"
"sync"
"testing"
"time"
)
type mockCompose struct {
up func(ctx context.Context) *exec.Cmd
down func(ctx context.Context) *exec.Cmd
build func(ctx context.Context) *exec.Cmd
stop func(ctx context.Context) *exec.Cmd
pull func(ctx context.Context, svc ...string) *exec.Cmd
}
func (m mockCompose) defaultImpl(ctx context.Context, name string) *exec.Cmd {
return exec.CommandContext(ctx, "echo", name)
}
func (m mockCompose) Up(ctx context.Context) *exec.Cmd {
if m.up == nil {
return m.defaultImpl(ctx, "up")
}
return m.up(ctx)
}
func (m mockCompose) Down(ctx context.Context) *exec.Cmd {
if m.down == nil {
return m.defaultImpl(ctx, "down")
}
return m.down(ctx)
}
func (m mockCompose) Stop(ctx context.Context) *exec.Cmd {
if m.stop == nil {
return m.defaultImpl(ctx, "stop")
}
return m.stop(ctx)
}
func (m mockCompose) Pull(ctx context.Context, svc ...string) *exec.Cmd {
if m.pull == nil {
return m.defaultImpl(ctx, "pull")
}
return m.pull(ctx, svc...)
}
func (m mockCompose) Build(ctx context.Context) *exec.Cmd {
if m.build == nil {
return m.defaultImpl(ctx, "build")
}
return m.build(ctx)
}
type mockWaiter struct {
duration time.Duration
}
func (m mockWaiter) Wait(ctx context.Context) error {
timer := time.NewTimer(m.duration)
defer timer.Stop()
for {
select {
case <-timer.C:
return nil
case <-ctx.Done():
return nil
}
}
}
type mockCrasher struct {
crashed bool
code int
}
func (m *mockCrasher) Crash(code int) {
m.crashed = true
m.code = code
}
func TestShouldCleanupUponKillSignal(t *testing.T) {
mockCrash := &mockCrasher{}
h := Harness{
ProjectName: t.Name(),
File: "./whatever.yml",
Services: []Service{
{
Name: "foo",
Waiter: mockWaiter{10 * time.Second},
},
},
crasher: mockCrash,
}
h.cc = mockCompose{}
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
_, _ = h.Run(context.Background(), func() int {
return 0
})
wg.Done()
}()
time.Sleep(100 * time.Millisecond)
t.Log("sending interrupt")
h.termSig <- os.Interrupt
wg.Wait()
if mockCrash.crashed == false {
t.Fatal("should have crashed")
}
if mockCrash.code != 1 {
t.Fatalf("should have had exit code %d, had %d", 1, mockCrash.code)
}
}
func TestShouldErrIfPanicDuringRun(t *testing.T) {
h := Harness{
ProjectName: t.Name(),
File: "./whatever.yml",
cc: mockCompose{},
}
_, err := h.Run(context.Background(), func() int {
panic("foo")
})
if err == nil {
t.Fatal("should return error")
}
}
func TestShouldRunOurCleanupFunc(t *testing.T) {
h := Harness{
ProjectName: t.Name(),
File: "./whatever.yml",
cc: mockCompose{},
}
cleaned := false
h.CleanupFunc(func(ctx context.Context) {
t.Log("custom cleanup")
cleaned = true
})
_, err := h.Run(context.Background(), func() int {
return 0
})
if err != nil {
t.Fatal(err)
}
if cleaned != true {
t.Fatal("cleanup func wasnt run")
}
}
func TestShouldErrorIfDockerComposeCommandCantStart(t *testing.T) {
h := Harness{
ProjectName: t.Name(),
File: "./whatever.yml",
}
mc := mockCompose{}
mc.up = func(ctx context.Context) *exec.Cmd {
return exec.CommandContext(ctx, "assumably-nonexisting-command")
}
h.cc = mc
_, err := h.Run(context.Background(), func() int {
return 0
})
if err == nil {
t.Fatalf("should have errored")
}
t.Log(err)
}