fix repo url being decoded while downloading repo index (#6060)

Signed-off-by: Karuppiah Natarajan <karuppiah7890@gmail.com>
pull/6590/head
Karuppiah Natarajan 6 years ago committed by Martin Hickey
parent fb2cbb0019
commit 519ccac294

@ -21,6 +21,7 @@ import (
"io/ioutil" "io/ioutil"
"net/url" "net/url"
"os" "os"
"path"
"path/filepath" "path/filepath"
"strings" "strings"
@ -111,14 +112,13 @@ func (r *ChartRepository) Load() error {
// cachePath is prepended to any index that does not have an absolute path. This // cachePath is prepended to any index that does not have an absolute path. This
// is for pre-2.2.0 repo files. // is for pre-2.2.0 repo files.
func (r *ChartRepository) DownloadIndexFile(cachePath string) error { func (r *ChartRepository) DownloadIndexFile(cachePath string) error {
var indexURL string
parsedURL, err := url.Parse(r.Config.URL) parsedURL, err := url.Parse(r.Config.URL)
if err != nil { if err != nil {
return err return err
} }
parsedURL.Path = strings.TrimSuffix(parsedURL.Path, "/") + "/index.yaml" parsedURL.RawPath = path.Join(parsedURL.RawPath, "index.yaml")
parsedURL.Path = path.Join(parsedURL.Path, "index.yaml")
indexURL = parsedURL.String() indexURL := parsedURL.String()
r.setCredentials() r.setCredentials()
resp, err := r.Client.Get(indexURL) resp, err := r.Client.Get(indexURL)

@ -18,6 +18,7 @@ package repo
import ( import (
"io/ioutil" "io/ioutil"
"net/http"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -143,48 +144,105 @@ func TestMerge(t *testing.T) {
} }
func TestDownloadIndexFile(t *testing.T) { func TestDownloadIndexFile(t *testing.T) {
srv, err := startLocalServerForTests(nil) t.Run("should download index file", func(t *testing.T) {
if err != nil { srv, err := startLocalServerForTests(nil)
t.Fatal(err) if err != nil {
} t.Fatal(err)
defer srv.Close() }
defer srv.Close()
dirName, err := ioutil.TempDir("", "tmp") dirName, err := ioutil.TempDir("", "tmp")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer os.RemoveAll(dirName) defer os.RemoveAll(dirName)
indexFilePath := filepath.Join(dirName, testRepo+"-index.yaml") indexFilePath := filepath.Join(dirName, testRepo+"-index.yaml")
r, err := NewChartRepository(&Entry{ r, err := NewChartRepository(&Entry{
Name: testRepo, Name: testRepo,
URL: srv.URL, URL: srv.URL,
Cache: indexFilePath, Cache: indexFilePath,
}, getter.All(environment.EnvSettings{})) }, getter.All(environment.EnvSettings{}))
if err != nil { if err != nil {
t.Errorf("Problem creating chart repository from %s: %v", testRepo, err) t.Errorf("Problem creating chart repository from %s: %v", testRepo, err)
} }
if err := r.DownloadIndexFile(""); err != nil { if err := r.DownloadIndexFile(""); err != nil {
t.Errorf("%#v", err) t.Errorf("%#v", err)
} }
if _, err := os.Stat(indexFilePath); err != nil { if _, err := os.Stat(indexFilePath); err != nil {
t.Errorf("error finding created index file: %#v", err) t.Errorf("error finding created index file: %#v", err)
} }
b, err := ioutil.ReadFile(indexFilePath) b, err := ioutil.ReadFile(indexFilePath)
if err != nil { if err != nil {
t.Errorf("error reading index file: %#v", err) t.Errorf("error reading index file: %#v", err)
} }
i, err := loadIndex(b) i, err := loadIndex(b)
if err != nil { if err != nil {
t.Errorf("Index %q failed to parse: %s", testfile, err) t.Errorf("Index %q failed to parse: %s", testfile, err)
return return
} }
verifyLocalIndex(t, i) verifyLocalIndex(t, i)
})
t.Run("should not decode the path in the repo url while downloading index", func(t *testing.T) {
chartRepoURLPath := "/some%2Fpath/test"
fileBytes, err := ioutil.ReadFile("testdata/local-index.yaml")
if err != nil {
t.Fatal(err)
}
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.RawPath == chartRepoURLPath+"/index.yaml" {
w.Write(fileBytes)
}
})
srv, err := startLocalServerForTests(handler)
if err != nil {
t.Fatal(err)
}
defer srv.Close()
dirName, err := ioutil.TempDir("", "tmp")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dirName)
indexFilePath := filepath.Join(dirName, testRepo+"-index.yaml")
r, err := NewChartRepository(&Entry{
Name: testRepo,
URL: srv.URL + chartRepoURLPath,
Cache: indexFilePath,
}, getter.All(environment.EnvSettings{}))
if err != nil {
t.Errorf("Problem creating chart repository from %s: %v", "testrepo", err)
}
if err := r.DownloadIndexFile(""); err != nil {
t.Errorf("%#v", err)
}
if _, err := os.Stat(indexFilePath); err != nil {
t.Errorf("error finding created index file: %#v", err)
}
b, err := ioutil.ReadFile(indexFilePath)
if err != nil {
t.Errorf("error reading index file: %#v", err)
}
i, err := loadIndex(b)
if err != nil {
t.Errorf("Index %q failed to parse: %s", testfile, err)
return
}
verifyLocalIndex(t, i)
})
} }
func verifyLocalIndex(t *testing.T, i *IndexFile) { func verifyLocalIndex(t *testing.T, i *IndexFile) {

Loading…
Cancel
Save