From d46f7bc2ca9b160e9a7ddf51f56be3a77959ee1c Mon Sep 17 00:00:00 2001 From: Martin Hickey Date: Fri, 18 Sep 2020 10:35:54 +0000 Subject: [PATCH] Fix for issue 8761 Signed-off-by: Martin Hickey --- pkg/repo/index.go | 3 + pkg/repo/index_test.go | 85 ++++++++++++++++-------- pkg/repo/testdata/chartmuseum-index.yaml | 50 ++++++++++++++ 3 files changed, 112 insertions(+), 26 deletions(-) create mode 100644 pkg/repo/testdata/chartmuseum-index.yaml diff --git a/pkg/repo/index.go b/pkg/repo/index.go index 1fcb7d9cb..edeef99c1 100644 --- a/pkg/repo/index.go +++ b/pkg/repo/index.go @@ -86,6 +86,9 @@ type IndexFile struct { // IndexValidation is used to validate the integrity of an index file type IndexValidation struct { + // This is used ONLY for validation against chartmuseum's index files and + // is discarded after validation. + ServerInfo map[string]interface{} `yaml:"serverInfo,omitempty"` APIVersion string `yaml:"apiVersion"` Generated time.Time `yaml:"generated"` Entries map[string]interface{} `yaml:"entries"` diff --git a/pkg/repo/index_test.go b/pkg/repo/index_test.go index 425de70ef..198ac3409 100644 --- a/pkg/repo/index_test.go +++ b/pkg/repo/index_test.go @@ -30,9 +30,31 @@ import ( ) const ( - testfile = "testdata/local-index.yaml" - unorderedTestfile = "testdata/local-index-unordered.yaml" - testRepo = "test-repo" + testfile = "testdata/local-index.yaml" + chartmuseumtestfile = "testdata/chartmuseum-index.yaml" + unorderedTestfile = "testdata/local-index-unordered.yaml" + testRepo = "test-repo" + indexWithDuplicates = ` +apiVersion: v1 +entries: + nginx: + - urls: + - https://kubernetes-charts.storage.googleapis.com/nginx-0.2.0.tgz + name: nginx + description: string + version: 0.2.0 + home: https://github.com/something/else + digest: "sha256:1234567890abcdef" + nginx: + - urls: + - https://kubernetes-charts.storage.googleapis.com/alpine-1.0.0.tgz + - http://storage2.googleapis.com/kubernetes-charts/alpine-1.0.0.tgz + name: alpine + description: string + version: 1.0.0 + home: https://github.com/something + digest: "sha256:1234567890abcdef" +` ) func TestIndexFile(t *testing.T) { @@ -79,15 +101,42 @@ func TestIndexFile(t *testing.T) { } func TestLoadIndex(t *testing.T) { - b, err := ioutil.ReadFile(testfile) - if err != nil { - t.Fatal(err) + tests := []struct { + Name string + Filename string + }{ + { + Name: "regular index file", + Filename: testfile, + }, + { + Name: "chartmuseum index file", + Filename: chartmuseumtestfile, + }, } - i, err := loadIndex(b) - if err != nil { - t.Fatal(err) + + for _, tc := range tests { + tc := tc + t.Run(tc.Name, func(t *testing.T) { + t.Parallel() + b, err := ioutil.ReadFile(tc.Filename) + if err != nil { + t.Fatal(err) + } + i, err := loadIndex(b) + if err != nil { + t.Fatal(err) + } + verifyLocalIndex(t, i) + }) + } +} + +// TestLoadIndex_Duplicates is a regression to make sure that we don't non-deterministically allow duplicate packages. +func TestLoadIndex_Duplicates(t *testing.T) { + if _, err := loadIndex([]byte(indexWithDuplicates)); err == nil { + t.Errorf("Expected an error when duplicate entries are present") } - verifyLocalIndex(t, i) } func TestLoadIndexFile(t *testing.T) { @@ -422,19 +471,3 @@ func TestIndexAdd(t *testing.T) { t.Errorf("Expected http://example.com/charts/deis-0.1.0.tgz, got %s", i.Entries["deis"][0].URLs[0]) } } - -const mockDuplicateIndex = ` -entries: - foo: {} - bar: {} - baz: {} - bar: {} -` - -func TestValidateIndex(t *testing.T) { - expect := `key "bar" already set in map` - err := validateIndex([]byte(mockDuplicateIndex)) - if strings.Contains(expect, err.Error()) { - t.Errorf("Unexpected error: %s", err) - } -} diff --git a/pkg/repo/testdata/chartmuseum-index.yaml b/pkg/repo/testdata/chartmuseum-index.yaml new file mode 100644 index 000000000..3077596f4 --- /dev/null +++ b/pkg/repo/testdata/chartmuseum-index.yaml @@ -0,0 +1,50 @@ +serverInfo: + contextPath: /v1/helm +apiVersion: v1 +entries: + nginx: + - urls: + - https://kubernetes-charts.storage.googleapis.com/nginx-0.2.0.tgz + name: nginx + description: string + version: 0.2.0 + home: https://github.com/something/else + digest: "sha256:1234567890abcdef" + keywords: + - popular + - web server + - proxy + - urls: + - https://kubernetes-charts.storage.googleapis.com/nginx-0.1.0.tgz + name: nginx + description: string + version: 0.1.0 + home: https://github.com/something + digest: "sha256:1234567890abcdef" + keywords: + - popular + - web server + - proxy + alpine: + - urls: + - https://kubernetes-charts.storage.googleapis.com/alpine-1.0.0.tgz + - http://storage2.googleapis.com/kubernetes-charts/alpine-1.0.0.tgz + name: alpine + description: string + version: 1.0.0 + home: https://github.com/something + keywords: + - linux + - alpine + - small + - sumtin + digest: "sha256:1234567890abcdef" + chartWithNoURL: + - name: chartWithNoURL + description: string + version: 1.0.0 + home: https://github.com/something + keywords: + - small + - sumtin + digest: "sha256:1234567890abcdef"