Skip to content

Commit

Permalink
testing cache refresh on policy manager
Browse files Browse the repository at this point in the history
Signed-off-by: Alekh Raj [email protected]
  • Loading branch information
alekh2 committed Jun 5, 2024
1 parent 6536b13 commit 051e65f
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions sdk/helper/keysutil/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1067,3 +1067,73 @@ func isUnsupportedGoHashType(hashType HashType, err error) bool {

return false
}

func Test_KeyRefresh(t *testing.T) {
ctx := context.Background()
storage := &logical.InmemStorage{}
lockManagerWithCache, _ := NewLockManager(true, 0)

// create a new policy
p, upserted, err := lockManagerWithCache.GetPolicy(ctx, PolicyRequest{
Upsert: true,
Storage: storage,
KeyType: KeyType_AES256_GCM96,
Name: "test",
}, rand.Reader)

if err != nil {
t.Fatal(err)
}
if p == nil {
t.Fatal("nil policy")
}
if !upserted {
t.Fatal("expected an upsert")
}

// re-access a new policy
p, upserted, err = lockManagerWithCache.GetPolicy(ctx, PolicyRequest{
Upsert: true,
Storage: storage,
KeyType: KeyType_AES256_GCM96,
Name: "test",
}, rand.Reader)

if upserted {
t.Fatal("should not be upsert")
}

// clean up backend storage
storage = &logical.InmemStorage{}

// read should return true
p, upserted, err = lockManagerWithCache.GetPolicy(ctx, PolicyRequest{
Upsert: true,
Storage: storage,
KeyType: KeyType_AES256_GCM96,
Name: "test",
}, rand.Reader)

if upserted {
t.Fatal("should not be upsert")
}

// refresh cache
ctx = CacheRefreshContext(ctx, true)

// read should re-create the key
p, upserted, err = lockManagerWithCache.GetPolicy(ctx, PolicyRequest{
Upsert: true,
Storage: storage,
KeyType: KeyType_AES256_GCM96,
Name: "test",
}, rand.Reader)

if !upserted {
t.Fatal("should be upsert")
}

if !lockManagerWithCache.useCache {
p.Unlock()
}
}

0 comments on commit 051e65f

Please sign in to comment.