test(examples): controller-level component golden tests#158
Open
sourcehawk wants to merge 2 commits into
Open
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. |
Comment on lines
41
to
42
| golden.AssertYAML(t, "testdata/deployment.yaml", res.(golden.Previewer), | ||
| golden.WithScheme(testScheme()), golden.Update(*update)) |
Comment on lines
39
to
40
| golden.AssertYAML(t, "testdata/configmap.yaml", res.(golden.Previewer), | ||
| golden.WithScheme(scheme), golden.Update(*update)) |
Comment on lines
26
to
27
| golden.AssertYAML(t, "testdata/secret.yaml", res.(golden.Previewer), | ||
| golden.WithScheme(scheme), golden.Update(*update)) |
Comment on lines
37
to
38
| golden.AssertYAML(t, "testdata/configmap.yaml", res.(golden.Previewer), | ||
| golden.WithScheme(scheme), golden.Update(*update)) |
Comment on lines
+65
to
+66
| 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.