From eb11508972e627335193e7171a4e0de79fc81d30 Mon Sep 17 00:00:00 2001 From: Nathan McKinley Date: Wed, 2 Jan 2019 13:08:11 -0800 Subject: [PATCH] Remember to skip checking data sources for destroy. --- .../examples/base_configs/test_file.go.erb | 5 +- .../tests/resource_compute_image_test.go | 113 +++++++++++++++++ third_party/terraform/utils/image_test.go | 118 ------------------ 3 files changed, 117 insertions(+), 119 deletions(-) delete mode 100644 third_party/terraform/utils/image_test.go diff --git a/templates/terraform/examples/base_configs/test_file.go.erb b/templates/terraform/examples/base_configs/test_file.go.erb index d383f4402108..849c1fec1bbf 100644 --- a/templates/terraform/examples/base_configs/test_file.go.erb +++ b/templates/terraform/examples/base_configs/test_file.go.erb @@ -53,10 +53,13 @@ func testAcc<%= test_slug -%>(val string) string { <%- end %> func testAccCheck<%= resource_name -%>Destroy(s *terraform.State) error { - for _, rs := range s.RootModule().Resources { + for name, rs := range s.RootModule().Resources { if rs.Type != "<%= terraform_name -%>" { continue } + if strings.HasPrefix(name, "data.") { + continue + } config := testAccProvider.Meta().(*Config) diff --git a/third_party/terraform/tests/resource_compute_image_test.go b/third_party/terraform/tests/resource_compute_image_test.go index 4305ce4096be..29b31a5be8f2 100644 --- a/third_party/terraform/tests/resource_compute_image_test.go +++ b/third_party/terraform/tests/resource_compute_image_test.go @@ -162,6 +162,99 @@ func testAccCheckComputeImageExists(n string, image *compute.Image) resource.Tes } } +func TestAccComputeImage_resolveImage(t *testing.T) { + t.Parallel() + + var image compute.Image + rand := acctest.RandString(10) + name := fmt.Sprintf("test-image-%s", rand) + fam := fmt.Sprintf("test-image-family-%s", rand) + + // You'll note that this test does not have a CheckDestroy. This is not + // a typo - the test "creates" an image from a disk which is created from + // a public image. The purpose of the test is to show that we do NOT, in + // fact, create a new image in this circumstance. So, the resources under + // test are "the latest public debian image" - which of course we do not + // destroy. + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckComputeImageDestroy, + Steps: []resource.TestStep{ + { + Config: testAccComputeImage_resolving(name, fam), + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeImageExists( + "google_compute_image.foobar", &image), + testAccCheckComputeImageResolution("google_compute_image.foobar"), + ), + }, + }, + }) +} + +func testAccCheckComputeImageResolution(n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + config := testAccProvider.Meta().(*Config) + project := config.Project + + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Resource not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No ID is set") + } + if rs.Primary.Attributes["name"] == "" { + return fmt.Errorf("No image name is set") + } + if rs.Primary.Attributes["family"] == "" { + return fmt.Errorf("No image family is set") + } + if rs.Primary.Attributes["self_link"] == "" { + return fmt.Errorf("No self_link is set") + } + + name := rs.Primary.Attributes["name"] + family := rs.Primary.Attributes["family"] + link := rs.Primary.Attributes["self_link"] + + latestDebian, err := config.clientCompute.Images.GetFromFamily("debian-cloud", "debian-9").Do() + if err != nil { + return fmt.Errorf("Error retrieving latest debian: %s", err) + } + + images := map[string]string{ + "family/" + latestDebian.Family: "projects/debian-cloud/global/images/family/" + latestDebian.Family, + "projects/debian-cloud/global/images/" + latestDebian.Name: "projects/debian-cloud/global/images/" + latestDebian.Name, + latestDebian.Family: "projects/debian-cloud/global/images/family/" + latestDebian.Family, + latestDebian.Name: "projects/debian-cloud/global/images/" + latestDebian.Name, + latestDebian.SelfLink: latestDebian.SelfLink, + + "global/images/" + name: "global/images/" + name, + "global/images/family/" + family: "global/images/family/" + family, + name: "global/images/" + name, + family: "global/images/family/" + family, + "family/" + family: "global/images/family/" + family, + project + "/" + name: "projects/" + project + "/global/images/" + name, + project + "/" + family: "projects/" + project + "/global/images/family/" + family, + link: link, + } + + for input, expectation := range images { + result, err := resolveImage(config, project, input) + if err != nil { + return fmt.Errorf("Error resolving input %s to image: %+v\n", input, err) + } + if result != expectation { + return fmt.Errorf("Expected input '%s' to resolve to '%s', it resolved to '%s' instead.\n", input, expectation, result) + } + } + return nil + } +} + func testAccCheckComputeImageDescription(image *compute.Image, description string) resource.TestCheckFunc { return func(s *terraform.State) error { if image.Description != description { @@ -247,6 +340,26 @@ func testAccCheckComputeImageHasSourceDisk(image *compute.Image) resource.TestCh } } +func testAccComputeImage_resolving(name, family string) string { + return fmt.Sprintf(` +data "google_compute_image" "my_image" { + family = "debian-9" + project = "debian-cloud" +} + +resource "google_compute_disk" "foobar" { + name = "%s" + zone = "us-central1-a" + image = "${data.google_compute_image.my_image.self_link}" +} +resource "google_compute_image" "foobar" { + name = "%s" + family = "%s" + source_disk = "${google_compute_disk.foobar.self_link}" +} +`, name, name, family) +} + func testAccComputeImage_basic(name string) string { return fmt.Sprintf(` resource "google_compute_image" "foobar" { diff --git a/third_party/terraform/utils/image_test.go b/third_party/terraform/utils/image_test.go deleted file mode 100644 index 05954f9c6433..000000000000 --- a/third_party/terraform/utils/image_test.go +++ /dev/null @@ -1,118 +0,0 @@ -package google - -import ( - "fmt" - "testing" - - "google.golang.org/api/compute/v1" - - "github.com/hashicorp/terraform/helper/acctest" - "github.com/hashicorp/terraform/helper/resource" - "github.com/hashicorp/terraform/terraform" -) - -func TestAccComputeImage_resolveImage(t *testing.T) { - t.Parallel() - - var image compute.Image - rand := acctest.RandString(10) - name := fmt.Sprintf("test-image-%s", rand) - fam := fmt.Sprintf("test-image-family-%s", rand) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: testAccComputeImage_resolving(name, fam), - Check: resource.ComposeTestCheckFunc( - testAccCheckComputeImageExists( - "google_compute_image.foobar", &image), - testAccCheckComputeImageResolution("google_compute_image.foobar"), - ), - }, - }, - }) -} - -func testAccCheckComputeImageResolution(n string) resource.TestCheckFunc { - return func(s *terraform.State) error { - config := testAccProvider.Meta().(*Config) - project := config.Project - - rs, ok := s.RootModule().Resources[n] - if !ok { - return fmt.Errorf("Resource not found: %s", n) - } - - if rs.Primary.ID == "" { - return fmt.Errorf("No ID is set") - } - if rs.Primary.Attributes["name"] == "" { - return fmt.Errorf("No image name is set") - } - if rs.Primary.Attributes["family"] == "" { - return fmt.Errorf("No image family is set") - } - if rs.Primary.Attributes["self_link"] == "" { - return fmt.Errorf("No self_link is set") - } - - name := rs.Primary.Attributes["name"] - family := rs.Primary.Attributes["family"] - link := rs.Primary.Attributes["self_link"] - - latestDebian, err := config.clientCompute.Images.GetFromFamily("debian-cloud", "debian-9").Do() - if err != nil { - return fmt.Errorf("Error retrieving latest debian: %s", err) - } - - images := map[string]string{ - "family/" + latestDebian.Family: "projects/debian-cloud/global/images/family/" + latestDebian.Family, - "projects/debian-cloud/global/images/" + latestDebian.Name: "projects/debian-cloud/global/images/" + latestDebian.Name, - latestDebian.Family: "projects/debian-cloud/global/images/family/" + latestDebian.Family, - latestDebian.Name: "projects/debian-cloud/global/images/" + latestDebian.Name, - latestDebian.SelfLink: latestDebian.SelfLink, - - "global/images/" + name: "global/images/" + name, - "global/images/family/" + family: "global/images/family/" + family, - name: "global/images/" + name, - family: "global/images/family/" + family, - "family/" + family: "global/images/family/" + family, - project + "/" + name: "projects/" + project + "/global/images/" + name, - project + "/" + family: "projects/" + project + "/global/images/family/" + family, - link: link, - } - - for input, expectation := range images { - result, err := resolveImage(config, project, input) - if err != nil { - return fmt.Errorf("Error resolving input %s to image: %+v\n", input, err) - } - if result != expectation { - return fmt.Errorf("Expected input '%s' to resolve to '%s', it resolved to '%s' instead.\n", input, expectation, result) - } - } - return nil - } -} - -func testAccComputeImage_resolving(name, family string) string { - return fmt.Sprintf(` -data "google_compute_image" "my_image" { - family = "debian-9" - project = "debian-cloud" -} - -resource "google_compute_disk" "foobar" { - name = "%s" - zone = "us-central1-a" - image = "${data.google_compute_image.my_image.self_link}" -} -resource "google_compute_image" "foobar" { - name = "%s" - family = "%s" - source_disk = "${google_compute_disk.foobar.self_link}" -} -`, name, name, family) -}