github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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 testAccCheckComputeImageDestroy(s *terraform.State) error { 33 config := testAccProvider.Meta().(*Config) 34 35 for _, rs := range s.RootModule().Resources { 36 if rs.Type != "google_compute_image" { 37 continue 38 } 39 40 _, err := config.clientCompute.Images.Get( 41 config.Project, rs.Primary.ID).Do() 42 if err == nil { 43 return fmt.Errorf("Image still exists") 44 } 45 } 46 47 return nil 48 } 49 50 func testAccCheckComputeImageExists(n string, image *compute.Image) resource.TestCheckFunc { 51 return func(s *terraform.State) error { 52 rs, ok := s.RootModule().Resources[n] 53 if !ok { 54 return fmt.Errorf("Not found: %s", n) 55 } 56 57 if rs.Primary.ID == "" { 58 return fmt.Errorf("No ID is set") 59 } 60 61 config := testAccProvider.Meta().(*Config) 62 63 found, err := config.clientCompute.Images.Get( 64 config.Project, rs.Primary.ID).Do() 65 if err != nil { 66 return err 67 } 68 69 if found.Name != rs.Primary.ID { 70 return fmt.Errorf("Image not found") 71 } 72 73 *image = *found 74 75 return nil 76 } 77 } 78 79 var testAccComputeImage_basic = fmt.Sprintf(` 80 resource "google_compute_image" "foobar" { 81 name = "image-test-%s" 82 raw_disk { 83 source = "https://storage.googleapis.com/bosh-cpi-artifacts/bosh-stemcell-3262.4-google-kvm-ubuntu-trusty-go_agent-raw.tar.gz" 84 } 85 }`, acctest.RandString(10))