github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_ami_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 "testing" 7 8 "github.com/aws/aws-sdk-go/aws" 9 "github.com/aws/aws-sdk-go/service/ec2" 10 "github.com/hashicorp/terraform/helper/acctest" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/terraform" 13 ) 14 15 func TestAccAWSAMI_basic(t *testing.T) { 16 var ami ec2.Image 17 rInt := acctest.RandInt() 18 19 resource.Test(t, resource.TestCase{ 20 PreCheck: func() { testAccPreCheck(t) }, 21 Providers: testAccProviders, 22 CheckDestroy: testAccCheckAmiDestroy, 23 Steps: []resource.TestStep{ 24 { 25 Config: testAccAmiConfig_basic(rInt), 26 Check: resource.ComposeTestCheckFunc( 27 testAccCheckAmiExists("aws_ami.foo", &ami), 28 resource.TestCheckResourceAttr( 29 "aws_ami.foo", "name", fmt.Sprintf("tf-testing-%d", rInt)), 30 ), 31 }, 32 }, 33 }) 34 } 35 36 func TestAccAWSAMI_snapshotSize(t *testing.T) { 37 var ami ec2.Image 38 var bd ec2.BlockDeviceMapping 39 rInt := acctest.RandInt() 40 41 expectedDevice := &ec2.EbsBlockDevice{ 42 DeleteOnTermination: aws.Bool(true), 43 Encrypted: aws.Bool(false), 44 Iops: aws.Int64(0), 45 VolumeSize: aws.Int64(20), 46 VolumeType: aws.String("standard"), 47 } 48 49 resource.Test(t, resource.TestCase{ 50 PreCheck: func() { testAccPreCheck(t) }, 51 Providers: testAccProviders, 52 CheckDestroy: testAccCheckAmiDestroy, 53 Steps: []resource.TestStep{ 54 { 55 Config: testAccAmiConfig_snapshotSize(rInt), 56 Check: resource.ComposeTestCheckFunc( 57 testAccCheckAmiExists("aws_ami.foo", &ami), 58 testAccCheckAmiBlockDevice(&ami, &bd, "/dev/sda1"), 59 testAccCheckAmiEbsBlockDevice(&bd, expectedDevice), 60 resource.TestCheckResourceAttr( 61 "aws_ami.foo", "name", fmt.Sprintf("tf-testing-%d", rInt)), 62 resource.TestCheckResourceAttr( 63 "aws_ami.foo", "architecture", "x86_64"), 64 ), 65 }, 66 }, 67 }) 68 } 69 70 func testAccCheckAmiDestroy(s *terraform.State) error { 71 conn := testAccProvider.Meta().(*AWSClient).ec2conn 72 73 for _, rs := range s.RootModule().Resources { 74 if rs.Type != "aws_ami" { 75 continue 76 } 77 78 // Try to find the AMI 79 log.Printf("AMI-ID: %s", rs.Primary.ID) 80 DescribeAmiOpts := &ec2.DescribeImagesInput{ 81 ImageIds: []*string{aws.String(rs.Primary.ID)}, 82 } 83 resp, err := conn.DescribeImages(DescribeAmiOpts) 84 if err != nil { 85 return err 86 } 87 88 if len(resp.Images) > 0 { 89 state := resp.Images[0].State 90 return fmt.Errorf("AMI %s still exists in the state: %s.", *resp.Images[0].ImageId, *state) 91 } 92 } 93 return nil 94 } 95 96 func testAccCheckAmiExists(n string, ami *ec2.Image) resource.TestCheckFunc { 97 return func(s *terraform.State) error { 98 rs, ok := s.RootModule().Resources[n] 99 if !ok { 100 return fmt.Errorf("AMI Not found: %s", n) 101 } 102 103 if rs.Primary.ID == "" { 104 return fmt.Errorf("No AMI ID is set") 105 } 106 107 conn := testAccProvider.Meta().(*AWSClient).ec2conn 108 opts := &ec2.DescribeImagesInput{ 109 ImageIds: []*string{aws.String(rs.Primary.ID)}, 110 } 111 resp, err := conn.DescribeImages(opts) 112 if err != nil { 113 return err 114 } 115 if len(resp.Images) == 0 { 116 return fmt.Errorf("AMI not found") 117 } 118 *ami = *resp.Images[0] 119 return nil 120 } 121 } 122 123 func testAccCheckAmiBlockDevice(ami *ec2.Image, blockDevice *ec2.BlockDeviceMapping, n string) resource.TestCheckFunc { 124 return func(s *terraform.State) error { 125 devices := make(map[string]*ec2.BlockDeviceMapping) 126 for _, device := range ami.BlockDeviceMappings { 127 devices[*device.DeviceName] = device 128 } 129 130 // Check if the block device exists 131 if _, ok := devices[n]; !ok { 132 return fmt.Errorf("block device doesn't exist: %s", n) 133 } 134 135 *blockDevice = *devices[n] 136 return nil 137 } 138 } 139 140 func testAccCheckAmiEbsBlockDevice(bd *ec2.BlockDeviceMapping, ed *ec2.EbsBlockDevice) resource.TestCheckFunc { 141 return func(s *terraform.State) error { 142 // Test for things that ed has, don't care about unset values 143 cd := bd.Ebs 144 if ed.VolumeType != nil { 145 if *ed.VolumeType != *cd.VolumeType { 146 return fmt.Errorf("Volume type mismatch. Expected: %s Got: %s", 147 *ed.VolumeType, *cd.VolumeType) 148 } 149 } 150 if ed.DeleteOnTermination != nil { 151 if *ed.DeleteOnTermination != *cd.DeleteOnTermination { 152 return fmt.Errorf("DeleteOnTermination mismatch. Expected: %t Got: %t", 153 *ed.DeleteOnTermination, *cd.DeleteOnTermination) 154 } 155 } 156 if ed.Encrypted != nil { 157 if *ed.Encrypted != *cd.Encrypted { 158 return fmt.Errorf("Encrypted mismatch. Expected: %t Got: %t", 159 *ed.Encrypted, *cd.Encrypted) 160 } 161 } 162 // Integer defaults need to not be `0` so we don't get a panic 163 if ed.Iops != nil && *ed.Iops != 0 { 164 if *ed.Iops != *cd.Iops { 165 return fmt.Errorf("IOPS mismatch. Expected: %d Got: %d", 166 *ed.Iops, *cd.Iops) 167 } 168 } 169 if ed.VolumeSize != nil && *ed.VolumeSize != 0 { 170 if *ed.VolumeSize != *cd.VolumeSize { 171 return fmt.Errorf("Volume Size mismatch. Expected: %d Got: %d", 172 *ed.VolumeSize, *cd.VolumeSize) 173 } 174 } 175 176 return nil 177 } 178 } 179 180 func testAccAmiConfig_basic(rInt int) string { 181 return fmt.Sprintf(` 182 resource "aws_ebs_volume" "foo" { 183 availability_zone = "us-west-2a" 184 size = 8 185 tags { 186 Name = "tf-acc-test" 187 } 188 } 189 190 resource "aws_ebs_snapshot" "foo" { 191 volume_id = "${aws_ebs_volume.foo.id}" 192 } 193 194 resource "aws_ami" "foo" { 195 name = "tf-testing-%d" 196 virtualization_type = "hvm" 197 root_device_name = "/dev/sda1" 198 ebs_block_device { 199 device_name = "/dev/sda1" 200 snapshot_id = "${aws_ebs_snapshot.foo.id}" 201 } 202 } 203 `, rInt) 204 } 205 206 func testAccAmiConfig_snapshotSize(rInt int) string { 207 return fmt.Sprintf(` 208 resource "aws_ebs_volume" "foo" { 209 availability_zone = "us-west-2a" 210 size = 20 211 tags { 212 Name = "tf-acc-test" 213 } 214 } 215 216 resource "aws_ebs_snapshot" "foo" { 217 volume_id = "${aws_ebs_volume.foo.id}" 218 } 219 220 resource "aws_ami" "foo" { 221 name = "tf-testing-%d" 222 virtualization_type = "hvm" 223 root_device_name = "/dev/sda1" 224 ebs_block_device { 225 device_name = "/dev/sda1" 226 snapshot_id = "${aws_ebs_snapshot.foo.id}" 227 } 228 } 229 `, rInt) 230 }