testrunner

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2026 License: GPL-3.0 Imports: 17 Imported by: 0

Documentation

Overview

Package testrunner provides a YAML-driven test framework for example services.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertCLIOutput

func AssertCLIOutput(stdout, stderr string, exitCode int, expect CLIExpectation) []error

AssertCLIOutput checks CLI command output matches expectations.

func AssertHeaders

func AssertHeaders(resp http.Header, expected map[string]string) []error

AssertHeaders checks response headers match expectations.

func AssertResponse

func AssertResponse(resp *Response, expect Expectation) []error

AssertResponse checks if the response matches expectations.

func FormatErrors

func FormatErrors(errors []error) string

FormatErrors formats multiple errors into a single string. Uses newlines for separation to preserve readability.

Types

type AssertionError

type AssertionError struct {
	Field    string
	Expected any
	Actual   any
	Message  string
}

AssertionError represents a failed assertion.

func (*AssertionError) Error

func (e *AssertionError) Error() string

type BuildConfig

type BuildConfig struct {
	Package string   `yaml:"package"`
	Timeout Duration `yaml:"timeout"`
}

BuildConfig configures how to build the example binary.

type CC2Config

type CC2Config struct {
	Timeout  Duration `yaml:"timeout"`   // Default timeout per test (default: 2m)
	CacheDir string   `yaml:"cache_dir"` // Shared OCI cache
	Image    string   `yaml:"image"`     // Default image (alpine:latest)
}

CC2Config configures cc2 tests.

type CC2Expectation

type CC2Expectation struct {
	ExitCode       int                   `yaml:"exit_code"`
	StdoutEquals   string                `yaml:"stdout_equals"`
	StdoutContains string                `yaml:"stdout_contains"`
	StderrEquals   string                `yaml:"stderr_equals"`
	StderrContains string                `yaml:"stderr_contains"`
	Files          map[string]FileExpect `yaml:"files"`
}

CC2Expectation defines expected cc2 test results.

type CC2Fixtures

type CC2Fixtures struct {
	Files map[string]string `yaml:"files"` // path -> content
	Dirs  []string          `yaml:"dirs"`
}

CC2Fixtures defines test setup files.

type CC2Flags

type CC2Flags struct {
	Memory     uint64   `yaml:"memory"`
	CPUs       int      `yaml:"cpus"`
	Timeout    Duration `yaml:"timeout"`
	Workdir    string   `yaml:"workdir"`
	User       string   `yaml:"user"`
	Dmesg      bool     `yaml:"dmesg"`
	Exec       bool     `yaml:"exec"`
	Packetdump string   `yaml:"packetdump"`
	GPU        bool     `yaml:"gpu"`
	Arch       string   `yaml:"arch"`
	CacheDir   string   `yaml:"cache_dir"`
	Build      string   `yaml:"build"`
	Env        []string `yaml:"env"`
	Mounts     []string `yaml:"mounts"`
}

CC2Flags maps to cc2 command-line flags.

type CC2TestCase

type CC2TestCase struct {
	Name   string `yaml:"name"`
	Skip   bool   `yaml:"skip"`
	SkipCI bool   `yaml:"skip_ci"`

	// Source (one of - uses cc2.Image default if none specified)
	Image      string `yaml:"image"`
	Dockerfile string `yaml:"dockerfile"`
	Bundle     string `yaml:"bundle"`

	// cc2 flags
	Flags CC2Flags `yaml:"flags"`

	// Command and input
	Command []string `yaml:"command"`
	Stdin   string   `yaml:"stdin"`

	// Fixtures
	Fixtures CC2Fixtures `yaml:"fixtures"`

	// Expected results
	Expect CC2Expectation `yaml:"expect"`
}

CC2TestCase defines a single cc2 test case.

type CLIConfig

type CLIConfig struct {
	// Timeout for each CLI test execution.
	Timeout Duration `yaml:"timeout"`
	// Env sets environment variables for all CLI tests.
	Env map[string]string `yaml:"env"`
}

CLIConfig configures CLI (non-server) tests.

type CLIExpectation

type CLIExpectation struct {
	ExitCode       int    `yaml:"exit_code"`
	StdoutContains string `yaml:"stdout_contains"`
	StdoutEquals   string `yaml:"stdout_equals"`
	StderrContains string `yaml:"stderr_contains"`
	StderrEquals   string `yaml:"stderr_equals"`
}

CLIExpectation defines expected CLI output values.

type CLITestCase

type CLITestCase struct {
	Name   string            `yaml:"name"`
	Args   []string          `yaml:"args"`
	Stdin  string            `yaml:"stdin"`
	Env    map[string]string `yaml:"env"`
	Expect CLIExpectation    `yaml:"expect"`
}

CLITestCase defines a single CLI test case.

type Duration

type Duration time.Duration

Duration wraps time.Duration for YAML unmarshaling.

func (Duration) Duration

func (d Duration) Duration() time.Duration

Duration returns the time.Duration value.

func (*Duration) UnmarshalYAML

func (d *Duration) UnmarshalYAML(value *yaml.Node) error

UnmarshalYAML implements yaml.Unmarshaler for Duration.

type ExampleResult

type ExampleResult struct {
	Name     string
	Tests    []TestResult
	Total    int
	Passed   int
	Failed   int
	Error    string
	Duration time.Duration
}

ExampleResult contains results for a single example.

type Expectation

type Expectation struct {
	Status       int               `yaml:"status"`
	Headers      map[string]string `yaml:"headers"`
	BodyContains string            `yaml:"body_contains"`
	BodyEquals   string            `yaml:"body_equals"`
	JSON         map[string]any    `yaml:"json"`
}

Expectation defines expected response values.

type FileExpect

type FileExpect struct {
	Exists   bool   `yaml:"exists"`
	Contains string `yaml:"contains"`
}

FileExpect defines file existence/content expectations.

type Output

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

Output handles terminal output with optional rich formatting.

func NewOutput

func NewOutput() *Output

NewOutput creates a new Output instance.

func (*Output) Cleanup

func (o *Output) Cleanup()

Cleanup stops any running spinners and restores terminal state. Call this on program exit or interrupt.

func (*Output) IsTTY

func (o *Output) IsTTY() bool

IsTTY returns whether the output is a terminal.

func (*Output) PrintBanner

func (o *Output) PrintBanner(exampleCount, testCount int)

PrintBanner prints a styled banner at the start of the test run.

func (*Output) PrintBuildProgress

func (o *Output) PrintBuildProgress(current, total int, name string)

PrintBuildProgress prints build progress.

func (*Output) PrintExampleError

func (o *Output) PrintExampleError(err string)

PrintExampleError prints an error for an example.

func (*Output) PrintExampleHeader

func (o *Output) PrintExampleHeader(name string)

PrintExampleHeader prints the header for an example.

func (*Output) PrintResults

func (o *Output) PrintResults(results *Results)

PrintResults prints the final test results summary.

func (*Output) PrintTestFail

func (o *Output) PrintTestFail(name string, errMsg string, dir string, details interface{})

PrintTestFail prints a failing test result with a retry command.

func (*Output) PrintTestPass

func (o *Output) PrintTestPass(name string, duration time.Duration)

PrintTestPass prints a passing test result.

func (*Output) StartSpinner

func (o *Output) StartSpinner(message string)

StartSpinner starts an animated spinner with a message.

func (*Output) StartTestRun

func (o *Output) StartTestRun(name string)

StartTestRun shows the currently running test with a spinner and elapsed time.

func (*Output) StopSpinner

func (o *Output) StopSpinner()

StopSpinner stops the animated spinner.

func (*Output) StopTestRun

func (o *Output) StopTestRun()

StopTestRun stops the test spinner.

type Response

type Response struct {
	StatusCode int
	Body       []byte
	Headers    http.Header
}

Response represents an HTTP response.

func (*Response) JSON

func (r *Response) JSON(v any) error

JSON unmarshals the response body into v.

func (*Response) String

func (r *Response) String() string

String returns the response body as a string.

type Results

type Results struct {
	Examples []ExampleResult
	Total    int
	Passed   int
	Failed   int
	Duration time.Duration
}

Results contains the results of running tests.

type Runner

type Runner struct {
	Verbose   bool
	KeepAlive bool
	Parallel  int
	Output    *Output
	// CC2Binary is the path to the cc2 binary for CC2 tests.
	// If empty, CC2 tests will attempt to build cc2 automatically.
	CC2Binary string
}

Runner manages the lifecycle of example services for testing.

func NewRunner

func NewRunner() *Runner

NewRunner creates a new test runner.

func (*Runner) PrintResults

func (r *Runner) PrintResults(results *Results)

PrintResults prints a summary of test results using the runner's output.

func (*Runner) Run

func (r *Runner) Run(ctx context.Context, patterns []string) (*Results, error)

Run executes tests for all matching patterns.

type Server

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

Server represents a running example server.

func (*Server) Stop

func (s *Server) Stop() error

Stop terminates the server.

type ServerConfig

type ServerConfig struct {
	Port            int               `yaml:"port"`
	StartupTimeout  Duration          `yaml:"startup_timeout"`
	ShutdownTimeout Duration          `yaml:"shutdown_timeout"`
	Env             map[string]string `yaml:"env"`
}

ServerConfig configures how to run the server.

type TestCase

type TestCase struct {
	Name       string            `yaml:"name"`
	Method     string            `yaml:"method"`
	Path       string            `yaml:"path"`
	Headers    map[string]string `yaml:"headers"`
	Body       any               `yaml:"body"`
	BodyRaw    string            `yaml:"body_raw"`
	BodyBase64 string            `yaml:"body_base64"`
	Expect     Expectation       `yaml:"expect"`
}

TestCase defines a single HTTP test case.

type TestResult

type TestResult struct {
	Name     string
	Passed   bool
	Error    string
	Duration time.Duration
	// Details contains additional context for debugging failures.
	Details *TestResultDetails
}

TestResult contains the result of a single test case.

type TestResultDetails

type TestResultDetails struct {
	// For CLI tests
	Stdout   string
	Stderr   string
	ExitCode int
	Args     []string
	// For HTTP tests
	Method     string
	Path       string
	StatusCode int
	Body       string
}

TestResultDetails provides additional context for test failures.

type TestSpec

type TestSpec struct {
	Name        string       `yaml:"name"`
	Description string       `yaml:"description"`
	Build       BuildConfig  `yaml:"build"`
	Server      ServerConfig `yaml:"server"`
	Tests       []TestCase   `yaml:"tests"`
	// CLI specifies this is a CLI test (no server).
	CLI      *CLIConfig    `yaml:"cli,omitempty"`
	CLITests []CLITestCase `yaml:"cli_tests,omitempty"`
	// CC2 specifies this is a cc2 test (container VM).
	CC2      *CC2Config    `yaml:"cc2,omitempty"`
	CC2Tests []CC2TestCase `yaml:"cc2_tests,omitempty"`
}

TestSpec defines the complete test specification for an example.

func LoadSpec

func LoadSpec(path string) (*TestSpec, error)

LoadSpec loads a test specification from a YAML file.

func (*TestSpec) IsCC2

func (s *TestSpec) IsCC2() bool

IsCC2 returns true if this spec is for cc2 tests (container VM).

func (*TestSpec) IsCLI

func (s *TestSpec) IsCLI() bool

IsCLI returns true if this spec is for CLI tests (no server).

Directories

Path Synopsis
cmd
runtest command
Command runtest executes YAML-driven tests for example services.
Command runtest executes YAML-driven tests for example services.

Jump to

Keyboard shortcuts

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