2015-04-14 18:00:51 +00:00
|
|
|
package physical
|
|
|
|
|
2016-04-26 03:10:32 +00:00
|
|
|
import (
|
|
|
|
"testing"
|
2016-08-19 20:45:17 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/vault/helper/logformat"
|
|
|
|
log "github.com/mgutz/logxi/v1"
|
2016-04-26 03:10:32 +00:00
|
|
|
)
|
2015-04-14 18:00:51 +00:00
|
|
|
|
|
|
|
func TestCache(t *testing.T) {
|
2016-08-19 20:45:17 +00:00
|
|
|
logger := logformat.NewVaultLogger(log.LevelTrace)
|
|
|
|
|
2016-04-26 03:10:32 +00:00
|
|
|
inm := NewInmem(logger)
|
2016-08-26 14:27:06 +00:00
|
|
|
cache := NewCache(inm, 0, logger)
|
2015-04-14 18:00:51 +00:00
|
|
|
testBackend(t, cache)
|
|
|
|
testBackend_ListPrefix(t, cache)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCache_Purge(t *testing.T) {
|
2016-08-19 20:45:17 +00:00
|
|
|
logger := logformat.NewVaultLogger(log.LevelTrace)
|
|
|
|
|
2016-04-26 03:10:32 +00:00
|
|
|
inm := NewInmem(logger)
|
2016-08-26 14:27:06 +00:00
|
|
|
cache := NewCache(inm, 0, logger)
|
2015-04-14 18:00:51 +00:00
|
|
|
|
|
|
|
ent := &Entry{
|
|
|
|
Key: "foo",
|
|
|
|
Value: []byte("bar"),
|
|
|
|
}
|
|
|
|
err := cache.Put(ent)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete from under
|
|
|
|
inm.Delete("foo")
|
|
|
|
|
|
|
|
// Read should work
|
|
|
|
out, err := cache.Get("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("should have key")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the cache
|
|
|
|
cache.Purge()
|
|
|
|
|
|
|
|
// Read should fail
|
|
|
|
out, err = cache.Get("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
|
|
|
|
|
|
|
func TestCache_IgnoreCore(t *testing.T) {
|
|
|
|
logger := logformat.NewVaultLogger(log.LevelTrace)
|
|
|
|
|
|
|
|
inm := NewInmem(logger)
|
|
|
|
cache := NewCache(inm, 0, logger)
|
|
|
|
|
|
|
|
var ent *Entry
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// First try normal handling
|
|
|
|
ent = &Entry{
|
|
|
|
Key: "foo",
|
|
|
|
Value: []byte("bar"),
|
|
|
|
}
|
|
|
|
if err := cache.Put(ent); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ent = &Entry{
|
|
|
|
Key: "foo",
|
|
|
|
Value: []byte("foobar"),
|
|
|
|
}
|
|
|
|
if err := inm.Put(ent); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ent, err = cache.Get("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if string(ent.Value) != "bar" {
|
|
|
|
t.Fatal("expected cached value")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now try core path
|
|
|
|
ent = &Entry{
|
|
|
|
Key: "core/foo",
|
|
|
|
Value: []byte("bar"),
|
|
|
|
}
|
|
|
|
if err := cache.Put(ent); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ent = &Entry{
|
|
|
|
Key: "core/foo",
|
|
|
|
Value: []byte("foobar"),
|
|
|
|
}
|
|
|
|
if err := inm.Put(ent); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ent, err = cache.Get("core/foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if string(ent.Value) != "foobar" {
|
|
|
|
t.Fatal("expected cached value")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now make sure looked-up values aren't added
|
|
|
|
ent = &Entry{
|
|
|
|
Key: "core/zip",
|
|
|
|
Value: []byte("zap"),
|
|
|
|
}
|
|
|
|
if err := inm.Put(ent); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ent, err = cache.Get("core/zip")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if string(ent.Value) != "zap" {
|
|
|
|
t.Fatal("expected non-cached value")
|
|
|
|
}
|
|
|
|
ent = &Entry{
|
|
|
|
Key: "core/zip",
|
|
|
|
Value: []byte("zipzap"),
|
|
|
|
}
|
|
|
|
if err := inm.Put(ent); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ent, err = cache.Get("core/zip")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if string(ent.Value) != "zipzap" {
|
|
|
|
t.Fatal("expected non-cached value")
|
|
|
|
}
|
|
|
|
}
|