-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathinstances_test.go
378 lines (332 loc) · 11.1 KB
/
instances_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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package linodego_test
import (
"context"
"strconv"
"testing"
"github.com/linode/linodego"
)
func TestListInstances(t *testing.T) {
if testing.Short() {
t.Skip("Skipping test in short mode.")
}
client, instance, _, teardown, err := setupInstanceWithoutDisks(t, "fixtures/TestListInstances")
defer teardown()
if err != nil {
t.Error(err)
}
listOpts := linodego.NewListOptions(1, "{\"id\": "+strconv.Itoa(instance.ID)+"}")
linodes, err := client.ListInstances(context.Background(), listOpts)
if err != nil {
t.Errorf("Error listing instances, expected struct, got error %v", err)
}
if len(linodes) != 1 {
t.Errorf("Expected a list of instances, but got %v", linodes)
}
if linodes[0].ID != instance.ID {
t.Errorf("Expected list of instances to include test instance, but got %v", linodes)
}
}
func TestGetInstance(t *testing.T) {
if testing.Short() {
t.Skip("Skipping test in short mode.")
}
client, instance, _, teardown, err := setupInstanceWithoutDisks(t, "fixtures/TestGetInstance")
defer teardown()
if err != nil {
t.Error(err)
}
instanceGot, err := client.GetInstance(context.Background(), instance.ID)
if err != nil {
t.Errorf("Error getting instance: %s", err)
}
if instanceGot.ID != instance.ID {
t.Errorf("Expected instance ID %d to match %d", instanceGot.ID, instance.ID)
}
if instance.Specs.Disk <= 0 {
t.Errorf("Error parsing instance spec for disk size: %v", instance.Specs)
}
}
func TestListInstanceDisks(t *testing.T) {
if testing.Short() {
t.Skip("Skipping test in short mode.")
}
client, instance, teardown, err := setupInstance(t, "fixtures/TestListInstanceDisks")
defer teardown()
if err != nil {
t.Error(err)
}
disks, err := client.ListInstanceDisks(context.Background(), instance.ID, nil)
if err != nil {
t.Errorf("Error listing instance disks, expected struct, got error %v", err)
}
if len(disks) == 0 {
t.Errorf("Expected a list of instance disks, but got %v", disks)
}
}
func TestResizeInstanceDisk(t *testing.T) {
if testing.Short() {
t.Skip("Skipping test in short mode.")
}
client, instance, _, teardown, err := setupInstanceWithoutDisks(t, "fixtures/TestResizeInstanceDisk")
defer teardown()
if err != nil {
t.Error(err)
}
instance, err = client.WaitForInstanceStatus(context.Background(), instance.ID, linodego.InstanceOffline, 180)
if err != nil {
t.Errorf("Error waiting for instance readiness for resize: %s", err)
}
disk, err := client.CreateInstanceDisk(context.Background(), instance.ID, linodego.InstanceDiskCreateOptions{
Label: "test",
Filesystem: "ext4",
Size: 2000,
})
if err != nil {
t.Errorf("Error creating disk for resize: %s", err)
}
disk, err = client.WaitForInstanceDiskStatus(context.Background(), instance.ID, disk.ID, linodego.DiskReady, 180)
if err != nil {
t.Errorf("Error waiting for disk readiness for resize: %s", err)
}
err = client.ResizeInstanceDisk(context.Background(), instance.ID, disk.ID, 4000)
if err != nil {
t.Errorf("Error resizing instance disk: %s", err)
}
}
func TestMultiplePrivateInstanceDisk(t *testing.T) {
// This is a long running test
if testing.Short() {
t.Skip("Skipping test in short mode.")
}
client, instance1, teardown1, err := setupInstance(t, "fixtures/TestMultiplePrivateInstanceDiskInitial")
defer teardown1()
if err != nil {
t.Error(err)
}
err = client.BootInstance(context.Background(), instance1.ID, 0)
if err != nil {
t.Error(err)
}
instance1, err = client.WaitForInstanceStatus(context.Background(), instance1.ID, linodego.InstanceRunning, 180)
if err != nil {
t.Errorf("Error waiting for instance readiness: %s", err)
}
disks, err := client.ListInstanceDisks(context.Background(), instance1.ID, nil)
if err != nil {
t.Error(err)
}
disk, err := client.WaitForInstanceDiskStatus(context.Background(), instance1.ID, disks[0].ID, linodego.DiskReady, 180)
if err != nil {
t.Errorf("Error waiting for disk readiness: %s", err)
}
imageCreateOptions := linodego.ImageCreateOptions{Label: "linodego-test-image", DiskID: disk.ID}
image, err := client.CreateImage(context.Background(), imageCreateOptions)
defer client.DeleteImage(context.Background(), image.ID)
if err != nil {
t.Error(err)
}
client, instance2, _, teardown2, err := setupInstanceWithoutDisks(t, "fixtures/TestMultiplePrivateInstanceDiskSecond")
defer teardown2()
if err != nil {
t.Error(err)
}
instance2, err = client.WaitForInstanceStatus(context.Background(), instance2.ID, linodego.InstanceOffline, 180)
if err != nil {
t.Errorf("Error waiting for instance readiness: %s", err)
}
_, err = client.WaitForEventFinished(context.Background(), instance1.ID, linodego.EntityLinode, linodego.ActionDiskImagize, disk.Created, 300)
if err != nil {
t.Errorf("Error waiting for imagize event: %s", err)
}
_, err = client.CreateInstanceDisk(context.Background(), instance2.ID, linodego.InstanceDiskCreateOptions{
Label: "linodego-test-instancedisk",
Image: image.ID,
RootPass: "R34lBAdP455",
Size: 2000,
})
if err != nil {
t.Errorf("Error creating disk from private image: %s", err)
}
disk, err = client.CreateInstanceDisk(context.Background(), instance2.ID, linodego.InstanceDiskCreateOptions{
Label: "linodego-test-2",
Size: 2000,
})
if err != nil {
t.Errorf("Error creating disk after a private image: %s", err)
}
disks, err = client.ListInstanceDisks(context.Background(), instance2.ID, nil)
if err != nil {
t.Errorf("Error listing instance disks, expected struct, got error %v", err)
}
if len(disks) != 2 {
t.Errorf("Expected a list of instance disks, but got %v", disks)
}
}
func TestPasswordResetInstanceDisk(t *testing.T) {
if testing.Short() {
t.Skip("Skipping test in short mode.")
}
client, instance, _, teardown, err := setupInstanceWithoutDisks(t, "fixtures/TestPasswordResetInstanceDisk")
defer teardown()
if err != nil {
t.Error(err)
}
instance, err = client.WaitForInstanceStatus(context.Background(), instance.ID, linodego.InstanceOffline, 180)
if err != nil {
t.Errorf("Error waiting for instance readiness for password reset: %s", err)
}
disk, err := client.CreateInstanceDisk(context.Background(), instance.ID, linodego.InstanceDiskCreateOptions{
Label: "test",
Filesystem: "ext4",
Image: "linode/debian9",
RootPass: "b4d_p455",
Size: 2000,
})
if err != nil {
t.Errorf("Error creating disk for password reset: %s", err)
}
instance, err = client.WaitForInstanceStatus(context.Background(), instance.ID, linodego.InstanceOffline, 180)
if err != nil {
t.Errorf("Error waiting for instance readiness after creating disk for password reset: %s", err)
}
disk, err = client.WaitForInstanceDiskStatus(context.Background(), instance.ID, disk.ID, linodego.DiskReady, 180)
if err != nil {
t.Errorf("Error waiting for disk readiness for password reset: %s", err)
}
err = client.PasswordResetInstanceDisk(context.Background(), instance.ID, disk.ID, "r34!_b4d_p455")
if err != nil {
t.Errorf("Error reseting password on instance disk: %s", err)
}
}
func TestListInstanceConfigs(t *testing.T) {
if testing.Short() {
t.Skip("Skipping test in short mode.")
}
client, instance, config, teardown, err := setupInstanceWithoutDisks(t, "fixtures/TestListInstanceConfigs")
defer teardown()
if err != nil {
t.Error(err)
}
configs, err := client.ListInstanceConfigs(context.Background(), instance.ID, nil)
if err != nil {
t.Errorf("Error listing instance configs, expected struct, got error %v", err)
}
if len(configs) == 0 {
t.Errorf("Expected a list of instance configs, but got %v", configs)
}
if configs[0].ID != config.ID {
t.Errorf("Expected config id %d, got %d", configs[0].ID, config.ID)
}
}
func TestUpdateInstanceConfig(t *testing.T) {
if testing.Short() {
t.Skip("Skipping test in short mode.")
}
client, instance, config, teardown, err := setupInstanceWithoutDisks(t, "fixtures/TestUpdateInstanceConfig")
defer teardown()
if err != nil {
t.Error(err)
}
updateConfigOpts := linodego.InstanceConfigUpdateOptions{
Label: "bar",
Devices: &linodego.InstanceConfigDeviceMap{},
RootDevice: "/dev/root",
}
_, err = client.UpdateInstanceConfig(context.Background(), instance.ID, config.ID, updateConfigOpts)
if err != nil {
t.Error(err)
}
}
func TestListInstanceVolumes(t *testing.T) {
if testing.Short() {
t.Skip("Skipping test in short mode.")
}
client, instance, config, teardown, err := setupInstanceWithoutDisks(t, "fixtures/TestListInstanceVolumes_instance")
defer teardown()
if err != nil {
t.Error(err)
}
clientVol, volume, teardown, err := setupVolume(t, "fixtures/TestListInstanceVolumes")
defer teardown()
if err != nil {
t.Error(err)
}
configOpts := linodego.InstanceConfigUpdateOptions{
Label: "volume-test",
Devices: &linodego.InstanceConfigDeviceMap{
SDA: &linodego.InstanceConfigDevice{
VolumeID: volume.ID,
},
},
}
_, err = client.UpdateInstanceConfig(context.Background(), instance.ID, config.ID, configOpts)
if err != nil {
t.Error(err)
}
volumes, err := clientVol.ListInstanceVolumes(context.Background(), instance.ID, nil)
if err != nil {
t.Errorf("Error listing instance volumes, expected struct, got error %v", err)
}
if len(volumes) == 0 {
t.Errorf("Expected an list of instance volumes, but got %v", volumes)
}
}
func setupInstance(t *testing.T, fixturesYaml string) (*linodego.Client, *linodego.Instance, func(), error) {
if t != nil {
t.Helper()
}
client, fixtureTeardown := createTestClient(t, fixturesYaml)
falseBool := false
createOpts := linodego.InstanceCreateOptions{
Label: "linodego-test-instance",
RootPass: "R34lBAdP455",
Region: "us-west",
Type: "g6-nanode-1",
Image: "linode/debian9",
Booted: &falseBool,
}
instance, err := client.CreateInstance(context.Background(), createOpts)
if err != nil {
t.Errorf("Error creating test Instance: %s", err)
}
teardown := func() {
if err := client.DeleteInstance(context.Background(), instance.ID); err != nil {
if t != nil {
t.Errorf("Error deleting test Instance: %s", err)
}
}
fixtureTeardown()
}
return client, instance, teardown, err
}
func setupInstanceWithoutDisks(t *testing.T, fixturesYaml string) (*linodego.Client, *linodego.Instance, *linodego.InstanceConfig, func(), error) {
t.Helper()
client, fixtureTeardown := createTestClient(t, fixturesYaml)
falseBool := false
createOpts := linodego.InstanceCreateOptions{
Label: "linodego-test-instance-wo-disk",
Region: "us-west",
Type: "g6-nanode-1",
Booted: &falseBool,
}
instance, err := client.CreateInstance(context.Background(), createOpts)
if err != nil {
t.Errorf("Error creating test Instance: %s", err)
return nil, nil, nil, fixtureTeardown, err
}
configOpts := linodego.InstanceConfigCreateOptions{
Label: "linodego-test-config",
}
config, err := client.CreateInstanceConfig(context.Background(), instance.ID, configOpts)
if err != nil {
t.Errorf("Error creating config: %s", err)
return nil, nil, nil, fixtureTeardown, err
}
teardown := func() {
if terr := client.DeleteInstance(context.Background(), instance.ID); terr != nil {
t.Errorf("Error deleting test Instance: %s", terr)
}
fixtureTeardown()
}
return client, instance, config, teardown, err
}