Skip to content

Commit

Permalink
tests/resource/aws_lightsail_domain: Remove hardcoded region and stan…
Browse files Browse the repository at this point in the history
…dardize disappears testing

Reference: #16302

Output from acceptance testing in AWS Commercial:

```
--- PASS: TestAccAWSLightsailDomain_disappears (10.22s)
--- PASS: TestAccAWSLightsailDomain_basic (10.27s)
```

Output from acceptance testing in AWS GovCloud (US):

```
--- SKIP: TestAccAWSLightsailDomain_basic (1.70s)
--- SKIP: TestAccAWSLightsailDomain_disappears (1.70s)
```
  • Loading branch information
bflad committed Nov 19, 2020
1 parent 746cc9c commit 7d41efe
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 31 deletions.
90 changes: 90 additions & 0 deletions aws/lightsail_domain_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package aws

import (
"context"
"sync"
"testing"

"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/service/lightsail"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

// Lightsail Domains can only be created in specific regions.

// testAccLightsailDomainRegion is the chosen Lightsail Domains testing region
//
// Cached to prevent issues should multiple regions become available.
var testAccLightsailDomainRegion string

// testAccProviderLightsailDomain is the Lightsail Domains provider instance
//
// This Provider can be used in testing code for API calls without requiring
// the use of saving and referencing specific ProviderFactories instances.
//
// testAccPreCheckLightsailDomain(t) must be called before using this provider instance.
var testAccProviderLightsailDomain *schema.Provider

// testAccProviderLightsailDomainConfigure ensures the provider is only configured once
var testAccProviderLightsailDomainConfigure sync.Once

// Prevent panic with testAccCheckResourceDisappears
func init() {
testAccProviderLightsailDomain = Provider()
}

// testAccPreCheckLightsailDomain verifies AWS credentials and that Lightsail Domains is supported
func testAccPreCheckLightsailDomain(t *testing.T) {
testAccPartitionHasServicePreCheck(lightsail.EndpointsID, t)

// Since we are outside the scope of the Terraform configuration we must
// call Configure() to properly initialize the provider configuration.
testAccProviderLightsailDomainConfigure.Do(func() {
region := testAccGetLightsailDomainRegion()

if region == "" {
t.Skip("Lightsail Domains not available in this AWS Partition")
}

config := map[string]interface{}{
"region": region,
}

diags := testAccProviderLightsailDomain.Configure(context.Background(), terraform.NewResourceConfigRaw(config))

if diags != nil && diags.HasError() {
for _, d := range diags {
if d.Severity == diag.Error {
t.Fatalf("error configuring Lightsail Domains provider: %s", d.Summary)
}
}
}
})
}

// testAccLightsailDomainRegionProviderConfig is the Terraform provider configuration for Lightsail Domains region testing
//
// Testing Lightsail Domains assumes no other provider configurations
// are necessary and overwrites the "aws" provider configuration.
func testAccLightsailDomainRegionProviderConfig() string {
return testAccRegionalProviderConfig(testAccGetLightsailDomainRegion())
}

// testAccGetLightsailDomainRegion returns the Lightsail Domains region for testing
func testAccGetLightsailDomainRegion() string {
if testAccLightsailDomainRegion != "" {
return testAccLightsailDomainRegion
}

// AWS Commercial: https://lightsail.aws.amazon.com/ls/docs/en_us/articles/lightsail-how-to-create-dns-entry
// AWS GovCloud (US) - service not supported: https://docs.aws.amazon.com/govcloud-us/latest/UserGuide/using-services.html
// AWS China - service not supported: https://www.amazonaws.cn/en/about-aws/regional-product-services/
switch testAccGetPartition() {
case endpoints.AwsPartitionID:
testAccLightsailDomainRegion = endpoints.UsEast1RegionID
}

return testAccLightsailDomainRegion
}
49 changes: 18 additions & 31 deletions aws/resource_aws_lightsail_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ import (
func TestAccAWSLightsailDomain_basic(t *testing.T) {
var domain lightsail.Domain
lightsailDomainName := fmt.Sprintf("tf-test-lightsail-%s.com", acctest.RandString(5))
resourceName := "aws_lightsail_domain.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSLightsail(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSLightsailDomainDestroy,
PreCheck: func() { testAccPreCheck(t); testAccPreCheckLightsailDomain(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckAWSLightsailDomainDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLightsailDomainConfig_basic(lightsailDomainName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSLightsailDomainExists("aws_lightsail_domain.domain_test", &domain),
testAccCheckAWSLightsailDomainExists(resourceName, &domain),
),
},
},
Expand All @@ -35,30 +36,18 @@ func TestAccAWSLightsailDomain_basic(t *testing.T) {
func TestAccAWSLightsailDomain_disappears(t *testing.T) {
var domain lightsail.Domain
lightsailDomainName := fmt.Sprintf("tf-test-lightsail-%s.com", acctest.RandString(5))

domainDestroy := func(*terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).lightsailconn
_, err := conn.DeleteDomain(&lightsail.DeleteDomainInput{
DomainName: aws.String(lightsailDomainName),
})

if err != nil {
return fmt.Errorf("Error deleting Lightsail Domain in disapear test")
}

return nil
}
resourceName := "aws_lightsail_domain.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSLightsail(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSLightsailDomainDestroy,
PreCheck: func() { testAccPreCheck(t); testAccPreCheckLightsailDomain(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckAWSLightsailDomainDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLightsailDomainConfig_basic(lightsailDomainName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSLightsailDomainExists("aws_lightsail_domain.domain_test", &domain),
domainDestroy,
testAccCheckAWSLightsailDomainExists(resourceName, &domain),
testAccCheckResourceDisappears(testAccProviderLightsailDomain, resourceAwsLightsailDomain(), resourceName),
),
ExpectNonEmptyPlan: true,
},
Expand All @@ -77,7 +66,7 @@ func testAccCheckAWSLightsailDomainExists(n string, domain *lightsail.Domain) re
return errors.New("No Lightsail Domain ID is set")
}

conn := testAccProvider.Meta().(*AWSClient).lightsailconn
conn := testAccProviderLightsailDomain.Meta().(*AWSClient).lightsailconn

resp, err := conn.GetDomain(&lightsail.GetDomainInput{
DomainName: aws.String(rs.Primary.ID),
Expand All @@ -101,7 +90,7 @@ func testAccCheckAWSLightsailDomainDestroy(s *terraform.State) error {
continue
}

conn := testAccProvider.Meta().(*AWSClient).lightsailconn
conn := testAccProviderLightsailDomain.Meta().(*AWSClient).lightsailconn

resp, err := conn.GetDomain(&lightsail.GetDomainInput{
DomainName: aws.String(rs.Primary.ID),
Expand All @@ -126,13 +115,11 @@ func testAccCheckAWSLightsailDomainDestroy(s *terraform.State) error {
}

func testAccAWSLightsailDomainConfig_basic(lightsailDomainName string) string {
return fmt.Sprintf(`
provider "aws" {
region = "us-east-1"
}
resource "aws_lightsail_domain" "domain_test" {
return composeConfig(
testAccLightsailDomainRegionProviderConfig(),
fmt.Sprintf(`
resource "aws_lightsail_domain" "test" {
domain_name = "%s"
}
`, lightsailDomainName) //lintignore:AT004
`, lightsailDomainName))
}

0 comments on commit 7d41efe

Please sign in to comment.