Add support for sub-directory scanning as in issue #1401

pull/1997/head
Amanda Cameron 8 years ago
parent a9f0be9bf3
commit 415e52bf55

@ -115,6 +115,14 @@ func Save(c *chart.Chart, outDir string) (string, error) {
filename := fmt.Sprintf("%s-%s.tgz", cfile.Name, cfile.Version)
filename = filepath.Join(outDir, filename)
if stat, err := os.Stat(filepath.Dir(filename)); os.IsNotExist(err) {
if err := os.MkdirAll(filepath.Dir(filename), 0755); !os.IsExist(err) {
return "", err
}
} else if !stat.IsDir() {
return "", fmt.Errorf("is not a directory: %s", filepath.Dir(filename))
}
f, err := os.Create(filename)
if err != nil {
return "", err

@ -54,6 +54,7 @@ func TestLoadChartRepository(t *testing.T) {
filepath.Join(testRepository, "frobnitz-1.2.3.tgz"),
filepath.Join(testRepository, "sprocket-1.1.0.tgz"),
filepath.Join(testRepository, "sprocket-1.2.0.tgz"),
filepath.Join(testRepository, "universe/zarthal-1.0.0.tgz"),
}
if r.Config.Name != testRepository {
@ -118,8 +119,8 @@ func verifyIndex(t *testing.T, actual *IndexFile) {
}
entries := actual.Entries
if numEntries := len(entries); numEntries != 2 {
t.Errorf("Expected 2 charts to be listed in index file but got %v", numEntries)
if numEntries := len(entries); numEntries != 3 {
t.Errorf("Expected 3 charts to be listed in index file but got %v", numEntries)
}
expects := map[string]ChartVersions{
@ -145,6 +146,14 @@ func verifyIndex(t *testing.T, actual *IndexFile) {
},
},
},
"zarthal": {
{
Metadata: &chart.Metadata{
Name: "zarthal",
Version: "1.0.0",
},
},
},
}
for name, versions := range expects {

@ -231,9 +231,26 @@ func IndexDirectory(dir, baseURL string) (*IndexFile, error) {
if err != nil {
return nil, err
}
moreArchives, err := filepath.Glob(filepath.Join(dir, "**/*.tgz"))
if err != nil {
return nil, err
}
archives = append(archives, moreArchives...)
index := NewIndexFile()
for _, arch := range archives {
fname := filepath.Base(arch)
fname, err := filepath.Rel(dir, arch)
if err != nil {
return index, err
}
var parentDir string
parentDir, fname = filepath.Split(fname)
parentURL, err := urlutil.URLJoin(baseURL, parentDir)
if err != nil {
parentURL = filepath.Join(baseURL, parentDir)
}
c, err := chartutil.Load(arch)
if err != nil {
// Assume this is not a chart.
@ -243,7 +260,7 @@ func IndexDirectory(dir, baseURL string) (*IndexFile, error) {
if err != nil {
return index, err
}
index.Add(c.Metadata, fname, baseURL, hash)
index.Add(c.Metadata, fname, parentURL, hash)
}
return index, nil
}

@ -278,27 +278,35 @@ func TestIndexDirectory(t *testing.T) {
t.Fatal(err)
}
if l := len(index.Entries); l != 2 {
t.Fatalf("Expected 2 entries, got %d", l)
if l := len(index.Entries); l != 3 {
t.Fatalf("Expected 3 entries, got %d", l)
}
// Other things test the entry generation more thoroughly. We just test a
// few fields.
cname := "frobnitz"
frobs, ok := index.Entries[cname]
if !ok {
t.Fatalf("Could not read chart %s", cname)
}
frob := frobs[0]
if len(frob.Digest) == 0 {
t.Errorf("Missing digest of file %s.", frob.Name)
corpus := []struct{ chartName, downloadLink string }{
{"frobnitz", "http://localhost:8080/frobnitz-1.2.3.tgz"},
{"zarthal", "http://localhost:8080/universe/zarthal-1.0.0.tgz"},
}
if frob.URLs[0] != "http://localhost:8080/frobnitz-1.2.3.tgz" {
t.Errorf("Unexpected URLs: %v", frob.URLs)
}
if frob.Name != "frobnitz" {
t.Errorf("Expected frobnitz, got %q", frob.Name)
for _, test := range corpus {
cname := test.chartName
frobs, ok := index.Entries[cname]
if !ok {
t.Fatalf("Could not read chart %s", cname)
}
frob := frobs[0]
if len(frob.Digest) == 0 {
t.Errorf("Missing digest of file %s.", frob.Name)
}
if frob.URLs[0] != test.downloadLink {
t.Errorf("Unexpected URLs: %v", frob.URLs)
}
if frob.Name != cname {
t.Errorf("Expected %q, got %q", cname, frob.Name)
}
}
}

Loading…
Cancel
Save