Repository navigation
Expand file tree
/
Copy pathtest_suite_execution.go
More file actions
397 lines (346 loc) · 13.9 KB
/
Copy pathtest_suite_execution.go
File metadata and controls
397 lines (346 loc) · 13.9 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package main
import (
"database/sql"
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"github.com/lib/pq"
"html"
"html/template"
"log"
"net"
"net/http"
"os/exec"
"regexp"
"strconv"
"strings"
"time"
)
type SuiteExecutionHierarchyRow struct {
Type string `db:"type"`
Id int `db:"id"`
IdPath NullIntSlice `db:"id_path"`
Title string `db:"title"`
Description sql.NullString `db:"description"`
Behaviour sql.NullString `db:"behaviour"`
Live sql.NullBool `db:"live"`
TestFunction sql.NullString `db:"test_function"`
Timeout sql.NullInt64 `db:"timeout"`
Parent sql.NullInt64 `db:"parent"`
OutcomeTotal NullIntSlice `db:"outcome_total"`
Outcome sql.NullString `db:"outcome"`
Reason sql.NullString `db:"reason"`
Duration sql.NullInt64 `db:"duration"`
}
type TestResult struct {
Outcome string `json:"outcome"` // one of {"pass", "warning", "critical", "skip"}
Reason string `json:"reason"`
Duration int `json:"duration"`
}
type SuiteExecution struct {
Id int `json:"id"`
Passkey string `json:"passkey"`
Timestamp time.Time `json:"timestamp"`
UserAgent string `json:"userAgent"`
IP string `json:"ip"`
BrowserAuditVersion string `json:"browserAuditVersion"`
Settings map[string]string `json:"settings"`
TestResults map[string]TestResult `json:"testResults"`
}
func ResultsPageHandler(w http.ResponseWriter, r *http.Request) {
v := struct {
Id string
Passkey string
} {
mux.Vars(r)["id"],
mux.Vars(r)["passkey"],
}
t, err := template.ParseFiles("./results.html")
if err != nil {
log.Panic(err)
}
t.Execute(w, v)
}
func GetSuiteExecutionHandler(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"]
passkey := mux.Vars(r)["passkey"]
DontCache(&w)
w.Header().Set("Content-Type", "text/javascript")
// Recover metadata for this suite execution
row, err := db.Query("SELECT * FROM suite_execution_metadata($1, $2)", id, passkey)
if err, ok := err.(*pq.Error); ok {
// If the given test suite execution ID doesn't exist, or if the given
// passkey is incorrect for this test suite execution, output some
// JavaScript that shows the error message and hides the other (now
// irrelevant) UI objects
if err.Code.Class() == "22" && (err.Column == "id" || err.Column == "passkey") {
fmt.Fprintln(w, "browserAuditUI.notificationBar.hide();\n")
fmt.Fprintln(w, "browserAuditUI.scoreboard.hide();\n")
fmt.Fprintln(w, "browserAuditUI.executionInfoPanel.hide();\n")
fmt.Fprintln(w, "browserAuditUI.testReportList.hide();\n")
fmt.Fprintln(w, "$('#scoreboard-denied').removeClass('hide');\n")
log.Printf("Couldn't find metadata for test suite execution ID %s with passkey %s: %s\n", id, passkey, err)
return
} else {
// Otherwise, something unanticipated happened: panic
log.Panic(err)
}
}
// Extract metadata from the result row to populate the execution
// information panel in the BrowserAudit UI
var timestampISO8601, ip, userAgent string
row.Next()
if err := row.Scan(×tampISO8601, &ip, &userAgent); err != nil {
log.Panic(err)
}
// Look up the hostname(s) associated with the IP address used during
// this test suite execution
var hostname string
if hostnames, err := net.LookupAddr(ip); err == nil {
hostname = "\"(" + strings.Join(hostnames, ", ") + ")\""
} else {
hostname = "null"
}
// Look up the ISO 3166-1 alpha-2 country code associated with this IP
// address in the GeoIP2 database
var country string
if geoipRecord, err := geoipDB.Country(net.ParseIP(ip)); err == nil {
country = fmt.Sprintf("[ [\"/static/img/flags-iso/flat/16/%s.png\", \"%s\"] ]", geoipRecord.Country.IsoCode, geoipRecord.Country.Names["en"])
} else {
country = "null"
}
fmt.Fprintln(w, "(function() {")
fmt.Fprintf(w,
" browserAuditUI.executionInfoPanel.addInfo(\"Test date & time\", new Date(\"%s\").toString(), \"(<time class=\\\"ago\\\" datetime=\\\"%s\\\"></time>)\", null, null);\n",
timestampISO8601, timestampISO8601)
fmt.Fprintf(w,
" browserAuditUI.executionInfoPanel.addInfo(\"IP address\", \"%s\", %s, %s, \"This was the IP address being used by the web browser at the time the test suite was run. It is not necessarily the same as the IP address being used to view this results page.\");\n",
ip, hostname, country)
fmt.Fprintf(w,
" browserAuditUI.executionInfoPanel.addInfo(\"Browser identifier\", \"<tt class=\\\"useragent\\\">%s</tt>\", null, null, \"This was the <code>User-Agent</code> string reported by the web browser at the time the test suite was run. It is not guaranteed to be accurate, particularly if privacy extensions were installed in the browser.\");\n",
html.EscapeString(userAgent))
fmt.Fprintf(w,
" $.ua.set(he.decode(\"%s\"));\n",
html.EscapeString(userAgent))
fmt.Fprintln(w, " $(document).ready(browserAuditUI.executionInfoPanel.ready);")
// Get the test suite hierarchy for this execution
hierarchy, err := db.Queryx("SELECT * FROM test_suite_execution_hierarchy($1)", id)
if err != nil {
log.Panic(err)
}
// The JavaScript variables f and t will be used to temporarily store
// references to test functions and test UI objects respectively
fmt.Fprintln(w, " var f, t;")
hierRow := SuiteExecutionHierarchyRow{}
// The first row in the resulting table is the "meta-row" defining the
// overall outcome totals for this suite execution, which are used to set
// the values in the scoreboard in the BrowserAudit UI
if hierarchy.Next() {
if err := hierarchy.StructScan(&hierRow); err != nil {
log.Panic(err)
}
fmt.Fprintf(w, " browserAuditUI.scoreboard.setTotal(%d);\n", hierRow.OutcomeTotal.IntSlice[0] + hierRow.OutcomeTotal.IntSlice[1] + hierRow.OutcomeTotal.IntSlice[2] + hierRow.OutcomeTotal.IntSlice[3]);
fmt.Fprintf(w, " browserAuditUI.scoreboard.setOutcome('pass', %d);\n", hierRow.OutcomeTotal.IntSlice[0]);
fmt.Fprintf(w, " browserAuditUI.scoreboard.setOutcome('warning', %d);\n", hierRow.OutcomeTotal.IntSlice[1]);
fmt.Fprintf(w, " browserAuditUI.scoreboard.setOutcome('critical', %d);\n", hierRow.OutcomeTotal.IntSlice[2]);
fmt.Fprintf(w, " browserAuditUI.scoreboard.setOutcome('skip', %d);\n", hierRow.OutcomeTotal.IntSlice[3]);
}
// The remaining rows in the resulting table represent the test suite
// hierarchy for this suite execution: for each row in the hierarchy,
// output JavaScript that reconstructs the BrowserAudit UI
for hierarchy.Next() {
if err := hierarchy.StructScan(&hierRow); err != nil {
log.Panic(err)
}
// timeout and duration will be NULL for all categories, and parent will
// be NULL for top-level categories; in these cases, make them
// JavaScript nulls stored in strings
var timeout, parent, duration string
if hierRow.Timeout.Valid {
timeout = strconv.FormatInt(hierRow.Timeout.Int64, 10)
} else {
timeout = "null"
}
if hierRow.Parent.Valid {
parent = strconv.FormatInt(hierRow.Parent.Int64, 10)
} else {
parent = "null"
}
if hierRow.Duration.Valid {
duration = strconv.FormatInt(hierRow.Duration.Int64, 10)
} else {
duration = "null"
}
if (hierRow.Type == "c") { // row represents a category
// Create a new UI object for this category
fmt.Fprintf(w,
" var cat%d = browserAuditUI.testReportCategory(%d, \"%s\", \"%s\");\n",
hierRow.Id, hierRow.Id, strings.Replace(hierRow.Title, "\"", "\\\"", -1), strings.Replace(hierRow.Description.String, "\"", "\\\"", -1))
// If this category has a parent (i.e., as long as it isn't a top-
// level category), add the UI object to its parent category UI
// object; if it's a top-level category, add the UI object to the
// test report list UI object
if hierRow.Parent.Valid {
fmt.Fprintf(w,
" cat%s.addChild(cat%d);\n",
parent, hierRow.Id)
} else {
fmt.Fprintf(w,
" browserAuditUI.testReportList.addChild(cat%d);\n",
hierRow.Id)
}
// If this category contains tests whose outcomes were "warning",
// "critical" or "skip", add labels with the corresponding numbers
// of tests with these outcomes to the category UI object
if hierRow.OutcomeTotal.IntSlice[1] > 0 {
fmt.Fprintf(w,
" cat%d.setOutcome('warning', %d);\n",
hierRow.Id, hierRow.OutcomeTotal.IntSlice[1])
}
if hierRow.OutcomeTotal.IntSlice[2] > 0 {
fmt.Fprintf(w,
" cat%d.setOutcome('critical', %d);\n",
hierRow.Id, hierRow.OutcomeTotal.IntSlice[2])
}
if hierRow.OutcomeTotal.IntSlice[3] > 0 {
fmt.Fprintf(w,
" cat%d.setOutcome('skip', %d);\n",
hierRow.Id, hierRow.OutcomeTotal.IntSlice[3])
}
// If this category contains only tests that passed, collapse the
// category UI object
if hierRow.OutcomeTotal.IntSlice[1] == 0 && hierRow.OutcomeTotal.IntSlice[2] == 0 && hierRow.OutcomeTotal.IntSlice[3] == 0 {
fmt.Fprintf(w,
" cat%d.setCollapsed(true);\n",
hierRow.Id)
}
} else { // hierRow.Type == "t": row represents a test
fmt.Fprintf(w,
" f = %s;\n",
hierRow.TestFunction.String)
fmt.Fprintf(w,
" t = browserAuditUI.testReport(%d, \"%s\", \"%s\", %s, f, f.reportData);\n",
hierRow.Id, strings.Replace(hierRow.Title, "\"", "\\\"", -1), hierRow.Behaviour.String, timeout)
fmt.Fprintf(w,
" cat%s.addChild(t);\n",
parent)
fmt.Fprintf(w,
" t.setResult(\"%s\", \"%s\", %s);\n",
hierRow.Outcome.String, html.EscapeString(hierRow.Reason.String), duration)
}
}
if err := hierarchy.Err(); err != nil {
log.Panic(err)
}
fmt.Fprintln(w, " browserAuditUI.notificationBar.hide();")
fmt.Fprintln(w, "})();")
}
func PutSuiteExecutionHandler(w http.ResponseWriter, r *http.Request) {
// Parse the JSON-encoded suite execution data in the request body
decoder := json.NewDecoder(r.Body)
var execution SuiteExecution
err := decoder.Decode(&execution)
if err != nil {
log.Printf("Error deserialising test suite execution JSON string: %s\n", err)
http.Error(w, "Invalid test suite execution JSON object", http.StatusBadRequest)
return
}
// Add the user agent, IP address and BrowserAudit version to the struct
execution.UserAgent = r.UserAgent()
execution.IP = net.ParseIP(r.Header["X-Real-Ip"][0]).String()
commitHash, err := exec.Command("git", "rev-parse", "HEAD").Output()
if err != nil {
log.Panic(err)
}
execution.BrowserAuditVersion = string(commitHash)[:40] // trims newline at end of hash
tx, err := db.Beginx()
if err != nil {
log.Panic(err)
}
// Insert the suite execution summary data into the database; this assigns a
// suite execution ID and passkey for this submission, which we will use and
// return later
err = tx.QueryRowx(
`INSERT INTO suite_execution (user_agent, ip, browseraudit_version) VALUES ($1, $2, $3) RETURNING id, passkey`,
execution.UserAgent,
execution.IP,
execution.BrowserAuditVersion,
).Scan(&execution.Id, &execution.Passkey)
if err != nil {
tx.Rollback()
log.Panic(err)
}
// Insert the settings used for this suite execution into the database
for settingKey, settingValue := range execution.Settings {
_, err = tx.Exec(
`INSERT INTO suite_execution_setting (id, key, value) VALUES ($1, $2, $3)`,
execution.Id,
settingKey,
settingValue,
)
if err, ok := err.(*pq.Error); ok {
tx.Rollback()
// Check whether the key/value pair violated an integrity constraint
var baErr error
if err.Code.Class() == "23" && err.Constraint == "valid_pair" {
baErr = fmt.Errorf("Invalid setting key/value pair: %s => %s", settingKey, settingValue)
}
if baErr != nil {
// If this is an error we've anticipated, return HTTP 400 and
// log the error message
log.Printf("Error recording test results: %s\n", baErr)
http.Error(w, baErr.Error(), http.StatusBadRequest)
return
} else {
// Otherwise, something unanticipated happened: panic
log.Panic(err)
}
}
}
// Insert each of the test results for this suite execution into the
// database
for testId, testResult := range execution.TestResults {
_, err = tx.Exec(
`INSERT INTO suite_execution_test (id, test_id, outcome, reason, duration) VALUES ($1, $2, $3, $4, $5)`,
execution.Id,
testId,
testResult.Outcome,
testResult.Reason,
testResult.Duration,
)
if err, ok := err.(*pq.Error); ok {
//fmt.Printf("Code=%#v DataTypeName=%#v Column=%#v Constraint=%#v\n", err.Code, err.DataTypeName, err.Column, err.Constraint)
tx.Rollback()
// Check whether any fields violated a type or integrity constraint
var baErr error
if err.Code == "22P02" && regexp.MustCompile(".*?integer:").MatchString(err.Message) {
baErr = fmt.Errorf("Invalid test ID: %s", testId)
} else if err.Code.Class() == "23" && err.Constraint == "test_id_fkey" {
baErr = fmt.Errorf("Nonexistent test ID: %s", testId)
} else if err.Code.Class() == "23" && err.Constraint == "test_id_live" {
baErr = fmt.Errorf("Non-live test ID: %s", testId)
} else if err.Code == "22P02" && regexp.MustCompile(".*?test_outcome:").MatchString(err.Message) {
baErr = fmt.Errorf("Error in result for test ID %s: invalid test outcome %s", testId, testResult.Outcome)
} else if err.Code.Class() == "23" && err.Constraint == "duration_nonnegative" {
baErr = fmt.Errorf("Error in result for test ID %s: %d is a negative duration", testId, testResult.Duration)
}
if (baErr != nil) {
// If this is an error we've anticipated, return HTTP 400 and
// log the error message
log.Printf("Error recording test results: %s\n", baErr)
http.Error(w, baErr.Error(), http.StatusBadRequest)
return
} else {
// Otherwise, something unanticipated happened: panic
log.Panic(err)
}
}
}
// Commit insertions into the database
err = tx.Commit()
if err != nil {
log.Panic(err)
}
// Return the suite execution ID and passkey assigned to this submission
fmt.Fprintf(w, "%d %s", execution.Id, execution.Passkey)
}