forked from hashicorp/terraform-provider-vsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_vsphere_host_port_group_test.go
267 lines (242 loc) · 7.58 KB
/
resource_vsphere_host_port_group_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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package vsphere
import (
"fmt"
"os"
"reflect"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccResourceVSphereHostPortGroup(t *testing.T) {
var tp *testing.T
testAccResourceVSphereHostPortGroupCases := []struct {
name string
testCase resource.TestCase
}{
{
"basic, inherited policy",
resource.TestCase{
PreCheck: func() {
testAccPreCheck(tp)
testAccResourceVSphereHostPortGroupPreCheck(tp)
},
Providers: testAccProviders,
CheckDestroy: testAccResourceVSphereHostPortGroupExists(false),
Steps: []resource.TestStep{
{
Config: testAccResourceVSphereHostPortGroupConfig(),
Check: resource.ComposeTestCheckFunc(
testAccResourceVSphereHostPortGroupExists(true),
),
},
},
},
},
{
"more complex configuration and overridden attributes",
resource.TestCase{
PreCheck: func() {
testAccPreCheck(tp)
testAccResourceVSphereHostPortGroupPreCheck(tp)
},
Providers: testAccProviders,
CheckDestroy: testAccResourceVSphereHostPortGroupExists(false),
Steps: []resource.TestStep{
{
Config: testAccResourceVSphereHostPortGroupConfigWithOverrides(),
Check: resource.ComposeTestCheckFunc(
testAccResourceVSphereHostPortGroupExists(true),
testAccResourceVSphereHostPortGroupCheckVlan(1000),
testAccResourceVSphereHostPortGroupCheckEffectiveActive([]string{os.Getenv("VSPHERE_HOST_NIC0")}),
testAccResourceVSphereHostPortGroupCheckEffectiveStandby([]string{os.Getenv("VSPHERE_HOST_NIC1")}),
testAccResourceVSphereHostPortGroupCheckEffectivePromisc(true),
),
},
},
},
},
{
"basic, then complex config",
resource.TestCase{
PreCheck: func() {
testAccPreCheck(tp)
testAccResourceVSphereHostPortGroupPreCheck(tp)
},
Providers: testAccProviders,
CheckDestroy: testAccResourceVSphereHostPortGroupExists(false),
Steps: []resource.TestStep{
{
Config: testAccResourceVSphereHostPortGroupConfig(),
Check: resource.ComposeTestCheckFunc(
testAccResourceVSphereHostPortGroupExists(true),
),
},
{
Config: testAccResourceVSphereHostPortGroupConfigWithOverrides(),
Check: resource.ComposeTestCheckFunc(
testAccResourceVSphereHostPortGroupExists(true),
testAccResourceVSphereHostPortGroupCheckVlan(1000),
testAccResourceVSphereHostPortGroupCheckEffectiveActive([]string{os.Getenv("VSPHERE_HOST_NIC0")}),
testAccResourceVSphereHostPortGroupCheckEffectiveStandby([]string{os.Getenv("VSPHERE_HOST_NIC1")}),
testAccResourceVSphereHostPortGroupCheckEffectivePromisc(true),
),
},
},
},
},
}
for _, tc := range testAccResourceVSphereHostPortGroupCases {
t.Run(tc.name, func(t *testing.T) {
tp = t
resource.Test(t, tc.testCase)
})
}
}
func testAccResourceVSphereHostPortGroupPreCheck(t *testing.T) {
if os.Getenv("VSPHERE_HOST_NIC0") == "" {
t.Skip("set VSPHERE_HOST_NIC0 to run vsphere_host_port_group acceptance tests")
}
if os.Getenv("VSPHERE_HOST_NIC1") == "" {
t.Skip("set VSPHERE_HOST_NIC1 to run vsphere_host_port_group acceptance tests")
}
if os.Getenv("VSPHERE_ESXI_HOST") == "" {
t.Skip("set VSPHERE_ESXI_HOST to run vsphere_host_port_group acceptance tests")
}
}
func testAccResourceVSphereHostPortGroupExists(expected bool) resource.TestCheckFunc {
return func(s *terraform.State) error {
name := "PGTerraformTest"
id := "pg"
_, err := testGetPortGroup(s, id)
if err != nil {
if err.Error() == fmt.Sprintf("could not find port group %s", name) && expected == false {
// Expected missing
return nil
}
return err
}
if expected == false {
return fmt.Errorf("expected port group %s to still be missing", name)
}
return nil
}
}
func testAccResourceVSphereHostPortGroupCheckVlan(expected int32) resource.TestCheckFunc {
return func(s *terraform.State) error {
id := "pg"
pg, err := testGetPortGroup(s, id)
if err != nil {
return err
}
actual := pg.Spec.VlanId
if expected != actual {
return fmt.Errorf("expected VLAN ID to be %d, got %d", expected, actual)
}
return nil
}
}
func testAccResourceVSphereHostPortGroupCheckEffectiveActive(expected []string) resource.TestCheckFunc {
return func(s *terraform.State) error {
id := "pg"
pg, err := testGetPortGroup(s, id)
if err != nil {
return err
}
actual := pg.ComputedPolicy.NicTeaming.NicOrder.ActiveNic
if !reflect.DeepEqual(expected, actual) {
return fmt.Errorf("expected effective active NICs to be %#v, got %#v", expected, actual)
}
return nil
}
}
func testAccResourceVSphereHostPortGroupCheckEffectiveStandby(expected []string) resource.TestCheckFunc {
return func(s *terraform.State) error {
id := "pg"
pg, err := testGetPortGroup(s, id)
if err != nil {
return err
}
actual := pg.ComputedPolicy.NicTeaming.NicOrder.StandbyNic
if !reflect.DeepEqual(expected, actual) {
return fmt.Errorf("expected effective standby NICs to be %#v, got %#v", expected, actual)
}
return nil
}
}
func testAccResourceVSphereHostPortGroupCheckEffectivePromisc(expected bool) resource.TestCheckFunc {
return func(s *terraform.State) error {
id := "pg"
pg, err := testGetPortGroup(s, id)
if err != nil {
return err
}
actual := *pg.ComputedPolicy.Security.AllowPromiscuous
if expected != actual {
return fmt.Errorf("expected effective allow promiscuous to be %t, got %t", expected, actual)
}
return nil
}
}
func testAccResourceVSphereHostPortGroupConfig() 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 = []
}
resource "vsphere_host_port_group" "pg" {
name = "PGTerraformTest"
host_system_id = "${data.vsphere_host.esxi_host.id}"
virtual_switch_name = "${vsphere_host_virtual_switch.switch.name}"
}
`, os.Getenv("VSPHERE_HOST_NIC0"), os.Getenv("VSPHERE_HOST_NIC1"), os.Getenv("VSPHERE_DATACENTER"), os.Getenv("VSPHERE_ESXI_HOST"))
}
func testAccResourceVSphereHostPortGroupConfigWithOverrides() 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 = []
allow_promiscuous = false
}
resource "vsphere_host_port_group" "pg" {
name = "PGTerraformTest"
host_system_id = "${data.vsphere_host.esxi_host.id}"
virtual_switch_name = "${vsphere_host_virtual_switch.switch.name}"
vlan_id = 1000
active_nics = ["${var.host_nic0}"]
standby_nics = ["${var.host_nic1}"]
allow_promiscuous = true
}
`, os.Getenv("VSPHERE_HOST_NIC0"), os.Getenv("VSPHERE_HOST_NIC1"), os.Getenv("VSPHERE_DATACENTER"), os.Getenv("VSPHERE_ESXI_HOST"))
}