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