doudiaozhi6658 2018-05-21 18:05
浏览 37

如何模拟AWS SDK for Go呼叫

I am writing a unit test for a function that uses the AWS SDK for Go to get a secret from the AWS Secrets Manager:

main.go

//Helper function to get secret from AWS Secret Manager
func getAWSSecrets(svc secretsmanageriface.SecretsManagerAPI) (secretMap map[string]string, err error) {
    //Get secret config values
    req, resp := svc.GetSecretValueRequest(&secretsmanager.GetSecretValueInput{
        SecretId: aws.String("my/secret/string"),
    })

    err = req.Send()
    if err != nil {
        return nil, err
    }

 ...
}

In order to test this, I need to create a mock AWS Secret Manager Client. I've set up the basic skeleton of how that will work:

main_test.go

type MockSecretsManagerClient struct {
  secretsmanageriface.SecretsManagerAPI
} 

func (m *MockSecretsManagerClient) GetSecretValueRequest(input *secretsmanager.GetSecretValueInput) (req *request.Request, resp *secretsmanager.GetSecretValueOutput){
  // Confused on how to mock out the returned `req`
}


// tests getAWSSecrets
func (suite *ServerTestSuite) TestGetAWSSecrets() {
    //Setup test 
  mockSvc := &MockSecretsManagerClient{}
  req, resp := getAWSSecrets(mockSvc)
}

I'm running into trouble trying to mock the returned request from GetSecretValueRequest. Furthermore, once I mock this request, I'm not sure how to handle mocking req.Send(). Is there a simple way to do this? Or are there any good examples of someone doing this?

  • 写回答

1条回答 默认 最新

  • dsdfd2322 2018-05-23 19:48
    关注

    First, find the service in the "AWS SDK for Go API Reference."

    Then look up the API call. Your call is here https://docs.aws.amazon.com/sdk-for-go/api/service/secretsmanager/#SecretsManager.GetSecretValueRequest

    The prototype for the API call is

    func (c *SecretsManager) GetSecretValueRequest(input *GetSecretValueInput) (req *request.Request, output *GetSecretValueOutput)
    

    So it returns a request.Request and a GetSecretValueOutput

    The two output items are structs and they are linked in the documentation. The mock should return those two items in the same way ie

    func (m *MockSecretsManagerClient) GetSecretValueRequest(input *secretsmanager.GetSecretValueInput) (req *request.Request, resp *secretsmanager.GetSecretValueOutput) {
    
        req = new(request.Request)
        r := new(http.Response)
        r.Status = "200 OK"
        r.Status = 200
        req.HTTPRequest = r
        resp.SecretString = new(String("this is the dummy value"))
    
        return
    }
    

    If you need the mock values to be mock like "real" data from a live API service then write a quick program to do a call and Printf the return values using "%#v" format. This should give you most of what you need

    评论

报告相同问题?

悬赏问题

  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题