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