1package integration 2 3import ( 4 "context" 5 "testing" 6 7 "github.com/linode/linodego" 8) 9 10var testSnapshotLabel = "snapshot-linodego-testing" 11 12func TestListInstanceBackups(t *testing.T) { 13 client, instance, backup, teardown, err := setupInstanceBackup(t, "fixtures/TestListInstanceBackups") 14 defer teardown() 15 if err != nil { 16 t.Error(err) 17 } 18 19 backupGotten, err := client.GetInstanceSnapshot(context.Background(), instance.ID, backup.ID) 20 if err != nil { 21 t.Errorf("Error getting backup: %v", err) 22 } else if backupGotten.Label != backup.Label { 23 t.Errorf("Error getting backup, Labels dont match") 24 } 25 26 assertDateSet(t, backupGotten.Created) 27 assertDateSet(t, backupGotten.Updated) 28 29 backups, err := client.GetInstanceBackups(context.Background(), instance.ID) 30 if err != nil { 31 t.Errorf("Error getting backups: %v", err) 32 } 33 34 if backups.Snapshot.InProgress == nil && backups.Snapshot.Current == nil { 35 t.Errorf("Error getting snapshot: No Current or InProgress Snapshot") 36 } 37 38 if backups.Snapshot.InProgress != nil && backups.Snapshot.InProgress.Label != testSnapshotLabel { 39 t.Errorf("Expected snapshot did not match inprogress snapshot: %v", backups.Snapshot.InProgress) 40 } else if backups.Snapshot.Current != nil && backups.Snapshot.Current.Label != testSnapshotLabel { 41 t.Errorf("Expected snapshot did not match current snapshot: %v", backups.Snapshot.Current) 42 } 43 44 _, err = client.WaitForSnapshotStatus(context.Background(), instance.ID, backup.ID, linodego.SnapshotSuccessful, 180) 45 if err != nil { 46 t.Errorf("Error waiting for snapshot: %v", err) 47 } 48 49 restoreOpts := linodego.RestoreInstanceOptions{ 50 LinodeID: instance.ID, 51 Overwrite: true, 52 } 53 54 err = client.RestoreInstanceBackup(context.Background(), instance.ID, backup.ID, restoreOpts) 55 if err != nil { 56 t.Errorf("Error restoring backup: %v", err) 57 } 58 59 err = client.CancelInstanceBackups(context.Background(), instance.ID) 60 if err != nil { 61 t.Errorf("Error cancelling backups: %v", err) 62 } 63} 64 65func setupInstanceBackup(t *testing.T, fixturesYaml string) (*linodego.Client, *linodego.Instance, *linodego.InstanceSnapshot, func(), error) { 66 t.Helper() 67 client, instance, _, fixtureTeardown, err := setupInstanceWithoutDisks(t, fixturesYaml) 68 if err != nil { 69 t.Errorf("Error creating instance, got error %v", err) 70 } 71 72 client.WaitForInstanceStatus(context.Background(), instance.ID, linodego.InstanceOffline, 180) 73 createOpts := linodego.InstanceDiskCreateOptions{ 74 Size: 10, 75 Label: "snapshot-linodego-testing", 76 Filesystem: "ext4", 77 } 78 disk, err := client.CreateInstanceDisk(context.Background(), instance.ID, createOpts) 79 if err != nil { 80 t.Errorf("Error creating Instance Disk: %v", err) 81 } 82 83 // wait for disk to finish provisioning 84 event, err := client.WaitForEventFinished(context.Background(), instance.ID, linodego.EntityLinode, linodego.ActionDiskCreate, *disk.Created, 240) 85 if err != nil { 86 t.Errorf("Error waiting for instance snapshot: %v", err) 87 } 88 89 if event.Status == linodego.EventFailed { 90 t.Errorf("Error creating instance disk: Disk Create Failed") 91 } 92 93 err = client.EnableInstanceBackups(context.Background(), instance.ID) 94 if err != nil { 95 t.Errorf("Error enabling Instance Backups: %v", err) 96 } 97 98 snapshot, err := client.CreateInstanceSnapshot(context.Background(), instance.ID, testSnapshotLabel) 99 if err != nil { 100 t.Errorf("Error creating instance snapshot: %v", err) 101 } 102 103 event, err = client.WaitForEventFinished(context.Background(), instance.ID, linodego.EntityLinode, linodego.ActionLinodeSnapshot, *instance.Created, 240) 104 if err != nil { 105 t.Errorf("Error waiting for instance snapshot: %v", err) 106 } 107 if event.Status == linodego.EventFailed { 108 t.Errorf("Error taking instance snapshot: Snapshot Failed") 109 } 110 111 return client, instance, snapshot, fixtureTeardown, err 112} 113