Skip to content

Instantly share code, notes, and snippets.

@danquack

danquack/main.go Secret

Created December 8, 2021 15:11
Show Gist options
  • Save danquack/a870a0b46f1245cac4a4aef7acfea152 to your computer and use it in GitHub Desktop.
Save danquack/a870a0b46f1245cac4a4aef7acfea152 to your computer and use it in GitHub Desktop.
Mocking AWS SDK
package main
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/organizations"
"github.com/aws/aws-sdk-go/service/organizations/organizationsiface"
)
type Organizations struct {
Client organizationsiface.OrganizationsAPI
}
func (s *Organizations) ListAccounts(in *organizations.ListAccountsInput) (*organizations.ListAccountsOutput, error) {
result, err := s.Client.ListAccounts(in)
return result, err
}
func WhatAreMyAccounts(client *Organizations) (*organizations.ListAccountsOutput, error) {
return client.ListAccounts(
&organizations.ListAccountsInput{
MaxResults: aws.Int64(5),
NextToken: nil,
},
)
}
package main
import (
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/organizations"
"github.com/aws/aws-sdk-go/service/organizations/organizationsiface"
"github.com/stretchr/testify/assert"
)
type MockedOrganizations struct {
organizationsiface.OrganizationsAPI
}
func (m *MockedOrganizations) ListAccounts(in *organizations.ListAccountsInput) (*organizations.ListAccountsOutput, error) {
return &organizations.ListAccountsOutput{
Accounts: []*organizations.Account{
{
Arn: aws.String(""),
Email: aws.String("test1@example.com"),
Id: aws.String("234567890"),
Name: aws.String("test-1"),
},
{
Arn: aws.String(""),
Email: aws.String("test2@example.com"),
Id: aws.String("123456789"),
Name: aws.String("test-2"),
},
},
}, nil
}
func TestListAccounts(t *testing.T) {
test := Organizations{
Client: &MockedOrganizations{},
}
resp, err := WhatAreMyAccounts(&test)
assert.Equal(t, len(resp.Accounts), 2)
assert.NoError(t, err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment