Skip to content

Commit e85a618

Browse files
committed
Merge pull request hashicorp#3717 from rakutentech/improve-acceptance-test
provider/vsphere: Fix acceptance tests for using optional parameters
2 parents e6c4a4f + 12fc9a1 commit e85a618

2 files changed

Lines changed: 46 additions & 52 deletions

File tree

builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go

Lines changed: 38 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,21 @@ import (
1515

1616
func TestAccVSphereVirtualMachine_basic(t *testing.T) {
1717
var vm virtualMachine
18-
datacenter := os.Getenv("VSPHERE_DATACENTER")
19-
cluster := os.Getenv("VSPHERE_CLUSTER")
20-
datastore := os.Getenv("VSPHERE_DATASTORE")
18+
var locationOpt string
19+
var datastoreOpt string
20+
21+
if v := os.Getenv("VSPHERE_DATACENTER"); v != "" {
22+
locationOpt += fmt.Sprintf(" datacenter = \"%s\"\n", v)
23+
}
24+
if v := os.Getenv("VSPHERE_CLUSTER"); v != "" {
25+
locationOpt += fmt.Sprintf(" cluster = \"%s\"\n", v)
26+
}
27+
if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" {
28+
locationOpt += fmt.Sprintf(" resource_pool = \"%s\"\n", v)
29+
}
30+
if v := os.Getenv("VSPHERE_DATASTORE"); v != "" {
31+
datastoreOpt = fmt.Sprintf(" datastore = \"%s\"\n", v)
32+
}
2133
template := os.Getenv("VSPHERE_TEMPLATE")
2234
gateway := os.Getenv("VSPHERE_NETWORK_GATEWAY")
2335
label := os.Getenv("VSPHERE_NETWORK_LABEL")
@@ -31,28 +43,23 @@ func TestAccVSphereVirtualMachine_basic(t *testing.T) {
3143
resource.TestStep{
3244
Config: fmt.Sprintf(
3345
testAccCheckVSphereVirtualMachineConfig_basic,
34-
datacenter,
35-
cluster,
46+
locationOpt,
3647
gateway,
3748
label,
3849
ip_address,
39-
datastore,
50+
datastoreOpt,
4051
template,
4152
),
4253
Check: resource.ComposeTestCheckFunc(
4354
testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.foo", &vm),
4455
resource.TestCheckResourceAttr(
4556
"vsphere_virtual_machine.foo", "name", "terraform-test"),
46-
resource.TestCheckResourceAttr(
47-
"vsphere_virtual_machine.foo", "datacenter", datacenter),
4857
resource.TestCheckResourceAttr(
4958
"vsphere_virtual_machine.foo", "vcpu", "2"),
5059
resource.TestCheckResourceAttr(
5160
"vsphere_virtual_machine.foo", "memory", "4096"),
5261
resource.TestCheckResourceAttr(
5362
"vsphere_virtual_machine.foo", "disk.#", "2"),
54-
resource.TestCheckResourceAttr(
55-
"vsphere_virtual_machine.foo", "disk.0.datastore", datastore),
5663
resource.TestCheckResourceAttr(
5764
"vsphere_virtual_machine.foo", "disk.0.template", template),
5865
resource.TestCheckResourceAttr(
@@ -67,12 +74,23 @@ func TestAccVSphereVirtualMachine_basic(t *testing.T) {
6774

6875
func TestAccVSphereVirtualMachine_dhcp(t *testing.T) {
6976
var vm virtualMachine
70-
datacenter := os.Getenv("VSPHERE_DATACENTER")
71-
cluster := os.Getenv("VSPHERE_CLUSTER")
72-
datastore := os.Getenv("VSPHERE_DATASTORE")
77+
var locationOpt string
78+
var datastoreOpt string
79+
80+
if v := os.Getenv("VSPHERE_DATACENTER"); v != "" {
81+
locationOpt += fmt.Sprintf(" datacenter = \"%s\"\n", v)
82+
}
83+
if v := os.Getenv("VSPHERE_CLUSTER"); v != "" {
84+
locationOpt += fmt.Sprintf(" cluster = \"%s\"\n", v)
85+
}
86+
if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" {
87+
locationOpt += fmt.Sprintf(" resource_pool = \"%s\"\n", v)
88+
}
89+
if v := os.Getenv("VSPHERE_DATASTORE"); v != "" {
90+
datastoreOpt = fmt.Sprintf(" datastore = \"%s\"\n", v)
91+
}
7392
template := os.Getenv("VSPHERE_TEMPLATE")
7493
label := os.Getenv("VSPHERE_NETWORK_LABEL_DHCP")
75-
password := os.Getenv("VSPHERE_VM_PASSWORD")
7694

7795
resource.Test(t, resource.TestCase{
7896
PreCheck: func() { testAccPreCheck(t) },
@@ -82,27 +100,21 @@ func TestAccVSphereVirtualMachine_dhcp(t *testing.T) {
82100
resource.TestStep{
83101
Config: fmt.Sprintf(
84102
testAccCheckVSphereVirtualMachineConfig_dhcp,
85-
datacenter,
86-
cluster,
103+
locationOpt,
87104
label,
88-
datastore,
105+
datastoreOpt,
89106
template,
90-
password,
91107
),
92108
Check: resource.ComposeTestCheckFunc(
93109
testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.bar", &vm),
94110
resource.TestCheckResourceAttr(
95111
"vsphere_virtual_machine.bar", "name", "terraform-test"),
96-
resource.TestCheckResourceAttr(
97-
"vsphere_virtual_machine.bar", "datacenter", datacenter),
98112
resource.TestCheckResourceAttr(
99113
"vsphere_virtual_machine.bar", "vcpu", "2"),
100114
resource.TestCheckResourceAttr(
101115
"vsphere_virtual_machine.bar", "memory", "4096"),
102116
resource.TestCheckResourceAttr(
103117
"vsphere_virtual_machine.bar", "disk.#", "1"),
104-
resource.TestCheckResourceAttr(
105-
"vsphere_virtual_machine.bar", "disk.0.datastore", datastore),
106118
resource.TestCheckResourceAttr(
107119
"vsphere_virtual_machine.bar", "disk.0.template", template),
108120
resource.TestCheckResourceAttr(
@@ -168,20 +180,6 @@ func testAccCheckVSphereVirtualMachineExists(n string, vm *virtualMachine) resou
168180
}
169181

170182
_, err = object.NewSearchIndex(client.Client).FindChild(context.TODO(), dcFolders.VmFolder, rs.Primary.Attributes["name"])
171-
/*
172-
vmRef, err := client.SearchIndex().FindChild(dcFolders.VmFolder, rs.Primary.Attributes["name"])
173-
if err != nil {
174-
return fmt.Errorf("error %s", err)
175-
}
176-
177-
found := govmomi.NewVirtualMachine(client, vmRef.Reference())
178-
fmt.Printf("%v", found)
179-
180-
if found.Name != rs.Primary.ID {
181-
return fmt.Errorf("Instance not found")
182-
}
183-
*instance = *found
184-
*/
185183

186184
*vm = virtualMachine{
187185
name: rs.Primary.ID,
@@ -194,8 +192,7 @@ func testAccCheckVSphereVirtualMachineExists(n string, vm *virtualMachine) resou
194192
const testAccCheckVSphereVirtualMachineConfig_basic = `
195193
resource "vsphere_virtual_machine" "foo" {
196194
name = "terraform-test"
197-
datacenter = "%s"
198-
cluster = "%s"
195+
%s
199196
vcpu = 2
200197
memory = 4096
201198
gateway = "%s"
@@ -205,7 +202,7 @@ resource "vsphere_virtual_machine" "foo" {
205202
subnet_mask = "255.255.255.0"
206203
}
207204
disk {
208-
datastore = "%s"
205+
%s
209206
template = "%s"
210207
iops = 500
211208
}
@@ -219,22 +216,15 @@ resource "vsphere_virtual_machine" "foo" {
219216
const testAccCheckVSphereVirtualMachineConfig_dhcp = `
220217
resource "vsphere_virtual_machine" "bar" {
221218
name = "terraform-test"
222-
datacenter = "%s"
223-
cluster = "%s"
219+
%s
224220
vcpu = 2
225221
memory = 4096
226222
network_interface {
227223
label = "%s"
228224
}
229225
disk {
230-
datastore = "%s"
226+
%s
231227
template = "%s"
232228
}
233-
234-
connection {
235-
host = "${self.network_interface.0.ip_address}"
236-
user = "root"
237-
password = "%s"
238-
}
239229
}
240230
`

website/source/docs/providers/vsphere/index.html.markdown

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,19 @@ configuration fields to be set using the documented environment variables.
6464

6565
In addition, the following environment variables are used in tests, and must be set to valid values for your vSphere environment:
6666

67-
* VSPHERE\_CLUSTER
68-
* VSPHERE\_DATACENTER
69-
* VSPHERE\_DATASTORE
7067
* VSPHERE\_NETWORK\_GATEWAY
7168
* VSPHERE\_NETWORK\_IP\_ADDRESS
7269
* VSPHERE\_NETWORK\_LABEL
7370
* VSPHERE\_NETWORK\_LABEL\_DHCP
7471
* VSPHERE\_TEMPLATE
75-
* VSPHERE\_VM\_PASSWORD
72+
73+
The following environment variables depend on your vSphere environment:
74+
75+
* VSPHERE\_DATACENTER
76+
* VSPHERE\_CLUSTER
77+
* VSPHERE\_RESOURCE\_POOL
78+
* VSPHERE\_DATASTORE
79+
7680

7781
These are used to set and verify attributes on the `vsphere_virtual_machine`
7882
resource in tests.

0 commit comments

Comments
 (0)