Skip to content

Commit

Permalink
fix(repo): ensure index generates in the right order
Browse files Browse the repository at this point in the history
  • Loading branch information
Adnan Abdulhussein committed Oct 19, 2016
1 parent 313a9dc commit ee7fe7c
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 17 deletions.
1 change: 1 addition & 0 deletions cmd/helm/repo_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,6 @@ func index(dir, url, mergeTo string) error {
}
i.Merge(i2)
}
i.SortEntries()
return i.WriteFile(out, 0755)
}
36 changes: 33 additions & 3 deletions cmd/helm/repo_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func TestRepoIndexCmd(t *testing.T) {
if err := os.Link("testdata/testcharts/compressedchart-0.1.0.tgz", comp); err != nil {
t.Fatal(err)
}
comp2 := filepath.Join(dir, "compressedchart-0.2.0.tgz")
if err := os.Link("testdata/testcharts/compressedchart-0.2.0.tgz", comp2); err != nil {
t.Fatal(err)
}

buf := bytes.NewBuffer(nil)
c := newRepoIndexCmd(buf)
Expand All @@ -57,16 +61,32 @@ func TestRepoIndexCmd(t *testing.T) {
t.Errorf("expected 1 entry, got %d: %#v", len(index.Entries), index.Entries)
}

vs := index.Entries["compressedchart"]
if len(vs) != 2 {
t.Errorf("expected 2 versions, got %d: %#v", len(vs), vs)
}

expectedVersion := "0.2.0"
if vs[0].Version != expectedVersion {
t.Errorf("expected %q, got %q", expectedVersion, vs[0].Version)
}

// Test with `--merge`

// Remove first chart.
// Remove first two charts.
if err := os.Remove(comp); err != nil {
t.Fatal(err)
}
// Add another chart.
if err := os.Remove(comp2); err != nil {
t.Fatal(err)
}
// Add a new chart and a new version of an existing chart
if err := os.Link("testdata/testcharts/reqtest-0.1.0.tgz", filepath.Join(dir, "reqtest-0.1.0.tgz")); err != nil {
t.Fatal(err)
}
if err := os.Link("testdata/testcharts/compressedchart-0.3.0.tgz", filepath.Join(dir, "compressedchart-0.3.0.tgz")); err != nil {
t.Fatal(err)
}

c.ParseFlags([]string{"--merge", destIndex})
if err := c.RunE(c, []string{dir}); err != nil {
Expand All @@ -79,6 +99,16 @@ func TestRepoIndexCmd(t *testing.T) {
}

if len(index.Entries) != 2 {
t.Errorf("expected 2 entry, got %d: %#v", len(index.Entries), index.Entries)
t.Errorf("expected 2 entries, got %d: %#v", len(index.Entries), index.Entries)
}

vs = index.Entries["compressedchart"]
if len(vs) != 3 {
t.Errorf("expected 3 versions, got %d: %#v", len(vs), vs)
}

expectedVersion = "0.3.0"
if vs[0].Version != expectedVersion {
t.Errorf("expected %q, got %q", expectedVersion, vs[0].Version)
}
}
Binary file not shown.
Binary file not shown.
5 changes: 4 additions & 1 deletion pkg/repo/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func NewIndexFile() *IndexFile {
}

// Add adds a file to the index
// This can leave the index in an unsorted state
func (i IndexFile) Add(md *chart.Metadata, filename, baseURL, digest string) {
u := filename
if baseURL != "" {
Expand Down Expand Up @@ -176,6 +177,8 @@ func (i IndexFile) WriteFile(dest string, mode os.FileMode) error {
//
// If one of the entries in the given index does _not_ already exist, it is added.
// In all other cases, the existing record is preserved.
//
// This can leave the index in an unsorted state
func (i *IndexFile) Merge(f *IndexFile) {
for _, cvs := range f.Entries {
for _, cv := range cvs {
Expand All @@ -202,7 +205,7 @@ type ChartVersion struct {
//
// It indexes only charts that have been packaged (*.tgz).
//
// It writes the results to dir/index.yaml.
// The index returned will be in an unsorted state
func IndexDirectory(dir, baseURL string) (*IndexFile, error) {
archives, err := filepath.Glob(filepath.Join(dir, "*.tgz"))
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions pkg/repo/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,25 +197,25 @@ func verifyLocalIndex(t *testing.T, i *IndexFile) {
Metadata: &chart.Metadata{
Name: "nginx",
Description: "string",
Version: "0.1.0",
Version: "0.2.0",
Keywords: []string{"popular", "web server", "proxy"},
Home: "https://github.com/something",
Home: "https://github.com/something/else",
},
URLs: []string{
"http://storage.googleapis.com/kubernetes-charts/nginx-0.1.0.tgz",
"http://storage.googleapis.com/kubernetes-charts/nginx-0.2.0.tgz",
},
Digest: "sha256:1234567890abcdef",
},
{
Metadata: &chart.Metadata{
Name: "nginx",
Description: "string",
Version: "0.2.0",
Version: "0.1.0",
Keywords: []string{"popular", "web server", "proxy"},
Home: "https://github.com/something/else",
Home: "https://github.com/something",
},
URLs: []string{
"http://storage.googleapis.com/kubernetes-charts/nginx-0.2.0.tgz",
"http://storage.googleapis.com/kubernetes-charts/nginx-0.1.0.tgz",
},
Digest: "sha256:1234567890abcdef",
},
Expand Down
8 changes: 7 additions & 1 deletion pkg/repo/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func TestLoadChartRepository(t *testing.T) {
t.Errorf("Problem loading chart repository from %s: %v", testRepository, err)
}

paths := []string{filepath.Join(testRepository, "frobnitz-1.2.3.tgz"), filepath.Join(testRepository, "sprocket-1.2.0.tgz")}
paths := []string{filepath.Join(testRepository, "frobnitz-1.2.3.tgz"), filepath.Join(testRepository, "sprocket-1.1.0.tgz"), filepath.Join(testRepository, "sprocket-1.2.0.tgz")}

if cr.RootPath != testRepository {
t.Errorf("Expected %s as RootPath but got %s", testRepository, cr.RootPath)
Expand Down Expand Up @@ -211,6 +211,12 @@ func verifyIndex(t *testing.T, actual *IndexFile) {
Version: "1.2.0",
},
},
{
Metadata: &chart.Metadata{
Name: "sprocket",
Version: "1.1.0",
},
},
},
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/repo/testdata/local-index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ apiVersion: v1
entries:
nginx:
- urls:
- http://storage.googleapis.com/kubernetes-charts/nginx-0.1.0.tgz
- http://storage.googleapis.com/kubernetes-charts/nginx-0.2.0.tgz
name: nginx
description: string
version: 0.1.0
home: https://github.com/something
version: 0.2.0
home: https://github.com/something/else
digest: "sha256:1234567890abcdef"
keywords:
- popular
- web server
- proxy
- urls:
- http://storage.googleapis.com/kubernetes-charts/nginx-0.2.0.tgz
- http://storage.googleapis.com/kubernetes-charts/nginx-0.1.0.tgz
name: nginx
description: string
version: 0.2.0
home: https://github.com/something/else
version: 0.1.0
home: https://github.com/something
digest: "sha256:1234567890abcdef"
keywords:
- popular
Expand Down
Binary file added pkg/repo/testdata/repository/sprocket-1.1.0.tgz
Binary file not shown.

0 comments on commit ee7fe7c

Please sign in to comment.