From 88c2e29f91eaa60a6edca0cb0840633ea777d585 Mon Sep 17 00:00:00 2001 From: Clint Date: Tue, 9 May 2017 11:12:07 -0500 Subject: [PATCH] provider/aws: Consolidate IAM Account Alias tests (#14316) * provider/aws: combine aws alias tests * tweaks * fix up * uncomment --- .../aws/data_source_aws_iam_account_alias.go | 1 + .../data_source_aws_iam_account_alias_test.go | 43 ---------- .../aws/import_aws_iam_account_alias_test.go | 2 +- .../resource_aws_iam_account_alias_test.go | 81 ++++++++++++++++++- 4 files changed, 80 insertions(+), 47 deletions(-) delete mode 100644 builtin/providers/aws/data_source_aws_iam_account_alias_test.go diff --git a/builtin/providers/aws/data_source_aws_iam_account_alias.go b/builtin/providers/aws/data_source_aws_iam_account_alias.go index c8b8378cf..f93897373 100644 --- a/builtin/providers/aws/data_source_aws_iam_account_alias.go +++ b/builtin/providers/aws/data_source_aws_iam_account_alias.go @@ -34,6 +34,7 @@ func dataSourceAwsIamAccountAliasRead(d *schema.ResourceData, meta interface{}) if err != nil { return err } + // 'AccountAliases': [] if there is no alias. if resp == nil || len(resp.AccountAliases) == 0 { return fmt.Errorf("no IAM account alias found") diff --git a/builtin/providers/aws/data_source_aws_iam_account_alias_test.go b/builtin/providers/aws/data_source_aws_iam_account_alias_test.go deleted file mode 100644 index a23a6a93e..000000000 --- a/builtin/providers/aws/data_source_aws_iam_account_alias_test.go +++ /dev/null @@ -1,43 +0,0 @@ -package aws - -import ( - "fmt" - "testing" - - "github.com/hashicorp/terraform/helper/resource" - "github.com/hashicorp/terraform/terraform" -) - -func TestAccAWSIamAccountAlias_basic(t *testing.T) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: testAccCheckAwsIamAccountAliasConfig_basic, - Check: resource.ComposeTestCheckFunc( - testAccCheckAwsIamAccountAlias("data.aws_iam_account_alias.current"), - ), - }, - }, - }) -} - -func testAccCheckAwsIamAccountAlias(n string) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] - if !ok { - return fmt.Errorf("Can't find Account Alias resource: %s", n) - } - - if rs.Primary.Attributes["account_alias"] == "" { - return fmt.Errorf("Missing Account Alias") - } - - return nil - } -} - -const testAccCheckAwsIamAccountAliasConfig_basic = ` -data "aws_iam_account_alias" "current" { } -` diff --git a/builtin/providers/aws/import_aws_iam_account_alias_test.go b/builtin/providers/aws/import_aws_iam_account_alias_test.go index e2d00b68c..28829a419 100644 --- a/builtin/providers/aws/import_aws_iam_account_alias_test.go +++ b/builtin/providers/aws/import_aws_iam_account_alias_test.go @@ -7,7 +7,7 @@ import ( "github.com/hashicorp/terraform/helper/resource" ) -func TestAccAWSIAMAccountAlias_importBasic(t *testing.T) { +func testAccAWSIAMAccountAlias_importBasic(t *testing.T) { resourceName := "aws_iam_account_alias.test" rstring := acctest.RandString(5) diff --git a/builtin/providers/aws/resource_aws_iam_account_alias_test.go b/builtin/providers/aws/resource_aws_iam_account_alias_test.go index 7106566a2..455af4a97 100644 --- a/builtin/providers/aws/resource_aws_iam_account_alias_test.go +++ b/builtin/providers/aws/resource_aws_iam_account_alias_test.go @@ -11,7 +11,30 @@ import ( "github.com/hashicorp/terraform/terraform" ) -func TestAccAWSIAMAccountAlias_basic(t *testing.T) { +func TestAccAWSIAMAccountAlias(t *testing.T) { + testCases := map[string]map[string]func(t *testing.T){ + "Basic": { + "basic": testAccAWSIAMAccountAlias_basic_with_datasource, + }, + "Import": { + "import": testAccAWSIAMAccountAlias_importBasic, + }, + } + + for group, m := range testCases { + m := m + t.Run(group, func(t *testing.T) { + for name, tc := range m { + tc := tc + t.Run(name, func(t *testing.T) { + tc(t) + }) + } + }) + } +} + +func testAccAWSIAMAccountAlias_basic_with_datasource(t *testing.T) { var account_alias string rstring := acctest.RandString(5) @@ -27,6 +50,18 @@ func TestAccAWSIAMAccountAlias_basic(t *testing.T) { testAccCheckAWSIAMAccountAliasExists("aws_iam_account_alias.test", &account_alias), ), }, + { + Config: testAccAWSIAMAccountAliasConfig_with_datasource(rstring), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSIAMAccountAliasExists("aws_iam_account_alias.test", &account_alias), + testAccCheckAWSIAMAccountAliasDataExists("data.aws_iam_account_alias.current", &account_alias), + ), + // We expect a non-empty plan due to the way data sources and depends_on + // work, or don't work. See https://github.com/hashicorp/terraform/issues/11139#issuecomment-275121893 + // We accept this limitation and feel this test is OK because of the + // explicity check above + ExpectNonEmptyPlan: true, + }, }, }) } @@ -56,6 +91,21 @@ func testAccCheckAWSIAMAccountAliasDestroy(s *terraform.State) error { } +func testAccCheckAWSIAMAccountAliasDataExists(n string, a *string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.Attributes["account_alias"] != *a { + return fmt.Errorf("Data Source account_alias didn't match, expected (%s), got (%s)", *a, rs.Primary.Attributes["account_alias"]) + } + + return nil + } +} + func testAccCheckAWSIAMAccountAliasExists(n string, a *string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -82,10 +132,35 @@ func testAccCheckAWSIAMAccountAliasExists(n string, a *string) resource.TestChec } } -func testAccAWSIAMAccountAliasConfig(rstring string) string { +func testAccCheckAwsIamAccountAlias(n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Can't find Account Alias resource: %s", n) + } + + if rs.Primary.Attributes["account_alias"] == "" { + return fmt.Errorf("Missing Account Alias") + } + + return nil + } +} + +func testAccAWSIAMAccountAliasConfig_with_datasource(rstring string) string { return fmt.Sprintf(` resource "aws_iam_account_alias" "test" { account_alias = "terraform-%s-alias" } -`, rstring) + +data "aws_iam_account_alias" "current" { + depends_on = ["aws_iam_account_alias.test"] +}`, rstring) +} + +func testAccAWSIAMAccountAliasConfig(rstring string) string { + return fmt.Sprintf(` +resource "aws_iam_account_alias" "test" { + account_alias = "terraform-%s-alias" +}`, rstring) }