forked from hashicorp/terraform-provider-vsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_vsphere_host_virtual_switch_test.go
248 lines (220 loc) · 6.51 KB
/
resource_vsphere_host_virtual_switch_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package vsphere
import (
"errors"
"fmt"
"os"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccResourceVSphereHostVirtualSwitch(t *testing.T) {
var tp *testing.T
testAccResourceVSphereHostVirtualSwitchCases := []struct {
name string
testCase resource.TestCase
}{
{
"basic",
resource.TestCase{
PreCheck: func() {
testAccPreCheck(tp)
testAccResourceVSphereHostVirtualSwitchPreCheck(tp)
},
Providers: testAccProviders,
CheckDestroy: testAccResourceVSphereHostVirtualSwitchExists(false),
Steps: []resource.TestStep{
{
Config: testAccResourceVSphereHostVirtualSwitchConfig(),
Check: resource.ComposeTestCheckFunc(
testAccResourceVSphereHostVirtualSwitchExists(true),
),
},
},
},
},
{
"basic, then remove a NIC",
resource.TestCase{
PreCheck: func() {
testAccPreCheck(tp)
testAccResourceVSphereHostVirtualSwitchPreCheck(tp)
},
Providers: testAccProviders,
CheckDestroy: testAccResourceVSphereHostVirtualSwitchExists(false),
Steps: []resource.TestStep{
{
Config: testAccResourceVSphereHostVirtualSwitchConfig(),
Check: resource.ComposeTestCheckFunc(
testAccResourceVSphereHostVirtualSwitchExists(true),
),
},
{
Config: testAccResourceVSphereHostVirtualSwitchConfigSingleNIC(),
Check: resource.ComposeTestCheckFunc(
testAccResourceVSphereHostVirtualSwitchExists(true),
),
},
},
},
},
{
"standby with explicit failover order",
resource.TestCase{
PreCheck: func() {
testAccPreCheck(tp)
testAccResourceVSphereHostVirtualSwitchPreCheck(tp)
},
Providers: testAccProviders,
CheckDestroy: testAccResourceVSphereHostVirtualSwitchExists(false),
Steps: []resource.TestStep{
{
Config: testAccResourceVSphereHostVirtualSwitchConfigStandbyLink(),
Check: resource.ComposeTestCheckFunc(
testAccResourceVSphereHostVirtualSwitchExists(true),
),
},
},
},
},
{
"basic, then change to standby with failover order",
resource.TestCase{
PreCheck: func() {
testAccPreCheck(tp)
testAccResourceVSphereHostVirtualSwitchPreCheck(tp)
},
Providers: testAccProviders,
CheckDestroy: testAccResourceVSphereHostVirtualSwitchExists(false),
Steps: []resource.TestStep{
{
Config: testAccResourceVSphereHostVirtualSwitchConfig(),
Check: resource.ComposeTestCheckFunc(
testAccResourceVSphereHostVirtualSwitchExists(true),
),
},
{
Config: testAccResourceVSphereHostVirtualSwitchConfigStandbyLink(),
Check: resource.ComposeTestCheckFunc(
testAccResourceVSphereHostVirtualSwitchExists(true),
),
},
},
},
},
}
for _, tc := range testAccResourceVSphereHostVirtualSwitchCases {
t.Run(tc.name, func(t *testing.T) {
tp = t
resource.Test(t, tc.testCase)
})
}
}
func testAccResourceVSphereHostVirtualSwitchPreCheck(t *testing.T) {
if os.Getenv("VSPHERE_HOST_NIC0") == "" {
t.Skip("set VSPHERE_HOST_NIC0 to run vsphere_host_virtual_switch acceptance tests")
}
if os.Getenv("VSPHERE_HOST_NIC1") == "" {
t.Skip("set VSPHERE_HOST_NIC1 to run vsphere_host_virtual_switch acceptance tests")
}
if os.Getenv("VSPHERE_ESXI_HOST") == "" {
t.Skip("set VSPHERE_ESXI_HOST to run vsphere_host_virtual_switch acceptance tests")
}
}
func testAccResourceVSphereHostVirtualSwitchExists(expected bool) resource.TestCheckFunc {
return func(s *terraform.State) error {
vars, err := testClientVariablesForResource(s, "vsphere_host_virtual_switch.switch")
if err != nil {
return errors.New("vsphere_host_virtual_switch.switch not found in state")
}
hsID, name, err := splitHostVirtualSwitchID(vars.resourceID)
if err != nil {
return err
}
ns, err := hostNetworkSystemFromHostSystemID(vars.client, hsID)
if err != nil {
return fmt.Errorf("error loading host network system: %s", err)
}
_, err = hostVSwitchFromName(vars.client, ns, name)
if err != nil {
if err.Error() == fmt.Sprintf("could not find virtual switch %s", name) && expected == false {
// Expected missing
return nil
}
return err
}
if !expected {
return fmt.Errorf("expected vSwitch %s to be missing", name)
}
return nil
}
}
func testAccResourceVSphereHostVirtualSwitchConfig() string {
return fmt.Sprintf(`
variable "host_nic0" {
default = "%s"
}
variable "host_nic1" {
default = "%s"
}
data "vsphere_datacenter" "datacenter" {
name = "%s"
}
data "vsphere_host" "esxi_host" {
name = "%s"
datacenter_id = "${data.vsphere_datacenter.datacenter.id}"
}
resource "vsphere_host_virtual_switch" "switch" {
name = "vSwitchTerraformTest"
host_system_id = "${data.vsphere_host.esxi_host.id}"
network_adapters = ["${var.host_nic0}", "${var.host_nic1}"]
active_nics = ["${var.host_nic0}", "${var.host_nic1}"]
standby_nics = []
}
`, os.Getenv("VSPHERE_HOST_NIC0"), os.Getenv("VSPHERE_HOST_NIC1"), os.Getenv("VSPHERE_DATACENTER"), os.Getenv("VSPHERE_ESXI_HOST"))
}
func testAccResourceVSphereHostVirtualSwitchConfigSingleNIC() string {
return fmt.Sprintf(`
variable "host_nic0" {
default = "%s"
}
data "vsphere_datacenter" "datacenter" {
name = "%s"
}
data "vsphere_host" "esxi_host" {
name = "%s"
datacenter_id = "${data.vsphere_datacenter.datacenter.id}"
}
resource "vsphere_host_virtual_switch" "switch" {
name = "vSwitchTerraformTest"
host_system_id = "${data.vsphere_host.esxi_host.id}"
network_adapters = ["${var.host_nic0}"]
active_nics = ["${var.host_nic0}"]
standby_nics = []
}
`, os.Getenv("VSPHERE_HOST_NIC0"), os.Getenv("VSPHERE_DATACENTER"), os.Getenv("VSPHERE_ESXI_HOST"))
}
func testAccResourceVSphereHostVirtualSwitchConfigStandbyLink() string {
return fmt.Sprintf(`
variable "host_nic0" {
default = "%s"
}
variable "host_nic1" {
default = "%s"
}
data "vsphere_datacenter" "datacenter" {
name = "%s"
}
data "vsphere_host" "esxi_host" {
name = "%s"
datacenter_id = "${data.vsphere_datacenter.datacenter.id}"
}
resource "vsphere_host_virtual_switch" "switch" {
name = "vSwitchTerraformTest"
host_system_id = "${data.vsphere_host.esxi_host.id}"
network_adapters = ["${var.host_nic0}", "${var.host_nic1}"]
active_nics = ["${var.host_nic0}"]
standby_nics = ["${var.host_nic1}"]
teaming_policy = "failover_explicit"
}
`, os.Getenv("VSPHERE_HOST_NIC0"), os.Getenv("VSPHERE_HOST_NIC1"), os.Getenv("VSPHERE_DATACENTER"), os.Getenv("VSPHERE_ESXI_HOST"))
}