-
Notifications
You must be signed in to change notification settings - Fork 220
Added check for empty charts in chart-repo #604
Conversation
Added check for empty charts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks so much for picking this up!
It would also be great if we could add a test for this. I think we'd just need to add an HTTP client or extend the existing one (
monocular/cmd/chart-repo/utils_test.go
Lines 58 to 74 in e19c1b1
type goodHTTPClient struct{} | |
func (h *goodHTTPClient) 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([]byte(validRepoIndexYAML)) | |
return w.Result(), nil | |
} |
testdata/
monocular/cmd/chart-repo/utils_test.go
Lines 45 to 46 in e19c1b1
var validRepoIndexYAMLBytes, _ = ioutil.ReadFile("testdata/valid-index.yaml") | |
var validRepoIndexYAML = string(validRepoIndexYAMLBytes) |
cmd/chart-repo/utils.go
Outdated
@@ -98,6 +98,10 @@ func syncRepo(dbSession datastore.Session, repoName, repoURL string, authorizati | |||
} | |||
|
|||
charts := chartsFromIndex(index, r) | |||
if len(charts) == 0 { | |||
log.Fatal("No charts are found") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can I suggest simply returning an errors.New("no charts in repository index")
, as the caller of this function will log the error with Fatal:
monocular/cmd/chart-repo/sync.go
Line 64 in e19c1b1
if err = syncRepo(dbSession, args[0], args[1], authorizationHeader); err != nil { |
The issue with using Fatal here is that it will exit the program here, and that should really be the job of the caller of this function. This function should just return a response and not control the flow of the program.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@prydonius Thanks for explaining.
Do you mean to add a test something like this:
type chartHTTPClient struct{}
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
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hpandeycodeit I think we can remove the trailing slashes and subpath parts in that, and just return an empty index. We should also call this something like emptyChartRepoHTTPClient
to make the intent more clear.
I'm also not sure if w.Write(nil)
will be sufficient though, we might need to create a test index file (like
entries: |
entries
key that is empty.
e.g.:
type emptyChartRepoHTTPClient struct{}
func (h *emptyChartRepoHTTPClient) Do(req *http.Request) (*http.Response, error){
w := httptest.NewRecorder()
w.Write([]byte(emptyRepoIndexYAML))
return w.Result(), nil
}
We would then need a test that is similar to
monocular/cmd/chart-repo/utils_test.go
Line 157 in e19c1b1
func Test_syncURLInvalidity(t *testing.T) { |
netClient
like monocular/cmd/chart-repo/utils_test.go
Line 202 in e19c1b1
netClient = &badHTTPClient{} |
Does that make sense?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@prydonius Yes, it does. I have added the test case. Let me know if it looks okay to you.
:)
Signed-off-by: Himanshu Pandey <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good, just a few comments. Thanks!
cmd/chart-repo/utils.go
Outdated
@@ -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") |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
cmd/chart-repo/utils_test.go
Outdated
@@ -73,6 +73,24 @@ func (h *goodHTTPClient) Do(req *http.Request) (*http.Response, error) { | |||
return w.Result(), nil | |||
} | |||
|
|||
type chartHTTPClient struct{} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
cmd/chart-repo/utils_test.go
Outdated
|
||
func Test_emptyChartRepo(t *testing.T) { | ||
netClient = &badHTTPClient{} | ||
_, err := fetchRepoIndex(repo{URL: "https://my.examplerepo.com"}) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
cmd/chart-repo/utils_test.go
Outdated
|
||
|
||
func Test_emptyChartRepo(t *testing.T) { | ||
netClient = &badHTTPClient{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
netClient = &badHTTPClient{} | |
netClient = &emptyChartRepoHTTPClient{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is great! 2 more things before we can merge this:
- run gofmt on your changes: https://golang.org/cmd/gofmt/
- sign-off commits - see https://github.com/helm/monocular/pull/604/checks
Signed-off-by: Himanshu Pandey <[email protected]>
Thanks @hpandeycodeit! |
* Signed-off-by: Himanshu Pandey <[email protected]> Added check for empty charts * Added Test case for Empty Charts Signed-off-by: Himanshu Pandey <[email protected]> * Corrected the test case Signed-off-by: Himanshu Pandey <[email protected]>
Thank you @prydonius! |
Added check for empty charts : Issue #603