-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add better tests for providers * better coverage for AWS Lambda
- Loading branch information
Showing
2 changed files
with
217 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,125 @@ | ||
package awskinesis_test | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/service/kinesis" | ||
"github.com/golang/mock/gomock" | ||
"github.com/serverless/event-gateway/function" | ||
"github.com/serverless/event-gateway/providers/awskinesis" | ||
"github.com/serverless/event-gateway/providers/awskinesis/mock" | ||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
func TestLoad_MalformedJSON(t *testing.T) { | ||
config := []byte(`{"streamName": "", "region": `) | ||
loader := awskinesis.ProviderLoader{} | ||
func TestLoad(t *testing.T) { | ||
for _, testCase := range loadTests { | ||
config := []byte(testCase.config) | ||
loader := awskinesis.ProviderLoader{} | ||
|
||
provider, err := loader.Load(config) | ||
_, err := loader.Load(config) | ||
|
||
assert.Nil(t, provider) | ||
assert.EqualError(t, err, "unable to load function provider config: unexpected end of JSON input") | ||
assert.Equal(t, testCase.expectedError, err) | ||
} | ||
} | ||
|
||
func TestLoad_MissingStreamName(t *testing.T) { | ||
config := []byte(`{"streamName": "", "region": "us-east-1"}`) | ||
loader := awskinesis.ProviderLoader{} | ||
func TestCall(t *testing.T) { | ||
for _, testCase := range callTests { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
serviceMock := mock.NewMockKinesisAPI(mockCtrl) | ||
serviceMock.EXPECT().PutRecord(gomock.Any()).Return(testCase.putRecordResult, testCase.putRecordError) | ||
|
||
provider := awskinesis.AWSKinesis{ | ||
Service: serviceMock, | ||
StreamName: "teststream", | ||
Region: "us-east-1", | ||
} | ||
|
||
provider, err := loader.Load(config) | ||
output, err := provider.Call([]byte("testpayload")) | ||
|
||
assert.Nil(t, provider) | ||
assert.EqualError(t, err, "missing required fields for AWS Kinesis function") | ||
assert.Equal(t, testCase.expectedResult, output) | ||
assert.Equal(t, testCase.expectedError, err) | ||
} | ||
} | ||
|
||
func TestLoad_MissingRegion(t *testing.T) { | ||
config := []byte(`{"streamName": "test", "region": ""}`) | ||
loader := awskinesis.ProviderLoader{} | ||
func TestMarshalLogObject(t *testing.T) { | ||
for _, testCase := range logTests { | ||
enc := zapcore.NewMapObjectEncoder() | ||
|
||
provider, err := loader.Load(config) | ||
testCase.provider.MarshalLogObject(enc) | ||
|
||
assert.Nil(t, provider) | ||
assert.EqualError(t, err, "missing required fields for AWS Kinesis function") | ||
assert.Equal(t, testCase.expectedFields, enc.Fields) | ||
} | ||
} | ||
|
||
func TestCall(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
serviceMock := mock.NewMockKinesisAPI(mockCtrl) | ||
serviceMock.EXPECT().PutRecord(gomock.Any()).Return(&kinesis.PutRecordOutput{SequenceNumber: aws.String("testseq")}, nil) | ||
provider := awskinesis.AWSKinesis{ | ||
Service: serviceMock, | ||
StreamName: "teststream", | ||
Region: "us-east-1", | ||
} | ||
var loadTests = []struct { | ||
config string | ||
expectedError error | ||
}{ | ||
{ | ||
`{"streamName": "", "region": `, | ||
errors.New("unable to load function provider config: unexpected end of JSON input"), | ||
}, | ||
{ | ||
`{"streamName": "", "region": "us-east-1"}`, | ||
errors.New("missing required fields for AWS Kinesis function"), | ||
}, | ||
{ | ||
`{"streamName": "test", "region": ""}`, | ||
errors.New("missing required fields for AWS Kinesis function"), | ||
}, | ||
} | ||
|
||
output, err := provider.Call([]byte("testpayload")) | ||
var callTests = []struct { | ||
putRecordResult *kinesis.PutRecordOutput | ||
putRecordError error | ||
expectedResult []byte | ||
expectedError error | ||
}{ | ||
{ | ||
&kinesis.PutRecordOutput{SequenceNumber: aws.String("testseq")}, | ||
nil, | ||
[]byte("testseq"), | ||
nil, | ||
}, | ||
{ | ||
nil, | ||
awserr.New("", "", nil), | ||
[]byte(nil), | ||
&function.ErrFunctionCallFailed{Original: awserr.New("", "", nil)}, | ||
}, | ||
} | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, []byte("testseq"), output) | ||
var logTests = []struct { | ||
provider function.Provider | ||
expectedFields map[string]interface{} | ||
}{ | ||
{ | ||
awskinesis.AWSKinesis{ | ||
StreamName: "test", | ||
Region: "us-east-1", | ||
}, | ||
map[string]interface{}{ | ||
"streamName": "test", | ||
"region": "us-east-1", | ||
}, | ||
}, | ||
{ | ||
awskinesis.AWSKinesis{ | ||
AWSAccessKeyID: "id", | ||
AWSSecretAccessKey: "key", | ||
AWSSessionToken: "token", | ||
}, | ||
map[string]interface{}{ | ||
"streamName": "", | ||
"region": "", | ||
"awsAccessKeyId": "*****", | ||
"awsSecretAccessKey": "*****", | ||
"awsSessionToken": "*****", | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,148 @@ | ||
package awslambda_test | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/service/lambda" | ||
"github.com/golang/mock/gomock" | ||
"github.com/serverless/event-gateway/function" | ||
"github.com/serverless/event-gateway/providers/awslambda" | ||
"github.com/serverless/event-gateway/providers/awslambda/mock" | ||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
func TestLoad_MalformedJSON(t *testing.T) { | ||
config := []byte(`{"arn": "", "region": `) | ||
loader := awslambda.ProviderLoader{} | ||
func TestLoad(t *testing.T) { | ||
for _, testCase := range loadTests { | ||
config := []byte(testCase.config) | ||
loader := awslambda.ProviderLoader{} | ||
|
||
provider, err := loader.Load(config) | ||
_, err := loader.Load(config) | ||
|
||
assert.Nil(t, provider) | ||
assert.EqualError(t, err, "unable to load function provider config: unexpected end of JSON input") | ||
assert.Equal(t, testCase.expectedError, err) | ||
} | ||
} | ||
|
||
func TestLoad_MissingARN(t *testing.T) { | ||
config := []byte(`{"arn": "", "region": "us-east-1"}`) | ||
loader := awslambda.ProviderLoader{} | ||
|
||
provider, err := loader.Load(config) | ||
func TestCall(t *testing.T) { | ||
for _, testCase := range callTests { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
serviceMock := mock.NewMockLambdaAPI(mockCtrl) | ||
opts := &lambda.InvokeInput{ | ||
FunctionName: aws.String("testarn"), | ||
Payload: []byte("testpayload"), | ||
} | ||
serviceMock.EXPECT().Invoke(opts).Return(testCase.invokeResult, testCase.invokeError) | ||
|
||
assert.Nil(t, provider) | ||
assert.EqualError(t, err, "missing required fields for AWS Lambda function") | ||
} | ||
provider := awslambda.AWSLambda{ | ||
Service: serviceMock, | ||
|
||
func TestLoad_MissingRegion(t *testing.T) { | ||
config := []byte(`{"arn": "test", "region": ""}`) | ||
loader := awslambda.ProviderLoader{} | ||
ARN: "testarn", | ||
Region: "us-east-1", | ||
} | ||
|
||
provider, err := loader.Load(config) | ||
output, err := provider.Call([]byte("testpayload")) | ||
|
||
assert.Nil(t, provider) | ||
assert.EqualError(t, err, "missing required fields for AWS Lambda function") | ||
assert.Equal(t, testCase.expectedResult, output) | ||
assert.Equal(t, testCase.expectedError, err) | ||
} | ||
} | ||
|
||
func TestCall(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
serviceMock := mock.NewMockLambdaAPI(mockCtrl) | ||
opts := &lambda.InvokeInput{ | ||
FunctionName: aws.String("testarn"), | ||
Payload: []byte("testpayload"), | ||
} | ||
serviceMock.EXPECT().Invoke(opts).Return(&lambda.InvokeOutput{Payload: []byte("testoutput")}, nil) | ||
provider := awslambda.AWSLambda{ | ||
Service: serviceMock, | ||
func TestMarshalLogObject(t *testing.T) { | ||
for _, testCase := range logTests { | ||
enc := zapcore.NewMapObjectEncoder() | ||
|
||
testCase.provider.MarshalLogObject(enc) | ||
|
||
ARN: "testarn", | ||
Region: "us-east-1", | ||
assert.Equal(t, testCase.expectedFields, enc.Fields) | ||
} | ||
} | ||
|
||
output, err := provider.Call([]byte("testpayload")) | ||
var loadTests = []struct { | ||
config string | ||
expectedError error | ||
}{ | ||
{ | ||
`{"arn": "", "region": `, | ||
errors.New("unable to load function provider config: unexpected end of JSON input"), | ||
}, | ||
{ | ||
`{"arn": "", "region": "us-east-1"}`, | ||
errors.New("missing required fields for AWS Lambda function"), | ||
}, | ||
{ | ||
`{"arn": "test", "region": ""}`, | ||
errors.New("missing required fields for AWS Lambda function"), | ||
}, | ||
} | ||
|
||
var callTests = []struct { | ||
invokeResult *lambda.InvokeOutput | ||
invokeError error | ||
expectedResult []byte | ||
expectedError error | ||
}{ | ||
{ | ||
&lambda.InvokeOutput{Payload: []byte("testres")}, | ||
nil, | ||
[]byte("testres"), | ||
nil, | ||
}, | ||
{ | ||
&lambda.InvokeOutput{FunctionError: aws.String("FuncErr")}, | ||
nil, | ||
[]byte(nil), | ||
&function.ErrFunctionError{Original: errors.New("FuncErr")}, | ||
}, | ||
{ | ||
nil, | ||
awserr.New("TestCode", "", nil), | ||
[]byte(nil), | ||
&function.ErrFunctionCallFailed{Original: awserr.New("TestCode", "", nil)}, | ||
}, | ||
{ | ||
nil, | ||
awserr.New("AccessDeniedException", "", nil), | ||
[]byte(nil), | ||
&function.ErrFunctionAccessDenied{Original: awserr.New("AccessDeniedException", "", nil)}, | ||
}, | ||
{ | ||
nil, | ||
awserr.New(lambda.ErrCodeServiceException, "", nil), | ||
[]byte(nil), | ||
&function.ErrFunctionProviderError{Original: awserr.New(lambda.ErrCodeServiceException, "", nil)}, | ||
}, | ||
} | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, []byte("testoutput"), output) | ||
var logTests = []struct { | ||
provider function.Provider | ||
expectedFields map[string]interface{} | ||
}{ | ||
{ | ||
awslambda.AWSLambda{ | ||
ARN: "test", | ||
Region: "us-east-1", | ||
}, | ||
map[string]interface{}{ | ||
"arn": "test", | ||
"region": "us-east-1", | ||
}, | ||
}, | ||
{ | ||
awslambda.AWSLambda{ | ||
AWSAccessKeyID: "id", | ||
AWSSecretAccessKey: "key", | ||
AWSSessionToken: "token", | ||
}, | ||
map[string]interface{}{ | ||
"arn": "", | ||
"region": "", | ||
"awsAccessKeyId": "*****", | ||
"awsSecretAccessKey": "*****", | ||
"awsSessionToken": "*****", | ||
}, | ||
}, | ||
} |