Add chunk plan cache configuration and storage
This commit is contained in:
22
internal/core/workspace/chunk_plan.go
Normal file
22
internal/core/workspace/chunk_plan.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package workspace
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func DefaultChunkPlanRoot(userCacheDir func() (string, error)) (string, error) {
|
||||
if userCacheDir == nil {
|
||||
return "", fmt.Errorf("user cache directory resolver must not be nil")
|
||||
}
|
||||
root, err := userCacheDir()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("resolve user cache directory: %w", err)
|
||||
}
|
||||
root = strings.TrimSpace(root)
|
||||
if root == "" {
|
||||
return "", fmt.Errorf("user cache directory must not be empty")
|
||||
}
|
||||
return filepath.Join(filepath.Clean(root), "notarius", "chunk-plans"), nil
|
||||
}
|
||||
57
internal/core/workspace/chunk_plan_test.go
Normal file
57
internal/core/workspace/chunk_plan_test.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package workspace
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/config"
|
||||
)
|
||||
|
||||
func TestDefaultChunkPlanRoot(t *testing.T) {
|
||||
got, err := DefaultChunkPlanRoot(func() (string, error) { return "/cache/user", nil })
|
||||
if err != nil {
|
||||
t.Fatalf("DefaultChunkPlanRoot() error = %v", err)
|
||||
}
|
||||
if want := filepath.Join("/cache/user", "notarius", "chunk-plans"); got != want {
|
||||
t.Fatalf("root = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultChunkPlanRootRejectsResolverFailures(t *testing.T) {
|
||||
boom := errors.New("resolver failed")
|
||||
if _, err := DefaultChunkPlanRoot(func() (string, error) { return "", boom }); !errors.Is(err, boom) {
|
||||
t.Fatalf("resolver error = %v", err)
|
||||
}
|
||||
if _, err := DefaultChunkPlanRoot(func() (string, error) { return " \t ", nil }); err == nil || !strings.Contains(err.Error(), "empty") {
|
||||
t.Fatalf("empty result error = %v", err)
|
||||
}
|
||||
if _, err := DefaultChunkPlanRoot(nil); err == nil {
|
||||
t.Fatal("nil resolver error = nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestChunkPlanDirectoryIsIndependentFromWorkspaceSettings(t *testing.T) {
|
||||
base := config.Default()
|
||||
base.Workspace.Directory = "/workspace/one"
|
||||
base.Workspace.ChunkCache.Directory = "/cache/plans"
|
||||
first := FromConfig(base)
|
||||
|
||||
changedWorkspace := base
|
||||
changedWorkspace.Workspace.Directory = "/workspace/two"
|
||||
second := FromConfig(changedWorkspace)
|
||||
if base.Workspace.ChunkCache.Directory != changedWorkspace.Workspace.ChunkCache.Directory {
|
||||
t.Fatal("workspace directory changed chunk plan directory")
|
||||
}
|
||||
if first.RootDir == second.RootDir || first.CheckpointsRoot == second.CheckpointsRoot || first.DebugRoot == second.DebugRoot {
|
||||
t.Fatalf("workspace settings did not follow workspace directory: %#v %#v", first, second)
|
||||
}
|
||||
|
||||
changedCache := base
|
||||
changedCache.Workspace.ChunkCache.Directory = "/cache/other"
|
||||
third := FromConfig(changedCache)
|
||||
if first != third {
|
||||
t.Fatalf("chunk plan directory changed workspace settings: %#v %#v", first, third)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user