github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/resource_aws_ebs_volume_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/service/ec2" 9 "github.com/hashicorp/terraform/helper/acctest" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 "regexp" 13 ) 14 15 func TestAccAWSEBSVolume_basic(t *testing.T) { 16 var v ec2.Volume 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 IDRefreshName: "aws_ebs_volume.test", 20 Providers: testAccProviders, 21 Steps: []resource.TestStep{ 22 { 23 Config: testAccAwsEbsVolumeConfig, 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckVolumeExists("aws_ebs_volume.test", &v), 26 ), 27 }, 28 }, 29 }) 30 } 31 32 func TestAccAWSEBSVolume_kmsKey(t *testing.T) { 33 var v ec2.Volume 34 ri := acctest.RandInt() 35 config := fmt.Sprintf(testAccAwsEbsVolumeConfigWithKmsKey, ri) 36 keyRegex := regexp.MustCompile("^arn:aws:([a-zA-Z0-9\\-])+:([a-z]{2}-[a-z]+-\\d{1})?:(\\d{12})?:(.*)$") 37 38 resource.Test(t, resource.TestCase{ 39 PreCheck: func() { testAccPreCheck(t) }, 40 IDRefreshName: "aws_ebs_volume.test", 41 Providers: testAccProviders, 42 Steps: []resource.TestStep{ 43 { 44 Config: config, 45 Check: resource.ComposeTestCheckFunc( 46 testAccCheckVolumeExists("aws_ebs_volume.test", &v), 47 resource.TestCheckResourceAttr("aws_ebs_volume.test", "encrypted", "true"), 48 resource.TestMatchResourceAttr("aws_ebs_volume.test", "kms_key_id", keyRegex), 49 ), 50 }, 51 }, 52 }) 53 } 54 55 func TestAccAWSEBSVolume_NoIops(t *testing.T) { 56 var v ec2.Volume 57 resource.Test(t, resource.TestCase{ 58 PreCheck: func() { testAccPreCheck(t) }, 59 Providers: testAccProviders, 60 Steps: []resource.TestStep{ 61 { 62 Config: testAccAwsEbsVolumeConfigWithNoIops, 63 Check: resource.ComposeTestCheckFunc( 64 testAccCheckVolumeExists("aws_ebs_volume.iops_test", &v), 65 ), 66 }, 67 }, 68 }) 69 } 70 71 func TestAccAWSEBSVolume_withTags(t *testing.T) { 72 var v ec2.Volume 73 resource.Test(t, resource.TestCase{ 74 PreCheck: func() { testAccPreCheck(t) }, 75 IDRefreshName: "aws_ebs_volume.tags_test", 76 Providers: testAccProviders, 77 Steps: []resource.TestStep{ 78 { 79 Config: testAccAwsEbsVolumeConfigWithTags, 80 Check: resource.ComposeTestCheckFunc( 81 testAccCheckVolumeExists("aws_ebs_volume.tags_test", &v), 82 ), 83 }, 84 }, 85 }) 86 } 87 88 func testAccCheckVolumeExists(n string, v *ec2.Volume) resource.TestCheckFunc { 89 return func(s *terraform.State) error { 90 rs, ok := s.RootModule().Resources[n] 91 if !ok { 92 return fmt.Errorf("Not found: %s", n) 93 } 94 95 if rs.Primary.ID == "" { 96 return fmt.Errorf("No ID is set") 97 } 98 99 conn := testAccProvider.Meta().(*AWSClient).ec2conn 100 101 request := &ec2.DescribeVolumesInput{ 102 VolumeIds: []*string{aws.String(rs.Primary.ID)}, 103 } 104 105 response, err := conn.DescribeVolumes(request) 106 if err == nil { 107 if response.Volumes != nil && len(response.Volumes) > 0 { 108 *v = *response.Volumes[0] 109 return nil 110 } 111 } 112 return fmt.Errorf("Error finding EC2 volume %s", rs.Primary.ID) 113 } 114 } 115 116 const testAccAwsEbsVolumeConfig = ` 117 resource "aws_ebs_volume" "test" { 118 availability_zone = "us-west-2a" 119 size = 1 120 } 121 ` 122 123 const testAccAwsEbsVolumeConfigWithKmsKey = ` 124 resource "aws_kms_key" "foo" { 125 description = "Terraform acc test %d" 126 policy = <<POLICY 127 { 128 "Version": "2012-10-17", 129 "Id": "kms-tf-1", 130 "Statement": [ 131 { 132 "Sid": "Enable IAM User Permissions", 133 "Effect": "Allow", 134 "Principal": { 135 "AWS": "*" 136 }, 137 "Action": "kms:*", 138 "Resource": "*" 139 } 140 ] 141 } 142 POLICY 143 } 144 145 resource "aws_ebs_volume" "test" { 146 availability_zone = "us-west-2a" 147 size = 1 148 encrypted = true 149 kms_key_id = "${aws_kms_key.foo.arn}" 150 } 151 ` 152 153 const testAccAwsEbsVolumeConfigWithTags = ` 154 resource "aws_ebs_volume" "tags_test" { 155 availability_zone = "us-west-2a" 156 size = 1 157 tags { 158 Name = "TerraformTest" 159 } 160 } 161 ` 162 163 const testAccAwsEbsVolumeConfigWithNoIops = ` 164 resource "aws_ebs_volume" "iops_test" { 165 availability_zone = "us-west-2a" 166 size = 10 167 type = "gp2" 168 iops = 0 169 tags { 170 Name = "TerraformTest" 171 } 172 } 173 `