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

Conversation

hpandeycodeit
Copy link
Contributor

Added check for empty charts : Issue #603

Copy link
Member

@prydonius prydonius left a 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 (

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
}
) to return an empty index that we load from testdata/
var validRepoIndexYAMLBytes, _ = ioutil.ReadFile("testdata/valid-index.yaml")
var validRepoIndexYAML = string(validRepoIndexYAMLBytes)

@@ -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")
Copy link
Member

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:

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.

Copy link
Contributor Author

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
}  

Copy link
Member

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

) that has an 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

func Test_syncURLInvalidity(t *testing.T) {
, but also sets the netClient like
netClient = &badHTTPClient{}
, and asserts than an error is returned.

Does that make sense?

Copy link
Contributor Author

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]>
Copy link
Member

@prydonius prydonius left a 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!

@@ -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

@@ -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 Test_emptyChartRepo(t *testing.T) {
netClient = &badHTTPClient{}
_, 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



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

Copy link
Member

@prydonius prydonius left a 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:

  1. run gofmt on your changes: https://golang.org/cmd/gofmt/
  2. sign-off commits - see https://github.com/helm/monocular/pull/604/checks

Signed-off-by: Himanshu Pandey <[email protected]>
@prydonius
Copy link
Member

Thanks @hpandeycodeit!

@prydonius prydonius merged commit a5df290 into helm:master Mar 7, 2019
andresmgot pushed a commit to andresmgot/monocular that referenced this pull request Mar 8, 2019
* 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]>
@hpandeycodeit
Copy link
Contributor Author

Thank you @prydonius!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants