-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathstandard_test.go
More file actions
654 lines (573 loc) · 22.8 KB
/
standard_test.go
File metadata and controls
654 lines (573 loc) · 22.8 KB
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
package host
import (
"context"
"errors"
"flag"
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/iancoleman/strcase"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/testing/protocmp"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/emptypb"
"github.com/smartcontractkit/chainlink-common/pkg/capabilities/v2/protoc/pkg/test_capabilities/actionandtrigger"
"github.com/smartcontractkit/chainlink-common/pkg/capabilities/v2/protoc/pkg/test_capabilities/basicaction"
"github.com/smartcontractkit/chainlink-common/pkg/capabilities/v2/protoc/pkg/test_capabilities/basictrigger"
"github.com/smartcontractkit/chainlink-common/pkg/capabilities/v2/protoc/pkg/test_capabilities/nodeaction"
"github.com/smartcontractkit/chainlink-protos/cre/go/sdk"
"github.com/smartcontractkit/chainlink-protos/cre/go/values"
valuespb "github.com/smartcontractkit/chainlink-protos/cre/go/values/pb"
)
// See the README.md in standard_tests for more information.
var anyTestConfig = []byte("config")
var anyTestTriggerValue = "test value"
var testPath string
func init() {
flag.StringVar(&testPath, "path", "./standard_tests", "Path to the standard tests")
}
func TestStandardConfig(t *testing.T) {
t.Parallel()
mockExecutionHelper := NewMockExecutionHelper(t)
mockExecutionHelper.EXPECT().GetWorkflowExecutionID().Return("id")
// Some languages call time during initiation of the executable before the main is called.
// This would be in unknown mode, which would call Node mode by default.
mockExecutionHelper.EXPECT().GetNodeTime().RunAndReturn(func() time.Time {
return time.Now()
}).Maybe()
wrappedConfig := runWithBasicTrigger(t, mockExecutionHelper)
wrappedValue := wrappedConfig.GetValue()
require.NotNil(t, wrappedValue, "Expected a value in the response")
actualConfig := wrappedConfig.GetValue().GetBytesValue()
require.ElementsMatch(t, anyTestConfig, actualConfig)
}
func TestStandardErrors(t *testing.T) {
t.Parallel()
mockExecutionHelper := NewMockExecutionHelper(t)
mockExecutionHelper.EXPECT().GetWorkflowExecutionID().Return("id")
mockExecutionHelper.EXPECT().GetNodeTime().RunAndReturn(func() time.Time {
return time.Now()
}).Maybe()
errMsg := runWithBasicTrigger(t, mockExecutionHelper)
assert.Contains(t, errMsg.GetError(), "workflow execution failure")
}
func TestStandardCapabilityCallsAreAsync(t *testing.T) {
// This test expects basic action's PerformAction to be called twice asynchronously and the results concatenated.
// To ensure the calls are actually async, the mock will block the first call until the second call is made.
// The first call sets InputThing to true, the second to false.
t.Parallel()
mockExecutionHelper := NewMockExecutionHelper(t)
mockExecutionHelper.EXPECT().GetWorkflowExecutionID().Return("id")
mockExecutionHelper.EXPECT().GetNodeTime().RunAndReturn(func() time.Time {
return time.Now()
}).Maybe()
mockExecutionHelper.EXPECT().GetDONTime().RunAndReturn(func() (time.Time, error) {
return time.Now(), nil
}).Maybe()
m := makeTestModule(t)
request := triggerExecuteRequest(t, 0, &basictrigger.Outputs{CoolOutput: anyTestTriggerValue})
callsSeen := map[bool]bool{}
mt := sync.Mutex{}
mt.Lock()
mockExecutionHelper.EXPECT().CallCapability(mock.Anything, mock.Anything).RunAndReturn(func(_ context.Context, request *sdk.CapabilityRequest) (*sdk.CapabilityResponse, error) {
assert.Equal(t, "basic-test-action@1.0.0", request.Id)
assert.Equal(t, "PerformAction", request.Method)
input := &basicaction.Inputs{}
assert.NoError(t, request.Payload.UnmarshalTo(input))
assert.False(t, callsSeen[input.InputThing])
callsSeen[input.InputThing] = true
payload, err := anypb.New(&basicaction.Outputs{AdaptedThing: fmt.Sprintf("%t", input.InputThing)})
require.NoError(t, err)
// Don't return until the second call has been executed
if input.InputThing {
mt.Lock()
}
defer mt.Unlock()
return &sdk.CapabilityResponse{
Response: &sdk.CapabilityResponse_Payload{Payload: payload},
}, nil
})
result := executeWithResult[string](t, m, request, mockExecutionHelper)
assert.Equal(t, "truefalse", result)
}
func TestStandardModeSwitch(t *testing.T) {
t.Parallel()
t.Run("successful mode switch", func(t *testing.T) {
mockExecutionHelper := NewMockExecutionHelper(t)
mockExecutionHelper.EXPECT().GetWorkflowExecutionID().Return("id")
// Node calls may occur on initialization depending on the language.
var donCall bool
var nodeCall bool
mockExecutionHelper.EXPECT().GetNodeTime().RunAndReturn(func() time.Time {
if donCall {
nodeCall = true
}
return time.Now()
})
// We want to make sure time.Now() is called at least twice in DON mode and once in node Mode
mockExecutionHelper.EXPECT().GetDONTime().RunAndReturn(func() (time.Time, error) {
donCall = true
return time.Now(), nil
}).Times(2)
mockExecutionHelper.EXPECT().CallCapability(mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, request *sdk.CapabilityRequest) (*sdk.CapabilityResponse, error) {
if request.Id == "basic-test-action@1.0.0" {
input := &basicaction.Inputs{}
assert.NoError(t, request.Payload.UnmarshalTo(input))
assert.True(t, input.InputThing)
payload, err := anypb.New(&basicaction.Outputs{AdaptedThing: fmt.Sprintf("test")})
require.NoError(t, err)
return &sdk.CapabilityResponse{
Response: &sdk.CapabilityResponse_Payload{Payload: payload},
}, nil
}
return setupNodeCallAndConsensusCall(t, 555)(ctx, request)
})
m := makeTestModule(t)
request := triggerExecuteRequest(t, 0, &basictrigger.Outputs{CoolOutput: anyTestTriggerValue})
result := executeWithResult[string](t, m, request, mockExecutionHelper)
require.Equal(t, "test556", result)
require.True(t, donCall)
require.True(t, nodeCall)
})
t.Run("node runtime in don mode", func(t *testing.T) {
mockExecutionHelper := NewMockExecutionHelper(t)
mockExecutionHelper.EXPECT().GetWorkflowExecutionID().Return("id")
mockExecutionHelper.EXPECT().GetNodeTime().RunAndReturn(func() time.Time {
return time.Now()
}).Maybe()
mockExecutionHelper.EXPECT().GetDONTime().RunAndReturn(func() (time.Time, error) {
return time.Now(), nil
}).Maybe()
mockExecutionHelper.EXPECT().CallCapability(mock.Anything, mock.Anything).RunAndReturn(func(_ context.Context, request *sdk.CapabilityRequest) (*sdk.CapabilityResponse, error) {
response := values.Proto(values.NewString("hi"))
payload, err := anypb.New(response)
require.NoError(t, err)
return &sdk.CapabilityResponse{
Response: &sdk.CapabilityResponse_Payload{
Payload: payload,
},
}, nil
}).Once()
m := makeTestModule(t)
request := triggerExecuteRequest(t, 0, &basictrigger.Outputs{CoolOutput: anyTestTriggerValue})
errStr := executeWithError(t, m, request, mockExecutionHelper)
require.Contains(t, errStr, "cannot use NodeRuntime outside RunInNodeMode")
})
t.Run("don runtime in node mode", func(t *testing.T) {
mockExecutionHelper := NewMockExecutionHelper(t)
mockExecutionHelper.EXPECT().GetWorkflowExecutionID().Return("id")
mockExecutionHelper.EXPECT().GetNodeTime().RunAndReturn(func() time.Time {
return time.Now()
}).Maybe()
mockExecutionHelper.EXPECT().GetDONTime().RunAndReturn(func() (time.Time, error) {
return time.Now(), nil
}).Maybe()
mockExecutionHelper.EXPECT().CallCapability(mock.Anything, mock.Anything).RunAndReturn(func(_ context.Context, request *sdk.CapabilityRequest) (*sdk.CapabilityResponse, error) {
assert.Equal(t, "consensus@1.0.0-alpha", request.Id)
input := &sdk.SimpleConsensusInputs{}
require.NoError(t, request.Payload.UnmarshalTo(input))
var errMsg string
switch msg := input.Observation.(type) {
case *sdk.SimpleConsensusInputs_Error:
errMsg = msg.Error
default:
require.Fail(t, "observation must be an error")
}
return &sdk.CapabilityResponse{
Response: &sdk.CapabilityResponse_Error{Error: errMsg},
}, nil
}).Once()
m := makeTestModule(t)
request := triggerExecuteRequest(t, 0, &basictrigger.Outputs{CoolOutput: anyTestTriggerValue})
errStr := executeWithError(t, m, request, mockExecutionHelper)
require.Contains(t, errStr, "cannot use Runtime inside RunInNodeMode")
})
}
func TestStandardLogging(t *testing.T) {
t.Parallel()
mockExecutionHelper := NewMockExecutionHelper(t)
mockExecutionHelper.EXPECT().GetWorkflowExecutionID().Return("id")
mockExecutionHelper.EXPECT().GetNodeTime().RunAndReturn(func() time.Time {
return time.Now()
}).Maybe()
mockExecutionHelper.EXPECT().GetDONTime().RunAndReturn(func() (time.Time, error) {
return time.Now(), nil
}).Maybe()
mockExecutionHelper.EXPECT().EmitUserLog(mock.Anything).RunAndReturn(func(s string) error {
assert.True(t, strings.Contains(s, "log from wasm!"))
return nil
}).Once()
runWithBasicTrigger(t, mockExecutionHelper)
}
func TestStandardMultipleTriggers(t *testing.T) {
t.Parallel()
m := makeTestModule(t)
t.Run("test registration", func(t *testing.T) {
mockExecutionHelper := NewMockExecutionHelper(t)
mockExecutionHelper.EXPECT().GetWorkflowExecutionID().Return("id")
mockExecutionHelper.EXPECT().GetNodeTime().RunAndReturn(func() time.Time {
return time.Now()
}).Maybe()
subscribe := &sdk.ExecuteRequest{Request: &sdk.ExecuteRequest_Subscribe{Subscribe: &emptypb.Empty{}}}
actual, err := m.Execute(t.Context(), subscribe, mockExecutionHelper)
require.NoError(t, err)
payload0, err := anypb.New(&basictrigger.Config{
Name: "first-trigger",
Number: 100,
})
require.NoError(t, err)
payload1, err := anypb.New(&actionandtrigger.Config{
Name: "second-trigger",
Number: 150,
})
require.NoError(t, err)
payload2, err := anypb.New(&basictrigger.Config{
Name: "third-trigger",
Number: 200,
})
require.NoError(t, err)
expected := &sdk.TriggerSubscriptionRequest{
Subscriptions: []*sdk.TriggerSubscription{
{
Id: "basic-test-trigger@1.0.0",
Payload: payload0,
Method: "Trigger",
},
{
Id: "basic-test-action-trigger@1.0.0",
Payload: payload1,
Method: "Trigger",
},
{
Id: "basic-test-trigger@1.0.0",
Payload: payload2,
Method: "Trigger",
},
},
}
assertProto(t, expected, actual.GetTriggerSubscriptions())
})
t.Run("first callback", func(t *testing.T) {
mockExecutionHelper := NewMockExecutionHelper(t)
mockExecutionHelper.EXPECT().GetWorkflowExecutionID().Return("id")
mockExecutionHelper.EXPECT().GetNodeTime().RunAndReturn(func() time.Time {
return time.Now()
}).Maybe()
request := triggerExecuteRequest(t, 0, &basictrigger.Outputs{CoolOutput: anyTestTriggerValue})
result := executeWithResult[string](t, m, request, mockExecutionHelper)
require.Equal(t, fmt.Sprintf("called 0 with %v", anyTestTriggerValue), result)
})
t.Run("same trigger as first one but different registration", func(t *testing.T) {
mockExecutionHelper := NewMockExecutionHelper(t)
mockExecutionHelper.EXPECT().GetWorkflowExecutionID().Return("id")
mockExecutionHelper.EXPECT().GetNodeTime().RunAndReturn(func() time.Time {
return time.Now()
}).Maybe()
request := triggerExecuteRequest(t, 2, &basictrigger.Outputs{CoolOutput: "different"})
result := executeWithResult[string](t, m, request, mockExecutionHelper)
require.Equal(t, "called 2 with different", result)
})
t.Run("different capability callback", func(t *testing.T) {
mockExecutionHelper := NewMockExecutionHelper(t)
mockExecutionHelper.EXPECT().GetWorkflowExecutionID().Return("id")
mockExecutionHelper.EXPECT().GetNodeTime().RunAndReturn(func() time.Time {
return time.Now()
}).Maybe()
request := triggerExecuteRequest(t, 1, &actionandtrigger.TriggerEvent{CoolOutput: "different"})
result := executeWithResult[string](t, m, request, mockExecutionHelper)
require.Equal(t, "called 1 with different", result)
})
}
func TestStandardRandom(t *testing.T) {
t.Parallel()
m := makeTestModule(t)
// Test binary executes node mode code conditionally based on the value >= 100
anyId := "Id"
gte100Exec := NewMockExecutionHelper(t)
gte100Exec.EXPECT().GetWorkflowExecutionID().Return(anyId)
gte100Exec.EXPECT().GetNodeTime().RunAndReturn(func() time.Time {
return time.Now()
}).Maybe()
gte100Exec.EXPECT().GetDONTime().RunAndReturn(func() (time.Time, error) {
return time.Now(), nil
}).Maybe()
// RunAndReturn
gte100Exec.EXPECT().CallCapability(mock.Anything, mock.Anything).RunAndReturn(setupNodeCallAndConsensusCall(t, 150))
m.Start()
defer m.Close()
trigger := &basictrigger.Outputs{CoolOutput: "trigger1"}
triggerPayload, err := anypb.New(trigger)
require.NoError(t, err)
anyRequest := &sdk.ExecuteRequest{
Request: &sdk.ExecuteRequest_Trigger{
Trigger: &sdk.Trigger{
Id: uint64(0),
Payload: triggerPayload,
},
},
}
// any since uint64 can be int64 or *big.Int
value1 := executeWithResult[any](t, m, anyRequest, gte100Exec)
t.Run("Same execution id gives the same randoms even if random is called in node mode", func(t *testing.T) {
lt100Exec := NewMockExecutionHelper(t)
lt100Exec.EXPECT().GetWorkflowExecutionID().Return(anyId)
lt100Exec.EXPECT().GetNodeTime().RunAndReturn(func() time.Time {
return time.Now()
}).Maybe()
lt100Exec.EXPECT().GetDONTime().RunAndReturn(func() (time.Time, error) {
return time.Now(), nil
}).Maybe()
lt100Exec.EXPECT().CallCapability(mock.Anything, mock.Anything).RunAndReturn(setupNodeCallAndConsensusCall(t, 99))
lt100Exec.EXPECT().EmitUserLog(mock.Anything).RunAndReturn(func(s string) error {
parts := strings.Split(s, "***")
_, err = strconv.ParseUint(parts[1], 10, 64)
require.NoError(t, err)
return nil
}).Once()
value2 := executeWithResult[any](t, m, anyRequest, lt100Exec)
require.Equal(t, value1, value2, "Expected the same random number to be generated for the same trigger")
})
t.Run("Different execution id give different randoms", func(t *testing.T) {
require.NoError(t, err)
gte100Exec2 := NewMockExecutionHelper(t)
gte100Exec2.EXPECT().GetWorkflowExecutionID().Return("differentId")
gte100Exec2.EXPECT().GetNodeTime().RunAndReturn(func() time.Time {
return time.Now()
}).Maybe()
gte100Exec2.EXPECT().GetDONTime().RunAndReturn(func() (time.Time, error) {
return time.Now(), nil
}).Maybe()
gte100Exec2.EXPECT().CallCapability(mock.Anything, mock.Anything).RunAndReturn(setupNodeCallAndConsensusCall(t, 120))
value2 := executeWithResult[any](t, m, anyRequest, gte100Exec2)
require.NotEqual(t, value1, value2, "Expected different random numbers for different triggers")
})
}
func TestStandardSecrets(t *testing.T) {
t.Parallel()
m := makeTestModule(t)
t.Run("returns the secret value", func(t *testing.T) {
result := runSecretTest(t, m, &sdk.SecretResponse{
Response: &sdk.SecretResponse_Secret{
Secret: &sdk.Secret{
Value: "Bar",
},
},
})
require.Equal(t, "Bar", result.GetValue().GetStringValue())
})
t.Run("returns an error if the secret doesn't exist", func(t *testing.T) {
resp := runSecretTest(t, m, &sdk.SecretResponse{
Response: &sdk.SecretResponse_Error{
Error: &sdk.SecretError{
Error: "could not find secret",
},
},
})
assert.ErrorContains(t, errors.New(resp.GetError()), "could not find secret")
})
}
func TestStandardSecretsFailInNodeMode(t *testing.T) {
mockExecutionHelper := NewMockExecutionHelper(t)
mockExecutionHelper.EXPECT().GetWorkflowExecutionID().Return("id")
mockExecutionHelper.EXPECT().GetNodeTime().RunAndReturn(func() time.Time {
return time.Now()
}).Maybe()
mockExecutionHelper.EXPECT().GetDONTime().RunAndReturn(func() (time.Time, error) {
return time.Now(), nil
}).Maybe()
mockExecutionHelper.EXPECT().CallCapability(mock.Anything, mock.Anything).RunAndReturn(func(_ context.Context, request *sdk.CapabilityRequest) (*sdk.CapabilityResponse, error) {
assert.Equal(t, "consensus@1.0.0-alpha", request.Id)
input := &sdk.SimpleConsensusInputs{}
require.NoError(t, request.Payload.UnmarshalTo(input))
var errMsg string
switch msg := input.Observation.(type) {
case *sdk.SimpleConsensusInputs_Error:
errMsg = msg.Error
default:
require.Fail(t, "observation must be an error")
}
return &sdk.CapabilityResponse{
Response: &sdk.CapabilityResponse_Error{Error: errMsg},
}, nil
}).Once()
m := makeTestModule(t)
request := triggerExecuteRequest(t, 0, &basictrigger.Outputs{CoolOutput: anyTestTriggerValue})
errStr := executeWithError(t, m, request, mockExecutionHelper)
require.Contains(t, errStr, "cannot use Runtime inside RunInNodeMode")
}
func triggerExecuteRequest(t *testing.T, id uint64, trigger proto.Message) *sdk.ExecuteRequest {
wrappedTrigger, err := anypb.New(trigger)
require.NoError(t, err)
return &sdk.ExecuteRequest{
Config: anyTestConfig,
Request: &sdk.ExecuteRequest_Trigger{
Trigger: &sdk.Trigger{Id: id, Payload: wrappedTrigger},
},
MaxResponseSize: uint64(defaultMaxResponseSizeBytes),
}
}
func runWithBasicTrigger(t *testing.T, executor ExecutionHelper) *sdk.ExecutionResult {
trigger := &basictrigger.Outputs{CoolOutput: anyTestTriggerValue}
executeRequest := triggerExecuteRequest(t, 0, trigger)
m := makeTestModule(t)
response, err := m.Execute(t.Context(), executeRequest, executor)
require.NoError(t, err)
return response
}
// makeTestModule compiles the test module from the Makefile in the testPath directory
// The test to compile and run is determined by the test name.
// To re-use a binary, an outer test can create the module and use t.Run to run subtests using that module.
// When subtests have their own binaries, those binaries are expected to be nested in a subfolder.
func makeTestModule(t *testing.T) *module {
testName := strcase.ToSnake(t.Name()[len("TestStandard"):])
return makeTestModuleByName(t, testName, nil)
}
func makeTestModuleWithCfg(t *testing.T, cfg *ModuleConfig) *module {
testName := strcase.ToSnake(t.Name()[len("TestStandard"):])
return makeTestModuleByName(t, testName, cfg)
}
func makeTestModuleByName(t *testing.T, testName string, cfg *ModuleConfig) *module {
wasmName := path.Join(testName, "test.wasm")
cmd := exec.Command("make", wasmName) // #nosec
absPath, err := filepath.Abs(testPath)
require.NoError(t, err, "Failed to get absolute path for test directory")
cmd.Dir = absPath
output, err := cmd.CombinedOutput()
require.NoError(t, err, string(output))
binary, err := os.ReadFile(filepath.Join(absPath, wasmName))
require.NoError(t, err)
if cfg == nil {
cfg = defaultNoDAGModCfg(t)
}
mod, err := NewModule(cfg, binary)
require.NoError(t, err)
return mod
}
func setupNodeCallAndConsensusCall(t *testing.T, output int32) func(_ context.Context, request *sdk.CapabilityRequest) (*sdk.CapabilityResponse, error) {
return func(_ context.Context, request *sdk.CapabilityRequest) (*sdk.CapabilityResponse, error) {
nodeResponse := &nodeaction.NodeOutputs{OutputThing: output}
var err error
var payload *anypb.Any
switch request.Id {
case "basic-test-node-action@1.0.0":
input := &nodeaction.NodeInputs{}
assert.NoError(t, request.Payload.UnmarshalTo(input))
assert.True(t, input.InputThing)
payload, err = anypb.New(nodeResponse)
if err != nil {
require.Fail(t, err.Error())
}
case "consensus@1.0.0-alpha":
input := &sdk.SimpleConsensusInputs{}
require.NoError(t, request.Payload.UnmarshalTo(input))
expectedObservation := wrapValue(t, nodeResponse)
expectedInput := &sdk.SimpleConsensusInputs{
Observation: &sdk.SimpleConsensusInputs_Value{Value: expectedObservation},
Descriptors: &sdk.ConsensusDescriptor{
Descriptor_: &sdk.ConsensusDescriptor_FieldsMap{
FieldsMap: &sdk.FieldsMap{
Fields: map[string]*sdk.ConsensusDescriptor{
"OutputThing": {
Descriptor_: &sdk.ConsensusDescriptor_Aggregation{
Aggregation: sdk.AggregationType_AGGREGATION_TYPE_MEDIAN,
},
},
},
},
},
},
Default: wrapValue(t, &nodeaction.NodeOutputs{OutputThing: 123}),
}
assertProto(t, expectedInput, input)
cResponse := &nodeaction.NodeOutputs{OutputThing: output + 1}
response := wrapValue(t, cResponse)
payload, err = anypb.New(response)
require.NoError(t, err)
default:
err = fmt.Errorf("unexpected capability: %s", request.Id)
assert.Fail(t, err.Error())
return nil, err
}
return &sdk.CapabilityResponse{
Response: &sdk.CapabilityResponse_Payload{
Payload: payload,
},
}, nil
}
}
func wrapValue(t *testing.T, nodeResponse *nodeaction.NodeOutputs) *valuespb.Value {
valueInput, err := values.Wrap(nodeResponse)
require.NoError(t, err)
return values.Proto(valueInput)
}
func assertProto[T proto.Message](t *testing.T, expected, actual T) {
t.Helper()
diff := cmp.Diff(expected, actual, protocmp.Transform())
var sb strings.Builder
for _, line := range strings.Split(diff, "\n") {
if strings.HasPrefix(line, "+") || strings.HasPrefix(line, "-") {
sb.WriteString(line + "\n")
}
}
assert.Empty(t, sb.String())
}
func runSecretTest(t *testing.T, m *module, secretResponse *sdk.SecretResponse) *sdk.ExecutionResult {
mockExecutionHelper := NewMockExecutionHelper(t)
mockExecutionHelper.EXPECT().GetWorkflowExecutionID().Return("Id")
mockExecutionHelper.EXPECT().GetNodeTime().RunAndReturn(func() time.Time {
return time.Now()
}).Maybe()
mockExecutionHelper.EXPECT().GetDONTime().RunAndReturn(func() (time.Time, error) {
return time.Now(), nil
}).Maybe()
mockExecutionHelper.EXPECT().GetSecrets(mock.Anything, mock.Anything).
RunAndReturn(func(_ context.Context, request *sdk.GetSecretsRequest) ([]*sdk.SecretResponse, error) {
assert.Len(t, request.Requests, 1)
assert.Equal(t, "Foo", request.Requests[0].Id)
return []*sdk.SecretResponse{secretResponse}, nil
}).
Once()
trigger := &basictrigger.Outputs{CoolOutput: anyTestTriggerValue}
executeRequest := triggerExecuteRequest(t, 0, trigger)
response, err := m.Execute(t.Context(), executeRequest, mockExecutionHelper)
require.NoError(t, err)
return response
}
func executeWithResult[T any](t *testing.T, m *module, req *sdk.ExecuteRequest, executor ExecutionHelper) T {
res, err := m.Execute(t.Context(), req, executor)
require.NoError(t, err)
var result T
switch v := res.Result.(type) {
case *sdk.ExecutionResult_Value:
wrappedValue, err := values.FromProto(v.Value)
require.NoError(t, err)
require.NoError(t, wrappedValue.UnwrapTo(&result))
case *sdk.ExecutionResult_Error:
require.Failf(t, "unexpected error in result", "error: %s", v.Error)
default:
require.Failf(t, "unexpected result type", "result: %v", res)
}
return result
}
func executeWithError(t *testing.T, m *module, req *sdk.ExecuteRequest, executor ExecutionHelper) string {
res, err := m.Execute(t.Context(), req, executor)
require.NoError(t, err)
switch e := res.Result.(type) {
case *sdk.ExecutionResult_Error:
return e.Error
default:
require.Failf(t, "unexpected result type", "%T", e)
return ""
}
}