Commit 00ad7d1c authored by Jack Greenfield's avatar Jack Greenfield

Merge pull request #461 from jackgr/manager

Repo name routes, tests and cleanups
parents e7119a92 73ff6768
......@@ -39,7 +39,7 @@ func registerChartRepoRoutes(c *router.Context, h *router.Handler) {
func listChartReposHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error {
handler := "manager: list chart repositories"
repos, err := c.Manager.ListChartRepos()
repos, err := c.Manager.ListRepos()
if err != nil {
return err
}
......@@ -58,7 +58,7 @@ func addChartRepoHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.C
return nil
}
if err := c.Manager.AddChartRepo(cr); err != nil {
if err := c.Manager.AddRepo(cr); err != nil {
httputil.BadRequest(w, r, err)
return nil
}
......@@ -77,7 +77,7 @@ func removeChartRepoHandlerFunc(w http.ResponseWriter, r *http.Request, c *route
return err
}
err = c.Manager.RemoveChartRepo(name)
err = c.Manager.RemoveRepo(name)
if err != nil {
return err
}
......@@ -95,7 +95,7 @@ func getChartRepoHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.C
return err
}
cr, err := c.Manager.GetChartRepo(repoURL)
cr, err := c.Manager.GetRepo(repoURL)
if err != nil {
httputil.BadRequest(w, r, err)
return nil
......
......@@ -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 (
TestRepoBucket = "kubernetes-charts-testing"
TestRepoURL = "gs://" + TestRepoBucket
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, TestRepoBucket, "")
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, TestRepoBucket, "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, TestRepoBucket, 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.StatusCreated {
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, TestRepoBucket, "")
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, TestRepoBucket, 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()
}
......@@ -50,18 +50,18 @@ type Manager interface {
GetChart(chartName string) (*chart.Chart, error)
// Repo Charts
ListRepoCharts(repoURL string, regex *regexp.Regexp) ([]string, error)
GetChartForRepo(repoURL, chartName string) (*chart.Chart, error)
ListRepoCharts(repoName string, regex *regexp.Regexp) ([]string, error)
GetChartForRepo(repoName, chartName string) (*chart.Chart, error)
// Credentials
CreateCredential(name string, c *repo.Credential) error
GetCredential(name string) (*repo.Credential, error)
// Chart Repositories
ListChartRepos() (map[string]string, error)
AddChartRepo(addition repo.IRepo) error
RemoveChartRepo(name string) error
GetChartRepo(URL string) (repo.IRepo, error)
ListRepos() (map[string]string, error)
AddRepo(addition repo.IRepo) error
RemoveRepo(repoName string) error
GetRepo(repoName string) (repo.IRepo, error)
}
type manager struct {
......@@ -366,28 +366,34 @@ func (m *manager) GetChart(reference string) (*chart.Chart, error) {
return c, nil
}
// ListChartRepos returns the list of available chart repository URLs
func (m *manager) ListChartRepos() (map[string]string, error) {
// ListRepos returns the list of available repository URLs
func (m *manager) ListRepos() (map[string]string, error) {
return m.service.ListRepos()
}
// AddChartRepo adds a chart repository to the list
func (m *manager) AddChartRepo(addition repo.IRepo) error {
// AddRepo adds a repository to the list
func (m *manager) AddRepo(addition repo.IRepo) error {
return m.service.CreateRepo(addition)
}
// RemoveChartRepo removes a chart repository from the list by URL
func (m *manager) RemoveChartRepo(name string) error {
url, err := m.service.GetRepoURLByName(name)
// RemoveRepo removes a repository from the list by URL
func (m *manager) RemoveRepo(repoName string) error {
repoURL, err := m.service.GetRepoURLByName(repoName)
if err != nil {
return err
}
return m.service.DeleteRepo(url)
return m.service.DeleteRepo(repoURL)
}
// GetChartRepo returns the chart repository with the given URL
func (m *manager) GetChartRepo(URL string) (repo.IRepo, error) {
return m.service.GetRepoByURL(URL)
// GetRepo returns the repository with the given URL
func (m *manager) GetRepo(repoName string) (repo.IRepo, error) {
repoURL, err := m.service.GetRepoURLByName(repoName)
if err != nil {
return nil, err
}
return m.service.GetRepoByURL(repoURL)
}
func generateManifestName() string {
......@@ -412,10 +418,15 @@ func getResourceErrors(c *common.Configuration) []string {
return errs
}
// ListRepoCharts lists charts in a given repository whose URLs
// ListRepoCharts lists charts in a given repository whose names
// conform to the supplied regular expression, or all charts, if the regular
// expression is nil.
func (m *manager) ListRepoCharts(repoURL string, regex *regexp.Regexp) ([]string, error) {
func (m *manager) ListRepoCharts(repoName string, regex *regexp.Regexp) ([]string, error) {
repoURL, err := m.service.GetRepoURLByName(repoName)
if err != nil {
return nil, err
}
r, err := m.repoProvider.GetRepoByURL(repoURL)
if err != nil {
return nil, err
......@@ -425,7 +436,12 @@ func (m *manager) ListRepoCharts(repoURL string, regex *regexp.Regexp) ([]string
}
// GetChartForRepo returns a chart by name from a given repository.
func (m *manager) GetChartForRepo(repoURL, chartName string) (*chart.Chart, error) {
func (m *manager) GetChartForRepo(repoName, chartName string) (*chart.Chart, error) {
repoURL, err := m.service.GetRepoURLByName(repoName)
if err != nil {
return nil, err
}
r, err := m.repoProvider.GetRepoByURL(repoURL)
if err != nil {
return nil, err
......
......@@ -112,54 +112,40 @@ func (m *mockManager) GetRepoForChart(chartName string) (string, error) {
}
func (m *mockManager) GetMetadataForChart(chartName string) (*chart.Chartfile, error) {
return nil, nil
return &chart.Chartfile{}, nil
}
func (m *mockManager) GetChart(chartName string) (*chart.Chart, error) {
return nil, nil
return &chart.Chart{}, nil
}
func (m *mockManager) AddChartRepo(addition repo.IRepo) error {
return nil
func (m *mockManager) ListRepoCharts(repoName string, regex *regexp.Regexp) ([]string, error) {
return []string{}, nil
}
func (m *mockManager) ListChartRepos() (map[string]string, error) {
return map[string]string{}, nil
func (m *mockManager) GetChartForRepo(repoName, chartName string) (*chart.Chart, error) {
return &chart.Chart{}, nil
}
func (m *mockManager) RemoveChartRepo(name string) error {
func (m *mockManager) CreateCredential(name string, c *repo.Credential) error {
return nil
}
func (m *mockManager) GetChartRepo(URL string) (repo.IRepo, error) {
return nil, nil
func (m *mockManager) GetCredential(name string) (*repo.Credential, error) {
return &repo.Credential{}, nil
}
func (m *mockManager) ListRepos() ([]*repo.Repo, error) {
return []*repo.Repo{}, nil
func (m *mockManager) ListRepos() (map[string]string, error) {
return map[string]string{}, nil
}
func (m *mockManager) CreateRepo(pr *repo.Repo) error {
return nil
}
func (m *mockManager) GetRepo(name string) (*repo.Repo, error) {
return &repo.Repo{}, nil
}
func (m *mockManager) DeleteRepo(name string) error {
func (m *mockManager) AddRepo(addition repo.IRepo) error {
return nil
}
func (m *mockManager) ListRepoCharts(repoName string, regex *regexp.Regexp) ([]string, error) {
return []string{}, nil
}
func (m *mockManager) GetChartForRepo(repoName, chartName string) (*chart.Chart, error) {
return nil, nil
}
func (m *mockManager) CreateCredential(name string, c *repo.Credential) error {
func (m *mockManager) RemoveRepo(name string) error {
return nil
}
func (m *mockManager) GetCredential(name string) (*repo.Credential, error) {
return &repo.Credential{}, nil
func (m *mockManager) GetRepo(name string) (repo.IRepo, error) {
return &repo.Repo{}, nil
}
......@@ -59,12 +59,12 @@ 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)
}
// NewGCSRepo creates a new IStorageRepo for a given GCS repository.
func NewGCSRepo(URL, credentialName, repoName string, httpClient *http.Client) (IStorageRepo, error) {
func NewGCSRepo(URL, credentialName, repoName string, httpClient *http.Client) (*GCSRepo, error) {
r, err := newRepo(URL, credentialName, repoName, GCSRepoFormat, GCSRepoType)
if err != nil {
return nil, err
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment