Debian Codegraph @

storage/2018-03-28/blob/blobs/lifecycle_test.go

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