Skip to content

Commit

Permalink
Collecting account id after provider is "ready".
Browse files Browse the repository at this point in the history
  • Loading branch information
Brad Sickles committed Jun 28, 2016
1 parent 7837786 commit ef77f92
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions builtin/providers/aws/resource_aws_ami_launch_permission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,28 @@ type testLaunchPermission struct {

func TestAccAWSAMILaunchPermission_Basic(t *testing.T) {
tlp := &testLaunchPermission{}
getAccountId := func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).stsconn
res, err := conn.GetCallerIdentity(&sts.GetCallerIdentityInput{})
if err != nil {
return fmt.Errorf("could not initialize ami launch permission test: %s", err)
}
tlp.AccountID = *res.Account
if tlp.AccountID == "" {
return fmt.Errorf("could not get account id for launch permission test: %s", err)
}
return nil
}

r.Test(t, r.TestCase{
PreCheck: func() {
testAccPreCheck(t)
conn := testAccProvider.Meta().(*AWSClient).stsconn
res, err := conn.GetCallerIdentity(&sts.GetCallerIdentityInput{})
if err != nil {
t.Fatalf("could not initialize ami launch permission test: %s", err)
}
tlp.AccountID = *res.Account
},
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []r.TestStep{
// First step is a way to get the aws provider initialized so we can use its account id
r.TestStep{
Config: testAccAWSAMILaunchPermissionConfig(nil),
Check: r.ComposeTestCheckFunc(getAccountId),
},
r.TestStep{
Config: testAccAWSAMILaunchPermissionConfig(tlp),
Check: r.ComposeTestCheckFunc(
Expand Down Expand Up @@ -82,7 +92,7 @@ func testAccAWSAMILaunchPermissionDestroyed(tlp *testLaunchPermission) r.TestChe
}

func testAccAWSAMILaunchPermissionConfig(tlp *testLaunchPermission) string {
return fmt.Sprintf(`
base := `
provider "aws" {
region = "us-east-1"
}
Expand Down Expand Up @@ -127,6 +137,13 @@ resource "aws_ami_from_instance" "test" {
description = "Testing Terraform aws_ami_from_instance resource"
source_instance_id = "${aws_instance.test.id}"
}
`

if tlp == nil {
return base
}

return base + fmt.Sprintf(`
resource "aws_ami_launch_permission" "self-test" {
image_id = "${aws_ami_from_instance.test.id}"
Expand Down

0 comments on commit ef77f92

Please sign in to comment.