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"
repository := "repository_for_testing_log_api"
tag := "tag_for_testing_log_api"
operation := "op_for_test_log_api"
now := time.Now()
err := dao.AddAccessLog(models.AccessLog{
ProjectID: projectID,
Username: username,
RepoName: repository,
RepoTag: tag,
Operation: operation,
OpTime: now,
})
require.Nil(t, err)
defer dao.GetOrmer().QueryTable(&models.AccessLog{}).
Filter("username", username).Delete()
statusCode, result, err := apiTest.LogGet(*testUser) url := "/api/logs"
assert.Nil(err) // 401
assert.Equal(200, statusCode) cc := &codeCheckingCase{
request: &testingRequest{
logNum := len(result) method: http.MethodGet,
url: url,
fmt.Println("add the project first.") },
project := apilib.ProjectReq{ code: http.StatusUnauthorized,
ProjectName: "project_for_test_log",
Metadata: map[string]string{models.ProMetaPublic: "true"},
} }
runCodeCheckingCases(t, cc)
reply, err := apiTest.ProjectsPost(*testUser, project) // 200, empty log list
if err != nil { c := &testingRequest{
t.Error("Error while creat project", err.Error()) method: http.MethodGet,
t.Log(err) url: url,
} else { credential: nonSysAdmin,
assert.Equal(int(201), reply, "Case 2: Project creation status should be 201") 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(),
},
} }
//case 1: right parameters, expect the right output logs := []*models.AccessLog{}
statusCode, result, err = apiTest.LogGet(*testUser) err = handleAndParse(c, &logs)
if err != nil { require.Nil(t, err)
t.Error("Error while get log information", err.Error()) require.Equal(t, 0, len(logs))
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 // 200
var projects []apilib.Project c.credential = projGuest
var addProjectID int32 err = handleAndParse(c, &logs)
httpStatusCode, projects, err := apiTest.ProjectsGet( require.Nil(t, err)
&apilib.ProjectQuery{ require.Equal(t, 1, len(logs))
Name: project.ProjectName, assert.Equal(t, projectID, logs[0].ProjectID)
Owner: testUser.Name, assert.Equal(t, username, logs[0].Username)
Public: true, assert.Equal(t, repository, logs[0].RepoName)
}) assert.Equal(t, tag, logs[0].RepoTag)
if err != nil { assert.Equal(t, operation, logs[0].Operation)
t.Error("Error while search project by proName and isPublic", err.Error())
t.Log(err)
} else {
assert.Equal(int(200), httpStatusCode, "httpStatusCode should be 200")
addProjectID = projects[0].ProjectId
}
//delete the project
projectID := strconv.Itoa(int(addProjectID))
httpStatusCode, err = apiTest.ProjectsDelete(*testUser, projectID)
if err != nil {
t.Error("Error while delete project", err.Error())
t.Log(err)
} else {
assert.Equal(int(200), httpStatusCode, "Case 1: Project creation status should be 200")
//t.Log(result)
}
CommonDelUser()
fmt.Printf("\n")
}
func getLog(result []apilib.AccessLog) (int, int) {
var num, index int
for i := 0; i < len(result); i++ {
if result[i].RepoName == "project_for_test_log/" {
num++
index = i
}
}
return num, index
} }