github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/data_source_aws_ebs_snapshot_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  func TestAccAWSEbsSnapshotDataSource_basic(t *testing.T) {
    12  	resource.Test(t, resource.TestCase{
    13  		PreCheck:  func() { testAccPreCheck(t) },
    14  		Providers: testAccProviders,
    15  		Steps: []resource.TestStep{
    16  			{
    17  				Config: testAccCheckAwsEbsSnapshotDataSourceConfig,
    18  				Check: resource.ComposeTestCheckFunc(
    19  					testAccCheckAwsEbsSnapshotDataSourceID("data.aws_ebs_snapshot.snapshot"),
    20  					resource.TestCheckResourceAttr("data.aws_ebs_snapshot.snapshot", "volume_size", "40"),
    21  				),
    22  			},
    23  		},
    24  	})
    25  }
    26  
    27  func TestAccAWSEbsSnapshotDataSource_multipleFilters(t *testing.T) {
    28  	resource.Test(t, resource.TestCase{
    29  		PreCheck:  func() { testAccPreCheck(t) },
    30  		Providers: testAccProviders,
    31  		Steps: []resource.TestStep{
    32  			{
    33  				Config: testAccCheckAwsEbsSnapshotDataSourceConfigWithMultipleFilters,
    34  				Check: resource.ComposeTestCheckFunc(
    35  					testAccCheckAwsEbsSnapshotDataSourceID("data.aws_ebs_snapshot.snapshot"),
    36  					resource.TestCheckResourceAttr("data.aws_ebs_snapshot.snapshot", "volume_size", "10"),
    37  				),
    38  			},
    39  		},
    40  	})
    41  }
    42  
    43  func testAccCheckAwsEbsSnapshotDataSourceID(n string) resource.TestCheckFunc {
    44  	return func(s *terraform.State) error {
    45  		rs, ok := s.RootModule().Resources[n]
    46  		if !ok {
    47  			return fmt.Errorf("Can't find Volume data source: %s", n)
    48  		}
    49  
    50  		if rs.Primary.ID == "" {
    51  			return fmt.Errorf("Snapshot data source ID not set")
    52  		}
    53  		return nil
    54  	}
    55  }
    56  
    57  const testAccCheckAwsEbsSnapshotDataSourceConfig = `
    58  resource "aws_ebs_volume" "example" {
    59      availability_zone = "us-west-2a"
    60      type = "gp2"
    61      size = 40
    62      tags {
    63          Name = "External Volume"
    64      }
    65  }
    66  
    67  resource "aws_ebs_snapshot" "snapshot" {
    68      volume_id = "${aws_ebs_volume.example.id}"
    69  }
    70  
    71  data "aws_ebs_snapshot" "snapshot" {
    72      most_recent = true
    73      snapshot_ids = ["${aws_ebs_snapshot.snapshot.id}"]
    74  }
    75  `
    76  
    77  const testAccCheckAwsEbsSnapshotDataSourceConfigWithMultipleFilters = `
    78  resource "aws_ebs_volume" "external1" {
    79      availability_zone = "us-west-2a"
    80      type = "gp2"
    81      size = 10
    82      tags {
    83          Name = "External Volume 1"
    84      }
    85  }
    86  
    87  resource "aws_ebs_snapshot" "snapshot" {
    88      volume_id = "${aws_ebs_volume.external1.id}"
    89  }
    90  
    91  data "aws_ebs_snapshot" "snapshot" {
    92      most_recent = true
    93      snapshot_ids = ["${aws_ebs_snapshot.snapshot.id}"]
    94      filter {
    95  	name = "volume-size"
    96  	values = ["10"]
    97      }
    98  }
    99  `