| 1 |
package blobs |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"net/http" |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/Azure/azure-sdk-for-go/profiles/latest/storage/mgmt/storage" |
| 11 |
"github.com/tombuildsstuff/giovanni/storage/2019-12-12/blob/containers" |
| 12 |
"github.com/tombuildsstuff/giovanni/storage/internal/auth" |
| 13 |
"github.com/tombuildsstuff/giovanni/testhelpers" |
| 14 |
) |
| 15 |
|
| 16 |
func TestSnapshotLifecycle(t *testing.T) { |
| 17 |
client, err := testhelpers.Build(t) |
| 18 |
if err != nil { |
| 19 |
t.Fatal(err) |
| 20 |
} |
| 21 |
ctx := context.TODO() |
| 22 |
|
| 23 |
resourceGroup := fmt.Sprintf("acctestrg-%d", testhelpers.RandomInt()) |
| 24 |
accountName := fmt.Sprintf("acctestsa%s", testhelpers.RandomString()) |
| 25 |
containerName := fmt.Sprintf("cont-%d", testhelpers.RandomInt()) |
| 26 |
fileName := "example.txt" |
| 27 |
|
| •28 |
testData, err := client.BuildTestResources(ctx, resourceGroup, accountName, storage.BlobStorage) |
| 29 |
if err != nil { |
| 30 |
t.Fatal(err) |
| 31 |
} |
| 32 |
defer client.DestroyTestResources(ctx, resourceGroup, accountName) |
| 33 |
|
| 34 |
containersClient := containers.NewWithEnvironment(client.Environment) |
| 35 |
containersClient.Client = client.PrepareWithStorageResourceManagerAuth(containersClient.Client) |
| 36 |
|
| 37 |
_, err = containersClient.Create(ctx, accountName, containerName, containers.CreateInput{}) |
| 38 |
if err != nil { |
| 39 |
t.Fatalf("Error creating: %s", err) |
| 40 |
} |
| 41 |
defer containersClient.Delete(ctx, accountName, containerName) |
| 42 |
|
| 43 |
storageAuth := auth.NewSharedKeyLiteAuthorizer(accountName, testData.StorageAccountKey) |
| 44 |
blobClient := NewWithEnvironment(client.Environment) |
| 45 |
blobClient.Client = client.PrepareWithAuthorizer(blobClient.Client, storageAuth) |
| 46 |
|
| 47 |
t.Logf("[DEBUG] Copying file to Blob Storage..") |
| 48 |
copyInput := CopyInput{ |
| 49 |
CopySource: "http://releases.ubuntu.com/14.04/ubuntu-14.04.6-desktop-amd64.iso", |
| 50 |
} |
| 51 |
|
| 52 |
refreshInterval := 5 * time.Second |
| 53 |
if err := blobClient.CopyAndWait(ctx, accountName, containerName, fileName, copyInput, refreshInterval); err != nil { |
| 54 |
t.Fatalf("Error copying: %s", err) |
| 55 |
} |
| 56 |
|
| 57 |
t.Logf("[DEBUG] First Snapshot..") |
| 58 |
firstSnapshot, err := blobClient.Snapshot(ctx, accountName, containerName, fileName, SnapshotInput{}) |
| 59 |
if err != nil { |
| 60 |
t.Fatalf("Error taking first snapshot: %s", err) |
| 61 |
} |
| 62 |
t.Logf("[DEBUG] First Snapshot ID: %q", firstSnapshot.SnapshotDateTime) |
| 63 |
|
| 64 |
t.Log("[DEBUG] Waiting 2 seconds..") |
| 65 |
time.Sleep(2 * time.Second) |
| 66 |
|
| 67 |
t.Logf("[DEBUG] Second Snapshot..") |
| 68 |
secondSnapshot, err := blobClient.Snapshot(ctx, accountName, containerName, fileName, SnapshotInput{ |
| 69 |
MetaData: map[string]string{ |
| 70 |
"hello": "world", |
| 71 |
}, |
| 72 |
}) |
| 73 |
if err != nil { |
| 74 |
t.Fatalf("Error taking Second snapshot: %s", err) |
| 75 |
} |
| 76 |
t.Logf("[DEBUG] Second Snapshot ID: %q", secondSnapshot.SnapshotDateTime) |
| 77 |
|
| 78 |
t.Logf("[DEBUG] Leasing the Blob..") |
| 79 |
leaseDetails, err := blobClient.AcquireLease(ctx, accountName, containerName, fileName, AcquireLeaseInput{ |
| 80 |
// infinite |
| 81 |
LeaseDuration: -1, |
| 82 |
}) |
| 83 |
if err != nil { |
| 84 |
t.Fatalf("Error leasing Blob: %s", err) |
| 85 |
} |
| 86 |
t.Logf("[DEBUG] Lease ID: %q", leaseDetails.LeaseID) |
| 87 |
|
| 88 |
t.Logf("[DEBUG] Third Snapshot..") |
| 89 |
thirdSnapshot, err := blobClient.Snapshot(ctx, accountName, containerName, fileName, SnapshotInput{ |
| 90 |
LeaseID: &leaseDetails.LeaseID, |
| 91 |
}) |
| 92 |
if err != nil { |
| 93 |
t.Fatalf("Error taking Third snapshot: %s", err) |
| 94 |
} |
| 95 |
t.Logf("[DEBUG] Third Snapshot ID: %q", thirdSnapshot.SnapshotDateTime) |
| 96 |
|
| 97 |
t.Logf("[DEBUG] Releasing Lease..") |
| 98 |
if _, err := blobClient.ReleaseLease(ctx, accountName, containerName, fileName, leaseDetails.LeaseID); err != nil { |
| 99 |
t.Fatalf("Error releasing Lease: %s", err) |
| 100 |
} |
| 101 |
|
| 102 |
// get the properties from the blob, which should include the LastModifiedDate |
| 103 |
t.Logf("[DEBUG] Retrieving Properties for Blob") |
| 104 |
props, err := blobClient.GetProperties(ctx, accountName, containerName, fileName, GetPropertiesInput{}) |
| 105 |
if err != nil { |
| 106 |
t.Fatalf("Error getting properties: %s", err) |
| 107 |
} |
| 108 |
|
| 109 |
// confirm that the If-Modified-None returns an error |
| 110 |
t.Logf("[DEBUG] Third Snapshot..") |
| 111 |
fourthSnapshot, err := blobClient.Snapshot(ctx, accountName, containerName, fileName, SnapshotInput{ |
| 112 |
LeaseID: &leaseDetails.LeaseID, |
| 113 |
IfModifiedSince: &props.LastModified, |
| 114 |
}) |
| 115 |
if err == nil { |
| 116 |
t.Fatalf("Expected an error but didn't get one") |
| 117 |
} |
| 118 |
if fourthSnapshot.Response.StatusCode != http.StatusPreconditionFailed { |
| 119 |
t.Fatalf("Expected the status code to be Precondition Failed but got: %d", fourthSnapshot.Response.StatusCode) |
| 120 |
} |
| 121 |
|
| 122 |
t.Logf("[DEBUG] Retrieving the Second Snapshot Properties..") |
| 123 |
getSecondSnapshotInput := GetSnapshotPropertiesInput{ |
| 124 |
SnapshotID: secondSnapshot.SnapshotDateTime, |
| 125 |
} |
| 126 |
if _, err := blobClient.GetSnapshotProperties(ctx, accountName, containerName, fileName, getSecondSnapshotInput); err != nil { |
| 127 |
t.Fatalf("Error retrieving properties for the second snapshot: %s", err) |
| 128 |
} |
| 129 |
|
| 130 |
t.Logf("[DEBUG] Deleting the Second Snapshot..") |
| 131 |
deleteSnapshotInput := DeleteSnapshotInput{ |
| 132 |
SnapshotDateTime: secondSnapshot.SnapshotDateTime, |
| 133 |
} |
| 134 |
if _, err := blobClient.DeleteSnapshot(ctx, accountName, containerName, fileName, deleteSnapshotInput); err != nil { |
| 135 |
t.Fatalf("Error deleting snapshot: %s", err) |
| 136 |
} |
| 137 |
|
| 138 |
t.Logf("[DEBUG] Re-Retrieving the Second Snapshot Properties..") |
| 139 |
secondSnapshotProps, err := blobClient.GetSnapshotProperties(ctx, accountName, containerName, fileName, getSecondSnapshotInput) |
| 140 |
if err == nil { |
| 141 |
t.Fatalf("Expected an error retrieving the snapshot but got none") |
| 142 |
} |
| 143 |
if secondSnapshotProps.Response.StatusCode != http.StatusNotFound { |
| 144 |
t.Fatalf("Expected the status code to be %d but got %q", http.StatusNoContent, secondSnapshotProps.Response.StatusCode) |
| 145 |
} |
| 146 |
|
| 147 |
t.Logf("[DEBUG] Deleting all the snapshots..") |
| 148 |
if _, err := blobClient.DeleteSnapshots(ctx, accountName, containerName, fileName, DeleteSnapshotsInput{}); err != nil { |
| 149 |
t.Fatalf("Error deleting snapshots: %s", err) |
| 150 |
} |
| 151 |
|
| 152 |
t.Logf("[DEBUG] Deleting the Blob..") |
| 153 |
deleteInput := DeleteInput{ |
| 154 |
DeleteSnapshots: false, |
| 155 |
} |
| 156 |
if _, err := blobClient.Delete(ctx, accountName, containerName, fileName, deleteInput); err != nil { |
| 157 |
t.Fatalf("Error deleting Blob: %s", err) |
| 158 |
} |
| 159 |
} |