Skip to content

Commit

Permalink
Add new metadata options acceptance tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed Mar 27, 2020
1 parent d71bf50 commit aefe408
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 2 deletions.
51 changes: 51 additions & 0 deletions aws/data_source_aws_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,34 @@ func TestAccAWSInstanceDataSource_creditSpecification(t *testing.T) {
})
}

func TestAccAWSInstanceDataSource_metadataOptions(t *testing.T) {
resourceName := "aws_instance.test"
datasourceName := "data.aws_instance.test"
rName := acctest.RandomWithPrefix("tf-acc-test")
instanceType := "m1.small"

resource.ParallelTest(t, resource.TestCase{
// No subnet_id specified requires default VPC or EC2-Classic.
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckHasDefaultVpcOrEc2Classic(t)
testAccPreCheckOffersEc2InstanceType(t, instanceType)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccInstanceDataSourceConfig_metadataOptions(rName, instanceType),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(datasourceName, "metadata_options.#", resourceName, "metadata_options.#"),
resource.TestCheckResourceAttrPair(datasourceName, "metadata_options.0.http_endpoint", resourceName, "metadata_options.0.http_endpoint"),
resource.TestCheckResourceAttrPair(datasourceName, "metadata_options.0.http_tokens", resourceName, "metadata_options.0.http_tokens"),
resource.TestCheckResourceAttrPair(datasourceName, "metadata_options.0.http_put_response_hop_limit", resourceName, "metadata_options.0.http_put_response_hop_limit"),
),
},
},
})
}

// Lookup based on InstanceID
const testAccInstanceDataSourceConfig = `
resource "aws_instance" "test" {
Expand Down Expand Up @@ -835,3 +863,26 @@ data "aws_instance" "test" {
}
`)
}

func testAccInstanceDataSourceConfig_metadataOptions(rName, instanceType string) string {
return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(`
resource "aws_instance" "test" {
ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
instance_type = %[2]q
tags = {
Name = %[1]q
}
metadata_options {
http_endpoint = "enabled"
http_tokens = "required"
http_put_response_hop_limit = 2
}
}
data "aws_instance" "test" {
instance_id = aws_instance.test.id
}
`, rName, instanceType)
}
41 changes: 41 additions & 0 deletions aws/data_source_aws_launch_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,29 @@ func TestAccAWSLaunchTemplateDataSource_filter_tags(t *testing.T) {
})
}

func TestAccAWSLaunchTemplateDataSource_metadataOptions(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
dataSourceName := "data.aws_launch_template.test"
resourceName := "aws_launch_template.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSLaunchTemplateDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLaunchTemplateDataSourceConfig_metadataOptions(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "metadata_options.#", resourceName, "metadata_options.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "metadata_options.0.http_endpoint", resourceName, "metadata_options.0.http_endpoint"),
resource.TestCheckResourceAttrPair(dataSourceName, "metadata_options.0.http_tokens", resourceName, "metadata_options.0.http_tokens"),
resource.TestCheckResourceAttrPair(dataSourceName, "metadata_options.0.http_put_response_hop_limit", resourceName, "metadata_options.0.http_put_response_hop_limit"),
),
},
},
})
}

func testAccAWSLaunchTemplateDataSourceConfig_Basic(rName string) string {
return fmt.Sprintf(`
resource "aws_launch_template" "test" {
Expand Down Expand Up @@ -124,3 +147,21 @@ data "aws_launch_template" "test" {
}
`, rName, rInt)
}

func testAccAWSLaunchTemplateDataSourceConfig_metadataOptions(rName string) string {
return fmt.Sprintf(`
resource "aws_launch_template" "test" {
name = %[1]q
metadata_options {
http_endpoint = "enabled"
http_tokens = "required"
http_put_response_hop_limit = 2
}
}
data "aws_launch_template" "test" {
name = aws_launch_template.test.name
}
`, rName)
}
49 changes: 49 additions & 0 deletions aws/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
"strings"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/organizations"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -1137,6 +1139,53 @@ func testAccCheckAWSProviderPartition(providers *[]*schema.Provider, expectedPar
}
}

// testAccPreCheckHasDefaultVpcOrEc2Classic checks that the test region has a default VPC or has the EC2-Classic platform.
// This check is useful to ensure that an instance can be launched without specifying a subnet.
func testAccPreCheckHasDefaultVpcOrEc2Classic(t *testing.T) {
client := testAccProvider.Meta().(*AWSClient)

if !testAccHasDefaultVpc(t) && !hasEc2Classic(client.supportedplatforms) {
t.Skipf("skipping tests; %s does not have a default VPC or EC2-Classic", client.region)
}
}

func testAccHasDefaultVpc(t *testing.T) bool {
conn := testAccProvider.Meta().(*AWSClient).ec2conn

resp, err := conn.DescribeAccountAttributes(&ec2.DescribeAccountAttributesInput{
AttributeNames: aws.StringSlice([]string{ec2.AccountAttributeNameDefaultVpc}),
})
if testAccPreCheckSkipError(err) ||
len(resp.AccountAttributes) == 0 ||
len(resp.AccountAttributes[0].AttributeValues) == 0 ||
aws.StringValue(resp.AccountAttributes[0].AttributeValues[0].AttributeValue) == "none" {
return false
}
if err != nil {
t.Fatalf("error describing EC2 account attributes: %s", err)
}

return true
}

// testAccPreCheckOffersEc2InstanceType checks that the test region offers the specified EC2 instance type.
func testAccPreCheckOffersEc2InstanceType(t *testing.T, instanceType string) {
client := testAccProvider.Meta().(*AWSClient)

resp, err := client.ec2conn.DescribeInstanceTypeOfferings(&ec2.DescribeInstanceTypeOfferingsInput{
Filters: buildEC2AttributeFilterList(map[string]string{
"instance-type": instanceType,
}),
LocationType: aws.String(ec2.LocationTypeRegion),
})
if testAccPreCheckSkipError(err) || len(resp.InstanceTypeOfferings) == 0 {
t.Skipf("skipping tests; %s does not offer EC2 instance type: %s", client.region, instanceType)
}
if err != nil {
t.Fatalf("error describing EC2 instance type offerings: %s", err)
}
}

func testAccAWSProviderConfigEndpoints(endpoints string) string {
//lintignore:AT004
return fmt.Sprintf(`
Expand Down
86 changes: 86 additions & 0 deletions aws/resource_aws_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2529,6 +2529,52 @@ func TestAccAWSInstance_hibernation(t *testing.T) {
})
}

func TestAccAWSInstance_metadataOptions(t *testing.T) {
var v ec2.Instance
resourceName := "aws_instance.test"
rName := acctest.RandomWithPrefix("tf-acc-test")
instanceType := "m1.small"

resource.ParallelTest(t, resource.TestCase{
// No subnet_id specified requires default VPC or EC2-Classic.
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckHasDefaultVpcOrEc2Classic(t)
testAccPreCheckOffersEc2InstanceType(t, instanceType)
},
IDRefreshName: resourceName,
Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccInstanceConfigMetadataOptions(rName, instanceType),
Check: resource.ComposeTestCheckFunc(
testAccCheckInstanceExists(resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "metadata_options.#", "1"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_endpoint", "disabled"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_tokens", "optional"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_put_response_hop_limit", "1"),
),
},
{
Config: testAccInstanceConfigMetadataOptionsUpdated(rName, instanceType),
Check: resource.ComposeTestCheckFunc(
testAccCheckInstanceExists(resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "metadata_options.#", "1"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_endpoint", "enabled"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_tokens", "required"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_put_response_hop_limit", "2"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckInstanceNotRecreated(t *testing.T,
before, after *ec2.Instance) resource.TestCheckFunc {
return func(s *terraform.State) error {
Expand Down Expand Up @@ -4285,3 +4331,43 @@ resource "aws_instance" "test" {
}
`, hibernation)
}

func testAccInstanceConfigMetadataOptions(rName, instanceType string) string {
return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(`
resource "aws_instance" "test" {
ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
instance_type = %[2]q
tags = {
Name = %[1]q
}
metadata_options {
http_endpoint = "disabled"
}
}
data "aws_instance" "test" {
instance_id = aws_instance.test.id
}
`, rName, instanceType)
}

func testAccInstanceConfigMetadataOptionsUpdated(rName, instanceType string) string {
return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(`
resource "aws_instance" "test" {
ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
instance_type = %[2]q
tags = {
Name = %[1]q
}
metadata_options {
http_endpoint = "enabled"
http_tokens = "required"
http_put_response_hop_limit = 2
}
}
`, rName, instanceType)
}
43 changes: 43 additions & 0 deletions aws/resource_aws_launch_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,35 @@ func TestAccAWSLaunchTemplate_licenseSpecification(t *testing.T) {
})
}

func TestAccAWSLaunchTemplate_metadataOptions(t *testing.T) {
var template ec2.LaunchTemplate
resourceName := "aws_launch_template.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSLaunchTemplateDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLaunchTemplateConfig_metadataOptions(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchTemplateExists(resourceName, &template),
resource.TestCheckResourceAttr(resourceName, "metadata_options.#", "1"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_endpoint", "enabled"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_tokens", "required"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_put_response_hop_limit", "2"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckAWSLaunchTemplateExists(n string, t *ec2.LaunchTemplate) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -1507,3 +1536,17 @@ resource "aws_autoscaling_group" "test" {
}
}
`

func testAccAWSLaunchTemplateConfig_metadataOptions(rName string) string {
return fmt.Sprintf(`
resource "aws_launch_template" "test" {
name = %[1]q
metadata_options {
http_endpoint = "enabled"
http_tokens = "required"
http_put_response_hop_limit = 2
}
}
`, rName)
}
4 changes: 2 additions & 2 deletions website/docs/r/launch_template.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ resource "aws_launch_template" "foo" {
}
metadata_options {
http_endpoint = "enabled"
http_tokens = "required"
http_endpoint = "enabled"
http_tokens = "required"
http_put_response_hop_limit = 1
}
Expand Down

0 comments on commit aefe408

Please sign in to comment.