243 lines
8.7 KiB
Go
243 lines
8.7 KiB
Go
package artifactcache
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
|
)
|
|
|
|
func newStore(t *testing.T, root string) *FilesystemStore {
|
|
t.Helper()
|
|
store, err := NewFilesystemStore(root)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return store
|
|
}
|
|
|
|
func TestFilesystemCacheUsesUnchangedHashAndRawArtifact(t *testing.T) {
|
|
root := filepath.Join(t.TempDir(), "selected cache ")
|
|
store := newStore(t, root)
|
|
const hash = "opaque-native:ABC_def-123"
|
|
ctx := context.Background()
|
|
content, err := store.Lookup(ctx, contracts.CacheAuto, hash)
|
|
if err != nil || content != nil {
|
|
t.Fatalf("missing lookup = %q, %v", content, err)
|
|
}
|
|
if _, err := os.Stat(root); !errors.Is(err, os.ErrNotExist) {
|
|
t.Fatalf("lookup created the cache root: %v", err)
|
|
}
|
|
want := []byte("{\n \"items\": [\"accepted\"]\n}\n")
|
|
if err := store.Store(ctx, contracts.CacheAuto, hash, want); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
onDisk, err := os.ReadFile(filepath.Join(root, hash+".json"))
|
|
if err != nil || !bytes.Equal(onDisk, want) {
|
|
t.Fatalf("stored bytes = %q, %v; want unchanged artifact bytes", onDisk, err)
|
|
}
|
|
got, err := store.Lookup(ctx, contracts.CacheAuto, hash)
|
|
if err != nil || !bytes.Equal(got, want) {
|
|
t.Fatalf("lookup = %q, %v", got, err)
|
|
}
|
|
entries, err := os.ReadDir(root)
|
|
if err != nil || len(entries) != 1 || entries[0].Name() != hash+".json" {
|
|
t.Fatalf("cache contains other than the raw artifact: %v, %v", entries, err)
|
|
}
|
|
}
|
|
|
|
func TestFilesystemCacheModesPreservePriorAcceptedResultUntilReplacement(t *testing.T) {
|
|
ctx := context.Background()
|
|
for _, mode := range []contracts.CacheMode{contracts.CacheAuto, contracts.CacheBypass, contracts.CacheRefresh} {
|
|
t.Run(string(mode), func(t *testing.T) {
|
|
root := t.TempDir()
|
|
store := newStore(t, root)
|
|
before, after := []byte(`{"items":["before"]}`), []byte(`{"items":["after"]}`)
|
|
if err := store.Store(ctx, contracts.CacheAuto, "task", before); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
found, err := store.Lookup(ctx, mode, "task")
|
|
if err != nil || (found != nil) != (mode == contracts.CacheAuto) {
|
|
t.Fatalf("%s lookup = %q, %v", mode, found, err)
|
|
}
|
|
if err := store.Store(ctx, mode, "task", []byte("{")); mode != contracts.CacheBypass && err == nil {
|
|
t.Fatal("malformed replacement was accepted")
|
|
}
|
|
found, err = store.Lookup(ctx, contracts.CacheAuto, "task")
|
|
if err != nil || !bytes.Equal(found, before) {
|
|
t.Fatalf("failed replacement changed prior acceptance: %q, %v", found, err)
|
|
}
|
|
if err := store.Store(ctx, mode, "task", after); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
found, err = store.Lookup(ctx, contracts.CacheAuto, "task")
|
|
want := after
|
|
if mode == contracts.CacheBypass {
|
|
want = before
|
|
}
|
|
if err != nil || !bytes.Equal(found, want) {
|
|
t.Fatalf("replacement = %q, %v; want %q", found, err, want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestFilesystemCacheBypassDoesNotTouchStorage(t *testing.T) {
|
|
root := filepath.Join(t.TempDir(), "absent")
|
|
store := newStore(t, root)
|
|
if got, err := store.Lookup(context.Background(), contracts.CacheBypass, "task"); err != nil || got != nil {
|
|
t.Fatalf("bypassed lookup = %q, %v", got, err)
|
|
}
|
|
if err := store.Store(context.Background(), contracts.CacheBypass, "task", []byte(`{"accepted":true}`)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := os.Stat(root); !errors.Is(err, os.ErrNotExist) {
|
|
t.Fatalf("bypass created storage: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestFilesystemCacheMissesReturnUsefulReadDiagnostics(t *testing.T) {
|
|
ctx := context.Background()
|
|
root := t.TempDir()
|
|
store := newStore(t, root)
|
|
if err := os.WriteFile(filepath.Join(root, "malformed.json"), []byte("not JSON"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Mkdir(filepath.Join(root, "directory.json"), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, hash := range []string{"malformed", "directory"} {
|
|
if got, err := store.Lookup(ctx, contracts.CacheAuto, hash); got != nil || err == nil || !strings.Contains(err.Error(), hash) {
|
|
t.Fatalf("%s lookup = %q, %v; want diagnostic miss", hash, got, err)
|
|
}
|
|
}
|
|
blockedRoot := filepath.Join(t.TempDir(), "regular-file")
|
|
if err := os.WriteFile(blockedRoot, []byte("unchanged"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
blocked := newStore(t, blockedRoot)
|
|
if got, err := blocked.Lookup(ctx, contracts.CacheAuto, "task"); got != nil || err == nil {
|
|
t.Fatalf("unreadable root lookup = %q, %v", got, err)
|
|
}
|
|
if err := blocked.Store(ctx, contracts.CacheAuto, "task", []byte("{}")); err == nil {
|
|
t.Fatal("write failure did not return its diagnostic")
|
|
}
|
|
if content, err := os.ReadFile(blockedRoot); err != nil || string(content) != "unchanged" {
|
|
t.Fatalf("failed cache operation changed its root: %q, %v", content, err)
|
|
}
|
|
}
|
|
|
|
func TestFilesystemCacheConfinesPathsAndLeavesLegacyRecordsAlone(t *testing.T) {
|
|
ctx := context.Background()
|
|
root := t.TempDir()
|
|
store := newStore(t, root)
|
|
for _, name := range []string{"checkpoints", "chunk-plans"} {
|
|
if err := os.Mkdir(filepath.Join(root, name), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(root, name, "legacy.json"), []byte("old-format-record"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
for _, hash := range []string{"", "../outside", "nested/entry", "/absolute", "nested\\entry"} {
|
|
if got, err := store.Lookup(ctx, contracts.CacheAuto, hash); got != nil || err == nil {
|
|
t.Fatalf("unsafe lookup %q = %q, %v", hash, got, err)
|
|
}
|
|
if err := store.Store(ctx, contracts.CacheAuto, hash, []byte("{}")); err == nil {
|
|
t.Fatalf("unsafe store %q succeeded", hash)
|
|
}
|
|
}
|
|
if err := store.Store(ctx, contracts.CacheAuto, "new-task", []byte("{}")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, name := range []string{"checkpoints", "chunk-plans"} {
|
|
content, err := os.ReadFile(filepath.Join(root, name, "legacy.json"))
|
|
if err != nil || string(content) != "old-format-record" {
|
|
t.Fatalf("legacy %s changed: %q, %v", name, content, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFilesystemCacheRejectsSymlinkEntryWithoutChangingTarget(t *testing.T) {
|
|
ctx := context.Background()
|
|
root := t.TempDir()
|
|
outside := filepath.Join(t.TempDir(), "outside.json")
|
|
if err := os.WriteFile(outside, []byte(`{"original":true}`), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Symlink(outside, filepath.Join(root, "task.json")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
store := newStore(t, root)
|
|
if got, err := store.Lookup(ctx, contracts.CacheAuto, "task"); got != nil || err == nil {
|
|
t.Fatalf("symlink lookup = %q, %v", got, err)
|
|
}
|
|
if err := store.Store(ctx, contracts.CacheRefresh, "task", []byte("{}")); err == nil {
|
|
t.Fatal("symlink entry was replaced")
|
|
}
|
|
content, err := os.ReadFile(outside)
|
|
if err != nil || string(content) != `{"original":true}` {
|
|
t.Fatalf("symlink target changed: %q, %v", content, err)
|
|
}
|
|
}
|
|
|
|
func TestFilesystemCacheConcurrentReplacementPublishesWholeArtifacts(t *testing.T) {
|
|
store := newStore(t, t.TempDir())
|
|
ctx := context.Background()
|
|
values := [][]byte{[]byte(`{"items":["` + strings.Repeat("a", 8192) + `"]}`), []byte(`{"items":["` + strings.Repeat("b", 8192) + `"]}`)}
|
|
if err := store.Store(ctx, contracts.CacheAuto, "shared", values[0]); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var writers sync.WaitGroup
|
|
for _, content := range values {
|
|
writers.Go(func() {
|
|
for range 20 {
|
|
if err := store.Store(ctx, contracts.CacheAuto, "shared", content); err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
}
|
|
})
|
|
}
|
|
for range 100 {
|
|
got, err := store.Lookup(ctx, contracts.CacheAuto, "shared")
|
|
if err != nil || (!bytes.Equal(got, values[0]) && !bytes.Equal(got, values[1])) {
|
|
t.Errorf("replacement exposed partial content: %d bytes, %v", len(got), err)
|
|
break
|
|
}
|
|
}
|
|
writers.Wait()
|
|
}
|
|
|
|
func TestFilesystemCacheCancellationAndFailedPublicationLeaveNoEntry(t *testing.T) {
|
|
root := filepath.Join(t.TempDir(), "cache")
|
|
store := newStore(t, root)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
if _, err := store.Lookup(ctx, contracts.CacheAuto, "task"); !errors.Is(err, context.Canceled) {
|
|
t.Fatalf("canceled lookup = %v", err)
|
|
}
|
|
if err := store.Store(ctx, contracts.CacheRefresh, "task", []byte("{}")); !errors.Is(err, context.Canceled) {
|
|
t.Fatalf("canceled store = %v", err)
|
|
}
|
|
if _, err := os.Stat(root); !errors.Is(err, os.ErrNotExist) {
|
|
t.Fatalf("canceled cache operation created storage: %v", err)
|
|
}
|
|
if err := os.MkdirAll(filepath.Join(root, "directory.json"), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.Store(context.Background(), contracts.CacheRefresh, "directory", []byte("{}")); err == nil {
|
|
t.Fatal("publication over a directory unexpectedly succeeded")
|
|
}
|
|
entries, err := os.ReadDir(root)
|
|
if err != nil || len(entries) != 1 || entries[0].Name() != "directory.json" {
|
|
t.Fatalf("failed publication left temporary entries: %v, %v", entries, err)
|
|
}
|
|
}
|