Fix failures of UT cases of chart API related

Signed-off-by: Steven Zou <szou@vmware.com>
This commit is contained in:
Steven Zou 2018-09-20 23:30:36 +08:00
parent d4c423ea8e
commit 78b9cbf35e
6 changed files with 40 additions and 13 deletions

View File

@ -136,7 +136,7 @@ func singleJoiningSlash(a, b string) string {
// Remove 'chartrepo' from the endpoints of manipulation API
// Remove 'chartrepo' from the endpoints of repository services
func rewriteURLPath(req *http.Request) {
incomingURLPath := req.RequestURI
incomingURLPath := req.URL.Path
// Health check endpoint
if incomingURLPath == chartRepoHealthEndpoint {

View File

@ -22,6 +22,7 @@ import (
"github.com/goharbor/harbor/src/common/security"
"github.com/goharbor/harbor/src/common/utils/log"
"github.com/goharbor/harbor/src/core/config"
"github.com/goharbor/harbor/src/core/filter"
"github.com/goharbor/harbor/src/core/promgr"
"github.com/goharbor/harbor/src/core/utils"
)
@ -50,7 +51,7 @@ const (
// Prepare inits security context and project manager from request
// context
func (b *BaseController) Prepare() {
/*ctx, err := filter.GetSecurityContext(b.Ctx.Request)
ctx, err := filter.GetSecurityContext(b.Ctx.Request)
if err != nil {
log.Errorf("failed to get security context: %v", err)
b.CustomAbort(http.StatusInternalServerError, "")
@ -62,7 +63,7 @@ func (b *BaseController) Prepare() {
log.Errorf("failed to get project manager: %v", err)
b.CustomAbort(http.StatusInternalServerError, "")
}
b.ProjectMgr = pm*/
b.ProjectMgr = pm
}
// RenderFormatedError renders errors with well formted style `{"error": "This is an error"}`

View File

@ -67,7 +67,7 @@ func (cra *ChartRepositoryAPI) Prepare() {
// Exclude the following URI
// -/index.yaml
// -/api/chartserver/health
incomingURI := cra.Ctx.Request.RequestURI
incomingURI := cra.Ctx.Request.URL.Path
if incomingURI == rootUploadingEndpoint {
// Forward to the default repository
cra.namespace = defaultRepo

View File

@ -80,17 +80,15 @@ func TestGetHealthStatus(t *testing.T) {
err := handleAndParse(&testingRequest{
url: "/api/chartrepo/health",
method: http.MethodGet,
credential: projAdmin,
credential: sysAdmin,
}, &status)
if err != nil {
t.Fatal(err)
}
if v, ok := status["healthy"]; !ok {
t.Fatal("expect 'healthy' but got nil")
} else {
t.Fatalf("expect 'healthy' but got %v", v)
if _, ok := status["health"]; !ok {
t.Fatal("expect 'health' but got nil")
}
}
@ -112,7 +110,7 @@ func TestGetIndex(t *testing.T) {
request: &testingRequest{
url: "/chartrepo/index.yaml",
method: http.MethodGet,
credential: projAdmin,
credential: sysAdmin,
},
code: http.StatusOK,
})
@ -152,7 +150,7 @@ func TesListCharts(t *testing.T) {
func TestListChartVersions(t *testing.T) {
chartVersions := make(chartserver.ChartVersions, 0)
err := handleAndParse(&testingRequest{
url: "/api/chartrepo/library/chart/harbor",
url: "/api/chartrepo/library/charts/harbor",
method: http.MethodGet,
credential: projAdmin,
}, &chartVersions)
@ -170,7 +168,7 @@ func TestListChartVersions(t *testing.T) {
func TestGetChartVersion(t *testing.T) {
chartV := &chartserver.ChartVersionDetails{}
err := handleAndParse(&testingRequest{
url: "/api/chartrepo/library/chart/harbor/0.2.0",
url: "/api/chartrepo/library/charts/harbor/0.2.0",
method: http.MethodGet,
credential: projAdmin,
}, chartV)
@ -194,7 +192,7 @@ func TestDeleteChartVersion(t *testing.T) {
request: &testingRequest{
url: "/api/chartrepo/library/charts/harbor/0.2.1",
method: http.MethodDelete,
credential: projDeveloper,
credential: projAdmin,
},
code: http.StatusOK,
})

View File

@ -170,6 +170,23 @@ func init() {
beego.Router("/api/system/gc/:id", &GCAPI{}, "get:GetGC")
beego.Router("/api/system/gc/:id([0-9]+)/log", &GCAPI{}, "get:GetLog")
beego.Router("/api/system/gc/schedule", &GCAPI{}, "get:Get;put:Put;post:Post")
// Charts are controlled under projects
chartRepositoryAPIType := &ChartRepositoryAPI{}
beego.Router("/api/chartrepo/health", chartRepositoryAPIType, "get:GetHealthStatus")
beego.Router("/api/chartrepo/:repo/charts", chartRepositoryAPIType, "get:ListCharts")
beego.Router("/api/chartrepo/:repo/charts/:name", chartRepositoryAPIType, "get:ListChartVersions")
beego.Router("/api/chartrepo/:repo/charts/:name", chartRepositoryAPIType, "delete:DeleteChart")
beego.Router("/api/chartrepo/:repo/charts/:name/:version", chartRepositoryAPIType, "get:GetChartVersion")
beego.Router("/api/chartrepo/:repo/charts/:name/:version", chartRepositoryAPIType, "delete:DeleteChartVersion")
beego.Router("/api/chartrepo/:repo/charts", chartRepositoryAPIType, "post:UploadChartVersion")
beego.Router("/api/chartrepo/:repo/prov", chartRepositoryAPIType, "post:UploadChartProvFile")
beego.Router("/api/chartrepo/charts", chartRepositoryAPIType, "post:UploadChartVersion")
// Repository services
beego.Router("/chartrepo/:repo/index.yaml", chartRepositoryAPIType, "get:GetIndexByRepo")
beego.Router("/chartrepo/index.yaml", chartRepositoryAPIType, "get:GetIndex")
beego.Router("/chartrepo/:repo/charts/:filename", chartRepositoryAPIType, "get:DownloadChart")
// Labels for chart
chartLabelAPIType := &ChartLabelAPI{}
beego.Router("/api/chartrepo/:repo/charts/:name/:version/labels", chartLabelAPIType, "get:GetLabels;post:MarkLabel")

View File

@ -7,10 +7,14 @@ package testing
import (
"encoding/json"
"net/http"
"strings"
hlog "github.com/goharbor/harbor/src/common/utils/log"
)
// MockChartRepoHandler is the backend chart server handler
var MockChartRepoHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hlog.Infof("Incoming testing request: %s", r.RequestURI)
switch r.RequestURI {
case "/health":
if r.Method == http.MethodGet {
@ -88,6 +92,13 @@ var MockChartRepoHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.
w.Write([]byte("{}"))
return
}
default:
if r.Method == http.MethodGet {
if strings.HasSuffix(r.RequestURI, "/index.yaml") {
w.Write([]byte(repo2IndexYaml))
return
}
}
}
w.WriteHeader(http.StatusNotImplemented)