Initial commit

This commit is contained in:
simon987
2019-01-12 16:18:14 -05:00
commit 83276ce8b0
25 changed files with 1367 additions and 0 deletions

110
test/api_log_test.go Normal file
View File

@@ -0,0 +1,110 @@
package test
import (
"src/task_tracker/api"
"testing"
"time"
)
func TestTraceValid(t *testing.T) {
r := Post("/log/trace", api.LogEntry{
Scope:"test",
Message:"This is a test message",
TimeStamp: time.Now().Unix(),
})
if r.StatusCode != 200 {
t.Fail()
}
}
func TestTraceInvalidScope(t *testing.T) {
r := Post("/log/trace", api.LogEntry{
Message:"this is a test message",
TimeStamp: time.Now().Unix(),
})
if r.StatusCode != 500 {
t.Fail()
}
r = Post("/log/trace", api.LogEntry{
Scope:"",
Message:"this is a test message",
TimeStamp: time.Now().Unix(),
})
if r.StatusCode != 500 {
t.Fail()
}
if GenericJson(r.Body)["message"] != "invalid scope" {
t.Fail()
}
}
func TestTraceInvalidMessage(t *testing.T) {
r := Post("/log/trace", api.LogEntry{
Scope:"test",
Message:"",
TimeStamp: time.Now().Unix(),
})
if r.StatusCode != 500 {
t.Fail()
}
if GenericJson(r.Body)["message"] != "invalid message" {
t.Fail()
}
}
func TestTraceInvalidTime(t *testing.T) {
r := Post("/log/trace", api.LogEntry{
Scope: "test",
Message:"test",
})
if r.StatusCode != 500 {
t.Fail()
}
if GenericJson(r.Body)["message"] != "invalid timestamp" {
t.Fail()
}
}
func TestWarnValid(t *testing.T) {
r := Post("/log/warn", api.LogEntry{
Scope: "test",
Message:"test",
TimeStamp:time.Now().Unix(),
})
if r.StatusCode != 200 {
t.Fail()
}
}
func TestInfoValid(t *testing.T) {
r := Post("/log/info", api.LogEntry{
Scope: "test",
Message:"test",
TimeStamp:time.Now().Unix(),
})
if r.StatusCode != 200 {
t.Fail()
}
}
func TestErrorValid(t *testing.T) {
r := Post("/log/error", api.LogEntry{
Scope: "test",
Message:"test",
TimeStamp:time.Now().Unix(),
})
if r.StatusCode != 200 {
t.Fail()
}
}

114
test/api_project_test.go Normal file
View File

@@ -0,0 +1,114 @@
package test
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"src/task_tracker/api"
"testing"
)
func TestCreateGetProject(t *testing.T) {
resp := createProject(api.CreateProjectRequest{
Name: "Test name",
GitUrl: "http://github.com/test/test",
Version: "Test Version",
})
id := resp.Id
if id == 0 {
t.Fail()
}
if resp.Ok != true {
t.Fail()
}
getResp, _ := getProject(id)
if getResp.Project.Id != id {
t.Fail()
}
if getResp.Project.Name != "Test name" {
t.Fail()
}
if getResp.Project.Version != "Test Version" {
t.Fail()
}
if getResp.Project.GitUrl != "http://github.com/test/test" {
t.Fail()
}
}
func TestCreateProjectInvalid(t *testing.T) {
resp := createProject(api.CreateProjectRequest{
})
if resp.Ok != false {
t.Fail()
}
}
func TestCreateDuplicateProject(t *testing.T) {
createProject(api.CreateProjectRequest{
Name: "duplicate name",
})
resp := createProject(api.CreateProjectRequest{
Name: "duplicate name",
})
if resp.Ok != false {
t.Fail()
}
if len(resp.Message) <= 0 {
t.Fail()
}
}
func TestGetProjectNotFound(t *testing.T) {
getResp, r := getProject(12345)
if getResp.Ok != false {
t.Fail()
}
if len(getResp.Message) <= 0 {
t.Fail()
}
if r.StatusCode != 404 {
t.Fail()
}
}
func createProject(req api.CreateProjectRequest) *api.CreateProjectResponse {
r := Post("/project/create", req)
var resp api.CreateProjectResponse
data, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(data, &resp)
handleErr(err)
return &resp
}
func getProject(id int64) (*api.GetProjectResponse, *http.Response) {
r := Get(fmt.Sprintf("/project/get/%d", id))
var getResp api.GetProjectResponse
data, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(data, &getResp)
handleErr(err)
return &getResp, r
}

74
test/api_task_test.go Normal file
View File

@@ -0,0 +1,74 @@
package test
import (
"encoding/json"
"io/ioutil"
"src/task_tracker/api"
"testing"
)
func TestCreateTaskValid(t *testing.T) {
//Make sure there is always a project for id:1
createProject(api.CreateProjectRequest{
Name: "Some Test name",
Version: "Test Version",
GitUrl: "http://github.com/test/test",
})
resp := createTask(api.CreateTaskRequest{
Project:1,
Recipe: "{}",
MaxRetries:3,
})
if resp.Ok != true {
t.Fail()
}
}
func TestCreateTaskInvalidProject(t *testing.T) {
resp := createTask(api.CreateTaskRequest{
Project:123456,
Recipe: "{}",
MaxRetries:3,
})
if resp.Ok != false {
t.Fail()
}
if len(resp.Message) <= 0 {
t.Fail()
}
}
func TestCreateTaskInvalidRetries(t *testing.T) {
resp := createTask(api.CreateTaskRequest{
Project:1,
MaxRetries:-1,
})
if resp.Ok != false {
t.Fail()
}
if len(resp.Message) <= 0 {
t.Fail()
}
}
func createTask(request api.CreateTaskRequest) *api.CreateTaskResponse {
r := Post("/task/create", request)
var resp api.CreateTaskResponse
data, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(data, &resp)
handleErr(err)
return &resp
}

83
test/api_worker_test.go Normal file
View File

@@ -0,0 +1,83 @@
package test
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"src/task_tracker/api"
"testing"
)
func TestCreateGetWorker(t *testing.T) {
resp, r := createWorker(api.CreateWorkerRequest{})
if r.StatusCode != 200 {
t.Fail()
}
if resp.Ok != true {
t.Fail()
}
getResp, r := getWorker(resp.WorkerId)
if r.StatusCode != 200 {
t.Fail()
}
if resp.WorkerId != getResp.Worker.Id.String() {
t.Fail()
}
}
func TestGetWorkerNotFound(t *testing.T) {
resp, r := getWorker("8bfc0ccd-d5ce-4dc5-a235-3a7ae760d9c6")
if r.StatusCode != 404 {
t.Fail()
}
if resp.Ok != false {
t.Fail()
}
}
func TestGetWorkerInvalid(t *testing.T) {
resp, r := getWorker("invalid-uuid")
if r.StatusCode != 400 {
t.Fail()
}
if resp.Ok != false {
t.Fail()
}
if len(resp.Message) <= 0 {
t.Fail()
}
}
func createWorker(req api.CreateWorkerRequest) (*api.CreateWorkerResponse, *http.Response) {
r := Post("/worker/create", req)
var resp *api.CreateWorkerResponse
data, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(data, &resp)
handleErr(err)
return resp, r
}
func getWorker(id string) (*api.GetWorkerResponse, *http.Response) {
r := Get(fmt.Sprintf("/worker/get/%s", id))
var resp *api.GetWorkerResponse
data, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(data, &resp)
handleErr(err)
return resp, r
}

53
test/common.go Normal file
View File

@@ -0,0 +1,53 @@
package test
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"src/task_tracker/config"
)
func Post(path string, x interface{}) *http.Response {
body, err := json.Marshal(x)
buf := bytes.NewBuffer(body)
r, err := http.Post("http://" + config.Cfg.ServerAddr + path, "application/json", buf)
handleErr(err)
return r
}
func Get(path string) *http.Response {
r, err := http.Get("http://" + config.Cfg.ServerAddr + path)
handleErr(err)
return r
}
func handleErr(err error) {
if err != nil {
panic(err)
}
}
func Print(body io.ReadCloser) {
rawBody, _ := ioutil.ReadAll(body)
fmt.Println(string(rawBody))
}
func GenericJson(body io.ReadCloser) map[string]interface{} {
var obj map[string]interface{}
data, _ := ioutil.ReadAll(body)
err := json.Unmarshal(data, &obj)
handleErr(err)
return obj
}

5
test/config.yml Normal file
View File

@@ -0,0 +1,5 @@
server:
address: "127.0.0.1:5001"
database:
conn_str : "user=task_tracker dbname=task_tracker_test sslmode=disable"

20
test/main_test.go Normal file
View File

@@ -0,0 +1,20 @@
package test
import (
"src/task_tracker/api"
"src/task_tracker/config"
"testing"
"time"
)
func TestMain(m *testing.M) {
config.SetupConfig()
testApi := api.New()
testApi.Database.Reset()
go testApi.Run()
time.Sleep(time.Millisecond * 100)
m.Run()
}

1
test/schema.sql Symbolic link
View File

@@ -0,0 +1 @@
../schema.sql