From 53a42eaa0f7b20e7e28f6541865dd31f4b2b98a4 Mon Sep 17 00:00:00 2001 From: clint shryock Date: Tue, 2 Feb 2016 12:37:06 -0600 Subject: [PATCH] provider/aws: Add a regression test for Route53 records This is a follow up on #4892 with tests that demonstrate creating a record and a zone, then destroying said record, and confirming that a new plan is generated, using the ExpectNonEmptyPlan flag This simulates the bug reported in #4641 by mimicking the state file that one would have if they created a record with Terraform v0.6.6, which is to say a weight = 0 for a default value. When upgrading, there would be an expected plan change to get that to -1. To mimic the statefile we apply the record and then in a follow up step change the attributes directly. We then try to delete the record. I tested this by grabbing the source of aws_resource_route53.go from Terraform v0.6.9 and running the included test, which fails. The test will pass with #4892 , because we no longer reconstruct what the record should be based on the state (instead finding via the API and elimination/matching) --- .../aws/resource_aws_route53_record.go | 2 +- .../aws/resource_aws_route53_record_test.go | 58 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/builtin/providers/aws/resource_aws_route53_record.go b/builtin/providers/aws/resource_aws_route53_record.go index 2678385c1..7fa9aae12 100644 --- a/builtin/providers/aws/resource_aws_route53_record.go +++ b/builtin/providers/aws/resource_aws_route53_record.go @@ -355,7 +355,7 @@ func resourceAwsRoute53RecordDelete(d *schema.ResourceData, meta interface{}) er } } - // Create the new records + // Change batch for deleting changeBatch := &route53.ChangeBatch{ Comment: aws.String("Deleted by Terraform"), Changes: []*route53.Change{ diff --git a/builtin/providers/aws/resource_aws_route53_record_test.go b/builtin/providers/aws/resource_aws_route53_record_test.go index f07215df5..65df31729 100644 --- a/builtin/providers/aws/resource_aws_route53_record_test.go +++ b/builtin/providers/aws/resource_aws_route53_record_test.go @@ -258,6 +258,58 @@ func TestAccAWSRoute53Record_TypeChange(t *testing.T) { }) } +// Test record deletion out of band and make sure we render a new plan +// Part of regression test(s) for https://github.com/hashicorp/terraform/pull/4892 +func TestAccAWSRoute53Record_planUpdate(t *testing.T) { + var zone route53.GetHostedZoneOutput + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckRoute53RecordDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccRoute53RecordConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckRoute53RecordExists("aws_route53_record.default"), + testAccCheckRoute53ZoneExists("aws_route53_zone.main", &zone), + ), + }, + resource.TestStep{ + Config: testAccRoute53RecordConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckRoute53DeleteRecord("aws_route53_record.default", &zone), + ), + ExpectNonEmptyPlan: true, + }, + resource.TestStep{ + Config: testAccRoute53RecordNoConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckRoute53ZoneExists("aws_route53_zone.main", &zone), + ), + }, + }, + }) +} + +func testAccCheckRoute53DeleteRecord(n string, zone *route53.GetHostedZoneOutput) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No hosted zone ID is set") + } + + // Manually set the weight to 0 to replicate a record created in Terraform + // pre-0.6.9 + rs.Primary.Attributes["weight"] = "0" + + return nil + } +} + func testAccCheckRoute53RecordDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).r53conn for _, rs := range s.RootModule().Resources { @@ -354,6 +406,12 @@ resource "aws_route53_record" "default" { } ` +const testAccRoute53RecordNoConfig = ` +resource "aws_route53_zone" "main" { + name = "notexample.com" +} +` + const testAccRoute53RecordConfigSuffix = ` resource "aws_route53_zone" "main" { name = "notexample.com"