agenttoolstest

package
v0.6.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package agenttoolstest is an in-memory fake of the parts of sandboxd the aerolvm CLI and MCP server use: v1 sandboxes (create, get, list by name, start, destroy, ports), the toolbox file and exec endpoints, the exec WebSocket, and sessions. Tests in agenttools, agentmcp and cmd/aerolvm share it so the three layers are checked against the same wire behaviour. It never imports server packages.

Index

Constants

View Source
const DropStream = "\x00drop"

DropStream, returned as the signal from an Exec, makes the fake close the exec stream without an exit message, like a connection lost mid-command.

View Source
const Token = "test-token"

Token is the bearer token the fake accepts.

Variables

This section is empty.

Functions

func Interpret

func Interpret(cmd string, in io.Reader, out func(byte, []byte), killed <-chan struct{}) (int, string)

Interpret is the built-in command language:

echo TEXT        stdout TEXT + newline, exit 0
stderr TEXT      stderr TEXT + newline, exit 0
exit N           exit N
cat              copy stdin to stdout until EOF
yes N            N bytes of "y\n" on stdout
sleep            run until killed, then report signal "killed"
signal NAME      exit by signal NAME
anything else    stdout "ran: CMD" + newline

Types

type Exec

type Exec func(cmd string, in io.Reader, out func(stream byte, b []byte), killed <-chan struct{}) (code int, signal string)

Exec is a scripted command. It writes output with out(stream, bytes) (stream 1 = stdout, 2 = stderr), reads stdin from in (closed at EOF), and returns the exit code and signal. killed is closed when the client sends KILL or the stream drops.

type ExecStart added in v0.6.6

type ExecStart struct {
	Command string            `json:"command"`
	Env     map[string]string `json:"env"`
	TTY     bool              `json:"tty"`
	Cols    int               `json:"cols"`
	Rows    int               `json:"rows"`
}

ExecStart is the first frame of a streaming exec.

type Sandbox

type Sandbox struct {
	models.Sandbox
	Files    map[string][]byte
	Sessions map[string]*Session
}

Sandbox is one fake sandbox with its files and sessions.

type Server

type Server struct {
	*httptest.Server

	// IgnoreNameFilter makes GET /sandboxes ignore ?name=, like a server
	// that predates name lookup (CEO review CF5).
	IgnoreNameFilter bool
	// IgnoreLimit makes GET /sandboxes return every row even when the
	// caller sent ?limit=, like a daemon that predates single-node paging.
	IgnoreLimit bool
	// BadFileInfo makes GET /toolbox/files/info return a body that is
	// not the info JSON, so callers exercise their decode failure path.
	BadFileInfo bool
	// HangAttach accepts the session websocket and then waits for the
	// client to go away, so a follower can be detached instead of
	// observing an immediate exit.
	HangAttach bool
	// AttachReady is closed once a hanging attach has the websocket.
	// Tests wait on it before cancelling the client.
	AttachReady chan struct{}
	// AttachExitSignal is sent on the attach exit message. Empty means
	// the command exited by code alone.
	AttachExitSignal string
	// DropAfterCreate drops the connection this many times right after a
	// create succeeds, so the client never sees the reply (lost reply, D5).
	DropAfterCreate int
	// ExecFunc overrides the built-in command interpreter.
	ExecFunc Exec
	// ExecStartError makes a streamed exec answer its start message with
	// this error frame, as toolboxd does for a command it can't start.
	ExecStartError string
	// BufferedPad adds this many bytes of padding to buffered exec stdout.
	BufferedPad int
	// FailStart makes POST /start fail with 500.
	FailStart bool
	// FailDestroy makes DELETE /sandboxes/{id} fail with 500 after the
	// sandbox was found, so callers can tell a destroy error from a miss.
	FailDestroy bool
	// FailToolbox makes every toolbox route fail with 500.
	FailToolbox bool
	// FailSessions makes session create/get/delete fail with 500.
	FailSessions bool
	// FailExpose makes POST /ports/{port} fail with 500.
	FailExpose bool
	// DropAttach upgrades the session websocket and closes it before an
	// exit message, so Wait returns an error instead of a code.
	DropAttach bool
	// FailAttach makes the session websocket handshake fail with 500.
	FailAttach bool
	// OnAttach runs, without the lock held, once a session attach has its
	// websocket and before DropAttach drops it: a test stops or destroys
	// the sandbox there to see what the client makes of the dropped stream.
	OnAttach func(sandboxID, sessionID string)
	// LiveAttach makes a session attach interactive, like a shell: it
	// replays the log, then runs the exec function on the session's
	// command with the client's stdin. A client "close" or a dropped
	// connection detaches without an exit message, as toolboxd does.
	LiveAttach bool
	// ConflictAlways makes every create fail with 409 without creating
	// anything (a conflict on something other than the name).
	ConflictAlways bool
	// FailCreates makes the next N creates fail with 500.
	FailCreates int
	// CreateDelay holds each create this long, to widen races in tests.
	CreateDelay time.Duration

	Requests       []string
	CreatePosts    int
	CreatedIDs     []string
	StreamDials    int
	Signals        []string
	Resizes        []string
	LastExecStart  ExecStart
	ExecCommands   []string
	SessionCreates []models.CreateSessionRequest
	Detaches       int
	BufferedExecs  int
	LastCreate     models.CreateSandboxRequest
	StartCalls     int
	DestroyCalls   int
	ListQueries    []string
	UploadedBodies int
	// contains filtered or unexported fields
}

Server is the fake sandboxd. Fields after mu are guarded by it; read them through Snapshot-style helpers or after the client calls return.

func New

func New(t testing.TB) *Server

New starts a fake sandboxd that the test closes on cleanup.

func (*Server) AddSandbox

func (s *Server) AddSandbox(sb models.Sandbox) *Sandbox

AddSandbox registers a sandbox. A zero ID gets a generated one.

func (*Server) AddSession added in v0.6.6

func (s *Server) AddSession(sandboxID string, session models.Session)

AddSession registers a session in a fake sandbox, as one started by another client (an SSH login, an earlier `aerolvm shell`) would be.

func (*Server) AppendSessionLog

func (s *Server) AppendSessionLog(sandboxID, sessionID string, b []byte)

AppendSessionLog adds output to a fake session's log.

func (*Server) Count

func (s *Server) Count() int

Count is the number of live sandboxes.

func (*Server) File

func (s *Server) File(id, path string) ([]byte, bool)

File returns a file's content from a fake sandbox.

func (*Server) Observe

func (s *Server) Observe(fn func(*Server))

Observe runs fn with the server locked, for reading observation fields.

func (*Server) PutFile

func (s *Server) PutFile(id, path string, data []byte)

PutFile stores a file in a fake sandbox.

func (*Server) Remove

func (s *Server) Remove(id string)

Remove deletes a sandbox, as an idle lifecycle destroy would.

func (*Server) Sandbox

func (s *Server) Sandbox(id string) (models.Sandbox, bool)

Sandbox returns a copy of the fake sandbox's row.

func (*Server) SetStatus

func (s *Server) SetStatus(id string, status models.SandboxStatus)

SetStatus changes a sandbox's status (e.g. stopped by its idle timer).

type Session

type Session struct {
	models.Session
	Log []byte
}

Session is one fake background process.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL