-
Notifications
You must be signed in to change notification settings - Fork 288
/
fake_client.go
121 lines (105 loc) · 2.77 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
package dockercompose
import (
"context"
"encoding/json"
"fmt"
"io"
"testing"
"github.com/windmilleng/tilt/internal/container"
"github.com/windmilleng/tilt/pkg/model"
)
type FakeDCClient struct {
t *testing.T
ctx context.Context
RunLogOutput map[model.TargetName]<-chan string
ContainerIdOutput container.ID
eventJson chan string
ConfigOutput string
ServicesOutput string
UpCalls []UpCall
DownError error
}
// Represents a single call to Up
type UpCall struct {
PathToConfig []string
ServiceName model.TargetName
ShouldBuild bool
}
func NewFakeDockerComposeClient(t *testing.T, ctx context.Context) *FakeDCClient {
return &FakeDCClient{
t: t,
ctx: ctx,
eventJson: make(chan string, 100),
RunLogOutput: make(map[model.TargetName]<-chan string),
}
}
func (c *FakeDCClient) Up(ctx context.Context, configPaths []string, serviceName model.TargetName,
shouldBuild bool, stdout, stderr io.Writer) error {
c.UpCalls = append(c.UpCalls, UpCall{configPaths, serviceName, shouldBuild})
return nil
}
func (c *FakeDCClient) Down(ctx context.Context, configPaths []string, stdout, stderr io.Writer) error {
if c.DownError != nil {
err := c.DownError
c.DownError = err
return err
}
return nil
}
func (c *FakeDCClient) StreamLogs(ctx context.Context, configPaths []string, serviceName model.TargetName) (io.ReadCloser, error) {
output := c.RunLogOutput[serviceName]
reader, writer := io.Pipe()
go func() {
done := false
for !done {
select {
case <-ctx.Done():
done = true
case s, ok := <-output:
if !ok {
done = true
} else {
_, _ = writer.Write([]byte(s))
}
}
}
_ = writer.Close()
}()
return reader, nil
}
func (c *FakeDCClient) StreamEvents(ctx context.Context, configPaths []string) (<-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(ctx context.Context, configPaths []string) (string, error) {
return c.ConfigOutput, nil
}
func (c *FakeDCClient) Services(ctx context.Context, configPaths []string) (string, error) {
return c.ServicesOutput, nil
}
func (c *FakeDCClient) ContainerID(ctx context.Context, configPaths []string, serviceName model.TargetName) (container.ID, error) {
return c.ContainerIdOutput, nil
}