refactor: update tests for new YAML output order after MustString() migration#1
refactor: update tests for new YAML output order after MustString() migration#1Rindrics wants to merge 8 commits intoissue-947-long-linefrom
Conversation
Add assertGolden helper function to simplify updating test expectations when resource output format changes. Migrate TestMergeDataMapFrom as the first example.
Migrate resource_test.go to use golden file testing for AsYAML() output. This makes it easier to update test expectations when output format changes. - Add assertGoldenYAML helper function - Convert assert.Equal patterns to assertGoldenYAML calls - Generate golden files for 18 test cases The migration script (migrate_to_golden.py) is included for reference.
Migrate krusty tests to use golden file testing for AsYAML() output. This makes it easier to update test expectations when output format changes. - Add AssertYAMLEqualsGolden helper function to kusttest - Update AssertActualEqualsExpected to support golden files - Convert originannotation_test.go to use AssertYAMLEqualsGolden - Generate golden files for 258 test cases
Update expectedContent in build_test.go to match the new field order from resource.AsYAML() which now uses kyaml encoder instead of JSON-to-YAML conversion.
This ensures that duplicate keys in YAML are detected and reported as errors, maintaining compatibility with the previous behavior where sigs.k8s.io/yaml.JSONToYAML would catch these errors. Fixes TestDuplicateKeys which was failing after switching to RNode.MustString() for YAML output.
- Convert TestApplySmPatch to use subtests with golden files - Convert TestDropLocalNodes to use golden files - Convert TestVolumeRemoveEmptyDirInOverlay to use golden files - Update golden files to match new YAML output order These tests were failing due to output order changes after switching Resource.AsYAML() to use RNode.MustString() instead of sigs.k8s.io/yaml.JSONToYAML.
After changing Resource.AsYAML() to use RNode.MustString() instead of sigs.k8s.io/yaml.JSONToYAML, the YAML output order changed from alphabetical to preserving the original YAML order. This caused many tests to fail due to order differences. To handle these order differences and make future changes easier to manage, migrate affected tests to use golden files: - Add golden_test.go helper for resmap tests - Migrate TestDeAnchorSingleDoc, TestDeAnchorIntoDoc, TestDeAnchorResourceList, TestApplySmPatch_General, TestApplySmPatch_Deletion, TestApplyFilter, and TestAbsorbAll in resmap package - Migrate TestSliceFromPatches in resource package This approach allows the tests to accept the new output order while maintaining the ability to detect unexpected changes.
Review Summary by QodoMigrate test suite to golden file pattern for YAML output validation after MustString() migration
WalkthroughsDescription• Migrated test suite to use golden file testing pattern for YAML output validation • Added golden file testing infrastructure with helper functions and flag support (-update, -update-golden) • Fixed duplicate key detection in Resource.AsYAML() by calling MarshalJSON() before YAML conversion • Updated 100+ test cases across api/resource, api/resmap, and api/krusty packages to use golden files • Created 80+ golden files containing expected YAML output with preserved field ordering • Updated kustomize/commands/build tests to match new YAML field order from MustString() migration • Removed 1000+ lines of hardcoded expected YAML strings from test code • Added Python migration scripts to automate test conversion to golden file pattern Diagramflowchart LR
A["Old Test Pattern<br/>Hardcoded YAML Strings"] -->|"Replace with"| B["Golden File Testing<br/>assertGoldenYAML()"]
C["Resource.AsYAML()<br/>JSONToYAML"] -->|"Uses"| D["RNode.MustString()<br/>Preserves Field Order"]
D -->|"Requires"| B
E["Test Files<br/>resource_test.go<br/>reswrangler_test.go<br/>originannotation_test.go"] -->|"Migrate to"| F["Golden Files<br/>testdata/golden/"]
B -->|"Supports"| G["Flag Updates<br/>-update<br/>-update-golden"]
File Changes1. api/krusty/originannotation_test.go
|
These Python scripts were used to automate the migration to golden file testing. Since the migration is complete, they are no longer needed.
Code Review by Qodo
1. Golden collision subtests
|
Summary
This PR updates all tests to handle the change in YAML output order that resulted from switching
Resource.AsYAML()to useRNode.MustString()instead ofsigs.k8s.io/yaml.JSONToYAML(see PR kubernetes-sigs#6046). The change preserves the original YAML field order instead of sorting fields alphabetically, which requires updating test expectations.Background
PR kubernetes-sigs#6046 changed
Resource.AsYAML()to useRNode.MustString()to fix long line wrapping issues and preserve original YAML formatting. This change also affects the output order of YAML fields:While both orderings are valid YAML, this change requires updating all test expectations to match the new output order.
Duplicate Key Detection
Added duplicate key detection by calling
MarshalJSON()before converting to YAML. This maintains the same error detection behavior as before while using the new YAML serialization method.Test Updates
Migrated affected tests to use golden files to handle the output order differences:
api/resource: Added golden file testing helper and migratedTestSliceFromPatches,TestApplySmPatch,TestMergeDataMapFrom, andTestDropLocalNodesapi/resmap: Added golden file testing helper and migratedTestDeAnchorSingleDoc,TestDeAnchorIntoDoc,TestDeAnchorResourceList,TestApplySmPatch_General,TestApplySmPatch_Deletion,TestApplyFilter, andTestAbsorbAllapi/krusty: Migrated all tests usingAssertActualEqualsExpectedto use golden fileskustomize/commands/build: Updated expected YAML strings to match new output orderthese migrations are done by temporal scripts, but they are removed after being used:
a399917(this PR)Testing
All tests pass with the new implementation:
Benefits
Related PRs and Issues