Repository navigation
fix(diff): avoid panic when expected and actual value types differ - #2777
Open
amalsaidov wants to merge 1 commit into
Open
amalsaidov wants to merge 1 commit into
amalsaidov wants to merge 1 commit into
Conversation
PrettyDiff is used to render the diff shown when a resource assertion
fails. The prune helpers walk the actual object and, whenever a key holds
a nested map, recurse into the matching key of the expected object with an
unchecked type assertion (expected[k].(map[string]any)).
When the expected manifest holds a scalar at a key where the live resource
holds a map (for example a ConfigMap asserted with "data: foo" while the
real object has a "data" map), that assertion panics with
"interface conversion: interface {} is string, not map[string]interface {}"
and crashes the run instead of printing the diff that explains the failure.
Guard the expected lookup with the comma-ok form and only recurse when the
expected value is a map too, so a type mismatch simply shows up in the diff.
AI-assisted; directed, reviewed and tested by the author.
Signed-off-by: Amal Saidov <amalsaidov@users.noreply.github.com>
This branch has not been deployed
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.
Explanation
This is a bug fix.
PrettyDiff(pkg/utils/diff) renders the diff that Chainsaw prints when a resource assertion fails. Today, if the expected manifest holds a scalar at a key where the live resource holds a map, Chainsaw panics instead of printing that diff, so the whole run crashes on what should be an ordinary assertion failure.Related issue
No existing issue. Found while reading the assertion/diff code. Happy to open an issue first if a maintainer prefers.
Proposed Changes
The
prune,pruneMetadataandpruneRoothelpers walk the actual object and, whenever a key holds a nested map, recurse into the same key of the expected object using an unchecked type assertion:When
expected[k]is present but is not a map (for example aConfigMapasserted withdata: foowhile the real object has adatamap), that assertion panics:This path is reachable from
resourceError.Error()(pkg/engine/operations/errors/resource.go), i.e. the normal failure-reporting path, so a slightly malformed assertion crashes the run rather than showing a helpful diff.The fix guards the expected lookup with the comma-ok form and only recurses when the expected value is also a map. On a type mismatch the differing values simply show up in the rendered diff, which is the useful behaviour.
Added unit tests in
pkg/utils/diff/diff_test.gocovering a scalar-vs-map mismatch at the root and undermetadata(both panic without this change, pass with it). No user-facing behaviour changes beyond no longer crashing, so no proof manifests.Checklist
AI-assisted; directed, reviewed and tested by the author.