provider/aws: Consolidate IAM Account Alias tests (#14316)

* provider/aws: combine aws alias tests

* tweaks

* fix up

* uncomment
This commit is contained in:
Clint 2017-05-09 11:12:07 -05:00 committed by GitHub
parent 8d91080af8
commit 88c2e29f91
4 changed files with 80 additions and 47 deletions

View File

@ -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")

View File

@ -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" { }
`

View File

@ -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)

View File

@ -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)
}