Files
orca/internal/paths/paths_test.go
T
Jon Chery 437aab39b4 feat(P0a1): multi-namespace path resolver + config demotion + known_hosts flock + CA migration spec (v0.9 P0a1)
P0a1 — Re-architecture Foundation (path resolver + config demotion).

Path resolver (REQ-070, R-002):
- internal/paths/paths.go: 23 functions for the multi-namespace layout
  (Root/ClusterDir/NamespaceDir/NS*/DefaultNamespace/CA/MasterKey/CacheDB/
  Txn/Peers/KnownHosts/SSH/Server/Config). Honors $ORCA_HOME. 100% coverage.
- internal/certpaths/certpaths.go: refactored as thin shim delegating to
  paths, preserving the v0.8 flat-layout API for backward compat during
  the dual-write window (REQ-090). Package doc explains the v0.10-P14
  migration plan. certpaths deleted after v0.10-P14. 100% coverage.

Config demotion (REQ-069, R-014):
- internal/config/markdown.go: minimal hand-rolled YAML frontmatter parser
  (no new dep — yaml.v3 not in go.mod). Returns same *Config struct as HCL.
- internal/config/config.go: renamed Load body to LoadHCL (// Deprecated
  per R-013), added dispatcher Load() routing on extension (.hcl->HCL,
  .md->Markdown, .yaml->Markdown). Signature preserved so root.go unchanged.
- dispatch_test.go + markdown_test.go: 89.8% coverage on config package.

Known_hosts flock (REQ-063, deferred P1 from REVIEW_v0.8 A2):
- internal/security/flock.go: stdlib syscall.Flock advisory lock helper.
- internal/proxmox/bootstrap.go: TOFUHostKeyCallback capture + ResetHostKey
  both acquire the flock before read-modify-write on known_hosts. Prevents
  concurrent writers under v0.9 parallel SSH fan-out. 3 flock tests.

CA migration spec (grill C-07):
- .ciagent/CA_MIGRATION_SPEC_v0.9.md: Option A (preserve trust root,
  RECOMMENDED) vs Option B (forced re-bootstrap). Pre-flight checks,
  migration steps, rollback, post-migration invariants, spike plan.

Verification: build pass, 17/17 Go packages pass, 20/20 bats pass, gofmt
clean, go vet clean, verify-reqs 90 consistent. Coverage: paths 100%,
certpaths 100%, config 89.8%, emit covered.

---ci---
project: orca
phase: P0a1
milestone: v0.9
status: execute
---/ci---
2026-08-05 16:38:26 +00:00

210 lines
6.0 KiB
Go

package paths
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestRoot_HonorsORCAHOME(t *testing.T) {
dir := t.TempDir()
t.Setenv("ORCA_HOME", dir)
if got, want := Root(), dir; got != want {
t.Errorf("Root() = %q, want %q", got, want)
}
}
func TestRoot_EmptyORCAHOMEFallsBack(t *testing.T) {
t.Setenv("ORCA_HOME", "")
home, err := os.UserHomeDir()
if err != nil {
t.Skipf("os.UserHomeDir: %v", err)
}
want := filepath.Join(home, defaultHomeSubdir)
if got := Root(); got != want {
t.Errorf("Root() with empty ORCA_HOME = %q, want %q", got, want)
}
}
func TestRoot_UnsetORCAHOMEFallsBack(t *testing.T) {
os.Unsetenv("ORCA_HOME")
home, err := os.UserHomeDir()
if err != nil {
t.Skipf("os.UserHomeDir: %v", err)
}
want := filepath.Join(home, defaultHomeSubdir)
got := Root()
if got != want {
t.Errorf("Root() default = %q, want %q", got, want)
}
if !strings.HasPrefix(got, home) {
t.Errorf("Root() default %q does not start with home %q", got, home)
}
}
func TestRoot_RelativeORCAHOME(t *testing.T) {
t.Setenv("ORCA_HOME", "relative/orca/home")
if got, want := Root(), "relative/orca/home"; got != want {
t.Errorf("Root() relative = %q, want %q", got, want)
}
}
func TestDefaultNamespace(t *testing.T) {
if got, want := DefaultNamespace(), "_defaults"; got != want {
t.Errorf("DefaultNamespace() = %q, want %q", got, want)
}
}
func TestClusterDir(t *testing.T) {
dir := t.TempDir()
t.Setenv("ORCA_HOME", dir)
got := ClusterDir()
want := filepath.Join(dir, "cluster")
if got != want {
t.Errorf("ClusterDir() = %q, want %q", got, want)
}
if !strings.HasPrefix(got, Root()+string(filepath.Separator)) {
t.Errorf("ClusterDir() %q not under Root() %q", got, Root())
}
}
func TestNamespaceDir(t *testing.T) {
dir := t.TempDir()
t.Setenv("ORCA_HOME", dir)
ns := "prod"
got := NamespaceDir(ns)
want := filepath.Join(dir, ns)
if got != want {
t.Errorf("NamespaceDir(%q) = %q, want %q", ns, got, want)
}
if !strings.HasPrefix(got, Root()+string(filepath.Separator)) {
t.Errorf("NamespaceDir() %q not under Root() %q", got, Root())
}
}
func TestNamespacePaths(t *testing.T) {
dir := t.TempDir()
t.Setenv("ORCA_HOME", dir)
ns := "prod"
nsDir := NamespaceDir(ns)
cases := []struct {
name string
got string
want string
}{
{"NSDb", NSDb(ns), filepath.Join(nsDir, "db", "orca.db")},
{"NSEnv", NSEnv(ns), filepath.Join(nsDir, ".env")},
{"NSSecrets", NSSecrets(ns), filepath.Join(nsDir, ".env.secrets")},
{"NSJobs", NSJobs(ns), filepath.Join(nsDir, "jobs")},
{"NSAlloc", NSAlloc(ns), filepath.Join(nsDir, "alloc")},
{"NSMd", NSMd(ns), filepath.Join(nsDir, "ns.md")},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if tc.got != tc.want {
t.Errorf("%s(%q) = %q, want %q", tc.name, ns, tc.got, tc.want)
}
if !strings.HasPrefix(tc.got, nsDir+string(filepath.Separator)) {
t.Errorf("%s() %q not under NamespaceDir() %q", tc.name, tc.got, nsDir)
}
})
}
}
func TestDefaultNamespacePaths(t *testing.T) {
dir := t.TempDir()
t.Setenv("ORCA_HOME", dir)
ns := DefaultNamespace()
nsDir := NamespaceDir(ns)
if got, want := NSDb(ns), filepath.Join(nsDir, "db", "orca.db"); got != want {
t.Errorf("NSDb(_defaults) = %q, want %q", got, want)
}
if got, want := NSEnv(ns), filepath.Join(nsDir, ".env"); got != want {
t.Errorf("NSEnv(_defaults) = %q, want %q", got, want)
}
}
func TestClusterPaths(t *testing.T) {
dir := t.TempDir()
t.Setenv("ORCA_HOME", dir)
cDir := ClusterDir()
cases := []struct {
name string
got string
want string
}{
{"CACertPath", CACertPath(), filepath.Join(cDir, "ca.crt")},
{"CAKeyPath", CAKeyPath(), filepath.Join(cDir, "ca.key")},
{"MasterKeyPath", MasterKeyPath(), filepath.Join(cDir, "master.key")},
{"KnownHostsPath", KnownHostsPath(), filepath.Join(cDir, "known_hosts")},
{"SSHKeyPath", SSHKeyPath(), filepath.Join(cDir, "orca_ssh_key")},
{"SSHPubPath", SSHPubPath(), filepath.Join(cDir, "orca_ssh_key.pub")},
{"ServerCertPath", ServerCertPath(), filepath.Join(cDir, "server.crt")},
{"ServerKeyPath", ServerKeyPath(), filepath.Join(cDir, "server.key")},
{"ConfigPath", ConfigPath(), filepath.Join(cDir, "config.md")},
{"LegacyHCLConfigPath", LegacyHCLConfigPath(), filepath.Join(cDir, "config.hcl")},
{"TxnDir", TxnDir(), filepath.Join(cDir, "txns")},
{"PeersDir", PeersDir(), filepath.Join(cDir, "peers")},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if tc.got != tc.want {
t.Errorf("%s() = %q, want %q", tc.name, tc.got, tc.want)
}
if !strings.HasPrefix(tc.got, cDir+string(filepath.Separator)) {
t.Errorf("%s() %q not under ClusterDir() %q", tc.name, tc.got, cDir)
}
})
}
}
func TestPeerDir(t *testing.T) {
dir := t.TempDir()
t.Setenv("ORCA_HOME", dir)
host := "node1.example.com"
got := PeerDir(host)
want := filepath.Join(PeersDir(), host)
if got != want {
t.Errorf("PeerDir(%q) = %q, want %q", host, got, want)
}
if !strings.HasPrefix(got, PeersDir()+string(filepath.Separator)) {
t.Errorf("PeerDir() %q not under PeersDir() %q", got, PeersDir())
}
}
func TestCacheDB(t *testing.T) {
dir := t.TempDir()
t.Setenv("ORCA_HOME", dir)
got := CacheDB()
want := filepath.Join(dir, "orca_cache.db")
if got != want {
t.Errorf("CacheDB() = %q, want %q", got, want)
}
if !strings.HasPrefix(got, Root()+string(filepath.Separator)) {
t.Errorf("CacheDB() %q not under Root() %q", got, Root())
}
}
func TestPathSeparatorsOSAppropriate(t *testing.T) {
dir := t.TempDir()
t.Setenv("ORCA_HOME", dir)
// Every returned path must use the OS separator (filepath.Join).
sep := string(filepath.Separator)
for _, p := range []string{
ClusterDir(), NamespaceDir("ns"), NSDb("ns"), NSEnv("ns"),
NSSecrets("ns"), NSJobs("ns"), NSAlloc("ns"), NSMd("ns"),
CACertPath(), CAKeyPath(), MasterKeyPath(), CacheDB(),
TxnDir(), PeersDir(), PeerDir("h"), KnownHostsPath(),
SSHKeyPath(), SSHPubPath(), ServerCertPath(), ServerKeyPath(),
ConfigPath(), LegacyHCLConfigPath(),
} {
if !strings.Contains(p, sep) {
t.Errorf("path %q lacks OS separator %q (not joined?)", p, sep)
}
}
}