banger/internal/daemon/workspace_rejection_test.go
Thales Maciel 88bc466d58
tests: targeted coverage for doctor, workspace rejections, and nat capability
Three thematic test files pinning behavior surfaces that had none
before, following the review's recommendation to plug concrete
error/cleanup branches rather than chase a coverage percentage.

doctor_test.go
  Covers Daemon.doctorReport end-to-end with a permissive runner +
  fake executables on PATH. Pins: store error surfaces as fail,
  store success as pass, missing firecracker kills the host-runtime
  check, the three default capability feature checks (work disk,
  vm dns, nat) are emitted, vm-defaults is always-pass with
  provenance. Previously 0% — now the Doctor() command's contract
  with the CLI is under guard.

workspace_rejection_test.go
  Covers the four early-exit branches of PrepareVMWorkspace that
  the existing happy-path + lock-release tests never hit: malformed
  mode, --from without --branch, VM not running, VM not found.
  Each one returns before any SSH I/O, so the fake-firecracker
  infra the happy-path test needs is unnecessary — a bare wired
  daemon with a stored VMRecord suffices.

nat_capability_test.go
  Covers natCapability.ApplyConfigChange (unchanged flag → no-op,
  VM not alive → no-op, toggle on live VM → runner reached) and
  natCapability.Cleanup (NAT disabled → no-op, runtime handles
  missing → defensive no-op, full wiring → ensureNAT(false)). A
  countingRunner + startFakeFirecracker fixture stands in for the
  real host plumbing, with waitForVMAlive polling past the
  exec -a race window that startFakeFirecracker exposes on
  loaded CI boxes.

make coverage-total 37.8% → 38.6%. The number isn't the point —
these tests exist so the next refactor in this area has to
break an explicit assertion to drift.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 12:58:12 -03:00

87 lines
2.9 KiB
Go

package daemon
import (
"context"
"io"
"log/slog"
"path/filepath"
"strings"
"testing"
"banger/internal/api"
"banger/internal/model"
)
// newWorkspaceRejectionDaemon returns a running-VM + wired daemon
// suitable for the PrepareVMWorkspace rejection tests. No real guest
// state — rejection paths return before any SSH I/O, so the fake
// firecracker infra the happy-path tests need is unnecessary here.
func newWorkspaceRejectionDaemon(t *testing.T) (*Daemon, model.VMRecord) {
t.Helper()
vm := testVM("rejectbox", "image-reject", "172.16.0.211")
vm.State = model.VMStateRunning
vm.Runtime.State = model.VMStateRunning
d := &Daemon{
store: openDaemonStore(t),
config: model.DaemonConfig{SSHKeyPath: filepath.Join(t.TempDir(), "id_ed25519")},
logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
}
wireServices(d)
upsertDaemonVM(t, context.Background(), d.store, vm)
// Handle cache entry with a live-looking PID so vmAlive returns
// true for the "VM is running" path; the rejection tests that want
// the not-running branch clear this override explicitly.
d.vm.setVMHandlesInMemory(vm.ID, model.VMHandles{PID: 1}) // init is always alive
return d, vm
}
func TestPrepareVMWorkspace_RejectsMalformedMode(t *testing.T) {
d, vm := newWorkspaceRejectionDaemon(t)
_, err := d.ws.PrepareVMWorkspace(context.Background(), api.VMWorkspacePrepareParams{
IDOrName: vm.Name,
SourcePath: "/tmp/fake",
Mode: "bogus_mode",
})
if err == nil || !strings.Contains(err.Error(), "unsupported workspace mode") {
t.Fatalf("err = %v, want unsupported-mode rejection", err)
}
}
func TestPrepareVMWorkspace_RejectsFromWithoutBranch(t *testing.T) {
d, vm := newWorkspaceRejectionDaemon(t)
_, err := d.ws.PrepareVMWorkspace(context.Background(), api.VMWorkspacePrepareParams{
IDOrName: vm.Name,
SourcePath: "/tmp/fake",
From: "HEAD",
// Branch deliberately left empty.
})
if err == nil || !strings.Contains(err.Error(), "workspace from requires branch") {
t.Fatalf("err = %v, want from-without-branch rejection", err)
}
}
func TestPrepareVMWorkspace_RejectsNotRunningVM(t *testing.T) {
d, vm := newWorkspaceRejectionDaemon(t)
// Clear handles so vmAlive returns false — simulates a VM that's
// been stopped or never booted.
d.vm.clearVMHandles(vm)
_, err := d.ws.PrepareVMWorkspace(context.Background(), api.VMWorkspacePrepareParams{
IDOrName: vm.Name,
SourcePath: "/tmp/fake",
})
if err == nil || !strings.Contains(err.Error(), "is not running") {
t.Fatalf("err = %v, want not-running rejection", err)
}
}
func TestPrepareVMWorkspace_RejectsUnknownVM(t *testing.T) {
d, _ := newWorkspaceRejectionDaemon(t)
_, err := d.ws.PrepareVMWorkspace(context.Background(), api.VMWorkspacePrepareParams{
IDOrName: "ghost-vm",
SourcePath: "/tmp/fake",
})
if err == nil || !strings.Contains(err.Error(), "not found") {
t.Fatalf("err = %v, want VM-not-found rejection", err)
}
}