Help us shape the future of Go! Take the 2026 Go Developer Survey

consoletest

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Overview

Package consoletest runs a real lockfile console in the test process: lockkit's console.Run on lockfile's backend, attached to a fake lockhub (hubtest), a local source, plugins built from sandbox/testdata, a scripted human and a real socket in the test's private runtime directory.

Index

Constants

View Source
const HubInstance = "files"

HubInstance is the instance name of the hub NewHub makes.

Variables

This section is empty.

Functions

func NewHub added in v0.6.0

NewHub is a fake lockhub for one console (instance HubInstance), the options that attach a console to it, and the console's instance key.

func SameUserIsolation

func SameUserIsolation(t testing.TB) *console.IsolationEnv

SameUserIsolation lets a test console run in the test's own account (lockkit's AllowSameUser, a test-only switch), with its runtime dir in a fresh short directory under /tmp (socket path length).

Types

type Console

type Console struct {
	IO      *ScriptIO
	Socket  string
	Work    string
	Root    string
	Profile string
	// SharedDir is the socket directory of a Shared console.
	SharedDir string
	// PluginDir is the plugin store (tests tamper with it).
	PluginDir string
	// StartLines is how many lines the console printed before it listened
	// (start-up: isolation, policy review, status).
	StartLines int
	// Hub is the fake lockhub the console is attached to, nil when the
	// test gave its own (Options.Hub); HubOptions attach a console to it.
	Hub        *hubtest.Hub
	HubOptions console.HubOptions
	// contains filtered or unexported fields
}

Console is a running test console.

func Start

func Start(t testing.TB, o Options) *Console

Start runs a console serving profile "samples" until the test ends.

func (*Console) AuditLog added in v0.5.0

func (c *Console) AuditLog(t testing.TB) string

AuditLog is the console's audit log as it is on disk.

func (*Console) ConfigPath added in v0.5.0

func (c *Console) ConfigPath() string

ConfigPath is the console's config file (a test changes the policy on disk, then restarts).

func (*Console) Restart added in v0.5.0

func (c *Console) Restart(t testing.TB, o Options) *Console

Restart stops c and starts a console again on the same config, state (approved policy, audit log, hub plans), plugin store, work directory and source, with o's Answers, Hub and Now (the rest of o is ignored); without o.Hub, on c's hub.

func (*Console) Stop

func (c *Console) Stop(t testing.TB)

Stop logs out and waits for the console to end.

type Options

type Options struct {
	// Answers script the human at the console's terminal: the policy
	// approval first, then the console's own decisions. Requests are
	// decided on the hub (Console.Hub), never here.
	Answers []string
	Plugins []Plugin
	// Files are created in the local source, by key.
	Files map[string]string
	// Profile is extra TOML for the profile "samples" (local source,
	// prefix "invoices/", answer_quantum "1s" unless set here).
	Profile string
	// Path replaces the local source's root (a missing one makes every
	// fetch fail).
	Path    string
	Backend func(*backend.Options)
	// Shared runs the console as in a separated setup: its socket in a
	// shared directory (mode 2750, this process's group) and its own uid
	// faked as another account's, so that this process is served only as
	// a client, and only while Clients (nil: always) allows it.
	Shared  bool
	Clients func() bool
	// Hub attaches the console to this hub; unset, to a fake lockhub of
	// its own (Console.Hub). A console is hub-attached only.
	Hub console.HubOptions
	// Now is the console's clock (hub mode: tickets, decisions, plans);
	// nil is time.Now.
	Now func() time.Time
}

Options shape a test console.

type Plugin

type Plugin struct {
	Name, Binary, Question string
	TimeoutS               int
}

Plugin is a test plugin to install: Name is qualified ("test.answer1", "acme/test.answer0"); Binary is the sandbox/testdata/plugins directory (the name's last dot part when empty).

type ScriptIO

type ScriptIO struct {
	// contains filtered or unexported fields
}

ScriptIO answers the console's prompts from a script and records everything the console prints.

func NewScriptIO

func NewScriptIO(answers ...string) *ScriptIO

NewScriptIO answers the prompts with answers, in order.

func (*ScriptIO) Ask

func (s *ScriptIO) Ask(ctx context.Context, prompt string, _ time.Duration) (string, bool)

func (*ScriptIO) AskSecret

func (s *ScriptIO) AskSecret(context.Context, string) ([]byte, error)

func (*ScriptIO) Lines

func (s *ScriptIO) Lines() int

Lines is the number of lines printed so far.

func (*ScriptIO) Output

func (s *ScriptIO) Output() string

Output is everything printed so far, one line each.

func (*ScriptIO) OutputFrom

func (s *ScriptIO) OutputFrom(n int) string

OutputFrom is what was printed from line n on.

func (*ScriptIO) Println

func (s *ScriptIO) Println(l string)

func (*ScriptIO) Push

func (s *ScriptIO) Push(answers ...string)

Push adds answers to the script.

Jump to

Keyboard shortcuts

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