github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_ebs_volume_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "regexp" 8 9 "github.com/aws/aws-sdk-go/aws" 10 "github.com/aws/aws-sdk-go/service/ec2" 11 "github.com/hashicorp/terraform/helper/acctest" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/terraform" 14 ) 15 16 func TestAccAWSEBSVolume_basic(t *testing.T) { 17 var v ec2.Volume 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 IDRefreshName: "aws_ebs_volume.test", 21 Providers: testAccProviders, 22 Steps: []resource.TestStep{ 23 { 24 Config: testAccAwsEbsVolumeConfig, 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckVolumeExists("aws_ebs_volume.test", &v), 27 ), 28 }, 29 }, 30 }) 31 } 32 33 func TestAccAWSEBSVolume_updateSize(t *testing.T) { 34 var v ec2.Volume 35 resource.Test(t, resource.TestCase{ 36 PreCheck: func() { testAccPreCheck(t) }, 37 IDRefreshName: "aws_ebs_volume.test", 38 Providers: testAccProviders, 39 Steps: []resource.TestStep{ 40 { 41 Config: testAccAwsEbsVolumeConfig, 42 Check: resource.ComposeTestCheckFunc( 43 testAccCheckVolumeExists("aws_ebs_volume.test", &v), 44 resource.TestCheckResourceAttr("aws_ebs_volume.test", "size", "1"), 45 ), 46 }, 47 { 48 Config: testAccAwsEbsVolumeConfigUpdateSize, 49 Check: resource.ComposeTestCheckFunc( 50 testAccCheckVolumeExists("aws_ebs_volume.test", &v), 51 resource.TestCheckResourceAttr("aws_ebs_volume.test", "size", "10"), 52 ), 53 }, 54 }, 55 }) 56 } 57 58 func TestAccAWSEBSVolume_updateType(t *testing.T) { 59 var v ec2.Volume 60 resource.Test(t, resource.TestCase{ 61 PreCheck: func() { testAccPreCheck(t) }, 62 IDRefreshName: "aws_ebs_volume.test", 63 Providers: testAccProviders, 64 Steps: []resource.TestStep{ 65 { 66 Config: testAccAwsEbsVolumeConfig, 67 Check: resource.ComposeTestCheckFunc( 68 testAccCheckVolumeExists("aws_ebs_volume.test", &v), 69 resource.TestCheckResourceAttr("aws_ebs_volume.test", "type", "gp2"), 70 ), 71 }, 72 { 73 Config: testAccAwsEbsVolumeConfigUpdateType, 74 Check: resource.ComposeTestCheckFunc( 75 testAccCheckVolumeExists("aws_ebs_volume.test", &v), 76 resource.TestCheckResourceAttr("aws_ebs_volume.test", "type", "sc1"), 77 ), 78 }, 79 }, 80 }) 81 } 82 83 func TestAccAWSEBSVolume_updateIops(t *testing.T) { 84 var v ec2.Volume 85 resource.Test(t, resource.TestCase{ 86 PreCheck: func() { testAccPreCheck(t) }, 87 IDRefreshName: "aws_ebs_volume.test", 88 Providers: testAccProviders, 89 Steps: []resource.TestStep{ 90 { 91 Config: testAccAwsEbsVolumeConfigWithIops, 92 Check: resource.ComposeTestCheckFunc( 93 testAccCheckVolumeExists("aws_ebs_volume.test", &v), 94 resource.TestCheckResourceAttr("aws_ebs_volume.test", "iops", "100"), 95 ), 96 }, 97 { 98 Config: testAccAwsEbsVolumeConfigWithIopsUpdated, 99 Check: resource.ComposeTestCheckFunc( 100 testAccCheckVolumeExists("aws_ebs_volume.test", &v), 101 resource.TestCheckResourceAttr("aws_ebs_volume.test", "iops", "200"), 102 ), 103 }, 104 }, 105 }) 106 } 107 108 func TestAccAWSEBSVolume_kmsKey(t *testing.T) { 109 var v ec2.Volume 110 ri := acctest.RandInt() 111 config := fmt.Sprintf(testAccAwsEbsVolumeConfigWithKmsKey, ri) 112 keyRegex := regexp.MustCompile("^arn:aws:([a-zA-Z0-9\\-])+:([a-z]{2}-[a-z]+-\\d{1})?:(\\d{12})?:(.*)$") 113 114 resource.Test(t, resource.TestCase{ 115 PreCheck: func() { testAccPreCheck(t) }, 116 IDRefreshName: "aws_ebs_volume.test", 117 Providers: testAccProviders, 118 Steps: []resource.TestStep{ 119 { 120 Config: config, 121 Check: resource.ComposeTestCheckFunc( 122 testAccCheckVolumeExists("aws_ebs_volume.test", &v), 123 resource.TestCheckResourceAttr("aws_ebs_volume.test", "encrypted", "true"), 124 resource.TestMatchResourceAttr("aws_ebs_volume.test", "kms_key_id", keyRegex), 125 ), 126 }, 127 }, 128 }) 129 } 130 131 func TestAccAWSEBSVolume_NoIops(t *testing.T) { 132 var v ec2.Volume 133 resource.Test(t, resource.TestCase{ 134 PreCheck: func() { testAccPreCheck(t) }, 135 Providers: testAccProviders, 136 Steps: []resource.TestStep{ 137 { 138 Config: testAccAwsEbsVolumeConfigWithNoIops, 139 Check: resource.ComposeTestCheckFunc( 140 testAccCheckVolumeExists("aws_ebs_volume.iops_test", &v), 141 ), 142 }, 143 }, 144 }) 145 } 146 147 func TestAccAWSEBSVolume_withTags(t *testing.T) { 148 var v ec2.Volume 149 resource.Test(t, resource.TestCase{ 150 PreCheck: func() { testAccPreCheck(t) }, 151 IDRefreshName: "aws_ebs_volume.tags_test", 152 Providers: testAccProviders, 153 Steps: []resource.TestStep{ 154 { 155 Config: testAccAwsEbsVolumeConfigWithTags, 156 Check: resource.ComposeTestCheckFunc( 157 testAccCheckVolumeExists("aws_ebs_volume.tags_test", &v), 158 ), 159 }, 160 }, 161 }) 162 } 163 164 func testAccCheckVolumeExists(n string, v *ec2.Volume) resource.TestCheckFunc { 165 return func(s *terraform.State) error { 166 rs, ok := s.RootModule().Resources[n] 167 if !ok { 168 return fmt.Errorf("Not found: %s", n) 169 } 170 171 if rs.Primary.ID == "" { 172 return fmt.Errorf("No ID is set") 173 } 174 175 conn := testAccProvider.Meta().(*AWSClient).ec2conn 176 177 request := &ec2.DescribeVolumesInput{ 178 VolumeIds: []*string{aws.String(rs.Primary.ID)}, 179 } 180 181 response, err := conn.DescribeVolumes(request) 182 if err == nil { 183 if response.Volumes != nil && len(response.Volumes) > 0 { 184 *v = *response.Volumes[0] 185 return nil 186 } 187 } 188 return fmt.Errorf("Error finding EC2 volume %s", rs.Primary.ID) 189 } 190 } 191 192 const testAccAwsEbsVolumeConfig = ` 193 resource "aws_ebs_volume" "test" { 194 availability_zone = "us-west-2a" 195 type = "gp2" 196 size = 1 197 tags { 198 Name = "tf-acc-test-ebs-volume-test" 199 } 200 } 201 ` 202 203 const testAccAwsEbsVolumeConfigUpdateSize = ` 204 resource "aws_ebs_volume" "test" { 205 availability_zone = "us-west-2a" 206 type = "gp2" 207 size = 10 208 tags { 209 Name = "tf-acc-test-ebs-volume-test" 210 } 211 } 212 ` 213 214 const testAccAwsEbsVolumeConfigUpdateType = ` 215 resource "aws_ebs_volume" "test" { 216 availability_zone = "us-west-2a" 217 type = "sc1" 218 size = 500 219 tags { 220 Name = "tf-acc-test-ebs-volume-test" 221 } 222 } 223 ` 224 225 const testAccAwsEbsVolumeConfigWithIops = ` 226 resource "aws_ebs_volume" "test" { 227 availability_zone = "us-west-2a" 228 type = "io1" 229 size = 4 230 iops = 100 231 tags { 232 Name = "tf-acc-test-ebs-volume-test" 233 } 234 } 235 ` 236 237 const testAccAwsEbsVolumeConfigWithIopsUpdated = ` 238 resource "aws_ebs_volume" "test" { 239 availability_zone = "us-west-2a" 240 type = "io1" 241 size = 4 242 iops = 200 243 tags { 244 Name = "tf-acc-test-ebs-volume-test" 245 } 246 } 247 ` 248 249 const testAccAwsEbsVolumeConfigWithKmsKey = ` 250 resource "aws_kms_key" "foo" { 251 description = "Terraform acc test %d" 252 policy = <<POLICY 253 { 254 "Version": "2012-10-17", 255 "Id": "kms-tf-1", 256 "Statement": [ 257 { 258 "Sid": "Enable IAM User Permissions", 259 "Effect": "Allow", 260 "Principal": { 261 "AWS": "*" 262 }, 263 "Action": "kms:*", 264 "Resource": "*" 265 } 266 ] 267 } 268 POLICY 269 } 270 271 resource "aws_ebs_volume" "test" { 272 availability_zone = "us-west-2a" 273 size = 1 274 encrypted = true 275 kms_key_id = "${aws_kms_key.foo.arn}" 276 } 277 ` 278 279 const testAccAwsEbsVolumeConfigWithTags = ` 280 resource "aws_ebs_volume" "tags_test" { 281 availability_zone = "us-west-2a" 282 size = 1 283 tags { 284 Name = "TerraformTest" 285 } 286 } 287 ` 288 289 const testAccAwsEbsVolumeConfigWithNoIops = ` 290 resource "aws_ebs_volume" "iops_test" { 291 availability_zone = "us-west-2a" 292 size = 10 293 type = "gp2" 294 iops = 0 295 tags { 296 Name = "TerraformTest" 297 } 298 } 299 `