github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/vsphere/resource_vsphere_virtual_disk_test.go (about)

     1  package vsphere
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/terraform/helper/acctest"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  	"github.com/vmware/govmomi"
    13  	"github.com/vmware/govmomi/find"
    14  	"golang.org/x/net/context"
    15  )
    16  
    17  func TestAccVSphereVirtualDisk_basic(t *testing.T) {
    18  	var datacenterOpt string
    19  	var datastoreOpt string
    20  	var initTypeOpt string
    21  	var adapterTypeOpt string
    22  
    23  	rString := acctest.RandString(5)
    24  
    25  	if v := os.Getenv("VSPHERE_DATACENTER"); v != "" {
    26  		datacenterOpt = v
    27  	}
    28  	if v := os.Getenv("VSPHERE_DATASTORE"); v != "" {
    29  		datastoreOpt = v
    30  	}
    31  	if v := os.Getenv("VSPHERE_INIT_TYPE"); v != "" {
    32  		initTypeOpt += fmt.Sprintf("    type = \"%s\"\n", v)
    33  	} else {
    34  		initTypeOpt += fmt.Sprintf("    type = \"%s\"\n", "thin")
    35  	}
    36  	if v := os.Getenv("VSPHERE_ADAPTER_TYPE"); v != "" {
    37  		adapterTypeOpt += fmt.Sprintf("    adapter_type = \"%s\"\n", v)
    38  	}
    39  
    40  	resource.Test(t, resource.TestCase{
    41  		PreCheck:     func() { testAccPreCheck(t) },
    42  		Providers:    testAccProviders,
    43  		CheckDestroy: testAccCheckVSphereVirtualDiskDestroy,
    44  		Steps: []resource.TestStep{
    45  			{
    46  				Config: testAccCheckVSphereVirtuaDiskConfig_basic(rString, initTypeOpt, adapterTypeOpt, datacenterOpt, datastoreOpt),
    47  				Check: resource.ComposeTestCheckFunc(
    48  					testAccVSphereVirtualDiskExists("vsphere_virtual_disk.foo"),
    49  				),
    50  			},
    51  		},
    52  	})
    53  }
    54  
    55  func testAccVSphereVirtualDiskExists(name string) resource.TestCheckFunc {
    56  	return func(s *terraform.State) error {
    57  		rs, ok := s.RootModule().Resources[name]
    58  		if !ok {
    59  			return fmt.Errorf("Not found: %s", name)
    60  		}
    61  		if rs.Primary.ID == "" {
    62  			return fmt.Errorf("No ID is set")
    63  		}
    64  
    65  		client := testAccProvider.Meta().(*govmomi.Client)
    66  		finder := find.NewFinder(client.Client, true)
    67  
    68  		dc, err := finder.Datacenter(context.TODO(), rs.Primary.Attributes["datacenter"])
    69  		if err != nil {
    70  			return fmt.Errorf("error %s", err)
    71  		}
    72  		finder = finder.SetDatacenter(dc)
    73  
    74  		ds, err := finder.Datastore(context.TODO(), rs.Primary.Attributes["datastore"])
    75  		if err != nil {
    76  			return fmt.Errorf("error %s", err)
    77  		}
    78  
    79  		_, err = ds.Stat(context.TODO(), rs.Primary.Attributes["vmdk_path"])
    80  		if err != nil {
    81  			return fmt.Errorf("error %s", err)
    82  		}
    83  
    84  		return nil
    85  	}
    86  }
    87  
    88  func testAccCheckVSphereVirtualDiskDestroy(s *terraform.State) error {
    89  	log.Printf("[FINDME] test Destroy")
    90  	client := testAccProvider.Meta().(*govmomi.Client)
    91  	finder := find.NewFinder(client.Client, true)
    92  
    93  	for _, rs := range s.RootModule().Resources {
    94  		if rs.Type != "vsphere_virtual_disk" {
    95  			continue
    96  		}
    97  
    98  		dc, err := finder.Datacenter(context.TODO(), rs.Primary.Attributes["datacenter"])
    99  		if err != nil {
   100  			return fmt.Errorf("error %s", err)
   101  		}
   102  
   103  		finder = finder.SetDatacenter(dc)
   104  
   105  		ds, err := finder.Datastore(context.TODO(), rs.Primary.Attributes["datastore"])
   106  		if err != nil {
   107  			return fmt.Errorf("error %s", err)
   108  		}
   109  
   110  		_, err = ds.Stat(context.TODO(), rs.Primary.Attributes["vmdk_path"])
   111  		if err == nil {
   112  			return fmt.Errorf("error %s", err)
   113  		}
   114  	}
   115  
   116  	return nil
   117  }
   118  
   119  func testAccCheckVSphereVirtuaDiskConfig_basic(rName, initTypeOpt, adapterTypeOpt, datacenterOpt, datastoreOpt string) string {
   120  	return fmt.Sprintf(`
   121  resource "vsphere_virtual_disk" "foo" {
   122      size = 1
   123      vmdk_path = "tfTestDisk-%s.vmdk"
   124  %s
   125  %s
   126      datacenter = "%s"
   127      datastore = "%s"
   128  }
   129  `, rName, initTypeOpt, adapterTypeOpt, datacenterOpt, datastoreOpt)
   130  }