Add more UT cases for changed chart API

- 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 <szou@vmware.com>
This commit is contained in:
Steven Zou 2018-09-20 17:51:27 +08:00
parent e6de7f080d
commit 24c0be789d
9 changed files with 515 additions and 378 deletions

File diff suppressed because one or more lines are too long

View File

@ -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)
}
@ -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)
}

View File

@ -10,6 +10,7 @@ import (
"testing"
"github.com/ghodss/yaml"
htesting "github.com/goharbor/harbor/src/testing"
helm_repo "k8s.io/helm/pkg/repo"
)
@ -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)

View File

@ -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"
@ -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"
)
@ -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
}

View File

@ -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"
)
@ -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, "")
@ -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"}`

View File

@ -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{}
@ -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 {
@ -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
@ -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

View File

@ -16,14 +16,10 @@ package api
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"testing"
"time"
"github.com/goharbor/harbor/src/chartserver"
"github.com/goharbor/harbor/src/common/dao"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/tests/apitests/apilib"
@ -409,77 +405,3 @@ func TestDeletable(t *testing.T) {
assert.Equal(t, http.StatusOK, code)
assert.False(t, del)
}
// Provides a mock chart controller for deletable test cases
func mockChartController() (*httptest.Server, *chartserver.Controller, error) {
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.RequestURI {
case "/api/project_for_test_deletable/charts":
if r.Method == http.MethodGet {
w.Write([]byte("{}"))
return
}
case "/api/library/charts/harbor/0.2.0":
if r.Method == http.MethodGet {
w.Write([]byte(chartVersionOfHarbor020))
return
}
}
w.WriteHeader(http.StatusNotImplemented)
w.Write([]byte("not supported"))
}))
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
}
var chartVersionOfHarbor020 = `
{
"name": "harbor",
"home": "https://github.com/vmware/harbor",
"sources": [
"https://github.com/vmware/harbor/tree/master/contrib/helm/harbor"
],
"version": "0.2.0",
"description": "An Enterprise-class Docker Registry by VMware",
"keywords": [
"vmware",
"docker",
"registry",
"harbor"
],
"maintainers": [
{
"name": "Jesse Hu",
"email": "huh@vmware.com"
},
{
"name": "paulczar",
"email": "username.taken@gmail.com"
}
],
"engine": "gotpl",
"icon": "https://raw.githubusercontent.com/vmware/harbor/master/docs/img/harbor_logo.png",
"appVersion": "1.5.0",
"urls": [
"charts/harbor-0.2.0.tgz"
],
"created": "2018-08-29T10:26:21.141611102Z",
"digest": "fc8aae8dade9f0dfca12e9f1085081c49843d30a063a3fa7eb42497e3ceb277c"
}
`

File diff suppressed because one or more lines are too long

View File

@ -20,7 +20,7 @@ function listDeps(){
done
}
packages=$(go list ./... | grep -v -E 'vendor|tests')
packages=$(go list ./... | grep -v -E 'vendor|tests|testing')
for package in $packages
do