Merge remote-tracking branch 'upstream/dev' into dev

This commit is contained in:
kunw 2016-09-13 17:12:29 +08:00
commit 575b2b90da
4 changed files with 522 additions and 9 deletions

77
api/dataprepare_test.go Normal file
View File

@ -0,0 +1,77 @@
/*
Copyright (c) 2016 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package api
import (
"github.com/vmware/harbor/dao"
"github.com/vmware/harbor/models"
)
/*
const username string = "testUser0001"
const password string = "testUser0001"
const email string = "testUser0001@mydomain.com"
const projectname string = "testproject0001"
*/
func CommonAddUser() {
commonUser := models.User{
Username: TestUserName,
Email: TestUserPwd,
Password: TestUserEmail,
}
_, _ = dao.Register(commonUser)
}
func CommonGetUserID() int {
queryUser := models.User{
Username: TestUserName,
}
commonUser, _ := dao.GetUser(queryUser)
return commonUser.UserID
}
func CommonDelUser() {
queryUser := models.User{
Username: TestUserName,
}
commonUser, _ := dao.GetUser(queryUser)
_ = dao.DeleteUser(commonUser.UserID)
}
func CommonAddProject() {
queryUser := models.User{
Username: "admin",
}
adminUser, _ := dao.GetUser(queryUser)
commonProject := models.Project{
Name: TestProName,
OwnerID: adminUser.UserID,
}
_, _ = dao.AddProject(commonProject)
}
func CommonDelProject() {
commonProject, _ := dao.GetProjectByName(TestProName)
_ = dao.DeleteProject(commonProject.ProjectID)
}

View File

@ -5,14 +5,15 @@ package api
import (
"encoding/json"
"fmt"
"github.com/vmware/harbor/dao"
"github.com/vmware/harbor/models"
"github.com/vmware/harbor/tests/apitests/apilib"
"io/ioutil"
"net/http/httptest"
"path/filepath"
"runtime"
"github.com/vmware/harbor/dao"
"github.com/vmware/harbor/models"
"github.com/vmware/harbor/tests/apitests/apilib"
// "strconv"
// "strings"
"github.com/astaxie/beego"
"github.com/dghubble/sling"
@ -27,9 +28,14 @@ const (
testAcceptHeader = "text/plain"
adminName = "admin"
adminPwd = "Harbor12345"
//Prepare Test info
TestUserName = "testUser0001"
TestUserPwd = "testUser0001"
TestUserEmail = "testUser0001@mydomain.com"
TestProName = "testProject0001"
)
var admin, unknownUsr *usrInfo
var admin, unknownUsr, testUser *usrInfo
type api struct {
basePath string
@ -65,15 +71,21 @@ func init() {
beego.Router("/api/users/:id([0-9]+)/password", &UserAPI{}, "put:ChangePassword")
beego.Router("/api/projects/:id/publicity", &ProjectAPI{}, "put:ToggleProjectPublic")
beego.Router("/api/projects/:id([0-9]+)/logs/filter", &ProjectAPI{}, "post:FilterAccessLog")
beego.Router("/api/projects/:pid([0-9]+)/members/?:mid", &ProjectMemberAPI{}, "get:Get")
beego.Router("/api/projects/:pid([0-9]+)/members/?:mid", &ProjectMemberAPI{}, "get:Get;post:Post;delete:Delete;put:Put")
beego.Router("/api/statistics", &StatisticAPI{})
beego.Router("/api/users/?:id", &UserAPI{})
beego.Router("/api/logs", &LogAPI{})
beego.Router("/api/repositories", &RepositoryAPI{})
beego.Router("/api/repositories/tags", &RepositoryAPI{}, "get:GetTags")
beego.Router("/api/repositories/manifests", &RepositoryAPI{}, "get:GetManifests")
beego.Router("/api/repositories/top", &RepositoryAPI{}, "get:GetTopRepos")
_ = updateInitPassword(1, "Harbor12345")
//Init user Info
admin = &usrInfo{adminName, adminPwd}
unknownUsr = &usrInfo{"unknown", "unknown"}
testUser = &usrInfo{TestUserName, TestUserPwd}
}
@ -374,16 +386,120 @@ func (a api) GetProjectMembersByProID(prjUsr usrInfo, projectID string) (int, []
}
//Add project role member accompany with projectID
func (a api) AddProjectMember(prjUsr usrInfo, projectID string, roles apilib.RoleParam) (int, []byte, error) {
func (a api) AddProjectMember(prjUsr usrInfo, projectID string, roles apilib.RoleParam) (int, error) {
_sling := sling.New().Post(a.basePath)
path := "/api/projects/" + projectID + "/members/"
_sling = _sling.Path(path)
_sling = _sling.BodyJSON(roles)
httpStatusCode, _, err := request(_sling, jsonAcceptHeader, prjUsr)
return httpStatusCode, err
}
//Delete project role member accompany with projectID
func (a api) DeleteProjectMember(authInfo usrInfo, projectID string, userID string) (int, error) {
_sling := sling.New().Delete(a.basePath)
path := "/api/projects/" + projectID + "/members/" + userID
_sling = _sling.Path(path)
httpStatusCode, _, err := request(_sling, jsonAcceptHeader, authInfo)
return httpStatusCode, err
}
//Get role memberInfo by projectId and UserId
func (a api) GetMemByPIDUID(authInfo usrInfo, projectID string, userID string) (int, error) {
_sling := sling.New().Get(a.basePath)
path := "/api/projects/" + projectID + "/members/" + userID
_sling = _sling.Path(path)
httpStatusCode, body, err := request(_sling, jsonAcceptHeader, prjUsr)
return httpStatusCode, body, err
httpStatusCode, _, err := request(_sling, jsonAcceptHeader, authInfo)
return httpStatusCode, err
}
//Put:update current project role members accompany with relevant project and user
func (a api) PutProjectMember(authInfo usrInfo, projectID string, userID string, roles apilib.RoleParam) (int, error) {
_sling := sling.New().Put(a.basePath)
path := "/api/projects/" + projectID + "/members/" + userID
_sling = _sling.Path(path)
_sling = _sling.BodyJSON(roles)
httpStatusCode, _, err := request(_sling, jsonAcceptHeader, authInfo)
return httpStatusCode, err
}
//-------------------------Repositories Test---------------------------------------//
//Return relevant repos of projectID
func (a api) GetRepos(authInfo usrInfo, projectID string) (int, error) {
_sling := sling.New().Get(a.basePath)
path := "/api/repositories/"
_sling = _sling.Path(path)
type QueryParams struct {
ProjectID string `url:"project_id"`
}
_sling = _sling.QueryStruct(&QueryParams{ProjectID: projectID})
httpStatusCode, _, err := request(_sling, jsonAcceptHeader, authInfo)
return httpStatusCode, err
}
//Get tags of a relevant repository
func (a api) GetReposTags(authInfo usrInfo, repoName string) (int, error) {
_sling := sling.New().Get(a.basePath)
path := "/api/repositories/tags"
_sling = _sling.Path(path)
type QueryParams struct {
RepoName string `url:"repo_name"`
}
_sling = _sling.QueryStruct(&QueryParams{RepoName: repoName})
httpStatusCode, _, err := request(_sling, jsonAcceptHeader, authInfo)
return httpStatusCode, err
}
//Get manifests of a relevant repository
func (a api) GetReposManifests(authInfo usrInfo, repoName string, tag string) (int, error) {
_sling := sling.New().Get(a.basePath)
path := "/api/repositories/manifests"
_sling = _sling.Path(path)
type QueryParams struct {
RepoName string `url:"repo_name"`
Tag string `url:"tag"`
}
_sling = _sling.QueryStruct(&QueryParams{RepoName: repoName, Tag: tag})
httpStatusCode, _, err := request(_sling, jsonAcceptHeader, authInfo)
return httpStatusCode, err
}
//Get public repositories which are accessed most
func (a api) GetReposTop(authInfo usrInfo, count string) (int, error) {
_sling := sling.New().Get(a.basePath)
path := "/api/repositories/top"
_sling = _sling.Path(path)
type QueryParams struct {
Count string `url:"count"`
}
_sling = _sling.QueryStruct(&QueryParams{Count: count})
httpStatusCode, _, err := request(_sling, jsonAcceptHeader, authInfo)
return httpStatusCode, err
}
//Return projects created by Harbor

View File

@ -6,6 +6,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/vmware/harbor/tests/apitests/apilib"
"strconv"
)
func TestMemGet(t *testing.T) {
@ -49,6 +50,140 @@ func TestMemGet(t *testing.T) {
} else {
assert.Equal(int(404), httpStatusCode, "Case 3: Project creation status should be 404")
}
fmt.Printf("\n")
}
/**
* Add project role member accompany with projectID
* role_id = 1 : ProjectAdmin
* role_id = 2 : Developer
* role_id = 3 : Guest
*/
func TestMemPost(t *testing.T) {
var httpStatusCode int
var err error
assert := assert.New(t)
apiTest := newHarborAPI()
projectID := "1"
CommonAddUser()
roles := &apilib.RoleParam{[]int32{1}, TestUserName}
fmt.Printf("Add User \"%s\" successfully!\n", TestUserName)
fmt.Println("Testing Member Post API")
//-------------------case 1 : response code = 200------------------------//
fmt.Println("case 1: response code = 200")
httpStatusCode, err = apiTest.AddProjectMember(*admin, projectID, *roles)
if err != nil {
t.Error("Error whihle add project role member", err.Error())
t.Log(err)
} else {
assert.Equal(int(200), httpStatusCode, "httpStatusCode should be 200")
}
//---------case 2: Response Code=409,User is ready in project.----------//
fmt.Println("case 2: Response Code=409,User is ready in project.")
httpStatusCode, err = apiTest.AddProjectMember(*admin, projectID, *roles)
if err != nil {
t.Error("Error while add project role member", err.Error())
t.Log(err)
} else {
assert.Equal(int(409), httpStatusCode, "Case 2: httpStatusCode should be 409")
}
//---------case 3: Response Code=404,User does not exist.----------//
fmt.Println("case 3: Response Code=404,User does not exist.")
errorRoles := &apilib.RoleParam{[]int32{1}, "T"}
httpStatusCode, err = apiTest.AddProjectMember(*admin, projectID, *errorRoles)
if err != nil {
t.Error("Error while add project role member", err.Error())
t.Log(err)
} else {
assert.Equal(int(404), httpStatusCode, "Case 3: httpStatusCode status should be 404")
}
/*
//---------case 4: Response Code=403,User in session does not have permission to the project..----------//
fmt.Println("case 4:User in session does not have permission to the project.")
httpStatusCode, err = apiTest.AddProjectMember(*testUser, projectID, *roles)
if err != nil {
t.Error("Error while add project role member", err.Error())
t.Log(err)
} else {
assert.Equal(int(403), httpStatusCode, "Case 3: httpStatusCode status should be 403")
}
*/
}
func TestGetMemByPIDUID(t *testing.T) {
var httpStatusCode int
var err error
assert := assert.New(t)
apiTest := newHarborAPI()
projectID := "1"
userID := strconv.Itoa(CommonGetUserID())
fmt.Println("Testing Member Get API by PID and UID")
//-------------------case 1 : response code = 200------------------------//
fmt.Println("case 1: response code = 200")
httpStatusCode, err = apiTest.GetMemByPIDUID(*admin, projectID, userID)
if err != nil {
t.Error("Error whihle get project role member", err.Error())
t.Log(err)
} else {
assert.Equal(int(200), httpStatusCode, "httpStatusCode should be 200")
}
}
func TestPutMem(t *testing.T) {
var httpStatusCode int
var err error
assert := assert.New(t)
apiTest := newHarborAPI()
projectID := "1"
userID := strconv.Itoa(CommonGetUserID())
roles := &apilib.RoleParam{[]int32{3}, TestUserName}
fmt.Println("Testing Member Put API")
//-------------------case 1 : response code = 200------------------------//
fmt.Println("case 1: response code = 200")
httpStatusCode, err = apiTest.PutProjectMember(*admin, projectID, userID, *roles)
if err != nil {
t.Error("Error whihle put project role member", err.Error())
t.Log(err)
} else {
assert.Equal(int(200), httpStatusCode, "httpStatusCode should be 200")
}
}
func TestDeleteMemUser(t *testing.T) {
var httpStatusCode int
var err error
assert := assert.New(t)
apiTest := newHarborAPI()
projectID := "1"
fmt.Println("Testing Member Delete API")
//-------------------case 1 : response code = 200------------------------//
fmt.Println("case 1: response code = 200")
id := strconv.Itoa(CommonGetUserID())
httpStatusCode, err = apiTest.DeleteProjectMember(*admin, projectID, id)
if err != nil {
t.Error("Error whihle add project role member", err.Error())
t.Log(err)
} else {
assert.Equal(int(200), httpStatusCode, "httpStatusCode should be 200")
}
CommonDelUser()
}

185
api/repository_test.go Normal file
View File

@ -0,0 +1,185 @@
package api
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
// "github.com/vmware/harbor/tests/apitests/apilib"
// "strconv"
)
func TestGetRepos(t *testing.T) {
var httpStatusCode int
var err error
assert := assert.New(t)
apiTest := newHarborAPI()
projectID := "1"
fmt.Println("Testing Repos Get API")
//-------------------case 1 : response code = 200------------------------//
fmt.Println("case 1 : response code = 200")
httpStatusCode, err = apiTest.GetRepos(*admin, projectID)
if err != nil {
t.Error("Error whihle get repos by projectID", err.Error())
t.Log(err)
} else {
assert.Equal(int(200), httpStatusCode, "httpStatusCode should be 200")
}
//-------------------case 2 : response code = 400------------------------//
fmt.Println("case 2 : response code = 409,invalid project_id")
projectID = "ccc"
httpStatusCode, err = apiTest.GetRepos(*admin, projectID)
if err != nil {
t.Error("Error whihle get repos by projectID", err.Error())
t.Log(err)
} else {
assert.Equal(int(400), httpStatusCode, "httpStatusCode should be 400")
}
//-------------------case 3 : response code = 404------------------------//
fmt.Println("case 3 : response code = 404:project not found")
projectID = "111"
httpStatusCode, err = apiTest.GetRepos(*admin, projectID)
if err != nil {
t.Error("Error whihle get repos by projectID", err.Error())
t.Log(err)
} else {
assert.Equal(int(404), httpStatusCode, "httpStatusCode should be 404")
}
fmt.Printf("\n")
}
func TestGetReposTags(t *testing.T) {
var httpStatusCode int
var err error
var repoName string
assert := assert.New(t)
apiTest := newHarborAPI()
fmt.Println("Testing ReposTags Get API")
//-------------------case 1 : response code = 400------------------------//
fmt.Println("case 1 : response code = 400,repo_name is nil")
repoName = ""
httpStatusCode, err = apiTest.GetReposTags(*admin, repoName)
if err != nil {
t.Error("Error whihle get reposTags by repoName", err.Error())
t.Log(err)
} else {
assert.Equal(int(400), httpStatusCode, "httpStatusCode should be 400")
}
//-------------------case 2 : response code = 404------------------------//
fmt.Println("case 2 : response code = 404,repo not found")
repoName = "errorRepos"
httpStatusCode, err = apiTest.GetReposTags(*admin, repoName)
if err != nil {
t.Error("Error whihle get reposTags by repoName", err.Error())
t.Log(err)
} else {
assert.Equal(int(404), httpStatusCode, "httpStatusCode should be 404")
}
//-------------------case 3 : response code = 200------------------------//
fmt.Println("case 3 : response code = 200")
repoName = "library/hello-world"
httpStatusCode, err = apiTest.GetReposTags(*admin, repoName)
if err != nil {
t.Error("Error whihle get reposTags by repoName", err.Error())
t.Log(err)
} else {
assert.Equal(int(200), httpStatusCode, "httpStatusCode should be 200")
}
fmt.Printf("\n")
}
func TestGetReposManifests(t *testing.T) {
var httpStatusCode int
var err error
var repoName string
var tag string
assert := assert.New(t)
apiTest := newHarborAPI()
fmt.Println("Testing ReposManifests Get API")
//-------------------case 1 : response code = 200------------------------//
fmt.Println("case 1 : response code = 200")
repoName = "library/hello-world"
tag = "latest"
httpStatusCode, err = apiTest.GetReposManifests(*admin, repoName, tag)
if err != nil {
t.Error("Error whihle get reposManifests by repoName and tag", err.Error())
t.Log(err)
} else {
assert.Equal(int(200), httpStatusCode, "httpStatusCode should be 200")
}
//-------------------case 2 : response code = 404------------------------//
fmt.Println("case 2 : response code = 404:tags error,manifest unknown")
tag = "l"
httpStatusCode, err = apiTest.GetReposManifests(*admin, repoName, tag)
if err != nil {
t.Error("Error whihle get reposManifests by repoName and tag", err.Error())
t.Log(err)
} else {
assert.Equal(int(404), httpStatusCode, "httpStatusCode should be 404")
}
//-------------------case 3 : response code = 400------------------------//
fmt.Println("case 3 : response code = 400,repo_name or is nil")
repoName = ""
httpStatusCode, err = apiTest.GetReposManifests(*admin, repoName, tag)
if err != nil {
t.Error("Error whihle get reposManifests by repoName and tag", err.Error())
t.Log(err)
} else {
assert.Equal(int(400), httpStatusCode, "httpStatusCode should be 400")
}
//-------------------case 4 : response code = 404------------------------//
fmt.Println("case 4 : response code = 404,repo not found")
repoName = "111"
httpStatusCode, err = apiTest.GetReposManifests(*admin, repoName, tag)
if err != nil {
t.Error("Error whihle get reposManifests by repoName and tag", err.Error())
t.Log(err)
} else {
assert.Equal(int(404), httpStatusCode, "httpStatusCode should be 404")
}
fmt.Printf("\n")
}
func TestGetReposTop(t *testing.T) {
var httpStatusCode int
var err error
var count string
assert := assert.New(t)
apiTest := newHarborAPI()
fmt.Println("Testing ReposTop Get API")
//-------------------case 1 : response code = 200------------------------//
fmt.Println("case 1 : response code = 200")
count = "1"
httpStatusCode, err = apiTest.GetReposTop(*admin, count)
if err != nil {
t.Error("Error whihle get reposTop to show the most popular public repositories ", err.Error())
t.Log(err)
} else {
assert.Equal(int(200), httpStatusCode, "httpStatusCode should be 200")
}
//-------------------case 2 : response code = 400------------------------//
fmt.Println("case 2 : response code = 400,invalid count")
count = "cc"
httpStatusCode, err = apiTest.GetReposTop(*admin, count)
if err != nil {
t.Error("Error whihle get reposTop to show the most popular public repositories ", err.Error())
t.Log(err)
} else {
assert.Equal(int(400), httpStatusCode, "httpStatusCode should be 400")
}
fmt.Printf("\n")
}