1
0
out-of-tree/distro/debian/cache_test.go

107 lines
1.6 KiB
Go
Raw Permalink Normal View History

2023-05-11 18:45:44 +00:00
package debian
import (
"os"
"path/filepath"
"testing"
"github.com/rapidloop/skv"
2023-05-13 12:02:41 +00:00
"code.dumpstack.io/tools/out-of-tree/distro/debian/snapshot"
2023-05-11 18:45:44 +00:00
)
func TestCache(t *testing.T) {
dir, err := os.MkdirTemp("", "out-of-tree_cache_test_")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
path := filepath.Join(dir, "debian.cache")
c, err := NewCache(path)
if err != nil {
t.Fatal(err)
}
2023-05-13 12:02:41 +00:00
image := snapshot.Package{}
image.Deb.Hash = "12345"
2023-05-11 19:26:54 +00:00
version := "4.17.14-1"
2023-05-13 12:02:41 +00:00
dk := DebianKernel{
Version: DebianKernelVersion{Package: version},
Image: image,
2023-05-11 18:45:44 +00:00
}
err = c.Put([]DebianKernel{dk})
2023-05-11 18:45:44 +00:00
if err != nil {
t.Fatal(err)
}
dk2s, err := c.Get(version)
2023-05-11 18:45:44 +00:00
if err != nil {
t.Fatal(err)
}
dk2 := dk2s[0]
2023-05-11 18:45:44 +00:00
2023-05-11 19:26:54 +00:00
if dk.Image.Deb.Hash != dk2.Image.Deb.Hash {
2023-05-11 18:45:44 +00:00
t.Fatalf("mismatch")
}
c.Close()
c, err = NewCache(path)
if err != nil {
t.Fatal(err)
}
defer c.Close()
dk3s, err := c.Get(version)
2023-05-11 18:45:44 +00:00
if err != nil {
t.Fatal(err)
}
dk3 := dk3s[0]
2023-05-11 18:45:44 +00:00
2023-05-11 19:26:54 +00:00
if dk.Image.Deb.Hash != dk3.Image.Deb.Hash {
2023-05-11 18:45:44 +00:00
t.Fatalf("mismatch")
}
2023-05-11 19:26:54 +00:00
_, err = c.Get("key not exist")
2023-05-11 18:45:44 +00:00
if err == nil || err != skv.ErrNotFound {
t.Fatal(err)
}
}
2023-05-13 18:43:15 +00:00
func TestVersionsCache(t *testing.T) {
dir, err := os.MkdirTemp("", "out-of-tree_cache_test_")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
path := filepath.Join(dir, "debian.cache")
c, err := NewCache(path)
if err != nil {
t.Fatal(err)
}
defer c.Close()
versions := []string{"a", "b", "c"}
err = c.PutVersions(versions)
if err != nil {
t.Fatal(err)
}
result, err := c.GetVersions()
if err != nil {
t.Fatal(err)
}
if len(versions) != len(result) {
t.Fatal("mismatch")
}
}