From a005c40dfe15c2b4a0e96c95778235cc09e54981 Mon Sep 17 00:00:00 2001 From: Himanshu Pandey Date: Thu, 21 Feb 2019 21:28:42 -0800 Subject: [PATCH 1/3] Signed-off-by: Himanshu Pandey Added check for empty charts --- cmd/chart-repo/utils.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cmd/chart-repo/utils.go b/cmd/chart-repo/utils.go index fb7db55f9..396854521 100644 --- a/cmd/chart-repo/utils.go +++ b/cmd/chart-repo/utils.go @@ -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") + return nil + } err = importCharts(dbSession, charts) if err != nil { return err From 5355db7ac5648d89e6291645467117c37c53cc02 Mon Sep 17 00:00:00 2001 From: Himanshu Pandey Date: Fri, 1 Mar 2019 00:20:14 -0800 Subject: [PATCH 2/3] Added Test case for Empty Charts Signed-off-by: Himanshu Pandey --- cmd/chart-repo/testdata/empty-repo-index.yaml | 1 + cmd/chart-repo/utils.go | 2 +- cmd/chart-repo/utils_test.go | 37 +++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 cmd/chart-repo/testdata/empty-repo-index.yaml diff --git a/cmd/chart-repo/testdata/empty-repo-index.yaml b/cmd/chart-repo/testdata/empty-repo-index.yaml new file mode 100644 index 000000000..9e608aa03 --- /dev/null +++ b/cmd/chart-repo/testdata/empty-repo-index.yaml @@ -0,0 +1 @@ +entries: \ No newline at end of file diff --git a/cmd/chart-repo/utils.go b/cmd/chart-repo/utils.go index 396854521..113e95904 100644 --- a/cmd/chart-repo/utils.go +++ b/cmd/chart-repo/utils.go @@ -99,7 +99,7 @@ func syncRepo(dbSession datastore.Session, repoName, repoURL string, authorizati charts := chartsFromIndex(index, r) if len(charts) == 0 { - log.Fatal("No charts are found") + errors.New("no charts in repository index") return nil } err = importCharts(dbSession, charts) diff --git a/cmd/chart-repo/utils_test.go b/cmd/chart-repo/utils_test.go index 3565699f2..a2c685080 100644 --- a/cmd/chart-repo/utils_test.go +++ b/cmd/chart-repo/utils_test.go @@ -73,6 +73,24 @@ func (h *goodHTTPClient) Do(req *http.Request) (*http.Response, error) { return w.Result(), nil } +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 +} + type authenticatedHTTPClient struct{} func (h *authenticatedHTTPClient) Do(req *http.Request) (*http.Response, error) { @@ -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{} + _, err := fetchRepoIndex(repo{URL: "https://my.examplerepo.com"}) + assert.ExistsErr(t, err, "failed request") +} From 895e3d6ffd2f6f1fa7d4c10723ce9b90f9866bc7 Mon Sep 17 00:00:00 2001 From: Himanshu Pandey Date: Fri, 1 Mar 2019 10:56:31 -0800 Subject: [PATCH 3/3] Corrected the test case Signed-off-by: Himanshu Pandey --- cmd/chart-repo/utils.go | 3 +-- cmd/chart-repo/utils_test.go | 32 +++++++------------------------- 2 files changed, 8 insertions(+), 27 deletions(-) diff --git a/cmd/chart-repo/utils.go b/cmd/chart-repo/utils.go index 113e95904..969eae11f 100644 --- a/cmd/chart-repo/utils.go +++ b/cmd/chart-repo/utils.go @@ -99,8 +99,7 @@ func syncRepo(dbSession datastore.Session, repoName, repoURL string, authorizati charts := chartsFromIndex(index, r) if len(charts) == 0 { - errors.New("no charts in repository index") - return nil + return errors.New("no charts in repository index") } err = importCharts(dbSession, charts) if err != nil { diff --git a/cmd/chart-repo/utils_test.go b/cmd/chart-repo/utils_test.go index a2c685080..e70ba4c97 100644 --- a/cmd/chart-repo/utils_test.go +++ b/cmd/chart-repo/utils_test.go @@ -73,24 +73,6 @@ func (h *goodHTTPClient) Do(req *http.Request) (*http.Response, error) { return w.Result(), nil } -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 -} - type authenticatedHTTPClient struct{} func (h *authenticatedHTTPClient) Do(req *http.Request) (*http.Response, error) { @@ -594,16 +576,16 @@ var emptyRepoIndexYAML = string(emptyRepoIndexYAMLBytes) type emptyChartRepoHTTPClient struct{} - -func (h *emptyChartRepoHTTPClient) Do(req *http.Request) (*http.Response, error){ +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{} - _, err := fetchRepoIndex(repo{URL: "https://my.examplerepo.com"}) - assert.ExistsErr(t, err, "failed request") + netClient = &emptyChartRepoHTTPClient{} + m := mock.Mock{} + dbSession := mockstore.NewMockSession(&m) + err := syncRepo(dbSession, "testRepo", "https://my.examplerepo.com", "") + assert.ExistsErr(t, err, "Failed Request") }