Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add and use 'testAccAvailableEc2InstanceTypeForAvailabilityZone' config generator #13091

Merged
merged 9 commits into from
Jul 28, 2020
57 changes: 21 additions & 36 deletions aws/resource_aws_ami_from_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

func TestAccAWSAMIFromInstance_basic(t *testing.T) {
var image ec2.Image
rName := acctest.RandomWithPrefix("tf-acc")
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_ami_from_instance.test"

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -36,7 +36,7 @@ func TestAccAWSAMIFromInstance_basic(t *testing.T) {

func TestAccAWSAMIFromInstance_tags(t *testing.T) {
var image ec2.Image
rName := acctest.RandomWithPrefix("tf-acc")
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_ami_from_instance.test"

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -129,55 +129,38 @@ func testAccCheckAWSAMIFromInstanceDestroy(s *terraform.State) error {
return testAccCheckAWSEbsSnapshotDestroy(s)
}

func testAccAWSAMIFromInstanceConfigBase() string {
return `
data "aws_ec2_instance_type_offering" "available" {
filter {
name = "instance-type"
values = ["t3.micro", "t2.micro"]
}

preferred_instance_types = ["t3.micro", "t2.micro"]
}

data "aws_ami" "amzn-ami-minimal-hvm-ebs" {
most_recent = true
owners = ["amazon"]

filter {
name = "name"
values = ["amzn-ami-minimal-hvm-*"]
}

filter {
name = "root-device-type"
values = ["ebs"]
}
}

func testAccAWSAMIFromInstanceConfigBase(rName string) string {
return composeConfig(
testAccLatestAmazonLinuxHvmEbsAmiConfig(),
testAccAvailableEc2InstanceTypeForRegion("t3.micro", "t2.micro"),
fmt.Sprintf(`
resource "aws_instance" "test" {
ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
instance_type = data.aws_ec2_instance_type_offering.available.instance_type

tags = {
Name = "testAccAWSAMIFromInstanceConfig_TestAMI"
Name = %[1]q
}
}
`
`, rName))
}

func testAccAWSAMIFromInstanceConfig(rName string) string {
return testAccAWSAMIFromInstanceConfigBase() + fmt.Sprintf(`
return composeConfig(
testAccAWSAMIFromInstanceConfigBase(rName),
fmt.Sprintf(`
resource "aws_ami_from_instance" "test" {
name = %[1]q
description = "Testing Terraform aws_ami_from_instance resource"
source_instance_id = "${aws_instance.test.id}"
}
`, rName)
`, rName))
}

func testAccAWSAMIFromInstanceConfigTags1(rName, tagKey1, tagValue1 string) string {
return testAccAWSAMIFromInstanceConfigBase() + fmt.Sprintf(`
return composeConfig(
testAccAWSAMIFromInstanceConfigBase(rName),
fmt.Sprintf(`
resource "aws_ami_from_instance" "test" {
name = %[1]q
description = "Testing Terraform aws_ami_from_instance resource"
Expand All @@ -187,11 +170,13 @@ resource "aws_ami_from_instance" "test" {
%[2]q = %[3]q
}
}
`, rName, tagKey1, tagValue1)
`, rName, tagKey1, tagValue1))
}

func testAccAWSAMIFromInstanceConfigTags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string {
return testAccAWSAMIFromInstanceConfigBase() + fmt.Sprintf(`
return composeConfig(
testAccAWSAMIFromInstanceConfigBase(rName),
fmt.Sprintf(`
resource "aws_ami_from_instance" "test" {
name = %[1]q
description = "Testing Terraform aws_ami_from_instance resource"
Expand All @@ -202,5 +187,5 @@ resource "aws_ami_from_instance" "test" {
%[4]q = %[5]q
}
}
`, rName, tagKey1, tagValue1, tagKey2, tagValue2)
`, rName, tagKey1, tagValue1, tagKey2, tagValue2))
}
37 changes: 5 additions & 32 deletions aws/resource_aws_cloudwatch_metric_alarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -779,37 +779,10 @@ resource "aws_cloudwatch_metric_alarm" "test" {
// EC2 Automate requires a valid EC2 instance
// ValidationError: Invalid use of EC2 'Recover' action. i-abc123 is not a valid EC2 instance.
func testAccAWSCloudWatchMetricAlarmConfigAlarmActionsEC2Automate(rName, action string) string {
return fmt.Sprintf(`
data "aws_ami" "amzn-ami-minimal-hvm-ebs" {
most_recent = true
owners = ["amazon"]

filter {
name = "name"
values = ["amzn-ami-minimal-hvm-*"]
}

filter {
name = "root-device-type"
values = ["ebs"]
}
}

data "aws_ec2_instance_type_offering" "available" {
filter {
name = "instance-type"
values = ["t3.micro", "t2.micro"]
}

filter {
name = "location"
values = [aws_subnet.test.availability_zone]
}

location_type = "availability-zone"
preferred_instance_types = ["t3.micro", "t2.micro"]
}

return composeConfig(
testAccLatestAmazonLinuxHvmEbsAmiConfig(),
testAccAvailableEc2InstanceTypeForAvailabilityZone("aws_subnet.test.availability_zone", "t3.micro", "t2.micro"),
fmt.Sprintf(`
data "aws_partition" "current" {}

data "aws_region" "current" {}
Expand Down Expand Up @@ -858,7 +831,7 @@ resource "aws_cloudwatch_metric_alarm" "test" {
InstanceId = aws_instance.test.id
}
}
`, rName, action)
`, rName, action))
}

func testAccAWSCloudWatchMetricAlarmConfigAlarmActionsSNSTopic(rName string) string {
Expand Down
64 changes: 25 additions & 39 deletions aws/resource_aws_eip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,20 +279,21 @@ func TestAccAWSEIP_associated_user_private_ip(t *testing.T) {
func TestAccAWSEIP_Instance_Reassociate(t *testing.T) {
instanceResourceName := "aws_instance.test"
resourceName := "aws_eip.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEIPDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEIP_Instance(),
Config: testAccAWSEIP_Instance(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(resourceName, "instance", instanceResourceName, "id"),
),
},
{
Config: testAccAWSEIP_Instance(),
Config: testAccAWSEIP_Instance(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(resourceName, "instance", instanceResourceName, "id"),
),
Expand Down Expand Up @@ -981,7 +982,7 @@ data "aws_availability_zones" "available" {
values = ["opt-in-not-required"]
}
}

resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/24"
tags = {
Expand Down Expand Up @@ -1023,41 +1024,18 @@ resource "aws_eip" "test2" {
}
`

func testAccAWSEIP_Instance() string {
return `
data "aws_ami" "amzn-ami-minimal-hvm-ebs" {
most_recent = true
owners = ["amazon"]

filter {
name = "name"
values = ["amzn-ami-minimal-hvm-*"]
}

filter {
name = "root-device-type"
values = ["ebs"]
}
}

data "aws_ec2_instance_type_offering" "available" {
filter {
name = "instance-type"
values = ["t3.micro", "t2.micro"]
}

filter {
name = "location"
values = [aws_subnet.test.availability_zone]
}

location_type = "availability-zone"
preferred_instance_types = ["t3.micro", "t2.micro"]
}

func testAccAWSEIP_Instance(rName string) string {
return composeConfig(
testAccLatestAmazonLinuxHvmEbsAmiConfig(),
testAccAvailableEc2InstanceTypeForAvailabilityZone("aws_subnet.test.availability_zone", "t3.micro", "t2.micro"),
fmt.Sprintf(`
resource "aws_eip" "test" {
instance = aws_instance.test.id
vpc = true

tags = {
Name = %[1]q
}
}

resource "aws_instance" "test" {
Expand All @@ -1067,7 +1045,7 @@ resource "aws_instance" "test" {
subnet_id = aws_subnet.test.id

tags = {
Name = "testAccAWSEIP_Instance"
Name = %[1]q
}

lifecycle {
Expand All @@ -1079,20 +1057,24 @@ resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"

tags = {
Name = "terraform-testacc-eip-disassociate"
Name = %[1]q
}
}

resource "aws_internet_gateway" "test" {
vpc_id = aws_vpc.test.id

tags = {
Name = %[1]q
}
}

resource "aws_subnet" "test" {
cidr_block = "10.0.0.0/24"
vpc_id = aws_vpc.test.id

tags = {
Name = "tf-acc-eip-disassociate"
Name = %[1]q
}
}

Expand All @@ -1103,13 +1085,17 @@ resource "aws_route_table" "test" {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.test.id
}

tags = {
Name = %[1]q
}
}

resource "aws_route_table_association" "test" {
subnet_id = aws_subnet.test.id
route_table_id = aws_route_table.test.id
}
`
`, rName))
}

const testAccAWSEIPAssociate_not_associated = `
Expand Down
Loading