2021-02-18 04:39:52 +00:00
|
|
|
package overlay
|
2021-02-17 04:57:59 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
2021-02-20 02:41:44 +00:00
|
|
|
"io/fs"
|
2021-02-17 04:57:59 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2021-02-18 02:33:48 +00:00
|
|
|
//go:embed _test/embed
|
|
|
|
var embedded embed.FS
|
2021-02-17 04:57:59 +00:00
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
2021-02-18 04:39:52 +00:00
|
|
|
func TestOverlay(t *testing.T) {
|
2021-02-18 02:33:48 +00:00
|
|
|
tt := []struct {
|
|
|
|
Name string
|
|
|
|
File string
|
|
|
|
Expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "Embed",
|
|
|
|
File: "test1.txt",
|
|
|
|
Expected: "test1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Disk",
|
|
|
|
File: "test2.txt",
|
|
|
|
Expected: "test3",
|
|
|
|
},
|
2021-02-17 04:57:59 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 02:33:48 +00:00
|
|
|
x, err := New("_test/disk", embedded, WithSub("_test/embed"), WithCaching(false))
|
2021-02-17 04:57:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2021-02-18 02:33:48 +00:00
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
fi, err := x.Open(tc.File)
|
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
defer fi.Close()
|
|
|
|
|
2021-05-09 03:54:36 +00:00
|
|
|
contents, err := x.ReadFile(tc.File)
|
2021-02-18 02:33:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.EqualFold(string(contents), tc.Expected) {
|
2021-02-18 04:39:52 +00:00
|
|
|
t.Logf("fs did not match:\n\tgot: %s\n\texpected: %s\n", string(contents), tc.Expected)
|
2021-02-18 02:33:48 +00:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
})
|
2021-02-17 04:57:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-20 02:41:44 +00:00
|
|
|
func TestWalk(t *testing.T) {
|
|
|
|
expected := []string{"test1.txt", "test2.txt"}
|
|
|
|
|
|
|
|
x, err := New("_test/disk", embedded, WithSub("_test/embed"), WithCaching(false))
|
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
found := make([]string, 0)
|
|
|
|
if err := fs.WalkDir(x, ".", func(walkPath string, walkEntry fs.DirEntry, walkErr error) error {
|
|
|
|
if walkErr != nil {
|
|
|
|
return walkErr
|
|
|
|
}
|
|
|
|
|
|
|
|
if walkEntry.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
found = append(found, walkEntry.Name())
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, e := range expected {
|
|
|
|
exists := false
|
|
|
|
for _, f := range found {
|
|
|
|
if e == f {
|
|
|
|
exists = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
t.Logf("could not find %s in %s\n", e, found)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 02:33:48 +00:00
|
|
|
var emptyFS embed.FS
|
2021-02-17 04:57:59 +00:00
|
|
|
|
2021-02-18 02:33:48 +00:00
|
|
|
func TestInvalid(t *testing.T) {
|
|
|
|
_, err := New("/var/lib/myapp/assets/custom", emptyFS)
|
2021-02-17 04:57:59 +00:00
|
|
|
if err != nil {
|
2021-02-18 04:39:52 +00:00
|
|
|
t.Log("invalid FS should not error explicitly")
|
2021-02-17 04:57:59 +00:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkCache(b *testing.B) {
|
|
|
|
x, err := New("_test/disk", embedded)
|
|
|
|
if err != nil {
|
|
|
|
b.Log(err)
|
|
|
|
b.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx := 0; idx < b.N; idx++ {
|
|
|
|
x.exists("test2.txt")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkNoCache(b *testing.B) {
|
|
|
|
x, err := New("_test/disk", embedded, WithCaching(false))
|
|
|
|
if err != nil {
|
|
|
|
b.Log(err)
|
|
|
|
b.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx := 0; idx < b.N; idx++ {
|
|
|
|
x.exists("test2.txt")
|
|
|
|
}
|
|
|
|
}
|