| 1 |
package queues |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"log" |
| 7 |
"testing" |
| 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 TestQueuesLifecycle(t *testing.T) { |
| 15 |
client, err := testhelpers.Build(t) |
| 16 |
if err != nil { |
| 17 |
t.Fatal(err) |
| 18 |
} |
| 19 |
ctx := context.TODO() |
| 20 |
resourceGroup := fmt.Sprintf("acctestrg-%d", testhelpers.RandomInt()) |
| 21 |
accountName := fmt.Sprintf("acctestsa%s", testhelpers.RandomString()) |
| 22 |
queueName := fmt.Sprintf("queue-%d", testhelpers.RandomInt()) |
| 23 |
|
| •24 |
testData, err := client.BuildTestResources(ctx, resourceGroup, accountName, storage.Storage) |
| 25 |
if err != nil { |
| 26 |
t.Fatal(err) |
| 27 |
} |
| 28 |
defer client.DestroyTestResources(ctx, resourceGroup, accountName) |
| 29 |
|
| 30 |
storageAuth := auth.NewSharedKeyLiteAuthorizer(accountName, testData.StorageAccountKey) |
| 31 |
queuesClient := NewWithEnvironment(client.Environment) |
| 32 |
queuesClient.Client = client.PrepareWithAuthorizer(queuesClient.Client, storageAuth) |
| 33 |
|
| 34 |
// first let's test an empty container |
| 35 |
_, err = queuesClient.Create(ctx, accountName, queueName, map[string]string{}) |
| 36 |
if err != nil { |
| 37 |
t.Fatal(fmt.Errorf("Error creating: %s", err)) |
| 38 |
} |
| 39 |
|
| 40 |
// then let's retrieve it to ensure there's no metadata.. |
| 41 |
resp, err := queuesClient.GetMetaData(ctx, accountName, queueName) |
| 42 |
if err != nil { |
| 43 |
t.Fatalf("Error retrieving MetaData: %s", err) |
| 44 |
} |
| 45 |
if len(resp.MetaData) != 0 { |
| 46 |
t.Fatalf("Expected no MetaData but got: %s", err) |
| 47 |
} |
| 48 |
|
| 49 |
// then let's add some.. |
| 50 |
updatedMetaData := map[string]string{ |
| 51 |
"band": "panic", |
| 52 |
"boots": "the-overpass", |
| 53 |
} |
| 54 |
_, err = queuesClient.SetMetaData(ctx, accountName, queueName, updatedMetaData) |
| 55 |
if err != nil { |
| 56 |
t.Fatalf("Error setting MetaData: %s", err) |
| 57 |
} |
| 58 |
|
| 59 |
resp, err = queuesClient.GetMetaData(ctx, accountName, queueName) |
| 60 |
if err != nil { |
| 61 |
t.Fatalf("Error re-retrieving MetaData: %s", err) |
| 62 |
} |
| 63 |
|
| 64 |
if len(resp.MetaData) != 2 { |
| 65 |
t.Fatalf("Expected metadata to have 2 items but got: %s", resp.MetaData) |
| 66 |
} |
| 67 |
if resp.MetaData["band"] != "panic" { |
| 68 |
t.Fatalf("Expected `band` to be `panic` but got: %s", resp.MetaData["band"]) |
| 69 |
} |
| 70 |
if resp.MetaData["boots"] != "the-overpass" { |
| 71 |
t.Fatalf("Expected `boots` to be `the-overpass` but got: %s", resp.MetaData["boots"]) |
| 72 |
} |
| 73 |
|
| 74 |
// and woo let's remove it again |
| 75 |
_, err = queuesClient.SetMetaData(ctx, accountName, queueName, map[string]string{}) |
| 76 |
if err != nil { |
| 77 |
t.Fatalf("Error setting MetaData: %s", err) |
| 78 |
} |
| 79 |
|
| 80 |
resp, err = queuesClient.GetMetaData(ctx, accountName, queueName) |
| 81 |
if err != nil { |
| 82 |
t.Fatalf("Error retrieving MetaData: %s", err) |
| 83 |
} |
| 84 |
if len(resp.MetaData) != 0 { |
| 85 |
t.Fatalf("Expected no MetaData but got: %s", err) |
| 86 |
} |
| 87 |
|
| 88 |
log.Printf("[DEBUG] Deleting..") |
| 89 |
_, err = queuesClient.Delete(ctx, accountName, queueName) |
| 90 |
if err != nil { |
| 91 |
t.Fatal(fmt.Errorf("Error deleting: %s", err)) |
| 92 |
} |
| 93 |
} |