File: instance_snapshots_test.go

package info (click to toggle)
golang-github-linode-linodego 1.47.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 10,032 kB
  • sloc: makefile: 95; sh: 52; python: 24
file content (136 lines) | stat: -rw-r--r-- 4,436 bytes parent folder | download
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
package integration

import (
	"context"
	"testing"
	"time"

	"github.com/linode/linodego"
)

var testSnapshotLabel = "snapshot-linodego-testing"

func TestInstanceBackups_List(t *testing.T) {
	client, instance, backup, teardown, err := setupInstanceBackup(t, "fixtures/TestInstanceBackups_List")
	defer teardown()
	if err != nil {
		t.Error(err)
	}

	backupGotten, err := client.GetInstanceSnapshot(context.Background(), instance.ID, backup.ID)
	if err != nil {
		t.Errorf("Error getting backup: %v", err)
	} else if backupGotten.Label != backup.Label {
		t.Errorf("Error getting backup, Labels dont match")
	}

	// Get updated instance info
	instance, err = client.GetInstance(context.Background(), instance.ID)
	if err != nil {
		t.Fatalf("failed to get instance: %s", err)
	}

	if !instance.Backups.Available {
		t.Fatalf("expected Backups.Available to be true, got false")
	}

	assertDateSet(t, backupGotten.Created)
	assertDateSet(t, backupGotten.Updated)

	backups, err := client.GetInstanceBackups(context.Background(), instance.ID)
	if err != nil {
		t.Errorf("Error getting backups: %v", err)
	}

	if backups.Snapshot.InProgress == nil && backups.Snapshot.Current == nil {
		t.Errorf("Error getting snapshot: No Current or InProgress Snapshot")
	}

	if backups.Snapshot.InProgress != nil && backups.Snapshot.InProgress.Label != testSnapshotLabel {
		t.Errorf("Expected snapshot did not match inprogress snapshot: %v", backups.Snapshot.InProgress)
	} else if backups.Snapshot.Current != nil && backups.Snapshot.Current.Label != testSnapshotLabel {
		t.Errorf("Expected snapshot did not match current snapshot: %v", backups.Snapshot.Current)
	}

	backup, err = client.WaitForSnapshotStatus(context.Background(), instance.ID, backup.ID, linodego.SnapshotSuccessful, 360)
	if err != nil {
		t.Errorf("Error waiting for snapshot: %v", err)
	}

	if !backup.Available {
		t.Fatal("expected backup to be available")
	}

	restoreOpts := linodego.RestoreInstanceOptions{
		LinodeID:  instance.ID,
		Overwrite: true,
	}

	now := time.Now()

	err = client.RestoreInstanceBackup(context.Background(), instance.ID, backup.ID, restoreOpts)
	if err != nil {
		t.Errorf("Error restoring backup: %v", err)
	}

	err = client.CancelInstanceBackups(context.Background(), instance.ID)
	if err != nil {
		t.Errorf("Error cancelling backups: %v", err)
	}

	// wait for instnace to restore
	_, err = client.WaitForEventFinished(context.Background(), instance.ID, linodego.EntityLinode, linodego.ActionBackupsRestore, now, 360)
	if err != nil {
		t.Errorf("Error waiting for snapshot to complete: %v", err)
	}
}

func setupInstanceBackup(t *testing.T, fixturesYaml string) (*linodego.Client, *linodego.Instance, *linodego.InstanceSnapshot, func(), error) {
	t.Helper()
	client, instance, _, fixtureTeardown, err := setupInstanceWithoutDisks(t, fixturesYaml, true)
	if err != nil {
		t.Errorf("Error creating instance, got error %v", err)
	}

	client.WaitForInstanceStatus(context.Background(), instance.ID, linodego.InstanceOffline, 180)
	createOpts := linodego.InstanceDiskCreateOptions{
		Size:       10,
		Label:      "linodego-disk-test",
		Filesystem: "ext4",
	}
	disk, err := client.CreateInstanceDisk(context.Background(), instance.ID, createOpts)
	if err != nil {
		t.Errorf("Error creating Instance Disk: %v", err)
	}

	// wait for disk to finish provisioning
	event, err := client.WaitForEventFinished(context.Background(), instance.ID, linodego.EntityLinode, linodego.ActionDiskCreate, *disk.Created, 240)
	if err != nil {
		t.Errorf("Error waiting for instance snapshot: %v", err)
	}

	if event.Status == linodego.EventFailed {
		t.Errorf("Error creating instance disk: Disk Create Failed")
	}

	err = client.EnableInstanceBackups(context.Background(), instance.ID)
	if err != nil {
		t.Errorf("Error enabling Instance Backups: %v", err)
	}

	snapshot, err := client.CreateInstanceSnapshot(context.Background(), instance.ID, testSnapshotLabel)
	if err != nil {
		t.Errorf("Error creating instance snapshot: %v", err)
	}

	event, err = client.WaitForEventFinished(context.Background(), instance.ID, linodego.EntityLinode, linodego.ActionLinodeSnapshot, *instance.Created, 360)
	if err != nil {
		t.Errorf("Error waiting for instance snapshot: %v", err)
	}

	if event != nil && event.Status == linodego.EventFailed {
		t.Errorf("Error taking instance snapshot: Snapshot Failed")
	}

	return client, instance, snapshot, fixtureTeardown, err
}