test(examples): controller-level component golden tests - #158
Conversation
There was a problem hiding this comment.
Pull request overview
Adds controller-level golden snapshot tests for example controllers and refactors controller reconciliation to share component construction with tests, ensuring snapshots reflect the exact desired state that gets reconciled.
Changes:
- Refactor example controllers to extract shared
Build*Component()helpers used by bothReconcileand tests. - Add controller-level
golden.AssertComponentYAMLtests plus committedtestdata/*.yamlsnapshots for multiple examples. - Tighten several resource-level tests to call the real resource factory functions and add mutation/inspection assertions where applicable.
Reviewed changes
Copilot reviewed 16 out of 16 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| examples/grace-inconsistency/resources/deployment_test.go | Switch resource test to use the deployment factory and snapshot the previewed desired state. |
| examples/grace-inconsistency/app/testdata/component.yaml | Add golden snapshot for the controller-built component output. |
| examples/grace-inconsistency/app/controller.go | Extract BuildComponent to share component assembly between controller and tests. |
| examples/grace-inconsistency/app/controller_test.go | Add controller-level golden component snapshot test. |
| examples/extraction-and-guards/resources/secret_test.go | Update Secret resource snapshot test to use the factory and preview interface. |
| examples/extraction-and-guards/resources/configmap_test.go | Update ConfigMap resource snapshot test to use the factory and shared extracted state wiring. |
| examples/extraction-and-guards/app/testdata/component.yaml | Add golden snapshot for multi-resource component preview output. |
| examples/extraction-and-guards/app/controller.go | Extract BuildComponent to share ordered resource registration and shared-state wiring. |
| examples/extraction-and-guards/app/controller_test.go | Add controller-level golden component snapshot test. |
| examples/custom-resource/resources/certificate_test.go | Add mutation-inspection assertions and keep golden shape snapshot using the real factory. |
| examples/component-prerequisites/resources/deployment_test.go | Update Deployment tests to use the factory, add mutation inspection test, and snapshot per version. |
| examples/component-prerequisites/resources/configmap_test.go | Update ConfigMap resource snapshot test to use the factory and preview interface. |
| examples/component-prerequisites/app/testdata/infra-component.yaml | Add golden snapshot for infra component preview output. |
| examples/component-prerequisites/app/testdata/app-component.yaml | Add golden snapshot for app component preview output. |
| examples/component-prerequisites/app/controller.go | Extract BuildInfraComponent / BuildAppComponent for shared assembly and testability. |
| examples/component-prerequisites/app/controller_test.go | Add controller-level golden component snapshot tests for both infra and app components. |
| golden.AssertYAML(t, "testdata/deployment.yaml", res.(golden.Previewer), | ||
| golden.WithScheme(testScheme()), golden.Update(*update)) |
| golden.AssertYAML(t, "testdata/configmap.yaml", res.(golden.Previewer), | ||
| golden.WithScheme(scheme), golden.Update(*update)) |
| golden.AssertYAML(t, "testdata/secret.yaml", res.(golden.Previewer), | ||
| golden.WithScheme(scheme), golden.Update(*update)) |
| golden.AssertYAML(t, "testdata/configmap.yaml", res.(golden.Previewer), | ||
| golden.WithScheme(scheme), golden.Update(*update)) |
| golden.AssertYAML(t, tt.golden, res.(golden.Previewer), | ||
| golden.WithScheme(scheme), golden.Update(*update)) |
| require.NoError(t, err) | ||
|
|
||
| golden.AssertYAML(t, "testdata/certificate.yaml", res, golden.Update(*update)) | ||
| golden.AssertYAML(t, "testdata/certificate.yaml", res.(golden.Previewer), golden.Update(*update)) |
Extract Build*Component() methods from the reconcile loops in the component-prerequisites, extraction-and-guards, and grace-inconsistency examples so tests can build components through the same assembly path the controller uses. Add controller_test.go + testdata/ golden snapshots for each example using golden.AssertComponentYAML. Update resource tests in all three examples (and custom-resource) to call the actual factory functions rather than duplicating builder construction, so the golden files reflect real factory output. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Drop the three controller_test.go files added in the previous commit. main now carries component_test.go in each example's app package that already provides the same golden.AssertComponentYAML coverage, so the files were redundant and caused a redeclaration conflict on update and testOwner when CI merged the branch. Replace unchecked res.(golden.Previewer) casts in the resource tests with ok-checked require.True assertions, as flagged in review, so a non-Previewer resource produces a clear test failure instead of a panic. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
abb9b24 to
1042281
Compare
|
Closing in favour of #169. The controller-level component golden tests this PR set out to add have since That remaining change is still worth having, so it has been landed against |
Description
Adds controller-level
golden.AssertComponentYAMLsnapshot tests to thecomponent-prerequisites,extraction-and-guards, andgrace-inconsistencyexamples. To make this possible, the reconcile logic in each controller is
refactored to extract a
Build*Component()method that both the controllerand the tests share — so the golden snapshot reflects the same desired state
that gets reconciled, not a separately-constructed approximation.
Resource tests in those three examples and in
custom-resourceare alsotightened up to call the real factory functions instead of duplicating the
builder construction inline.
Changes
BuildInfraComponent/BuildAppComponentfromcomponent-prerequisitescontroller; same pattern forextraction-and-guardsandgrace-inconsistency(BuildComponent)controller_test.go+testdata/golden snapshots per example viagolden.AssertComponentYAMLNewConfigMapResource,NewDeploymentResource, etc.) rather than re-building primitives inlinecustom-resourcecertificate test to assert registered mutations viaconcepts.MutationInspectorinstead of re-running the full mutation logicTesting
go test ./examples/...andmake all(tests + lint + fmt) all pass clean. Golden files were generated with-updateand committed alongside the tests.