2017-10-11 17:21:20 +00:00
|
|
|
package storagepacker
|
|
|
|
|
|
|
|
import (
|
2019-05-01 17:47:41 +00:00
|
|
|
"context"
|
2019-06-20 20:02:11 +00:00
|
|
|
"fmt"
|
2017-10-11 17:21:20 +00:00
|
|
|
"testing"
|
|
|
|
|
2018-05-03 17:19:39 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
2017-10-11 17:21:20 +00:00
|
|
|
"github.com/golang/protobuf/ptypes"
|
2018-04-03 00:46:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2017-10-11 17:21:20 +00:00
|
|
|
uuid "github.com/hashicorp/go-uuid"
|
|
|
|
"github.com/hashicorp/vault/helper/identity"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2017-10-11 17:21:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkStoragePacker(b *testing.B) {
|
2018-04-03 00:46:59 +00:00
|
|
|
storagePacker, err := NewStoragePacker(&logical.InmemStorage{}, log.New(&log.LoggerOptions{Name: "storagepackertest"}), "")
|
2017-10-11 17:21:20 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-05-01 17:47:41 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2017-10-11 17:21:20 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
itemID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
item := &Item{
|
|
|
|
ID: itemID,
|
|
|
|
}
|
|
|
|
|
2019-05-01 17:47:41 +00:00
|
|
|
err = storagePacker.PutItem(ctx, item)
|
2017-10-11 17:21:20 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchedItem, err := storagePacker.GetItem(itemID)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if fetchedItem == nil {
|
|
|
|
b.Fatalf("failed to read stored item with ID: %q, iteration: %d", item.ID, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
if fetchedItem.ID != item.ID {
|
|
|
|
b.Fatalf("bad: item ID; expected: %q\n actual: %q", item.ID, fetchedItem.ID)
|
|
|
|
}
|
|
|
|
|
2019-05-01 17:47:41 +00:00
|
|
|
err = storagePacker.DeleteItem(ctx, item.ID)
|
2017-10-11 17:21:20 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchedItem, err = storagePacker.GetItem(item.ID)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
if fetchedItem != nil {
|
|
|
|
b.Fatalf("failed to delete item")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStoragePacker(t *testing.T) {
|
2018-04-03 00:46:59 +00:00
|
|
|
storagePacker, err := NewStoragePacker(&logical.InmemStorage{}, log.New(&log.LoggerOptions{Name: "storagepackertest"}), "")
|
2017-10-11 17:21:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-05-01 17:47:41 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2017-10-11 17:21:20 +00:00
|
|
|
// Persist a storage entry
|
|
|
|
item1 := &Item{
|
|
|
|
ID: "item1",
|
|
|
|
}
|
|
|
|
|
2019-05-01 17:47:41 +00:00
|
|
|
err = storagePacker.PutItem(ctx, item1)
|
2017-10-11 17:21:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that it can be read
|
|
|
|
fetchedItem, err := storagePacker.GetItem(item1.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if fetchedItem == nil {
|
|
|
|
t.Fatalf("failed to read the stored item")
|
|
|
|
}
|
|
|
|
|
|
|
|
if item1.ID != fetchedItem.ID {
|
|
|
|
t.Fatalf("bad: item ID; expected: %q\n actual: %q\n", item1.ID, fetchedItem.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete item1
|
2019-05-01 17:47:41 +00:00
|
|
|
err = storagePacker.DeleteItem(ctx, item1.ID)
|
2017-10-11 17:21:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the deletion was successful
|
|
|
|
fetchedItem, err = storagePacker.GetItem(item1.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if fetchedItem != nil {
|
|
|
|
t.Fatalf("failed to delete item")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStoragePacker_SerializeDeserializeComplexItem(t *testing.T) {
|
2018-04-03 00:46:59 +00:00
|
|
|
storagePacker, err := NewStoragePacker(&logical.InmemStorage{}, log.New(&log.LoggerOptions{Name: "storagepackertest"}), "")
|
2017-10-11 17:21:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-05-01 17:47:41 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2017-10-11 17:21:20 +00:00
|
|
|
timeNow := ptypes.TimestampNow()
|
|
|
|
|
|
|
|
alias1 := &identity.Alias{
|
|
|
|
ID: "alias_id",
|
2017-11-03 14:29:42 +00:00
|
|
|
CanonicalID: "canonical_id",
|
2017-10-11 17:21:20 +00:00
|
|
|
MountType: "mount_type",
|
|
|
|
MountAccessor: "mount_accessor",
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"aliasmkey": "aliasmvalue",
|
|
|
|
},
|
2017-11-03 14:29:42 +00:00
|
|
|
Name: "alias_name",
|
|
|
|
CreationTime: timeNow,
|
|
|
|
LastUpdateTime: timeNow,
|
|
|
|
MergedFromCanonicalIDs: []string{"merged_from_canonical_id"},
|
2017-10-11 17:21:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
entity := &identity.Entity{
|
|
|
|
Aliases: []*identity.Alias{alias1},
|
|
|
|
ID: "entity_id",
|
|
|
|
Name: "entity_name",
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"testkey1": "testvalue1",
|
|
|
|
"testkey2": "testvalue2",
|
|
|
|
},
|
|
|
|
CreationTime: timeNow,
|
|
|
|
LastUpdateTime: timeNow,
|
2019-05-07 23:42:44 +00:00
|
|
|
BucketKey: "entity_hash",
|
2017-10-11 17:21:20 +00:00
|
|
|
MergedEntityIDs: []string{"merged_entity_id1", "merged_entity_id2"},
|
|
|
|
Policies: []string{"policy1", "policy2"},
|
|
|
|
}
|
|
|
|
|
|
|
|
marshaledEntity, err := ptypes.MarshalAny(entity)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-05-01 17:47:41 +00:00
|
|
|
err = storagePacker.PutItem(ctx, &Item{
|
2017-10-11 17:21:20 +00:00
|
|
|
ID: entity.ID,
|
|
|
|
Message: marshaledEntity,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
itemFetched, err := storagePacker.GetItem(entity.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var itemDecoded identity.Entity
|
|
|
|
err = ptypes.UnmarshalAny(itemFetched.Message, &itemDecoded)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-05-03 17:19:39 +00:00
|
|
|
if !proto.Equal(&itemDecoded, entity) {
|
2017-10-11 17:21:20 +00:00
|
|
|
t.Fatalf("bad: expected: %#v\nactual: %#v\n", entity, itemDecoded)
|
|
|
|
}
|
|
|
|
}
|
2019-06-20 20:02:11 +00:00
|
|
|
|
|
|
|
func TestStoragePacker_DeleteMultiple(t *testing.T) {
|
|
|
|
storagePacker, err := NewStoragePacker(&logical.InmemStorage{}, log.New(&log.LoggerOptions{Name: "storagepackertest"}), "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// Persist a storage entry
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
item := &Item{
|
|
|
|
ID: fmt.Sprintf("item%d", i),
|
|
|
|
}
|
|
|
|
|
|
|
|
err = storagePacker.PutItem(ctx, item)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that it can be read
|
|
|
|
fetchedItem, err := storagePacker.GetItem(item.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if fetchedItem == nil {
|
|
|
|
t.Fatalf("failed to read the stored item")
|
|
|
|
}
|
|
|
|
|
|
|
|
if item.ID != fetchedItem.ID {
|
|
|
|
t.Fatalf("bad: item ID; expected: %q\n actual: %q\n", item.ID, fetchedItem.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
itemsToDelete := make([]string, 0, 50)
|
|
|
|
for i := 1; i < 100; i += 2 {
|
|
|
|
itemsToDelete = append(itemsToDelete, fmt.Sprintf("item%d", i))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = storagePacker.DeleteMultipleItems(ctx, nil, itemsToDelete...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the deletion was successful
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
fetchedItem, err := storagePacker.GetItem(fmt.Sprintf("item%d", i))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i%2 == 0 && fetchedItem == nil {
|
|
|
|
t.Fatal("expected item not found")
|
|
|
|
}
|
|
|
|
if i%2 == 1 && fetchedItem != nil {
|
|
|
|
t.Fatalf("failed to delete item")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|