Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Testing Against AWS APIs in Go

Testing Against AWS APIs in Go

Testing code that accesses AWS can seem impossible, but it’s really not that hard! Let’s take a look at how we can use Go’s features to write useful tests for this kind of code, without a whole lot of work.

Stephen R. Scaffidi

July 27, 2019
Tweet

Other Decks in Programming

Transcript

  1. func main() { awsSess, e := awsSession.NewSession() kingpin.FatalIfError(e, "Could not

    create AWS API session") instanceIds := GetAwsInstanceIds(awsSess) for _, id := range instanceIds { fmt.Println(id) } } func GetAwsInstanceIds(awsSess *awsSession.Session) []string { instanceIds := make([]string, 0) ec2Api := ec2.New(awsSess) instDescrs, err := ec2Api.DescribeInstances(&ec2.DescribeInstancesInput{}) kingpin.FatalIfError(err, "Could not get EC2 instances") for _, res := range instDescrs.Reservations { for _, instance := range res.Instances { instanceIds = append(instanceIds, *instance.InstanceId) } } return instanceIds }
  2. func main() { awsSess, e := awsSession.NewSession() kingpin.FatalIfError(e, "Could not

    create AWS API session") instanceIds := GetAwsInstanceIds(awsSess) for _, id := range instanceIds { fmt.Println(id) } } func GetAwsInstanceIds(awsSess *awsSession.Session) []string { instanceIds := make([]string, 0) ec2Api := ec2.New(awsSess) instDescrs, err := ec2Api.DescribeInstances(&ec2.DescribeInstancesInput{}) kingpin.FatalIfError(err, "Could not get EC2 instances") for _, res := range instDescrs.Reservations { for _, instance := range res.Instances { instanceIds = append(instanceIds, *instance.InstanceId) } } return instanceIds }
  3. func main() { awsSess, e := awsSession.NewSession() kingpin.FatalIfError(e, "Could not

    create AWS API session") instanceIds := GetAwsInstanceIds(awsSess) for _, id := range instanceIds { fmt.Println(id) } } func GetAwsInstanceIds(awsSess *awsSession.Session) []string { instanceIds := make([]string, 0) ec2Api := ec2.New(awsSess) instDescrs, err := ec2Api.DescribeInstances(&ec2.DescribeInstancesInput{}) kingpin.FatalIfError(err, "Could not get EC2 instances") for _, res := range instDescrs.Reservations { for _, instance := range res.Instances { instanceIds = append(instanceIds, *instance.InstanceId) } } return instanceIds }
  4. func main() { awsSess, e := awsSession.NewSession() kingpin.FatalIfError(e, "Could not

    create AWS API session") instanceIds := GetAwsInstanceIds(awsSess) for _, id := range instanceIds { fmt.Println(id) } } func GetAwsInstanceIds(awsSess *awsSession.Session) []string { instanceIds := make([]string, 0) ec2Api := ec2.New(awsSess) instDescrs, err := ec2Api.DescribeInstances(&ec2.DescribeInstancesInput{}) kingpin.FatalIfError(err, "Could not get EC2 instances") for _, res := range instDescrs.Reservations { for _, instance := range res.Instances { instanceIds = append(instanceIds, *instance.InstanceId) } } return instanceIds }
  5. func main() { awsSess, e := awsSession.NewSession() kingpin.FatalIfError(e, "Could not

    create AWS API session") ec2Api := ec2.New(awsSess) instanceIds := GetAwsInstanceIds(ec2Api) for _, id := range instanceIds { fmt.Println(id) } } func GetAwsInstanceIds(ec2Api ec2iface.EC2API) []string { instanceIds := make([]string, 0) instDescrs, err := ec2Api.DescribeInstances(&ec2.DescribeInstancesInput{}) kingpin.FatalIfError(err, "Could not get EC2 instances") for _, res := range instDescrs.Reservations { for _, instance := range res.Instances { instanceIds = append(instanceIds, *instance.InstanceId) } } return instanceIds }
  6. func main() { awsSess, e := awsSession.NewSession() kingpin.FatalIfError(e, "Could not

    create AWS API session") ec2Api := ec2.New(awsSess) instanceIds := GetAwsInstanceIds(ec2Api) for _, id := range instanceIds { fmt.Println(id) } } func GetAwsInstanceIds(ec2Api ec2iface.EC2API) []string { instanceIds := make([]string, 0) instDescrs, err := ec2Api.DescribeInstances(&ec2.DescribeInstancesInput{}) kingpin.FatalIfError(err, "Could not get EC2 instances") for _, res := range instDescrs.Reservations { for _, instance := range res.Instances { instanceIds = append(instanceIds, *instance.InstanceId) } } return instanceIds }
  7. func main() { awsSess, e := awsSession.NewSession() kingpin.FatalIfError(e, "Could not

    create AWS API session") ec2Api := ec2.New(awsSess) instanceIds := GetAwsInstanceIds(ec2Api) for _, id := range instanceIds { fmt.Println(id) } } func GetAwsInstanceIds(ec2Api ec2iface.EC2API) []string { instanceIds := make([]string, 0) instDescrs, err := ec2Api.DescribeInstances(&ec2.DescribeInstancesInput{}) kingpin.FatalIfError(err, "Could not get EC2 instances") for _, res := range instDescrs.Reservations { for _, instance := range res.Instances { instanceIds = append(instanceIds, *instance.InstanceId) } } return instanceIds }
  8. func makeInstance(instanceId string) *ec2.Instance { return &ec2.Instance{InstanceId: aws.String(instanceId)} } func

    makeDescribeInstancesOutput(instanceIds []string)
 *ec2.DescribeInstancesOutput { instances := make([]*ec2.Instance, 0) for _, id := range instanceIds { instances = append(instances, makeInstance(id)) } return &ec2.DescribeInstancesOutput{ Reservations: []*ec2.Reservation{ { Instances: instances, }, }, } }
  9. type testAwsEc2API struct { ec2iface.EC2API instanceIds []string } func (d

    testAwsEc2API) DescribeInstances(input *ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error) { return makeDescribeInstancesOutput(d.instanceIds), nil }
  10. type testAwsEc2API struct { ec2iface.EC2API instanceIds []string } func (d

    testAwsEc2API) DescribeInstances(input *ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error) { return makeDescribeInstancesOutput(d.instanceIds), nil }
  11. func Test_GetAwsInstanceIds(t *testing.T) { tests := []struct { name string

    instanceIds []string }{ { name: "get all instance IDs", instanceIds:
 []string{"i-11111", "i-22222", "i-33333"}, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { ec2API := &testAwsEc2API{ instanceIds: test.instanceIds, } gotInstanceIds := GetAwsInstanceIds(ec2API) assert.Equal(t, test.instanceIds, gotInstanceIds) }) } }
  12. func Test_GetAwsInstanceIds(t *testing.T) { tests := []struct { name string

    instanceIds []string }{ { name: "get all instance IDs", instanceIds:
 []string{"i-11111", "i-22222", "i-33333"}, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { ec2API := &testAwsEc2API{ instanceIds: test.instanceIds, } gotInstanceIds := GetAwsInstanceIds(ec2API) assert.Equal(t, test.instanceIds, gotInstanceIds) }) } }