| 1 |
package blobs |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"testing" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/Azure/azure-sdk-for-go/profiles/latest/storage/mgmt/storage" |
| 10 |
"github.com/tombuildsstuff/giovanni/storage/2018-11-09/blob/containers" |
| 11 |
"github.com/tombuildsstuff/giovanni/storage/internal/auth" |
| 12 |
"github.com/tombuildsstuff/giovanni/testhelpers" |
| 13 |
) |
| 14 |
|
| 15 |
var _ StorageBlob = Client{} |
| 16 |
|
| 17 |
func TestLifecycle(t *testing.T) { |
| 18 |
client, err := testhelpers.Build(t) |
| 19 |
if err != nil { |
| 20 |
t.Fatal(err) |
| 21 |
} |
| 22 |
ctx := context.TODO() |
| 23 |
|
| 24 |
resourceGroup := fmt.Sprintf("acctestrg-%d", testhelpers.RandomInt()) |
| 25 |
accountName := fmt.Sprintf("acctestsa%s", testhelpers.RandomString()) |
| 26 |
containerName := fmt.Sprintf("cont-%d", testhelpers.RandomInt()) |
| 27 |
fileName := "example.txt" |
| 28 |
|
| •29 |
testData, err := client.BuildTestResources(ctx, resourceGroup, accountName, storage.BlobStorage) |
| 30 |
if err != nil { |
| 31 |
t.Fatal(err) |
| 32 |
} |
| 33 |
defer client.DestroyTestResources(ctx, resourceGroup, accountName) |
| 34 |
|
| 35 |
storageAuth := auth.NewSharedKeyLiteAuthorizer(accountName, testData.StorageAccountKey) |
| 36 |
containersClient := containers.NewWithEnvironment(client.Environment) |
| 37 |
containersClient.Client = client.PrepareWithAuthorizer(containersClient.Client, storageAuth) |
| 38 |
|
| 39 |
_, err = containersClient.Create(ctx, accountName, containerName, containers.CreateInput{}) |
| 40 |
if err != nil { |
| 41 |
t.Fatal(fmt.Errorf("Error creating: %s", err)) |
| 42 |
} |
| 43 |
defer containersClient.Delete(ctx, accountName, containerName) |
| 44 |
|
| 45 |
blobClient := NewWithEnvironment(client.Environment) |
| 46 |
blobClient.Client = client.PrepareWithAuthorizer(blobClient.Client, storageAuth) |
| 47 |
|
| 48 |
t.Logf("[DEBUG] Copying file to Blob Storage..") |
| 49 |
copyInput := CopyInput{ |
| 50 |
CopySource: "http://releases.ubuntu.com/14.04/ubuntu-14.04.6-desktop-amd64.iso", |
| 51 |
} |
| 52 |
|
| 53 |
refreshInterval := 5 * time.Second |
| 54 |
if err := blobClient.CopyAndWait(ctx, accountName, containerName, fileName, copyInput, refreshInterval); err != nil { |
| 55 |
t.Fatalf("Error copying: %s", err) |
| 56 |
} |
| 57 |
|
| 58 |
t.Logf("[DEBUG] Retrieving Blob Properties..") |
| 59 |
details, err := blobClient.GetProperties(ctx, accountName, containerName, fileName, GetPropertiesInput{}) |
| 60 |
if err != nil { |
| 61 |
t.Fatalf("Error retrieving properties: %s", err) |
| 62 |
} |
| 63 |
|
| 64 |
// default value |
| 65 |
if details.AccessTier != Hot { |
| 66 |
t.Fatalf("Expected the AccessTier to be %q but got %q", Hot, details.AccessTier) |
| 67 |
} |
| 68 |
if details.BlobType != BlockBlob { |
| 69 |
t.Fatalf("Expected BlobType to be %q but got %q", BlockBlob, details.BlobType) |
| 70 |
} |
| 71 |
if len(details.MetaData) != 0 { |
| 72 |
t.Fatalf("Expected there to be no items of metadata but got %d", len(details.MetaData)) |
| 73 |
} |
| 74 |
|
| 75 |
t.Logf("[DEBUG] Checking it's returned in the List API..") |
| 76 |
listInput := containers.ListBlobsInput{} |
| 77 |
listResult, err := containersClient.ListBlobs(ctx, accountName, containerName, listInput) |
| 78 |
if err != nil { |
| 79 |
t.Fatalf("Error listing blobs: %s", err) |
| 80 |
} |
| 81 |
|
| 82 |
if len(listResult.Blobs.Blobs) != 1 { |
| 83 |
t.Fatalf("Expected there to be 1 blob in the container but got %d", len(listResult.Blobs.Blobs)) |
| 84 |
} |
| 85 |
|
| 86 |
t.Logf("[DEBUG] Setting MetaData..") |
| 87 |
metaDataInput := SetMetaDataInput{ |
| 88 |
MetaData: map[string]string{ |
| 89 |
"hello": "there", |
| 90 |
}, |
| 91 |
} |
| 92 |
if _, err := blobClient.SetMetaData(ctx, accountName, containerName, fileName, metaDataInput); err != nil { |
| 93 |
t.Fatalf("Error setting MetaData: %s", err) |
| 94 |
} |
| 95 |
|
| 96 |
t.Logf("[DEBUG] Re-retrieving Blob Properties..") |
| 97 |
details, err = blobClient.GetProperties(ctx, accountName, containerName, fileName, GetPropertiesInput{}) |
| 98 |
if err != nil { |
| 99 |
t.Fatalf("Error re-retrieving properties: %s", err) |
| 100 |
} |
| 101 |
|
| 102 |
// default value |
| 103 |
if details.AccessTier != Hot { |
| 104 |
t.Fatalf("Expected the AccessTier to be %q but got %q", Hot, details.AccessTier) |
| 105 |
} |
| 106 |
if details.BlobType != BlockBlob { |
| 107 |
t.Fatalf("Expected BlobType to be %q but got %q", BlockBlob, details.BlobType) |
| 108 |
} |
| 109 |
if len(details.MetaData) != 1 { |
| 110 |
t.Fatalf("Expected there to be 1 item of metadata but got %d", len(details.MetaData)) |
| 111 |
} |
| 112 |
if details.MetaData["hello"] != "there" { |
| 113 |
t.Fatalf("Expected `hello` to be `there` but got %q", details.MetaData["there"]) |
| 114 |
} |
| 115 |
|
| 116 |
t.Logf("[DEBUG] Retrieving the Block List..") |
| 117 |
getBlockListInput := GetBlockListInput{ |
| 118 |
BlockListType: All, |
| 119 |
} |
| 120 |
blockList, err := blobClient.GetBlockList(ctx, accountName, containerName, fileName, getBlockListInput) |
| 121 |
if err != nil { |
| 122 |
t.Fatalf("Error retrieving Block List: %s", err) |
| 123 |
} |
| 124 |
|
| 125 |
// since this is a copy from an existing file, all blocks should be present |
| 126 |
if len(blockList.CommittedBlocks.Blocks) == 0 { |
| 127 |
t.Fatalf("Expected there to be committed blocks but there weren't!") |
| 128 |
} |
| 129 |
if len(blockList.UncommittedBlocks.Blocks) != 0 { |
| 130 |
t.Fatalf("Expected all blocks to be committed but got %d uncommitted blocks", len(blockList.UncommittedBlocks.Blocks)) |
| 131 |
} |
| 132 |
|
| 133 |
t.Logf("[DEBUG] Changing the Access Tiers..") |
| 134 |
tiers := []AccessTier{ |
| 135 |
Hot, |
| 136 |
Cool, |
| 137 |
Archive, |
| 138 |
} |
| 139 |
for _, tier := range tiers { |
| 140 |
t.Logf("[DEBUG] Updating the Access Tier to %q..", string(tier)) |
| 141 |
if _, err := blobClient.SetTier(ctx, accountName, containerName, fileName, tier); err != nil { |
| 142 |
t.Fatalf("Error setting the Access Tier: %s", err) |
| 143 |
} |
| 144 |
|
| 145 |
t.Logf("[DEBUG] Re-retrieving Blob Properties..") |
| 146 |
details, err = blobClient.GetProperties(ctx, accountName, containerName, fileName, GetPropertiesInput{}) |
| 147 |
if err != nil { |
| 148 |
t.Fatalf("Error re-retrieving properties: %s", err) |
| 149 |
} |
| 150 |
|
| 151 |
if details.AccessTier != tier { |
| 152 |
t.Fatalf("Expected the AccessTier to be %q but got %q", tier, details.AccessTier) |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
t.Logf("[DEBUG] Deleting Blob") |
| 157 |
if _, err := blobClient.Delete(ctx, accountName, containerName, fileName, DeleteInput{}); err != nil { |
| 158 |
t.Fatalf("Error deleting Blob: %s", err) |
| 159 |
} |
| 160 |
} |