Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon web services 模拟AWS API网关请求和用于Golang Lambda功能单元测试的DynamoDB_Amazon Web Services_Unit Testing_Go - Fatal编程技术网

Amazon web services 模拟AWS API网关请求和用于Golang Lambda功能单元测试的DynamoDB

Amazon web services 模拟AWS API网关请求和用于Golang Lambda功能单元测试的DynamoDB,amazon-web-services,unit-testing,go,Amazon Web Services,Unit Testing,Go,设置 视窗10 Gov1.10.3 aws cli v1.16.67 我想做的事 测试使用golang编写的AWS Lambda函数。该函数接受来自API网关的请求,然后使用DynamoDB执行一些操作。以下大部分内容摘自文章(我是围棋新手) 主程序包 进口( “编码/json” “日志” “net/http” “操作系统” “regexp” “github.com/aws/aws lambda go/events” “github.com/aws/aws lambda go/lambda”

设置

  • 视窗10
  • Gov1.10.3
  • aws cli v1.16.67
我想做的事

测试使用
golang
编写的AWS Lambda函数。该函数接受来自
API网关的请求,然后使用
DynamoDB
执行一些操作。以下大部分内容摘自文章(我是围棋新手)

主程序包
进口(
“编码/json”
“日志”
“net/http”
“操作系统”
“regexp”
“github.com/aws/aws lambda go/events”
“github.com/aws/aws lambda go/lambda”
)
var uuidRegexp=regexp.MustCompile(`b[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}\b`)
var errorLogger=log.New(os.Stderr,“ERROR”,log.Llongfile)
类型作业结构{
ID字符串`json:“ID”`
ClientID字符串`json:“ClientID”`
标题字符串`json:“标题”`
Count int`json:“Count”`
}
//CreateJobCommand管理与DynamoDB的交互
func CreateJobCommand(req events.APIGatewayProxyRequest)(events.APIGatewayProxyResponse,错误){
如果请求标题[“内容类型”]!=“应用程序/json”{
返回clientError(http.StatusNotAcceptable)//406
}
newJob:=新(作业)
err:=json.Unmarshal([]字节(req.Body),newJob)
//确保请求已正确反序列化
如果错误!=零{
返回clientError(http.StatusUnprocessableEntity)//422
}
//验证ID和ClientID属性是否匹配正则表达式模式
if!uuidRegexp.MatchString(newJob.ID)| |!uuidRegexp.MatchString(newJob.ClientID){
返回clientError(http.StatusBadRequest)
}
//强制现场检查
如果newJob.Title==“”{
返回clientError(http.StatusBadRequest)
}
//将项目放入数据库
err=putItem(newJob)//putItem在另一个文件中定义
如果错误!=零{
返回服务器错误(err)
}
return events.APIGatewayProxyResponse{
状态代码:201,
},零
}
//添加用于处理错误的帮助器。这会将任何错误记录到os.Stderr
//并返回500个内部服务器错误响应,AWS API
//网关理解。
func serverError(err error)(events.APIGatewayProxyResponse,error){
errorLogger.Println(err.Error())
return events.APIGatewayProxyResponse{
状态代码:http.StatusInternalServerError,
正文:http.StatusText(http.StatusInternalServerError),
},零
}
//类似地,为发送与客户端错误相关的响应添加帮助程序。
func clientError(状态int)(events.APIGatewayProxyResponse,错误){
return events.APIGatewayProxyResponse{
状态代码:状态,
正文:http.StatusText(状态),
},零
}
func putItem(作业*作业)错误{
//创建aws会话
sess:=session.Must(session.NewSession(&aws.Config{
地区:aws.String(“us-east-1”),
端点:aws.String(“http://localhost:8000"),
}))
//创建dynamodb实例
db:=发电机B.新(sess)
//将作业结构封送到aws属性值对象中
jobAVMap,错误:=dynamodbattribute.MarshallMap(作业)
如果错误!=零{
返回错误
}
输入:=&dynamodb.PutItemInput{
TableName:aws.String(“测试表”),
项目:jobAVMap,
}
_,err=db.PutItem(输入)
返回错误
}
func main(){
lambda.Start(CreateJobCommand)
}
问题

我想写一组单元测试来测试这个函数。在我看来,我需要做的第一件事是模拟API网关请求和DynamoDB表,但我不知道如何做到这一点

问题

  • 我是否应该使用模拟框架
  • 如果有人知道任何有助于此主题的文档,您能指出吗?(我的谷歌技能尚未透露任何信息)

  • 谢谢

    请检查在docker中运行dynamo db是否无助于您实施测试

    检查:


    您还可以非常轻松地在测试中将事件传递给处理程序

    除了:


    看一看。它提供了一个易于使用的测试/模拟框架,通过在本地机器或Docker中旋转与AWS兼容的API来开发AWS相关的应用程序。它支持24个AWSAPI,其中包括DynamoDB和Lambda。这确实是一个非常好的功能测试工具,无需使用单独的AWS环境。

    尽管这个问题有点老了。我也面临同样的问题,并发现以下资源。把它放在这里是为了其他人的利益。这些资源展示了如何在golang中编写美味的lambda

  • 代码-

  • 代码-

  • 代码-


  • 这个问题现在有点老了,但是您可以运行并使用这个模块,它可以在本地运行lambda,并且可以用来测试lambda的实际响应


    完整披露我分叉并升级了这个模块,我这样做的方式是在指针接收器中传递依赖项(因为处理程序的签名是有限的)并使用接口。每个服务都有相应的接口。对于dynamodb-dynamodbiface。所以在你的案例中,兰姆达本身 您需要定义一个接收器:

    type myReceiver struct {
        dynI dynamodbiface.DynamoDBAPI
    }
    
    
    将main更改为:

    func main() {
    
        sess := session.Must(session.NewSession(&aws.Config{
            Region: aws.String("your region")},
        ))
    
        inj := myReceiver{
            dyn: dynamodb.New(sess),
        }
        lambda.Start(inj.CreateJobCommand)
    
    将处理程序更改为

    func (inj *myReceiver) CreateJobCommand(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error)
    
    所有对dynamodb API的后续调用都需要通过接口:

        _, err = inj.dynI.PutItem(input)
    
    
    然后在测试函数中,您需要模拟响应:

    
     type mockDynamo struct {
        dynI dynamodbiface.DynamoDBAPI
        dynResponse dynamodb.PutItemOutput
    
    } 
    
    func (mq mockDynamo) PutItem (in *dynamodb.PutItemInput) (*dynamodb.PutItemOutput , error) {
        return &dynamodv.dynResponse, nil
    }
    
    
            m1: = mockDynamo {
    
                dynResponse : dynamodb.PutItemOutput{
                
                some mocked output
            } 
    
    
            inj := myReceiver{
                dyn: m1,         
            }
    
         inj.CreateJobCommand(some mocked data  for APIGateway request)