From 7f5c687a9c0aaaab41db0d288e11b78450c9257d Mon Sep 17 00:00:00 2001 From: Ryn Daniels Date: Wed, 23 Oct 2019 15:17:28 +0200 Subject: [PATCH] Import test refactor for cloudy resources --- ...esource_aws_cloud9_environment_ec2_test.go | 38 ++-- aws/resource_aws_cloudformation_stack_test.go | 167 +++++++++++------- ..._cloudfront_origin_access_identity_test.go | 61 +++---- 3 files changed, 139 insertions(+), 127 deletions(-) diff --git a/aws/resource_aws_cloud9_environment_ec2_test.go b/aws/resource_aws_cloud9_environment_ec2_test.go index 0399b78edba..3f24b571443 100644 --- a/aws/resource_aws_cloud9_environment_ec2_test.go +++ b/aws/resource_aws_cloud9_environment_ec2_test.go @@ -18,7 +18,6 @@ func TestAccAWSCloud9EnvironmentEc2_basic(t *testing.T) { rString := acctest.RandString(8) envName := fmt.Sprintf("tf_acc_env_basic_%s", rString) uEnvName := fmt.Sprintf("tf_acc_env_basic_updated_%s", rString) - resourceName := "aws_cloud9_environment_ec2.test" resource.ParallelTest(t, resource.TestCase{ @@ -36,6 +35,12 @@ func TestAccAWSCloud9EnvironmentEc2_basic(t *testing.T) { resource.TestMatchResourceAttr(resourceName, "owner_arn", regexp.MustCompile(`^arn:`)), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"instance_type"}, + }, { Config: testAccAWSCloud9EnvironmentEc2Config(uEnvName), Check: resource.ComposeTestCheckFunc( @@ -59,7 +64,6 @@ func TestAccAWSCloud9EnvironmentEc2_allFields(t *testing.T) { description := fmt.Sprintf("Tf Acc Test %s", rString) uDescription := fmt.Sprintf("Tf Acc Test Updated %s", rString) userName := fmt.Sprintf("tf_acc_cloud9_env_%s", rString) - resourceName := "aws_cloud9_environment_ec2.test" resource.ParallelTest(t, resource.TestCase{ @@ -78,6 +82,12 @@ func TestAccAWSCloud9EnvironmentEc2_allFields(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "type", "ec2"), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"instance_type", "automatic_stop_time_minutes", "subnet_id"}, + }, { Config: testAccAWSCloud9EnvironmentEc2AllFieldsConfig(uEnvName, uDescription, userName), Check: resource.ComposeTestCheckFunc( @@ -93,30 +103,6 @@ func TestAccAWSCloud9EnvironmentEc2_allFields(t *testing.T) { }) } -func TestAccAWSCloud9EnvironmentEc2_importBasic(t *testing.T) { - rString := acctest.RandString(8) - name := fmt.Sprintf("tf_acc_api_doc_part_import_%s", rString) - - resourceName := "aws_cloud9_environment_ec2.test" - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSCloud9(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckAWSCloud9EnvironmentEc2Destroy, - Steps: []resource.TestStep{ - { - Config: testAccAWSCloud9EnvironmentEc2Config(name), - }, - { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"instance_type"}, - }, - }, - }) -} - func testAccCheckAWSCloud9EnvironmentEc2Exists(n string, res *cloud9.Environment) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] diff --git a/aws/resource_aws_cloudformation_stack_test.go b/aws/resource_aws_cloudformation_stack_test.go index bc0d3c52274..9431e13c8cb 100644 --- a/aws/resource_aws_cloudformation_stack_test.go +++ b/aws/resource_aws_cloudformation_stack_test.go @@ -11,10 +11,10 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/terraform" ) -func TestAccAWSCloudFormationStack_importBasic(t *testing.T) { +func TestAccAWSCloudFormationStack_basic(t *testing.T) { + var stack cloudformation.Stack stackName := fmt.Sprintf("tf-acc-test-basic-%s", acctest.RandString(10)) - - resourceName := "aws_cloudformation_stack.network" + resourceName := "aws_cloudformation_stack.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -23,6 +23,9 @@ func TestAccAWSCloudFormationStack_importBasic(t *testing.T) { Steps: []resource.TestStep{ { Config: testAccAWSCloudFormationStackConfig(stackName), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudFormationStackExists(resourceName, &stack), + ), }, { ResourceName: resourceName, @@ -33,28 +36,10 @@ func TestAccAWSCloudFormationStack_importBasic(t *testing.T) { }) } -func TestAccAWSCloudFormationStack_basic(t *testing.T) { - var stack cloudformation.Stack - stackName := fmt.Sprintf("tf-acc-test-basic-%s", acctest.RandString(10)) - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckAWSCloudFormationDestroy, - Steps: []resource.TestStep{ - { - Config: testAccAWSCloudFormationStackConfig(stackName), - Check: resource.ComposeTestCheckFunc( - testAccCheckCloudFormationStackExists("aws_cloudformation_stack.network", &stack), - ), - }, - }, - }) -} - func TestAccAWSCloudFormationStack_disappears(t *testing.T) { var stack cloudformation.Stack stackName := fmt.Sprintf("tf-acc-test-basic-%s", acctest.RandString(10)) + resourceName := "aws_cloudformation_stack.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -64,7 +49,7 @@ func TestAccAWSCloudFormationStack_disappears(t *testing.T) { { Config: testAccAWSCloudFormationStackConfig(stackName), Check: resource.ComposeTestCheckFunc( - testAccCheckCloudFormationStackExists("aws_cloudformation_stack.network", &stack), + testAccCheckCloudFormationStackExists(resourceName, &stack), testAccCheckCloudFormationStackDisappears(&stack), ), ExpectNonEmptyPlan: true, @@ -76,6 +61,7 @@ func TestAccAWSCloudFormationStack_disappears(t *testing.T) { func TestAccAWSCloudFormationStack_yaml(t *testing.T) { var stack cloudformation.Stack stackName := fmt.Sprintf("tf-acc-test-yaml-%s", acctest.RandString(10)) + resourceName := "aws_cloudformation_stack.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -85,9 +71,14 @@ func TestAccAWSCloudFormationStack_yaml(t *testing.T) { { Config: testAccAWSCloudFormationStackConfig_yaml(stackName), Check: resource.ComposeTestCheckFunc( - testAccCheckCloudFormationStackExists("aws_cloudformation_stack.yaml", &stack), + testAccCheckCloudFormationStackExists(resourceName, &stack), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, }, }) } @@ -95,6 +86,7 @@ func TestAccAWSCloudFormationStack_yaml(t *testing.T) { func TestAccAWSCloudFormationStack_defaultParams(t *testing.T) { var stack cloudformation.Stack stackName := fmt.Sprintf("tf-acc-test-default-params-%s", acctest.RandString(10)) + resourceName := "aws_cloudformation_stack.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -104,9 +96,15 @@ func TestAccAWSCloudFormationStack_defaultParams(t *testing.T) { { Config: testAccAWSCloudFormationStackConfig_defaultParams(stackName), Check: resource.ComposeTestCheckFunc( - testAccCheckCloudFormationStackExists("aws_cloudformation_stack.asg-demo", &stack), + testAccCheckCloudFormationStackExists(resourceName, &stack), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"parameters"}, + }, }, }) } @@ -114,8 +112,9 @@ func TestAccAWSCloudFormationStack_defaultParams(t *testing.T) { func TestAccAWSCloudFormationStack_allAttributes(t *testing.T) { var stack cloudformation.Stack stackName := fmt.Sprintf("tf-acc-test-all-attributes-%s", acctest.RandString(10)) - + resourceName := "aws_cloudformation_stack.test" expectedPolicyBody := "{\"Statement\":[{\"Action\":\"Update:*\",\"Effect\":\"Deny\",\"Principal\":\"*\",\"Resource\":\"LogicalResourceId/StaticVPC\"},{\"Action\":\"Update:*\",\"Effect\":\"Allow\",\"Principal\":\"*\",\"Resource\":\"*\"}]}" + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, @@ -124,37 +123,43 @@ func TestAccAWSCloudFormationStack_allAttributes(t *testing.T) { { Config: testAccAWSCloudFormationStackConfig_allAttributesWithBodies(stackName), Check: resource.ComposeTestCheckFunc( - testAccCheckCloudFormationStackExists("aws_cloudformation_stack.full", &stack), - resource.TestCheckResourceAttr("aws_cloudformation_stack.full", "name", stackName), - resource.TestCheckResourceAttr("aws_cloudformation_stack.full", "capabilities.#", "1"), - resource.TestCheckResourceAttr("aws_cloudformation_stack.full", "capabilities.1328347040", "CAPABILITY_IAM"), - resource.TestCheckResourceAttr("aws_cloudformation_stack.full", "disable_rollback", "false"), - resource.TestCheckResourceAttr("aws_cloudformation_stack.full", "notification_arns.#", "1"), - resource.TestCheckResourceAttr("aws_cloudformation_stack.full", "parameters.%", "1"), - resource.TestCheckResourceAttr("aws_cloudformation_stack.full", "parameters.VpcCIDR", "10.0.0.0/16"), - resource.TestCheckResourceAttr("aws_cloudformation_stack.full", "policy_body", expectedPolicyBody), - resource.TestCheckResourceAttr("aws_cloudformation_stack.full", "tags.%", "2"), - resource.TestCheckResourceAttr("aws_cloudformation_stack.full", "tags.First", "Mickey"), - resource.TestCheckResourceAttr("aws_cloudformation_stack.full", "tags.Second", "Mouse"), - resource.TestCheckResourceAttr("aws_cloudformation_stack.full", "timeout_in_minutes", "10"), + testAccCheckCloudFormationStackExists(resourceName, &stack), + resource.TestCheckResourceAttr(resourceName, "name", stackName), + resource.TestCheckResourceAttr(resourceName, "capabilities.#", "1"), + resource.TestCheckResourceAttr(resourceName, "capabilities.1328347040", "CAPABILITY_IAM"), + resource.TestCheckResourceAttr(resourceName, "disable_rollback", "false"), + resource.TestCheckResourceAttr(resourceName, "notification_arns.#", "1"), + resource.TestCheckResourceAttr(resourceName, "parameters.%", "1"), + resource.TestCheckResourceAttr(resourceName, "parameters.VpcCIDR", "10.0.0.0/16"), + resource.TestCheckResourceAttr(resourceName, "policy_body", expectedPolicyBody), + resource.TestCheckResourceAttr(resourceName, "tags.%", "2"), + resource.TestCheckResourceAttr(resourceName, "tags.First", "Mickey"), + resource.TestCheckResourceAttr(resourceName, "tags.Second", "Mouse"), + resource.TestCheckResourceAttr(resourceName, "timeout_in_minutes", "10"), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"on_failure", "parameters", "policy_body"}, + }, { Config: testAccAWSCloudFormationStackConfig_allAttributesWithBodies_modified(stackName), Check: resource.ComposeTestCheckFunc( - testAccCheckCloudFormationStackExists("aws_cloudformation_stack.full", &stack), - resource.TestCheckResourceAttr("aws_cloudformation_stack.full", "name", stackName), - resource.TestCheckResourceAttr("aws_cloudformation_stack.full", "capabilities.#", "1"), - resource.TestCheckResourceAttr("aws_cloudformation_stack.full", "capabilities.1328347040", "CAPABILITY_IAM"), - resource.TestCheckResourceAttr("aws_cloudformation_stack.full", "disable_rollback", "false"), - resource.TestCheckResourceAttr("aws_cloudformation_stack.full", "notification_arns.#", "1"), - resource.TestCheckResourceAttr("aws_cloudformation_stack.full", "parameters.%", "1"), - resource.TestCheckResourceAttr("aws_cloudformation_stack.full", "parameters.VpcCIDR", "10.0.0.0/16"), - resource.TestCheckResourceAttr("aws_cloudformation_stack.full", "policy_body", expectedPolicyBody), - resource.TestCheckResourceAttr("aws_cloudformation_stack.full", "tags.%", "2"), - resource.TestCheckResourceAttr("aws_cloudformation_stack.full", "tags.First", "Mickey"), - resource.TestCheckResourceAttr("aws_cloudformation_stack.full", "tags.Second", "Mouse"), - resource.TestCheckResourceAttr("aws_cloudformation_stack.full", "timeout_in_minutes", "10"), + testAccCheckCloudFormationStackExists(resourceName, &stack), + resource.TestCheckResourceAttr(resourceName, "name", stackName), + resource.TestCheckResourceAttr(resourceName, "capabilities.#", "1"), + resource.TestCheckResourceAttr(resourceName, "capabilities.1328347040", "CAPABILITY_IAM"), + resource.TestCheckResourceAttr(resourceName, "disable_rollback", "false"), + resource.TestCheckResourceAttr(resourceName, "notification_arns.#", "1"), + resource.TestCheckResourceAttr(resourceName, "parameters.%", "1"), + resource.TestCheckResourceAttr(resourceName, "parameters.VpcCIDR", "10.0.0.0/16"), + resource.TestCheckResourceAttr(resourceName, "policy_body", expectedPolicyBody), + resource.TestCheckResourceAttr(resourceName, "tags.%", "2"), + resource.TestCheckResourceAttr(resourceName, "tags.First", "Mickey"), + resource.TestCheckResourceAttr(resourceName, "tags.Second", "Mouse"), + resource.TestCheckResourceAttr(resourceName, "timeout_in_minutes", "10"), ), }, }, @@ -165,6 +170,7 @@ func TestAccAWSCloudFormationStack_allAttributes(t *testing.T) { func TestAccAWSCloudFormationStack_withParams(t *testing.T) { var stack cloudformation.Stack stackName := fmt.Sprintf("tf-acc-test-with-params-%s", acctest.RandString(10)) + resourceName := "aws_cloudformation_stack.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -174,13 +180,19 @@ func TestAccAWSCloudFormationStack_withParams(t *testing.T) { { Config: testAccAWSCloudFormationStackConfig_withParams(stackName), Check: resource.ComposeTestCheckFunc( - testAccCheckCloudFormationStackExists("aws_cloudformation_stack.with_params", &stack), + testAccCheckCloudFormationStackExists(resourceName, &stack), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"on_failure", "parameters"}, + }, { Config: testAccAWSCloudFormationStackConfig_withParams_modified(stackName), Check: resource.ComposeTestCheckFunc( - testAccCheckCloudFormationStackExists("aws_cloudformation_stack.with_params", &stack), + testAccCheckCloudFormationStackExists(resourceName, &stack), ), }, }, @@ -191,6 +203,7 @@ func TestAccAWSCloudFormationStack_withParams(t *testing.T) { func TestAccAWSCloudFormationStack_withUrl_withParams(t *testing.T) { var stack cloudformation.Stack rName := fmt.Sprintf("tf-acc-test-with-url-and-params-%s", acctest.RandString(10)) + resourceName := "aws_cloudformation_stack.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -200,13 +213,19 @@ func TestAccAWSCloudFormationStack_withUrl_withParams(t *testing.T) { { Config: testAccAWSCloudFormationStackConfig_templateUrl_withParams(rName, "tf-cf-stack.json", "11.0.0.0/16"), Check: resource.ComposeTestCheckFunc( - testAccCheckCloudFormationStackExists("aws_cloudformation_stack.with-url-and-params", &stack), + testAccCheckCloudFormationStackExists(resourceName, &stack), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"on_failure", "parameters", "template_url"}, + }, { Config: testAccAWSCloudFormationStackConfig_templateUrl_withParams(rName, "tf-cf-stack.json", "13.0.0.0/16"), Check: resource.ComposeTestCheckFunc( - testAccCheckCloudFormationStackExists("aws_cloudformation_stack.with-url-and-params", &stack), + testAccCheckCloudFormationStackExists(resourceName, &stack), ), }, }, @@ -216,6 +235,7 @@ func TestAccAWSCloudFormationStack_withUrl_withParams(t *testing.T) { func TestAccAWSCloudFormationStack_withUrl_withParams_withYaml(t *testing.T) { var stack cloudformation.Stack rName := fmt.Sprintf("tf-acc-test-with-params-and-yaml-%s", acctest.RandString(10)) + resourceName := "aws_cloudformation_stack.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -223,11 +243,17 @@ func TestAccAWSCloudFormationStack_withUrl_withParams_withYaml(t *testing.T) { CheckDestroy: testAccCheckAWSCloudFormationDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSCloudFormationStackConfig_templateUrl_withParams_withYaml(rName, "tf-cf-stack.yaml", "13.0.0.0/16"), + Config: testAccAWSCloudFormationStackConfig_templateUrl_withParams_withYaml(rName, "tf-cf-stack.test", "13.0.0.0/16"), Check: resource.ComposeTestCheckFunc( - testAccCheckCloudFormationStackExists("aws_cloudformation_stack.with-url-and-params-and-yaml", &stack), + testAccCheckCloudFormationStackExists(resourceName, &stack), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"on_failure", "parameters", "template_url"}, + }, }, }) } @@ -236,6 +262,7 @@ func TestAccAWSCloudFormationStack_withUrl_withParams_withYaml(t *testing.T) { func TestAccAWSCloudFormationStack_withUrl_withParams_noUpdate(t *testing.T) { var stack cloudformation.Stack rName := fmt.Sprintf("tf-acc-test-with-params-no-update-%s", acctest.RandString(10)) + resourceName := "aws_cloudformation_stack.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -245,13 +272,19 @@ func TestAccAWSCloudFormationStack_withUrl_withParams_noUpdate(t *testing.T) { { Config: testAccAWSCloudFormationStackConfig_templateUrl_withParams(rName, "tf-cf-stack-1.json", "11.0.0.0/16"), Check: resource.ComposeTestCheckFunc( - testAccCheckCloudFormationStackExists("aws_cloudformation_stack.with-url-and-params", &stack), + testAccCheckCloudFormationStackExists(resourceName, &stack), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"on_failure", "parameters", "template_url"}, + }, { Config: testAccAWSCloudFormationStackConfig_templateUrl_withParams(rName, "tf-cf-stack-2.json", "11.0.0.0/16"), Check: resource.ComposeTestCheckFunc( - testAccCheckCloudFormationStackExists("aws_cloudformation_stack.with-url-and-params", &stack), + testAccCheckCloudFormationStackExists(resourceName, &stack), ), }, }, @@ -336,7 +369,7 @@ func testAccCheckCloudFormationStackDisappears(stack *cloudformation.Stack) reso func testAccAWSCloudFormationStackConfig(stackName string) string { return fmt.Sprintf(` -resource "aws_cloudformation_stack" "network" { +resource "aws_cloudformation_stack" "test" { name = "%[1]s" template_body = <