Skip to content

Commit

Permalink
tests/resource/aws_instance: Refactor TestAccAWSInstance_hibernation …
Browse files Browse the repository at this point in the history
…to use aws_ami data source and launch with encrypted volume instead of copying AMI

Also verifies import and replacement.

Output from acceptance testing:

```
--- PASS: TestAccAWSInstance_hibernation (240.05s)
```
  • Loading branch information
bflad committed Feb 12, 2020
1 parent 1e03c68 commit d6263e8
Showing 1 changed file with 67 additions and 31 deletions.
98 changes: 67 additions & 31 deletions aws/resource_aws_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2498,18 +2498,32 @@ func TestAccAWSInstance_UserData_UnspecifiedToEmptyString(t *testing.T) {
}

func TestAccAWSInstance_hibernation(t *testing.T) {
var instance ec2.Instance
var instance1, instance2 ec2.Instance
resourceName := "aws_instance.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccInstanceConfigHibernation,
Config: testAccInstanceConfigHibernation(true),
Check: resource.ComposeTestCheckFunc(
testAccCheckInstanceExists(resourceName, &instance1),
resource.TestCheckResourceAttr(resourceName, "hibernation", "true"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccInstanceConfigHibernation(false),
Check: resource.ComposeTestCheckFunc(
testAccCheckInstanceExists("aws_instance.foo", &instance),
resource.TestCheckResourceAttr("aws_instance.foo", "hibernation", "true"),
testAccCheckInstanceExists(resourceName, &instance2),
testAccCheckInstanceRecreated(&instance1, &instance2),
resource.TestCheckResourceAttr(resourceName, "hibernation", "false"),
),
},
},
Expand All @@ -2526,6 +2540,16 @@ func testAccCheckInstanceNotRecreated(t *testing.T,
}
}

func testAccCheckInstanceRecreated(before, after *ec2.Instance) resource.TestCheckFunc {
return func(s *terraform.State) error {
if aws.StringValue(before.InstanceId) == aws.StringValue(after.InstanceId) {
return fmt.Errorf("EC2 Instance (%s) not recreated", aws.StringValue(before.InstanceId))
}

return nil
}
}

func testAccCheckInstanceDestroy(s *terraform.State) error {
return testAccCheckInstanceDestroyWithProvider(s, testAccProvider)
}
Expand Down Expand Up @@ -4190,39 +4214,51 @@ resource "aws_subnet" "test" {
`, rName)
}

// must be >= m3 and have an encrypted root volume to eanble hibernation
const testAccInstanceConfigHibernation = `
resource "aws_vpc" "foo" {
cidr_block = "10.1.0.0/16"
tags = {
Name = "terraform-testacc-instance-hibernation"
}
func testAccInstanceConfigHibernation(hibernation bool) 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"]
}
}
resource "aws_subnet" "foo" {
cidr_block = "10.1.1.0/24"
vpc_id = "${aws_vpc.foo.id}"
tags = {
Name = "tf-acc-instance-hibernation"
}
resource "aws_vpc" "test" {
cidr_block = "10.1.0.0/16"
tags = {
Name = "terraform-testacc-instance-hibernation"
}
}
resource "aws_instance" "foo" {
ami = "${aws_ami_copy.encrypted_ami.id}"
instance_type = "m3.medium"
subnet_id = "${aws_subnet.foo.id}"
hibernation = "true"
resource "aws_subnet" "test" {
cidr_block = "10.1.1.0/24"
vpc_id = aws_vpc.test.id
tags = {
Name = "tf-acc-instance-hibernation"
}
}
resource "aws_ami_copy" "encrypted_ami" {
name = "terraform-testacc-encrypted-ami"
description = "An encrypted AMI for Terraform acceptance testing"
source_ami_id = "ami-01e24be29428c15b2"
encrypted = "true"
source_ami_region = "us-west-2"
# must be >= m3 and have an encrypted root volume to eanble hibernation
resource "aws_instance" "test" {
ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
hibernation = %[1]t
instance_type = "m5.large"
subnet_id = aws_subnet.test.id
tags {
Name = "terraform-testacc-instance-hibernation"
root_block_device {
encrypted = true
volume_size = 20
}
}
`
`, hibernation)
}

0 comments on commit d6263e8

Please sign in to comment.