From dce9d88c25b9760a2cca7c44b0f5fa4507e0f702 Mon Sep 17 00:00:00 2001 From: Sushil Kumar Date: Tue, 2 May 2017 16:39:51 -0700 Subject: [PATCH] Added tests for --repo flag for helm fetch command - [ ] Added tests for --repo flag on `helm fetch` command - [ ] Also added tests for (pkg/repo).FindChartInRepoURL --- cmd/helm/fetch_test.go | 31 +++++++- pkg/repo/chartrepo_test.go | 83 ++++++++++++++++++++ pkg/repo/index_test.go | 12 +-- pkg/repo/local_test.go | 6 +- pkg/repo/testdata/local-index-unordered.yaml | 9 +++ pkg/repo/testdata/local-index.yaml | 9 +++ 6 files changed, 136 insertions(+), 14 deletions(-) diff --git a/cmd/helm/fetch_test.go b/cmd/helm/fetch_test.go index 8c1f2a74b..3958a6e25 100644 --- a/cmd/helm/fetch_test.go +++ b/cmd/helm/fetch_test.go @@ -40,6 +40,8 @@ func TestFetchCmd(t *testing.T) { settings.Home = old os.RemoveAll(hh.String()) }() + srv := repotest.NewServer(hh.String()) + defer srv.Stop() // all flags will get "--home=TMDIR -d outdir" appended. tests := []struct { @@ -105,11 +107,34 @@ func TestFetchCmd(t *testing.T) { expectDir: true, expectVerify: true, }, + { + name: "Chart fetch using repo URL", + chart: "signtest", + expectFile: "./signtest-0.1.0.tgz", + flags: []string{"--repo", srv.URL()}, + }, + { + name: "Fail fetching non-existent chart on repo URL", + chart: "someChart", + flags: []string{"--repo", srv.URL()}, + failExpect: "Failed to fetch chart", + fail: true, + }, + { + name: "Specific version chart fetch using repo URL", + chart: "signtest", + expectFile: "./signtest-0.1.0.tgz", + flags: []string{"--repo", srv.URL(), "--version", "0.1.0"}, + }, + { + name: "Specific version chart fetch using repo URL", + chart: "signtest", + flags: []string{"--repo", srv.URL(), "--version", "0.2.0"}, + failExpect: "Failed to fetch chart version", + fail: true, + }, } - srv := repotest.NewServer(hh.String()) - defer srv.Stop() - if _, err := srv.CopyCharts("testdata/testcharts/*.tgz*"); err != nil { t.Fatal(err) } diff --git a/pkg/repo/chartrepo_test.go b/pkg/repo/chartrepo_test.go index d28ba605d..cae3c41db 100644 --- a/pkg/repo/chartrepo_test.go +++ b/pkg/repo/chartrepo_test.go @@ -17,9 +17,13 @@ limitations under the License. package repo import ( + "io/ioutil" + "net/http" + "net/http/httptest" "os" "path/filepath" "reflect" + "strings" "testing" "time" @@ -185,3 +189,82 @@ func verifyIndex(t *testing.T, actual *IndexFile) { } } } + +// StartLocalServerForTests Start the local helm server +func StartLocalServerForTests(handler http.Handler) (*httptest.Server, error) { + if handler == nil { + fileBytes, err := ioutil.ReadFile("testdata/local-index.yaml") + if err != nil { + return nil, err + } + handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Write(fileBytes) + }) + } + + return httptest.NewServer(handler), nil +} + +func TestFindChartInRepoURL(t *testing.T) { + srv, err := StartLocalServerForTests(nil) + if err != nil { + t.Fatal(err) + } + defer srv.Close() + + chartURL, err := FindChartInRepoURL(srv.URL, "nginx", "", "", "", "", getter.All(environment.EnvSettings{})) + if err != nil { + t.Errorf("%s", err) + } + if chartURL != "https://kubernetes-charts.storage.googleapis.com/nginx-0.2.0.tgz" { + t.Errorf("%s is not the valid URL", chartURL) + } + + chartURL, err = FindChartInRepoURL(srv.URL, "nginx", "0.1.0", "", "", "", getter.All(environment.EnvSettings{})) + if err != nil { + t.Errorf("%s", err) + } + if chartURL != "https://kubernetes-charts.storage.googleapis.com/nginx-0.1.0.tgz" { + t.Errorf("%s is not the valid URL", chartURL) + } +} + +func TestErrorFindChartInRepoURL(t *testing.T) { + _, err := FindChartInRepoURL("http://someserver/something", "nginx", "", "", "", "", getter.All(environment.EnvSettings{})) + if err == nil { + t.Errorf("Expected error for bad chart URL, but did not get any errors") + } + if err != nil && !strings.Contains(err.Error(), `Looks like "http://someserver/something" is not a valid chart repository or cannot be reached: Get http://someserver/something/index.yaml`) { + t.Errorf("Expected error for bad chart URL, but got a different error (%v)", err) + } + + srv, err := StartLocalServerForTests(nil) + if err != nil { + t.Fatal(err) + } + defer srv.Close() + + _, err = FindChartInRepoURL(srv.URL, "nginx1", "", "", "", "", getter.All(environment.EnvSettings{})) + if err == nil { + t.Errorf("Expected error for chart not found, but did not get any errors") + } + if err != nil && err.Error() != `chart "nginx1" not found in `+srv.URL+` repository` { + t.Errorf("Expected error for chart not found, but got a different error (%v)", err) + } + + _, err = FindChartInRepoURL(srv.URL, "nginx1", "0.1.0", "", "", "", getter.All(environment.EnvSettings{})) + if err == nil { + t.Errorf("Expected error for chart not found, but did not get any errors") + } + if err != nil && err.Error() != `chart "nginx1" version "0.1.0" not found in `+srv.URL+` repository` { + t.Errorf("Expected error for chart not found, but got a different error (%v)", err) + } + + _, err = FindChartInRepoURL(srv.URL, "chartWithNoURL", "", "", "", "", getter.All(environment.EnvSettings{})) + if err == nil { + t.Errorf("Expected error for no chart URLs available, but did not get any errors") + } + if err != nil && err.Error() != `chart "chartWithNoURL" has no downloadable URLs` { + t.Errorf("Expected error for chart not found, but got a different error (%v)", err) + } +} diff --git a/pkg/repo/index_test.go b/pkg/repo/index_test.go index 0072caf3b..61ce5a434 100644 --- a/pkg/repo/index_test.go +++ b/pkg/repo/index_test.go @@ -18,8 +18,6 @@ package repo import ( "io/ioutil" - "net/http" - "net/http/httptest" "os" "path/filepath" "testing" @@ -131,14 +129,10 @@ func TestMerge(t *testing.T) { } func TestDownloadIndexFile(t *testing.T) { - fileBytes, err := ioutil.ReadFile("testdata/local-index.yaml") + srv, err := StartLocalServerForTests(nil) if err != nil { t.Fatal(err) } - - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.Write(fileBytes) - })) defer srv.Close() dirName, err := ioutil.TempDir("", "tmp") @@ -181,8 +175,8 @@ func TestDownloadIndexFile(t *testing.T) { func verifyLocalIndex(t *testing.T, i *IndexFile) { numEntries := len(i.Entries) - if numEntries != 2 { - t.Errorf("Expected 2 entries in index file but got %d", numEntries) + if numEntries != 3 { + t.Errorf("Expected 3 entries in index file but got %d", numEntries) } alpine, ok := i.Entries["alpine"] diff --git a/pkg/repo/local_test.go b/pkg/repo/local_test.go index 3e2ecbbcf..9d26244f4 100644 --- a/pkg/repo/local_test.go +++ b/pkg/repo/local_test.go @@ -19,7 +19,6 @@ package repo import ( "io/ioutil" "net/http" - "net/http/httptest" "strings" "testing" ) @@ -43,7 +42,10 @@ func TestRepositoryServer(t *testing.T) { } s := &RepositoryServer{RepoPath: "testdata/server"} - srv := httptest.NewServer(s) + srv, err := StartLocalServerForTests(s) + if err != nil { + t.Fatal(err) + } defer srv.Close() for _, tt := range tests { diff --git a/pkg/repo/testdata/local-index-unordered.yaml b/pkg/repo/testdata/local-index-unordered.yaml index ec529f110..7482baaae 100644 --- a/pkg/repo/testdata/local-index-unordered.yaml +++ b/pkg/repo/testdata/local-index-unordered.yaml @@ -37,3 +37,12 @@ entries: - small - sumtin digest: "sha256:1234567890abcdef" + chartWithNoURL: + - name: chartWithNoURL + description: string + version: 1.0.0 + home: https://github.com/something + keywords: + - small + - sumtin + digest: "sha256:1234567890abcdef" diff --git a/pkg/repo/testdata/local-index.yaml b/pkg/repo/testdata/local-index.yaml index f64c54c1b..e680d2a3e 100644 --- a/pkg/repo/testdata/local-index.yaml +++ b/pkg/repo/testdata/local-index.yaml @@ -37,3 +37,12 @@ entries: - small - sumtin digest: "sha256:1234567890abcdef" + chartWithNoURL: + - name: chartWithNoURL + description: string + version: 1.0.0 + home: https://github.com/something + keywords: + - small + - sumtin + digest: "sha256:1234567890abcdef"