Skip to content

Commit

Permalink
Adds acceptance test for org sharing
Browse files Browse the repository at this point in the history
  • Loading branch information
joraff committed Nov 9, 2021
1 parent d9b4f95 commit 08c38a8
Showing 1 changed file with 72 additions and 1 deletion.
73 changes: 72 additions & 1 deletion internal/service/ec2/ami_launch_permission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,32 @@ func TestAccEC2AMILaunchPermission_basic(t *testing.T) {
})
}

func TestAccEC2AMILaunchPermission_org(t *testing.T) {
resourceName := "aws_ami_launch_permission.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID),
Providers: acctest.Providers,
CheckDestroy: testAccCheckAMILaunchPermissionDestroy,
Steps: []resource.TestStep{
{
Config: testOrgAMILaunchPermissionConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAMILaunchPermissionExists(resourceName),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateIdFunc: testAccAMILaunchPermissionImportStateIdFunc(resourceName),
ImportStateVerify: true,
},
},
})
}

func TestAccEC2AMILaunchPermission_Disappears_launchPermission(t *testing.T) {
resourceName := "aws_ami_launch_permission.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
Expand Down Expand Up @@ -298,13 +324,58 @@ resource "aws_ami_launch_permission" "test" {
`, rName, rName)
}

func testOrgAMILaunchPermissionConfig(rName string) string {
return fmt.Sprintf(`
data "aws_ami" "amzn-ami-minimal-hvm" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn-ami-minimal-hvm-*"]
}
filter {
name = "root-device-type"
values = ["ebs"]
}
}
data "aws_organizations_organization" "current" {}
data "aws_region" "current" {}
resource "aws_ami_copy" "test" {
description = %q
name = %q
source_ami_id = data.aws_ami.amzn-ami-minimal-hvm.id
source_ami_region = data.aws_region.current.name
}
resource "aws_ami_launch_permission" "test" {
arn = data.aws_organizations_organization.current.arn
arn_type = "OrganizationArn"
image_id = aws_ami_copy.test.id
}
`, rName, rName)
}

func testAccAMILaunchPermissionImportStateIdFunc(resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return "", fmt.Errorf("Not found: %s", resourceName)
}

return fmt.Sprintf("%s/%s", rs.Primary.Attributes["account_id"], rs.Primary.Attributes["image_id"]), nil
accountID := rs.Primary.Attributes["account_id"]
imageId := rs.Primary.Attributes["image_id"]

if len(accountID) > 0 {
return fmt.Sprintf("%s,%s", accountID, imageId), nil
} else {
arn := rs.Primary.Attributes["arn"]
arn_type := rs.Primary.Attributes["arn_type"]
return fmt.Sprintf("%s,%s,%s", arn, arn_type, imageId), nil
}
}
}

0 comments on commit 08c38a8

Please sign in to comment.