github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/aws/data_source_aws_route53_zone_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/acctest"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccDataSourceAwsRoute53Zone(t *testing.T) {
    13  	rInt := acctest.RandInt()
    14  	publicResourceName := "aws_route53_zone.test"
    15  	publicDomain := fmt.Sprintf("terraformtestacchz-%d.com.", rInt)
    16  	privateResourceName := "aws_route53_zone.test_private"
    17  	privateDomain := fmt.Sprintf("test.acc-%d.", rInt)
    18  
    19  	resource.Test(t, resource.TestCase{
    20  		PreCheck:  func() { testAccPreCheck(t) },
    21  		Providers: testAccProviders,
    22  		Steps: []resource.TestStep{
    23  			{
    24  				Config: testAccDataSourceAwsRoute53ZoneConfig(rInt),
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccDataSourceAwsRoute53ZoneCheck(
    27  						publicResourceName, "data.aws_route53_zone.by_zone_id", publicDomain),
    28  					testAccDataSourceAwsRoute53ZoneCheck(
    29  						publicResourceName, "data.aws_route53_zone.by_name", publicDomain),
    30  					testAccDataSourceAwsRoute53ZoneCheck(
    31  						privateResourceName, "data.aws_route53_zone.by_vpc", privateDomain),
    32  					testAccDataSourceAwsRoute53ZoneCheck(
    33  						privateResourceName, "data.aws_route53_zone.by_tag", privateDomain),
    34  				),
    35  			},
    36  		},
    37  	})
    38  }
    39  
    40  // rsName for the name of the created resource
    41  // dsName for the name of the created data source
    42  // zName for the name of the domain
    43  func testAccDataSourceAwsRoute53ZoneCheck(rsName, dsName, zName string) resource.TestCheckFunc {
    44  	return func(s *terraform.State) error {
    45  		rs, ok := s.RootModule().Resources[rsName]
    46  		if !ok {
    47  			return fmt.Errorf("root module has no resource called %s", rsName)
    48  		}
    49  
    50  		hostedZone, ok := s.RootModule().Resources[dsName]
    51  		if !ok {
    52  			return fmt.Errorf("can't find zone %q in state", dsName)
    53  		}
    54  
    55  		attr := rs.Primary.Attributes
    56  		if attr["id"] != hostedZone.Primary.Attributes["id"] {
    57  			return fmt.Errorf(
    58  				"id is %s; want %s",
    59  				attr["id"],
    60  				hostedZone.Primary.Attributes["id"],
    61  			)
    62  		}
    63  
    64  		if attr["name"] != zName {
    65  			return fmt.Errorf("Route53 Zone name is %q; want %q", attr["name"], zName)
    66  		}
    67  
    68  		return nil
    69  	}
    70  }
    71  
    72  func testAccDataSourceAwsRoute53ZoneConfig(rInt int) string {
    73  	return fmt.Sprintf(`
    74  	provider "aws" {
    75  		region = "us-east-1"
    76  	}
    77  
    78  	resource "aws_vpc" "test" {
    79  		cidr_block = "172.16.0.0/16"
    80  	}
    81  
    82  	resource "aws_route53_zone" "test_private" {
    83  		name = "test.acc-%d."
    84  		vpc_id = "${aws_vpc.test.id}"
    85  		tags {
    86  			Environment = "dev-%d"
    87  		}
    88  	}
    89  
    90  	data "aws_route53_zone" "by_vpc" {
    91  	 name = "${aws_route53_zone.test_private.name}"
    92  	 vpc_id = "${aws_vpc.test.id}"
    93  	}
    94  
    95  	data "aws_route53_zone" "by_tag" {
    96  	 name = "${aws_route53_zone.test_private.name}"
    97  	 private_zone = true
    98  	 tags {
    99  		 Environment = "dev-%d"
   100  	 }
   101  	}
   102  
   103  	resource "aws_route53_zone" "test" {
   104  		name = "terraformtestacchz-%d.com."
   105  	}
   106  
   107  	data "aws_route53_zone" "by_zone_id" {
   108  		zone_id = "${aws_route53_zone.test.zone_id}"
   109  	}
   110  
   111  	data "aws_route53_zone" "by_name" {
   112  		name = "${data.aws_route53_zone.by_zone_id.name}"
   113  	}`, rInt, rInt, rInt, rInt)
   114  }