Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

Added check for empty charts in chart-repo #604

Merged
merged 3 commits into from
Mar 7, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/chart-repo/testdata/empty-repo-index.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
entries:
4 changes: 4 additions & 0 deletions cmd/chart-repo/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ func syncRepo(dbSession datastore.Session, repoName, repoURL string, authorizati
}

charts := chartsFromIndex(index, r)
if len(charts) == 0 {
errors.New("no charts in repository index")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need to return the error so that the caller can exit appropriately

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return nil
}
err = importCharts(dbSession, charts)
if err != nil {
return err
Expand Down
37 changes: 37 additions & 0 deletions cmd/chart-repo/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ func (h *goodHTTPClient) Do(req *http.Request) (*http.Response, error) {
return w.Result(), nil
}

type chartHTTPClient struct{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can remove this one and the Do below now that we have the emptyChartRepoHTTPClient?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like I forgot to remove this one. :)
Removed this one now.


func (h *chartHTTPClient) Do(req *http.Request) (*http.Response, error){
w := httptest.NewRecorder()
// Don't accept trailing slashes
if strings.HasPrefix(req.URL.Path, "//") {
w.WriteHeader(500)
}
// If subpath repo URL test, check that index.yaml is correctly added to the
// subpath
if req.URL.Host == "subpath.test" && req.URL.Path != "/subpath/index.yaml" {
w.WriteHeader(500)
}

w.Write(nil)
return w.Result(), nil
}

type authenticatedHTTPClient struct{}

func (h *authenticatedHTTPClient) Do(req *http.Request) (*http.Response, error) {
Expand Down Expand Up @@ -570,3 +588,22 @@ h251U/Daz6NiQBM9AxyAw6EHm8XAZBvCuebfzyrT
t.Error(err)
}
}

var emptyRepoIndexYAMLBytes, _ = ioutil.ReadFile("testdata/empty-repo-index.yaml")
var emptyRepoIndexYAML = string(emptyRepoIndexYAMLBytes)

type emptyChartRepoHTTPClient struct{}


func (h *emptyChartRepoHTTPClient) Do(req *http.Request) (*http.Response, error){
w := httptest.NewRecorder()
w.Write([]byte(emptyRepoIndexYAML))
return w.Result(), nil
}


func Test_emptyChartRepo(t *testing.T) {
netClient = &badHTTPClient{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
netClient = &badHTTPClient{}
netClient = &emptyChartRepoHTTPClient{}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

_, err := fetchRepoIndex(repo{URL: "https://my.examplerepo.com"})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we actually need to call syncRepo, as this is where the error for an empty repo is found

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

assert.ExistsErr(t, err, "failed request")
}