github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_ebs_snapshot_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 TestAccAWSEBSSnapshot_basic(t *testing.T) { 14 var v ec2.Snapshot 15 resource.Test(t, resource.TestCase{ 16 PreCheck: func() { testAccPreCheck(t) }, 17 Providers: testAccProviders, 18 Steps: []resource.TestStep{ 19 { 20 Config: testAccAwsEbsSnapshotConfig, 21 Check: resource.ComposeTestCheckFunc( 22 testAccCheckSnapshotExists("aws_ebs_snapshot.test", &v), 23 ), 24 }, 25 }, 26 }) 27 } 28 29 func TestAccAWSEBSSnapshot_withDescription(t *testing.T) { 30 var v ec2.Snapshot 31 resource.Test(t, resource.TestCase{ 32 PreCheck: func() { testAccPreCheck(t) }, 33 Providers: testAccProviders, 34 Steps: []resource.TestStep{ 35 { 36 Config: testAccAwsEbsSnapshotConfigWithDescription, 37 Check: resource.ComposeTestCheckFunc( 38 testAccCheckSnapshotExists("aws_ebs_snapshot.test", &v), 39 resource.TestCheckResourceAttr("aws_ebs_snapshot.test", "description", "EBS Snapshot Acceptance Test"), 40 ), 41 }, 42 }, 43 }) 44 } 45 46 func testAccCheckSnapshotExists(n string, v *ec2.Snapshot) resource.TestCheckFunc { 47 return func(s *terraform.State) error { 48 rs, ok := s.RootModule().Resources[n] 49 if !ok { 50 return fmt.Errorf("Not found: %s", n) 51 } 52 53 if rs.Primary.ID == "" { 54 return fmt.Errorf("No ID is set") 55 } 56 57 conn := testAccProvider.Meta().(*AWSClient).ec2conn 58 59 request := &ec2.DescribeSnapshotsInput{ 60 SnapshotIds: []*string{aws.String(rs.Primary.ID)}, 61 } 62 63 response, err := conn.DescribeSnapshots(request) 64 if err == nil { 65 if response.Snapshots != nil && len(response.Snapshots) > 0 { 66 *v = *response.Snapshots[0] 67 return nil 68 } 69 } 70 return fmt.Errorf("Error finding EC2 Snapshot %s", rs.Primary.ID) 71 } 72 } 73 74 const testAccAwsEbsSnapshotConfig = ` 75 resource "aws_ebs_volume" "test" { 76 availability_zone = "us-west-2a" 77 size = 1 78 } 79 80 resource "aws_ebs_snapshot" "test" { 81 volume_id = "${aws_ebs_volume.test.id}" 82 } 83 ` 84 85 const testAccAwsEbsSnapshotConfigWithDescription = ` 86 resource "aws_ebs_volume" "description_test" { 87 availability_zone = "us-west-2a" 88 size = 1 89 } 90 91 resource "aws_ebs_snapshot" "test" { 92 volume_id = "${aws_ebs_volume.description_test.id}" 93 description = "EBS Snapshot Acceptance Test" 94 } 95 `