test(metis): cover command fatal edges

This commit is contained in:
codex 2026-04-21 05:18:30 -03:00
parent db2ee619be
commit 8c51716780
2 changed files with 68 additions and 0 deletions

View File

@ -127,16 +127,75 @@ func TestMainAndCommandFatalBranches(t *testing.T) {
main()
})
mustPanic(func() { configCmd(nil) })
root := t.TempDir()
invPath, _ := writeTestInventory(t, root)
mustPanic(func() { configCmd([]string{"--inventory", invPath, "--node", "missing"}) })
mustPanic(func() { planCmd([]string{"--inventory", "/nope", "--node", "titan-15"}) })
mustPanic(func() { burnCmd([]string{"--inventory", "/nope", "--node", "titan-15", "--device", "/dev/sdz"}) })
mustPanic(func() { imageCmd(nil) })
mustPanic(func() {
imageCmd([]string{"--inventory", invPath, "--node", "titan-15", "--cache", filepath.Join(root, "cache")})
})
mustPanic(func() { injectCmd(nil) })
mustPanic(func() { injectCmd([]string{"--node", "titan-15"}) })
mustPanic(func() {
injectCmd([]string{"--inventory", invPath, "--node", "missing", "--boot", filepath.Join(root, "boot")})
})
mustPanic(func() { factsCmd([]string{"--inventory", "/nope", "--snapshots", "/nope"}) })
mustPanic(func() {
factsCmd([]string{"--inventory", invPath, "--snapshots", filepath.Join(root, "missing-snaps")})
})
mustPanic(func() { serveCmd([]string{"--bind", ":0"}) })
t.Setenv("METIS_INVENTORY_PATH", invPath)
t.Setenv("METIS_DATA_DIR", filepath.Join(root, "data"))
listenAndServe = func(string, http.Handler) error { return http.ErrServerClosed }
t.Cleanup(func() { listenAndServe = httpListenAndServe })
mustPanic(func() { serveCmd([]string{"--bind", ":0"}) })
mustPanic(func() { remoteBuildCmd([]string{"--node", "n1"}) })
mustPanic(func() { remoteFlashCmd([]string{"--node", "n1"}) })
}
func TestFactsCmdReportsEncodeErrors(t *testing.T) {
root := t.TempDir()
invPath, _ := writeTestInventory(t, root)
snapDir := filepath.Join(root, "snapshots")
if err := os.MkdirAll(snapDir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(snapDir, "snap.json"), []byte(`{"hostname":"titan-15","kernel":"6.6.63"}`), 0o644); err != nil {
t.Fatal(err)
}
oldStdout := os.Stdout
oldStderr := os.Stderr
badStdout, stdoutWriter, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
stderrReader, stderrWriter, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
os.Stdout = badStdout
os.Stderr = stderrWriter
t.Cleanup(func() {
os.Stdout = oldStdout
os.Stderr = oldStderr
_ = badStdout.Close()
_ = stdoutWriter.Close()
_ = stderrReader.Close()
_ = stderrWriter.Close()
})
factsCmd([]string{"--inventory", invPath, "--snapshots", snapDir})
_ = stderrWriter.Close()
buf := make([]byte, 1024)
n, _ := stderrReader.Read(buf)
if !strings.Contains(string(buf[:n]), "encode:") {
t.Fatalf("expected encode error on stderr, got %q", string(buf[:n]))
}
}
func TestRemoteCommandHelpers(t *testing.T) {
if !hasMountedChildren([]struct {
Mountpoint string `json:"mountpoint"`

View File

@ -33,4 +33,13 @@ func TestInjectorWriteBranches(t *testing.T) {
if err := inj.Write([]FileSpec{{Path: "x", Content: []byte("x")}}); err == nil {
t.Fatal("expected mkdir error")
}
targetDir := filepath.Join(boot, "already-dir")
if err := os.MkdirAll(targetDir, 0o755); err != nil {
t.Fatal(err)
}
inj = &Injector{BootPath: boot}
if err := inj.Write([]FileSpec{{Path: "already-dir", Content: []byte("x"), Mode: 0o644}}); err == nil {
t.Fatal("expected write error when target path is a directory")
}
}