Sara Fuerst
Sara Fuerst

Reputation: 6088

Is it possible to unit test this Go function that uses the AWS SDK without changing the parameters?

I am new to Go and have written a function that uses the AWS Secrets Manager to fetch a secret:

//Helper function to get secret from AWS Secret Manager
func getAWSSecrets() (secretMap map[string]string, err error) {
    // Create new AWS session in order to get db info from SecretsManager
    sess, err := session.NewSession()
    if err != nil {
        return nil, err
    }

    // Create a new instance of the SecretsManager client with session
    svc := secretsmanager.New(sess)

    //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
    }

    ...
}

I need to create a unit test for the function, and to do so I need to mock the AWS Secrets Manager. I discovered a Secrets Manager Interface that AWS was created to help with unit testing. In the example displayed, the AWS Secrets Manager is passed into the function being tested, making it easy to pass in the mock service. Is this the only way to successfully unit test the function? Or can the service be mocked in the function I have above?

Upvotes: 2

Views: 2977

Answers (1)

Carlos Frias
Carlos Frias

Reputation: 533

As the comments say, make the function call a method, and take advantage of the interface AWS is providing.

I would create a service, like this one:

package service

type SecretService struct {
    AwsProvider aws.SecretsManagerAPI
}

func NewSecretService() (*SecretService, err) {
    // Create new AWS session in order to get db info from SecretsManager
    sess, err := session.NewSession()
    if err != nil {
        return nil, err
    }

    return &SecretService{
        AwsProvider: secretsmanager.New(sess),
    }, nil
}

func (s *SecretService) GetAWSSecrets() {
    req, resp := s.AwsProvider.GetSecretValueRequest(&secretsmanager.GetSecretValueInput{
        SecretId: aws.String("my/secret/string"),
    })

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

    // More magic below ...
}

That way in the tests, I could pass any mock into SecretService, like this:

func TestSecretService_GetAWSSecrets(t *testing.T) {
  service := &service.SecretService{
    AwsProvider: <use your mock here>
  }
}

One caveat, I guess is that the mock has to implement all methods of SecretsManagerAPI, which I think it's a lot of work for this simple scenario... in any case you can create your own interface inside the service package with only the subset of methods you'll use, let's say you're only going to use GetSecretValueRequest, and CreateSecret:

package service

type SecretProvider interface {
    CreateSecret(*secretsmanager.CreateSecretInput) (*secretsmanager.CreateSecretOutput, error)
    GetSecretValueRequest(*secretsmanager.GetSecretValueInput) (*request.Request, *secretsmanager.GetSecretValueOutput)
}

Change the service:

type SecretService struct {
    AwsProvider SecretProvider
}

Now your mock only has to implement SecretProvider interface methods only. And of course, AWS SecretsManager implicitly impleements SecretProvider.

Upvotes: 2

Related Questions