Go-Snaps
Go-Snaps
Snapshot testing library for Go applications
Go-Snaps is a snapshot testing library for Go used in the Control Plane to verify that complex outputs match expected values.
Snapshot testing
Snapshot testing captures a component's output and compares it to a previously saved reference output (the "snapshot"). This approach is particularly useful for testing complex structures that are difficult to verify with traditional assertions.
Overviewโ
Go-Snaps provides a simple, effective way to implement snapshot testing in Go applications. It's particularly valuable for testing complex data structures, API responses, and generated code.
Why Go-Snaps?โ
๐ธ Snapshot Capture
Automatically save output snapshots for future comparisons.
๐ Snapshot Updates
Update snapshots when expected behavior changes.
๐ Diff Generation
Clear visualization of differences between current output and snapshot.
๐งฉ Custom Serializers
Support for custom serialization of complex types.
โฑ๏ธ Inline Snapshots
Store small snapshots directly in test files.
๐ง CLI Integration
Command-line tools for managing snapshots.
Integration in the Control Planeโ
Complex output testing
In the Control Plane, Go-Snaps is used to test components that produce complex outputs such as CRD generators, API responses, and resource templates.
The Control Plane uses Go-Snaps for testing various components:
API Responses
Testing the structure and content of API responses:
- JSON response bodies
- Error response formats
- Pagination structures
- Content negotiation results
Resource Templates
Verifying generated Kubernetes resources:
- Deployment manifests
- Service configurations
- ConfigMap content
- CRD definitions
Code Generation
Testing output of code generators:
- Controller boilerplate
- Client code
- Schema validation
- Documentation generation
Configuration Processing
Testing configuration transformation logic:
- Config file parsing
- Environment variable processing
- Default value application
- Validation error messages
Configurationโ
Snapshot configuration and formats
Go-Snaps provides various configuration options and supports multiple data formats commonly used in the Control Plane.
Snapshot Storageโ
By default, Go-Snaps stores snapshots in a __snapshots__
directory next to your test files:
pkg/controller/
โโโ controller_test.go
โโโ __snapshots__/
โ โโโ controller_test.snap
โโโ controller.go
Snapshots are stored in a readable format that's version control friendly.
Supported Formatsโ
MatchJSON
Match JSON data with normalized formatting.
MatchYAML
Match YAML data with normalized formatting.
MatchInline
Store small snapshots directly in test files.
Match
Match any serializable data structure.
MatchText
Match text content without normalization.
MatchSnapshot
Low-level API for custom matching.
Advanced Featuresโ
Custom Serializers
Define custom serialization for complex types
Update Mode
Update snapshots when behavior changes
Exclusion Paths
Exclude dynamic fields from comparison
Inline Snapshots
Store small snapshots directly in test files
CLI Usageโ
Go-Snaps provides a command-line tool for managing snapshots:
# Install the CLI
go install github.com/gkampitakis/go-snaps/cmd/go-snaps@latest
# Show differences in snapshots
go-snaps diff
# Clean up unused snapshots
go-snaps cleanup
# Update all snapshots
go-snaps update
# Update specific snapshots
go-snaps update --test TestDeploymentController
Best Practices in the Control Planeโ
Effective snapshot testing
The Control Plane follows these best practices for effective snapshot testing.
๐ฏ Test Focused Cases
Create separate snapshots for specific behaviors rather than large, catch-all snapshots.
๐พ Version Control Snapshots
Always commit snapshot files to version control alongside code changes.
๐งน Regular Cleanup
Use 'go-snaps cleanup' periodically to remove orphaned snapshots.
๐ Code Review Snapshots
Review snapshot changes carefully during code reviews to catch unintended changes.
โฑ๏ธ Normalize Timestamps
Use custom serializers or exclusion paths to handle dynamic values like timestamps.
๐ Descriptive Test Names
Use clear, descriptive test names that explain what the snapshot represents.
Related Resourcesโ
Ginkgo
BDD-style testing framework for Go applications.
Gomega
Matcher/assertion library that works well with Go-Snaps.
Testify
Another testing toolkit used in the Control Plane.