Skip to content

Commit

Permalink
Add more UT cases for changed chart API
Browse files Browse the repository at this point in the history
- add more cases in the ChartRepositoryAPI controller
- move chart utility testing functions to a separate go file under testing
- ignore testing coverage for testing folder
- update other UT cases to reflect the change of adding chart testing utility functions

Signed-off-by: Steven Zou <[email protected]>
  • Loading branch information
steven-zou committed Sep 20, 2018
1 parent e6de7f0 commit 24c0be7
Show file tree
Hide file tree
Showing 9 changed files with 515 additions and 378 deletions.
295 changes: 4 additions & 291 deletions src/chartserver/base_test.go

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/chartserver/chart_operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package chartserver

import (
"testing"

htesting "github.com/goharbor/harbor/src/testing"
)

func TestGetChartDetails(t *testing.T) {
chartOpr := ChartOperator{}
chartDetails, err := chartOpr.GetChartDetails(helmChartContent)
chartDetails, err := chartOpr.GetChartDetails(htesting.HelmChartContent)
if err != nil {
t.Fatal(err)
}
Expand All @@ -26,7 +28,7 @@ func TestGetChartDetails(t *testing.T) {

func TestGetChartList(t *testing.T) {
chartOpr := ChartOperator{}
infos, err := chartOpr.GetChartList(chartListContent)
infos, err := chartOpr.GetChartList(htesting.ChartListContent)
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 2 additions & 1 deletion src/chartserver/handler_proxy_traffic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"

"github.com/ghodss/yaml"
htesting "github.com/goharbor/harbor/src/testing"
helm_repo "k8s.io/helm/pkg/repo"
)

Expand Down Expand Up @@ -71,7 +72,7 @@ func TestDownloadChart(t *testing.T) {
}

gotSize := len(content)
expectSize := len(helmChartContent)
expectSize := len(htesting.HelmChartContent)

if gotSize != expectSize {
t.Fatalf("Expect %d bytes data but got %d bytes", expectSize, gotSize)
Expand Down
25 changes: 25 additions & 0 deletions src/core/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"os"
"strconv"
"strings"
"testing"

"github.com/goharbor/harbor/src/chartserver"
"github.com/goharbor/harbor/src/common"

"github.com/astaxie/beego"
Expand All @@ -32,6 +34,7 @@ import (
"github.com/goharbor/harbor/src/common/dao/project"
common_http "github.com/goharbor/harbor/src/common/http"
"github.com/goharbor/harbor/src/common/models"
htesting "github.com/goharbor/harbor/src/testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -292,3 +295,25 @@ func clean() {
}
}
}

// Provides a mock chart controller for deletable test cases
func mockChartController() (*httptest.Server, *chartserver.Controller, error) {
mockServer := httptest.NewServer(htesting.MockChartRepoHandler)

var oldController, newController *chartserver.Controller
url, err := url.Parse(mockServer.URL)
if err == nil {
newController, err = chartserver.NewController(url)
}

if err != nil {
mockServer.Close()
return nil, nil, err
}

// Override current controller and keep the old one for restoring
oldController = chartController
chartController = newController

return mockServer, oldController, nil
}
5 changes: 2 additions & 3 deletions src/core/api/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ 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"
)
Expand Down Expand Up @@ -51,7 +50,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, "")
Expand All @@ -63,7 +62,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"}`
Expand Down
164 changes: 162 additions & 2 deletions src/core/api/chart_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ package api
import (
"errors"
"net/http"
"net/http/httptest"
"testing"

"github.com/goharbor/harbor/src/chartserver"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/core/promgr/metamgr"
)

var (
crOldController *chartserver.Controller
crMockServer *httptest.Server
)

// Test access checking
func TestRequireAccess(t *testing.T) {
chartAPI := &ChartRepositoryAPI{}
Expand Down Expand Up @@ -58,6 +65,159 @@ func TestRequireNamespace(t *testing.T) {
}
}

// Prepare
func TestPrepareEnv(t *testing.T) {
var err error
crMockServer, crOldController, err = mockChartController()
if err != nil {
t.Fatalf("Failed to start mock chart service with error: %s", err)
}
}

// Test get health
func TestGetHealthStatus(t *testing.T) {
status := make(map[string]interface{})
err := handleAndParse(&testingRequest{
url: "/api/chartrepo/health",
method: http.MethodGet,
credential: projAdmin,
}, &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)
}
}

// Test get index by repo
func TestGetIndexByRepo(t *testing.T) {
runCodeCheckingCases(t, &codeCheckingCase{
request: &testingRequest{
url: "/chartrepo/library/index.yaml",
method: http.MethodGet,
credential: projDeveloper,
},
code: http.StatusOK,
})
}

// Test get index
func TestGetIndex(t *testing.T) {
runCodeCheckingCases(t, &codeCheckingCase{
request: &testingRequest{
url: "/chartrepo/index.yaml",
method: http.MethodGet,
credential: projAdmin,
},
code: http.StatusOK,
})
}

// Test download chart
func TestDownloadChart(t *testing.T) {
runCodeCheckingCases(t, &codeCheckingCase{
request: &testingRequest{
url: "/chartrepo/library/charts/harbor-0.2.0.tgz",
method: http.MethodGet,
credential: projDeveloper,
},
code: http.StatusOK,
})
}

// Test get charts
func TesListCharts(t *testing.T) {
charts := make([]*chartserver.ChartInfo, 0)
err := handleAndParse(&testingRequest{
url: "/api/chartrepo/library/charts",
method: http.MethodGet,
credential: projAdmin,
}, &charts)

if err != nil {
t.Fatal(err)
}

if len(charts) != 2 {
t.Fatalf("expect 2 charts but got %d", len(charts))
}
}

// Test get chart versions
func TestListChartVersions(t *testing.T) {
chartVersions := make(chartserver.ChartVersions, 0)
err := handleAndParse(&testingRequest{
url: "/api/chartrepo/library/chart/harbor",
method: http.MethodGet,
credential: projAdmin,
}, &chartVersions)

if err != nil {
t.Fatal(err)
}

if len(chartVersions) != 2 {
t.Fatalf("expect 2 chart versions but got %d", len(chartVersions))
}
}

// Test get chart version details
func TestGetChartVersion(t *testing.T) {
chartV := &chartserver.ChartVersionDetails{}
err := handleAndParse(&testingRequest{
url: "/api/chartrepo/library/chart/harbor/0.2.0",
method: http.MethodGet,
credential: projAdmin,
}, chartV)

if err != nil {
t.Fatal(err)
}

if chartV.Metadata.GetName() != "harbor" {
t.Fatalf("expect get chart 'harbor' but got %s", chartV.Metadata.GetName())
}

if chartV.Metadata.GetVersion() != "0.2.0" {
t.Fatalf("expect get chart version '0.2.0' but got %s", chartV.Metadata.GetVersion())
}
}

// Test delete chart version
func TestDeleteChartVersion(t *testing.T) {
runCodeCheckingCases(t, &codeCheckingCase{
request: &testingRequest{
url: "/api/chartrepo/library/charts/harbor/0.2.1",
method: http.MethodDelete,
credential: projDeveloper,
},
code: http.StatusOK,
})
}

// Test delete chart
func TestDeleteChart(t *testing.T) {
runCodeCheckingCases(t, &codeCheckingCase{
request: &testingRequest{
url: "/api/chartrepo/library/charts/harbor",
method: http.MethodDelete,
credential: projDeveloper,
},
code: http.StatusOK,
})
}

// Clear
func TestClearEnv(t *testing.T) {
crMockServer.Close()
chartController = crOldController
}

func createRequest(method string, url string) (*http.Request, error) {
req, err := http.NewRequest(method, url, nil)
if err != nil {
Expand Down Expand Up @@ -92,7 +252,7 @@ func (mpm *mockProjectManager) List(query *models.ProjectQueryParam) (*models.Pr
Projects: make([]*models.Project, 0),
}

results.Projects = append(results.Projects, &models.Project{ProjectID: 0, Name: "repo1"})
results.Projects = append(results.Projects, &models.Project{ProjectID: 0, Name: "library"})
results.Projects = append(results.Projects, &models.Project{ProjectID: 1, Name: "repo2"})

return results, nil
Expand Down Expand Up @@ -188,7 +348,7 @@ func (msc *mockSecurityContext) HasAllPerm(projectIDOrName interface{}) bool {

// Get current user's all project
func (msc *mockSecurityContext) GetMyProjects() ([]*models.Project, error) {
return []*models.Project{{ProjectID: 0, Name: "repo1"}}, nil
return []*models.Project{{ProjectID: 0, Name: "library"}}, nil
}

// Get user's role in provided project
Expand Down
Loading

0 comments on commit 24c0be7

Please sign in to comment.