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

Remove hardcoded AMI IDs from data_source_aws_instance_test #14145

Merged
merged 1 commit into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 49 additions & 59 deletions aws/data_source_aws_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,10 +494,9 @@ func TestAccAWSInstanceDataSource_metadataOptions(t *testing.T) {
}

// Lookup based on InstanceID
const testAccInstanceDataSourceConfig = `
var testAccInstanceDataSourceConfig = testAccLatestAmazonLinuxHvmEbsAmiConfig() + `
resource "aws_instance" "test" {
# us-west-2
ami = "ami-4fccb37f"
ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
instance_type = "m1.small"
tags = {
Name = "HelloWorld"
Expand All @@ -514,10 +513,9 @@ data "aws_instance" "test" {

// Use the tags attribute to filter
func testAccInstanceDataSourceConfig_Tags(rInt int) string {
return fmt.Sprintf(`
return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(`
resource "aws_instance" "test" {
# us-west-2
ami = "ami-4fccb37f"
ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
instance_type = "m1.small"

tags = {
Expand All @@ -536,13 +534,13 @@ data "aws_instance" "test" {
}

// filter on tag, populate more attributes
const testAccInstanceDataSourceConfig_AzUserData = `
var testAccInstanceDataSourceConfig_AzUserData = testAccCurrentAvailableAZsNoOptInDefaultExcludeConfig() +
testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(`
resource "aws_instance" "test" {
# us-west-2
ami = "ami-4fccb37f"
availability_zone = "us-west-2a"
ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
availability_zone = data.aws_availability_zones.current.names[0]

instance_type = "m1.small"
instance_type = "t2.micro"
user_data = "test:-with-character's"
tags = {
TFAccTest = "YesThisIsATest"
Expand All @@ -552,13 +550,12 @@ resource "aws_instance" "test" {
data "aws_instance" "test" {
instance_id = "${aws_instance.test.id}"
}
`
`)

// GP2IopsDevice
const testAccInstanceDataSourceConfig_gp2IopsDevice = `
var testAccInstanceDataSourceConfig_gp2IopsDevice = testAccLatestAmazonLinuxHvmEbsAmiConfig() + `
resource "aws_instance" "test" {
# us-west-2
ami = "ami-55a7ea65"
ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
instance_type = "m3.medium"
root_block_device {
volume_type = "gp2"
Expand All @@ -572,10 +569,9 @@ data "aws_instance" "test" {
`

// Block Device
const testAccInstanceDataSourceConfig_blockDevices = `
var testAccInstanceDataSourceConfig_blockDevices = testAccLatestAmazonLinuxHvmEbsAmiConfig() + `
resource "aws_instance" "test" {
# us-west-2
ami = "ami-55a7ea65"
ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
instance_type = "m3.medium"

root_block_device {
Expand Down Expand Up @@ -611,14 +607,13 @@ data "aws_instance" "test" {
}
`

const testAccInstanceDataSourceConfig_EbsBlockDevice_KmsKeyId = `
var testAccInstanceDataSourceConfig_EbsBlockDevice_KmsKeyId = testAccLatestAmazonLinuxHvmEbsAmiConfig() + `
resource "aws_kms_key" "test" {
deletion_window_in_days = 7
}

resource "aws_instance" "test" {
# us-west-2
ami = "ami-55a7ea65"
ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
instance_type = "m3.medium"

root_block_device {
Expand All @@ -638,14 +633,13 @@ data "aws_instance" "test" {
}
`

const testAccInstanceDataSourceConfig_RootBlockDevice_KmsKeyId = `
var testAccInstanceDataSourceConfig_RootBlockDevice_KmsKeyId = testAccLatestAmazonLinuxHvmEbsAmiConfig() + `
resource "aws_kms_key" "test" {
deletion_window_in_days = 7
}

resource "aws_instance" "test" {
# us-west-2
ami = "ami-55a7ea65"
ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
instance_type = "m3.medium"

root_block_device {
Expand All @@ -661,9 +655,9 @@ data "aws_instance" "test" {
}
`

const testAccInstanceDataSourceConfig_rootInstanceStore = `
var testAccInstanceDataSourceConfig_rootInstanceStore = testAccLatestAmazonLinuxHvmEbsAmiConfig() + `
resource "aws_instance" "test" {
ami = "ami-44c36524"
ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
instance_type = "m3.medium"
}
data "aws_instance" "test" {
Expand All @@ -672,33 +666,33 @@ data "aws_instance" "test" {
`

func testAccInstanceDataSourceConfig_privateIP(rName string) string {
return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + `
return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfigBasic(rName) + fmt.Sprintf(`
resource "aws_instance" "test" {
ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}"
ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
instance_type = "t2.micro"
subnet_id = "${aws_subnet.test.id}"
private_ip = "10.1.1.42"
private_ip = "10.0.0.42"
}

data "aws_instance" "test" {
instance_id = "${aws_instance.test.id}"
}
`
`)
}

func testAccInstanceDataSourceConfig_secondaryPrivateIPs(rName string) string {
return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + `
return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfigBasic(rName) + fmt.Sprintf(`
resource "aws_instance" "test" {
ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}"
instance_type = "t2.micro"
subnet_id = "${aws_subnet.test.id}"
secondary_private_ips = ["10.1.1.42"]
secondary_private_ips = ["10.0.0.42"]
}

data "aws_instance" "test" {
instance_id = "${aws_instance.test.id}"
}
`
`)
}

func testAccInstanceDataSourceConfig_keyPair(rName string) string {
Expand All @@ -709,7 +703,7 @@ resource "aws_key_pair" "test" {
}

resource "aws_instance" "test" {
ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}"
ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
instance_type = "t1.micro"
key_name = "${aws_key_pair.test.key_name}"

Expand All @@ -733,33 +727,33 @@ data "aws_instance" "test" {
}

func testAccInstanceDataSourceConfig_VPC(rName string) string {
return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + `
return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfigBasic(rName) + fmt.Sprintf(`
resource "aws_instance" "test" {
ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}"
instance_type = "m1.small"
ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
instance_type = "t2.small"
subnet_id = "${aws_subnet.test.id}"
associate_public_ip_address = true
tenancy = "dedicated"
#tenancy = "dedicated"
# pre-encoded base64 data
user_data = "3dc39dda39be1205215e776bad998da361a5955d"
}

data "aws_instance" "test" {
instance_id = "${aws_instance.test.id}"
}
`
`)
}

func testAccInstanceDataSourceConfig_PlacementGroup(rName string) string {
return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(`
return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfigBasic(rName) + fmt.Sprintf(`
resource "aws_placement_group" "test" {
name = %[1]q
strategy = "cluster"
}

# Limitations: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/placement-groups.html#concepts-placement-groups
resource "aws_instance" "test" {
ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}"
ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
instance_type = "c3.large"
subnet_id = "${aws_subnet.test.id}"
associate_public_ip_address = true
Expand All @@ -776,11 +770,7 @@ data "aws_instance" "test" {
}

func testAccInstanceDataSourceConfig_SecurityGroups(rInt int) string {
return fmt.Sprintf(`
provider "aws" {
region = "us-east-1"
}

return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(`
resource "aws_security_group" "tf_test_foo" {
name = "tf_test_foo-%d"
description = "foo"
Expand All @@ -794,7 +784,7 @@ resource "aws_security_group" "tf_test_foo" {
}

resource "aws_instance" "test" {
ami = "ami-408c7f28"
ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
instance_type = "m1.small"
security_groups = ["${aws_security_group.tf_test_foo.name}"]
user_data = "foo:-with-character's"
Expand All @@ -808,12 +798,12 @@ data "aws_instance" "test" {

func testAccInstanceDataSourceConfig_VPCSecurityGroups(rName string) string {
return testAccLatestAmazonLinuxHvmEbsAmiConfig() +
testAccAwsInstanceVpcConfig(rName, false) +
testAccAwsInstanceVpcConfigBasic(rName) +
testAccAwsInstanceVpcSecurityGroupConfig(rName) +
`
fmt.Sprintf(`
resource "aws_instance" "test" {
ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}"
instance_type = "t1.micro"
ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
instance_type = "t2.micro"
vpc_security_group_ids = ["${aws_security_group.test.id}"]
subnet_id = "${aws_subnet.test.id}"
depends_on = ["aws_internet_gateway.test"]
Expand All @@ -822,7 +812,7 @@ resource "aws_instance" "test" {
data "aws_instance" "test" {
instance_id = "${aws_instance.test.id}"
}
`
`)
}

func testAccInstanceDataSourceConfig_getPasswordData(rName string, val bool) string {
Expand All @@ -847,9 +837,9 @@ data "aws_instance" "test" {
}

func testAccInstanceDataSourceConfigGetUserData(rName string, getUserData bool) string {
return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(`
return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfigBasic(rName) + fmt.Sprintf(`
resource "aws_instance" "test" {
ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}"
ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
instance_type = "t2.micro"
subnet_id = "${aws_subnet.test.id}"

Expand All @@ -868,9 +858,9 @@ data "aws_instance" "test" {
}

func testAccInstanceDataSourceConfigGetUserDataNoUserData(rName string, getUserData bool) string {
return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(`
return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfigBasic(rName) + fmt.Sprintf(`
resource "aws_instance" "test" {
ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}"
ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
instance_type = "t2.micro"
subnet_id = "${aws_subnet.test.id}"
}
Expand All @@ -883,9 +873,9 @@ data "aws_instance" "test" {
}

func testAccInstanceDataSourceConfig_creditSpecification(rName string) string {
return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + `
return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfigBasic(rName) + fmt.Sprintf(`
resource "aws_instance" "test" {
ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}"
ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
instance_type = "t2.micro"
subnet_id = "${aws_subnet.test.id}"

Expand All @@ -897,7 +887,7 @@ resource "aws_instance" "test" {
data "aws_instance" "test" {
instance_id = "${aws_instance.test.id}"
}
`
`)
}

func testAccInstanceDataSourceConfig_metadataOptions(rName string) string {
Expand Down
50 changes: 50 additions & 0 deletions aws/resource_aws_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3323,6 +3323,25 @@ func driftTags(instance *ec2.Instance) resource.TestCheckFunc {
}
}

func testAccCurrentAvailableAZsNoOptInDefaultExcludeConfig() string {
// Exclude usw2-az4 (us-west-2d) as it has limited instance types.
return testAccCurrentAvailableAZsNoOptInExcludeConfig(`"usw2-az4", "usgw1-az2"`)
}

func testAccCurrentAvailableAZsNoOptInExcludeConfig(exclude string) string {
return fmt.Sprintf(`
data "aws_availability_zones" "current" {
exclude_zone_ids = [%s]
state = "available"

filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
`, exclude)
}

func testAccInstanceConfigInDefaultVpcBySgName(rName string) string {
return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(`
data "aws_availability_zones" "current" {
Expand Down Expand Up @@ -4963,6 +4982,37 @@ resource "aws_subnet" "test" {
`, rName, mapPublicIpOnLaunch)
}

func testAccAwsInstanceVpcConfigBasic(rName string) string {
return fmt.Sprintf(`
data "aws_availability_zones" "available" {
state = "available"

filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}

resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"

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

resource "aws_subnet" "test" {
availability_zone = data.aws_availability_zones.available.names[0]
cidr_block = "10.0.0.0/24"
vpc_id = aws_vpc.test.id

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

// testAccAwsInstanceVpcSecurityGroupConfig returns the configuration for tests that create
// 1) a VPC security group
// 2) an internet gateway in the VPC
Expand Down