forked from hashicorp/terraform-provider-vsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_vsphere_virtual_machine_snapshot_test.go
213 lines (183 loc) · 5.61 KB
/
resource_vsphere_virtual_machine_snapshot_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
package vsphere
import (
"context"
"fmt"
"os"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccResourceVSphereVirtualMachineSnapshot_Basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccResourceVSphereVirtualMachineSnapshotPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccResourceVSphereVirtualMachineSnapshotConfig(true),
Check: resource.ComposeTestCheckFunc(
testAccCheckVirtualMachineSnapshotExists("vsphere_virtual_machine_snapshot.snapshot"),
resource.TestCheckResourceAttr(
"vsphere_virtual_machine_snapshot.snapshot", "snapshot_name", "terraform-test-snapshot"),
),
},
resource.TestStep{
Config: testAccResourceVSphereVirtualMachineSnapshotConfig(false),
Check: resource.ComposeTestCheckFunc(
testAccCheckVirtualMachineHasNoSnapshots("vsphere_virtual_machine.vm"),
),
},
},
})
}
func testAccResourceVSphereVirtualMachineSnapshotPreCheck(t *testing.T) {
if os.Getenv("VSPHERE_DATACENTER") == "" {
t.Skip("set VSPHERE_DATACENTER to run vsphere_virtual_machine_snapshot acceptance tests")
}
if os.Getenv("VSPHERE_CLUSTER") == "" {
t.Skip("set VSPHERE_CLUSTER to run vsphere_virtual_machine_snapshot acceptance tests")
}
if os.Getenv("VSPHERE_RESOURCE_POOL") == "" {
t.Skip("set VSPHERE_RESOURCE_POOL to run vsphere_virtual_machine_snapshot acceptance tests")
}
if os.Getenv("VSPHERE_NETWORK_LABEL") == "" {
t.Skip("set VSPHERE_NETWORK_LABEL to run vsphere_virtual_machine_snapshot acceptance tests")
}
if os.Getenv("VSPHERE_IPV4_ADDRESS") == "" {
t.Skip("set VSPHERE_IPV4_ADDRESS to run vsphere_virtual_machine_snapshot acceptance tests")
}
if os.Getenv("VSPHERE_IPV4_PREFIX") == "" {
t.Skip("set VSPHERE_IPV4_PREFIX to run vsphere_virtual_machine_snapshot acceptance tests")
}
if os.Getenv("VSPHERE_IPV4_GATEWAY") == "" {
t.Skip("set VSPHERE_IPV4_GATEWAY to run vsphere_virtual_machine_snapshot acceptance tests")
}
if os.Getenv("VSPHERE_DATASTORE") == "" {
t.Skip("set VSPHERE_DATASTORE to run vsphere_virtual_machine_snapshot acceptance tests")
}
if os.Getenv("VSPHERE_TEMPLATE") == "" {
t.Skip("set VSPHERE_TEMPLATE to run vsphere_virtual_machine_snapshot acceptance tests")
}
}
func testAccCheckVirtualMachineSnapshotExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No Vm Snapshot ID is set")
}
client := testAccProvider.Meta().(*VSphereClient).vimClient
vm, err := virtualMachineFromUUID(client, rs.Primary.Attributes["virtual_machine_uuid"])
if err != nil {
return fmt.Errorf("error %s", err)
}
ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout) // This is 5 mins
defer cancel()
snapshot, err := vm.FindSnapshot(ctx, rs.Primary.ID)
if err != nil {
return fmt.Errorf("Error while getting the snapshot %v", snapshot)
}
return nil
}
}
func testAccCheckVirtualMachineHasNoSnapshots(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No VM ID is set")
}
client := testAccProvider.Meta().(*VSphereClient).vimClient
vm, err := virtualMachineFromUUID(client, rs.Primary.Attributes["uuid"])
if err != nil {
return fmt.Errorf("error %s", err)
}
props, err := virtualMachineProperties(vm)
if err != nil {
return fmt.Errorf("cannot get properties for virtual machine: %s", err)
}
if props.Snapshot != nil {
return fmt.Errorf("expected VM to not have snapshots, got %#v", props.Snapshot)
}
return nil
}
}
func testAccResourceVSphereVirtualMachineSnapshotConfig(enabled bool) string {
return fmt.Sprintf(`
variable "datacenter" {
default = "%s"
}
variable "cluster" {
default = "%s"
}
variable "resource_pool" {
default = "%s"
}
variable "network_label" {
default = "%s"
}
variable "ipv4_addr" {
default = "%s"
}
variable "ipv4_prefix" {
default = "%s"
}
variable "ipv4_gateway" {
default = "%s"
}
variable "datastore" {
default = "%s"
}
variable "template" {
default = "%s"
}
variable "snapshot_enabled" {
default = "%t"
}
resource "vsphere_virtual_machine" "vm" {
name = "terraform-test"
datacenter = "${var.datacenter}"
cluster = "${var.cluster}"
resource_pool = "${var.resource_pool}"
vcpu = 2
memory = 1024
network_interface {
label = "${var.network_label}"
ipv4_address = "${var.ipv4_addr}"
ipv4_prefix_length = "${var.ipv4_prefix}"
ipv4_gateway = "${var.ipv4_gateway}"
}
disk {
datastore = "${var.datastore}"
template = "${var.template}"
iops = 500
}
linked_clone = true
}
resource "vsphere_virtual_machine_snapshot" "snapshot" {
count = "${var.snapshot_enabled == "true" ? 1 : 0 }"
virtual_machine_uuid = "${vsphere_virtual_machine.vm.uuid}"
snapshot_name = "terraform-test-snapshot"
description = "Managed by Terraform"
memory = true
quiesce = true
}
`,
os.Getenv("VSPHERE_DATACENTER"),
os.Getenv("VSPHERE_CLUSTER"),
os.Getenv("VSPHERE_RESOURCE_POOL"),
os.Getenv("VSPHERE_NETWORK_LABEL"),
os.Getenv("VSPHERE_IPV4_ADDRESS"),
os.Getenv("VSPHERE_IPV4_PREFIX"),
os.Getenv("VSPHERE_IPV4_GATEWAY"),
os.Getenv("VSPHERE_DATASTORE"),
os.Getenv("VSPHERE_TEMPLATE"),
enabled,
)
}