mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-09-07 01:26:43 +00:00
149 lines
3.8 KiB
Go
149 lines
3.8 KiB
Go
package google
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
"github.com/hashicorp/terraform/terraform"
|
|
"google.golang.org/api/compute/v1"
|
|
)
|
|
|
|
func TestAccComputeImage_basic(t *testing.T) {
|
|
var image compute.Image
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testAccCheckComputeImageDestroy,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: testAccComputeImage_basic,
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckComputeImageExists(
|
|
"google_compute_image.foobar", &image),
|
|
testAccCheckComputeImageDescription(&image, "description-test"),
|
|
testAccCheckComputeImageFamily(&image, "family-test"),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccComputeImage_basedondisk(t *testing.T) {
|
|
var image compute.Image
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testAccCheckComputeImageDestroy,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: testAccComputeImage_basedondisk,
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckComputeImageExists(
|
|
"google_compute_image.foobar", &image),
|
|
testAccCheckComputeImageHasSourceDisk(&image),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func testAccCheckComputeImageDestroy(s *terraform.State) error {
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
if rs.Type != "google_compute_image" {
|
|
continue
|
|
}
|
|
|
|
_, err := config.clientCompute.Images.Get(
|
|
config.Project, rs.Primary.ID).Do()
|
|
if err == nil {
|
|
return fmt.Errorf("Image still exists")
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func testAccCheckComputeImageExists(n string, image *compute.Image) 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 ID is set")
|
|
}
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
found, err := config.clientCompute.Images.Get(
|
|
config.Project, rs.Primary.ID).Do()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if found.Name != rs.Primary.ID {
|
|
return fmt.Errorf("Image not found")
|
|
}
|
|
|
|
*image = *found
|
|
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func testAccCheckComputeImageDescription(image *compute.Image, description string) resource.TestCheckFunc {
|
|
return func(s *terraform.State) error {
|
|
if image.Description != description {
|
|
return fmt.Errorf("Wrong image description: expected '%s' got '%s'", description, image.Description)
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func testAccCheckComputeImageFamily(image *compute.Image, family string) resource.TestCheckFunc {
|
|
return func(s *terraform.State) error {
|
|
if image.Family != family {
|
|
return fmt.Errorf("Wrong image family: expected '%s' got '%s'", family, image.Family)
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func testAccCheckComputeImageHasSourceDisk(image *compute.Image) resource.TestCheckFunc {
|
|
return func(s *terraform.State) error {
|
|
if image.SourceType == "" {
|
|
return fmt.Errorf("No source disk")
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
var testAccComputeImage_basic = fmt.Sprintf(`
|
|
resource "google_compute_image" "foobar" {
|
|
name = "image-test-%s"
|
|
description = "description-test"
|
|
family = "family-test"
|
|
raw_disk {
|
|
source = "https://storage.googleapis.com/bosh-cpi-artifacts/bosh-stemcell-3262.4-google-kvm-ubuntu-trusty-go_agent-raw.tar.gz"
|
|
}
|
|
create_timeout = 5
|
|
}`, acctest.RandString(10))
|
|
|
|
var testAccComputeImage_basedondisk = fmt.Sprintf(`
|
|
resource "google_compute_disk" "foobar" {
|
|
name = "disk-test-%s"
|
|
zone = "us-central1-a"
|
|
image = "debian-8-jessie-v20160803"
|
|
}
|
|
resource "google_compute_image" "foobar" {
|
|
name = "image-test-%s"
|
|
source_disk = "${google_compute_disk.foobar.self_link}"
|
|
}`, acctest.RandString(10), acctest.RandString(10))
|