github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/data_source_aws_eip_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 TestAccDataSourceAwsEip(t *testing.T) {
    12  	resource.Test(t, resource.TestCase{
    13  		PreCheck:  func() { testAccPreCheck(t) },
    14  		Providers: testAccProviders,
    15  		Steps: []resource.TestStep{
    16  			resource.TestStep{
    17  				Config: testAccDataSourceAwsEipConfig,
    18  				Check: resource.ComposeTestCheckFunc(
    19  					testAccDataSourceAwsEipCheck("data.aws_eip.by_id"),
    20  					testAccDataSourceAwsEipCheck("data.aws_eip.by_public_ip"),
    21  				),
    22  			},
    23  		},
    24  	})
    25  }
    26  
    27  func testAccDataSourceAwsEipCheck(name string) resource.TestCheckFunc {
    28  	return func(s *terraform.State) error {
    29  		rs, ok := s.RootModule().Resources[name]
    30  		if !ok {
    31  			return fmt.Errorf("root module has no resource called %s", name)
    32  		}
    33  
    34  		eipRs, ok := s.RootModule().Resources["aws_eip.test"]
    35  		if !ok {
    36  			return fmt.Errorf("can't find aws_eip.test in state")
    37  		}
    38  
    39  		attr := rs.Primary.Attributes
    40  
    41  		if attr["id"] != eipRs.Primary.Attributes["id"] {
    42  			return fmt.Errorf(
    43  				"id is %s; want %s",
    44  				attr["id"],
    45  				eipRs.Primary.Attributes["id"],
    46  			)
    47  		}
    48  
    49  		if attr["public_ip"] != eipRs.Primary.Attributes["public_ip"] {
    50  			return fmt.Errorf(
    51  				"public_ip is %s; want %s",
    52  				attr["public_ip"],
    53  				eipRs.Primary.Attributes["public_ip"],
    54  			)
    55  		}
    56  
    57  		return nil
    58  	}
    59  }
    60  
    61  const testAccDataSourceAwsEipConfig = `
    62  provider "aws" {
    63    region = "us-west-2"
    64  }
    65  
    66  resource "aws_eip" "wrong1" {}
    67  resource "aws_eip" "test" {}
    68  resource "aws_eip" "wrong2" {}
    69  
    70  data "aws_eip" "by_id" {
    71    id = "${aws_eip.test.id}"
    72  }
    73  
    74  data "aws_eip" "by_public_ip" {
    75    public_ip = "${aws_eip.test.public_ip}"
    76  }
    77  `