Merge pull request #813 from zgdxiaoxiao/api-test

add policy api test
This commit is contained in:
yhua123 2016-09-21 15:29:45 +08:00 committed by GitHub
commit f11e9e8cd0
3 changed files with 404 additions and 19 deletions

View File

@ -18,14 +18,18 @@ package api
import ( import (
"github.com/vmware/harbor/dao" "github.com/vmware/harbor/dao"
"github.com/vmware/harbor/models" "github.com/vmware/harbor/models"
"os"
)
const (
//Prepare Test info
TestUserName = "testUser0001"
TestUserPwd = "testUser0001"
TestUserEmail = "testUser0001@mydomain.com"
TestProName = "testProject0001"
TestTargetName = "testTarget0001"
) )
/*
const username string = "testUser0001"
const password string = "testUser0001"
const email string = "testUser0001@mydomain.com"
const projectname string = "testproject0001"
*/
func CommonAddUser() { func CommonAddUser() {
commonUser := models.User{ commonUser := models.User{
@ -39,34 +43,34 @@ func CommonAddUser() {
} }
func CommonGetUserID() int { func CommonGetUserID() int {
queryUser := models.User{ queryUser := &models.User{
Username: TestUserName, Username: TestUserName,
} }
commonUser, _ := dao.GetUser(queryUser) commonUser, _ := dao.GetUser(*queryUser)
return commonUser.UserID return commonUser.UserID
} }
func CommonDelUser() { func CommonDelUser() {
queryUser := models.User{ queryUser := &models.User{
Username: TestUserName, Username: TestUserName,
} }
commonUser, _ := dao.GetUser(queryUser) commonUser, _ := dao.GetUser(*queryUser)
_ = dao.DeleteUser(commonUser.UserID) _ = dao.DeleteUser(commonUser.UserID)
} }
func CommonAddProject() { func CommonAddProject() {
queryUser := models.User{ queryUser := &models.User{
Username: "admin", Username: "admin",
} }
adminUser, _ := dao.GetUser(queryUser) adminUser, _ := dao.GetUser(*queryUser)
commonProject := models.Project{ commonProject := &models.Project{
Name: TestProName, Name: TestProName,
OwnerID: adminUser.UserID, OwnerID: adminUser.UserID,
} }
_, _ = dao.AddProject(commonProject) _, _ = dao.AddProject(*commonProject)
} }
@ -75,3 +79,28 @@ func CommonDelProject() {
_ = dao.DeleteProject(commonProject.ProjectID) _ = dao.DeleteProject(commonProject.ProjectID)
} }
func CommonAddTarget() {
endPoint := os.Getenv("REGISTRY_URL")
commonTarget := &models.RepTarget{
URL: endPoint,
Name: TestTargetName,
Username: adminName,
Password: adminPwd,
}
_, _ = dao.AddRepTarget(*commonTarget)
}
func CommonGetTarget() int {
target, _ := dao.GetRepTargetByName(TestTargetName)
return int(target.ID)
}
func CommonDelTarget() {
target, _ := dao.GetRepTargetByName(TestTargetName)
_ = dao.DeleteRepTarget(target.ID)
}
func CommonPolicyEabled(policyID int, enabled int) {
_ = dao.UpdateRepPolicyEnablement(int64(policyID), enabled)
}

View File

@ -28,11 +28,6 @@ const (
testAcceptHeader = "text/plain" testAcceptHeader = "text/plain"
adminName = "admin" adminName = "admin"
adminPwd = "Harbor12345" adminPwd = "Harbor12345"
//Prepare Test info
TestUserName = "testUser0001"
TestUserPwd = "testUser0001"
TestUserEmail = "testUser0001@mydomain.com"
TestProName = "testProject0001"
) )
var admin, unknownUsr, testUser *usrInfo var admin, unknownUsr, testUser *usrInfo
@ -84,6 +79,10 @@ func init() {
beego.Router("/api/targets/:id([0-9]+)", &TargetAPI{}) beego.Router("/api/targets/:id([0-9]+)", &TargetAPI{})
beego.Router("/api/targets/:id([0-9]+)/policies/", &TargetAPI{}, "get:ListPolicies") beego.Router("/api/targets/:id([0-9]+)/policies/", &TargetAPI{}, "get:ListPolicies")
beego.Router("/api/targets/ping", &TargetAPI{}, "post:Ping") beego.Router("/api/targets/ping", &TargetAPI{}, "post:Ping")
beego.Router("/api/policies/replication/:id([0-9]+)", &RepPolicyAPI{})
beego.Router("/api/policies/replication", &RepPolicyAPI{}, "get:List")
beego.Router("/api/policies/replication", &RepPolicyAPI{}, "post:Post;delete:Delete")
beego.Router("/api/policies/replication/:id([0-9]+)/enablement", &RepPolicyAPI{}, "put:UpdateEnablement")
_ = updateInitPassword(1, "Harbor12345") _ = updateInitPassword(1, "Harbor12345")
@ -603,6 +602,96 @@ func (a api) DeleteTargetsByID(authInfo usrInfo, targetID string) (int, error) {
return httpStatusCode, err return httpStatusCode, err
} }
//--------------------Replication_Policy Test--------------------------------//
//Create a new replication policy
func (a api) AddPolicy(authInfo usrInfo, repPolicy apilib.RepPolicyPost) (int, error) {
_sling := sling.New().Post(a.basePath)
path := "/api/policies/replication/"
_sling = _sling.Path(path)
_sling = _sling.BodyJSON(repPolicy)
httpStatusCode, _, err := request(_sling, jsonAcceptHeader, authInfo)
return httpStatusCode, err
}
//List policies by policyName and projectID
func (a api) ListPolicies(authInfo usrInfo, policyName string, proID string) (int, []apilib.RepPolicy, error) {
_sling := sling.New().Get(a.basePath)
path := "/api/policies/replication/"
_sling = _sling.Path(path)
type QueryParams struct {
PolicyName string `url:"name"`
ProjectID string `url:"project_id"`
}
_sling = _sling.QueryStruct(&QueryParams{PolicyName: policyName, ProjectID: proID})
var successPayload []apilib.RepPolicy
httpStatusCode, body, err := request(_sling, jsonAcceptHeader, authInfo)
if err == nil && httpStatusCode == 200 {
err = json.Unmarshal(body, &successPayload)
}
return httpStatusCode, successPayload, err
}
//Get replication policy by policyID
func (a api) GetPolicyByID(authInfo usrInfo, policyID string) (int, error) {
_sling := sling.New().Get(a.basePath)
path := "/api/policies/replication/" + policyID
_sling = _sling.Path(path)
httpStatusCode, _, err := request(_sling, jsonAcceptHeader, authInfo)
return httpStatusCode, err
}
//Update policyInfo by policyID
func (a api) PutPolicyInfoByID(authInfo usrInfo, policyID string, policyUpdate apilib.RepPolicyUpdate) (int, error) {
_sling := sling.New().Put(a.basePath)
path := "/api/policies/replication/" + policyID
_sling = _sling.Path(path)
_sling = _sling.BodyJSON(policyUpdate)
httpStatusCode, _, err := request(_sling, jsonAcceptHeader, authInfo)
return httpStatusCode, err
}
//Update policy enablement flag by policyID
func (a api) PutPolicyEnableByID(authInfo usrInfo, policyID string, policyEnable apilib.RepPolicyEnablementReq) (int, error) {
_sling := sling.New().Put(a.basePath)
path := "/api/policies/replication/" + policyID + "/enablement"
_sling = _sling.Path(path)
_sling = _sling.BodyJSON(policyEnable)
httpStatusCode, _, err := request(_sling, jsonAcceptHeader, authInfo)
return httpStatusCode, err
}
//Delete policy by policyID
func (a api) DeletePolicyByID(authInfo usrInfo, policyID string) (int, error) {
_sling := sling.New().Delete(a.basePath)
path := "/api/policies/replication/" + policyID
_sling = _sling.Path(path)
httpStatusCode, _, err := request(_sling, jsonAcceptHeader, authInfo)
return httpStatusCode, err
}
//Return projects created by Harbor //Return projects created by Harbor
//func (a HarborApi) ProjectsGet (projectName string, isPublic int32) ([]Project, error) { //func (a HarborApi) ProjectsGet (projectName string, isPublic int32) ([]Project, error) {
// } // }

View File

@ -0,0 +1,267 @@
package api
import (
"fmt"
"github.com/stretchr/testify/assert"
"github.com/vmware/harbor/tests/apitests/apilib"
"strconv"
"testing"
)
const (
addPolicyName = "testPolicy"
)
var addPolicyID int
func TestPoliciesPost(t *testing.T) {
var httpStatusCode int
var err error
assert := assert.New(t)
apiTest := newHarborAPI()
//add target
CommonAddTarget()
targetID := int64(CommonGetTarget())
repPolicy := &apilib.RepPolicyPost{int64(1), targetID, addPolicyName}
fmt.Println("Testing Policies Post API")
//-------------------case 1 : response code = 201------------------------//
fmt.Println("case 1 : response code = 201")
httpStatusCode, err = apiTest.AddPolicy(*admin, *repPolicy)
if err != nil {
t.Error("Error while add policy", err.Error())
t.Log(err)
} else {
assert.Equal(int(201), httpStatusCode, "httpStatusCode should be 201")
}
//-------------------case 2 : response code = 409------------------------//
fmt.Println("case 1 : response code = 409:policy already exists")
httpStatusCode, err = apiTest.AddPolicy(*admin, *repPolicy)
if err != nil {
t.Error("Error while add policy", err.Error())
t.Log(err)
} else {
assert.Equal(int(409), httpStatusCode, "httpStatusCode should be 409")
}
//-------------------case 3 : response code = 401------------------------//
fmt.Println("case 3 : response code = 401: User need to log in first.")
httpStatusCode, err = apiTest.AddPolicy(*unknownUsr, *repPolicy)
if err != nil {
t.Error("Error while add policy", err.Error())
t.Log(err)
} else {
assert.Equal(int(401), httpStatusCode, "httpStatusCode should be 401")
}
//-------------------case 4 : response code = 400------------------------//
fmt.Println("case 4 : response code = 400:project_id invalid.")
repPolicy = &apilib.RepPolicyPost{TargetId: targetID, Name: addPolicyName}
httpStatusCode, err = apiTest.AddPolicy(*admin, *repPolicy)
if err != nil {
t.Error("Error while add policy", err.Error())
t.Log(err)
} else {
assert.Equal(int(400), httpStatusCode, "httpStatusCode should be 400")
}
//-------------------case 5 : response code = 400------------------------//
fmt.Println("case 5 : response code = 400:project_id does not exist.")
repPolicy.ProjectId = int64(1111)
httpStatusCode, err = apiTest.AddPolicy(*admin, *repPolicy)
if err != nil {
t.Error("Error while add policy", err.Error())
t.Log(err)
} else {
assert.Equal(int(400), httpStatusCode, "httpStatusCode should be 400")
}
//-------------------case 6 : response code = 400------------------------//
fmt.Println("case 6 : response code = 400:target_id invalid.")
repPolicy = &apilib.RepPolicyPost{ProjectId: int64(1), Name: addPolicyName}
httpStatusCode, err = apiTest.AddPolicy(*admin, *repPolicy)
if err != nil {
t.Error("Error while add policy", err.Error())
t.Log(err)
} else {
assert.Equal(int(400), httpStatusCode, "httpStatusCode should be 400")
}
//-------------------case 7 : response code = 400------------------------//
fmt.Println("case 6 : response code = 400:target_id does not exist.")
repPolicy.TargetId = int64(1111)
httpStatusCode, err = apiTest.AddPolicy(*admin, *repPolicy)
if err != nil {
t.Error("Error while add policy", err.Error())
t.Log(err)
} else {
assert.Equal(int(400), httpStatusCode, "httpStatusCode should be 400")
}
}
func TestPoliciesList(t *testing.T) {
var httpStatusCode int
var err error
var reslut []apilib.RepPolicy
assert := assert.New(t)
apiTest := newHarborAPI()
fmt.Println("Testing Policies Get/List API")
//-------------------case 1 : response code = 200------------------------//
fmt.Println("case 1 : response code = 200")
projectID := "1"
httpStatusCode, reslut, err = apiTest.ListPolicies(*admin, addPolicyName, projectID)
if err != nil {
t.Error("Error while get policies", err.Error())
t.Log(err)
} else {
assert.Equal(int(200), httpStatusCode, "httpStatusCode should be 200")
addPolicyID = int(reslut[0].Id)
}
//-------------------case 2 : response code = 400------------------------//
fmt.Println("case 2 : response code = 400:invalid projectID")
projectID = "cc"
httpStatusCode, reslut, err = apiTest.ListPolicies(*admin, addPolicyName, projectID)
if err != nil {
t.Error("Error while get policies", err.Error())
t.Log(err)
} else {
assert.Equal(int(400), httpStatusCode, "httpStatusCode should be 400")
}
}
func TestPolicyGet(t *testing.T) {
var httpStatusCode int
var err error
assert := assert.New(t)
apiTest := newHarborAPI()
fmt.Println("Testing Policy Get API by PolicyID")
//-------------------case 1 : response code = 200------------------------//
fmt.Println("case 1 : response code = 200")
policyID := strconv.Itoa(addPolicyID)
httpStatusCode, err = apiTest.GetPolicyByID(*admin, policyID)
if err != nil {
t.Error("Error while get policy", err.Error())
t.Log(err)
} else {
assert.Equal(int(200), httpStatusCode, "httpStatusCode should be 200")
}
}
func TestPolicyUpdateInfo(t *testing.T) {
var httpStatusCode int
var err error
targetID := int64(CommonGetTarget())
policyInfo := &apilib.RepPolicyUpdate{TargetId: targetID, Name: "testNewName"}
assert := assert.New(t)
apiTest := newHarborAPI()
fmt.Println("Testing Policy PUT API to update policyInfo")
//-------------------case 1 : response code = 200------------------------//
fmt.Println("case 1 : response code = 200")
policyID := strconv.Itoa(addPolicyID)
httpStatusCode, err = apiTest.PutPolicyInfoByID(*admin, policyID, *policyInfo)
if err != nil {
t.Error("Error while update policyInfo", err.Error())
t.Log(err)
} else {
assert.Equal(int(200), httpStatusCode, "httpStatusCode should be 200")
}
}
func TestPolicyUpdateEnablement(t *testing.T) {
var httpStatusCode int
var err error
enablement := &apilib.RepPolicyEnablementReq{int32(0)}
assert := assert.New(t)
apiTest := newHarborAPI()
fmt.Println("Testing Policy PUT API to update policy enablement")
//-------------------case 1 : response code = 200------------------------//
fmt.Println("case 1 : response code = 200")
policyID := strconv.Itoa(addPolicyID)
httpStatusCode, err = apiTest.PutPolicyEnableByID(*admin, policyID, *enablement)
if err != nil {
t.Error("Error while put policy enablement", 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,Not Found")
policyID = "111"
httpStatusCode, err = apiTest.PutPolicyEnableByID(*admin, policyID, *enablement)
if err != nil {
t.Error("Error while put policy enablement", err.Error())
t.Log(err)
} else {
assert.Equal(int(404), httpStatusCode, "httpStatusCode should be 404")
}
}
func TestPolicyDelete(t *testing.T) {
var httpStatusCode int
var err error
assert := assert.New(t)
apiTest := newHarborAPI()
fmt.Println("Testing Policy Delete API")
//-------------------case 1 : response code = 412------------------------//
fmt.Println("case 1 : response code = 412:policy is enabled, can not be deleted")
CommonPolicyEabled(addPolicyID, 1)
policyID := strconv.Itoa(addPolicyID)
httpStatusCode, err = apiTest.DeletePolicyByID(*admin, policyID)
if err != nil {
t.Error("Error while delete policy", err.Error())
t.Log(err)
} else {
assert.Equal(int(412), httpStatusCode, "httpStatusCode should be 412")
}
//-------------------case 2 : response code = 200------------------------//
fmt.Println("case 2 : response code = 200")
CommonPolicyEabled(addPolicyID, 0)
policyID = strconv.Itoa(addPolicyID)
httpStatusCode, err = apiTest.DeletePolicyByID(*admin, policyID)
if err != nil {
t.Error("Error while delete policy", err.Error())
t.Log(err)
} else {
assert.Equal(int(200), httpStatusCode, "httpStatusCode should be 200")
}
CommonDelTarget()
}