Documentation
¶
Overview ¶
Package exectest exercises exec managers with pipe-backed terminals and synchronized output recording. Harnesses register cleanup with their owning test.
Index ¶
- func FailingTTYHandler(err error) exec.TTYHandlerFunc
- func FakeTerminalFactory(ft *FakeTerminal) exec.TerminalFactory
- func NewFakeTerminalFactory() exec.TerminalFactory
- func NoopTTYHandler() exec.TTYHandlerFunc
- func StopChTTYHandler(stopErr error, delay time.Duration) exec.TTYHandlerFunc
- type FakeTerminal
- func (t *FakeTerminal) Close() error
- func (t *FakeTerminal) InjectOutput(data []byte) (int, error)
- func (t *FakeTerminal) MarkEnd(mark []byte) error
- func (t *FakeTerminal) MasterFd() *os.File
- func (t *FakeTerminal) Resize(rows, cols uint16) error
- func (t *FakeTerminal) Resizes() []ResizeRecord
- func (t *FakeTerminal) SlaveFd() *os.File
- type Harness
- func (h *Harness) AttachSession(sessionID string) (*exec.Session, []byte, error)
- func (h *Harness) CloseSession(sessionID string) error
- func (h *Harness) CreateResourceSession(opts exec.SessionOptions) (*exec.Session, error)
- func (h *Harness) CreateSession(opts exec.SessionOptions) (*exec.Session, error)
- func (h *Harness) DetachSession(sessionID string) (*exec.Session, error)
- func (h *Harness) GetSession(sessionID string) (*exec.Session, error)
- func (h *Harness) WaitForClose(timeout time.Duration) *exec.StreamOutput
- type HarnessOption
- type RecordingOutput
- func (r *RecordingOutput) AllData() []byte
- func (r *RecordingOutput) Count() int
- func (r *RecordingOutput) DataOutputs() []exec.StreamOutput
- func (r *RecordingOutput) OnOutput(output exec.StreamOutput)
- func (r *RecordingOutput) Outputs() []exec.StreamOutput
- func (r *RecordingOutput) WaitForOutputs(count int, timeout time.Duration) []exec.StreamOutput
- func (r *RecordingOutput) WaitForSignal(signal exec.StreamSignal, timeout time.Duration) *exec.StreamOutput
- type ResizeRecord
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FailingTTYHandler ¶
func FailingTTYHandler(err error) exec.TTYHandlerFunc
FailingTTYHandler returns a TTYHandlerFunc that immediately returns an error.
func FakeTerminalFactory ¶
func FakeTerminalFactory(ft *FakeTerminal) exec.TerminalFactory
FakeTerminalFactory returns an exec.TerminalFactory that creates the given FakeTerminal on the first call and returns an error on subsequent calls. If ft is nil, the factory always returns (nil, os.ErrClosed).
func NewFakeTerminalFactory ¶
func NewFakeTerminalFactory() exec.TerminalFactory
NewFakeTerminalFactory returns a factory that creates a fresh FakeTerminal each time it is called.
func NoopTTYHandler ¶
func NoopTTYHandler() exec.TTYHandlerFunc
NoopTTYHandler returns a TTYHandlerFunc that does nothing — the session stays open until context cancellation or stopCh signal.
func StopChTTYHandler ¶
func StopChTTYHandler(stopErr error, delay time.Duration) exec.TTYHandlerFunc
StopChTTYHandler returns a TTYHandlerFunc that sends the given error on the stopCh after a delay, simulating a handler that fails after starting. The send is non-blocking so the goroutine never leaks if the session ends early. A deferred recover guards against sends on a closed channel.
Types ¶
type FakeTerminal ¶
type FakeTerminal struct {
// contains filtered or unexported fields
}
FakeTerminal implements exec.Terminal using os.Pipe pairs. This allows tests to inject data into the master side and read from the slave side without requiring real PTY support.
func NewFakeTerminal ¶
func NewFakeTerminal() (*FakeTerminal, error)
NewFakeTerminal creates a FakeTerminal backed by two os.Pipe pairs.
func (*FakeTerminal) Close ¶
func (t *FakeTerminal) Close() error
Close closes every pipe end once and returns any close failures.
func (*FakeTerminal) InjectOutput ¶
func (t *FakeTerminal) InjectOutput(data []byte) (int, error)
InjectOutput writes data to the master write end, simulating PTY output that the manager will read from MasterFd().
func (*FakeTerminal) MarkEnd ¶ added in v0.22.1
func (t *FakeTerminal) MarkEnd(mark []byte) error
MarkEnd implements exec.EndMarker: the mark is written to the master write end, so it is read after the output injected before it, as the mark of a real terminal is. The manager uses it to deliver the last of the output of a session before the Close signal, as it does on a real terminal. It fails once the terminal is closed.
func (*FakeTerminal) MasterFd ¶
func (t *FakeTerminal) MasterFd() *os.File
MasterFd returns the borrowed output reader; Close owns its lifetime.
func (*FakeTerminal) Resize ¶
func (t *FakeTerminal) Resize(rows, cols uint16) error
Resize records dimensions without changing the pipe-backed terminal.
func (*FakeTerminal) Resizes ¶
func (t *FakeTerminal) Resizes() []ResizeRecord
Resizes returns a copy of all resize records.
func (*FakeTerminal) SlaveFd ¶
func (t *FakeTerminal) SlaveFd() *os.File
SlaveFd returns the borrowed handler reader; Close owns its lifetime.
type Harness ¶
type Harness struct {
Manager *exec.Manager
Output *RecordingOutput
// contains filtered or unexported fields
}
Harness is a test DSL wrapping a real exec.Manager with fake terminals.
func Mount ¶
func Mount(t *testing.T, opts ...HarnessOption) *Harness
Mount creates a new Harness. The harness is automatically cleaned up via t.Cleanup().
func (*Harness) AttachSession ¶
AttachSession attaches to a session.
func (*Harness) CloseSession ¶
CloseSession closes a session by ID.
func (*Harness) CreateResourceSession ¶ added in v0.9.0
CreateResourceSession explicitly creates a resource-scoped session.
func (*Harness) CreateSession ¶
CreateSession creates a session using the harness context.
func (*Harness) DetachSession ¶
DetachSession detaches from a session.
func (*Harness) GetSession ¶
GetSession retrieves a session by ID.
func (*Harness) WaitForClose ¶
func (h *Harness) WaitForClose(timeout time.Duration) *exec.StreamOutput
WaitForClose blocks until the close signal for a session is emitted.
type HarnessOption ¶
type HarnessOption func(*harnessConfig)
HarnessOption configures a Harness.
func WithHandler ¶
func WithHandler(h exec.Handler) HarnessOption
WithHandler registers a handler with the harness.
func WithTerminalFactory ¶
func WithTerminalFactory(tf exec.TerminalFactory) HarnessOption
WithTerminalFactory overrides the terminal factory.
type RecordingOutput ¶
type RecordingOutput struct {
// contains filtered or unexported fields
}
RecordingOutput implements exec.OutputSink by recording all output. Thread-safe, with WaitFor* helpers for test assertions.
func NewRecordingOutput ¶
func NewRecordingOutput() *RecordingOutput
NewRecordingOutput creates a new RecordingOutput.
func (*RecordingOutput) AllData ¶
func (r *RecordingOutput) AllData() []byte
AllData concatenates all data from non-signal outputs.
func (*RecordingOutput) Count ¶
func (r *RecordingOutput) Count() int
Count returns the number of recorded outputs.
func (*RecordingOutput) DataOutputs ¶
func (r *RecordingOutput) DataOutputs() []exec.StreamOutput
DataOutputs returns only the outputs that have non-zero data and no signal.
func (*RecordingOutput) OnOutput ¶
func (r *RecordingOutput) OnOutput(output exec.StreamOutput)
OnOutput records output and wakes waiters. Callers must not mutate shared payloads afterward.
func (*RecordingOutput) Outputs ¶
func (r *RecordingOutput) Outputs() []exec.StreamOutput
Outputs returns a copy of all recorded outputs.
func (*RecordingOutput) WaitForOutputs ¶
func (r *RecordingOutput) WaitForOutputs(count int, timeout time.Duration) []exec.StreamOutput
WaitForOutputs blocks until at least count outputs have been recorded, or the timeout expires.
func (*RecordingOutput) WaitForSignal ¶
func (r *RecordingOutput) WaitForSignal(signal exec.StreamSignal, timeout time.Duration) *exec.StreamOutput
WaitForSignal blocks until an output with the given signal is recorded, or the timeout expires.
type ResizeRecord ¶
ResizeRecord captures a resize call.