open-vault/sdk/logical/storage_test.go
Brian Kassouf 303c2aee7c
Run a more strict formatter over the code (#11312)
* Update tooling

* Run gofumpt

* go mod vendor
2021-04-08 09:43:39 -07:00

102 lines
1.6 KiB
Go

package logical
import (
"context"
"testing"
"github.com/go-test/deep"
)
var keyList = []string{
"a",
"b",
"d",
"foo",
"foo42",
"foo/a/b/c",
"c/d/e/f/g",
}
func TestScanView(t *testing.T) {
s := prepKeyStorage(t)
keys := make([]string, 0)
err := ScanView(context.Background(), s, func(path string) {
keys = append(keys, path)
})
if err != nil {
t.Fatal(err)
}
if diff := deep.Equal(keys, keyList); diff != nil {
t.Fatal(diff)
}
}
func TestScanView_CancelContext(t *testing.T) {
s := prepKeyStorage(t)
ctx, cancelCtx := context.WithCancel(context.Background())
var i int
err := ScanView(ctx, s, func(path string) {
cancelCtx()
i++
})
if err == nil {
t.Error("Want context cancel err, got none")
}
if i != 1 {
t.Errorf("Want i==1, got %d", i)
}
}
func TestCollectKeys(t *testing.T) {
s := prepKeyStorage(t)
keys, err := CollectKeys(context.Background(), s)
if err != nil {
t.Fatal(err)
}
if diff := deep.Equal(keys, keyList); diff != nil {
t.Fatal(diff)
}
}
func TestCollectKeysPrefix(t *testing.T) {
s := prepKeyStorage(t)
keys, err := CollectKeysWithPrefix(context.Background(), s, "foo")
if err != nil {
t.Fatal(err)
}
exp := []string{
"foo",
"foo42",
"foo/a/b/c",
}
if diff := deep.Equal(keys, exp); diff != nil {
t.Fatal(diff)
}
}
func prepKeyStorage(t *testing.T) Storage {
t.Helper()
s := &InmemStorage{}
for _, key := range keyList {
if err := s.Put(context.Background(), &StorageEntry{
Key: key,
Value: nil,
SealWrap: false,
}); err != nil {
t.Fatal(err)
}
}
return s
}