github.com/paulmey/terraform@v0.5.2-0.20150519145237-046e9b4c884d/builtin/providers/aws/resource_aws_route53_zone_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  	"github.com/hashicorp/terraform/terraform"
    11  
    12  	"github.com/awslabs/aws-sdk-go/aws"
    13  	"github.com/awslabs/aws-sdk-go/service/route53"
    14  )
    15  
    16  func TestCleanPrefix(t *testing.T) {
    17  	cases := []struct {
    18  		Input, Prefix, Output string
    19  	}{
    20  		{"/hostedzone/foo", "/hostedzone/", "foo"},
    21  		{"/change/foo", "/change/", "foo"},
    22  		{"/bar", "/test", "/bar"},
    23  	}
    24  
    25  	for _, tc := range cases {
    26  		actual := cleanPrefix(tc.Input, tc.Prefix)
    27  		if actual != tc.Output {
    28  			t.Fatalf("input: %s\noutput: %s", tc.Input, actual)
    29  		}
    30  	}
    31  }
    32  
    33  func TestCleanZoneID(t *testing.T) {
    34  	cases := []struct {
    35  		Input, Output string
    36  	}{
    37  		{"/hostedzone/foo", "foo"},
    38  		{"/change/foo", "/change/foo"},
    39  		{"/bar", "/bar"},
    40  	}
    41  
    42  	for _, tc := range cases {
    43  		actual := cleanZoneID(tc.Input)
    44  		if actual != tc.Output {
    45  			t.Fatalf("input: %s\noutput: %s", tc.Input, actual)
    46  		}
    47  	}
    48  }
    49  
    50  func TestCleanChangeID(t *testing.T) {
    51  	cases := []struct {
    52  		Input, Output string
    53  	}{
    54  		{"/hostedzone/foo", "/hostedzone/foo"},
    55  		{"/change/foo", "foo"},
    56  		{"/bar", "/bar"},
    57  	}
    58  
    59  	for _, tc := range cases {
    60  		actual := cleanChangeID(tc.Input)
    61  		if actual != tc.Output {
    62  			t.Fatalf("input: %s\noutput: %s", tc.Input, actual)
    63  		}
    64  	}
    65  }
    66  
    67  func TestAccRoute53Zone_basic(t *testing.T) {
    68  	var zone route53.GetHostedZoneOutput
    69  	var td route53.ResourceTagSet
    70  
    71  	resource.Test(t, resource.TestCase{
    72  		PreCheck:     func() { testAccPreCheck(t) },
    73  		Providers:    testAccProviders,
    74  		CheckDestroy: testAccCheckRoute53ZoneDestroy,
    75  		Steps: []resource.TestStep{
    76  			resource.TestStep{
    77  				Config: testAccRoute53ZoneConfig,
    78  				Check: resource.ComposeTestCheckFunc(
    79  					testAccCheckRoute53ZoneExists("aws_route53_zone.main", &zone),
    80  					testAccLoadTagsR53(&zone, &td),
    81  					testAccCheckTagsR53(&td.Tags, "foo", "bar"),
    82  				),
    83  			},
    84  		},
    85  	})
    86  }
    87  
    88  func TestAccRoute53Zone_private_basic(t *testing.T) {
    89  	var zone route53.GetHostedZoneOutput
    90  
    91  	resource.Test(t, resource.TestCase{
    92  		PreCheck:     func() { testAccPreCheck(t) },
    93  		Providers:    testAccProviders,
    94  		CheckDestroy: testAccCheckRoute53ZoneDestroy,
    95  		Steps: []resource.TestStep{
    96  			resource.TestStep{
    97  				Config: testAccRoute53PrivateZoneConfig,
    98  				Check: resource.ComposeTestCheckFunc(
    99  					testAccCheckRoute53ZoneExists("aws_route53_zone.main", &zone),
   100  					testAccCheckRoute53ZoneAssociatesWithVpc("aws_vpc.main", &zone),
   101  				),
   102  			},
   103  		},
   104  	})
   105  }
   106  
   107  func TestAccRoute53Zone_private_region(t *testing.T) {
   108  	var zone route53.GetHostedZoneOutput
   109  
   110  	// record the initialized providers so that we can use them to
   111  	// check for the instances in each region
   112  	var providers []*schema.Provider
   113  	providerFactories := map[string]terraform.ResourceProviderFactory{
   114  		"aws": func() (terraform.ResourceProvider, error) {
   115  			p := Provider()
   116  			providers = append(providers, p.(*schema.Provider))
   117  			return p, nil
   118  		},
   119  	}
   120  
   121  	resource.Test(t, resource.TestCase{
   122  		PreCheck:          func() { testAccPreCheck(t) },
   123  		ProviderFactories: providerFactories,
   124  		CheckDestroy:      testAccCheckRoute53ZoneDestroyWithProviders(&providers),
   125  		Steps: []resource.TestStep{
   126  			resource.TestStep{
   127  				Config: testAccRoute53PrivateZoneRegionConfig,
   128  				Check: resource.ComposeTestCheckFunc(
   129  					testAccCheckRoute53ZoneExistsWithProviders("aws_route53_zone.main", &zone, &providers),
   130  					testAccCheckRoute53ZoneAssociatesWithVpc("aws_vpc.main", &zone),
   131  				),
   132  			},
   133  		},
   134  	})
   135  }
   136  
   137  func testAccCheckRoute53ZoneDestroy(s *terraform.State) error {
   138  	return testAccCheckRoute53ZoneDestroyWithProvider(s, testAccProvider)
   139  }
   140  
   141  func testAccCheckRoute53ZoneDestroyWithProviders(providers *[]*schema.Provider) resource.TestCheckFunc {
   142  	return func(s *terraform.State) error {
   143  		for _, provider := range *providers {
   144  			if provider.Meta() == nil {
   145  				continue
   146  			}
   147  			if err := testAccCheckRoute53ZoneDestroyWithProvider(s, provider); err != nil {
   148  				return err
   149  			}
   150  		}
   151  		return nil
   152  	}
   153  }
   154  
   155  func testAccCheckRoute53ZoneDestroyWithProvider(s *terraform.State, provider *schema.Provider) error {
   156  	conn := provider.Meta().(*AWSClient).r53conn
   157  	for _, rs := range s.RootModule().Resources {
   158  		if rs.Type != "aws_route53_zone" {
   159  			continue
   160  		}
   161  
   162  		_, err := conn.GetHostedZone(&route53.GetHostedZoneInput{ID: aws.String(rs.Primary.ID)})
   163  		if err == nil {
   164  			return fmt.Errorf("Hosted zone still exists")
   165  		}
   166  	}
   167  	return nil
   168  }
   169  
   170  func testAccCheckRoute53ZoneExists(n string, zone *route53.GetHostedZoneOutput) resource.TestCheckFunc {
   171  	return func(s *terraform.State) error {
   172  		return testAccCheckRoute53ZoneExistsWithProvider(s, n, zone, testAccProvider)
   173  	}
   174  }
   175  
   176  func testAccCheckRoute53ZoneExistsWithProviders(n string, zone *route53.GetHostedZoneOutput, providers *[]*schema.Provider) resource.TestCheckFunc {
   177  	return func(s *terraform.State) error {
   178  		for _, provider := range *providers {
   179  			if provider.Meta() == nil {
   180  				continue
   181  			}
   182  			if err := testAccCheckRoute53ZoneExistsWithProvider(s, n, zone, provider); err != nil {
   183  				return err
   184  			}
   185  		}
   186  		return nil
   187  	}
   188  }
   189  
   190  func testAccCheckRoute53ZoneExistsWithProvider(s *terraform.State, n string, zone *route53.GetHostedZoneOutput, provider *schema.Provider) error {
   191  	rs, ok := s.RootModule().Resources[n]
   192  	if !ok {
   193  		return fmt.Errorf("Not found: %s", n)
   194  	}
   195  
   196  	if rs.Primary.ID == "" {
   197  		return fmt.Errorf("No hosted zone ID is set")
   198  	}
   199  
   200  	conn := provider.Meta().(*AWSClient).r53conn
   201  	resp, err := conn.GetHostedZone(&route53.GetHostedZoneInput{ID: aws.String(rs.Primary.ID)})
   202  	if err != nil {
   203  		return fmt.Errorf("Hosted zone err: %v", err)
   204  	}
   205  
   206  	if !*resp.HostedZone.Config.PrivateZone {
   207  		sorted_ns := make([]string, len(resp.DelegationSet.NameServers))
   208  		for i, ns := range resp.DelegationSet.NameServers {
   209  			sorted_ns[i] = *ns
   210  		}
   211  		sort.Strings(sorted_ns)
   212  		for idx, ns := range sorted_ns {
   213  			attribute := fmt.Sprintf("name_servers.%d", idx)
   214  			dsns := rs.Primary.Attributes[attribute]
   215  			if dsns != ns {
   216  				return fmt.Errorf("Got: %v for %v, Expected: %v", dsns, attribute, ns)
   217  			}
   218  		}
   219  	}
   220  
   221  	*zone = *resp
   222  	return nil
   223  }
   224  
   225  func testAccCheckRoute53ZoneAssociatesWithVpc(n string, zone *route53.GetHostedZoneOutput) resource.TestCheckFunc {
   226  	return func(s *terraform.State) error {
   227  		rs, ok := s.RootModule().Resources[n]
   228  		if !ok {
   229  			return fmt.Errorf("Not found: %s", n)
   230  		}
   231  
   232  		if rs.Primary.ID == "" {
   233  			return fmt.Errorf("No VPC ID is set")
   234  		}
   235  
   236  		var associatedVPC *route53.VPC
   237  		for _, vpc := range zone.VPCs {
   238  			if *vpc.VPCID == rs.Primary.ID {
   239  				associatedVPC = vpc
   240  			}
   241  		}
   242  		if associatedVPC == nil {
   243  			return fmt.Errorf("VPC: %v is not associated to Zone: %v", n, cleanZoneID(*zone.HostedZone.ID))
   244  		}
   245  		return nil
   246  	}
   247  }
   248  
   249  func testAccLoadTagsR53(zone *route53.GetHostedZoneOutput, td *route53.ResourceTagSet) resource.TestCheckFunc {
   250  	return func(s *terraform.State) error {
   251  		conn := testAccProvider.Meta().(*AWSClient).r53conn
   252  
   253  		zone := cleanZoneID(*zone.HostedZone.ID)
   254  		req := &route53.ListTagsForResourceInput{
   255  			ResourceID:   aws.String(zone),
   256  			ResourceType: aws.String("hostedzone"),
   257  		}
   258  
   259  		resp, err := conn.ListTagsForResource(req)
   260  		if err != nil {
   261  			return err
   262  		}
   263  
   264  		if resp.ResourceTagSet != nil {
   265  			*td = *resp.ResourceTagSet
   266  		}
   267  
   268  		return nil
   269  	}
   270  }
   271  
   272  const testAccRoute53ZoneConfig = `
   273  resource "aws_route53_zone" "main" {
   274  	name = "hashicorp.com"
   275  
   276  	tags {
   277  		foo = "bar"
   278  		Name = "tf-route53-tag-test"
   279  	}
   280  }
   281  `
   282  
   283  const testAccRoute53PrivateZoneConfig = `
   284  resource "aws_vpc" "main" {
   285  	cidr_block = "172.29.0.0/24"
   286  	instance_tenancy = "default"
   287  	enable_dns_support = true
   288  	enable_dns_hostnames = true
   289  }
   290  
   291  resource "aws_route53_zone" "main" {
   292  	name = "hashicorp.com"
   293  	vpc_id = "${aws_vpc.main.id}"
   294  }
   295  `
   296  
   297  const testAccRoute53PrivateZoneRegionConfig = `
   298  provider "aws" {
   299  	alias = "west"
   300  	region = "us-west-2"
   301  }
   302  
   303  provider "aws" {
   304  	alias = "east"
   305  	region = "us-east-1"
   306  }
   307  
   308  resource "aws_vpc" "main" {
   309  	provider = "aws.east"
   310  	cidr_block = "172.29.0.0/24"
   311  	instance_tenancy = "default"
   312  	enable_dns_support = true
   313  	enable_dns_hostnames = true
   314  }
   315  
   316  resource "aws_route53_zone" "main" {
   317  	provider = "aws.west"
   318  	name = "hashicorp.com"
   319  	vpc_id = "${aws_vpc.main.id}"
   320  	vpc_region = "us-east-1"
   321  }
   322  `