-
Notifications
You must be signed in to change notification settings - Fork 82
/
workceptor_test.go
447 lines (402 loc) · 10.7 KB
/
workceptor_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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
package workceptor_test
import (
"context"
"crypto/tls"
"errors"
"fmt"
"os"
"testing"
"github.com/ansible/receptor/pkg/logger"
"github.com/ansible/receptor/pkg/workceptor"
"github.com/ansible/receptor/pkg/workceptor/mock_workceptor"
"github.com/ansible/receptor/tests/utils"
"go.uber.org/mock/gomock"
)
func testSetup(t *testing.T) (*gomock.Controller, *mock_workceptor.MockNetceptorForWorkceptor, *workceptor.Workceptor) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
ctx := context.Background()
mockNetceptor := mock_workceptor.NewMockNetceptorForWorkceptor(ctrl)
mockNetceptor.EXPECT().NodeID().Return("test").AnyTimes()
logger := logger.NewReceptorLogger("")
mockNetceptor.EXPECT().GetLogger().AnyTimes().Return(logger)
w, err := workceptor.New(ctx, mockNetceptor, "/tmp")
if err != nil {
t.Errorf("Error while creating Workceptor: %v", err)
}
return ctrl, mockNetceptor, w
}
func TestAllocateUnit(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockWorkUnit := mock_workceptor.NewMockWorkUnit(ctrl)
ctx := context.Background()
mockNetceptor := mock_workceptor.NewMockNetceptorForWorkceptor(ctrl)
logger := logger.NewReceptorLogger("")
mockNetceptor.EXPECT().GetLogger().AnyTimes().Return(logger)
workFunc := func(bwu workceptor.BaseWorkUnitForWorkUnit, w *workceptor.Workceptor, unitID string, workType string) workceptor.WorkUnit {
return mockWorkUnit
}
mockNetceptor.EXPECT().NodeID().Return("test").Times(4)
w, err := workceptor.New(ctx, mockNetceptor, "/tmp")
if err != nil {
t.Errorf("Error while creating Workceptor: %v", err)
}
const testType = "testType"
mockNetceptor.EXPECT().AddWorkCommand(gomock.Any(), gomock.Any()).Return(nil)
w.RegisterWorker(testType, workFunc, false)
const paramError = "SetFromParams error"
const saveError = "Save error"
testCases := []struct {
name string
workType string
setFromParamsError error
saveError error
mockSetParam bool
mockSave bool
expectedError string
}{
{
name: "normal case",
workType: testType,
setFromParamsError: nil,
saveError: nil,
mockSetParam: true,
mockSave: true,
expectedError: "",
},
{
name: "work type doesn't exist",
workType: "nonexistentType",
setFromParamsError: nil,
saveError: nil,
mockSetParam: false,
mockSave: false,
expectedError: fmt.Sprintf("unknown work type %s", "nonexistentType"),
},
{
name: paramError,
workType: testType,
setFromParamsError: errors.New(paramError),
saveError: nil,
mockSetParam: true,
mockSave: false,
expectedError: paramError,
},
{
name: saveError,
workType: testType,
setFromParamsError: nil,
saveError: errors.New(saveError),
mockSetParam: true,
mockSave: true,
expectedError: saveError,
},
}
checkError := func(err error, expectedError string, t *testing.T) {
if expectedError == "" && err != nil {
t.Errorf("Expected no error, got: %v", err)
} else if expectedError != "" && (err == nil || err.Error() != expectedError) {
t.Errorf("Expected error: %s, got: %v", expectedError, err)
}
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.mockSetParam {
mockWorkUnit.EXPECT().SetFromParams(gomock.Any()).Return(tc.setFromParamsError).Times(1)
}
if tc.mockSave {
mockWorkUnit.EXPECT().Save().Return(tc.saveError).Times(1)
}
_, err := w.AllocateUnit(tc.workType, "", map[string]string{"param": "value"})
checkError(err, tc.expectedError, t)
})
}
}
func TestRegisterWithControlService(t *testing.T) {
ctrl, _, w := testSetup(t)
mockServer := mock_workceptor.NewMockServerForWorkceptor(ctrl)
testCases := []struct {
name string
hasError bool
expectedCalls func()
}{
{
name: "normal case 1",
hasError: false,
expectedCalls: func() {
mockServer.EXPECT().AddControlFunc(gomock.Any(), gomock.Any()).Return(nil)
},
},
{
name: "error registering work",
hasError: true,
expectedCalls: func() {
mockServer.EXPECT().AddControlFunc(gomock.Any(), gomock.Any()).Return(errors.New("terminated"))
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tc.expectedCalls()
err := w.RegisterWithControlService(mockServer)
if tc.hasError && err.Error() != "could not add work control function: terminated" {
t.Error(err)
}
if !tc.hasError && err != nil {
t.Error(err)
}
})
}
}
func TestRegisterWorker(t *testing.T) {
_, mockNetceptor, w := testSetup(t)
testCases := []struct {
name string
typeName string
hasError bool
errorMsg string
expectedCalls func()
}{
{
name: "already registered",
typeName: "remote",
hasError: true,
errorMsg: "work type remote already registered",
expectedCalls: func() {
// For testing purposes
},
},
{
name: "normal with active unit",
typeName: "test",
hasError: false,
expectedCalls: func() {
mockNetceptor.EXPECT().AddWorkCommand(gomock.Any(), gomock.Any())
w.AllocateUnit("remote", "", map[string]string{})
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tc.expectedCalls()
err := w.RegisterWorker(tc.typeName, workceptor.NewRemoteWorker, false)
if tc.hasError && err.Error() != tc.errorMsg {
t.Error(err)
}
if !tc.hasError && err != nil {
t.Error(err)
}
})
}
}
func TestShouldVerifySignature(t *testing.T) {
_, _, w := testSetup(t)
testCases := []struct {
name string
workType string
}{
{
name: "return with remote true",
workType: "remote",
},
{
name: "return with false",
workType: "",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
shouldVerifySignature := w.ShouldVerifySignature(tc.workType, true)
t.Log(shouldVerifySignature)
if tc.workType == "remote" && !shouldVerifySignature {
t.Errorf("expected: true, received: %t", shouldVerifySignature)
}
if tc.workType == "" && shouldVerifySignature {
t.Errorf("expected: false, received: %t", shouldVerifySignature)
}
})
}
}
func TestVerifySignature(t *testing.T) {
_, _, w := testSetup(t)
_, public, err := utils.GenerateRSAPair()
if err != nil {
t.Error(err)
}
testCases := []struct {
name string
signature string
verifyingKey string
errorMsg string
}{
{
name: "signature is empty error",
signature: "",
errorMsg: "could not verify signature: signature is empty",
},
{
name: "verifying key not specified error",
signature: "sig",
verifyingKey: "",
errorMsg: "could not verify signature: verifying key not specified",
},
{
name: "no such key file error",
signature: "sig",
verifyingKey: "/tmp/nowhere.pub",
errorMsg: "could not load verifying key file: open /tmp/nowhere.pub: no such file or directory",
},
{
name: "token invalid number of segments error",
signature: "sig",
verifyingKey: public,
errorMsg: "could not verify signature: token contains an invalid number of segments",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
w.VerifyingKey = tc.verifyingKey
err := w.VerifySignature(tc.signature)
if tc.errorMsg != err.Error() {
t.Errorf("expected: %s, received: %s", tc.errorMsg, err)
}
})
}
}
func TestAllocateRemoteUnit(t *testing.T) {
_, mockNetceptor, w := testSetup(t)
testCases := []struct {
name string
workUnitID string
tlsClient string
ttl string
signWork bool
params map[string]string
errorMsg string
expectedCalls func()
}{
{
name: "get client tls config error",
workUnitID: "",
tlsClient: "something",
errorMsg: "terminated",
expectedCalls: func() {
mockNetceptor.EXPECT().GetClientTLSConfig(gomock.Any(), gomock.Any(), gomock.Any()).Return(&tls.Config{}, errors.New("terminated"))
},
},
{
name: "sending secrets over non tls connection error",
workUnitID: "",
tlsClient: "",
params: map[string]string{"secret_": "secret"},
errorMsg: "cannot send secrets over a non-TLS connection",
expectedCalls: func() {
// For testing purposes
},
},
{
name: "invalid duration error",
workUnitID: "",
tlsClient: "",
ttl: "ttl",
errorMsg: "time: invalid duration \"ttl\"",
expectedCalls: func() {
// For testing purposes
},
},
{
name: "normal case",
workUnitID: "",
tlsClient: "",
ttl: "1.5h",
errorMsg: "",
signWork: true,
expectedCalls: func() {
// For testing purposes
},
},
{
name: "pass workUnitID",
workUnitID: "testID12345678",
tlsClient: "",
ttl: "1.5h",
errorMsg: "",
signWork: true,
expectedCalls: func() {
// For testing purposes
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tc.expectedCalls()
wu, err := w.AllocateRemoteUnit("", "", tc.workUnitID, tc.tlsClient, tc.ttl, tc.signWork, tc.params)
if tc.errorMsg != "" && tc.errorMsg != err.Error() && err != nil {
t.Errorf("expected: %s, received: %s", tc.errorMsg, err)
}
if tc.errorMsg == "" && err != nil {
t.Error(err)
}
if tc.workUnitID != "" {
wuID := wu.ID()
if tc.workUnitID != wuID {
t.Errorf("expected workUnitID to equal %s but got %s", tc.workUnitID, wuID)
}
}
})
t.Cleanup(func() {
if tc.workUnitID != "" {
err := os.RemoveAll(fmt.Sprintf("/tmp/test/%s", tc.workUnitID))
if err != nil {
t.Errorf("removal of test directory /tmp/test/%s failed", tc.workUnitID)
}
}
})
}
}
func TestUnitStatus(t *testing.T) {
_, _, w := testSetup(t)
activeUnitsIDs := w.ListKnownUnitIDs()
_, err := w.UnitStatus(activeUnitsIDs[0])
if err != nil {
t.Error(err)
}
}
func TestCancelUnit(t *testing.T) {
_, _, w := testSetup(t)
activeUnitsIDs := w.ListKnownUnitIDs()
err := w.CancelUnit(activeUnitsIDs[0])
if err != nil {
t.Error(err)
}
}
func TestReleaseUnit(t *testing.T) {
_, _, w := testSetup(t)
activeUnitsIDs := w.ListKnownUnitIDs()
err := w.ReleaseUnit(activeUnitsIDs[0], true)
if err != nil {
t.Error(err)
}
}
func TestListKnownUnitIDs(t *testing.T) {
t.Parallel()
_, _, w := testSetup(t)
testCases := []struct {
name string
workType string
}{
{
name: "parallel test 1",
},
{
name: "parallel test 2",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
w.ListKnownUnitIDs()
})
}
}