mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-26 20:26:13 +01:00
modify test cases
This commit is contained in:
parent
3718ee4177
commit
18402f84f0
@ -7,6 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
@ -453,7 +454,8 @@ func (a testapi) PutProjectMember(authInfo usrInfo, projectID string, userID str
|
|||||||
|
|
||||||
//-------------------------Repositories Test---------------------------------------//
|
//-------------------------Repositories Test---------------------------------------//
|
||||||
//Return relevant repos of projectID
|
//Return relevant repos of projectID
|
||||||
func (a testapi) GetRepos(authInfo usrInfo, projectID, detail string) (int, error) {
|
func (a testapi) GetRepos(authInfo usrInfo, projectID,
|
||||||
|
keyword, detail string) (int, interface{}, error) {
|
||||||
_sling := sling.New().Get(a.basePath)
|
_sling := sling.New().Get(a.basePath)
|
||||||
|
|
||||||
path := "/api/repositories/"
|
path := "/api/repositories/"
|
||||||
@ -463,14 +465,36 @@ func (a testapi) GetRepos(authInfo usrInfo, projectID, detail string) (int, erro
|
|||||||
type QueryParams struct {
|
type QueryParams struct {
|
||||||
ProjectID string `url:"project_id"`
|
ProjectID string `url:"project_id"`
|
||||||
Detail string `url:"detail"`
|
Detail string `url:"detail"`
|
||||||
|
Keyword string `url:"q"`
|
||||||
}
|
}
|
||||||
|
|
||||||
_sling = _sling.QueryStruct(&QueryParams{
|
_sling = _sling.QueryStruct(&QueryParams{
|
||||||
ProjectID: projectID,
|
ProjectID: projectID,
|
||||||
Detail: detail,
|
Detail: detail,
|
||||||
|
Keyword: keyword,
|
||||||
})
|
})
|
||||||
httpStatusCode, _, err := request(_sling, jsonAcceptHeader, authInfo)
|
code, body, err := request(_sling, jsonAcceptHeader, authInfo)
|
||||||
return httpStatusCode, err
|
if err != nil {
|
||||||
|
return 0, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if code == http.StatusOK {
|
||||||
|
if detail == "1" || detail == "true" {
|
||||||
|
repositories := []repoResp{}
|
||||||
|
if err = json.Unmarshal(body, &repositories); err != nil {
|
||||||
|
return 0, nil, err
|
||||||
|
}
|
||||||
|
return code, repositories, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
repositories := []string{}
|
||||||
|
if err = json.Unmarshal(body, &repositories); err != nil {
|
||||||
|
return 0, nil, err
|
||||||
|
}
|
||||||
|
return code, repositories, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return code, nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//Get tags of a relevant repository
|
//Get tags of a relevant repository
|
||||||
|
@ -5,32 +5,36 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
// "github.com/vmware/harbor/tests/apitests/apilib"
|
|
||||||
// "strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetRepos(t *testing.T) {
|
func TestGetRepos(t *testing.T) {
|
||||||
var httpStatusCode int
|
|
||||||
var err error
|
|
||||||
|
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
apiTest := newHarborAPI()
|
apiTest := newHarborAPI()
|
||||||
projectID := "1"
|
projectID := "1"
|
||||||
|
keyword := "hello-world"
|
||||||
|
detail := "true"
|
||||||
|
|
||||||
fmt.Println("Testing Repos Get API")
|
fmt.Println("Testing Repos Get API")
|
||||||
//-------------------case 1 : response code = 200------------------------//
|
//-------------------case 1 : response code = 200------------------------//
|
||||||
fmt.Println("case 1 : response code = 200")
|
fmt.Println("case 1 : response code = 200")
|
||||||
httpStatusCode, err = apiTest.GetRepos(*admin, projectID, "true")
|
code, repositories, err := apiTest.GetRepos(*admin, projectID, keyword, detail)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("Error whihle get repos by projectID", err.Error())
|
t.Errorf("failed to get repositories: %v", err)
|
||||||
t.Log(err)
|
|
||||||
} else {
|
} else {
|
||||||
assert.Equal(int(200), httpStatusCode, "httpStatusCode should be 200")
|
assert.Equal(int(200), code, "response code should be 200")
|
||||||
|
if repos, ok := repositories.([]repoResp); ok {
|
||||||
|
assert.Equal(int(1), len(repos), "the length of repositories should be 1")
|
||||||
|
assert.Equal(repos[0].Name, "library/hello-world", "unexpected repository name")
|
||||||
|
} else {
|
||||||
|
t.Error("the response should return more info as detail is true")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------case 2 : response code = 400------------------------//
|
//-------------------case 2 : response code = 400------------------------//
|
||||||
fmt.Println("case 2 : response code = 409,invalid project_id")
|
fmt.Println("case 2 : response code = 400,invalid project_id")
|
||||||
projectID = "ccc"
|
projectID = "ccc"
|
||||||
httpStatusCode, err = apiTest.GetRepos(*admin, projectID, "0")
|
httpStatusCode, _, err := apiTest.GetRepos(*admin, projectID, keyword, detail)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("Error whihle get repos by projectID", err.Error())
|
t.Error("Error whihle get repos by projectID", err.Error())
|
||||||
t.Log(err)
|
t.Log(err)
|
||||||
@ -40,7 +44,7 @@ func TestGetRepos(t *testing.T) {
|
|||||||
//-------------------case 3 : response code = 404------------------------//
|
//-------------------case 3 : response code = 404------------------------//
|
||||||
fmt.Println("case 3 : response code = 404:project not found")
|
fmt.Println("case 3 : response code = 404:project not found")
|
||||||
projectID = "111"
|
projectID = "111"
|
||||||
httpStatusCode, err = apiTest.GetRepos(*admin, projectID, "0")
|
httpStatusCode, _, err = apiTest.GetRepos(*admin, projectID, keyword, detail)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("Error whihle get repos by projectID", err.Error())
|
t.Error("Error whihle get repos by projectID", err.Error())
|
||||||
t.Log(err)
|
t.Log(err)
|
||||||
@ -48,6 +52,23 @@ func TestGetRepos(t *testing.T) {
|
|||||||
assert.Equal(int(404), httpStatusCode, "httpStatusCode should be 404")
|
assert.Equal(int(404), httpStatusCode, "httpStatusCode should be 404")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-------------------case 4 : response code = 200------------------------//
|
||||||
|
fmt.Println("case 4 : response code = 200")
|
||||||
|
projectID = "1"
|
||||||
|
detail = "false"
|
||||||
|
code, repositories, err = apiTest.GetRepos(*admin, projectID, keyword, detail)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to get repositories: %v", err)
|
||||||
|
} else {
|
||||||
|
assert.Equal(int(200), code, "response code should be 200")
|
||||||
|
if repos, ok := repositories.([]string); ok {
|
||||||
|
assert.Equal(int(1), len(repos), "the length of repositories should be 1")
|
||||||
|
assert.Equal(repos[0], "library/hello-world", "unexpected repository name")
|
||||||
|
} else {
|
||||||
|
t.Error("the response should not return detail info as detail is false")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Printf("\n")
|
fmt.Printf("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user