github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/google/resource_compute_disk_test.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"code.google.com/p/google-api-go-client/compute/v1"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccComputeDisk_basic(t *testing.T) {
    13  	var disk compute.Disk
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckComputeDiskDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccComputeDisk_basic,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckComputeDiskExists(
    24  						"google_compute_disk.foobar", &disk),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func testAccCheckComputeDiskDestroy(s *terraform.State) error {
    32  	config := testAccProvider.Meta().(*Config)
    33  
    34  	for _, rs := range s.RootModule().Resources {
    35  		if rs.Type != "google_compute_disk" {
    36  			continue
    37  		}
    38  
    39  		_, err := config.clientCompute.Disks.Get(
    40  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
    41  		if err == nil {
    42  			return fmt.Errorf("Disk still exists")
    43  		}
    44  	}
    45  
    46  	return nil
    47  }
    48  
    49  func testAccCheckComputeDiskExists(n string, disk *compute.Disk) resource.TestCheckFunc {
    50  	return func(s *terraform.State) error {
    51  		rs, ok := s.RootModule().Resources[n]
    52  		if !ok {
    53  			return fmt.Errorf("Not found: %s", n)
    54  		}
    55  
    56  		if rs.Primary.ID == "" {
    57  			return fmt.Errorf("No ID is set")
    58  		}
    59  
    60  		config := testAccProvider.Meta().(*Config)
    61  
    62  		found, err := config.clientCompute.Disks.Get(
    63  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
    64  		if err != nil {
    65  			return err
    66  		}
    67  
    68  		if found.Name != rs.Primary.ID {
    69  			return fmt.Errorf("Disk not found")
    70  		}
    71  
    72  		*disk = *found
    73  
    74  		return nil
    75  	}
    76  }
    77  
    78  const testAccComputeDisk_basic = `
    79  resource "google_compute_disk" "foobar" {
    80  	name = "terraform-test"
    81  	image = "debian-7-wheezy-v20140814"
    82  	size = 50
    83  	type = "pd-ssd"
    84  	zone = "us-central1-a"
    85  }`