2017-08-03 17:24:27 +00:00
|
|
|
package inmem
|
2015-04-14 18:00:51 +00:00
|
|
|
|
2016-04-26 03:10:32 +00:00
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2016-04-26 03:10:32 +00:00
|
|
|
"testing"
|
2016-08-19 20:45:17 +00:00
|
|
|
|
2020-10-13 15:11:54 +00:00
|
|
|
"github.com/armon/go-metrics"
|
2018-04-03 00:46:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/logging"
|
|
|
|
"github.com/hashicorp/vault/sdk/physical"
|
2016-04-26 03:10:32 +00:00
|
|
|
)
|
2015-04-14 18:00:51 +00:00
|
|
|
|
|
|
|
func TestCache(t *testing.T) {
|
2018-04-03 00:46:59 +00:00
|
|
|
logger := logging.NewVaultLogger(log.Debug)
|
2016-08-19 20:45:17 +00:00
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
inm, err := NewInmem(nil, logger)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-10-13 15:11:54 +00:00
|
|
|
|
|
|
|
cache := physical.NewCache(inm, 0, logger, &metrics.BlackholeSink{})
|
2019-10-08 20:23:43 +00:00
|
|
|
cache.SetEnabled(true)
|
2017-08-03 17:24:27 +00:00
|
|
|
physical.ExerciseBackend(t, cache)
|
|
|
|
physical.ExerciseBackend_ListPrefix(t, cache)
|
2015-04-14 18:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCache_Purge(t *testing.T) {
|
2018-04-03 00:46:59 +00:00
|
|
|
logger := logging.NewVaultLogger(log.Debug)
|
2016-08-19 20:45:17 +00:00
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
inm, err := NewInmem(nil, logger)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-10-13 15:11:54 +00:00
|
|
|
cache := physical.NewCache(inm, 0, logger, &metrics.BlackholeSink{})
|
2018-01-26 03:21:51 +00:00
|
|
|
cache.SetEnabled(true)
|
2015-04-14 18:00:51 +00:00
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
ent := &physical.Entry{
|
2015-04-14 18:00:51 +00:00
|
|
|
Key: "foo",
|
|
|
|
Value: []byte("bar"),
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
err = cache.Put(context.Background(), ent)
|
2015-04-14 18:00:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete from under
|
2018-01-19 06:44:44 +00:00
|
|
|
inm.Delete(context.Background(), "foo")
|
2018-01-26 03:21:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-14 18:00:51 +00:00
|
|
|
|
|
|
|
// Read should work
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err := cache.Get(context.Background(), "foo")
|
2015-04-14 18:00:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("should have key")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the cache
|
2018-01-19 22:00:13 +00:00
|
|
|
cache.Purge(context.Background())
|
2015-04-14 18:00:51 +00:00
|
|
|
|
|
|
|
// Read should fail
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err = cache.Get(context.Background(), "foo")
|
2015-04-14 18:00:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatalf("should not have key")
|
|
|
|
}
|
|
|
|
}
|
2017-03-03 21:04:31 +00:00
|
|
|
|
2018-01-26 03:21:51 +00:00
|
|
|
func TestCache_Disable(t *testing.T) {
|
2018-04-03 00:46:59 +00:00
|
|
|
logger := logging.NewVaultLogger(log.Debug)
|
2017-03-03 21:04:31 +00:00
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
inm, err := NewInmem(nil, logger)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-10-13 15:11:54 +00:00
|
|
|
cache := physical.NewCache(inm, 0, logger, &metrics.BlackholeSink{})
|
2017-08-03 17:24:27 +00:00
|
|
|
|
2018-01-26 03:21:51 +00:00
|
|
|
disabledTests := func() {
|
|
|
|
ent := &physical.Entry{
|
|
|
|
Key: "foo",
|
|
|
|
Value: []byte("bar"),
|
|
|
|
}
|
|
|
|
err = inm.Put(context.Background(), ent)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-03-03 21:04:31 +00:00
|
|
|
|
2018-01-26 03:21:51 +00:00
|
|
|
// Read should work
|
|
|
|
out, err := cache.Get(context.Background(), "foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("should have key")
|
|
|
|
}
|
2017-03-03 21:04:31 +00:00
|
|
|
|
2018-01-26 03:21:51 +00:00
|
|
|
err = inm.Delete(context.Background(), ent.Key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-03-03 21:04:31 +00:00
|
|
|
|
2018-01-26 03:21:51 +00:00
|
|
|
// Should not work
|
|
|
|
out, err = cache.Get(context.Background(), "foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatalf("should not have key")
|
|
|
|
}
|
2017-03-03 21:04:31 +00:00
|
|
|
|
2018-01-26 03:21:51 +00:00
|
|
|
// Put through the cache and try again
|
|
|
|
err = cache.Put(context.Background(), ent)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-12-06 17:25:17 +00:00
|
|
|
|
2018-01-26 03:21:51 +00:00
|
|
|
// Read should work in both
|
|
|
|
out, err = inm.Get(context.Background(), "foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("should have key")
|
|
|
|
}
|
|
|
|
out, err = cache.Get(context.Background(), "foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("should have key")
|
|
|
|
}
|
2017-12-06 17:25:17 +00:00
|
|
|
|
2018-01-26 03:21:51 +00:00
|
|
|
err = inm.Delete(context.Background(), ent.Key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-12-06 17:25:17 +00:00
|
|
|
|
2018-01-26 03:21:51 +00:00
|
|
|
// Should not work
|
|
|
|
out, err = cache.Get(context.Background(), "foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatalf("should not have key")
|
|
|
|
}
|
|
|
|
}
|
2017-12-06 17:25:17 +00:00
|
|
|
|
2018-01-26 03:21:51 +00:00
|
|
|
enabledTests := func() {
|
|
|
|
ent := &physical.Entry{
|
2017-12-06 17:25:17 +00:00
|
|
|
Key: "foo",
|
|
|
|
Value: []byte("bar"),
|
2018-01-26 03:21:51 +00:00
|
|
|
}
|
|
|
|
err = inm.Put(context.Background(), ent)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-12-06 17:25:17 +00:00
|
|
|
|
2018-01-26 03:21:51 +00:00
|
|
|
// Read should work
|
|
|
|
out, err := cache.Get(context.Background(), "foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("should have key")
|
|
|
|
}
|
2017-12-06 17:25:17 +00:00
|
|
|
|
2018-01-26 03:21:51 +00:00
|
|
|
err = inm.Delete(context.Background(), ent.Key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-12-06 17:25:17 +00:00
|
|
|
|
2018-01-26 03:21:51 +00:00
|
|
|
// Should work
|
|
|
|
out, err = cache.Get(context.Background(), "foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("should have key")
|
|
|
|
}
|
2017-12-06 17:25:17 +00:00
|
|
|
|
2018-01-26 03:21:51 +00:00
|
|
|
// Put through the cache and try again
|
|
|
|
err = cache.Put(context.Background(), ent)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-12-06 17:25:17 +00:00
|
|
|
|
2018-01-26 03:21:51 +00:00
|
|
|
// Read should work for both
|
|
|
|
out, err = inm.Get(context.Background(), "foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("should have key")
|
|
|
|
}
|
|
|
|
out, err = cache.Get(context.Background(), "foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("should have key")
|
|
|
|
}
|
2017-12-06 17:25:17 +00:00
|
|
|
|
2018-01-26 03:21:51 +00:00
|
|
|
err = inm.Delete(context.Background(), ent.Key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-12-06 17:25:17 +00:00
|
|
|
|
2018-01-26 03:21:51 +00:00
|
|
|
// Should work
|
|
|
|
out, err = cache.Get(context.Background(), "foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("should have key")
|
|
|
|
}
|
2017-12-06 17:25:17 +00:00
|
|
|
|
2018-01-26 03:21:51 +00:00
|
|
|
// Put through the cache
|
|
|
|
err = cache.Put(context.Background(), ent)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-12-06 17:25:17 +00:00
|
|
|
|
2018-01-26 03:21:51 +00:00
|
|
|
// Read should work for both
|
|
|
|
out, err = inm.Get(context.Background(), "foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("should have key")
|
|
|
|
}
|
|
|
|
out, err = cache.Get(context.Background(), "foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("should have key")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete via cache
|
|
|
|
err = cache.Delete(context.Background(), ent.Key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read should not work for either
|
|
|
|
out, err = inm.Get(context.Background(), "foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatalf("should not have key")
|
|
|
|
}
|
|
|
|
out, err = cache.Get(context.Background(), "foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatalf("should not have key")
|
|
|
|
}
|
2017-12-06 17:25:17 +00:00
|
|
|
}
|
2018-01-26 03:21:51 +00:00
|
|
|
|
|
|
|
disabledTests()
|
|
|
|
cache.SetEnabled(true)
|
|
|
|
enabledTests()
|
|
|
|
cache.SetEnabled(false)
|
|
|
|
disabledTests()
|
2017-12-06 17:25:17 +00:00
|
|
|
}
|
2019-10-08 20:23:43 +00:00
|
|
|
|
|
|
|
func TestCache_Refresh(t *testing.T) {
|
|
|
|
logger := logging.NewVaultLogger(log.Debug)
|
|
|
|
|
|
|
|
inm, err := NewInmem(nil, logger)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-10-13 15:11:54 +00:00
|
|
|
cache := physical.NewCache(inm, 0, logger, &metrics.BlackholeSink{})
|
2019-10-08 20:23:43 +00:00
|
|
|
cache.SetEnabled(true)
|
|
|
|
|
|
|
|
ent := &physical.Entry{
|
|
|
|
Key: "foo",
|
|
|
|
Value: []byte("bar"),
|
|
|
|
}
|
|
|
|
err = cache.Put(context.Background(), ent)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ent2 := &physical.Entry{
|
|
|
|
Key: "foo",
|
|
|
|
Value: []byte("baz"),
|
|
|
|
}
|
|
|
|
// Update below cache
|
|
|
|
err = inm.Put(context.Background(), ent2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := cache.Get(context.Background(), "foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(r.Value) != "bar" {
|
|
|
|
t.Fatalf("expected value bar, got %s", string(r.Value))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Refresh the cache
|
|
|
|
r, err = cache.Get(physical.CacheRefreshContext(context.Background(), true), "foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(r.Value) != "baz" {
|
|
|
|
t.Fatalf("expected value baz, got %s", string(r.Value))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure new value is in cache
|
|
|
|
r, err = cache.Get(context.Background(), "foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if string(r.Value) != "baz" {
|
|
|
|
t.Fatalf("expected value baz, got %s", string(r.Value))
|
|
|
|
}
|
|
|
|
}
|