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