128 lines
4.1 KiB
Go
128 lines
4.1 KiB
Go
package workspace
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/core/config"
|
|
)
|
|
|
|
func TestFromConfigBuildsWorkspaceRoots(t *testing.T) {
|
|
cfg := config.Default()
|
|
cfg.Workspace.Directory = "/var/lib/notarius"
|
|
cfg.Workspace.Resume.Enabled = true
|
|
cfg.Workspace.Debug.Enabled = true
|
|
cfg.RecomputeEffectiveDiagnostics()
|
|
|
|
settings := FromConfig(cfg)
|
|
|
|
if settings.RootDir != "/var/lib/notarius" {
|
|
t.Fatalf("RootDir = %q, want /var/lib/notarius", settings.RootDir)
|
|
}
|
|
if settings.DiagnosticsRoot != "/var/lib/notarius/diagnostics" || !settings.DiagnosticsEnabled {
|
|
t.Fatalf("diagnostics settings = %+v, want workspace diagnostics root enabled", settings)
|
|
}
|
|
if settings.CheckpointsRoot != "/var/lib/notarius/checkpoints" || !settings.ResumeEnabled {
|
|
t.Fatalf("checkpoint settings = %+v, want workspace checkpoints root enabled", settings)
|
|
}
|
|
if settings.DebugRoot != "/var/lib/notarius/debug" || !settings.DebugEnabled {
|
|
t.Fatalf("debug settings = %+v, want workspace debug root enabled", settings)
|
|
}
|
|
}
|
|
|
|
func TestFromConfigKeepsLegacyDiagnosticsRootWithoutWorkspaceRoot(t *testing.T) {
|
|
cfg := config.Default()
|
|
cfg.Diagnostics.WorkDir = "/tmp/notarius-legacy"
|
|
cfg.Workspace.Resume.Enabled = true
|
|
cfg.Workspace.Debug.Enabled = true
|
|
|
|
settings := FromConfig(cfg)
|
|
|
|
if settings.RootDir != "" {
|
|
t.Fatalf("RootDir = %q, want empty", settings.RootDir)
|
|
}
|
|
if settings.DiagnosticsRoot != "/tmp/notarius-legacy" || !settings.DiagnosticsEnabled {
|
|
t.Fatalf("diagnostics settings = %+v, want legacy diagnostics root enabled", settings)
|
|
}
|
|
if settings.CheckpointsRoot != "" || settings.ResumeEnabled {
|
|
t.Fatalf("checkpoint settings = %+v, want disabled empty root", settings)
|
|
}
|
|
if settings.DebugRoot != "" || settings.DebugEnabled {
|
|
t.Fatalf("debug settings = %+v, want disabled empty root", settings)
|
|
}
|
|
}
|
|
|
|
func TestPathConstructors(t *testing.T) {
|
|
root := t.TempDir()
|
|
settings := Settings{
|
|
RootDir: root,
|
|
DiagnosticsRoot: filepath.Join(root, "diagnostics"),
|
|
CheckpointsRoot: filepath.Join(root, "checkpoints"),
|
|
DebugRoot: filepath.Join(root, "debug"),
|
|
DiagnosticsEnabled: true,
|
|
ResumeEnabled: true,
|
|
DebugEnabled: true,
|
|
}
|
|
|
|
diagnosticsDir, err := settings.DiagnosticsRunDirectory("run-123")
|
|
if err != nil {
|
|
t.Fatalf("DiagnosticsRunDirectory: %v", err)
|
|
}
|
|
if diagnosticsDir != filepath.Join(root, "diagnostics", "run-123") {
|
|
t.Fatalf("diagnostics dir = %q", diagnosticsDir)
|
|
}
|
|
|
|
checkpointDir, err := settings.CheckpointIdentityDirectory("pipeline/input-digest/pipeline-digest")
|
|
if err != nil {
|
|
t.Fatalf("CheckpointIdentityDirectory: %v", err)
|
|
}
|
|
if checkpointDir != filepath.Join(root, "checkpoints", "pipeline", "input-digest", "pipeline-digest") {
|
|
t.Fatalf("checkpoint dir = %q", checkpointDir)
|
|
}
|
|
|
|
debugDir, err := settings.DebugRunDirectory("run-456")
|
|
if err != nil {
|
|
t.Fatalf("DebugRunDirectory: %v", err)
|
|
}
|
|
if debugDir != filepath.Join(root, "debug", "run-456") {
|
|
t.Fatalf("debug dir = %q", debugDir)
|
|
}
|
|
}
|
|
|
|
func TestDisabledPathConstructorsReturnEmptyPaths(t *testing.T) {
|
|
settings := Settings{}
|
|
|
|
for name, call := range map[string]func() (string, error){
|
|
"diagnostics": func() (string, error) { return settings.DiagnosticsRunDirectory("run-1") },
|
|
"checkpoint": func() (string, error) { return settings.CheckpointIdentityDirectory("identity") },
|
|
"debug": func() (string, error) { return settings.DebugRunDirectory("run-1") },
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
got, err := call()
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if got != "" {
|
|
t.Fatalf("path = %q, want empty", got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestRunDirectoryConstructorsRejectNestedNames(t *testing.T) {
|
|
root := t.TempDir()
|
|
settings := Settings{
|
|
DiagnosticsRoot: filepath.Join(root, "diagnostics"),
|
|
DebugRoot: filepath.Join(root, "debug"),
|
|
DiagnosticsEnabled: true,
|
|
DebugEnabled: true,
|
|
}
|
|
|
|
if got, err := settings.DiagnosticsRunDirectory("run-1/nested"); err == nil {
|
|
t.Fatalf("DiagnosticsRunDirectory returned %q, want error", got)
|
|
}
|
|
if got, err := settings.DebugRunDirectory("run-1/nested"); err == nil {
|
|
t.Fatalf("DebugRunDirectory returned %q, want error", got)
|
|
}
|
|
}
|