Skip to content
This repository has been archived by the owner on Nov 17, 2021. It is now read-only.

Commit

Permalink
Ignore file path and use all files for hash
Browse files Browse the repository at this point in the history
  • Loading branch information
snorwin committed Mar 25, 2021
1 parent 133f0e6 commit 53c87df
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
9 changes: 5 additions & 4 deletions pkg/utils/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"hash/fnv"
"path/filepath"
"sort"

helm "helm.sh/helm/v3/pkg/chart"
Expand All @@ -15,16 +16,16 @@ func Hash(chart *helm.Chart, values chartutil.Values) string {
algorithm := fnv.New64()

// copy and sort the helm chart's files
files := make([]*helm.File, len(chart.Files))
copy(files, chart.Files)
files := make([]*helm.File, len(chart.Raw))
copy(files, chart.Raw)
sort.Slice(files, func(i, j int) bool {
return files[i] == nil || files[j] == nil || files[i].Name > files[j].Name
return files[i] == nil || files[j] == nil || filepath.Base(files[i].Name) > filepath.Base(files[j].Name)
})

// add files to the hash
for _, file := range files {
if file != nil {
_, _ = algorithm.Write([]byte(file.Name))
_, _ = algorithm.Write([]byte(filepath.Base(file.Name)))
_, _ = algorithm.Write(file.Data)
}
}
Expand Down
30 changes: 15 additions & 15 deletions pkg/utils/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var _ = Describe("Hash", func() {
Context("Hash", func() {
It("should_be_idempotent", func() {
chart := &chart.Chart{
Files: []*chart.File{
Raw: []*chart.File{
{
Name: "file1.yaml",
Data: []byte{1, 1, 1, 1},
Expand All @@ -41,36 +41,36 @@ var _ = Describe("Hash", func() {
ShouldNot(Equal(utils.Hash(&chart.Chart{}, chartutil.Values{"key1": "value1", "key2": "value2", "key3": "changed"})))
})
It("should_be_different_if_chart_files_are_different", func() {
Ω(utils.Hash(&chart.Chart{Files: []*chart.File{{Name: "file1.yaml", Data: []byte{1, 1, 1, 1}}}}, chartutil.Values{})).
ShouldNot(Equal(utils.Hash(&chart.Chart{Files: []*chart.File{{Name: "file1.yaml", Data: []byte{1, 1, 1}}}}, chartutil.Values{})))
Ω(utils.Hash(&chart.Chart{Raw: []*chart.File{{Name: "file1.yaml", Data: []byte{1, 1, 1, 1}}}}, chartutil.Values{})).
ShouldNot(Equal(utils.Hash(&chart.Chart{Raw: []*chart.File{{Name: "file1.yaml", Data: []byte{1, 1, 1}}}}, chartutil.Values{})))

Ω(utils.Hash(&chart.Chart{Files: []*chart.File{{Name: "file1.yaml", Data: []byte{1, 1, 1, 1}}}}, chartutil.Values{})).
ShouldNot(Equal(utils.Hash(&chart.Chart{Files: []*chart.File{{Name: "file2.yaml", Data: []byte{1, 1, 1, 1}}}}, chartutil.Values{})))
Ω(utils.Hash(&chart.Chart{Raw: []*chart.File{{Name: "file1.yaml", Data: []byte{1, 1, 1, 1}}}}, chartutil.Values{})).
ShouldNot(Equal(utils.Hash(&chart.Chart{Raw: []*chart.File{{Name: "file2.yaml", Data: []byte{1, 1, 1, 1}}}}, chartutil.Values{})))
})
It("should_ignore_file_order_and_nil", func() {
It("should_ignore_file_order_and_full_path_and_nil", func() {
chart1 := &chart.Chart{
Files: []*chart.File{
Raw: []*chart.File{
{
Name: "file1.yaml",
Name: "..1111/file1.yaml",
Data: []byte{1, 1, 1, 1},
},
{
Name: "file2.yaml",
Name: "..1111/file2.yaml",
Data: []byte{2, 2, 2, 2},
},
nil,
},
}

chart2 := &chart.Chart{
Files: []*chart.File{
Raw: []*chart.File{
{
Name: "file2.yaml",
Name: "..2222/file2.yaml",
Data: []byte{2, 2, 2, 2},
},
nil,
{
Name: "file1.yaml",
Name: "..2222/file1.yaml",
Data: []byte{1, 1, 1, 1},
},
nil,
Expand All @@ -96,7 +96,7 @@ var _ = Describe("Hash", func() {
})
It("should_not_modify_file_order", func() {
expected := &chart.Chart{
Files: []*chart.File{
Raw: []*chart.File{
{
Name: "zzzz",
Data: []byte{1, 1, 1, 1},
Expand All @@ -112,7 +112,7 @@ var _ = Describe("Hash", func() {
},
}
actual := &chart.Chart{
Files: []*chart.File{
Raw: []*chart.File{
{
Name: "zzzz",
Data: []byte{1, 1, 1, 1},
Expand All @@ -130,7 +130,7 @@ var _ = Describe("Hash", func() {

utils.Hash(actual, chartutil.Values{})

Ω(actual.Files).Should(Equal(expected.Files))
Ω(actual.Raw).Should(Equal(expected.Raw))
})
})
})

0 comments on commit 53c87df

Please sign in to comment.