Skip to content

Commit

Permalink
(MINOR) improve R53 functions
Browse files Browse the repository at this point in the history
## CHANGES

* rename the `AssertRecordExistsInHostedZone` to `AssertRoute53RecordExistsInHostedZone` for consistency
* ensure that a `context.Context` is properly passed to `ListHostedZonesByName`, as is required by https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/route53#Client.ListHostedZonesByName
  • Loading branch information
mdb authored Dec 21, 2021
1 parent 12e6c31 commit 632881c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ enable testing across technologies such as:
* EC2
* IAM
* RDS
* Route53
* S3
* DynamoDB
* Databases such as:
Expand Down Expand Up @@ -119,4 +120,4 @@ Contributions are welcome! Please follow this process:
* Create a new branch off of `main` in your repository; **we do not accept contributions off of forks' `main` branches** (it also makes merging future changes from our repository to yours more difficult).
* Make your changes and commit them, including updates to any tests.
* Run the testing suite on at least any of the packages that you updated or added.
* Open a pull request back to our repository. Please reference any issue that you are fixing.
* Open a pull request back to our repository. Please reference any issue that you are fixing.
16 changes: 8 additions & 8 deletions pkg/aws/route53.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import (
// Route53Client is an AWS Route53 API client.
// Typically, it's a [Route53](https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/route53#Client).
type Route53Client interface {
ListHostedZonesByNameInput(*route53.ListHostedZonesByNameInput) (*route53.ListHostedZonesOutput, error)
ListHostedZonesByName(context.Context, *route53.ListHostedZonesByNameInput) (*route53.ListHostedZonesOutput, error)
ListResourceRecordSets(context.Context, *route53.ListResourceRecordSetsInput) (*route53.ListResourceRecordSetsOutput, error)
}

// AssertRoute53HostedZoneExists asserts whether or not the Route53 zone name
// it's passed is found amongst those reported by the AWS API.
func AssertRoute53HostedZoneExists(t *testing.T, client Route53Client, zoneName string) {
_, found, err := findZoneE(client, zoneName)
func AssertRoute53HostedZoneExists(t *testing.T, ctx context.Context, client Route53Client, zoneName string) {
_, found, err := findZoneE(ctx, client, zoneName)

assert.Nil(t, err)
assert.True(t, found, fmt.Sprintf("'%s' not found", zoneName))
Expand All @@ -41,15 +41,15 @@ type AssertRecordInput struct {
ZoneName string
}

// AssertRecordExistsInHostedZone asserts whether or not the Route53 record
// AssertRoute53RecordExistsInHostedZone asserts whether or not the Route53 record
// name it's passed exists amongst those associated with the the Route53 zone whose
// name it's passed.
func AssertRecordExistsInHostedZone(t *testing.T, ctx context.Context, client Route53Client, recordInput AssertRecordInput) {
func AssertRoute53RecordExistsInHostedZone(t *testing.T, ctx context.Context, client Route53Client, recordInput AssertRecordInput) {
recordFound := false
zoneName := recordInput.ZoneName
recordName := recordInput.RecordName

z, zoneFound, err := findZoneE(client, zoneName)
z, zoneFound, err := findZoneE(ctx, client, zoneName)

assert.Nil(t, err)
assert.True(t, zoneFound, fmt.Sprintf("zone '%s' not found", zoneName))
Expand All @@ -74,8 +74,8 @@ func AssertRecordExistsInHostedZone(t *testing.T, ctx context.Context, client Ro
assert.True(t, recordFound, fmt.Sprintf("record '%s' not found", recordName))
}

func findZoneE(client Route53Client, zoneName string) (*types.HostedZone, bool, error) {
zones, err := client.ListHostedZonesByNameInput(&route53.ListHostedZonesByNameInput{
func findZoneE(ctx context.Context, client Route53Client, zoneName string) (*types.HostedZone, bool, error) {
zones, err := client.ListHostedZonesByName(ctx, &route53.ListHostedZonesByNameInput{
DNSName: &zoneName,
})
if err != nil {
Expand Down
38 changes: 19 additions & 19 deletions pkg/aws/route53_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Route53ClientMock struct {
listResourceRecordSetsErr error
}

func (c Route53ClientMock) ListHostedZonesByNameInput(input *route53.ListHostedZonesByNameInput) (*route53.ListHostedZonesOutput, error) {
func (c Route53ClientMock) ListHostedZonesByName(ctx context.Context, input *route53.ListHostedZonesByNameInput) (*route53.ListHostedZonesOutput, error) {
return c.listHostedZonesOutput, c.listHostedZonesErr
}

Expand All @@ -35,9 +35,9 @@ func TestAssertRoute53HostedZoneExists_NotFound(t *testing.T) {
listHostedZonesOutput: &route53.ListHostedZonesOutput{},
listHostedZonesErr: nil,
}
AssertRoute53HostedZoneExists(fakeTest, client, "bar.com")
AssertRoute53HostedZoneExists(fakeTest, context.Background(), client, "bar.com")

assert.True(t, fakeTest.Failed(), "expected AssertRoute53HostedZoneExists to fail")
assert.True(t, fakeTest.Failed(), "expected AssertHostedZoneExists to fail")
}

func TestAssertRoute53HostedZoneExists_Error(t *testing.T) {
Expand All @@ -46,9 +46,9 @@ func TestAssertRoute53HostedZoneExists_Error(t *testing.T) {
listHostedZonesOutput: &route53.ListHostedZonesOutput{},
listHostedZonesErr: errors.New("some error"),
}
AssertRoute53HostedZoneExists(fakeTest, client, "foo.com")
AssertRoute53HostedZoneExists(fakeTest, context.Background(), client, "foo.com")

assert.True(t, fakeTest.Failed(), "expected AssertRoute53HostedZoneExists to fail")
assert.True(t, fakeTest.Failed(), "expected AssertHostedZoneExists to fail")
}

func TestAssertRoute53HostedZoneExists_Found(t *testing.T) {
Expand All @@ -64,12 +64,12 @@ func TestAssertRoute53HostedZoneExists_Found(t *testing.T) {
},
listHostedZonesErr: nil,
}
AssertRoute53HostedZoneExists(fakeTest, client, name)
AssertRoute53HostedZoneExists(fakeTest, context.Background(), client, name)

assert.False(t, fakeTest.Failed(), "expected AssertRoute53HostedZoneExists to pass")
assert.False(t, fakeTest.Failed(), "expected AssertHostedZoneExists to pass")
}

func TestAssertRecordExistsInHostedZone_Found(t *testing.T) {
func TestAssertRoute53RecordExistsInHostedZone_Found(t *testing.T) {
fakeTest := &testing.T{}
zoneName := "foo.com"
recordName := fmt.Sprintf("foo.%s", zoneName)
Expand All @@ -92,15 +92,15 @@ func TestAssertRecordExistsInHostedZone_Found(t *testing.T) {
listResourceRecordSetsErr: nil,
}

AssertRecordExistsInHostedZone(fakeTest, context.Background(), client, AssertRecordInput{
AssertRoute53RecordExistsInHostedZone(fakeTest, context.Background(), client, AssertRecordInput{
RecordName: recordName,
ZoneName: zoneName,
})

assert.False(t, fakeTest.Failed(), "expected AssertRecordExistsInZone to pass")
}

func TestAssertRecordExistsInHostedZone_RecordNotFound(t *testing.T) {
func TestAssertRoute53RecordExistsInHostedZone_RecordNotFound(t *testing.T) {
fakeTest := &testing.T{}
zoneName := "foo.com"
recordName := fmt.Sprintf("foo.%s", zoneName)
Expand All @@ -119,15 +119,15 @@ func TestAssertRecordExistsInHostedZone_RecordNotFound(t *testing.T) {
listResourceRecordSetsErr: nil,
}

AssertRecordExistsInHostedZone(fakeTest, context.Background(), client, AssertRecordInput{
AssertRoute53RecordExistsInHostedZone(fakeTest, context.Background(), client, AssertRecordInput{
RecordName: recordName,
ZoneName: zoneName,
})

assert.True(t, fakeTest.Failed(), "expected AssertRecordExistsInZone to fail")
}

func TestAssertRecordExistsInHostedZone_RecordTypeNotFound(t *testing.T) {
func TestAssertRoute53RecordExistsInHostedZone_RecordTypeNotFound(t *testing.T) {
fakeTest := &testing.T{}
zoneName := "foo.com"
recordName := fmt.Sprintf("foo.%s", zoneName)
Expand All @@ -151,7 +151,7 @@ func TestAssertRecordExistsInHostedZone_RecordTypeNotFound(t *testing.T) {
listResourceRecordSetsErr: nil,
}

AssertRecordExistsInHostedZone(fakeTest, context.Background(), client, AssertRecordInput{
AssertRoute53RecordExistsInHostedZone(fakeTest, context.Background(), client, AssertRecordInput{
RecordName: recordName,
RecordType: types.RRTypeSoa,
ZoneName: zoneName,
Expand All @@ -160,7 +160,7 @@ func TestAssertRecordExistsInHostedZone_RecordTypeNotFound(t *testing.T) {
assert.True(t, fakeTest.Failed(), "expected AssertRecordExistsInZone to fail")
}

func TestAssertRecordExistsInHostedZone_ListResourceRecordSets_Error(t *testing.T) {
func TestAssertRoute53RecordExistsInHostedZone_ListResourceRecordSets_Error(t *testing.T) {
fakeTest := &testing.T{}
zoneName := "foo.com"
recordName := fmt.Sprintf("foo.%s", zoneName)
Expand All @@ -179,15 +179,15 @@ func TestAssertRecordExistsInHostedZone_ListResourceRecordSets_Error(t *testing.
listResourceRecordSetsErr: errors.New("some error"),
}

AssertRecordExistsInHostedZone(fakeTest, context.Background(), client, AssertRecordInput{
AssertRoute53RecordExistsInHostedZone(fakeTest, context.Background(), client, AssertRecordInput{
RecordName: recordName,
ZoneName: zoneName,
})

assert.True(t, fakeTest.Failed(), "expected AssertRecordExistsInZone to fail")
}

func TestAssertRecordExistsInHostedZone_ZoneNotFound(t *testing.T) {
func TestAssertRoute53RecordExistsInHostedZone_ZoneNotFound(t *testing.T) {
fakeTest := &testing.T{}
zoneName := "foo.com"
recordName := fmt.Sprintf("foo.%s", zoneName)
Expand All @@ -206,15 +206,15 @@ func TestAssertRecordExistsInHostedZone_ZoneNotFound(t *testing.T) {
listResourceRecordSetsErr: nil,
}

AssertRecordExistsInHostedZone(fakeTest, context.Background(), client, AssertRecordInput{
AssertRoute53RecordExistsInHostedZone(fakeTest, context.Background(), client, AssertRecordInput{
RecordName: recordName,
ZoneName: zoneName,
})

assert.True(t, fakeTest.Failed(), "expected AssertRecordExistsInZone to fail")
}

func TestAssertRecordExistsInHostedZone_ListHostedZonesByNameInput_Error(t *testing.T) {
func TestAssertRoute53RecordExistsInHostedZone_ListHostedZonesByNameInput_Error(t *testing.T) {
fakeTest := &testing.T{}
zoneName := "foo.com"
recordName := fmt.Sprintf("foo.%s", zoneName)
Expand All @@ -237,7 +237,7 @@ func TestAssertRecordExistsInHostedZone_ListHostedZonesByNameInput_Error(t *test
listResourceRecordSetsErr: nil,
}

AssertRecordExistsInHostedZone(fakeTest, context.Background(), client, AssertRecordInput{
AssertRoute53RecordExistsInHostedZone(fakeTest, context.Background(), client, AssertRecordInput{
RecordName: recordName,
ZoneName: zoneName,
})
Expand Down

0 comments on commit 632881c

Please sign in to comment.