harbor/dao/dao_test.go

1375 lines
38 KiB
Go
Raw Normal View History

2016-02-01 12:59:10 +01:00
/*
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.
*/
2016-02-26 11:54:14 +01:00
2016-02-24 11:16:16 +01:00
package dao
2016-02-01 12:59:10 +01:00
import (
"os"
"testing"
"time"
"github.com/astaxie/beego/orm"
2016-05-10 13:38:50 +02:00
"github.com/vmware/harbor/models"
"github.com/vmware/harbor/utils/log"
2016-02-01 12:59:10 +01:00
)
func execUpdate(o orm.Ormer, sql string, params ...interface{}) error {
2016-02-01 12:59:10 +01:00
p, err := o.Raw(sql).Prepare()
if err != nil {
return err
}
defer p.Close()
_, err = p.Exec(params...)
2016-02-01 12:59:10 +01:00
if err != nil {
return err
}
return nil
}
func clearUp(username string) {
2016-03-28 09:34:41 +02:00
var err error
2016-02-01 12:59:10 +01:00
o := orm.NewOrm()
o.Begin()
2016-03-28 09:34:41 +02:00
err = execUpdate(o, `delete pm
from project_member pm
join user u
on pm.user_id = u.user_id
where u.username = ?`, username)
if err != nil {
o.Rollback()
log.Error(err)
}
err = execUpdate(o, `delete pm
from project_member pm
join project p
on pm.project_id = p.project_id
where p.name = ?`, projectName)
2016-02-01 12:59:10 +01:00
if err != nil {
o.Rollback()
2016-03-28 09:34:41 +02:00
log.Error(err)
2016-02-01 12:59:10 +01:00
}
2016-03-28 09:34:41 +02:00
err = execUpdate(o, `delete al
from access_log al
join user u
on al.user_id = u.user_id
where u.username = ?`, username)
2016-02-01 12:59:10 +01:00
if err != nil {
o.Rollback()
2016-03-28 09:34:41 +02:00
log.Error(err)
2016-02-01 12:59:10 +01:00
}
2016-03-28 09:34:41 +02:00
err = execUpdate(o, `delete al
from access_log al
join project p
on al.project_id = p.project_id
where p.name = ?`, projectName)
2016-02-01 12:59:10 +01:00
if err != nil {
o.Rollback()
2016-03-28 09:34:41 +02:00
log.Error(err)
2016-02-01 12:59:10 +01:00
}
2016-03-28 09:34:41 +02:00
err = execUpdate(o, `delete from project where name = ?`, projectName)
2016-02-01 12:59:10 +01:00
if err != nil {
o.Rollback()
2016-03-28 09:34:41 +02:00
log.Error(err)
2016-02-01 12:59:10 +01:00
}
2016-03-28 09:34:41 +02:00
err = execUpdate(o, `delete from user where username = ?`, username)
2016-02-01 12:59:10 +01:00
if err != nil {
o.Rollback()
2016-03-28 09:34:41 +02:00
log.Error(err)
2016-02-01 12:59:10 +01:00
}
err = execUpdate(o, `delete from replication_job where id < 99`)
if err != nil {
log.Error(err)
}
err = execUpdate(o, `delete from replication_policy where id < 99`)
if err != nil {
log.Error(err)
}
err = execUpdate(o, `delete from replication_target where id < 99`)
if err != nil {
log.Error(err)
}
2016-02-01 12:59:10 +01:00
o.Commit()
}
2016-02-26 04:26:54 +01:00
const username string = "Tester01"
const projectName string = "test_project"
2016-05-03 10:54:01 +02:00
const repoTag string = "test1.1"
const repoTag2 string = "test1.2"
2016-02-26 04:26:54 +01:00
const SysAdmin int = 1
const projectAdmin int = 2
const developer int = 3
const guest int = 4
2016-02-01 12:59:10 +01:00
2016-02-26 04:26:54 +01:00
const publicityOn = 1
const publicityOff = 0
2016-02-01 12:59:10 +01:00
func TestMain(m *testing.M) {
2016-02-23 13:49:59 +01:00
dbHost := os.Getenv("DB_HOST")
if len(dbHost) == 0 {
log.Fatalf("environment variable DB_HOST is not set")
}
dbUser := os.Getenv("DB_USR")
if len(dbUser) == 0 {
log.Fatalf("environment variable DB_USR is not set")
}
dbPort := os.Getenv("DB_PORT")
if len(dbPort) == 0 {
2016-02-24 03:53:25 +01:00
log.Fatalf("environment variable DB_PORT is not set")
2016-02-23 13:49:59 +01:00
}
dbPassword := os.Getenv("DB_PWD")
2016-02-01 12:59:10 +01:00
2016-03-29 07:33:49 +02:00
log.Infof("DB_HOST: %s, DB_USR: %s, DB_PORT: %s, DB_PWD: %s\n", dbHost, dbUser, dbPort, dbPassword)
2016-04-01 13:23:31 +02:00
os.Setenv("MYSQL_HOST", dbHost)
os.Setenv("MYSQL_PORT", dbPort)
2016-02-23 13:49:59 +01:00
os.Setenv("MYSQL_USR", dbUser)
os.Setenv("MYSQL_PWD", dbPassword)
os.Setenv("AUTH_MODE", "db_auth")
2016-02-24 11:16:16 +01:00
InitDB()
2016-02-26 04:26:54 +01:00
clearUp(username)
2016-02-23 13:49:59 +01:00
os.Exit(m.Run())
2016-02-01 12:59:10 +01:00
}
2016-02-23 13:49:59 +01:00
func TestRegister(t *testing.T) {
2016-02-01 12:59:10 +01:00
user := models.User{
2016-02-26 04:26:54 +01:00
Username: username,
2016-02-01 12:59:10 +01:00
Email: "tester01@vmware.com",
Password: "Abc12345",
Realname: "tester01",
Comment: "register",
}
2016-02-24 11:16:16 +01:00
_, err := Register(user)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in Register: %v", err)
2016-02-01 12:59:10 +01:00
}
//Check if user registered successfully.
queryUser := models.User{
2016-02-26 04:26:54 +01:00
Username: username,
2016-02-01 12:59:10 +01:00
}
2016-02-24 11:16:16 +01:00
newUser, err := GetUser(queryUser)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in GetUser: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-26 04:26:54 +01:00
if newUser.Username != username {
t.Errorf("Username does not match, expected: %s, actual: %s", username, newUser.Username)
2016-02-23 13:49:59 +01:00
}
if newUser.Email != "tester01@vmware.com" {
t.Errorf("Email does not match, expected: %s, actual: %s", "tester01@vmware.com", newUser.Email)
}
2016-02-01 12:59:10 +01:00
}
2016-02-23 13:49:59 +01:00
func TestUserExists(t *testing.T) {
2016-02-01 12:59:10 +01:00
var exists bool
var err error
2016-02-26 04:26:54 +01:00
exists, err = UserExists(models.User{Username: username}, "username")
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in UserExists: %v", err)
}
if !exists {
2016-02-26 04:26:54 +01:00
t.Errorf("User %s was inserted but does not exist", username)
2016-02-01 12:59:10 +01:00
}
2016-02-24 11:16:16 +01:00
exists, err = UserExists(models.User{Email: "tester01@vmware.com"}, "email")
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in UserExists: %v", err)
}
if !exists {
t.Errorf("User with email %s inserted but does not exist", "tester01@vmware.com")
}
2016-02-24 11:16:16 +01:00
exists, err = UserExists(models.User{Username: "NOTHERE"}, "username")
2016-02-23 13:49:59 +01:00
if err != nil {
t.Errorf("Error occurred in UserExists: %v", err)
}
if exists {
t.Errorf("User %s was not inserted but does exist", "NOTHERE")
2016-02-01 12:59:10 +01:00
}
}
2016-02-23 13:49:59 +01:00
func TestLoginByUserName(t *testing.T) {
2016-02-01 12:59:10 +01:00
userQuery := models.User{
2016-02-26 04:26:54 +01:00
Username: username,
2016-02-01 12:59:10 +01:00
Password: "Abc12345",
}
2016-04-15 07:17:32 +02:00
loginUser, err := LoginByDb(models.AuthModel{
Principal: userQuery.Username,
Password: userQuery.Password,
})
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in LoginByDb: %v", err)
2016-02-01 12:59:10 +01:00
}
if loginUser == nil {
2016-02-23 13:49:59 +01:00
t.Errorf("No found for user logined by username and password: %v", userQuery)
2016-02-01 12:59:10 +01:00
}
2016-02-26 04:26:54 +01:00
if loginUser.Username != username {
t.Errorf("User's username does not match after login, expected: %s, actual: %s", username, loginUser.Username)
2016-02-23 13:49:59 +01:00
}
2016-02-01 12:59:10 +01:00
}
2016-02-23 13:49:59 +01:00
func TestLoginByEmail(t *testing.T) {
2016-02-01 12:59:10 +01:00
userQuery := models.User{
Email: "tester01@vmware.com",
Password: "Abc12345",
}
2016-04-15 07:17:32 +02:00
loginUser, err := LoginByDb(models.AuthModel{
Principal: userQuery.Email,
Password: userQuery.Password,
})
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in LoginByDb: %v", err)
2016-02-01 12:59:10 +01:00
}
if loginUser == nil {
2016-02-23 13:49:59 +01:00
t.Errorf("No found for user logined by email and password : %v", userQuery)
}
2016-02-26 04:26:54 +01:00
if loginUser.Username != username {
t.Errorf("User's username does not match after login, expected: %s, actual: %s", username, loginUser.Username)
2016-02-01 12:59:10 +01:00
}
}
var currentUser *models.User
2016-02-23 13:49:59 +01:00
func TestGetUser(t *testing.T) {
2016-02-01 12:59:10 +01:00
queryUser := models.User{
2016-02-26 04:26:54 +01:00
Username: username,
2016-02-01 12:59:10 +01:00
}
var err error
2016-02-24 11:16:16 +01:00
currentUser, err = GetUser(queryUser)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in GetUser: %v", err)
2016-02-01 12:59:10 +01:00
}
if currentUser == nil {
2016-02-23 13:49:59 +01:00
t.Errorf("No user found queried by user query: %+v", queryUser)
}
if currentUser.Email != "tester01@vmware.com" {
t.Errorf("the user's email does not match, expected: tester01@vmware.com, actual: %s", currentUser.Email)
2016-02-01 12:59:10 +01:00
}
}
2016-02-23 13:49:59 +01:00
func TestListUsers(t *testing.T) {
2016-02-24 11:16:16 +01:00
users, err := ListUsers(models.User{})
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in ListUsers: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-23 13:49:59 +01:00
if len(users) != 1 {
t.Errorf("Expect one user in list, but the acutal length is %d, the list: %+v", len(users), users)
}
2016-02-26 04:26:54 +01:00
users2, err := ListUsers(models.User{Username: username})
2016-02-23 13:49:59 +01:00
if len(users2) != 1 {
t.Errorf("Expect one user in list, but the acutal length is %d, the list: %+v", len(users), users)
}
2016-02-26 04:26:54 +01:00
if users2[0].Username != username {
t.Errorf("The username in result list does not match, expected: %s, actual: %s", username, users2[0].Username)
2016-02-01 12:59:10 +01:00
}
}
2016-02-23 13:49:59 +01:00
func TestResetUserPassword(t *testing.T) {
2016-02-24 11:16:16 +01:00
uuid, err := GenerateRandomString()
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in GenerateRandomString: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-26 04:26:54 +01:00
err = UpdateUserResetUUID(models.User{ResetUUID: uuid, Email: currentUser.Email})
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in UpdateUserResetUuid: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-26 03:15:01 +01:00
err = ResetUserPassword(models.User{UserID: currentUser.UserID, Password: "HarborTester12345", ResetUUID: uuid, Salt: currentUser.Salt})
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in ResetUserPassword: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-24 11:16:16 +01:00
loginedUser, err := LoginByDb(models.AuthModel{Principal: currentUser.Username, Password: "HarborTester12345"})
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in LoginByDb: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-26 04:26:54 +01:00
if loginedUser.Username != username {
t.Errorf("The username returned by Login does not match, expected: %s, acutal: %s", username, loginedUser.Username)
2016-02-23 13:49:59 +01:00
}
2016-02-01 12:59:10 +01:00
}
2016-02-23 13:49:59 +01:00
func TestChangeUserPassword(t *testing.T) {
2016-02-26 03:15:01 +01:00
err := ChangeUserPassword(models.User{UserID: currentUser.UserID, Password: "NewHarborTester12345", Salt: currentUser.Salt})
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in ChangeUserPassword: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-24 11:16:16 +01:00
loginedUser, err := LoginByDb(models.AuthModel{Principal: currentUser.Username, Password: "NewHarborTester12345"})
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in LoginByDb: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-26 04:26:54 +01:00
if loginedUser.Username != username {
t.Errorf("The username returned by Login does not match, expected: %s, acutal: %s", username, loginedUser.Username)
2016-02-23 13:49:59 +01:00
}
2016-02-01 12:59:10 +01:00
}
func TestChangeUserPasswordWithOldPassword(t *testing.T) {
2016-02-26 03:15:01 +01:00
err := ChangeUserPassword(models.User{UserID: currentUser.UserID, Password: "NewerHarborTester12345", Salt: currentUser.Salt}, "NewHarborTester12345")
if err != nil {
t.Errorf("Error occurred in ChangeUserPassword: %v", err)
}
2016-02-24 11:16:16 +01:00
loginedUser, err := LoginByDb(models.AuthModel{Principal: currentUser.Username, Password: "NewerHarborTester12345"})
if err != nil {
t.Errorf("Error occurred in LoginByDb: %v", err)
}
2016-02-26 04:26:54 +01:00
if loginedUser.Username != username {
t.Errorf("The username returned by Login does not match, expected: %s, acutal: %s", username, loginedUser.Username)
}
}
func TestChangeUserPasswordWithIncorrectOldPassword(t *testing.T) {
2016-02-26 03:15:01 +01:00
err := ChangeUserPassword(models.User{UserID: currentUser.UserID, Password: "NNewerHarborTester12345", Salt: currentUser.Salt}, "WrongNewerHarborTester12345")
if err == nil {
t.Errorf("Error does not occurred due to old password is incorrect.")
}
2016-02-24 11:16:16 +01:00
loginedUser, err := LoginByDb(models.AuthModel{Principal: currentUser.Username, Password: "NNewerHarborTester12345"})
if err != nil {
t.Errorf("Error occurred in LoginByDb: %v", err)
}
if loginedUser != nil {
t.Errorf("The login user is not nil, acutal: %+v", loginedUser)
}
}
2016-02-23 13:49:59 +01:00
func TestQueryRelevantProjectsWhenNoProjectAdded(t *testing.T) {
2016-05-16 11:50:27 +02:00
projects, err := SearchProjects(currentUser.UserID)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in QueryRelevantProjects: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-23 13:49:59 +01:00
if len(projects) != 1 {
t.Errorf("Expected only one project in DB, but actual: %d", len(projects))
}
if projects[0].Name != "library" {
t.Errorf("There name of the project does not match, expected: %s, actual: %s", "library", projects[0].Name)
2016-02-01 12:59:10 +01:00
}
}
2016-02-23 13:49:59 +01:00
func TestAddProject(t *testing.T) {
2016-02-01 12:59:10 +01:00
project := models.Project{
2016-02-26 03:15:01 +01:00
OwnerID: currentUser.UserID,
2016-02-26 04:26:54 +01:00
Name: projectName,
2016-02-01 12:59:10 +01:00
CreationTime: time.Now(),
OwnerName: currentUser.Username,
}
_, err := AddProject(project)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in AddProject: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-26 04:26:54 +01:00
newProject, err := GetProjectByName(projectName)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in GetProjectByName: %v", err)
2016-02-01 12:59:10 +01:00
}
if newProject == nil {
2016-02-26 04:26:54 +01:00
t.Errorf("No project found queried by project name: %v", projectName)
2016-02-01 12:59:10 +01:00
}
}
var currentProject *models.Project
2016-02-23 13:49:59 +01:00
func TestGetProject(t *testing.T) {
2016-02-01 12:59:10 +01:00
var err error
2016-02-26 04:26:54 +01:00
currentProject, err = GetProjectByName(projectName)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in GetProjectByName: %v", err)
2016-02-01 12:59:10 +01:00
}
if currentProject == nil {
2016-02-26 04:26:54 +01:00
t.Errorf("No project found queried by project name: %v", projectName)
2016-02-23 13:49:59 +01:00
}
2016-02-26 04:26:54 +01:00
if currentProject.Name != projectName {
t.Errorf("Project name does not match, expected: %s, actual: %s", projectName, currentProject.Name)
2016-02-01 12:59:10 +01:00
}
}
2016-02-23 13:49:59 +01:00
func TestGetAccessLog(t *testing.T) {
2016-02-01 12:59:10 +01:00
queryAccessLog := models.AccessLog{
2016-02-26 03:15:01 +01:00
UserID: currentUser.UserID,
ProjectID: currentProject.ProjectID,
2016-02-01 12:59:10 +01:00
}
2016-02-24 11:16:16 +01:00
accessLogs, err := GetAccessLogs(queryAccessLog)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in GetAccessLog: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-23 13:49:59 +01:00
if len(accessLogs) != 1 {
t.Errorf("The length of accesslog list should be 1, actual: %d", len(accessLogs))
}
2016-02-26 04:26:54 +01:00
if accessLogs[0].RepoName != projectName+"/" {
t.Errorf("The project name does not match, expected: %s, actual: %s", projectName+"/", accessLogs[0].RepoName)
2016-02-01 12:59:10 +01:00
}
}
2016-05-03 10:54:01 +02:00
func TestAddAccessLog(t *testing.T) {
var err error
var accessLogList []models.AccessLog
accessLog := models.AccessLog{
UserID: currentUser.UserID,
ProjectID: currentProject.ProjectID,
RepoName: currentProject.Name + "/",
RepoTag: repoTag,
GUID: "N/A",
Operation: "create",
OpTime: time.Now(),
}
err = AddAccessLog(accessLog)
if err != nil {
t.Errorf("Error occurred in AddAccessLog: %v", err)
}
accessLogList, err = GetAccessLogs(accessLog)
if err != nil {
t.Errorf("Error occurred in GetAccessLog: %v", err)
}
if len(accessLogList) != 1 {
t.Errorf("The length of accesslog list should be 1, actual: %d", len(accessLogList))
}
if accessLogList[0].RepoName != projectName+"/" {
t.Errorf("The project name does not match, expected: %s, actual: %s", projectName+"/", accessLogList[0].RepoName)
}
if accessLogList[0].RepoTag != repoTag {
t.Errorf("The repo tag does not match, expected: %s, actual: %s", repoTag, accessLogList[0].RepoTag)
}
}
func TestAccessLog(t *testing.T) {
var err error
var accessLogList []models.AccessLog
accessLog := models.AccessLog{
UserID: currentUser.UserID,
ProjectID: currentProject.ProjectID,
RepoName: currentProject.Name + "/",
RepoTag: repoTag2,
Operation: "create",
}
err = AccessLog(currentUser.Username, currentProject.Name, currentProject.Name+"/", repoTag2, "create")
if err != nil {
t.Errorf("Error occurred in AccessLog: %v", err)
}
accessLogList, err = GetAccessLogs(accessLog)
if err != nil {
t.Errorf("Error occurred in GetAccessLog: %v", err)
}
if len(accessLogList) != 1 {
t.Errorf("The length of accesslog list should be 1, actual: %d", len(accessLogList))
}
if accessLogList[0].RepoName != projectName+"/" {
t.Errorf("The project name does not match, expected: %s, actual: %s", projectName+"/", accessLogList[0].RepoName)
}
if accessLogList[0].RepoTag != repoTag2 {
t.Errorf("The repo tag does not match, expected: %s, actual: %s", repoTag2, accessLogList[0].RepoTag)
}
}
2016-02-23 13:49:59 +01:00
func TestProjectExists(t *testing.T) {
2016-02-01 12:59:10 +01:00
var exists bool
var err error
2016-02-26 03:15:01 +01:00
exists, err = ProjectExists(currentProject.ProjectID)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in ProjectExists: %v", err)
}
if !exists {
2016-02-26 03:15:01 +01:00
t.Errorf("The project with id: %d, does not exist", currentProject.ProjectID)
2016-02-01 12:59:10 +01:00
}
2016-02-24 11:16:16 +01:00
exists, err = ProjectExists(currentProject.Name)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in ProjectExists: %v", err)
}
if !exists {
t.Errorf("The project with name: %s, does not exist", currentProject.Name)
2016-02-01 12:59:10 +01:00
}
}
2016-02-24 11:16:16 +01:00
func TestGetProjectById(t *testing.T) {
2016-02-26 03:15:01 +01:00
id := currentProject.ProjectID
2016-02-26 04:26:54 +01:00
p, err := GetProjectByID(id)
2016-02-24 11:16:16 +01:00
if err != nil {
t.Errorf("Error in GetProjectById: %v, id: %d", err, id)
}
if p.Name != currentProject.Name {
t.Errorf("project name does not match, expected: %s, actual: %s", currentProject.Name, p.Name)
}
}
func TestGetUserByProject(t *testing.T) {
2016-02-26 03:15:01 +01:00
pid := currentProject.ProjectID
2016-02-24 11:16:16 +01:00
u1 := models.User{
Username: "%%Tester%%",
}
u2 := models.User{
Username: "nononono",
}
users, err := GetUserByProject(pid, u1)
if err != nil {
2016-02-26 07:24:44 +01:00
t.Errorf("Error happened in GetUserByProject: %v, project Id: %d, user: %+v", err, pid, u1)
2016-02-24 11:16:16 +01:00
}
if len(users) != 1 {
t.Errorf("unexpected length of user list, expected: 1, the users list: %+v", users)
}
users, err = GetUserByProject(pid, u2)
if err != nil {
2016-02-26 07:24:44 +01:00
t.Errorf("Error happened in GetUserByProject: %v, project Id: %d, user: %+v", err, pid, u2)
2016-02-24 11:16:16 +01:00
}
if len(users) != 0 {
t.Errorf("unexpected length of user list, expected: 0, the users list: %+v", users)
}
}
2016-02-23 13:49:59 +01:00
func TestToggleProjectPublicity(t *testing.T) {
2016-02-26 04:26:54 +01:00
err := ToggleProjectPublicity(currentProject.ProjectID, publicityOn)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in ToggleProjectPublicity: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-26 04:26:54 +01:00
currentProject, err = GetProjectByName(projectName)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in GetProjectByName: %v", err)
}
2016-02-26 04:26:54 +01:00
if currentProject.Public != publicityOn {
2016-02-26 03:15:01 +01:00
t.Errorf("project, id: %d, its publicity is not on", currentProject.ProjectID)
2016-02-01 12:59:10 +01:00
}
2016-02-26 04:26:54 +01:00
err = ToggleProjectPublicity(currentProject.ProjectID, publicityOff)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in ToggleProjectPublicity: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-26 04:26:54 +01:00
currentProject, err = GetProjectByName(projectName)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in GetProjectByName: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-26 04:26:54 +01:00
if currentProject.Public != publicityOff {
2016-02-26 03:15:01 +01:00
t.Errorf("project, id: %d, its publicity is not off", currentProject.ProjectID)
2016-02-23 13:49:59 +01:00
}
2016-02-24 11:16:16 +01:00
}
func TestIsProjectPublic(t *testing.T) {
2016-02-26 04:26:54 +01:00
if isPublic := IsProjectPublic(projectName); isPublic {
2016-02-26 03:15:01 +01:00
t.Errorf("project, id: %d, its publicity is not false after turning off", currentProject.ProjectID)
2016-02-24 11:16:16 +01:00
}
}
func TestGetUserProjectRoles(t *testing.T) {
2016-03-29 06:09:27 +02:00
r, err := GetUserProjectRoles(currentUser.UserID, currentProject.ProjectID)
2016-02-24 11:16:16 +01:00
if err != nil {
2016-03-29 06:09:27 +02:00
t.Errorf("Error happened in GetUserProjectRole: %v, userID: %+v, project Id: %d", err, currentUser.UserID, currentProject.ProjectID)
2016-02-24 11:16:16 +01:00
}
2016-02-01 12:59:10 +01:00
//Get the size of current user project role.
2016-02-23 13:49:59 +01:00
if len(r) != 1 {
2016-02-26 03:15:01 +01:00
t.Errorf("The user, id: %d, should only have one role in project, id: %d, but actual: %d", currentUser.UserID, currentProject.ProjectID, len(r))
2016-02-01 12:59:10 +01:00
}
2016-02-23 13:49:59 +01:00
if r[0].Name != "projectAdmin" {
t.Errorf("the expected rolename is: projectAdmin, actual: %s", r[0].Name)
}
2016-02-01 12:59:10 +01:00
}
2016-02-23 13:49:59 +01:00
func TestProjectPermission(t *testing.T) {
2016-02-24 11:16:16 +01:00
roleCode, err := GetPermission(currentUser.Username, currentProject.Name)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in GetPermission: %v", err)
}
if roleCode != "MDRWS" {
t.Errorf("The expected role code is MDRWS,but actual: %s", roleCode)
2016-02-01 12:59:10 +01:00
}
}
2016-05-10 16:08:36 +02:00
func TestGetUserRelevantProjects(t *testing.T) {
2016-05-17 11:03:40 +02:00
projects, err := GetUserRelevantProjects(currentUser.UserID, "")
2016-02-01 12:59:10 +01:00
if err != nil {
2016-05-10 16:08:36 +02:00
t.Errorf("Error occurred in GetUserRelevantProjects: %v", err)
2016-02-23 13:49:59 +01:00
}
2016-05-16 12:08:50 +02:00
if len(projects) != 1 {
t.Errorf("Expected length of relevant projects is 1, but actual: %d, the projects: %+v", len(projects), projects)
2016-02-01 12:59:10 +01:00
}
2016-05-16 12:08:50 +02:00
if projects[0].Name != projectName {
2016-02-26 04:26:54 +01:00
t.Errorf("Expected project name in the list: %s, actual: %s", projectName, projects[1].Name)
2016-02-01 12:59:10 +01:00
}
}
2016-05-10 16:08:36 +02:00
func TestGetAllProjects(t *testing.T) {
2016-05-17 11:03:40 +02:00
projects, err := GetAllProjects("")
2016-05-10 16:08:36 +02:00
if err != nil {
t.Errorf("Error occurred in GetAllProjects: %v", err)
}
2016-05-10 16:11:07 +02:00
if len(projects) != 2 {
t.Errorf("Expected length of projects is 2, but actual: %d, the projects: %+v", len(projects), projects)
}
if projects[1].Name != projectName {
t.Errorf("Expected project name in the list: %s, actual: %s", projectName, projects[1].Name)
}
2016-05-10 16:08:36 +02:00
}
2016-05-19 08:40:09 +02:00
func TestGetPublicProjects(t *testing.T) {
projects, err := GetPublicProjects("")
if err != nil {
t.Errorf("Error occurred in getProjects: %v", err)
}
if len(projects) != 1 {
t.Errorf("Expected length of projects is 1, but actual: %d, the projects: %+v", len(projects), projects)
}
if projects[0].Name != "library" {
t.Errorf("Expected project name in the list: %s, actual: %s", "library", projects[0].Name)
}
}
2016-03-28 09:34:41 +02:00
func TestAddProjectMember(t *testing.T) {
2016-03-29 06:09:27 +02:00
err := AddProjectMember(currentProject.ProjectID, 1, models.DEVELOPER)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-03-28 09:34:41 +02:00
t.Errorf("Error occurred in AddProjectMember: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-03-29 06:09:27 +02:00
roles, err := GetUserProjectRoles(1, currentProject.ProjectID)
2016-03-28 09:34:41 +02:00
if err != nil {
t.Errorf("Error occurred in GetUserProjectRoles: %v", err)
}
2016-02-01 12:59:10 +01:00
2016-03-28 09:34:41 +02:00
flag := false
for _, role := range roles {
if role.Name == "developer" {
flag = true
break
}
2016-02-01 12:59:10 +01:00
}
2016-03-28 09:34:41 +02:00
if !flag {
t.Errorf("the user which ID is 1 does not have developer privileges")
2016-02-23 13:49:59 +01:00
}
2016-02-01 12:59:10 +01:00
}
2016-03-28 09:34:41 +02:00
func TestDeleteProjectMember(t *testing.T) {
err := DeleteProjectMember(currentProject.ProjectID, 1)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-03-28 09:34:41 +02:00
t.Errorf("Error occurred in DeleteProjectMember: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-03-29 06:09:27 +02:00
roles, err := GetUserProjectRoles(1, currentProject.ProjectID)
2016-03-28 09:34:41 +02:00
if err != nil {
t.Errorf("Error occurred in GetUserProjectRoles: %v", err)
}
if len(roles) != 0 {
t.Errorf("delete record failed from table project_member")
2016-02-01 12:59:10 +01:00
}
}
2016-02-24 11:16:16 +01:00
func TestToggleAdminRole(t *testing.T) {
2016-06-02 13:03:04 +02:00
err := ToggleUserAdminRole(currentUser.UserID, 1)
2016-02-24 11:16:16 +01:00
if err != nil {
t.Errorf("Error in toggle ToggleUserAdmin role: %v, user: %+v", err, currentUser)
}
2016-02-26 03:15:01 +01:00
isAdmin, err := IsAdminRole(currentUser.UserID)
2016-02-24 11:16:16 +01:00
if err != nil {
2016-02-26 03:15:01 +01:00
t.Errorf("Error in IsAdminRole: %v, user id: %d", err, currentUser.UserID)
2016-02-24 11:16:16 +01:00
}
if !isAdmin {
2016-02-26 03:15:01 +01:00
t.Errorf("User is not admin after toggled, user id: %d", currentUser.UserID)
2016-02-24 11:16:16 +01:00
}
2016-06-02 13:03:04 +02:00
err = ToggleUserAdminRole(currentUser.UserID, 0)
2016-02-24 11:16:16 +01:00
if err != nil {
t.Errorf("Error in toggle ToggleUserAdmin role: %v, user: %+v", err, currentUser)
}
2016-02-26 03:15:01 +01:00
isAdmin, err = IsAdminRole(currentUser.UserID)
2016-02-24 11:16:16 +01:00
if err != nil {
2016-02-26 03:15:01 +01:00
t.Errorf("Error in IsAdminRole: %v, user id: %d", err, currentUser.UserID)
2016-02-24 11:16:16 +01:00
}
if isAdmin {
2016-02-26 03:15:01 +01:00
t.Errorf("User is still admin after toggled, user id: %d", currentUser.UserID)
2016-02-24 11:16:16 +01:00
}
}
2016-05-23 08:24:42 +02:00
func TestChangeUserProfile(t *testing.T) {
user := models.User{UserID: currentUser.UserID, Email: username + "@163.com", Realname: "test", Comment: "Unit Test"}
err := ChangeUserProfile(user)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-05-23 08:24:42 +02:00
t.Errorf("Error occurred in ChangeUserProfile: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-05-23 08:24:42 +02:00
loginedUser, err := GetUser(models.User{UserID: currentUser.UserID})
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in GetUser: %v", err)
}
2016-05-23 08:24:42 +02:00
if loginedUser != nil {
if loginedUser.Email != username+"@163.com" {
t.Errorf("user email does not update, expected: %s, acutal: %s", username+"@163.com", loginedUser.Email)
}
if loginedUser.Realname != "test" {
t.Errorf("user realname does not update, expected: %s, acutal: %s", "test", loginedUser.Realname)
}
if loginedUser.Comment != "Unit Test" {
t.Errorf("user email does not update, expected: %s, acutal: %s", "Unit Test", loginedUser.Comment)
}
2016-02-01 12:59:10 +01:00
}
}
2016-05-30 11:22:38 +02:00
func TestGetRecentLogs(t *testing.T) {
logs, err := GetRecentLogs(currentUser.UserID, 10, "2016-05-13 00:00:00", time.Now().String())
if err != nil {
t.Errorf("error occured in getting recent logs, error: %v", err)
}
if len(logs) <= 0 {
t.Errorf("get logs error, expected: %d, actual: %d", 1, len(logs))
}
}
2016-06-27 08:26:41 +02:00
func TestGetTopRepos(t *testing.T) {
err := ToggleProjectPublicity(currentProject.ProjectID, publicityOn)
if err != nil {
t.Errorf("Error occurred in ToggleProjectPublicity: %v", err)
}
err = AccessLog(currentUser.Username, currentProject.Name, currentProject.Name+"/ubuntu", repoTag2, "push")
if err != nil {
t.Errorf("Error occurred in AccessLog: %v", err)
}
err = AccessLog(currentUser.Username, currentProject.Name, currentProject.Name+"/ubuntu", repoTag2, "pull")
if err != nil {
t.Errorf("Error occurred in AccessLog: %v", err)
}
topRepos, err := GetTopRepos(10)
if err != nil {
t.Errorf("error occured in getting top repos, error: %v", err)
}
if topRepos[0].RepoName != currentProject.Name+"/ubuntu" {
t.Errorf("error occured in get top reop's name, expected: %v, actual: %v", currentProject.Name+"/ubuntu", topRepos[0].RepoName)
}
if topRepos[0].AccessCount != 1 {
t.Errorf("error occured in get top reop's access count, expected: %v, actual: %v", 1, topRepos[0].AccessCount)
}
if topRepos[0].Creator != currentUser.Username {
t.Errorf("error occured in get top reop's creator, expected: %v, actual: %v", currentUser.Username, topRepos[0].Creator)
}
err = ToggleProjectPublicity(currentProject.ProjectID, publicityOff)
if err != nil {
t.Errorf("Error occurred in ToggleProjectPublicity: %v", err)
}
o := GetOrmer()
_, err = o.QueryTable("access_log").Filter("operation__in", "push,pull").Delete()
if err != nil {
t.Errorf("error occurred in deleting access logs, %v", err)
}
}
2016-02-23 13:49:59 +01:00
func TestDeleteUser(t *testing.T) {
2016-02-26 03:15:01 +01:00
err := DeleteUser(currentUser.UserID)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in DeleteUser: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-24 11:16:16 +01:00
user, err := GetUser(*currentUser)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in GetUser: %v", err)
}
if user != nil {
2016-02-26 07:24:44 +01:00
t.Errorf("user is not nil after deletion, user: %+v", user)
2016-02-01 12:59:10 +01:00
}
}
2016-05-10 13:38:50 +02:00
var targetID, policyID, policyID2, policyID3, jobID, jobID2, jobID3 int64
2016-05-10 13:38:50 +02:00
func TestAddRepTarget(t *testing.T) {
target := models.RepTarget{
2016-06-13 02:42:11 +02:00
Name: "test",
2016-05-10 13:38:50 +02:00
URL: "127.0.0.1:5000",
Username: "admin",
Password: "admin",
}
//_, err := AddRepTarget(target)
id, err := AddRepTarget(target)
t.Logf("added target, id: %d", id)
if err != nil {
t.Errorf("Error occurred in AddRepTarget: %v", err)
} else {
targetID = id
}
id2 := id + 99
tgt, err := GetRepTarget(id2)
if err != nil {
t.Errorf("Error occurred in GetTarget: %v, id: %d", err, id2)
}
if tgt != nil {
t.Errorf("There should not be a target with id: %d", id2)
}
tgt, err = GetRepTarget(id)
if err != nil {
t.Errorf("Error occurred in GetTarget: %v, id: %d", err, id)
}
if tgt == nil {
t.Errorf("Unable to find a target with id: %d", id)
}
if tgt.URL != "127.0.0.1:5000" {
t.Errorf("Unexpected url in target: %s, expected 127.0.0.1:5000", tgt.URL)
}
if tgt.Username != "admin" {
t.Errorf("Unexpected username in target: %s, expected admin", tgt.Username)
}
}
2016-06-07 07:41:02 +02:00
func TestGetRepTargetByName(t *testing.T) {
target, err := GetRepTarget(targetID)
if err != nil {
t.Fatalf("failed to get target %d: %v", targetID, err)
}
target2, err := GetRepTargetByName(target.Name)
if err != nil {
t.Fatalf("failed to get target %s: %v", target.Name, err)
}
if target.Name != target2.Name {
t.Errorf("unexpected target name: %s, expected: %s", target2.Name, target.Name)
}
}
func TestUpdateRepTarget(t *testing.T) {
target := &models.RepTarget{
Name: "name",
URL: "http://url",
Username: "username",
Password: "password",
}
id, err := AddRepTarget(*target)
if err != nil {
t.Fatalf("failed to add target: %v", err)
}
defer func() {
if err := DeleteRepTarget(id); err != nil {
t.Logf("failed to delete target %d: %v", id, err)
}
}()
target.ID = id
target.Name = "new_name"
target.URL = "http://new_url"
target.Username = "new_username"
target.Password = "new_password"
if err = UpdateRepTarget(*target); err != nil {
t.Fatalf("failed to update target: %v", err)
}
target, err = GetRepTarget(id)
if err != nil {
t.Fatalf("failed to get target %d: %v", id, err)
}
if target.Name != "new_name" {
t.Errorf("unexpected name: %s, expected: %s", target.Name, "new_name")
}
if target.URL != "http://new_url" {
t.Errorf("unexpected url: %s, expected: %s", target.URL, "http://new_url")
}
if target.Username != "new_username" {
t.Errorf("unexpected username: %s, expected: %s", target.Username, "new_username")
}
if target.Password != "new_password" {
t.Errorf("unexpected password: %s, expected: %s", target.Password, "new_password")
}
}
2016-06-13 02:42:11 +02:00
func TestFilterRepTargets(t *testing.T) {
targets, err := FilterRepTargets("test")
if err != nil {
t.Fatalf("failed to get all targets: %v", err)
}
if len(targets) == 0 {
t.Errorf("unexpected num of targets: %d, expected: %d", len(targets), 1)
}
}
2016-05-10 13:38:50 +02:00
func TestAddRepPolicy(t *testing.T) {
policy := models.RepPolicy{
ProjectID: 1,
Enabled: 1,
TargetID: targetID,
Description: "whatever",
Name: "mypolicy",
}
id, err := AddRepPolicy(policy)
t.Logf("added policy, id: %d", id)
if err != nil {
t.Errorf("Error occurred in AddRepPolicy: %v", err)
} else {
policyID = id
}
p, err := GetRepPolicy(id)
if err != nil {
t.Errorf("Error occurred in GetPolicy: %v, id: %d", err, id)
}
if p == nil {
t.Errorf("Unable to find a policy with id: %d", id)
}
if p.Name != "mypolicy" || p.TargetID != targetID || p.Enabled != 1 || p.Description != "whatever" {
t.Errorf("The data does not match, expected: Name: mypolicy, TargetID: %d, Enabled: 1, Description: whatever;\n result: Name: %s, TargetID: %d, Enabled: %d, Description: %s",
targetID, p.Name, p.TargetID, p.Enabled, p.Description)
}
2016-05-27 12:46:07 +02:00
var tm = time.Now().AddDate(0, 0, -1)
2016-05-10 13:38:50 +02:00
if !p.StartTime.After(tm) {
t.Errorf("Unexpected start_time: %v", p.StartTime)
}
}
func TestGetRepPolicyByTarget(t *testing.T) {
policies, err := GetRepPolicyByTarget(targetID)
if err != nil {
t.Fatalf("failed to get policy according target %d: %v", targetID, err)
}
if len(policies) == 0 {
t.Fatal("unexpected length of policies 0, expected is >0")
}
if policies[0].ID != policyID {
2016-06-16 08:24:35 +02:00
t.Fatalf("unexpected policy: %d, expected: %d", policies[0].ID, policyID)
}
}
func TestGetRepPolicyByProjectAndTarget(t *testing.T) {
policies, err := GetRepPolicyByProjectAndTarget(1, targetID)
if err != nil {
t.Fatalf("failed to get policy according project %d and target %d: %v", 1, targetID, err)
}
if len(policies) == 0 {
t.Fatal("unexpected length of policies 0, expected is >0")
}
if policies[0].ID != policyID {
t.Fatalf("unexpected policy: %d, expected: %d", policies[0].ID, policyID)
}
}
2016-06-07 07:41:02 +02:00
func TestGetRepPolicyByName(t *testing.T) {
policy, err := GetRepPolicy(policyID)
if err != nil {
t.Fatalf("failed to get policy %d: %v", policyID, err)
}
policy2, err := GetRepPolicyByName(policy.Name)
if err != nil {
t.Fatalf("failed to get policy %s: %v", policy.Name, err)
}
if policy.Name != policy2.Name {
t.Errorf("unexpected name: %s, expected: %s", policy2.Name, policy.Name)
}
}
2016-05-10 13:38:50 +02:00
func TestDisableRepPolicy(t *testing.T) {
err := DisableRepPolicy(policyID)
if err != nil {
t.Errorf("Failed to disable policy, id: %d", policyID)
}
p, err := GetRepPolicy(policyID)
if err != nil {
t.Errorf("Error occurred in GetPolicy: %v, id: %d", err, policyID)
}
if p == nil {
t.Errorf("Unable to find a policy with id: %d", policyID)
}
if p.Enabled == 1 {
t.Errorf("The Enabled value of replication policy is still 1 after disabled, id: %d", policyID)
}
}
func TestEnableRepPolicy(t *testing.T) {
err := EnableRepPolicy(policyID)
if err != nil {
t.Errorf("Failed to disable policy, id: %d", policyID)
}
p, err := GetRepPolicy(policyID)
if err != nil {
t.Errorf("Error occurred in GetPolicy: %v, id: %d", err, policyID)
}
if p == nil {
t.Errorf("Unable to find a policy with id: %d", policyID)
}
if p.Enabled == 0 {
t.Errorf("The Enabled value of replication policy is still 0 after disabled, id: %d", policyID)
}
}
func TestAddRepPolicy2(t *testing.T) {
policy2 := models.RepPolicy{
ProjectID: 3,
Enabled: 0,
TargetID: 3,
Description: "whatever",
Name: "mypolicy",
}
policyID2, err := AddRepPolicy(policy2)
t.Logf("added policy, id: %d", policyID2)
2016-05-10 13:38:50 +02:00
if err != nil {
t.Errorf("Error occurred in AddRepPolicy: %v", err)
}
p, err := GetRepPolicy(policyID2)
2016-05-10 13:38:50 +02:00
if err != nil {
t.Errorf("Error occurred in GetPolicy: %v, id: %d", err, policyID2)
2016-05-10 13:38:50 +02:00
}
if p == nil {
t.Errorf("Unable to find a policy with id: %d", policyID2)
2016-05-10 13:38:50 +02:00
}
var tm time.Time
if p.StartTime.After(tm) {
t.Errorf("Unexpected start_time: %v", p.StartTime)
}
}
func TestAddRepJob(t *testing.T) {
job := models.RepJob{
Repository: "library/ubuntu",
PolicyID: policyID,
Operation: "transfer",
TagList: []string{"12.01", "14.04", "latest"},
2016-05-10 13:38:50 +02:00
}
id, err := AddRepJob(job)
if err != nil {
t.Errorf("Error occurred in AddRepJob: %v", err)
return
2016-05-10 13:38:50 +02:00
}
2016-05-27 12:46:07 +02:00
jobID = id
2016-05-10 13:38:50 +02:00
j, err := GetRepJob(id)
if err != nil {
t.Errorf("Error occurred in GetRepJob: %v, id: %d", err, id)
return
2016-05-10 13:38:50 +02:00
}
if j == nil {
t.Errorf("Unable to find a job with id: %d", id)
return
2016-05-10 13:38:50 +02:00
}
if j.Status != models.JobPending || j.Repository != "library/ubuntu" || j.PolicyID != policyID || j.Operation != "transfer" || len(j.TagList) != 3 {
t.Errorf("Expected data of job, id: %d, Status: %s, Repository: library/ubuntu, PolicyID: %d, Operation: transfer, taglist length 3"+
"but in returned data:, Status: %s, Repository: %s, Operation: %s, PolicyID: %d, TagList: %v", id, models.JobPending, policyID, j.Status, j.Repository, j.Operation, j.PolicyID, j.TagList)
return
2016-05-10 13:38:50 +02:00
}
}
func TestUpdateRepJobStatus(t *testing.T) {
err := UpdateRepJobStatus(jobID, models.JobFinished)
if err != nil {
t.Errorf("Error occured in UpdateRepJobStatus, error: %v, id: %d", err, jobID)
return
}
j, err := GetRepJob(jobID)
if err != nil {
t.Errorf("Error occurred in GetRepJob: %v, id: %d", err, jobID)
}
if j == nil {
t.Errorf("Unable to find a job with id: %d", jobID)
}
if j.Status != models.JobFinished {
t.Errorf("Job's status: %s, expected: %s, id: %d", j.Status, models.JobFinished, jobID)
}
err = UpdateRepJobStatus(jobID, models.JobPending)
if err != nil {
t.Errorf("Error occured in UpdateRepJobStatus when update it back to status pending, error: %v, id: %d", err, jobID)
return
}
2016-05-10 13:38:50 +02:00
}
func TestGetRepPolicyByProject(t *testing.T) {
p1, err := GetRepPolicyByProject(99)
if err != nil {
t.Errorf("Error occured in GetRepPolicyByProject:%v, project ID: %d", err, 99)
return
}
if len(p1) > 0 {
t.Errorf("Unexpected length of policy list, expected: 0, in fact: %d, project id: %d", len(p1), 99)
return
}
p2, err := GetRepPolicyByProject(1)
if err != nil {
t.Errorf("Error occuered in GetRepPolicyByProject:%v, project ID: %d", err, 2)
return
}
if len(p2) != 1 {
t.Errorf("Unexpected length of policy list, expected: 1, in fact: %d, project id: %d", len(p2), 1)
return
}
if p2[0].ID != policyID {
t.Errorf("Unexpecred policy id in result, expected: %d, in fact: %d", policyID, p2[0].ID)
return
}
}
func TestGetRepJobByPolicy(t *testing.T) {
jobs, err := GetRepJobByPolicy(999)
if err != nil {
log.Errorf("Error occured in GetRepJobByPolicy: %v, policy ID: %d", err, 999)
return
}
if len(jobs) > 0 {
log.Errorf("Unexpected length of jobs, expected: 0, in fact: %d", len(jobs))
return
}
jobs, err = GetRepJobByPolicy(policyID)
if err != nil {
log.Errorf("Error occured in GetRepJobByPolicy: %v, policy ID: %d", err, policyID)
return
}
if len(jobs) != 1 {
log.Errorf("Unexpected length of jobs, expected: 1, in fact: %d", len(jobs))
return
}
if jobs[0].ID != jobID {
log.Errorf("Unexpected job ID in the result, expected: %d, in fact: %d", jobID, jobs[0].ID)
return
}
}
func TestDeleteRepJob(t *testing.T) {
err := DeleteRepJob(jobID)
if err != nil {
t.Errorf("Error occured in DeleteRepJob: %v, id: %d", err, jobID)
return
}
t.Logf("deleted rep job, id: %d", jobID)
j, err := GetRepJob(jobID)
if err != nil {
t.Errorf("Error occured in GetRepJob:%v", err)
2016-06-17 12:58:16 +02:00
return
}
if j != nil {
t.Errorf("Able to find rep job after deletion, id: %d", jobID)
2016-06-17 12:58:16 +02:00
return
}
}
func TestFilterRepJobs(t *testing.T) {
2016-06-29 11:15:49 +02:00
jobs, err := FilterRepJobs(policyID, "", "", nil, nil, 1000)
if err != nil {
log.Errorf("Error occured in FilterRepJobs: %v, policy ID: %d", err, policyID)
return
}
if len(jobs) != 1 {
log.Errorf("Unexpected length of jobs, expected: 1, in fact: %d", len(jobs))
return
}
if jobs[0].ID != jobID {
log.Errorf("Unexpected job ID in the result, expected: %d, in fact: %d", jobID, jobs[0].ID)
return
}
}
func TestGetRepoJobToStop(t *testing.T) {
jobs := [...]models.RepJob{
models.RepJob{
Repository: "library/ubuntu",
PolicyID: policyID,
Operation: "transfer",
Status: models.JobRunning,
},
models.RepJob{
Repository: "library/ubuntu",
PolicyID: policyID,
Operation: "transfer",
Status: models.JobFinished,
},
models.RepJob{
Repository: "library/ubuntu",
PolicyID: policyID,
Operation: "transfer",
Status: models.JobCanceled,
},
}
var err error
var i int64
2016-06-20 07:04:27 +02:00
var ids []int64
for _, j := range jobs {
i, err = AddRepJob(j)
ids = append(ids, i)
if err != nil {
log.Errorf("Failed to add Job: %+v, error: %v", j, err)
return
}
}
res, err := GetRepJobToStop(policyID)
if err != nil {
log.Errorf("Failed to Get Jobs, error: %v", err)
return
}
//time.Sleep(15 * time.Second)
if len(res) != 1 {
log.Errorf("Expected length of stoppable jobs, expected:1, in fact: %d", len(res))
return
}
for _, id := range ids {
err = DeleteRepJob(id)
if err != nil {
log.Errorf("Failed to delete job, id: %d, error: %v", id, err)
return
}
}
}
2016-05-10 13:38:50 +02:00
func TestDeleteRepTarget(t *testing.T) {
err := DeleteRepTarget(targetID)
if err != nil {
t.Errorf("Error occured in DeleteRepTarget: %v, id: %d", err, targetID)
return
}
t.Logf("deleted target, id: %d", targetID)
tgt, err := GetRepTarget(targetID)
if err != nil {
t.Errorf("Error occurred in GetTarget: %v, id: %d", err, targetID)
}
if tgt != nil {
t.Errorf("Able to find target after deletion, id: %d", targetID)
}
}
2016-06-13 11:32:22 +02:00
func TestFilterRepPolicies(t *testing.T) {
_, err := FilterRepPolicies("name", 0)
if err != nil {
t.Fatalf("failed to filter policy")
}
}
func TestUpdateRepPolicy(t *testing.T) {
policy := &models.RepPolicy{
ID: policyID,
Name: "new_policy_name",
}
if err := UpdateRepPolicy(policy); err != nil {
t.Fatalf("failed to update policy")
}
}
2016-05-10 13:38:50 +02:00
func TestDeleteRepPolicy(t *testing.T) {
err := DeleteRepPolicy(policyID)
if err != nil {
t.Errorf("Error occured in DeleteRepPolicy: %v, id: %d", err, policyID)
return
}
t.Logf("delete rep policy, id: %d", policyID)
p, err := GetRepPolicy(policyID)
2016-06-13 10:49:46 +02:00
if err != nil && err != orm.ErrNoRows {
2016-05-10 13:38:50 +02:00
t.Errorf("Error occured in GetRepPolicy:%v", err)
}
if p != nil {
t.Errorf("Able to find rep policy after deletion, id: %d", policyID)
}
}
func TestResetRepJobs(t *testing.T) {
job1 := models.RepJob{
Repository: "library/ubuntua",
PolicyID: policyID,
Operation: "transfer",
Status: models.JobRunning,
}
job2 := models.RepJob{
Repository: "library/ubuntub",
PolicyID: policyID,
Operation: "transfer",
Status: models.JobCanceled,
}
id1, err := AddRepJob(job1)
2016-05-10 13:38:50 +02:00
if err != nil {
t.Errorf("Failed to add job: %+v, error: %v", job1, err)
2016-05-10 13:38:50 +02:00
return
}
id2, err := AddRepJob(job2)
2016-05-10 13:38:50 +02:00
if err != nil {
t.Errorf("Failed to add job: %+v, error: %v", job2, err)
return
2016-05-10 13:38:50 +02:00
}
err = ResetRunningJobs()
if err != nil {
t.Errorf("Failed to reset running jobs, error: %v", err)
}
j1, err := GetRepJob(id1)
if err != nil {
t.Errorf("Failed to get rep job, id: %d, error: %v", id1, err)
return
}
if j1.Status != models.JobPending {
t.Errorf("The rep job: %d, status should be Pending, but infact: %s", id1, j1.Status)
return
}
j2, err := GetRepJob(id2)
if err != nil {
t.Errorf("Failed to get rep job, id: %d, error: %v", id2, err)
return
}
if j2.Status == models.JobPending {
t.Errorf("The rep job: %d, status should be Canceled, but infact: %s", id2, j2.Status)
return
}
}
func TestGetJobByStatus(t *testing.T) {
r1, err := GetRepJobByStatus(models.JobPending, models.JobRunning)
if err != nil {
t.Errorf("Failed to run GetRepJobByStatus, error: %v", err)
}
if len(r1) != 1 {
t.Errorf("Unexpected length of result, expected 1, but in fact:%d", len(r1))
return
}
r2, err := GetRepJobByStatus(models.JobPending, models.JobCanceled)
if err != nil {
t.Errorf("Failed to run GetRepJobByStatus, error: %v", err)
}
if len(r2) != 2 {
t.Errorf("Unexpected length of result, expected 2, but in fact:%d", len(r2))
return
}
for _, j := range r2 {
DeleteRepJob(j.ID)
2016-05-10 13:38:50 +02:00
}
}
2016-05-24 07:20:11 +02:00
2016-05-20 11:35:16 +02:00
func TestGetOrmer(t *testing.T) {
o := GetOrmer()
if o == nil {
t.Errorf("Error get ormer.")
}
}