Documentation
¶
Overview ¶
Package testrunner provides a YAML-driven test framework for example services.
Index ¶
- func AssertCLIOutput(stdout, stderr string, exitCode int, expect CLIExpectation) []error
- func AssertHeaders(resp http.Header, expected map[string]string) []error
- func AssertResponse(resp *Response, expect Expectation) []error
- func FormatErrors(errors []error) string
- type AssertionError
- type BuildConfig
- type CC2Config
- type CC2Expectation
- type CC2Fixtures
- type CC2Flags
- type CC2TestCase
- type CLIConfig
- type CLIExpectation
- type CLITestCase
- type Duration
- type ExampleResult
- type Expectation
- type FileExpect
- type Output
- func (o *Output) Cleanup()
- func (o *Output) IsTTY() bool
- func (o *Output) PrintBanner(exampleCount, testCount int)
- func (o *Output) PrintBuildProgress(current, total int, name string)
- func (o *Output) PrintExampleError(err string)
- func (o *Output) PrintExampleHeader(name string)
- func (o *Output) PrintResults(results *Results)
- func (o *Output) PrintTestFail(name string, errMsg string, dir string, details interface{})
- func (o *Output) PrintTestPass(name string, duration time.Duration)
- func (o *Output) StartSpinner(message string)
- func (o *Output) StartTestRun(name string)
- func (o *Output) StopSpinner()
- func (o *Output) StopTestRun()
- type Response
- type Results
- type Runner
- type Server
- type ServerConfig
- type TestCase
- type TestResult
- type TestResultDetails
- type TestSpec
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 ¶
AssertHeaders checks response headers match expectations.
func AssertResponse ¶
func AssertResponse(resp *Response, expect Expectation) []error
AssertResponse checks if the response matches expectations.
func FormatErrors ¶
FormatErrors formats multiple errors into a single string. Uses newlines for separation to preserve readability.
Types ¶
type AssertionError ¶
AssertionError represents a failed assertion.
func (*AssertionError) Error ¶
func (e *AssertionError) Error() string
type BuildConfig ¶
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 ¶
Duration wraps time.Duration for YAML unmarshaling.
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 ¶
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 (*Output) Cleanup ¶
func (o *Output) Cleanup()
Cleanup stops any running spinners and restores terminal state. Call this on program exit or interrupt.
func (*Output) PrintBanner ¶
PrintBanner prints a styled banner at the start of the test run.
func (*Output) PrintBuildProgress ¶
PrintBuildProgress prints build progress.
func (*Output) PrintExampleError ¶
PrintExampleError prints an error for an example.
func (*Output) PrintExampleHeader ¶
PrintExampleHeader prints the header for an example.
func (*Output) PrintResults ¶
PrintResults prints the final test results summary.
func (*Output) PrintTestFail ¶
PrintTestFail prints a failing test result with a retry command.
func (*Output) PrintTestPass ¶
PrintTestPass prints a passing test result.
func (*Output) StartSpinner ¶
StartSpinner starts an animated spinner with a message.
func (*Output) StartTestRun ¶
StartTestRun shows the currently running test with a spinner and elapsed time.
type Response ¶
Response represents an HTTP response.
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 (*Runner) PrintResults ¶
PrintResults prints a summary of test results using the runner's output.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server represents a running example 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.