And*_*M16 3 unit-testing go aws-lambda aws-sdk-go aws-lambda-go
我正在使用aws-sdk-go和aws-lambda-go构建一个aws lambda ,我遇到了一个小问题.
我想测试我的lambda处理程序.为此,我需要模拟一个有效的context.Context,它包含lamdacontext.LambdaContext的有效属性并且满足lambdacontext.FromContext.
我似乎无法找到建立这样的模拟的方法,因为lambdacontext.FromContext总是回报我_, false.
这是我的主要内容,在events.SNSEvent事件上有一个简单的处理程序:
package main
import (
"context"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-lambda-go/lambdacontext"
)
func main() {
lambda.Start(handleRequest)
}
func handleRequest(ctx context.Context, snsEvent events.SNSEvent) error {
lc, ok := lambdacontext.FromContext(ctx); if !ok {
// Always false
...
return someErr
}
. . .
return nil
}
Run Code Online (Sandbox Code Playgroud)
这是我的测试handleRequest:
package main
import (
"context"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambdacontext"
"github.com/stretchr/testify/assert"
"gitlab.easy-network.it/meg/aml-rekognition/testdata"
"testing"
)
const imgMock = `
{
\"some_parameter\": \"some_value\"
}`
func TestHandleRequest(t *testing.T) {
c := context.Background()
ctxV := context.WithValue(c, "", map[string]interface{}{
"AwsRequestID" : "some_aws_id",
"InvokedFunctionArn" : "some_arn",
"Identity" : lambdacontext.CognitoIdentity{},
"ClientContext" : lambdacontext.ClientContext{},
})
snsEventMock := events.SNSEvent{
Records: []events.SNSEventRecord{
{
SNS: events.SNSEntity{
Message: imgMock,
},
},
},
}
err := handleRequest(ctxV, snsEventMock)
assert.NoError(t, err)
}
Run Code Online (Sandbox Code Playgroud)
我也试过其他的模拟,比如传递一个带有这些参数的结构等,但我总是得到false.例如,我也尝试过:
type TestMock struct {
AwsRequestID string
InvokedFunctionArn string
Identity lambdacontext.CognitoIdentity
ClientContext lambdacontext.ClientContext
}
func TestHandleRequest(t *testing.T) {
c := context.Background()
testMock := TestMock{
AwsRequestID : "some_aws_id",
InvokedFunctionArn : "some_arn",
Identity : lambdacontext.CognitoIdentity{},
ClientContext : lambdacontext.ClientContext{},
}
ctxV := context.WithValue(c, "", testMock)
. . .
}
Run Code Online (Sandbox Code Playgroud)
我查看了源代码,FromContext我一直在摸不着头脑.
// LambdaContext is the set of metadata that is passed for every Invoke.
type LambdaContext struct {
AwsRequestID string
InvokedFunctionArn string
Identity CognitoIdentity
ClientContext ClientContext
}
// An unexported type to be used as the key for types in this package.
// This prevents collisions with keys defined in other packages.
type key struct{}
// The key for a LambdaContext in Contexts.
// Users of this package must use lambdacontext.NewContext and
lambdacontext.FromContext
// instead of using this key directly.
var contextKey = &key{}
// FromContext returns the LambdaContext value stored in ctx, if any.
func FromContext(ctx context.Context) (*LambdaContext, bool) {
lc, ok := ctx.Value(contextKey).(*LambdaContext)
return lc, ok
}
Run Code Online (Sandbox Code Playgroud)
当然,false即使我只是传递context.Background()给它,它也会返回.
我应该如何建立一个有效的任何想法context.Context,让lambdacontext.FromContext返回true?
lambda.FromContext()检查传递是否context.Context包含一个包含在lambdacontext包内的"私钥"的值:
// An unexported type to be used as the key for types in this package.
// This prevents collisions with keys defined in other packages.
type key struct{}
// The key for a LambdaContext in Contexts.
// Users of this package must use lambdacontext.NewContext and lambdacontext.FromContext
// instead of using this key directly.
var contextKey = &key{}
// FromContext returns the LambdaContext value stored in ctx, if any.
func FromContext(ctx context.Context) (*LambdaContext, bool) {
lc, ok := ctx.Value(contextKey).(*LambdaContext)
return lc, ok
}
Run Code Online (Sandbox Code Playgroud)
您无法访问此密钥,也无法"重现"它(您无法创建一个等于此"私有"密钥的值).
但是有一种简单的方法,只需使用它lambdacontext.NewContext()来导出具有此键的上下文:
// NewContext returns a new Context that carries value lc.
func NewContext(parent context.Context, lc *LambdaContext) context.Context {
return context.WithValue(parent, contextKey, lc)
}
Run Code Online (Sandbox Code Playgroud)
所以解决方案:
ctx := context.Background()
// Add keys to your liking, then:
lc := new(lambdacontext.LambdaContext)
ctx = lambdacontext.NewContext(ctx, lc)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1190 次 |
| 最近记录: |