| 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 TestLeaseLifecycle(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 := "ubuntu.iso" |
| 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 |
containersClient := containers.NewWithEnvironment(client.Environment) |
| 34 |
containersClient.Client = client.PrepareWithStorageResourceManagerAuth(containersClient.Client) |
| 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 |
storageAuth := auth.NewSharedKeyLiteAuthorizer(accountName, testData.StorageAccountKey) |
| 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 |
defer blobClient.Delete(ctx, accountName, containerName, fileName, DeleteInput{}) |
| 56 |
|
| 57 |
// Test begins here |
| 58 |
t.Logf("[DEBUG] Acquiring Lease..") |
| 59 |
leaseInput := AcquireLeaseInput{ |
| 60 |
LeaseDuration: -1, |
| 61 |
} |
| 62 |
leaseInfo, err := blobClient.AcquireLease(ctx, accountName, containerName, fileName, leaseInput) |
| 63 |
if err != nil { |
| 64 |
t.Fatalf("Error acquiring lease: %s", err) |
| 65 |
} |
| 66 |
t.Logf("[DEBUG] Lease ID: %q", leaseInfo.LeaseID) |
| 67 |
|
| 68 |
t.Logf("[DEBUG] Changing Lease..") |
| 69 |
changeLeaseInput := ChangeLeaseInput{ |
| 70 |
ExistingLeaseID: leaseInfo.LeaseID, |
| 71 |
ProposedLeaseID: "31f5bb01-cdd9-4166-bcdc-95186076bde0", |
| 72 |
} |
| 73 |
changeLeaseResult, err := blobClient.ChangeLease(ctx, accountName, containerName, fileName, changeLeaseInput) |
| 74 |
if err != nil { |
| 75 |
t.Fatalf("Error changing lease: %s", err) |
| 76 |
} |
| 77 |
t.Logf("[DEBUG] New Lease ID: %q", changeLeaseResult.LeaseID) |
| 78 |
|
| 79 |
t.Logf("[DEBUG] Releasing Lease..") |
| 80 |
if _, err := blobClient.ReleaseLease(ctx, accountName, containerName, fileName, changeLeaseResult.LeaseID); err != nil { |
| 81 |
t.Fatalf("Error releasing lease: %s", err) |
| 82 |
} |
| 83 |
|
| 84 |
t.Logf("[DEBUG] Acquiring a new lease..") |
| 85 |
leaseInput = AcquireLeaseInput{ |
| 86 |
LeaseDuration: 30, |
| 87 |
} |
| 88 |
leaseInfo, err = blobClient.AcquireLease(ctx, accountName, containerName, fileName, leaseInput) |
| 89 |
if err != nil { |
| 90 |
t.Fatalf("Error acquiring lease: %s", err) |
| 91 |
} |
| 92 |
t.Logf("[DEBUG] Lease ID: %q", leaseInfo.LeaseID) |
| 93 |
|
| 94 |
t.Logf("[DEBUG] Renewing lease..") |
| 95 |
if _, err := blobClient.RenewLease(ctx, accountName, containerName, fileName, leaseInfo.LeaseID); err != nil { |
| 96 |
t.Fatalf("Error renewing lease: %s", err) |
| 97 |
} |
| 98 |
|
| 99 |
t.Logf("[DEBUG] Breaking lease..") |
| 100 |
breakLeaseInput := BreakLeaseInput{ |
| 101 |
LeaseID: leaseInfo.LeaseID, |
| 102 |
} |
| 103 |
if _, err := blobClient.BreakLease(ctx, accountName, containerName, fileName, breakLeaseInput); err != nil { |
| 104 |
t.Fatalf("Error breaking lease: %s", err) |
| 105 |
} |
| 106 |
} |