github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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  		Providers: testAccProviders,
    18  		Steps: []resource.TestStep{
    19  			resource.TestStep{
    20  				Config: testAccAwsEbsVolumeConfig,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccCheckVolumeExists("aws_ebs_volume.test", &v),
    23  				),
    24  			},
    25  		},
    26  	})
    27  }
    28  
    29  func TestAccAWSEBSVolume_withTags(t *testing.T) {
    30  	var v ec2.Volume
    31  	resource.Test(t, resource.TestCase{
    32  		PreCheck:  func() { testAccPreCheck(t) },
    33  		Providers: testAccProviders,
    34  		Steps: []resource.TestStep{
    35  			resource.TestStep{
    36  				Config: testAccAwsEbsVolumeConfigWithTags,
    37  				Check: resource.ComposeTestCheckFunc(
    38  					testAccCheckVolumeExists("aws_ebs_volume.tags_test", &v),
    39  				),
    40  			},
    41  		},
    42  	})
    43  }
    44  
    45  func testAccCheckVolumeExists(n string, v *ec2.Volume) resource.TestCheckFunc {
    46  	return func(s *terraform.State) error {
    47  		rs, ok := s.RootModule().Resources[n]
    48  		if !ok {
    49  			return fmt.Errorf("Not found: %s", n)
    50  		}
    51  
    52  		if rs.Primary.ID == "" {
    53  			return fmt.Errorf("No ID is set")
    54  		}
    55  
    56  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
    57  
    58  		request := &ec2.DescribeVolumesInput{
    59  			VolumeIds: []*string{aws.String(rs.Primary.ID)},
    60  		}
    61  
    62  		response, err := conn.DescribeVolumes(request)
    63  		if err == nil {
    64  			if response.Volumes != nil && len(response.Volumes) > 0 {
    65  				*v = *response.Volumes[0]
    66  				return nil
    67  			}
    68  		}
    69  		return fmt.Errorf("Error finding EC2 volume %s", rs.Primary.ID)
    70  	}
    71  }
    72  
    73  const testAccAwsEbsVolumeConfig = `
    74  resource "aws_ebs_volume" "test" {
    75  	availability_zone = "us-west-2a"
    76  	size = 1
    77  }
    78  `
    79  
    80  const testAccAwsEbsVolumeConfigWithTags = `
    81  resource "aws_ebs_volume" "tags_test" {
    82  	availability_zone = "us-west-2a"
    83  	size = 1
    84  	tags {
    85  		Name = "TerraformTest"
    86  	}
    87  }
    88  `