From ef77f928495a271d508b7973ad434acabcdee1e1 Mon Sep 17 00:00:00 2001 From: Brad Sickles Date: Tue, 28 Jun 2016 09:27:54 -0400 Subject: [PATCH] Collecting account id after provider is "ready". --- ...resource_aws_ami_launch_permission_test.go | 37 ++++++++++++++----- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/builtin/providers/aws/resource_aws_ami_launch_permission_test.go b/builtin/providers/aws/resource_aws_ami_launch_permission_test.go index 4f67d48fb6c4..ed4f0cd4975e 100644 --- a/builtin/providers/aws/resource_aws_ami_launch_permission_test.go +++ b/builtin/providers/aws/resource_aws_ami_launch_permission_test.go @@ -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( @@ -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" } @@ -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}"