-
Notifications
You must be signed in to change notification settings - Fork 303
/
fake_client.go
233 lines (198 loc) · 5.62 KB
/
fake_client.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
package dockercompose
import (
"context"
"encoding/json"
"fmt"
"io"
"strings"
"sync"
"testing"
"time"
"unicode"
"github.com/compose-spec/compose-go/loader"
"github.com/stretchr/testify/require"
"github.com/compose-spec/compose-go/types"
"github.com/tilt-dev/tilt/internal/container"
"github.com/tilt-dev/tilt/pkg/model"
)
type FakeDCClient struct {
t *testing.T
ctx context.Context
mu sync.Mutex
RunLogOutput map[string]<-chan string
ContainerIdOutput container.ID
eventJson chan string
ConfigOutput string
VersionOutput string
upCalls []UpCall
downCalls []DownCall
rmCalls []RmCall
DownError error
RmError error
WorkDir string
}
var _ DockerComposeClient = &FakeDCClient{}
// Represents a single call to Up
type UpCall struct {
Spec model.DockerComposeUpSpec
ShouldBuild bool
}
// Represents a single call to Down
type DownCall struct {
Proj model.DockerComposeProject
}
type RmCall struct {
Specs []model.DockerComposeUpSpec
}
func NewFakeDockerComposeClient(t *testing.T, ctx context.Context) *FakeDCClient {
return &FakeDCClient{
t: t,
ctx: ctx,
eventJson: make(chan string, 100),
RunLogOutput: make(map[string]<-chan string),
}
}
func (c *FakeDCClient) Up(ctx context.Context, spec model.DockerComposeUpSpec,
shouldBuild bool, stdout, stderr io.Writer) error {
c.mu.Lock()
defer c.mu.Unlock()
c.upCalls = append(c.upCalls, UpCall{spec, shouldBuild})
return nil
}
func (c *FakeDCClient) Down(ctx context.Context, proj model.DockerComposeProject, stdout, stderr io.Writer) error {
c.mu.Lock()
defer c.mu.Unlock()
c.downCalls = append(c.downCalls, DownCall{proj})
if c.DownError != nil {
err := c.DownError
c.DownError = nil
return err
}
return nil
}
func (c *FakeDCClient) Rm(ctx context.Context, specs []model.DockerComposeUpSpec, stdout, stderr io.Writer) error {
c.mu.Lock()
defer c.mu.Unlock()
c.rmCalls = append(c.rmCalls, RmCall{specs})
if c.RmError != nil {
err := c.RmError
c.RmError = nil
return err
}
return nil
}
func (c *FakeDCClient) StreamLogs(ctx context.Context, spec model.DockerComposeUpSpec) io.ReadCloser {
output := c.RunLogOutput[spec.Service]
reader, writer := io.Pipe()
go func() {
c.t.Helper()
// docker-compose always logs an "Attaching to foo, bar" at the start of a log session
_, err := writer.Write([]byte(fmt.Sprintf("Attaching to %s\n", spec.Service)))
require.NoError(c.t, err, "Failed to write to fake Docker Compose logs")
done := false
for !done {
select {
case <-ctx.Done():
done = true
case s, ok := <-output:
if !ok {
done = true
} else {
logLine := fmt.Sprintf("%s %s\n",
time.Now().Format(time.RFC3339Nano),
strings.TrimRightFunc(s, unicode.IsSpace))
_, err = writer.Write([]byte(logLine))
require.NoError(c.t, err, "Failed to write to fake Docker Compose logs")
}
}
}
// we call docker-compose logs with --follow, so it only terminates (normally) when the container exits
// and it writes a message with the container exit code
_, err = writer.Write([]byte(fmt.Sprintf("%s exited with code 0\n", spec.Service)))
require.NoError(c.t, err, "Failed to write to fake Docker Compose logs")
require.NoError(c.t, writer.Close(), "Failed to close fake Docker Compose logs writer")
}()
return reader
}
func (c *FakeDCClient) StreamEvents(ctx context.Context, p model.DockerComposeProject) (<-chan string, error) {
events := make(chan string, 10)
go func() {
for {
select {
case event := <-c.eventJson:
select {
case events <- event: // send event to channel (unless it's full)
default:
panic(fmt.Sprintf("no room on events channel to send event: '%s'. Something "+
"is wrong (or you need to increase the buffer).", event))
}
case <-ctx.Done():
return
}
}
}()
return events, nil
}
func (c *FakeDCClient) SendEvent(evt Event) error {
j, err := json.Marshal(evt)
if err != nil {
return err
}
c.eventJson <- string(j)
return nil
}
func (c *FakeDCClient) Config(_ context.Context, _ []string) (string, error) {
return c.ConfigOutput, nil
}
func (c *FakeDCClient) Project(_ context.Context, m model.DockerComposeProject) (*types.Project, error) {
// this is a dummy ProjectOptions that lets us use compose's logic to apply options
// for consistency, but we have to then pull the data out ourselves since we're calling
// loader.Load ourselves
opts, err := composeProjectOptions(m)
if err != nil {
return nil, err
}
workDir := opts.WorkingDir
projectName := opts.Name
if projectName == "" {
projectName = model.NormalizeName(workDir)
}
if projectName == "" {
projectName = "fakedc"
}
p, err := loader.Load(types.ConfigDetails{
WorkingDir: workDir,
ConfigFiles: []types.ConfigFile{
{
Content: []byte(c.ConfigOutput),
},
},
Environment: opts.Environment,
}, dcLoaderOption(projectName))
return p, err
}
func (c *FakeDCClient) ContainerID(ctx context.Context, spec model.DockerComposeUpSpec) (container.ID, error) {
return c.ContainerIdOutput, nil
}
func (c *FakeDCClient) Version(_ context.Context) (string, string, error) {
if c.VersionOutput != "" {
return c.VersionOutput, "tilt-fake", nil
}
// default to a "known good" version that won't produce warnings
return "v1.29.2", "tilt-fake", nil
}
func (c *FakeDCClient) UpCalls() []UpCall {
c.mu.Lock()
defer c.mu.Unlock()
return append([]UpCall{}, c.upCalls...)
}
func (c *FakeDCClient) DownCalls() []DownCall {
c.mu.Lock()
defer c.mu.Unlock()
return append([]DownCall{}, c.downCalls...)
}
func (c *FakeDCClient) RmCalls() []RmCall {
c.mu.Lock()
defer c.mu.Unlock()
return append([]RmCall{}, c.rmCalls...)
}