From 7d41efe42b3f46b9ea77e520a0fee28b66103aec Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Wed, 18 Nov 2020 23:15:55 -0500 Subject: [PATCH] tests/resource/aws_lightsail_domain: Remove hardcoded region and standardize disappears testing Reference: https://github.com/hashicorp/terraform-provider-aws/issues/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) ``` --- aws/lightsail_domain_test.go | 90 +++++++++++++++++++++++ aws/resource_aws_lightsail_domain_test.go | 49 +++++------- 2 files changed, 108 insertions(+), 31 deletions(-) create mode 100644 aws/lightsail_domain_test.go diff --git a/aws/lightsail_domain_test.go b/aws/lightsail_domain_test.go new file mode 100644 index 00000000000..4a5fe44f521 --- /dev/null +++ b/aws/lightsail_domain_test.go @@ -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 +} diff --git a/aws/resource_aws_lightsail_domain_test.go b/aws/resource_aws_lightsail_domain_test.go index 7aea0ba3930..d152cb72989 100644 --- a/aws/resource_aws_lightsail_domain_test.go +++ b/aws/resource_aws_lightsail_domain_test.go @@ -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), ), }, }, @@ -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, }, @@ -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), @@ -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), @@ -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)) }