github.com/hashicorp/vault/sdk@v0.13.0/logical/storage_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package logical 5 6 import ( 7 "context" 8 "testing" 9 10 "github.com/go-test/deep" 11 ) 12 13 var keyList = []string{ 14 "a", 15 "b", 16 "d", 17 "foo", 18 "foo42", 19 "foo/a/b/c", 20 "c/d/e/f/g", 21 } 22 23 func TestScanView(t *testing.T) { 24 s := prepKeyStorage(t) 25 26 keys := make([]string, 0) 27 err := ScanView(context.Background(), s, func(path string) { 28 keys = append(keys, path) 29 }) 30 if err != nil { 31 t.Fatal(err) 32 } 33 34 if diff := deep.Equal(keys, keyList); diff != nil { 35 t.Fatal(diff) 36 } 37 } 38 39 func TestScanView_CancelContext(t *testing.T) { 40 s := prepKeyStorage(t) 41 42 ctx, cancelCtx := context.WithCancel(context.Background()) 43 var i int 44 err := ScanView(ctx, s, func(path string) { 45 cancelCtx() 46 i++ 47 }) 48 49 if err == nil { 50 t.Error("Want context cancel err, got none") 51 } 52 if i != 1 { 53 t.Errorf("Want i==1, got %d", i) 54 } 55 } 56 57 func TestCollectKeys(t *testing.T) { 58 s := prepKeyStorage(t) 59 60 keys, err := CollectKeys(context.Background(), s) 61 if err != nil { 62 t.Fatal(err) 63 } 64 65 if diff := deep.Equal(keys, keyList); diff != nil { 66 t.Fatal(diff) 67 } 68 } 69 70 func TestCollectKeysPrefix(t *testing.T) { 71 s := prepKeyStorage(t) 72 73 keys, err := CollectKeysWithPrefix(context.Background(), s, "foo") 74 if err != nil { 75 t.Fatal(err) 76 } 77 78 exp := []string{ 79 "foo", 80 "foo42", 81 "foo/a/b/c", 82 } 83 84 if diff := deep.Equal(keys, exp); diff != nil { 85 t.Fatal(diff) 86 } 87 } 88 89 func prepKeyStorage(t *testing.T) Storage { 90 t.Helper() 91 s := &InmemStorage{} 92 93 for _, key := range keyList { 94 if err := s.Put(context.Background(), &StorageEntry{ 95 Key: key, 96 Value: nil, 97 SealWrap: false, 98 }); err != nil { 99 t.Fatal(err) 100 } 101 } 102 103 return s 104 }