Package cache contains caching functions.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
3.1 KiB

package cache
import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"
)
var kvTestCases = []kv{
{"foo", "bar"},
{"int", 666},
{"float", 23.42},
{"slice", []string{"foo", "bar"}},
}
func testKV(t *testing.T, b Backend) {
var (
ok bool
err error
)
iterTest := make(map[interface{}]interface{})
for _, testCase := range kvTestCases {
b.Set(testCase.Key, testCase.Value)
iterTest[testCase.Key] = testCase.Value
if ok, err = b.Contains(testCase.Key); err != nil {
t.Fatalf("contains failed: %v", err)
} else if !ok {
t.Fatalf("key %v missing", testCase.Key)
}
var value interface{}
if value, ok, err = b.Get(testCase.Key); err != nil {
t.Fatalf("get failed: %v", err)
} else if !ok {
t.Fatalf("key %v missing", testCase.Key)
} else if !reflect.DeepEqual(value, testCase.Value) {
t.Fatalf("corrupt, expected %+v, got %+v", testCase.Value, value)
}
}
if l := b.Len(); l != len(kvTestCases) {
t.Fatalf("corrupt, expected %d, got %d length", len(kvTestCases), l)
}
b.Iter(func(key, value interface{}) {
if !reflect.DeepEqual(value, iterTest[key]) {
t.Fatalf("expected %v, got %v", iterTest[key], value)
}
})
if ok, err = b.Replace("foo", "biz"); err != nil {
t.Fatalf("replace failed: %v", err)
} else if !ok {
t.Fatal("replace failed")
}
if ok, err = b.Replace("missing", "foo"); err != nil {
t.Fatalf("replace failed: %v", err)
} else if ok {
t.Fatal("replace failed, replace of missing entry returned true")
}
if ok, err = b.Delete("foo"); err != nil {
t.Fatalf("delete failed: %v", err)
} else if !ok {
t.Fatal("delete failed")
}
if ok, err = b.Delete("foo"); err != nil {
t.Fatalf("delete failed: %v", err)
} else if ok {
t.Fatal("delete failed, delete of missing entry returned true")
}
}
func TestKV(t *testing.T) {
t.Run("MemoryKV", func(t *testing.T) {
testKV(t, NewMemoryKV())
})
t.Run("DiskKV", func(t *testing.T) {
p := filepath.Join("testdata", "kv")
defer os.RemoveAll(p)
b, err := NewDiskKV(p, 0750)
if err != nil {
t.Fatal(err)
}
testKV(t, b)
})
}
func TestDiskKVError(t *testing.T) {
if os.Getuid() == 0 {
t.Skip("running as root")
}
var (
d *DiskKV
err error
)
// Test creation in privileged path
if d, err = NewDiskKV("/root", 0755); err == nil {
t.Fatal("NewDiskKV in /root should fail")
} else if d != nil {
t.Fatal("NewDiskKV must return nil on error")
} else {
t.Logf("error %v (expected)", err)
}
// Test reusage of privileged path
if d, err = NewDiskKV("/tmp", 0755); err == nil {
t.Fatal("NewDiskKV in /tmp should fail")
} else if d != nil {
t.Fatal("NewDiskKV must return nil on error")
} else {
t.Logf("error %v (expected)", err)
}
// Test creation over existing file
var f *os.File
if f, err = ioutil.TempFile("", "testkv"); err != nil {
t.Fatal(err)
} else {
defer os.Remove(f.Name())
defer f.Close()
if d, err = NewDiskKV(f.Name(), 0755); err == nil {
t.Fatalf("NewDiskKV in %s should fail", f.Name())
} else if d != nil {
t.Fatal("NewDiskKV must return nil on error")
} else {
t.Logf("error %v (expected)", err)
}
}
}