forked from evergreen-ci/logkeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logkeeper_test.go
196 lines (169 loc) · 6.38 KB
/
logkeeper_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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package logkeeper
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/evergreen-ci/logkeeper/db"
"github.com/mongodb/grip"
. "github.com/smartystreets/goconvey/convey"
"github.com/smartystreets/goconvey/convey/reporting"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
func resetDatabase(db *mgo.Database) {
grip.Error(db.DropDatabase())
}
func init() {
reporting.QuietMode()
}
func TestLogKeeper(t *testing.T) {
session, err := mgo.Dial("localhost:27017")
if err != nil {
t.Fatal(err)
}
if err = db.SetSession(session); err != nil {
t.Fatal(err)
}
Convey("LogKeeper instance running on testdatabase", t, func() {
lk := New(Options{DB: "logkeeper_test", MaxRequestSize: 1024 * 1024 * 10})
db := session.DB("logkeeper_test")
router := lk.NewRouter()
Convey("Call POST /build creates a build with the given builder/buildnum", func() {
r := newTestRequest(lk, "POST", "/build/", map[string]interface{}{"builder": "poop", "buildnum": 123})
data := checkEndpointResponse(router, r, http.StatusCreated)
So(data["id"], ShouldNotBeNil)
So(data["uri"], ShouldNotBeNil)
originalId, originalURI := data["id"], data["uri"]
// Call POST /build again,
r = newTestRequest(lk, "POST", "/build/", map[string]interface{}{"builder": "poop", "buildnum": 123})
data = checkEndpointResponse(router, r, http.StatusOK)
So(data["id"], ShouldEqual, originalId)
So(data["uri"], ShouldEqual, originalURI)
})
Convey("Logkeeper breaks oversize log into pieces", func() {
// Create build and test
r := newTestRequest(lk, "POST", "/build", map[string]interface{}{"builder": "myBuilder", "buildnum": 123})
data := checkEndpointResponse(router, r, http.StatusCreated)
So(data["id"], ShouldNotBeNil)
buildId := data["id"].(string)
r = newTestRequest(lk, "POST", "/build/"+buildId+"/test", map[string]interface{}{"test_filename": "myTestFileName", "command": "myCommand", "phase": "myPhase"})
data = checkEndpointResponse(router, r, http.StatusCreated)
So(data["id"], ShouldNotBeNil)
testId := data["id"].(string)
// Insert oversize log
line := strings.Repeat("a", 2097152)
now := time.Now().Unix()
r = newTestRequest(lk, "POST", "/build/"+buildId+"/test/"+testId, [][]interface{}{{now, line}, {now, line}, {now, line}})
data = checkEndpointResponse(router, r, http.StatusCreated)
So(len(data), ShouldBeGreaterThan, 0)
// Test should have seq = 2
test := &Test{}
err := db.C("tests").Find(bson.M{"_id": idFromString(testId)}).One(test)
So(err, ShouldBeNil)
So(test.Seq, ShouldEqual, 2)
// Test should have two logs
numLogs, err := db.C("logs").Find(bson.M{"test_id": idFromString(testId)}).Count()
So(err, ShouldBeNil)
So(numLogs, ShouldEqual, 2)
// First log should have two lines and seq=1
// Second log should have one line and seq=2
logs := db.C("logs").Find(bson.M{"test_id": idFromString(testId)}).Sort("seq").Iter()
log := &Log{}
firstLog := true
for logs.Next(log) {
if firstLog {
So(len(log.Lines), ShouldEqual, 2)
So(log.Seq, ShouldEqual, 1)
firstLog = false
} else {
So(len(log.Lines), ShouldEqual, 1)
So(log.Seq, ShouldEqual, 2)
}
}
So(db.DropDatabase(), ShouldBeNil)
// Create build
r = newTestRequest(lk, "POST", "/build", map[string]interface{}{"builder": "myBuilder", "buildnum": 123})
data = checkEndpointResponse(router, r, http.StatusCreated)
So(data["id"], ShouldNotBeNil)
buildId = data["id"].(string)
// Insert oversize global log
r = newTestRequest(lk, "POST", "/build/"+buildId, [][]interface{}{{now, line}, {now, line}, {now, line}})
data = checkEndpointResponse(router, r, http.StatusCreated)
So(len(data), ShouldBeGreaterThan, 0)
// Build should have seq = 2
build := &LogKeeperBuild{}
err = db.C("builds").Find(bson.M{"_id": idFromString(buildId)}).One(build)
So(err, ShouldBeNil)
So(build.Seq, ShouldEqual, 2)
// Build should have two logs
numLogs, err = db.C("logs").Find(bson.M{"build_id": idFromString(buildId)}).Count()
So(err, ShouldBeNil)
So(numLogs, ShouldEqual, 2)
// First log should have two lines and seq=1
// Second log should have one line and seq=2
logs = db.C("logs").Find(bson.M{"build_id": idFromString(buildId)}).Sort("seq").Iter()
log = &Log{}
firstLog = true
for logs.Next(log) {
if firstLog {
So(len(log.Lines), ShouldEqual, 2)
So(log.Seq, ShouldEqual, 1)
firstLog = false
} else {
So(len(log.Lines), ShouldEqual, 1)
So(log.Seq, ShouldEqual, 2)
}
}
// Inserting oversize log line fails
line = strings.Repeat("a", 4194305)
r = newTestRequest(lk, "POST", "/build/"+buildId, [][]interface{}{{now, line}})
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
So(w.Code, ShouldEqual, http.StatusBadRequest)
})
Convey("Adding the task id field will correctly insert it in the database", func() {
// Create build and test
r := newTestRequest(lk, "POST", "/build", map[string]interface{}{"builder": "myBuilder", "buildnum": 123})
data := checkEndpointResponse(router, r, http.StatusCreated)
So(data["id"], ShouldNotBeNil)
buildId := data["id"].(string)
r = newTestRequest(lk, "POST", "/build/"+buildId+"/test", map[string]interface{}{"test_filename": "myTestFileName", "command": "myCommand", "phase": "myPhase", "task_id": "abc123"})
data = checkEndpointResponse(router, r, http.StatusCreated)
So(data["id"], ShouldNotBeNil)
testId := data["id"].(string)
test := &Test{}
err := db.C("tests").Find(bson.M{"_id": idFromString(testId)}).One(test)
So(err, ShouldBeNil)
So(test.Info, ShouldNotBeNil)
So(test.Info["task_id"], ShouldEqual, "abc123")
})
// Clear database
Reset(func() { resetDatabase(db) })
})
}
func checkEndpointResponse(router http.Handler, req *http.Request, expectedCode int) map[string]interface{} {
w := httptest.NewRecorder()
decoded := map[string]interface{}{}
router.ServeHTTP(w, req)
err := json.Unmarshal(w.Body.Bytes(), &decoded)
So(err, ShouldBeNil)
So(w.Code, ShouldEqual, expectedCode)
return decoded
}
func newTestRequest(lk *logKeeper, method, path string, body interface{}) *http.Request {
req, err := http.NewRequest(method, lk.opts.URL+path, nil)
if err != nil {
panic(err)
}
jsonbytes, err := json.Marshal(body)
if err != nil {
panic(err)
}
req.Body = ioutil.NopCloser(bytes.NewReader(jsonbytes))
return req
}