github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/google/resource_compute_disk_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 TestAccComputeDisk_basic(t *testing.T) { 14 diskName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 15 var disk compute.Disk 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccCheckComputeDiskDestroy, 21 Steps: []resource.TestStep{ 22 resource.TestStep{ 23 Config: testAccComputeDisk_basic(diskName), 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckComputeDiskExists( 26 "google_compute_disk.foobar", &disk), 27 ), 28 }, 29 }, 30 }) 31 } 32 33 func testAccCheckComputeDiskDestroy(s *terraform.State) error { 34 config := testAccProvider.Meta().(*Config) 35 36 for _, rs := range s.RootModule().Resources { 37 if rs.Type != "google_compute_disk" { 38 continue 39 } 40 41 _, err := config.clientCompute.Disks.Get( 42 config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do() 43 if err == nil { 44 return fmt.Errorf("Disk still exists") 45 } 46 } 47 48 return nil 49 } 50 51 func testAccCheckComputeDiskExists(n string, disk *compute.Disk) resource.TestCheckFunc { 52 return func(s *terraform.State) error { 53 rs, ok := s.RootModule().Resources[n] 54 if !ok { 55 return fmt.Errorf("Not found: %s", n) 56 } 57 58 if rs.Primary.ID == "" { 59 return fmt.Errorf("No ID is set") 60 } 61 62 config := testAccProvider.Meta().(*Config) 63 64 found, err := config.clientCompute.Disks.Get( 65 config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do() 66 if err != nil { 67 return err 68 } 69 70 if found.Name != rs.Primary.ID { 71 return fmt.Errorf("Disk not found") 72 } 73 74 *disk = *found 75 76 return nil 77 } 78 } 79 80 func testAccComputeDisk_basic(diskName string) string { 81 return fmt.Sprintf(` 82 resource "google_compute_disk" "foobar" { 83 name = "%s" 84 image = "debian-8-jessie-v20160803" 85 size = 50 86 type = "pd-ssd" 87 zone = "us-central1-a" 88 }`, diskName) 89 }