Cuts the daemon-managed guest-session machinery (start/list/show/
logs/stop/kill/attach/send). The feature shipped aimed at agent-
orchestration workflows (programmatic stdin piping into a long-lived
guest process) that aren't driving any concrete user today, and the
~2.3K LOC of daemon surface area — attach bridge, FIFO keepalive,
controller registry, sessionstream framing, SQLite persistence — was
locking in an API we'd have to keep through v0.1.0.
Anything session-flavoured that people actually need today can be
done with `vm ssh + tmux` or `vm run -- cmd`.
Deleted:
- internal/cli/commands_vm_session.go
- internal/daemon/{guest_sessions,session_lifecycle,session_attach,session_stream,session_controller}.go
- internal/daemon/session/ (guest-session helpers package)
- internal/sessionstream/ (framing package)
- internal/daemon/guest_sessions_test.go
- internal/store/guest_session_test.go
- GuestSession* types from internal/{api,model}
- Store UpsertGuestSession/GetGuestSession/ListGuestSessionsByVM/DeleteGuestSession + scanner helpers
- guest.session.* RPC dispatch entries
- 5 CLI session tests, 2 completion tests, 2 printer tests
Extracted:
- ShellQuote + FormatStepError lifted to internal/daemon/workspace/util.go
(only non-session consumer); workspace package now self-contained
- internal/daemon/guest_ssh.go keeps guestSSHClient + dialGuest +
waitForGuestSSH — still used by workspace prepare/export
- internal/daemon/fake_firecracker_test.go preserves the test helper
that used to live in guest_sessions_test.go
Store schema: CREATE TABLE guest_sessions and its column migrations
removed. Existing dev DBs keep an orphan table (harmless, pre-v0.1.0).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
144 lines
3.8 KiB
Go
144 lines
3.8 KiB
Go
package daemon
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"log/slog"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"banger/internal/model"
|
|
"banger/internal/vmdns"
|
|
)
|
|
|
|
// TestCloseOnPartiallyInitialisedDaemon pins the contract that Open's
|
|
// error-path defer relies on: Close must be safe to call when a
|
|
// startup step failed before every subsystem was set up. If this
|
|
// breaks, `defer d.Close() on err != nil` in Open() starts panicking
|
|
// on zero-valued fields.
|
|
func TestCloseOnPartiallyInitialisedDaemon(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
build func(t *testing.T) *Daemon
|
|
verify func(t *testing.T, d *Daemon)
|
|
}{
|
|
{
|
|
name: "only store + closing channel (early failure)",
|
|
build: func(t *testing.T) *Daemon {
|
|
return &Daemon{
|
|
store: openDaemonStore(t),
|
|
closing: make(chan struct{}),
|
|
logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
|
|
}
|
|
},
|
|
verify: func(t *testing.T, d *Daemon) {
|
|
// closing channel should have been closed.
|
|
select {
|
|
case <-d.closing:
|
|
default:
|
|
t.Error("closing channel not closed by Close")
|
|
}
|
|
},
|
|
},
|
|
{
|
|
name: "with vmDNS listener (fail after startVMDNS)",
|
|
build: func(t *testing.T) *Daemon {
|
|
server, err := vmdns.New("127.0.0.1:0", nil)
|
|
if err != nil {
|
|
t.Fatalf("vmdns.New: %v", err)
|
|
}
|
|
return &Daemon{
|
|
store: openDaemonStore(t),
|
|
closing: make(chan struct{}),
|
|
vmDNS: server,
|
|
logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
|
|
}
|
|
},
|
|
verify: func(t *testing.T, d *Daemon) {
|
|
if d.vmDNS != nil {
|
|
t.Error("vmDNS not cleared by Close")
|
|
}
|
|
},
|
|
},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
d := tc.build(t)
|
|
if err := d.Close(); err != nil {
|
|
t.Fatalf("Close returned error: %v", err)
|
|
}
|
|
tc.verify(t, d)
|
|
|
|
// Second Close must be a no-op (sync.Once) — must not
|
|
// panic on channel or re-close.
|
|
if err := d.Close(); err != nil {
|
|
t.Fatalf("second Close error: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestCloseIdempotentUnderConcurrency catches regressions of the
|
|
// sync.Once guard that makes repeated Close calls safe. The open-
|
|
// failure defer relies on this: if the user cancels before Open
|
|
// returns and also calls Close afterwards, both paths must survive.
|
|
func TestCloseIdempotentUnderConcurrency(t *testing.T) {
|
|
d := &Daemon{
|
|
store: openDaemonStore(t),
|
|
closing: make(chan struct{}),
|
|
logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
|
|
config: model.DaemonConfig{BridgeName: ""},
|
|
}
|
|
|
|
var count atomic.Int32
|
|
done := make(chan struct{})
|
|
for i := 0; i < 5; i++ {
|
|
go func() {
|
|
if err := d.Close(); err != nil {
|
|
t.Errorf("Close error: %v", err)
|
|
}
|
|
count.Add(1)
|
|
if count.Load() == 5 {
|
|
close(done)
|
|
}
|
|
}()
|
|
}
|
|
<-done
|
|
|
|
// Channel must be closed exactly once (sync.Once covers the
|
|
// inner close(d.closing)). Reading from a closed channel is
|
|
// non-blocking; panicking here would mean the channel wasn't
|
|
// closed or was double-closed (close panics are uncatchable).
|
|
select {
|
|
case <-d.closing:
|
|
default:
|
|
t.Fatal("closing channel not closed after concurrent Close calls")
|
|
}
|
|
}
|
|
|
|
// TestOpenFailureRunsCloseCleanup is a structural check: confirms
|
|
// the deferred rollback in Open actually fires. Can't easily run
|
|
// Open() end-to-end (hits paths.Resolve + sudo), but we can simulate
|
|
// the pattern by threading a named-return err through the same
|
|
// defer and asserting Close runs.
|
|
func TestOpenFailureRunsCloseCleanup(t *testing.T) {
|
|
closed := false
|
|
fakeClose := func() { closed = true }
|
|
|
|
runOpen := func() (err error) {
|
|
defer func() {
|
|
if err != nil {
|
|
fakeClose()
|
|
}
|
|
}()
|
|
err = errors.New("simulated late-stage startup failure")
|
|
return err
|
|
}
|
|
|
|
if err := runOpen(); err == nil {
|
|
t.Fatal("expected simulated error")
|
|
}
|
|
if !closed {
|
|
t.Fatal("deferred cleanup did not fire on err != nil")
|
|
}
|
|
}
|