harness

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package harness provides scripted fakes for every capkit port. Contract tests instantiate a kit with these fakes so a tool call runs hermetically: no network, no subprocess, no wall clock. Each fake records what it was asked to do, so a test can assert on the request as well as the result.

The fakes satisfy the capkit interfaces structurally; this package therefore imports only the root contract and never a kit.

Index

Constants

This section is empty.

Variables

View Source
var DefaultTime = time.Date(2024, 5, 26, 12, 0, 0, 0, time.UTC)

DefaultTime is the clock origin every fixture uses, so signature and timestamp assertions are reproducible.

Functions

func Deps

func Deps() capkit.Deps

Deps builds a full capkit.Deps wired with permissive fakes.

func MinimalInput

func MinimalInput(schema json.RawMessage) json.RawMessage

MinimalInput derives a smallest legal input for a tool from its JSON schema: every required property is filled with a type-appropriate sample value. It exists so a contract test can call every registered tool without hand-written fixtures per tool.

Types

type Approval

type Approval struct {
	Err      error
	Requests []capkit.ApprovalRequest
}

Approval is an approving-by-default capkit.Approver.

func (*Approval) Require

func (a *Approval) Require(_ context.Context, req capkit.ApprovalRequest) error

type Artifacts

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

Artifacts is an in-memory capkit.ArtifactStore.

func (*Artifacts) Delete

func (a *Artifacts) Delete(_ context.Context, path string) error

func (*Artifacts) Get

func (a *Artifacts) Get(_ context.Context, path string) ([]byte, error)

func (*Artifacts) Put

func (a *Artifacts) Put(_ context.Context, name string, data []byte) (string, error)

type Audit

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

Audit records every capkit.AuditEvent it receives.

func (*Audit) Events

func (a *Audit) Events() []capkit.AuditEvent

Events returns a copy of the recorded events.

func (*Audit) Record

func (a *Audit) Record(_ context.Context, ev capkit.AuditEvent)

type Binaries

type Binaries struct {
	Path  string
	State capkit.BinaryStatus
	Err   error
}

Binaries is a scripted capkit.BinaryManager reporting every binary installed.

func (*Binaries) Ensure

func (*Binaries) Status

func (*Binaries) Update

type Clock

type Clock struct {
	Now_ time.Time
	Step time.Duration
	// contains filtered or unexported fields
}

Clock is a manual clock. Every call to Now advances by Step, so a test that needs distinct timestamps gets them without sleeping.

func NewClock

func NewClock(t time.Time) *Clock

NewClock builds a clock starting at t.

func (*Clock) Advance

func (c *Clock) Advance(d time.Duration)

Advance moves the clock forward by d.

func (*Clock) Now

func (c *Clock) Now() time.Time

func (*Clock) Set

func (c *Clock) Set(t time.Time)

Set pins the clock to t.

type CloudSignRequest

type CloudSignRequest struct {
	Request   capkit.Request
	AccessKey string
	SecretKey string
	At        time.Time
}

CloudSignRequest is one recorded signing request.

type CloudSigner

type CloudSigner struct {
	// Signature is the value embedded in the Authorization header.
	Signature string
	// AccessKey, when set, overrides the credential echoed into the header.
	AccessKey string
	Err       error
	// contains filtered or unexported fields
}

CloudSigner is a scripted cloud request signer. It records every request it was asked to sign and stamps a deterministic Authorization header, so a cloud tool can be exercised offline: the test asserts on what would have been sent without ever reaching a vendor API.

It satisfies cloudkit's Signer interface structurally, which keeps this package free of kit imports.

func (*CloudSigner) SignRequest

func (s *CloudSigner) SignRequest(req capkit.Request, accessKey, secretKey string, now time.Time) (capkit.Request, error)

SignRequest stamps req with a fake Authorization header and records the call.

func (*CloudSigner) Signed

func (s *CloudSigner) Signed() []CloudSignRequest

Signed returns a copy of every signing request.

type Engine

type Engine struct {
	KindName string
	Result   capkit.Result
	Err      error
}

Engine is a scripted capkit.TemplateEngine.

func (*Engine) Execute

func (*Engine) Kind

func (e *Engine) Kind() string

type Evidence

type Evidence struct {
	Prefix string
	Err    error
	// contains filtered or unexported fields
}

Evidence stores every capkit.Evidence it is handed and returns "<Prefix><n>".

func (*Evidence) Record

func (e *Evidence) Record(_ context.Context, ev capkit.Evidence) (string, error)

func (*Evidence) Records

func (e *Evidence) Records() []capkit.Evidence

Records returns a copy of the stored evidence.

type Fingerprints

type Fingerprints struct {
	Result []capkit.Fingerprint
	Err    error
}

Fingerprints is a scripted capkit.FingerprintProvider.

func (*Fingerprints) Fingerprint

type HTTP

type HTTP struct {
	// Default answers any request no route matches.
	Default Route
	// Routes are checked in order before Default.
	Routes []Route
	// Queue answers the first len(Queue) calls in order, before Routes and
	// Default apply, and is consumed as it is used. It exists for protocol-level
	// sequences a route table cannot express — "the invocation does not exist
	// yet on the first poll, succeeds on the second" — where the answer depends
	// on how many calls came before rather than on the request itself.
	Queue []Route
	// Err fails every call regardless of routes.
	Err error
	// contains filtered or unexported fields
}

HTTP is a scripted capkit.HTTPDoer and capkit.Fetcher. It answers from a route table, falling back to the default response, and records every request in call order.

func NewHTTP

func NewHTTP(status int, body string) *HTTP

NewHTTP builds a fake transport answering every request with status/body.

func NewHTTPJSON

func NewHTTPJSON(body string) *HTTP

NewHTTPJSON answers every request with a JSON body.

func (*HTTP) Calls

func (h *HTTP) Calls() int

Calls is the number of requests the fake received.

func (*HTTP) Do

func (h *HTTP) Do(ctx context.Context, req capkit.Request) (*capkit.Response, error)

func (*HTTP) Fetch

func (h *HTTP) Fetch(ctx context.Context, req capkit.Request) (*capkit.Response, error)

func (*HTTP) Last

func (h *HTTP) Last() (capkit.Request, bool)

Last returns the most recent request.

func (*HTTP) Requests

func (h *HTTP) Requests() []capkit.Request

Requests returns a copy of every request the fake received.

type IDs

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

IDs is a deterministic capkit.IDGenerator: the n-th call for a prefix returns "<prefix>-<n>".

func (*IDs) New

func (g *IDs) New(prefix string) string

type Limiter

type Limiter struct {
	Err      error
	Acquired []string
}

Limiter is a non-blocking capkit.Limiter that records the keys it admitted.

func (*Limiter) Acquire

func (l *Limiter) Acquire(_ context.Context, key string, _ capkit.RateLimitSpec) (func(), error)

type Logger

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

Logger keeps every line it is given.

func (*Logger) Error

func (l *Logger) Error(msg string, kv ...any)

func (*Logger) Info

func (l *Logger) Info(msg string, kv ...any)

func (*Logger) Lines

func (l *Logger) Lines() []string

Lines returns every logged line.

func (*Logger) Warn

func (l *Logger) Warn(msg string, kv ...any)

type Ports

type Ports struct {
	HTTP     *HTTP
	Fetcher  *HTTP
	Scope    *Scope
	Approval *Approval
	Limiter  *Limiter
	Audit    *Audit
	Evidence *Evidence
	Settings *Settings
	Sealer   Sealer
	Logger   *Logger
	Clock    *Clock
	IDs      *IDs
	Process  *Process
	Artifact *Artifacts
	Binaries *Binaries

	Fingerprints *Fingerprints
	Templates    *Templates
	Engine       *Engine
	Sessions     *Sessions
}

Ports is the assembled fake port set. Fields are exposed so a test can swap one fake for a stricter one.

func NewPorts

func NewPorts() *Ports

NewPorts builds a complete, permissive fake port set: an empty session list, no fingerprints, no templates, a transport answering every request with the given body, and a process runner that exits 0 with no output.

func (*Ports) Deps

func (p *Ports) Deps() capkit.Deps

Deps projects the fake ports into a capkit.Deps for a kit constructor.

type Process

type Process struct {
	Stdout   []byte
	Stderr   []byte
	ExitCode int
	Err      error
	// Handler, when set, overrides the preset and answers per spec.
	Handler func(capkit.ProcessSpec) (*capkit.ProcessResult, error)
	// contains filtered or unexported fields
}

Process is a scripted capkit.ProcessRunner: every Run returns the preset stdout/stderr/exit code and records the spec.

func (*Process) Calls

func (p *Process) Calls() []capkit.ProcessSpec

Calls returns a copy of every process spec the fake received.

func (*Process) Last

func (p *Process) Last() (capkit.ProcessSpec, bool)

Last returns the most recent process spec.

func (*Process) Run

type Route

type Route struct {
	Match   string
	Status  int
	Body    string
	Headers map[string][]string
	Err     error
}

Route is one scripted HTTP answer. When Match is non-empty only requests whose URL contains it take this route; otherwise it is the fallback.

type Scope

type Scope struct {
	Denied  []string
	Err     error
	Checked []capkit.Target
}

Scope is a permissive-by-default capkit.ScopeChecker. Denied lists the hosts that must be refused; Err denies everything.

func (*Scope) Check

func (s *Scope) Check(_ context.Context, t capkit.Target) error

type Sealer

type Sealer struct{ Key byte }

Sealer is a reversible XOR sealer for tests. It is not cryptographic and must never be used outside a test.

func (Sealer) Open

func (s Sealer) Open(sealed string) (string, error)

func (Sealer) Seal

func (s Sealer) Seal(plain string) (string, error)

type Session

type Session struct {
	SessionInfo capkit.SessionInfo
	Stdout      string
	Stderr      string
	ExitCode    int
	Err         error
	// contains filtered or unexported fields
}

Session is a scripted capkit.Session: Exec answers with the preset output.

func NewSession

func NewSession(id string) *Session

NewSession builds a session for host:22 running as root.

func (*Session) Calls

func (s *Session) Calls() []string

Calls returns every command the session was asked to run.

func (*Session) Close

func (s *Session) Close() error

func (*Session) Exec

func (s *Session) Exec(_ context.Context, cmd string, _ capkit.ExecOpts) (capkit.Output, error)

func (*Session) Info

func (s *Session) Info() capkit.SessionInfo

type Sessions

type Sessions struct {
	OpenErr error
	// contains filtered or unexported fields
}

Sessions is an in-memory capkit.SessionManager. NewSessions starts empty; NewSessionsWith pre-registers the given sessions.

func NewSessions

func NewSessions() *Sessions

func NewSessionsWith

func NewSessionsWith(list ...*Session) *Sessions

NewSessionsWith builds a manager holding the given sessions.

func (*Sessions) Add

func (m *Sessions) Add(s *Session)

Add registers s so tools can resolve it by ID.

func (*Sessions) Close

func (m *Sessions) Close(id string) error

func (*Sessions) Get

func (m *Sessions) Get(id string) (capkit.Session, bool)

func (*Sessions) List

func (m *Sessions) List() []capkit.SessionInfo

func (*Sessions) NextID

func (m *Sessions) NextID(prefix string) string

NextID is a helper for tests that need a deterministic session id.

func (*Sessions) Open

type Settings

type Settings struct {
	Strings map[string]string
	Bools   map[string]bool
	Ints    map[string]int64
}

Settings is an in-memory capkit.Settings.

func NewSettings

func NewSettings() *Settings

func (*Settings) GetBool

func (s *Settings) GetBool(_ context.Context, key string) (bool, bool)

func (*Settings) GetInt

func (s *Settings) GetInt(_ context.Context, key string) (int64, bool)

func (*Settings) GetString

func (s *Settings) GetString(_ context.Context, key string) (string, bool)

type Templates

type Templates struct {
	Refs []capkit.TemplateRef
	Err  error
}

Templates is an in-memory capkit.TemplateCatalog.

func (*Templates) Get

func (*Templates) Search

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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