| 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 TestAppendBlobLifecycle(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.KindBlobStorage) |
| 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 Append Blob..") |
| 46 |
if _, err := blobClient.PutAppendBlob(ctx, accountName, containerName, fileName, PutAppendBlobInput{}); err != nil { |
| 47 |
t.Fatalf("Error putting append blob: %s", err) |
| 48 |
} |
| 49 |
|
| 50 |
t.Logf("[DEBUG] Retrieving Properties..") |
| 51 |
props, err := blobClient.GetProperties(ctx, accountName, containerName, fileName, GetPropertiesInput{}) |
| 52 |
if err != nil { |
| 53 |
t.Fatalf("Error retrieving properties: %s", err) |
| 54 |
} |
| 55 |
if props.ContentLength != 0 { |
| 56 |
t.Fatalf("Expected Content-Length to be 0 but it was %d", props.ContentLength) |
| 57 |
} |
| 58 |
|
| 59 |
t.Logf("[DEBUG] Appending First Block..") |
| 60 |
appendInput := AppendBlockInput{ |
| 61 |
Content: &[]byte{ |
| 62 |
12, |
| 63 |
48, |
| 64 |
93, |
| 65 |
76, |
| 66 |
29, |
| 67 |
10, |
| 68 |
}, |
| 69 |
} |
| 70 |
if _, err := blobClient.AppendBlock(ctx, accountName, containerName, fileName, appendInput); err != nil { |
| 71 |
t.Fatalf("Error appending first block: %s", err) |
| 72 |
} |
| 73 |
|
| 74 |
t.Logf("[DEBUG] Re-Retrieving Properties..") |
| 75 |
props, err = blobClient.GetProperties(ctx, accountName, containerName, fileName, GetPropertiesInput{}) |
| 76 |
if err != nil { |
| 77 |
t.Fatalf("Error retrieving properties: %s", err) |
| 78 |
} |
| 79 |
if props.ContentLength != 6 { |
| 80 |
t.Fatalf("Expected Content-Length to be 6 but it was %d", props.ContentLength) |
| 81 |
} |
| 82 |
|
| 83 |
t.Logf("[DEBUG] Appending Second Block..") |
| 84 |
appendInput = AppendBlockInput{ |
| 85 |
Content: &[]byte{ |
| 86 |
92, |
| 87 |
62, |
| 88 |
64, |
| 89 |
47, |
| 90 |
83, |
| 91 |
77, |
| 92 |
}, |
| 93 |
} |
| 94 |
if _, err := blobClient.AppendBlock(ctx, accountName, containerName, fileName, appendInput); err != nil { |
| 95 |
t.Fatalf("Error appending Second block: %s", err) |
| 96 |
} |
| 97 |
|
| 98 |
t.Logf("[DEBUG] Re-Retrieving Properties..") |
| 99 |
props, err = blobClient.GetProperties(ctx, accountName, containerName, fileName, GetPropertiesInput{}) |
| 100 |
if err != nil { |
| 101 |
t.Fatalf("Error retrieving properties: %s", err) |
| 102 |
} |
| 103 |
if props.ContentLength != 12 { |
| 104 |
t.Fatalf("Expected Content-Length to be 12 but it was %d", props.ContentLength) |
| 105 |
} |
| 106 |
|
| 107 |
t.Logf("[DEBUG] Acquiring Lease..") |
| 108 |
leaseDetails, err := blobClient.AcquireLease(ctx, accountName, containerName, fileName, AcquireLeaseInput{ |
| 109 |
LeaseDuration: -1, |
| 110 |
}) |
| 111 |
if err != nil { |
| 112 |
t.Fatalf("Error acquiring Lease: %s", err) |
| 113 |
} |
| 114 |
t.Logf("[DEBUG] Lease ID is %q", leaseDetails.LeaseID) |
| 115 |
|
| 116 |
t.Logf("[DEBUG] Appending Third Block..") |
| 117 |
appendInput = AppendBlockInput{ |
| 118 |
Content: &[]byte{ |
| 119 |
64, |
| 120 |
35, |
| 121 |
28, |
| 122 |
93, |
| 123 |
11, |
| 124 |
23, |
| 125 |
}, |
| 126 |
LeaseID: &leaseDetails.LeaseID, |
| 127 |
} |
| 128 |
if _, err := blobClient.AppendBlock(ctx, accountName, containerName, fileName, appendInput); err != nil { |
| 129 |
t.Fatalf("Error appending Third block: %s", err) |
| 130 |
} |
| 131 |
|
| 132 |
t.Logf("[DEBUG] Re-Retrieving Properties..") |
| 133 |
props, err = blobClient.GetProperties(ctx, accountName, containerName, fileName, GetPropertiesInput{ |
| 134 |
LeaseID: &leaseDetails.LeaseID, |
| 135 |
}) |
| 136 |
if err != nil { |
| 137 |
t.Fatalf("Error retrieving properties: %s", err) |
| 138 |
} |
| 139 |
if props.ContentLength != 18 { |
| 140 |
t.Fatalf("Expected Content-Length to be 18 but it was %d", props.ContentLength) |
| 141 |
} |
| 142 |
|
| 143 |
t.Logf("[DEBUG] Breaking Lease..") |
| 144 |
breakLeaseInput := BreakLeaseInput{ |
| 145 |
LeaseID: leaseDetails.LeaseID, |
| 146 |
} |
| 147 |
if _, err := blobClient.BreakLease(ctx, accountName, containerName, fileName, breakLeaseInput); err != nil { |
| 148 |
t.Fatalf("Error breaking lease: %s", err) |
| 149 |
} |
| 150 |
|
| 151 |
t.Logf("[DEBUG] Deleting Lease..") |
| 152 |
if _, err := blobClient.Delete(ctx, accountName, containerName, fileName, DeleteInput{}); err != nil { |
| 153 |
t.Fatalf("Error deleting: %s", err) |
| 154 |
} |
| 155 |
} |