Merge pull request #4608 from ywk253100/180408_travis

Fix log API random failure on travis
This commit is contained in:
Daniel Jiang 2018-04-10 16:00:25 +08:00 committed by GitHub
commit e0ac6708d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 79 deletions

View File

@ -157,7 +157,8 @@ func handleAndParse(r *testingRequest, v interface{}) error {
} }
func runCodeCheckingCases(t *testing.T, cases ...*codeCheckingCase) { func runCodeCheckingCases(t *testing.T, cases ...*codeCheckingCase) {
for _, c := range cases { for i, c := range cases {
t.Logf("running case %d ...", i)
resp, err := handle(c.request) resp, err := handle(c.request)
require.Nil(t, err) require.Nil(t, err)
equal := assert.Equal(t, c.code, resp.Code) equal := assert.Equal(t, c.code, resp.Code)

View File

@ -15,96 +15,82 @@ package api
import ( import (
"fmt" "fmt"
"strconv" "net/http"
"testing" "testing"
"time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vmware/harbor/src/common/dao"
"github.com/vmware/harbor/src/common/models" "github.com/vmware/harbor/src/common/models"
"github.com/vmware/harbor/tests/apitests/apilib"
) )
func TestLogGet(t *testing.T) { func TestLogGet(t *testing.T) {
fmt.Println("Testing Log API") fmt.Println("Testing Log API")
apiTest := newHarborAPI()
assert := assert.New(t)
CommonAddUser() var projectID int64 = 1
username := "user_for_testing_log_api"
statusCode, result, err := apiTest.LogGet(*testUser) repository := "repository_for_testing_log_api"
assert.Nil(err) tag := "tag_for_testing_log_api"
assert.Equal(200, statusCode) operation := "op_for_test_log_api"
now := time.Now()
logNum := len(result) err := dao.AddAccessLog(models.AccessLog{
ProjectID: projectID,
fmt.Println("add the project first.") Username: username,
project := apilib.ProjectReq{ RepoName: repository,
ProjectName: "project_for_test_log", RepoTag: tag,
Metadata: map[string]string{models.ProMetaPublic: "true"}, Operation: operation,
} OpTime: now,
reply, err := apiTest.ProjectsPost(*testUser, project)
if err != nil {
t.Error("Error while creat project", err.Error())
t.Log(err)
} else {
assert.Equal(int(201), reply, "Case 2: Project creation status should be 201")
}
//case 1: right parameters, expect the right output
statusCode, result, err = apiTest.LogGet(*testUser)
if err != nil {
t.Error("Error while get log information", err.Error())
t.Log(err)
} else {
assert.Equal(logNum+1, len(result), "lines of logs should be equal")
num, index := getLog(result)
if num != 1 {
assert.Equal(1, num, "add my_project log number should be 1")
} else {
assert.Equal("project_for_test_log/", result[index].RepoName)
assert.Equal("N/A", result[index].RepoTag, "RepoTag should be equal")
assert.Equal("create", result[index].Operation, "Operation should be equal")
}
}
fmt.Println("log ", result)
//get the project
var projects []apilib.Project
var addProjectID int32
httpStatusCode, projects, err := apiTest.ProjectsGet(
&apilib.ProjectQuery{
Name: project.ProjectName,
Owner: testUser.Name,
Public: true,
}) })
if err != nil { require.Nil(t, err)
t.Error("Error while search project by proName and isPublic", err.Error()) defer dao.GetOrmer().QueryTable(&models.AccessLog{}).
t.Log(err) Filter("username", username).Delete()
} else {
assert.Equal(int(200), httpStatusCode, "httpStatusCode should be 200")
addProjectID = projects[0].ProjectId
}
//delete the project url := "/api/logs"
projectID := strconv.Itoa(int(addProjectID)) // 401
httpStatusCode, err = apiTest.ProjectsDelete(*testUser, projectID) cc := &codeCheckingCase{
if err != nil { request: &testingRequest{
t.Error("Error while delete project", err.Error()) method: http.MethodGet,
t.Log(err) url: url,
} else { },
assert.Equal(int(200), httpStatusCode, "Case 1: Project creation status should be 200") code: http.StatusUnauthorized,
//t.Log(result)
} }
CommonDelUser() runCodeCheckingCases(t, cc)
fmt.Printf("\n")
}
func getLog(result []apilib.AccessLog) (int, int) { // 200, empty log list
var num, index int c := &testingRequest{
for i := 0; i < len(result); i++ { method: http.MethodGet,
if result[i].RepoName == "project_for_test_log/" { url: url,
num++ credential: nonSysAdmin,
index = i queryStruct: struct {
Username string `url:"username"`
Repository string `url:"repository"`
Tag string `url:"tag"`
Operation string `url:"operation"`
BeginTimestamp int64 `url:"begin_timestamp"`
EndTimestamp int64 `url:"end_timestamp"`
}{
Username: username,
Repository: repository,
Tag: tag,
Operation: operation,
BeginTimestamp: now.Add(-1 * time.Second).Unix(),
EndTimestamp: now.Add(1 * time.Second).Unix(),
},
} }
} logs := []*models.AccessLog{}
return num, index err = handleAndParse(c, &logs)
require.Nil(t, err)
require.Equal(t, 0, len(logs))
// 200
c.credential = projGuest
err = handleAndParse(c, &logs)
require.Nil(t, err)
require.Equal(t, 1, len(logs))
assert.Equal(t, projectID, logs[0].ProjectID)
assert.Equal(t, username, logs[0].Username)
assert.Equal(t, repository, logs[0].RepoName)
assert.Equal(t, tag, logs[0].RepoTag)
assert.Equal(t, operation, logs[0].Operation)
} }