github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestAccAWSEBSVolume_basic(t *testing.T) { 14 var v ec2.Volume 15 resource.Test(t, resource.TestCase{ 16 PreCheck: func() { testAccPreCheck(t) }, 17 IDRefreshName: "aws_ebs_volume.test", 18 Providers: testAccProviders, 19 Steps: []resource.TestStep{ 20 resource.TestStep{ 21 Config: testAccAwsEbsVolumeConfig, 22 Check: resource.ComposeTestCheckFunc( 23 testAccCheckVolumeExists("aws_ebs_volume.test", &v), 24 ), 25 }, 26 }, 27 }) 28 } 29 30 func TestAccAWSEBSVolume_NoIops(t *testing.T) { 31 var v ec2.Volume 32 resource.Test(t, resource.TestCase{ 33 PreCheck: func() { testAccPreCheck(t) }, 34 Providers: testAccProviders, 35 Steps: []resource.TestStep{ 36 resource.TestStep{ 37 Config: testAccAwsEbsVolumeConfigWithNoIops, 38 Check: resource.ComposeTestCheckFunc( 39 testAccCheckVolumeExists("aws_ebs_volume.iops_test", &v), 40 ), 41 }, 42 }, 43 }) 44 } 45 46 func TestAccAWSEBSVolume_withTags(t *testing.T) { 47 var v ec2.Volume 48 resource.Test(t, resource.TestCase{ 49 PreCheck: func() { testAccPreCheck(t) }, 50 IDRefreshName: "aws_ebs_volume.tags_test", 51 Providers: testAccProviders, 52 Steps: []resource.TestStep{ 53 resource.TestStep{ 54 Config: testAccAwsEbsVolumeConfigWithTags, 55 Check: resource.ComposeTestCheckFunc( 56 testAccCheckVolumeExists("aws_ebs_volume.tags_test", &v), 57 ), 58 }, 59 }, 60 }) 61 } 62 63 func testAccCheckVolumeExists(n string, v *ec2.Volume) resource.TestCheckFunc { 64 return func(s *terraform.State) error { 65 rs, ok := s.RootModule().Resources[n] 66 if !ok { 67 return fmt.Errorf("Not found: %s", n) 68 } 69 70 if rs.Primary.ID == "" { 71 return fmt.Errorf("No ID is set") 72 } 73 74 conn := testAccProvider.Meta().(*AWSClient).ec2conn 75 76 request := &ec2.DescribeVolumesInput{ 77 VolumeIds: []*string{aws.String(rs.Primary.ID)}, 78 } 79 80 response, err := conn.DescribeVolumes(request) 81 if err == nil { 82 if response.Volumes != nil && len(response.Volumes) > 0 { 83 *v = *response.Volumes[0] 84 return nil 85 } 86 } 87 return fmt.Errorf("Error finding EC2 volume %s", rs.Primary.ID) 88 } 89 } 90 91 const testAccAwsEbsVolumeConfig = ` 92 resource "aws_ebs_volume" "test" { 93 availability_zone = "us-west-2a" 94 size = 1 95 } 96 ` 97 98 const testAccAwsEbsVolumeConfigWithTags = ` 99 resource "aws_ebs_volume" "tags_test" { 100 availability_zone = "us-west-2a" 101 size = 1 102 tags { 103 Name = "TerraformTest" 104 } 105 } 106 ` 107 108 const testAccAwsEbsVolumeConfigWithNoIops = ` 109 resource "aws_ebs_volume" "iops_test" { 110 availability_zone = "us-west-2a" 111 size = 10 112 type = "gp2" 113 iops = 0 114 tags { 115 Name = "TerraformTest" 116 } 117 } 118 `