github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/google/image_test.go (about) 1 package google 2 3 import ( 4 "fmt" 5 "testing" 6 7 compute "google.golang.org/api/compute/v1" 8 9 "github.com/hashicorp/terraform/helper/acctest" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccComputeImage_resolveImage(t *testing.T) { 15 var image compute.Image 16 rand := acctest.RandString(10) 17 name := fmt.Sprintf("test-image-%s", rand) 18 fam := fmt.Sprintf("test-image-family-%s", rand) 19 20 resource.Test(t, resource.TestCase{ 21 PreCheck: func() { testAccPreCheck(t) }, 22 Providers: testAccProviders, 23 CheckDestroy: testAccCheckComputeImageDestroy, 24 Steps: []resource.TestStep{ 25 { 26 Config: testAccComputeImage_resolving(name, fam), 27 Check: resource.ComposeTestCheckFunc( 28 testAccCheckComputeImageExists( 29 "google_compute_image.foobar", &image), 30 testAccCheckComputeImageResolution("google_compute_image.foobar"), 31 ), 32 }, 33 }, 34 }) 35 } 36 37 func testAccCheckComputeImageResolution(n string) resource.TestCheckFunc { 38 return func(s *terraform.State) error { 39 config := testAccProvider.Meta().(*Config) 40 project := config.Project 41 42 rs, ok := s.RootModule().Resources[n] 43 if !ok { 44 return fmt.Errorf("Resource not found: %s", n) 45 } 46 47 if rs.Primary.ID == "" { 48 return fmt.Errorf("No ID is set") 49 } 50 if rs.Primary.Attributes["name"] == "" { 51 return fmt.Errorf("No image name is set") 52 } 53 if rs.Primary.Attributes["family"] == "" { 54 return fmt.Errorf("No image family is set") 55 } 56 if rs.Primary.Attributes["self_link"] == "" { 57 return fmt.Errorf("No self_link is set") 58 } 59 60 name := rs.Primary.Attributes["name"] 61 family := rs.Primary.Attributes["family"] 62 link := rs.Primary.Attributes["self_link"] 63 64 images := map[string]string{ 65 "family/debian-8": "projects/debian-cloud/global/images/family/debian-8", 66 "projects/debian-cloud/global/images/debian-8-jessie-v20170110": "projects/debian-cloud/global/images/debian-8-jessie-v20170110", 67 "debian-8": "projects/debian-cloud/global/images/family/debian-8", 68 "debian-8-jessie-v20170110": "projects/debian-cloud/global/images/debian-8-jessie-v20170110", 69 "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20170110": "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20170110", 70 71 "global/images/" + name: "global/images/" + name, 72 "global/images/family/" + family: "global/images/family/" + family, 73 name: "global/images/" + name, 74 family: "global/images/family/" + family, 75 "family/" + family: "global/images/family/" + family, 76 project + "/" + name: "projects/" + project + "/global/images/" + name, 77 project + "/" + family: "projects/" + project + "/global/images/family/" + family, 78 link: link, 79 } 80 81 for input, expectation := range images { 82 result, err := resolveImage(config, input) 83 if err != nil { 84 return fmt.Errorf("Error resolving input %s to image: %+v\n", input, err) 85 } 86 if result != expectation { 87 return fmt.Errorf("Expected input '%s' to resolve to '%s', it resolved to '%s' instead.\n", input, expectation, result) 88 } 89 } 90 return nil 91 } 92 } 93 94 func testAccComputeImage_resolving(name, family string) string { 95 return fmt.Sprintf(` 96 resource "google_compute_disk" "foobar" { 97 name = "%s" 98 zone = "us-central1-a" 99 image = "debian-8-jessie-v20160803" 100 } 101 resource "google_compute_image" "foobar" { 102 name = "%s" 103 family = "%s" 104 source_disk = "${google_compute_disk.foobar.self_link}" 105 } 106 `, name, name, family) 107 }