terraform/builtin/providers/google/resource_compute_disk_test.go

90 lines
2.0 KiB
Go
Raw Normal View History

2014-08-26 01:23:28 +02:00
package google
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/acctest"
2014-08-26 01:23:28 +02:00
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"google.golang.org/api/compute/v1"
2014-08-26 01:23:28 +02:00
)
func TestAccComputeDisk_basic(t *testing.T) {
diskName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
2014-08-26 01:23:28 +02:00
var disk compute.Disk
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeDiskDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeDisk_basic(diskName),
2014-08-26 01:23:28 +02:00
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeDiskExists(
"google_compute_disk.foobar", &disk),
),
},
},
})
}
func testAccCheckComputeDiskDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
for _, rs := range s.RootModule().Resources {
2014-08-26 01:23:28 +02:00
if rs.Type != "google_compute_disk" {
continue
}
_, err := config.clientCompute.Disks.Get(
config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
2014-08-26 01:23:28 +02:00
if err == nil {
return fmt.Errorf("Disk still exists")
}
}
return nil
}
func testAccCheckComputeDiskExists(n string, disk *compute.Disk) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
2014-08-26 01:23:28 +02:00
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
2014-08-26 01:23:28 +02:00
return fmt.Errorf("No ID is set")
}
config := testAccProvider.Meta().(*Config)
found, err := config.clientCompute.Disks.Get(
config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
2014-08-26 01:23:28 +02:00
if err != nil {
return err
}
if found.Name != rs.Primary.ID {
2014-08-26 01:23:28 +02:00
return fmt.Errorf("Disk not found")
}
*disk = *found
return nil
}
}
func testAccComputeDisk_basic(diskName string) string {
return fmt.Sprintf(`
2014-08-26 01:23:28 +02:00
resource "google_compute_disk" "foobar" {
name = "%s"
image = "debian-7-wheezy-v20160301"
2014-08-26 01:23:28 +02:00
size = 50
type = "pd-ssd"
2014-08-26 01:23:28 +02:00
zone = "us-central1-a"
}`, diskName)
}