From 04dc670522257423bfcb38b7e3cd1429dd86cb4c Mon Sep 17 00:00:00 2001 From: jackgr Date: Sun, 27 Mar 2016 16:26:03 -0700 Subject: [PATCH] Chart repo route tests --- cmd/manager/chartrepos.go | 36 ++++++++ cmd/manager/chartrepos_test.go | 147 ++++++++++++++++++++++++++++++++- pkg/repo/gcs_repo.go | 2 +- 3 files changed, 180 insertions(+), 5 deletions(-) diff --git a/cmd/manager/chartrepos.go b/cmd/manager/chartrepos.go index c94107dbc..55ea07f34 100644 --- a/cmd/manager/chartrepos.go +++ b/cmd/manager/chartrepos.go @@ -159,3 +159,39 @@ func getRepoChartHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.C util.LogHandlerExitWithJSON(handler, w, repoChart, http.StatusOK) return nil } + +func addChartRepoHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { + handler := "manager: add chart repository" + util.LogHandlerEntry(handler, r) + defer r.Body.Close() + cr := &repo.Repo{} + if err := httputil.Decode(w, r, cr); err != nil { + httputil.BadRequest(w, r, err) + return nil + } + + if err := c.Manager.AddChartRepo(cr); err != nil { + httputil.BadRequest(w, r, err) + return nil + } + + util.LogHandlerExitWithText(handler, w, "added", http.StatusOK) + return nil +} + +func removeChartRepoHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { + handler := "manager: remove chart repository" + util.LogHandlerEntry(handler, r) + URL, err := pos(w, r, 2) + if err != nil { + return err + } + + err = c.Manager.RemoveChartRepo(URL) + if err != nil { + return err + } + + util.LogHandlerExitWithText(handler, w, "removed", http.StatusOK) + return nil +} diff --git a/cmd/manager/chartrepos_test.go b/cmd/manager/chartrepos_test.go index 0eaed403a..c9df699b3 100644 --- a/cmd/manager/chartrepos_test.go +++ b/cmd/manager/chartrepos_test.go @@ -17,19 +17,158 @@ limitations under the License. package main import ( + "github.com/kubernetes/helm/pkg/repo" + + "bytes" + "encoding/json" + "fmt" + "io" "net/http" + "net/url" "testing" ) -func TestListChartRepositories(t *testing.T) { +var ( + // TestRepoURL = "gs://kubernetes-charts-testing" + TestRepoURL = "foo" + TestChartName = "frobnitz-0.0.1.tgz" + TestRepoType = string(repo.GCSRepoType) + TestRepoFormat = string(repo.GCSRepoFormat) + TestRepoCredentialName = "default" +) + +func TestListChartRepos(t *testing.T) { c := stubContext() s := httpHarness(c, "GET /repositories", listChartReposHandlerFunc) defer s.Close() - res, err := http.Get(s.URL + "/repositories") + URL := getTestURL(t, s.URL, "", "") + res, err := http.Get(URL) + if err != nil { + t.Fatalf("Failed GET: %s", err) + } + + if res.StatusCode != http.StatusOK { + t.Errorf("Expected status %d, got %d", http.StatusOK, res.StatusCode) + } +} + +func TestGetChartRepo(t *testing.T) { + c := stubContext() + s := httpHarness(c, "GET /repositories/*", getChartRepoHandlerFunc) + defer s.Close() + + URL := getTestURL(t, s.URL, url.QueryEscape(TestRepoURL), "") + res, err := http.Get(URL) + if err != nil { + t.Fatalf("Failed GET: %s", err) + } + + if res.StatusCode != http.StatusOK { + t.Errorf("Expected status %d, got %d", http.StatusOK, res.StatusCode) + } +} + +func TestListRepoCharts(t *testing.T) { + c := stubContext() + s := httpHarness(c, "GET /repositories/*/charts", listRepoChartsHandlerFunc) + defer s.Close() + + URL := getTestURL(t, s.URL, url.QueryEscape(TestRepoURL), "charts") + res, err := http.Get(URL) if err != nil { - t.Errorf("Failed GET: %s", err) - } else if res.StatusCode != http.StatusOK { + t.Fatalf("Failed GET: %s", err) + } + + if res.StatusCode != http.StatusOK { t.Errorf("Expected status %d, got %d", http.StatusOK, res.StatusCode) } } + +func TestGetRepoChart(t *testing.T) { + c := stubContext() + s := httpHarness(c, "GET /repositories/*/charts/*", getRepoChartHandlerFunc) + defer s.Close() + + chartURL := fmt.Sprintf("charts/%s", TestChartName) + URL := getTestURL(t, s.URL, url.QueryEscape(TestRepoURL), chartURL) + res, err := http.Get(URL) + if err != nil { + t.Fatalf("Failed GET: %s", err) + } + + if res.StatusCode != http.StatusOK { + t.Errorf("Expected status %d, got %d", http.StatusOK, res.StatusCode) + } +} + +func TestAddChartRepo(t *testing.T) { + c := stubContext() + s := httpHarness(c, "POST /repositories", addChartRepoHandlerFunc) + defer s.Close() + + URL := getTestURL(t, s.URL, "", "") + body := getTestRepo(t, URL) + res, err := http.Post(URL, "application/json", body) + if err != nil { + t.Fatalf("Failed POST: %s", err) + } + + if res.StatusCode != http.StatusOK { + t.Errorf("Expected status %d, got %d", http.StatusOK, res.StatusCode) + } +} + +func TestRemoveChartRepo(t *testing.T) { + c := stubContext() + s := httpHarness(c, "DELETE /repositories/*", removeChartRepoHandlerFunc) + defer s.Close() + + URL := getTestURL(t, s.URL, url.QueryEscape(TestRepoURL), "") + req, err := http.NewRequest("DELETE", URL, nil) + if err != nil { + t.Fatalf("Cannot create DELETE request: %s", err) + } + + res, err := http.DefaultClient.Do(req) + if err != nil { + t.Fatalf("Failed DELETE: %s", err) + } + + defer res.Body.Close() + if res.StatusCode != http.StatusOK { + t.Errorf("Expected status %d, got %d", http.StatusOK, res.StatusCode) + } +} + +func getTestRepo(t *testing.T, URL string) io.Reader { + tr, err := repo.NewRepo(URL, TestRepoCredentialName, TestRepoFormat, TestRepoType) + if err != nil { + t.Fatalf("Cannot create test repository: %s", err) + } + + trb, err := json.Marshal(&tr) + if err != nil { + t.Fatalf("Cannot marshal test repository: %s", err) + } + + return bytes.NewReader(trb) +} + +func getTestURL(t *testing.T, baseURL, repoURL, chartURL string) string { + URL := fmt.Sprintf("%s/repositories", baseURL) + if repoURL != "" { + URL = fmt.Sprintf("%s/%s", URL, repoURL) + } + + if chartURL != "" { + URL = fmt.Sprintf("%s/%s", URL, chartURL) + } + + u, err := url.Parse(URL) + if err != nil { + t.Fatalf("cannot parse test URL %s: %s", URL, err) + } + + return u.String() +} diff --git a/pkg/repo/gcs_repo.go b/pkg/repo/gcs_repo.go index 8209fd7f5..10880a3eb 100644 --- a/pkg/repo/gcs_repo.go +++ b/pkg/repo/gcs_repo.go @@ -59,7 +59,7 @@ type GCSRepo struct { } // NewPublicGCSRepo creates a new an IStorageRepo for the public GCS repository. -func NewPublicGCSRepo(httpClient *http.Client) (IStorageRepo, error) { +func NewPublicGCSRepo(httpClient *http.Client) (*GCSRepo, error) { return NewGCSRepo(GCSPublicRepoURL, "", GCSPublicRepoBucket, nil) }