Skip to content

Commit

Permalink
Remember to skip checking data sources for destroy.
Browse files Browse the repository at this point in the history
  • Loading branch information
nat-henderson committed Jan 2, 2019
1 parent 95cebe4 commit eb11508
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 119 deletions.
5 changes: 4 additions & 1 deletion templates/terraform/examples/base_configs/test_file.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
113 changes: 113 additions & 0 deletions third_party/terraform/tests/resource_compute_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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" {
Expand Down
118 changes: 0 additions & 118 deletions third_party/terraform/utils/image_test.go

This file was deleted.

0 comments on commit eb11508

Please sign in to comment.