| 1 |
package tables |
| 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 TestTablesLifecycle(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 |
tableName := fmt.Sprintf("table%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.NewSharedKeyLiteTableAuthorizer(accountName, testData.StorageAccountKey) |
| 31 |
tablesClient := NewWithEnvironment(client.Environment) |
| 32 |
tablesClient.Client = client.PrepareWithAuthorizer(tablesClient.Client, storageAuth) |
| 33 |
|
| 34 |
t.Logf("[DEBUG] Creating Table..") |
| 35 |
if _, err := tablesClient.Create(ctx, accountName, tableName); err != nil { |
| 36 |
t.Fatalf("Error creating Table %q: %s", tableName, err) |
| 37 |
} |
| 38 |
|
| 39 |
// first look it up directly and confirm it's there |
| 40 |
t.Logf("[DEBUG] Checking if Table exists..") |
| 41 |
if _, err := tablesClient.Exists(ctx, accountName, tableName); err != nil { |
| 42 |
t.Fatalf("Error checking if Table %q exists: %s", tableName, err) |
| 43 |
} |
| 44 |
|
| 45 |
// then confirm it exists in the Query too |
| 46 |
t.Logf("[DEBUG] Querying for Tables..") |
| 47 |
result, err := tablesClient.Query(ctx, accountName, NoMetaData) |
| 48 |
if err != nil { |
| 49 |
t.Fatalf("Error retrieving Tables: %s", err) |
| 50 |
} |
| 51 |
found := false |
| 52 |
for _, v := range result.Tables { |
| 53 |
log.Printf("[DEBUG] Table: %q", v.TableName) |
| 54 |
|
| 55 |
if v.TableName == tableName { |
| 56 |
found = true |
| 57 |
} |
| 58 |
} |
| 59 |
if !found { |
| 60 |
t.Fatalf("%q was not found in the Query response!", tableName) |
| 61 |
} |
| 62 |
|
| 63 |
t.Logf("[DEBUG] Setting ACL's for Table %q..", tableName) |
| 64 |
acls := []SignedIdentifier{ |
| 65 |
{ |
| 66 |
Id: "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=", |
| 67 |
AccessPolicy: AccessPolicy{ |
| 68 |
Permission: "raud", |
| 69 |
Start: "2020-11-26T08:49:37.0000000Z", |
| 70 |
Expiry: "2020-11-27T08:49:37.0000000Z", |
| 71 |
}, |
| 72 |
}, |
| 73 |
} |
| 74 |
if _, err := tablesClient.SetACL(ctx, accountName, tableName, acls); err != nil { |
| 75 |
t.Fatalf("Error setting ACLs: %s", err) |
| 76 |
} |
| 77 |
|
| 78 |
t.Logf("[DEBUG] Retrieving ACL's for Table %q..", tableName) |
| 79 |
retrievedACLs, err := tablesClient.GetACL(ctx, accountName, tableName) |
| 80 |
if err != nil { |
| 81 |
t.Fatalf("Error retrieving ACLs: %s", err) |
| 82 |
} |
| 83 |
|
| 84 |
if len(retrievedACLs.SignedIdentifiers) != len(acls) { |
| 85 |
t.Fatalf("Expected %d but got %q ACLs", len(retrievedACLs.SignedIdentifiers), len(acls)) |
| 86 |
} |
| 87 |
|
| 88 |
for i, retrievedAcl := range retrievedACLs.SignedIdentifiers { |
| 89 |
expectedAcl := acls[i] |
| 90 |
|
| 91 |
if retrievedAcl.Id != expectedAcl.Id { |
| 92 |
t.Fatalf("Expected ID to be %q but got %q", retrievedAcl.Id, expectedAcl.Id) |
| 93 |
} |
| 94 |
|
| 95 |
if retrievedAcl.AccessPolicy.Start != expectedAcl.AccessPolicy.Start { |
| 96 |
t.Fatalf("Expected Start to be %q but got %q", retrievedAcl.AccessPolicy.Start, expectedAcl.AccessPolicy.Start) |
| 97 |
} |
| 98 |
|
| 99 |
if retrievedAcl.AccessPolicy.Expiry != expectedAcl.AccessPolicy.Expiry { |
| 100 |
t.Fatalf("Expected Expiry to be %q but got %q", retrievedAcl.AccessPolicy.Expiry, expectedAcl.AccessPolicy.Expiry) |
| 101 |
} |
| 102 |
|
| 103 |
if retrievedAcl.AccessPolicy.Permission != expectedAcl.AccessPolicy.Permission { |
| 104 |
t.Fatalf("Expected Permission to be %q but got %q", retrievedAcl.AccessPolicy.Permission, expectedAcl.AccessPolicy.Permission) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
t.Logf("[DEBUG] Deleting Table %q..", tableName) |
| 109 |
if _, err := tablesClient.Delete(ctx, accountName, tableName); err != nil { |
| 110 |
t.Fatalf("Error deleting %q: %s", tableName, err) |
| 111 |
} |
| 112 |
} |