| 1 |
package blobs |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/Azure/azure-sdk-for-go/profiles/latest/storage/mgmt/storage" |
| 9 |
"github.com/tombuildsstuff/giovanni/storage/2017-07-29/blob/containers" |
| 10 |
"github.com/tombuildsstuff/giovanni/storage/internal/auth" |
| 11 |
"github.com/tombuildsstuff/giovanni/testhelpers" |
| 12 |
) |
| 13 |
|
| 14 |
func TestPageBlobLifecycle(t *testing.T) { |
| 15 |
client, err := testhelpers.Build(t) |
| 16 |
if err != nil { |
| 17 |
t.Fatal(err) |
| 18 |
} |
| 19 |
ctx := context.TODO() |
| 20 |
|
| 21 |
resourceGroup := fmt.Sprintf("acctestrg-%d", testhelpers.RandomInt()) |
| 22 |
accountName := fmt.Sprintf("acctestsa%s", testhelpers.RandomString()) |
| 23 |
containerName := fmt.Sprintf("cont-%d", testhelpers.RandomInt()) |
| 24 |
fileName := "append-blob.txt" |
| 25 |
|
| •26 |
testData, err := client.BuildTestResources(ctx, resourceGroup, accountName, storage.StorageV2) |
| 27 |
if err != nil { |
| 28 |
t.Fatal(err) |
| 29 |
} |
| 30 |
defer client.DestroyTestResources(ctx, resourceGroup, accountName) |
| 31 |
|
| 32 |
storageAuth := auth.NewSharedKeyLiteAuthorizer(accountName, testData.StorageAccountKey) |
| 33 |
containersClient := containers.NewWithEnvironment(client.Environment) |
| 34 |
containersClient.Client = client.PrepareWithAuthorizer(containersClient.Client, storageAuth) |
| 35 |
|
| 36 |
_, err = containersClient.Create(ctx, accountName, containerName, containers.CreateInput{}) |
| 37 |
if err != nil { |
| 38 |
t.Fatal(fmt.Errorf("Error creating: %s", err)) |
| 39 |
} |
| 40 |
defer containersClient.Delete(ctx, accountName, containerName) |
| 41 |
|
| 42 |
blobClient := NewWithEnvironment(client.Environment) |
| 43 |
blobClient.Client = client.PrepareWithAuthorizer(blobClient.Client, storageAuth) |
| 44 |
|
| 45 |
t.Logf("[DEBUG] Putting Page Blob..") |
| 46 |
fileSize := int64(10240000) |
| 47 |
if _, err := blobClient.PutPageBlob(ctx, accountName, containerName, fileName, PutPageBlobInput{ |
| 48 |
BlobContentLengthBytes: fileSize, |
| 49 |
}); err != nil { |
| 50 |
t.Fatalf("Error putting page blob: %s", err) |
| 51 |
} |
| 52 |
|
| 53 |
t.Logf("[DEBUG] Retrieving Properties..") |
| 54 |
props, err := blobClient.GetProperties(ctx, accountName, containerName, fileName, GetPropertiesInput{}) |
| 55 |
if err != nil { |
| 56 |
t.Fatalf("Error retrieving properties: %s", err) |
| 57 |
} |
| 58 |
if props.ContentLength != fileSize { |
| 59 |
t.Fatalf("Expected Content-Length to be %d but it was %d", fileSize, props.ContentLength) |
| 60 |
} |
| 61 |
|
| 62 |
for iteration := 1; iteration <= 3; iteration++ { |
| 63 |
t.Logf("[DEBUG] Putting Page %d of 3..", iteration) |
| 64 |
byteArray := func() []byte { |
| 65 |
o := make([]byte, 0) |
| 66 |
|
| 67 |
for i := 0; i < 512; i++ { |
| 68 |
o = append(o, byte(i)) |
| 69 |
} |
| 70 |
|
| 71 |
return o |
| 72 |
}() |
| 73 |
startByte := int64(512 * iteration) |
| 74 |
endByte := int64(startByte + 511) |
| 75 |
putPageInput := PutPageUpdateInput{ |
| 76 |
StartByte: startByte, |
| 77 |
EndByte: endByte, |
| 78 |
Content: byteArray, |
| 79 |
} |
| 80 |
if _, err := blobClient.PutPageUpdate(ctx, accountName, containerName, fileName, putPageInput); err != nil { |
| 81 |
t.Fatalf("Error putting page: %s", err) |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
t.Logf("[DEBUG] Deleting..") |
| 86 |
if _, err := blobClient.Delete(ctx, accountName, containerName, fileName, DeleteInput{}); err != nil { |
| 87 |
t.Fatalf("Error deleting: %s", err) |
| 88 |
} |
| 89 |
} |