Skip to content

Commit c97f9dd

Browse files
authored
Make diff result as deterministic as possible (#231)
`helm diff upgrade` output is non-deterministic, due to that it shows diff entry in the order of the Go map objects containing source/target manifests to be compared. The non-determinism makes writing integration test with something and helm-diff harder, as snapshot testing is impossible. This patch fixes it by enhancing the diff logic to sort those map keys in alphabetical order and use the result for iterating map entries. Note that you can use the below snippet for non-scientific testing to verify that the result is (seemingly) deterministic now: ``` for (( i=0; i<10; ++i)); do helm --debug diff upgrade --dry-run foo stable/prometheus-operator | sha256sum ; done ``` Before this change it should print output like: ``` $ for (( i=0; i<10; ++i)); do helm --debug diff upgrade --dry-run foo stable/prometheus-operator | sha256sum ; done 754f01763544baeb4f405ccdcdf7ffd9075bfddf9777a993f1e787ea6096b0b4 - 88a5f76e3802e0e4693fe007d4ae6fc8c3ae5cf97b7244dc67e45a282385e2c3 - b57e66a22c49ec2a29dacc9e912a313aca85b403ac3d2960fc0afddeda544a77 - 769c605ea8c13f4251559948260f8c6104d821ce79de1367d2a6ee477be37777 - afce52fa595beaf106303ae162efb4f31a7336c84a630b1d4ea1e59ba1468292 - 12a3b3c1e3fdcd0b4b62517583135abe20249d69e6c4aed9acfab7c999771bdd - 294d5db8edec946737036014b549baa93601aadf430f1b80517db9eca2dd1330 - 31860127c9ae69e5d0154b60cc7ea05e992bac54e1453c88198940e719552f72 - dce05e1932ca7d6e15416de3b0a891ae68e3c885247f683fe50068b2c551213b - f0101d6e952fd0344d9e45518254cb3e0692156becaae106fcc7b1dae273ac04 - ``` After the change it should be like: ``` $ make build install/helm3 $ for (( i=0; i<10; ++i)); do helm --debug diff upgrade --dry-run foo stable/prometheus-operator | sha256sum ; done ee4e79f6adee9e2606c9d855949afd39cf5a13a650cb55a3d74184c7edaf956d - ee4e79f6adee9e2606c9d855949afd39cf5a13a650cb55a3d74184c7edaf956d - ee4e79f6adee9e2606c9d855949afd39cf5a13a650cb55a3d74184c7edaf956d - ee4e79f6adee9e2606c9d855949afd39cf5a13a650cb55a3d74184c7edaf956d - ee4e79f6adee9e2606c9d855949afd39cf5a13a650cb55a3d74184c7edaf956d - ee4e79f6adee9e2606c9d855949afd39cf5a13a650cb55a3d74184c7edaf956d - ee4e79f6adee9e2606c9d855949afd39cf5a13a650cb55a3d74184c7edaf956d - ee4e79f6adee9e2606c9d855949afd39cf5a13a650cb55a3d74184c7edaf956d - ee4e79f6adee9e2606c9d855949afd39cf5a13a650cb55a3d74184c7edaf956d - ee4e79f6adee9e2606c9d855949afd39cf5a13a650cb55a3d74184c7edaf956d - ```
1 parent 312d45a commit c97f9dd

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

diff/diff.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ func Manifests(oldIndex, newIndex map[string]*manifest.MappingResult, suppressed
2323
report.setupReportFormat(output)
2424
seenAnyChanges := false
2525
emptyMapping := &manifest.MappingResult{}
26-
for key, oldContent := range oldIndex {
26+
for _, key := range sortedKeys(oldIndex) {
27+
oldContent := oldIndex[key]
28+
2729
if newContent, ok := newIndex[key]; ok {
2830
if oldContent.Content != newContent.Content {
2931
// modified
@@ -51,7 +53,9 @@ func Manifests(oldIndex, newIndex map[string]*manifest.MappingResult, suppressed
5153
}
5254
}
5355

54-
for key, newContent := range newIndex {
56+
for _, key := range sortedKeys(newIndex) {
57+
newContent := newIndex[key]
58+
5559
if _, ok := oldIndex[key]; !ok {
5660
// added
5761
if !showSecrets {
@@ -262,3 +266,15 @@ func reIndexForRelease(index map[string]*manifest.MappingResult) map[string]*man
262266
}
263267
return newIndex
264268
}
269+
270+
func sortedKeys(manifests map[string]*manifest.MappingResult) []string {
271+
var keys []string
272+
273+
for key := range manifests {
274+
keys = append(keys, key)
275+
}
276+
277+
sort.Strings(keys)
278+
279+
return keys
280+
}

0 commit comments

Comments
 (0)