Skip to content

Commit

Permalink
Fixing test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brad Sickles committed Jun 27, 2016
1 parent 0661bd1 commit 375b4a7
Showing 1 changed file with 24 additions and 29 deletions.
53 changes: 24 additions & 29 deletions builtin/providers/aws/resource_aws_ami_launch_permission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package aws

import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/sts"
r "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"testing"
)

type testLaunchPermission struct {
ImageID string
AccountID string
}

func TestAccAWSAMILaunchPermission_Basic(t *testing.T) {
var amiId string
var accountId *string
tlp := &testLaunchPermission{}
r.Test(t, r.TestCase{
PreCheck: func() {
testAccPreCheck(t)
Expand All @@ -21,19 +23,19 @@ func TestAccAWSAMILaunchPermission_Basic(t *testing.T) {
if err != nil {
t.Fatalf("could not initialize ami launch permission test: %s", err)
}
*accountId = *res.Account
tlp.AccountID = *res.Account
},
Providers: testAccProviders,
Steps: []r.TestStep{
r.TestStep{
Config: testAccAWSAMILaunchPermissionConfig(*accountId),
Config: testAccAWSAMILaunchPermissionConfig(tlp),
Check: r.ComposeTestCheckFunc(
testCheckResourceGetAttr("aws_ami_from_instance", "id", &amiId),
testAccAWSAMILaunchPermissionExists(amiId, *accountId),
testCheckResourceGetAttr("aws_ami_from_instance", "id", &tlp.AccountID),
testAccAWSAMILaunchPermissionExists(tlp),
),
},
},
CheckDestroy: testAccAWSAMILaunchPermissionDestroyed(),
CheckDestroy: testAccAWSAMILaunchPermissionDestroyed(tlp),
})
}

Expand All @@ -55,38 +57,31 @@ func testCheckResourceGetAttr(name, key string, value *string) r.TestCheckFunc {
}
}

func testAccAWSAMILaunchPermissionExists(amiId string, accountId string) r.TestCheckFunc {
func testAccAWSAMILaunchPermissionExists(tlp *testLaunchPermission) r.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ec2conn
res, err := conn.DescribeImageAttribute(&ec2.DescribeImageAttributeInput{
ImageId: aws.String(amiId),
Attribute: aws.String("launchPermission"),
})
if err != nil {
if has, err := hasLaunchPermission(conn, tlp.ImageID, tlp.AccountID); err != nil {
return err
} else if !has {
return fmt.Errorf("launch permission does not exist for '%s' on '%s'", tlp.AccountID, tlp.ImageID)
}
for _, lp := range res.LaunchPermissions {
if *lp.UserId == accountId {
return nil
}
}
return fmt.Errorf("launch permission does not exist for '%s' on '%s'", accountId, amiId)
return nil
}
}

func testAccAWSAMILaunchPermissionDestroyed() r.TestCheckFunc {
func testAccAWSAMILaunchPermissionDestroyed(tlp *testLaunchPermission) r.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ec2conn

conn.DescribeImageAttribute(&ec2.DescribeImageAttributeInput{
ImageId: aws.String(""),
})

if has, err := hasLaunchPermission(conn, tlp.ImageID, tlp.AccountID); err != nil {
return err
} else if has {
return fmt.Errorf("launch permission still exists for '%s' on '%s'", tlp.AccountID, tlp.ImageID)
}
return nil
}
}

func testAccAWSAMILaunchPermissionConfig(accountId string) string {
func testAccAWSAMILaunchPermissionConfig(tlp *testLaunchPermission) string {
return fmt.Sprintf(`
provider "aws" {
region = "us-east-1"
Expand Down Expand Up @@ -137,5 +132,5 @@ resource "aws_ami_launch_permission" "self-test" {
image_id = "${aws_ami_from_instance.test.id}"
account_id = "%s"
}
`, accountId)
`, tlp.AccountID)
}

0 comments on commit 375b4a7

Please sign in to comment.