Skip to content

Commit

Permalink
provider/aws: Consolidate IAM Account Alias tests (#14316)
Browse files Browse the repository at this point in the history
* provider/aws: combine aws alias tests

* tweaks

* fix up

* uncomment
  • Loading branch information
catsby authored May 9, 2017
1 parent 8d91080 commit 88c2e29
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 47 deletions.
1 change: 1 addition & 0 deletions builtin/providers/aws/data_source_aws_iam_account_alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
43 changes: 0 additions & 43 deletions builtin/providers/aws/data_source_aws_iam_account_alias_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion builtin/providers/aws/import_aws_iam_account_alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
81 changes: 78 additions & 3 deletions builtin/providers/aws/resource_aws_iam_account_alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
},
},
})
}
Expand Down Expand Up @@ -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]
Expand All @@ -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)
}

0 comments on commit 88c2e29

Please sign in to comment.