forked from hashicorp/terraform-provider-vsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_test.go
180 lines (156 loc) · 5.69 KB
/
helper_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
package vsphere
import (
"context"
"fmt"
"os"
"regexp"
"testing"
"time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
"github.com/vmware/vic/pkg/vsphere/tags"
)
// testCheckVariables bundles common variables needed by various test checkers.
type testCheckVariables struct {
// A client for various operations.
client *govmomi.Client
// The client for tagging operations.
tagsClient *tags.RestClient
// The subject resource's ID.
resourceID string
// The subject resource's attributes.
resourceAttributes map[string]string
// The ESXi host that a various API call is directed at.
esxiHost string
// The datacenter that a various API call is directed at.
datacenter string
// A timeout to pass to various context creation calls.
timeout time.Duration
}
func testClientVariablesForResource(s *terraform.State, addr string) (testCheckVariables, error) {
rs, ok := s.RootModule().Resources[addr]
if !ok {
return testCheckVariables{}, fmt.Errorf("%s not found in state", addr)
}
return testCheckVariables{
client: testAccProvider.Meta().(*VSphereClient).vimClient,
tagsClient: testAccProvider.Meta().(*VSphereClient).tagsClient,
resourceID: rs.Primary.ID,
resourceAttributes: rs.Primary.Attributes,
esxiHost: os.Getenv("VSPHERE_ESXI_HOST"),
datacenter: os.Getenv("VSPHERE_DATACENTER"),
timeout: time.Minute * 5,
}, nil
}
// testAccESXiFlagSet returns true if VSPHERE_TEST_ESXI is set.
func testAccESXiFlagSet() bool {
return os.Getenv("VSPHERE_TEST_ESXI") != ""
}
// testAccSkipIfNotEsxi skips a test if VSPHERE_TEST_ESXI is not set.
func testAccSkipIfNotEsxi(t *testing.T) {
if !testAccESXiFlagSet() {
t.Skip("set VSPHERE_TEST_ESXI to run ESXi-specific acceptance tests")
}
}
// testAccSkipIfEsxi skips a test if VSPHERE_TEST_ESXI is set.
func testAccSkipIfEsxi(t *testing.T) {
if testAccESXiFlagSet() {
t.Skip("test skipped as VSPHERE_TEST_ESXI is set")
}
}
// expectErrorIfNotVirtualCenter returns the error message that
// validateVirtualCenter returns if VSPHERE_TEST_ESXI is set, to allow for test
// cases that will still run on ESXi, but will expect validation failure.
func expectErrorIfNotVirtualCenter() *regexp.Regexp {
if testAccESXiFlagSet() {
return regexp.MustCompile(errVirtualCenterOnly)
}
return nil
}
// testGetPortGroup is a convenience method to fetch a static port group
// resource for testing.
func testGetPortGroup(s *terraform.State, resourceName string) (*types.HostPortGroup, error) {
tVars, err := testClientVariablesForResource(s, fmt.Sprintf("vsphere_host_port_group.%s", resourceName))
if err != nil {
return nil, err
}
hsID, name, err := splitHostPortGroupID(tVars.resourceID)
if err != nil {
return nil, err
}
ns, err := hostNetworkSystemFromHostSystemID(tVars.client, hsID)
if err != nil {
return nil, fmt.Errorf("error loading host network system: %s", err)
}
return hostPortGroupFromName(tVars.client, ns, name)
}
// testGetVirtualMachine is a convenience method to fetch a virtual machine by
// resource name.
func testGetVirtualMachine(s *terraform.State, resourceName string) (*object.VirtualMachine, error) {
tVars, err := testClientVariablesForResource(s, fmt.Sprintf("vsphere_virtual_machine.%s", resourceName))
if err != nil {
return nil, err
}
uuid, ok := tVars.resourceAttributes["uuid"]
if !ok {
return nil, fmt.Errorf("resource %q has no UUID", resourceName)
}
return virtualMachineFromUUID(tVars.client, uuid)
}
// testGetVirtualMachineProperties is a convenience method that adds an extra
// step to testGetVirtualMachine to get the properties of a virtual machine.
func testGetVirtualMachineProperties(s *terraform.State, resourceName string) (*mo.VirtualMachine, error) {
vm, err := testGetVirtualMachine(s, resourceName)
if err != nil {
return nil, err
}
return virtualMachineProperties(vm)
}
// testPowerOffVM does an immediate power-off of the supplied virtual machine
// resource defined by the supplied resource address name. It is used to help
// set up a test scenarios where a VM is powered off.
func testPowerOffVM(s *terraform.State, resourceName string) error {
vm, err := testGetVirtualMachine(s, resourceName)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout)
defer cancel()
task, err := vm.PowerOff(ctx)
if err != nil {
return fmt.Errorf("error powering off VM: %s", err)
}
tctx, tcancel := context.WithTimeout(context.Background(), defaultAPITimeout)
defer tcancel()
if err := task.Wait(tctx); err != nil {
return fmt.Errorf("error waiting for poweroff: %s", err)
}
return nil
}
// testGetTagCategory gets a tag category by name.
func testGetTagCategory(s *terraform.State, resourceName string) (*tags.Category, error) {
tVars, err := testClientVariablesForResource(s, fmt.Sprintf("vsphere_tag_category.%s", resourceName))
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout)
defer cancel()
category, err := tVars.tagsClient.GetCategory(ctx, tVars.resourceID)
if err != nil {
return nil, fmt.Errorf("could not get tag category for ID %q: %s", tVars.resourceID, err)
}
return category, nil
}
// copyStatePtr returns a TestCheckFunc that copies the reference to the test
// run's state to t. This allows access to the state data in later steps where
// it's not normally accessible (ie: in pre-config parts in another test step).
func copyStatePtr(t **terraform.State) resource.TestCheckFunc {
return func(s *terraform.State) error {
*t = s
return nil
}
}