github.com/alexissmirnov/terraform@v0.4.3-0.20150423153700-1ef9731a2f14/builtin/providers/aws/resource_aws_route53_zone_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/hashcode"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  
    11  	"github.com/awslabs/aws-sdk-go/aws"
    12  	"github.com/awslabs/aws-sdk-go/service/route53"
    13  )
    14  
    15  func TestCleanPrefix(t *testing.T) {
    16  	cases := []struct {
    17  		Input, Prefix, Output string
    18  	}{
    19  		{"/hostedzone/foo", "/hostedzone/", "foo"},
    20  		{"/change/foo", "/change/", "foo"},
    21  		{"/bar", "/test", "/bar"},
    22  	}
    23  
    24  	for _, tc := range cases {
    25  		actual := cleanPrefix(tc.Input, tc.Prefix)
    26  		if actual != tc.Output {
    27  			t.Fatalf("input: %s\noutput: %s", tc.Input, actual)
    28  		}
    29  	}
    30  }
    31  
    32  func TestCleanZoneID(t *testing.T) {
    33  	cases := []struct {
    34  		Input, Output string
    35  	}{
    36  		{"/hostedzone/foo", "foo"},
    37  		{"/change/foo", "/change/foo"},
    38  		{"/bar", "/bar"},
    39  	}
    40  
    41  	for _, tc := range cases {
    42  		actual := cleanZoneID(tc.Input)
    43  		if actual != tc.Output {
    44  			t.Fatalf("input: %s\noutput: %s", tc.Input, actual)
    45  		}
    46  	}
    47  }
    48  
    49  func TestCleanChangeID(t *testing.T) {
    50  	cases := []struct {
    51  		Input, Output string
    52  	}{
    53  		{"/hostedzone/foo", "/hostedzone/foo"},
    54  		{"/change/foo", "foo"},
    55  		{"/bar", "/bar"},
    56  	}
    57  
    58  	for _, tc := range cases {
    59  		actual := cleanChangeID(tc.Input)
    60  		if actual != tc.Output {
    61  			t.Fatalf("input: %s\noutput: %s", tc.Input, actual)
    62  		}
    63  	}
    64  }
    65  
    66  func TestAccRoute53Zone(t *testing.T) {
    67  	var zone route53.HostedZone
    68  	var td route53.ResourceTagSet
    69  
    70  	resource.Test(t, resource.TestCase{
    71  		PreCheck:     func() { testAccPreCheck(t) },
    72  		Providers:    testAccProviders,
    73  		CheckDestroy: testAccCheckRoute53ZoneDestroy,
    74  		Steps: []resource.TestStep{
    75  			resource.TestStep{
    76  				Config: testAccRoute53ZoneConfig,
    77  				Check: resource.ComposeTestCheckFunc(
    78  					testAccCheckRoute53ZoneExists("aws_route53_zone.main", &zone),
    79  					testAccLoadTagsR53(&zone, &td),
    80  					testAccCheckTagsR53(&td.Tags, "foo", "bar"),
    81  				),
    82  			},
    83  		},
    84  	})
    85  }
    86  
    87  func testAccCheckRoute53ZoneDestroy(s *terraform.State) error {
    88  	conn := testAccProvider.Meta().(*AWSClient).r53conn
    89  	for _, rs := range s.RootModule().Resources {
    90  		if rs.Type != "aws_route53_zone" {
    91  			continue
    92  		}
    93  
    94  		_, err := conn.GetHostedZone(&route53.GetHostedZoneInput{ID: aws.String(rs.Primary.ID)})
    95  		if err == nil {
    96  			return fmt.Errorf("Hosted zone still exists")
    97  		}
    98  	}
    99  	return nil
   100  }
   101  
   102  func testAccCheckRoute53ZoneExists(n string, zone *route53.HostedZone) resource.TestCheckFunc {
   103  	return func(s *terraform.State) error {
   104  		rs, ok := s.RootModule().Resources[n]
   105  		if !ok {
   106  			return fmt.Errorf("Not found: %s", n)
   107  		}
   108  
   109  		if rs.Primary.ID == "" {
   110  			return fmt.Errorf("No hosted zone ID is set")
   111  		}
   112  
   113  		conn := testAccProvider.Meta().(*AWSClient).r53conn
   114  		resp, err := conn.GetHostedZone(&route53.GetHostedZoneInput{ID: aws.String(rs.Primary.ID)})
   115  		if err != nil {
   116  			return fmt.Errorf("Hosted zone err: %v", err)
   117  		}
   118  
   119  		for _, ns := range resp.DelegationSet.NameServers {
   120  			attribute := fmt.Sprintf("name_servers.%d", hashcode.String(*ns))
   121  			dsns := rs.Primary.Attributes[attribute]
   122  			if dsns != *ns {
   123  				return fmt.Errorf("Got: %v for %v, Expected: %v", dsns, attribute, ns)
   124  			}
   125  		}
   126  
   127  		*zone = *resp.HostedZone
   128  		return nil
   129  	}
   130  }
   131  
   132  func testAccLoadTagsR53(zone *route53.HostedZone, td *route53.ResourceTagSet) resource.TestCheckFunc {
   133  	return func(s *terraform.State) error {
   134  		conn := testAccProvider.Meta().(*AWSClient).r53conn
   135  
   136  		zone := cleanZoneID(*zone.ID)
   137  		req := &route53.ListTagsForResourceInput{
   138  			ResourceID:   aws.String(zone),
   139  			ResourceType: aws.String("hostedzone"),
   140  		}
   141  
   142  		resp, err := conn.ListTagsForResource(req)
   143  		if err != nil {
   144  			return err
   145  		}
   146  
   147  		if resp.ResourceTagSet != nil {
   148  			*td = *resp.ResourceTagSet
   149  		}
   150  
   151  		return nil
   152  	}
   153  }
   154  
   155  const testAccRoute53ZoneConfig = `
   156  resource "aws_route53_zone" "main" {
   157  	name = "hashicorp.com"
   158  
   159  	tags {
   160  		foo = "bar"
   161  		Name = "tf-route53-tag-test"
   162  	}
   163  }
   164  `