-
Notifications
You must be signed in to change notification settings - Fork 0
/
fakes.go
264 lines (228 loc) · 6.33 KB
/
fakes.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
package testutils
import (
"context"
"io/ioutil"
"net"
"os"
"path/filepath"
"sync"
"testing"
"time"
"google.golang.org/grpc"
"github.com/docker/swarmkit/agent/exec"
"github.com/docker/swarmkit/api"
"github.com/docker/swarmkit/ca"
"github.com/docker/swarmkit/identity"
"github.com/docker/swarmkit/log"
"github.com/stretchr/testify/require"
)
// TestExecutor is executor for integration tests
type TestExecutor struct {
mu sync.Mutex
desc *api.NodeDescription
}
// Describe just returns empty NodeDescription.
func (e *TestExecutor) Describe(ctx context.Context) (*api.NodeDescription, error) {
e.mu.Lock()
defer e.mu.Unlock()
if e.desc == nil {
return &api.NodeDescription{}, nil
}
return e.desc.Copy(), nil
}
// Configure does nothing.
func (e *TestExecutor) Configure(ctx context.Context, node *api.Node) error {
return nil
}
// SetNetworkBootstrapKeys does nothing.
func (e *TestExecutor) SetNetworkBootstrapKeys([]*api.EncryptionKey) error {
return nil
}
// Controller returns TestController.
func (e *TestExecutor) Controller(t *api.Task) (exec.Controller, error) {
return &TestController{
ch: make(chan struct{}),
}, nil
}
// UpdateNodeDescription sets the node description on the test executor
func (e *TestExecutor) UpdateNodeDescription(newDesc *api.NodeDescription) {
e.mu.Lock()
defer e.mu.Unlock()
e.desc = newDesc
}
// TestController is dummy channel based controller for tests.
type TestController struct {
ch chan struct{}
closeOnce sync.Once
}
// Update does nothing.
func (t *TestController) Update(ctx context.Context, task *api.Task) error {
return nil
}
// Prepare does nothing.
func (t *TestController) Prepare(ctx context.Context) error {
return nil
}
// Start does nothing.
func (t *TestController) Start(ctx context.Context) error {
return nil
}
// Wait waits on internal channel.
func (t *TestController) Wait(ctx context.Context) error {
select {
case <-t.ch:
case <-ctx.Done():
}
return nil
}
// Shutdown closes internal channel
func (t *TestController) Shutdown(ctx context.Context) error {
t.closeOnce.Do(func() {
close(t.ch)
})
return nil
}
// Terminate closes internal channel if it wasn't closed before.
func (t *TestController) Terminate(ctx context.Context) error {
t.closeOnce.Do(func() {
close(t.ch)
})
return nil
}
// Remove does nothing.
func (t *TestController) Remove(ctx context.Context) error {
return nil
}
// Close does nothing.
func (t *TestController) Close() error {
t.closeOnce.Do(func() {
close(t.ch)
})
return nil
}
// SessionHandler is an injectable function that can be used handle session requests
type SessionHandler func(*api.SessionRequest, api.Dispatcher_SessionServer) error
// MockDispatcher is a fake dispatcher that one agent at a time can connect to
type MockDispatcher struct {
mu sync.Mutex
sessionCh chan *api.SessionMessage
openSession *api.SessionRequest
closedSessions []*api.SessionRequest
sessionHandler SessionHandler
Addr string
}
// UpdateTaskStatus is not implemented
func (m *MockDispatcher) UpdateTaskStatus(context.Context, *api.UpdateTaskStatusRequest) (*api.UpdateTaskStatusResponse, error) {
panic("not implemented")
}
// Tasks keeps an open stream until canceled
func (m *MockDispatcher) Tasks(_ *api.TasksRequest, stream api.Dispatcher_TasksServer) error {
<-stream.Context().Done()
return nil
}
// Assignments keeps an open stream until canceled
func (m *MockDispatcher) Assignments(_ *api.AssignmentsRequest, stream api.Dispatcher_AssignmentsServer) error {
<-stream.Context().Done()
return nil
}
// Heartbeat always successfully heartbeats
func (m *MockDispatcher) Heartbeat(context.Context, *api.HeartbeatRequest) (*api.HeartbeatResponse, error) {
return &api.HeartbeatResponse{Period: time.Second * 5}, nil
}
// Session allows a session to be established, and sends the node info
func (m *MockDispatcher) Session(r *api.SessionRequest, stream api.Dispatcher_SessionServer) error {
m.mu.Lock()
handler := m.sessionHandler
m.openSession = r
m.mu.Unlock()
sessionID := identity.NewID()
defer func() {
m.mu.Lock()
defer m.mu.Unlock()
log.G(stream.Context()).Debugf("non-dispatcher side closed session: %s", sessionID)
m.closedSessions = append(m.closedSessions, r)
if m.openSession == r { // only overwrite session if it hasn't changed
m.openSession = nil
}
}()
if handler != nil {
return handler(r, stream)
}
// send the initial message first
if err := stream.Send(&api.SessionMessage{
SessionID: sessionID,
Managers: []*api.WeightedPeer{
{
Peer: &api.Peer{Addr: m.Addr},
},
},
}); err != nil {
return err
}
ctx := stream.Context()
for {
select {
case msg := <-m.sessionCh:
msg.SessionID = sessionID
if err := stream.Send(msg); err != nil {
return err
}
case <-ctx.Done():
return nil
}
}
}
// GetSessions return all the established and closed sessions
func (m *MockDispatcher) GetSessions() (*api.SessionRequest, []*api.SessionRequest) {
m.mu.Lock()
defer m.mu.Unlock()
return m.openSession, m.closedSessions
}
// SessionMessageChannel returns a writable channel to inject session messages
func (m *MockDispatcher) SessionMessageChannel() chan<- *api.SessionMessage {
return m.sessionCh
}
// SetSessionHandler lets you inject a custom function to handle session requests
func (m *MockDispatcher) SetSessionHandler(s SessionHandler) {
m.mu.Lock()
defer m.mu.Unlock()
m.sessionHandler = s
}
// NewMockDispatcher starts and returns a mock dispatcher instance that can be connected to
func NewMockDispatcher(t *testing.T, secConfig *ca.SecurityConfig, local bool) (*MockDispatcher, func()) {
var (
l net.Listener
err error
addr string
cleanup func()
)
if local {
tempDir, err := ioutil.TempDir("", "local-dispatcher-socket")
require.NoError(t, err)
addr = filepath.Join(tempDir, "socket")
l, err = net.Listen("unix", addr)
require.NoError(t, err)
cleanup = func() {
os.RemoveAll(tempDir)
}
} else {
l, err = net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
addr = l.Addr().String()
}
serverOpts := []grpc.ServerOption{grpc.Creds(secConfig.ServerTLSCreds)}
s := grpc.NewServer(serverOpts...)
m := &MockDispatcher{
Addr: addr,
sessionCh: make(chan *api.SessionMessage, 1),
}
api.RegisterDispatcherServer(s, m)
go s.Serve(l)
return m, func() {
l.Close()
s.Stop()
if cleanup != nil {
cleanup()
}
}
}