github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/google/resource_compute_image_test.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/acctest"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"google.golang.org/api/compute/v1"
    11  )
    12  
    13  func TestAccComputeImage_basic(t *testing.T) {
    14  	var image compute.Image
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckComputeImageDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccComputeImage_basic,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckComputeImageExists(
    25  						"google_compute_image.foobar", &image),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccComputeImage_basedondisk(t *testing.T) {
    33  	var image compute.Image
    34  
    35  	resource.Test(t, resource.TestCase{
    36  		PreCheck:     func() { testAccPreCheck(t) },
    37  		Providers:    testAccProviders,
    38  		CheckDestroy: testAccCheckComputeImageDestroy,
    39  		Steps: []resource.TestStep{
    40  			resource.TestStep{
    41  				Config: testAccComputeImage_basedondisk,
    42  				Check: resource.ComposeTestCheckFunc(
    43  					testAccCheckComputeImageExists(
    44  						"google_compute_image.foobar", &image),
    45  				),
    46  			},
    47  		},
    48  	})
    49  }
    50  
    51  func testAccCheckComputeImageDestroy(s *terraform.State) error {
    52  	config := testAccProvider.Meta().(*Config)
    53  
    54  	for _, rs := range s.RootModule().Resources {
    55  		if rs.Type != "google_compute_image" {
    56  			continue
    57  		}
    58  
    59  		_, err := config.clientCompute.Images.Get(
    60  			config.Project, rs.Primary.ID).Do()
    61  		if err == nil {
    62  			return fmt.Errorf("Image still exists")
    63  		}
    64  	}
    65  
    66  	return nil
    67  }
    68  
    69  func testAccCheckComputeImageExists(n string, image *compute.Image) resource.TestCheckFunc {
    70  	return func(s *terraform.State) error {
    71  		rs, ok := s.RootModule().Resources[n]
    72  		if !ok {
    73  			return fmt.Errorf("Not found: %s", n)
    74  		}
    75  
    76  		if rs.Primary.ID == "" {
    77  			return fmt.Errorf("No ID is set")
    78  		}
    79  
    80  		config := testAccProvider.Meta().(*Config)
    81  
    82  		found, err := config.clientCompute.Images.Get(
    83  			config.Project, rs.Primary.ID).Do()
    84  		if err != nil {
    85  			return err
    86  		}
    87  
    88  		if found.Name != rs.Primary.ID {
    89  			return fmt.Errorf("Image not found")
    90  		}
    91  
    92  		*image = *found
    93  
    94  		return nil
    95  	}
    96  }
    97  
    98  var testAccComputeImage_basic = fmt.Sprintf(`
    99  resource "google_compute_image" "foobar" {
   100  	name = "image-test-%s"
   101  	raw_disk {
   102  	  source = "https://storage.googleapis.com/bosh-cpi-artifacts/bosh-stemcell-3262.4-google-kvm-ubuntu-trusty-go_agent-raw.tar.gz"
   103  	}
   104  	create_timeout = 5
   105  }`, acctest.RandString(10))
   106  
   107  var testAccComputeImage_basedondisk = fmt.Sprintf(`
   108  resource "google_compute_disk" "foobar" {
   109  	name = "disk-test-%s"
   110  	zone = "us-central1-a"
   111  	image = "debian-8-jessie-v20160803"
   112  }
   113  resource "google_compute_image" "foobar" {
   114  	name = "image-test-%s"
   115  	source_disk = "${google_compute_disk.foobar.self_link}"
   116  }`, acctest.RandString(10), acctest.RandString(10))