-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathlambda_test.go
141 lines (113 loc) · 3.08 KB
/
lambda_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
package corral
import (
"context"
"encoding/json"
"os"
"testing"
"github.com/spf13/viper"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/aws/aws-sdk-go/service/lambda/lambdaiface"
"github.com/bcongdon/corral/internal/pkg/corfs"
"github.com/bcongdon/corral/internal/pkg/corlambda"
"github.com/stretchr/testify/assert"
)
func TestRunningInLambda(t *testing.T) {
res := runningInLambda()
assert.False(t, res)
for _, env := range []string{"LAMBDA_TASK_ROOT", "AWS_EXECUTION_ENV", "LAMBDA_RUNTIME_DIR"} {
os.Setenv(env, "value")
}
res = runningInLambda()
assert.True(t, res)
}
func TestHandleRequest(t *testing.T) {
testTask := task{
JobNumber: 0,
Phase: MapPhase,
BinID: 0,
IntermediateBins: 10,
Splits: []inputSplit{},
FileSystemType: corfs.Local,
WorkingLocation: ".",
}
job := &Job{
config: &config{},
}
// These values should be reset to 0 by Lambda handler function
job.bytesRead = 10
job.bytesWritten = 20
lambdaDriver = NewDriver(job)
output, err := handleRequest(context.Background(), testTask)
assert.Nil(t, err)
assert.Equal(t, "{\"BytesRead\":0,\"BytesWritten\":0}", output)
testTask.Phase = ReducePhase
output, err = handleRequest(context.Background(), testTask)
assert.Nil(t, err)
assert.Equal(t, "{\"BytesRead\":0,\"BytesWritten\":0}", output)
}
type mockLambdaClient struct {
lambdaiface.LambdaAPI
capturedPayload []byte
}
func (m *mockLambdaClient) Invoke(input *lambda.InvokeInput) (*lambda.InvokeOutput, error) {
m.capturedPayload = input.Payload
return &lambda.InvokeOutput{}, nil
}
func (*mockLambdaClient) GetFunction(*lambda.GetFunctionInput) (*lambda.GetFunctionOutput, error) {
return nil, nil
}
func (*mockLambdaClient) CreateFunction(*lambda.CreateFunctionInput) (*lambda.FunctionConfiguration, error) {
return nil, nil
}
func TestRunLambdaMapper(t *testing.T) {
mock := &mockLambdaClient{}
executor := &lambdaExecutor{
&corlambda.LambdaClient{
Client: mock,
},
nil,
"FunctionName",
}
job := &Job{
config: &config{WorkingLocation: "."},
}
err := executor.RunMapper(job, 0, 10, []inputSplit{})
assert.Nil(t, err)
var taskPayload task
err = json.Unmarshal(mock.capturedPayload, &taskPayload)
assert.Nil(t, err)
assert.Equal(t, uint(10), taskPayload.BinID)
assert.Equal(t, MapPhase, taskPayload.Phase)
}
func TestRunLambdaReducer(t *testing.T) {
mock := &mockLambdaClient{}
executor := &lambdaExecutor{
&corlambda.LambdaClient{
Client: mock,
},
nil,
"FunctionName",
}
job := &Job{
config: &config{WorkingLocation: "."},
}
err := executor.RunReducer(job, 0, 10)
assert.Nil(t, err)
var taskPayload task
err = json.Unmarshal(mock.capturedPayload, &taskPayload)
assert.Nil(t, err)
assert.Equal(t, uint(10), taskPayload.BinID)
assert.Equal(t, ReducePhase, taskPayload.Phase)
}
func TestDeployFunction(t *testing.T) {
mock := &mockLambdaClient{}
executor := &lambdaExecutor{
&corlambda.LambdaClient{
Client: mock,
},
nil,
"FunctionName",
}
viper.SetDefault("lambdaManageRole", false) // Disable testing role deployment
executor.Deploy()
}