Commit 415e52bf authored by Amanda Cameron's avatar Amanda Cameron

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

parent a9f0be9b
......@@ -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,13 +278,20 @@ 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"
corpus := []struct{ chartName, downloadLink string }{
{"frobnitz", "http://localhost:8080/frobnitz-1.2.3.tgz"},
{"zarthal", "http://localhost:8080/universe/zarthal-1.0.0.tgz"},
}
for _, test := range corpus {
cname := test.chartName
frobs, ok := index.Entries[cname]
if !ok {
t.Fatalf("Could not read chart %s", cname)
......@@ -294,11 +301,12 @@ func TestIndexDirectory(t *testing.T) {
if len(frob.Digest) == 0 {
t.Errorf("Missing digest of file %s.", frob.Name)
}
if frob.URLs[0] != "http://localhost:8080/frobnitz-1.2.3.tgz" {
if frob.URLs[0] != test.downloadLink {
t.Errorf("Unexpected URLs: %v", frob.URLs)
}
if frob.Name != "frobnitz" {
t.Errorf("Expected frobnitz, got %q", frob.Name)
if frob.Name != cname {
t.Errorf("Expected %q, got %q", cname, frob.Name)
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment