| 1 |
package containers |
| 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/internal/auth" |
| 11 |
"github.com/tombuildsstuff/giovanni/testhelpers" |
| 12 |
) |
| 13 |
|
| 14 |
var _ StorageContainer = Client{} |
| 15 |
|
| 16 |
func TestContainerLifecycle(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 |
|
| •27 |
testData, err := client.BuildTestResources(ctx, resourceGroup, accountName, storage.BlobStorage) |
| 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 := NewWithEnvironment(client.Environment) |
| 35 |
containersClient.Client = client.PrepareWithAuthorizer(containersClient.Client, storageAuth) |
| 36 |
|
| 37 |
// first let's test an empty container |
| 38 |
input := CreateInput{} |
| 39 |
_, err = containersClient.Create(ctx, accountName, containerName, input) |
| 40 |
if err != nil { |
| 41 |
t.Fatal(fmt.Errorf("Error creating: %s", err)) |
| 42 |
} |
| 43 |
|
| 44 |
container, err := containersClient.GetProperties(ctx, accountName, containerName) |
| 45 |
if err != nil { |
| 46 |
t.Fatal(fmt.Errorf("Error retrieving: %s", err)) |
| 47 |
} |
| 48 |
|
| 49 |
if container.AccessLevel != Private { |
| 50 |
t.Fatalf("Expected Access Level to be Private but got %q", container.AccessLevel) |
| 51 |
} |
| 52 |
if len(container.MetaData) != 0 { |
| 53 |
t.Fatalf("Expected MetaData to be empty but got: %s", container.MetaData) |
| 54 |
} |
| 55 |
if container.LeaseStatus != Unlocked { |
| 56 |
t.Fatalf("Expected Container Lease to be Unlocked but was: %s", container.LeaseStatus) |
| 57 |
} |
| 58 |
|
| 59 |
// then update the metadata |
| 60 |
metaData := map[string]string{ |
| 61 |
"dont": "kill-my-vibe", |
| 62 |
} |
| 63 |
_, err = containersClient.SetMetaData(ctx, accountName, containerName, metaData) |
| 64 |
if err != nil { |
| 65 |
t.Fatal(fmt.Errorf("Error updating metadata: %s", err)) |
| 66 |
} |
| 67 |
|
| 68 |
// give azure time to replicate |
| 69 |
time.Sleep(2 * time.Second) |
| 70 |
|
| 71 |
// then assert that |
| 72 |
container, err = containersClient.GetProperties(ctx, accountName, containerName) |
| 73 |
if err != nil { |
| 74 |
t.Fatal(fmt.Errorf("Error re-retrieving: %s", err)) |
| 75 |
} |
| 76 |
if len(container.MetaData) != 1 { |
| 77 |
t.Fatalf("Expected 1 item in the metadata but got: %s", container.MetaData) |
| 78 |
} |
| 79 |
if container.MetaData["dont"] != "kill-my-vibe" { |
| 80 |
t.Fatalf("Expected `kill-my-vibe` but got %q", container.MetaData["dont"]) |
| 81 |
} |
| 82 |
if container.AccessLevel != Private { |
| 83 |
t.Fatalf("Expected Access Level to be Private but got %q", container.AccessLevel) |
| 84 |
} |
| 85 |
if container.LeaseStatus != Unlocked { |
| 86 |
t.Fatalf("Expected Container Lease to be Unlocked but was: %s", container.LeaseStatus) |
| 87 |
} |
| 88 |
|
| 89 |
// then update the ACL |
| 90 |
_, err = containersClient.SetAccessControl(ctx, accountName, containerName, Blob) |
| 91 |
if err != nil { |
| 92 |
t.Fatal(fmt.Errorf("Error updating ACL's: %s", err)) |
| 93 |
} |
| 94 |
|
| 95 |
// give azure some time to replicate |
| 96 |
time.Sleep(2 * time.Second) |
| 97 |
|
| 98 |
// then assert that |
| 99 |
container, err = containersClient.GetProperties(ctx, accountName, containerName) |
| 100 |
if err != nil { |
| 101 |
t.Fatal(fmt.Errorf("Error re-retrieving: %s", err)) |
| 102 |
} |
| 103 |
if container.AccessLevel != Blob { |
| 104 |
t.Fatalf("Expected Access Level to be Blob but got %q", container.AccessLevel) |
| 105 |
} |
| 106 |
if len(container.MetaData) != 1 { |
| 107 |
t.Fatalf("Expected 1 item in the metadata but got: %s", container.MetaData) |
| 108 |
} |
| 109 |
if container.LeaseStatus != Unlocked { |
| 110 |
t.Fatalf("Expected Container Lease to be Unlocked but was: %s", container.LeaseStatus) |
| 111 |
} |
| 112 |
|
| 113 |
// acquire a lease for 30s |
| 114 |
acquireLeaseInput := AcquireLeaseInput{ |
| 115 |
LeaseDuration: 30, |
| 116 |
} |
| 117 |
acquireLeaseResp, err := containersClient.AcquireLease(ctx, accountName, containerName, acquireLeaseInput) |
| 118 |
if err != nil { |
| 119 |
t.Fatalf("Error acquiring lease: %s", err) |
| 120 |
} |
| 121 |
t.Logf("[DEBUG] Lease ID: %s", acquireLeaseResp.LeaseID) |
| 122 |
|
| 123 |
// we should then be able to update the ID |
| 124 |
t.Logf("[DEBUG] Changing lease..") |
| 125 |
updateLeaseInput := ChangeLeaseInput{ |
| 126 |
ExistingLeaseID: acquireLeaseResp.LeaseID, |
| 127 |
ProposedLeaseID: "aaaabbbb-aaaa-bbbb-cccc-aaaabbbbcccc", |
| 128 |
} |
| 129 |
updateLeaseResp, err := containersClient.ChangeLease(ctx, accountName, containerName, updateLeaseInput) |
| 130 |
if err != nil { |
| 131 |
t.Fatalf("Error changing lease: %s", err) |
| 132 |
} |
| 133 |
|
| 134 |
// then renew it |
| 135 |
_, err = containersClient.RenewLease(ctx, accountName, containerName, updateLeaseResp.LeaseID) |
| 136 |
if err != nil { |
| 137 |
t.Fatalf("Error renewing lease: %s", err) |
| 138 |
} |
| 139 |
|
| 140 |
// and then give it a timeout |
| 141 |
breakPeriod := 20 |
| 142 |
breakLeaseInput := BreakLeaseInput{ |
| 143 |
LeaseID: updateLeaseResp.LeaseID, |
| 144 |
BreakPeriod: &breakPeriod, |
| 145 |
} |
| 146 |
breakLeaseResp, err := containersClient.BreakLease(ctx, accountName, containerName, breakLeaseInput) |
| 147 |
if err != nil { |
| 148 |
t.Fatalf("Error breaking lease: %s", err) |
| 149 |
} |
| 150 |
if breakLeaseResp.LeaseTime == 0 { |
| 151 |
t.Fatalf("Lease broke immediately when should have waited: %d", breakLeaseResp.LeaseTime) |
| 152 |
} |
| 153 |
|
| 154 |
// and finally ditch it |
| 155 |
_, err = containersClient.ReleaseLease(ctx, accountName, containerName, updateLeaseResp.LeaseID) |
| 156 |
if err != nil { |
| 157 |
t.Fatalf("Error releasing lease: %s", err) |
| 158 |
} |
| 159 |
|
| 160 |
t.Logf("[DEBUG] Listing blobs in the container..") |
| 161 |
listInput := ListBlobsInput{} |
| 162 |
listResult, err := containersClient.ListBlobs(ctx, accountName, containerName, listInput) |
| 163 |
if err != nil { |
| 164 |
t.Fatalf("Error listing blobs: %s", err) |
| 165 |
} |
| 166 |
|
| 167 |
if len(listResult.Blobs.Blobs) != 0 { |
| 168 |
t.Fatalf("Expected there to be no blobs in the container but got %d", len(listResult.Blobs.Blobs)) |
| 169 |
} |
| 170 |
|
| 171 |
t.Logf("[DEBUG] Deleting..") |
| 172 |
_, err = containersClient.Delete(ctx, accountName, containerName) |
| 173 |
if err != nil { |
| 174 |
t.Fatal(fmt.Errorf("Error deleting: %s", err)) |
| 175 |
} |
| 176 |
} |