| 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/storage/internal/endpoints" |
| 13 |
"github.com/tombuildsstuff/giovanni/testhelpers" |
| 14 |
) |
| 15 |
|
| 16 |
func TestCopyFromExistingFile(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 |
fileName := "ubuntu.iso" |
| 27 |
copiedFileName := "copied.iso" |
| 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 |
containersClient := containers.NewWithEnvironment(client.Environment) |
| 36 |
containersClient.Client = client.PrepareWithStorageResourceManagerAuth(containersClient.Client) |
| 37 |
|
| 38 |
_, err = containersClient.Create(ctx, accountName, containerName, containers.CreateInput{}) |
| 39 |
if err != nil { |
| 40 |
t.Fatal(fmt.Errorf("Error creating: %s", err)) |
| 41 |
} |
| 42 |
defer containersClient.Delete(ctx, accountName, containerName) |
| 43 |
|
| 44 |
storageAuth := auth.NewSharedKeyLiteAuthorizer(accountName, testData.StorageAccountKey) |
| 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] Duplicating that file..") |
| 59 |
copiedInput := CopyInput{ |
| 60 |
CopySource: fmt.Sprintf("%s/%s/%s", endpoints.GetBlobEndpoint(blobClient.BaseURI, accountName), containerName, fileName), |
| 61 |
} |
| 62 |
if err := blobClient.CopyAndWait(ctx, accountName, containerName, copiedFileName, copiedInput, refreshInterval); err != nil { |
| 63 |
t.Fatalf("Error duplicating file: %s", err) |
| 64 |
} |
| 65 |
|
| 66 |
t.Logf("[DEBUG] Retrieving Properties for the Original File..") |
| 67 |
props, err := blobClient.GetProperties(ctx, accountName, containerName, fileName, GetPropertiesInput{}) |
| 68 |
if err != nil { |
| 69 |
t.Fatalf("Error getting properties for the original file: %s", err) |
| 70 |
} |
| 71 |
|
| 72 |
t.Logf("[DEBUG] Retrieving Properties for the Copied File..") |
| 73 |
copiedProps, err := blobClient.GetProperties(ctx, accountName, containerName, copiedFileName, GetPropertiesInput{}) |
| 74 |
if err != nil { |
| 75 |
t.Fatalf("Error getting properties for the copied file: %s", err) |
| 76 |
} |
| 77 |
|
| 78 |
if props.ContentLength != copiedProps.ContentLength { |
| 79 |
t.Fatalf("Expected the content length to be %d but it was %d", props.ContentLength, copiedProps.ContentLength) |
| 80 |
} |
| 81 |
|
| 82 |
t.Logf("[DEBUG] Deleting copied file..") |
| 83 |
if _, err := blobClient.Delete(ctx, accountName, containerName, copiedFileName, DeleteInput{}); err != nil { |
| 84 |
t.Fatalf("Error deleting file: %s", err) |
| 85 |
} |
| 86 |
|
| 87 |
t.Logf("[DEBUG] Deleting original file..") |
| 88 |
if _, err := blobClient.Delete(ctx, accountName, containerName, fileName, DeleteInput{}); err != nil { |
| 89 |
t.Fatalf("Error deleting file: %s", err) |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
func TestCopyFromURL(t *testing.T) { |
| 94 |
client, err := testhelpers.Build(t) |
| 95 |
if err != nil { |
| 96 |
t.Fatal(err) |
| 97 |
} |
| 98 |
ctx := context.TODO() |
| 99 |
|
| 100 |
resourceGroup := fmt.Sprintf("acctestrg-%d", testhelpers.RandomInt()) |
| 101 |
accountName := fmt.Sprintf("acctestsa%s", testhelpers.RandomString()) |
| 102 |
containerName := fmt.Sprintf("cont-%d", testhelpers.RandomInt()) |
| 103 |
fileName := "ubuntu.iso" |
| 104 |
|
| •105 |
testData, err := client.BuildTestResources(ctx, resourceGroup, accountName, storage.BlobStorage) |
| 106 |
if err != nil { |
| 107 |
t.Fatal(err) |
| 108 |
} |
| 109 |
defer client.DestroyTestResources(ctx, resourceGroup, accountName) |
| 110 |
|
| 111 |
containersClient := containers.NewWithEnvironment(client.Environment) |
| 112 |
containersClient.Client = client.PrepareWithStorageResourceManagerAuth(containersClient.Client) |
| 113 |
|
| 114 |
_, err = containersClient.Create(ctx, accountName, containerName, containers.CreateInput{}) |
| 115 |
if err != nil { |
| 116 |
t.Fatal(fmt.Errorf("Error creating: %s", err)) |
| 117 |
} |
| 118 |
defer containersClient.Delete(ctx, accountName, containerName) |
| 119 |
|
| 120 |
storageAuth := auth.NewSharedKeyLiteAuthorizer(accountName, testData.StorageAccountKey) |
| 121 |
blobClient := NewWithEnvironment(client.Environment) |
| 122 |
blobClient.Client = client.PrepareWithAuthorizer(blobClient.Client, storageAuth) |
| 123 |
|
| 124 |
t.Logf("[DEBUG] Copying file to Blob Storage..") |
| 125 |
copyInput := CopyInput{ |
| 126 |
CopySource: "http://releases.ubuntu.com/14.04/ubuntu-14.04.6-desktop-amd64.iso", |
| 127 |
} |
| 128 |
|
| 129 |
refreshInterval := 5 * time.Second |
| 130 |
if err := blobClient.CopyAndWait(ctx, accountName, containerName, fileName, copyInput, refreshInterval); err != nil { |
| 131 |
t.Fatalf("Error copying: %s", err) |
| 132 |
} |
| 133 |
|
| 134 |
t.Logf("[DEBUG] Retrieving Properties..") |
| 135 |
props, err := blobClient.GetProperties(ctx, accountName, containerName, fileName, GetPropertiesInput{}) |
| 136 |
if err != nil { |
| 137 |
t.Fatalf("Error getting properties: %s", err) |
| 138 |
} |
| 139 |
|
| 140 |
if props.ContentLength == 0 { |
| 141 |
t.Fatalf("Expected the file to be there but looks like it isn't: %d", props.ContentLength) |
| 142 |
} |
| 143 |
|
| 144 |
t.Logf("[DEBUG] Deleting file..") |
| 145 |
if _, err := blobClient.Delete(ctx, accountName, containerName, fileName, DeleteInput{}); err != nil { |
| 146 |
t.Fatalf("Error deleting file: %s", err) |
| 147 |
} |
| 148 |
} |