-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_job_test.go
163 lines (136 loc) · 5.32 KB
/
test_job_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"encoding/json"
"io/ioutil"
"strconv"
"testing"
"time"
)
var FetchJobsAPIResponse []byte = []byte(`
[{
"command":"bin/rails runner -e test '$LOAD_PATH.push(\"#{Rails.root}/test\"); require \"test/controllers/dashboard_controller_test.rb\".gsub(/^test\\//,\"\")'",
"created_at":"2016-07-09T09:03:05.717Z",
"id":109136,
"cost_prediction":"1.824951",
"sent_at_seconds_since_epoch":1468054988,
"test_run":{"commit_sha":"f151713e400ac3d8dc1291fe21a413a6f813072d",
"id":1915,
"project":{"repository_ssh_url":"git@github.com:ispyropoulos/katana.git"}
}
},
{
"command":"bin/rails runner -e test '$LOAD_PATH.push(\"#{Rails.root}/test\"); require \"test/controllers/dashboard_controller_test.rb\".gsub(/^test\\//,\"\")'",
"created_at":"2016-07-09T09:03:05.717Z",
"id":109136,
"cost_prediction":"0",
"sent_at_seconds_since_epoch":1468054988,
"test_run":{"commit_sha":"f151713e400ac3d8dc1291fe21a413a6f813072d",
"id":1915,
"project":{"repository_ssh_url":"git@github.com:ispyropoulos/katana.git"}
}
}]
`)
func prepareTestJobBuilder() TestJobBuilder {
var parsedResponse interface{}
_ = json.Unmarshal(FetchJobsAPIResponse, &parsedResponse)
return TestJobBuilder(parsedResponse.([]interface{})[0].(map[string]interface{}))
}
func prepareNoPredictionTestJobBuilder() TestJobBuilder {
var parsedResponse interface{}
_ = json.Unmarshal(FetchJobsAPIResponse, &parsedResponse)
return TestJobBuilder(parsedResponse.([]interface{})[1].(map[string]interface{}))
}
func TestBuilderId(t *testing.T) {
builder := prepareTestJobBuilder()
if builder.id() != 109136 {
t.Error("It should return the correct id (109136) but got: ", builder.id())
}
}
// http://stackoverflow.com/a/522281/974285
// http://stackoverflow.com/a/34422459/974285
func TestBuilderCreatedAt(t *testing.T) {
builder := prepareTestJobBuilder()
parsedTime, _ := time.Parse(time.RFC3339, "2016-07-09T09:03:05.717Z")
if builder.createdAt() != parsedTime {
t.Error("It should parse 2016-07-09T09:03:05.717Z but got: ", builder.createdAt())
}
}
func TestBuilderSentAtSecondsSinceEpoch(t *testing.T) {
builder := prepareTestJobBuilder()
if builder.sentAtSecondsSinceEpoch() != 1468054988 {
t.Error("It should return 1468054988 but got: ", builder.sentAtSecondsSinceEpoch())
}
}
func TestBuilderCostPredictionSeconds(t *testing.T) {
builder := prepareTestJobBuilder()
if builder.costPredictionSeconds() != 1.824951 {
t.Error("It should return 1.824951 but got: ", builder.costPredictionSeconds())
}
}
func TestBuilderCostPredictionSecondsWhenPredictionIsZero(t *testing.T) {
builder := prepareNoPredictionTestJobBuilder()
if builder.costPredictionSeconds() != NO_PREDICTION_WORKLOAD_SECONDS {
t.Error("It should return "+strconv.Itoa(NO_PREDICTION_WORKLOAD_SECONDS)+" but got: ", builder.costPredictionSeconds())
}
}
func TestBuilderTestRunId(t *testing.T) {
builder := prepareTestJobBuilder()
if builder.testRunId() != 1915 {
t.Error("It should return 1915 but got: ", builder.testRunId())
}
}
func TestBuilderCommitSha(t *testing.T) {
builder := prepareTestJobBuilder()
if builder.commitSha() != "f151713e400ac3d8dc1291fe21a413a6f813072d" {
t.Error("It should return f151713e400ac3d8dc1291fe21a413a6f813072d but got: ", builder.testRunId())
}
}
func TestRunSettingWorkerInQueueSeconds(t *testing.T) {
testJob := TestJob{
Id: 23,
TestRunId: 12,
CostPredictionSeconds: 12,
SentAtSecondsSinceEpoch: 100,
Command: "ls",
QueuedAtSecondsSinceEpoch: time.Now().Unix() - 2,
}
testJob.Run(Logger{"test", ioutil.Discard})
// Calling Run should only take some milliseconds so rounded it should be 2 seconds.
if testJob.WorkerInQueueSeconds != 2 {
t.Error("It should set WorkerInQueueSeconds to 2 but it is: ", testJob.WorkerInQueueSeconds)
}
}
func TestRunSettingWorkerCommandRunSeconds(t *testing.T) {
testJob := TestJob{
Id: 23,
TestRunId: 12,
CostPredictionSeconds: 12,
SentAtSecondsSinceEpoch: 100,
Command: "sleep 1",
QueuedAtSecondsSinceEpoch: time.Now().Unix() - 2,
}
testJob.Run(Logger{"test", ioutil.Discard})
// Calling Run should only take some milliseconds so rounded it should be 1 seconds.
if testJob.WorkerCommandRunSeconds != 1 {
t.Error("It should set WorkerCommandRunSeconds to 1 but it is: ", testJob.WorkerCommandRunSeconds)
}
}
func TestJsonMarshal(t *testing.T) {
testJob := TestJob{
Id: 23,
TestRunId: 12,
CostPredictionSeconds: 12,
SentAtSecondsSinceEpoch: 100,
Command: "sleep 1",
QueuedAtSecondsSinceEpoch: 123,
WorkerCommandRunSeconds: 100,
WorkerInQueueSeconds: 20,
StartedAtSecondsSinceEpoch: 10,
CommitSha: "f151713e400ac3d8dc1291fe21a413a6f813072d",
}
jsonData, _ := json.Marshal(testJob)
expected := `{"id":23,"cost_prediction_seconds":12,"sent_at_seconds_since_epoch":100,"started_at_seconds_since_epoch":10,"created_at":"0001-01-01T00:00:00Z","command":"sleep 1","result":"","status":0,"test_run_id":12,"worker_in_queue_seconds":20,"worker_command_run_seconds":100,"QueuedAtSecondsSinceEpoch":123,"CommitSha":"f151713e400ac3d8dc1291fe21a413a6f813072d"}`
if string(jsonData) != expected {
t.Error("Expected: \n", expected, "\nGot: \n", string(jsonData))
}
}