mirror of
https://github.com/simon987/task_tracker.git
synced 2025-12-12 06:28:50 +00:00
added optional task unique field
This commit is contained in:
@@ -13,7 +13,7 @@ import (
|
||||
|
||||
func TestWebHookNoSignature(t *testing.T) {
|
||||
|
||||
r := Post("/git/receivehook", api.GitPayload{})
|
||||
r := Post("/git/receivehook", api.GitPayload{}, nil)
|
||||
|
||||
if r.StatusCode != 403 {
|
||||
t.Error()
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
|
||||
func TestIndex(t *testing.T) {
|
||||
|
||||
r := Get("/")
|
||||
r := Get("/", nil)
|
||||
|
||||
body, _ := ioutil.ReadAll(r.Body)
|
||||
var info api.Info
|
||||
|
||||
@@ -16,7 +16,7 @@ func TestTraceValid(t *testing.T) {
|
||||
Scope: "test",
|
||||
Message: "This is a test message",
|
||||
TimeStamp: time.Now().Unix(),
|
||||
})
|
||||
}, nil)
|
||||
|
||||
if r.StatusCode != 200 {
|
||||
t.Fail()
|
||||
@@ -27,7 +27,7 @@ func TestTraceInvalidScope(t *testing.T) {
|
||||
r := Post("/log/trace", api.LogRequest{
|
||||
Message: "this is a test message",
|
||||
TimeStamp: time.Now().Unix(),
|
||||
})
|
||||
}, nil)
|
||||
|
||||
if r.StatusCode != 500 {
|
||||
t.Fail()
|
||||
@@ -37,7 +37,7 @@ func TestTraceInvalidScope(t *testing.T) {
|
||||
Scope: "",
|
||||
Message: "this is a test message",
|
||||
TimeStamp: time.Now().Unix(),
|
||||
})
|
||||
}, nil)
|
||||
|
||||
if r.StatusCode != 500 {
|
||||
t.Fail()
|
||||
@@ -52,7 +52,7 @@ func TestTraceInvalidMessage(t *testing.T) {
|
||||
Scope: "test",
|
||||
Message: "",
|
||||
TimeStamp: time.Now().Unix(),
|
||||
})
|
||||
}, nil)
|
||||
|
||||
if r.StatusCode != 500 {
|
||||
t.Fail()
|
||||
@@ -66,7 +66,7 @@ func TestTraceInvalidTime(t *testing.T) {
|
||||
r := Post("/log/trace", api.LogRequest{
|
||||
Scope: "test",
|
||||
Message: "test",
|
||||
})
|
||||
}, nil)
|
||||
if r.StatusCode != 500 {
|
||||
t.Fail()
|
||||
}
|
||||
@@ -81,7 +81,7 @@ func TestWarnValid(t *testing.T) {
|
||||
Scope: "test",
|
||||
Message: "test",
|
||||
TimeStamp: time.Now().Unix(),
|
||||
})
|
||||
}, nil)
|
||||
if r.StatusCode != 200 {
|
||||
t.Fail()
|
||||
}
|
||||
@@ -93,7 +93,7 @@ func TestInfoValid(t *testing.T) {
|
||||
Scope: "test",
|
||||
Message: "test",
|
||||
TimeStamp: time.Now().Unix(),
|
||||
})
|
||||
}, nil)
|
||||
if r.StatusCode != 200 {
|
||||
t.Fail()
|
||||
}
|
||||
@@ -105,7 +105,7 @@ func TestErrorValid(t *testing.T) {
|
||||
Scope: "test",
|
||||
Message: "test",
|
||||
TimeStamp: time.Now().Unix(),
|
||||
})
|
||||
}, nil)
|
||||
if r.StatusCode != 200 {
|
||||
t.Fail()
|
||||
}
|
||||
@@ -171,7 +171,7 @@ func getLogs(since int64, level logrus.Level) *api.GetLogResponse {
|
||||
r := Post(fmt.Sprintf("/logs"), api.GetLogRequest{
|
||||
Since: since,
|
||||
Level: level,
|
||||
})
|
||||
}, nil)
|
||||
|
||||
resp := &api.GetLogResponse{}
|
||||
data, _ := ioutil.ReadAll(r.Body)
|
||||
|
||||
@@ -3,7 +3,6 @@ package test
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"src/task_tracker/api"
|
||||
@@ -132,28 +131,30 @@ func TestGetProjectStats(t *testing.T) {
|
||||
CloneUrl: "http://github.com/drone/test",
|
||||
GitRepo: "drone/test",
|
||||
Priority: 3,
|
||||
Public: true,
|
||||
})
|
||||
|
||||
pid := r.Id
|
||||
worker := genWid()
|
||||
|
||||
createTask(api.CreateTaskRequest{
|
||||
Priority: 1,
|
||||
Project: pid,
|
||||
MaxRetries: 0,
|
||||
Recipe: "{}",
|
||||
})
|
||||
}, worker)
|
||||
createTask(api.CreateTaskRequest{
|
||||
Priority: 2,
|
||||
Project: pid,
|
||||
MaxRetries: 0,
|
||||
Recipe: "{}",
|
||||
})
|
||||
}, worker)
|
||||
createTask(api.CreateTaskRequest{
|
||||
Priority: 3,
|
||||
Project: pid,
|
||||
MaxRetries: 0,
|
||||
Recipe: "{}",
|
||||
})
|
||||
}, worker)
|
||||
|
||||
stats := getProjectStats(pid)
|
||||
|
||||
@@ -169,7 +170,7 @@ func TestGetProjectStats(t *testing.T) {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
if stats.Stats.Assignees[0].Assignee != uuid.Nil {
|
||||
if stats.Stats.Assignees[0].Assignee != "unassigned" {
|
||||
t.Error()
|
||||
}
|
||||
if stats.Stats.Assignees[0].TaskCount != 3 {
|
||||
@@ -189,19 +190,132 @@ func TestGetProjectStatsNotFound(t *testing.T) {
|
||||
})
|
||||
s := getProjectStats(r.Id)
|
||||
|
||||
if s.Ok != false {
|
||||
if s.Ok != true {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
if len(s.Message) <= 0 {
|
||||
if s.Stats == nil {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestUpdateProjectValid(t *testing.T) {
|
||||
|
||||
pid := createProject(api.CreateProjectRequest{
|
||||
Public: true,
|
||||
Version: "versionA",
|
||||
Motd: "MotdA",
|
||||
Name: "NameA",
|
||||
CloneUrl: "CloneUrlA",
|
||||
GitRepo: "GitRepoA",
|
||||
Priority: 1,
|
||||
}).Id
|
||||
|
||||
updateResp := updateProject(api.UpdateProjectRequest{
|
||||
Priority: 2,
|
||||
GitRepo: "GitRepoB",
|
||||
CloneUrl: "CloneUrlB",
|
||||
Name: "NameB",
|
||||
Motd: "MotdB",
|
||||
Public: false,
|
||||
}, pid)
|
||||
|
||||
if updateResp.Ok != true {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
proj, _ := getProject(pid)
|
||||
|
||||
if proj.Project.Public != false {
|
||||
t.Error()
|
||||
}
|
||||
if proj.Project.Motd != "MotdB" {
|
||||
t.Error()
|
||||
}
|
||||
if proj.Project.CloneUrl != "CloneUrlB" {
|
||||
t.Error()
|
||||
}
|
||||
if proj.Project.GitRepo != "GitRepoB" {
|
||||
t.Error()
|
||||
}
|
||||
if proj.Project.Priority != 2 {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateProjectInvalid(t *testing.T) {
|
||||
|
||||
pid := createProject(api.CreateProjectRequest{
|
||||
Public: true,
|
||||
Version: "lllllllllllll",
|
||||
Motd: "2wwwwwwwwwwwwwww",
|
||||
Name: "aaaaaaaaaaaaaaaaaaaaaa",
|
||||
CloneUrl: "333333333333333",
|
||||
GitRepo: "llllllllllllllllllls",
|
||||
Priority: 1,
|
||||
}).Id
|
||||
|
||||
updateResp := updateProject(api.UpdateProjectRequest{
|
||||
Priority: -1,
|
||||
GitRepo: "GitRepo------",
|
||||
CloneUrl: "CloneUrlB000000",
|
||||
Name: "NameB-0",
|
||||
Motd: "MotdB000000",
|
||||
Public: false,
|
||||
}, pid)
|
||||
|
||||
if updateResp.Ok != false {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
if len(updateResp.Message) <= 0 {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateProjectConstraintFail(t *testing.T) {
|
||||
|
||||
pid := createProject(api.CreateProjectRequest{
|
||||
Public: true,
|
||||
Version: "testUpdateProjectConstraintFail",
|
||||
Motd: "testUpdateProjectConstraintFail",
|
||||
Name: "testUpdateProjectConstraintFail",
|
||||
CloneUrl: "testUpdateProjectConstraintFail",
|
||||
GitRepo: "testUpdateProjectConstraintFail",
|
||||
Priority: 1,
|
||||
}).Id
|
||||
|
||||
createProject(api.CreateProjectRequest{
|
||||
Public: true,
|
||||
Version: "testUpdateProjectConstraintFail_d",
|
||||
Motd: "testUpdateProjectConstraintFail_d",
|
||||
Name: "testUpdateProjectConstraintFail_d",
|
||||
CloneUrl: "testUpdateProjectConstraintFail_d",
|
||||
GitRepo: "testUpdateProjectConstraintFail_d",
|
||||
Priority: 1,
|
||||
})
|
||||
|
||||
updateResp := updateProject(api.UpdateProjectRequest{
|
||||
Priority: 1,
|
||||
GitRepo: "testUpdateProjectConstraintFail_d",
|
||||
CloneUrl: "testUpdateProjectConstraintFail_d",
|
||||
Name: "testUpdateProjectConstraintFail_d",
|
||||
Motd: "testUpdateProjectConstraintFail_d",
|
||||
}, pid)
|
||||
|
||||
if updateResp.Ok != false {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
if len(updateResp.Message) <= 0 {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func createProject(req api.CreateProjectRequest) *api.CreateProjectResponse {
|
||||
|
||||
r := Post("/project/create", req)
|
||||
r := Post("/project/create", req, nil)
|
||||
|
||||
var resp api.CreateProjectResponse
|
||||
data, _ := ioutil.ReadAll(r.Body)
|
||||
@@ -213,7 +327,7 @@ func createProject(req api.CreateProjectRequest) *api.CreateProjectResponse {
|
||||
|
||||
func getProject(id int64) (*api.GetProjectResponse, *http.Response) {
|
||||
|
||||
r := Get(fmt.Sprintf("/project/get/%d", id))
|
||||
r := Get(fmt.Sprintf("/project/get/%d", id), nil)
|
||||
|
||||
var getResp api.GetProjectResponse
|
||||
data, _ := ioutil.ReadAll(r.Body)
|
||||
@@ -225,7 +339,7 @@ func getProject(id int64) (*api.GetProjectResponse, *http.Response) {
|
||||
|
||||
func getProjectStats(id int64) *api.GetProjectStatsResponse {
|
||||
|
||||
r := Get(fmt.Sprintf("/project/stats/%d", id))
|
||||
r := Get(fmt.Sprintf("/project/stats/%d", id), nil)
|
||||
|
||||
var getResp api.GetProjectStatsResponse
|
||||
data, _ := ioutil.ReadAll(r.Body)
|
||||
@@ -234,3 +348,15 @@ func getProjectStats(id int64) *api.GetProjectStatsResponse {
|
||||
|
||||
return &getResp
|
||||
}
|
||||
|
||||
func updateProject(request api.UpdateProjectRequest, pid int64) *api.UpdateProjectResponse {
|
||||
|
||||
r := Post(fmt.Sprintf("/project/update/%d", pid), request, nil)
|
||||
|
||||
var resp api.UpdateProjectResponse
|
||||
data, _ := ioutil.ReadAll(r.Body)
|
||||
err := json.Unmarshal(data, &resp)
|
||||
handleErr(err)
|
||||
|
||||
return &resp
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@ func BenchmarkCreateTask(b *testing.B) {
|
||||
CloneUrl: "http://localhost",
|
||||
})
|
||||
|
||||
worker := genWid()
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
createTask(api.CreateTaskRequest{
|
||||
@@ -22,6 +24,6 @@ func BenchmarkCreateTask(b *testing.B) {
|
||||
Priority: 1,
|
||||
Recipe: "{}",
|
||||
MaxRetries: 1,
|
||||
})
|
||||
}, worker)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/google/uuid"
|
||||
"io/ioutil"
|
||||
"src/task_tracker/api"
|
||||
"src/task_tracker/storage"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -18,11 +19,13 @@ func TestCreateTaskValid(t *testing.T) {
|
||||
CloneUrl: "http://github.com/test/test",
|
||||
})
|
||||
|
||||
worker := genWid()
|
||||
|
||||
resp := createTask(api.CreateTaskRequest{
|
||||
Project: 1,
|
||||
Recipe: "{}",
|
||||
MaxRetries: 3,
|
||||
})
|
||||
}, worker)
|
||||
|
||||
if resp.Ok != true {
|
||||
t.Fail()
|
||||
@@ -31,11 +34,13 @@ func TestCreateTaskValid(t *testing.T) {
|
||||
|
||||
func TestCreateTaskInvalidProject(t *testing.T) {
|
||||
|
||||
worker := genWid()
|
||||
|
||||
resp := createTask(api.CreateTaskRequest{
|
||||
Project: 123456,
|
||||
Recipe: "{}",
|
||||
MaxRetries: 3,
|
||||
})
|
||||
}, worker)
|
||||
|
||||
if resp.Ok != false {
|
||||
t.Error()
|
||||
@@ -62,7 +67,9 @@ func TestGetTaskInvalidWid(t *testing.T) {
|
||||
func TestGetTaskInvalidWorker(t *testing.T) {
|
||||
|
||||
id := uuid.New()
|
||||
resp := getTask(&id)
|
||||
resp := getTask(&storage.Worker{
|
||||
Id: id,
|
||||
})
|
||||
|
||||
if resp.Ok != false {
|
||||
t.Error()
|
||||
@@ -76,7 +83,9 @@ func TestGetTaskInvalidWorker(t *testing.T) {
|
||||
func TestGetTaskFromProjectInvalidWorker(t *testing.T) {
|
||||
|
||||
id := uuid.New()
|
||||
resp := getTaskFromProject(1, &id)
|
||||
resp := getTaskFromProject(1, &storage.Worker{
|
||||
Id: id,
|
||||
})
|
||||
|
||||
if resp.Ok != false {
|
||||
t.Error()
|
||||
@@ -89,10 +98,12 @@ func TestGetTaskFromProjectInvalidWorker(t *testing.T) {
|
||||
|
||||
func TestCreateTaskInvalidRetries(t *testing.T) {
|
||||
|
||||
worker := genWid()
|
||||
|
||||
resp := createTask(api.CreateTaskRequest{
|
||||
Project: 1,
|
||||
MaxRetries: -1,
|
||||
})
|
||||
}, worker)
|
||||
|
||||
if resp.Ok != false {
|
||||
t.Error()
|
||||
@@ -105,11 +116,13 @@ func TestCreateTaskInvalidRetries(t *testing.T) {
|
||||
|
||||
func TestCreateTaskInvalidRecipe(t *testing.T) {
|
||||
|
||||
worker := genWid()
|
||||
|
||||
resp := createTask(api.CreateTaskRequest{
|
||||
Project: 1,
|
||||
Recipe: "",
|
||||
MaxRetries: 3,
|
||||
})
|
||||
}, worker)
|
||||
|
||||
if resp.Ok != false {
|
||||
t.Error()
|
||||
@@ -132,12 +145,14 @@ func TestCreateGetTask(t *testing.T) {
|
||||
Public: true,
|
||||
})
|
||||
|
||||
worker := genWid()
|
||||
|
||||
createTask(api.CreateTaskRequest{
|
||||
Project: resp.Id,
|
||||
Recipe: "{\"url\":\"test\"}",
|
||||
MaxRetries: 3,
|
||||
Priority: 9999,
|
||||
})
|
||||
}, worker)
|
||||
|
||||
taskResp := getTaskFromProject(resp.Id, genWid())
|
||||
|
||||
@@ -194,26 +209,27 @@ func createTasks(prefix string) (int64, int64) {
|
||||
Priority: 999,
|
||||
Public: true,
|
||||
})
|
||||
worker := genWid()
|
||||
createTask(api.CreateTaskRequest{
|
||||
Project: lowP.Id,
|
||||
Recipe: "low1",
|
||||
Priority: 0,
|
||||
})
|
||||
}, worker)
|
||||
createTask(api.CreateTaskRequest{
|
||||
Project: lowP.Id,
|
||||
Recipe: "low2",
|
||||
Priority: 1,
|
||||
})
|
||||
}, worker)
|
||||
createTask(api.CreateTaskRequest{
|
||||
Project: highP.Id,
|
||||
Recipe: "high1",
|
||||
Priority: 100,
|
||||
})
|
||||
}, worker)
|
||||
createTask(api.CreateTaskRequest{
|
||||
Project: highP.Id,
|
||||
Recipe: "high2",
|
||||
Priority: 101,
|
||||
})
|
||||
}, worker)
|
||||
|
||||
return lowP.Id, highP.Id
|
||||
}
|
||||
@@ -274,7 +290,7 @@ func TestTaskPriority(t *testing.T) {
|
||||
|
||||
func TestTaskNoAccess(t *testing.T) {
|
||||
|
||||
wid := genWid()
|
||||
worker := genWid()
|
||||
|
||||
pid := createProject(api.CreateProjectRequest{
|
||||
Name: "This is a private proj",
|
||||
@@ -292,16 +308,16 @@ func TestTaskNoAccess(t *testing.T) {
|
||||
MaxAssignTime: 10,
|
||||
MaxRetries: 2,
|
||||
Recipe: "---",
|
||||
})
|
||||
}, worker)
|
||||
|
||||
if createResp.Ok != true {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
grantAccess(wid, pid)
|
||||
removeAccess(wid, pid)
|
||||
grantAccess(&worker.Id, pid)
|
||||
removeAccess(&worker.Id, pid)
|
||||
|
||||
tResp := getTaskFromProject(pid, wid)
|
||||
tResp := getTaskFromProject(pid, worker)
|
||||
|
||||
if tResp.Ok != false {
|
||||
t.Error()
|
||||
@@ -316,7 +332,7 @@ func TestTaskNoAccess(t *testing.T) {
|
||||
|
||||
func TestTaskHasAccess(t *testing.T) {
|
||||
|
||||
wid := genWid()
|
||||
worker := genWid()
|
||||
|
||||
pid := createProject(api.CreateProjectRequest{
|
||||
Name: "This is a private proj1",
|
||||
@@ -334,15 +350,15 @@ func TestTaskHasAccess(t *testing.T) {
|
||||
MaxAssignTime: 10,
|
||||
MaxRetries: 2,
|
||||
Recipe: "---",
|
||||
})
|
||||
}, worker)
|
||||
|
||||
if createResp.Ok != true {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
grantAccess(wid, pid)
|
||||
grantAccess(&worker.Id, pid)
|
||||
|
||||
tResp := getTaskFromProject(pid, wid)
|
||||
tResp := getTaskFromProject(pid, worker)
|
||||
|
||||
if tResp.Ok != true {
|
||||
t.Error()
|
||||
@@ -354,16 +370,16 @@ func TestTaskHasAccess(t *testing.T) {
|
||||
|
||||
func TestNoMoreTasks(t *testing.T) {
|
||||
|
||||
wid := genWid()
|
||||
worker := genWid()
|
||||
|
||||
for i := 0; i < 15; i++ {
|
||||
getTask(wid)
|
||||
getTask(worker)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReleaseTaskSuccess(t *testing.T) {
|
||||
|
||||
//wid := genWid()
|
||||
worker := genWid()
|
||||
|
||||
pid := createProject(api.CreateProjectRequest{
|
||||
Priority: 0,
|
||||
@@ -372,6 +388,7 @@ func TestReleaseTaskSuccess(t *testing.T) {
|
||||
Version: "11111111111111111",
|
||||
Name: "testreleasetask",
|
||||
Motd: "",
|
||||
Public: true,
|
||||
}).Id
|
||||
|
||||
createTask(api.CreateTaskRequest{
|
||||
@@ -379,13 +396,119 @@ func TestReleaseTaskSuccess(t *testing.T) {
|
||||
Project: pid,
|
||||
Recipe: "{}",
|
||||
MaxRetries: 3,
|
||||
})
|
||||
}, worker)
|
||||
|
||||
task := getTaskFromProject(pid, worker).Task
|
||||
|
||||
releaseResp := releaseTask(api.ReleaseTaskRequest{
|
||||
TaskId: task.Id,
|
||||
Success: true,
|
||||
}, worker)
|
||||
|
||||
if releaseResp.Ok != true {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
otherTask := getTaskFromProject(pid, worker)
|
||||
|
||||
//Shouldn't have more tasks available
|
||||
if otherTask.Ok != false {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func createTask(request api.CreateTaskRequest) *api.CreateTaskResponse {
|
||||
func TestCreateIntCollision(t *testing.T) {
|
||||
|
||||
r := Post("/task/create", request)
|
||||
pid := createProject(api.CreateProjectRequest{
|
||||
Priority: 1,
|
||||
GitRepo: "testcreateintcollision",
|
||||
CloneUrl: "testcreateintcollision",
|
||||
Motd: "testcreateintcollision",
|
||||
Public: true,
|
||||
Name: "testcreateintcollision",
|
||||
Version: "testcreateintcollision",
|
||||
}).Id
|
||||
|
||||
w := genWid()
|
||||
|
||||
if createTask(api.CreateTaskRequest{
|
||||
Project: pid,
|
||||
Hash64: 123,
|
||||
Priority: 1,
|
||||
Recipe: "{}",
|
||||
}, w).Ok != true {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
resp := createTask(api.CreateTaskRequest{
|
||||
Project: pid,
|
||||
Hash64: 123,
|
||||
Priority: 1,
|
||||
Recipe: "{}",
|
||||
}, w)
|
||||
|
||||
if resp.Ok != false {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
fmt.Println(resp.Message)
|
||||
if len(resp.Message) <= 0 {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateStringCollision(t *testing.T) {
|
||||
|
||||
pid := createProject(api.CreateProjectRequest{
|
||||
Priority: 1,
|
||||
GitRepo: "testcreatestringcollision",
|
||||
CloneUrl: "testcreatestringcollision",
|
||||
Motd: "testcreatestringcollision",
|
||||
Public: true,
|
||||
Name: "testcreatestringcollision",
|
||||
Version: "testcreatestringcollision",
|
||||
}).Id
|
||||
|
||||
w := genWid()
|
||||
|
||||
if createTask(api.CreateTaskRequest{
|
||||
Project: pid,
|
||||
UniqueString: "Hello, world",
|
||||
Priority: 1,
|
||||
Recipe: "{}",
|
||||
}, w).Ok != true {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
resp := createTask(api.CreateTaskRequest{
|
||||
Project: pid,
|
||||
UniqueString: "Hello, world",
|
||||
Priority: 1,
|
||||
Recipe: "{}",
|
||||
}, w)
|
||||
|
||||
if !createTask(api.CreateTaskRequest{
|
||||
Project: pid,
|
||||
UniqueString: "This one should work",
|
||||
Priority: 1,
|
||||
Recipe: "{}",
|
||||
}, w).Ok {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
if resp.Ok != false {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
fmt.Println(resp.Message)
|
||||
if len(resp.Message) <= 0 {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func createTask(request api.CreateTaskRequest, worker *storage.Worker) *api.CreateTaskResponse {
|
||||
|
||||
r := Post("/task/create", request, worker)
|
||||
|
||||
var resp api.CreateTaskResponse
|
||||
data, _ := ioutil.ReadAll(r.Body)
|
||||
@@ -395,9 +518,9 @@ func createTask(request api.CreateTaskRequest) *api.CreateTaskResponse {
|
||||
return &resp
|
||||
}
|
||||
|
||||
func getTask(wid *uuid.UUID) *api.GetTaskResponse {
|
||||
func getTask(worker *storage.Worker) *api.GetTaskResponse {
|
||||
|
||||
r := Get(fmt.Sprintf("/task/get?wid=%s", wid))
|
||||
r := Get(fmt.Sprintf("/task/get"), worker)
|
||||
|
||||
var resp api.GetTaskResponse
|
||||
data, _ := ioutil.ReadAll(r.Body)
|
||||
@@ -407,9 +530,9 @@ func getTask(wid *uuid.UUID) *api.GetTaskResponse {
|
||||
return &resp
|
||||
}
|
||||
|
||||
func getTaskFromProject(project int64, wid *uuid.UUID) *api.GetTaskResponse {
|
||||
func getTaskFromProject(project int64, worker *storage.Worker) *api.GetTaskResponse {
|
||||
|
||||
r := Get(fmt.Sprintf("/task/get/%d?wid=%s", project, wid))
|
||||
r := Get(fmt.Sprintf("/task/get/%d", project), worker)
|
||||
|
||||
var resp api.GetTaskResponse
|
||||
data, _ := ioutil.ReadAll(r.Body)
|
||||
@@ -418,3 +541,15 @@ func getTaskFromProject(project int64, wid *uuid.UUID) *api.GetTaskResponse {
|
||||
|
||||
return &resp
|
||||
}
|
||||
|
||||
func releaseTask(request api.ReleaseTaskRequest, worker *storage.Worker) *api.ReleaseTaskResponse {
|
||||
|
||||
r := Post("/task/release", request, worker)
|
||||
|
||||
var resp api.ReleaseTaskResponse
|
||||
data, _ := ioutil.ReadAll(r.Body)
|
||||
err := json.Unmarshal(data, &resp)
|
||||
handleErr(err)
|
||||
|
||||
return &resp
|
||||
}
|
||||
|
||||
@@ -7,12 +7,15 @@ import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"src/task_tracker/api"
|
||||
"src/task_tracker/storage"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCreateGetWorker(t *testing.T) {
|
||||
|
||||
resp, r := createWorker(api.CreateWorkerRequest{})
|
||||
resp, r := createWorker(api.CreateWorkerRequest{
|
||||
Alias: "my_worker_alias",
|
||||
})
|
||||
|
||||
if r.StatusCode != 200 {
|
||||
t.Error()
|
||||
@@ -22,12 +25,12 @@ func TestCreateGetWorker(t *testing.T) {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
getResp, r := getWorker(resp.WorkerId.String())
|
||||
getResp, r := getWorker(resp.Worker.Id.String())
|
||||
|
||||
if r.StatusCode != 200 {
|
||||
t.Error()
|
||||
}
|
||||
if resp.WorkerId != getResp.Worker.Id {
|
||||
if resp.Worker.Id != getResp.Worker.Id {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
@@ -37,6 +40,9 @@ func TestCreateGetWorker(t *testing.T) {
|
||||
if len(getResp.Worker.Identity.UserAgent) <= 0 {
|
||||
t.Error()
|
||||
}
|
||||
if resp.Worker.Alias != "my_worker_alias" {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetWorkerNotFound(t *testing.T) {
|
||||
@@ -70,7 +76,7 @@ func TestGrantAccessFailedProjectConstraint(t *testing.T) {
|
||||
|
||||
wid := genWid()
|
||||
|
||||
resp := grantAccess(wid, 38274593)
|
||||
resp := grantAccess(&wid.Id, 38274593)
|
||||
|
||||
if resp.Ok != false {
|
||||
t.Error()
|
||||
@@ -82,9 +88,9 @@ func TestGrantAccessFailedProjectConstraint(t *testing.T) {
|
||||
|
||||
func TestRemoveAccessFailedProjectConstraint(t *testing.T) {
|
||||
|
||||
wid := genWid()
|
||||
worker := genWid()
|
||||
|
||||
resp := removeAccess(wid, 38274593)
|
||||
resp := removeAccess(&worker.Id, 38274593)
|
||||
|
||||
if resp.Ok != false {
|
||||
t.Error()
|
||||
@@ -138,8 +144,43 @@ func TestGrantAccessFailedWorkerConstraint(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateAliasValid(t *testing.T) {
|
||||
|
||||
wid := genWid()
|
||||
|
||||
updateResp := updateWorker(api.UpdateWorkerRequest{
|
||||
Alias: "new alias",
|
||||
}, wid)
|
||||
|
||||
if updateResp.Ok != true {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
w, _ := getWorker(wid.Id.String())
|
||||
|
||||
if w.Worker.Alias != "new alias" {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateWorkerAliasInvalid(t *testing.T) {
|
||||
|
||||
resp, _ := createWorker(api.CreateWorkerRequest{
|
||||
Alias: "unassigned", //reserved alias
|
||||
})
|
||||
|
||||
if resp.Ok != false {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
if len(resp.Message) <= 0 {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func createWorker(req api.CreateWorkerRequest) (*api.CreateWorkerResponse, *http.Response) {
|
||||
r := Post("/worker/create", req)
|
||||
r := Post("/worker/create", req, nil)
|
||||
|
||||
var resp *api.CreateWorkerResponse
|
||||
data, _ := ioutil.ReadAll(r.Body)
|
||||
@@ -151,7 +192,7 @@ func createWorker(req api.CreateWorkerRequest) (*api.CreateWorkerResponse, *http
|
||||
|
||||
func getWorker(id string) (*api.GetWorkerResponse, *http.Response) {
|
||||
|
||||
r := Get(fmt.Sprintf("/worker/get/%s", id))
|
||||
r := Get(fmt.Sprintf("/worker/get/%s", id), nil)
|
||||
|
||||
var resp *api.GetWorkerResponse
|
||||
data, _ := ioutil.ReadAll(r.Body)
|
||||
@@ -161,10 +202,10 @@ func getWorker(id string) (*api.GetWorkerResponse, *http.Response) {
|
||||
return resp, r
|
||||
}
|
||||
|
||||
func genWid() *uuid.UUID {
|
||||
func genWid() *storage.Worker {
|
||||
|
||||
resp, _ := createWorker(api.CreateWorkerRequest{})
|
||||
return &resp.WorkerId
|
||||
return resp.Worker
|
||||
}
|
||||
|
||||
func grantAccess(wid *uuid.UUID, project int64) *api.WorkerAccessResponse {
|
||||
@@ -172,7 +213,7 @@ func grantAccess(wid *uuid.UUID, project int64) *api.WorkerAccessResponse {
|
||||
r := Post("/access/grant", api.WorkerAccessRequest{
|
||||
WorkerId: wid,
|
||||
ProjectId: project,
|
||||
})
|
||||
}, nil)
|
||||
|
||||
var resp *api.WorkerAccessResponse
|
||||
data, _ := ioutil.ReadAll(r.Body)
|
||||
@@ -187,7 +228,7 @@ func removeAccess(wid *uuid.UUID, project int64) *api.WorkerAccessResponse {
|
||||
r := Post("/access/remove", api.WorkerAccessRequest{
|
||||
WorkerId: wid,
|
||||
ProjectId: project,
|
||||
})
|
||||
}, nil)
|
||||
|
||||
var resp *api.WorkerAccessResponse
|
||||
data, _ := ioutil.ReadAll(r.Body)
|
||||
@@ -196,3 +237,15 @@ func removeAccess(wid *uuid.UUID, project int64) *api.WorkerAccessResponse {
|
||||
|
||||
return resp
|
||||
}
|
||||
|
||||
func updateWorker(request api.UpdateWorkerRequest, w *storage.Worker) *api.UpdateWorkerResponse {
|
||||
|
||||
r := Post("/worker/update", request, w)
|
||||
|
||||
var resp *api.UpdateWorkerResponse
|
||||
data, _ := ioutil.ReadAll(r.Body)
|
||||
err := json.Unmarshal(data, &resp)
|
||||
handleErr(err)
|
||||
|
||||
return resp
|
||||
}
|
||||
|
||||
@@ -2,26 +2,61 @@ package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto"
|
||||
"crypto/hmac"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"src/task_tracker/config"
|
||||
"src/task_tracker/storage"
|
||||
)
|
||||
|
||||
func Post(path string, x interface{}) *http.Response {
|
||||
func Post(path string, x interface{}, worker *storage.Worker) *http.Response {
|
||||
|
||||
body, err := json.Marshal(x)
|
||||
buf := bytes.NewBuffer(body)
|
||||
|
||||
r, err := http.Post("http://"+config.Cfg.ServerAddr+path, "application/json", buf)
|
||||
req, err := http.NewRequest("POST", "http://"+config.Cfg.ServerAddr+path, buf)
|
||||
handleErr(err)
|
||||
|
||||
if worker != nil {
|
||||
mac := hmac.New(crypto.SHA256.New, worker.Secret)
|
||||
mac.Write(body)
|
||||
sig := hex.EncodeToString(mac.Sum(nil))
|
||||
|
||||
req.Header.Add("X-Worker-Id", worker.Id.String())
|
||||
req.Header.Add("X-Signature", sig)
|
||||
}
|
||||
|
||||
client := http.Client{}
|
||||
r, err := client.Do(req)
|
||||
handleErr(err)
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func Get(path string) *http.Response {
|
||||
r, err := http.Get("http://" + config.Cfg.ServerAddr + path)
|
||||
func Get(path string, worker *storage.Worker) *http.Response {
|
||||
|
||||
url := "http://" + config.Cfg.ServerAddr + path
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
handleErr(err)
|
||||
|
||||
if worker != nil {
|
||||
|
||||
fmt.Println(worker.Secret)
|
||||
mac := hmac.New(crypto.SHA256.New, worker.Secret)
|
||||
mac.Write([]byte(path))
|
||||
sig := hex.EncodeToString(mac.Sum(nil))
|
||||
|
||||
req.Header.Add("X-Worker-Id", worker.Id.String())
|
||||
req.Header.Add("X-Signature", sig)
|
||||
}
|
||||
|
||||
client := http.Client{}
|
||||
r, err := client.Do(req)
|
||||
handleErr(err)
|
||||
|
||||
return r
|
||||
|
||||
@@ -26,9 +26,10 @@ CREATE TABLE worker_identity
|
||||
CREATE TABLE worker
|
||||
(
|
||||
id TEXT PRIMARY KEY,
|
||||
alias TEXT DEFAULT NULL,
|
||||
alias TEXT,
|
||||
created INTEGER,
|
||||
identity INTEGER REFERENCES workerIdentity (id)
|
||||
identity INTEGER REFERENCES worker_identity (id),
|
||||
secret BYTEA
|
||||
);
|
||||
|
||||
CREATE TABLE project
|
||||
@@ -61,7 +62,8 @@ CREATE TABLE task
|
||||
status Status DEFAULT 'new',
|
||||
recipe TEXT,
|
||||
max_assign_time INTEGER DEFAULT 0,
|
||||
assign_time INTEGER DEFAULT 0
|
||||
assign_time INTEGER DEFAULT 0,
|
||||
hash64 BIGINT UNIQUE
|
||||
);
|
||||
|
||||
CREATE TABLE log_entry
|
||||
|
||||
Reference in New Issue
Block a user