2014-07-22 01:34:01 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
this.EXPORTED_SYMBOLS = [
|
2015-09-04 21:37:32 +03:00
|
|
|
"StructuredLogger",
|
|
|
|
"StructuredFormatter"
|
2014-07-22 01:34:01 +04:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TestLogger: Logger class generating messages compliant with the
|
2015-07-16 17:38:40 +03:00
|
|
|
* structured logging protocol for tests exposed by mozlog
|
2014-07-22 01:34:01 +04:00
|
|
|
*
|
|
|
|
* @param name
|
|
|
|
* The name of the logger to instantiate.
|
|
|
|
* @param dumpFun
|
|
|
|
* An underlying function to be used to log raw messages. This function
|
|
|
|
* will receive the complete serialized json string to log.
|
|
|
|
* @param mutators
|
|
|
|
* An array of functions used to add global context to log messages.
|
|
|
|
* These will each be called with the complete object to log as an
|
|
|
|
* argument.
|
|
|
|
*/
|
2015-09-04 21:37:32 +03:00
|
|
|
this.StructuredLogger = function(name, dumpFun=dump, mutators=[]) {
|
2014-07-22 01:34:01 +04:00
|
|
|
this.name = name;
|
|
|
|
this._dumpFun = dumpFun;
|
|
|
|
this._mutatorFuns = mutators;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-16 17:38:40 +03:00
|
|
|
* Log functions producing messages in the format specified by mozlog
|
2014-07-22 01:34:01 +04:00
|
|
|
*/
|
2015-09-04 21:37:32 +03:00
|
|
|
StructuredLogger.prototype = {
|
|
|
|
testStart: function (test) {
|
2016-06-06 18:28:22 +03:00
|
|
|
var data = {test: this._testId(test)};
|
2015-09-04 21:37:32 +03:00
|
|
|
this._logData("test_start", data);
|
|
|
|
},
|
|
|
|
|
|
|
|
testStatus: function (test, subtest, status, expected="PASS",
|
|
|
|
message=null, stack=null, extra=null) {
|
|
|
|
|
|
|
|
if (subtest === null || subtest === undefined) {
|
|
|
|
// Fix for assertions that don't pass in a name
|
|
|
|
subtest = "undefined assertion name";
|
|
|
|
}
|
2014-07-22 01:34:01 +04:00
|
|
|
|
2015-09-04 21:37:32 +03:00
|
|
|
var data = {
|
2016-06-06 18:28:22 +03:00
|
|
|
test: this._testId(test),
|
2015-09-04 21:37:32 +03:00
|
|
|
subtest: subtest,
|
|
|
|
status: status,
|
|
|
|
};
|
2014-07-22 01:34:01 +04:00
|
|
|
|
2015-09-04 21:37:32 +03:00
|
|
|
if (expected != status && status != "SKIP") {
|
|
|
|
data.expected = expected;
|
|
|
|
}
|
|
|
|
if (message !== null) {
|
|
|
|
data.message = String(message);
|
|
|
|
}
|
|
|
|
if (stack !== null) {
|
|
|
|
data.stack = stack;
|
|
|
|
}
|
|
|
|
if (extra !== null) {
|
|
|
|
data.extra = extra;
|
|
|
|
}
|
2014-07-22 01:34:01 +04:00
|
|
|
|
2015-09-04 21:37:32 +03:00
|
|
|
this._logData("test_status", data);
|
|
|
|
},
|
2014-07-22 01:34:01 +04:00
|
|
|
|
2015-09-04 21:37:32 +03:00
|
|
|
testEnd: function (test, status, expected="OK", message=null, stack=null, extra=null) {
|
2016-06-06 18:28:22 +03:00
|
|
|
var data = {test: this._testId(test), status: status};
|
2014-07-22 01:34:01 +04:00
|
|
|
|
2015-09-04 21:37:32 +03:00
|
|
|
if (expected != status && status != "SKIP") {
|
|
|
|
data.expected = expected;
|
|
|
|
}
|
|
|
|
if (message !== null) {
|
|
|
|
data.message = String(message);
|
|
|
|
}
|
|
|
|
if (stack !== null) {
|
|
|
|
data.stack = stack;
|
|
|
|
}
|
|
|
|
if (extra !== null) {
|
|
|
|
data.extra = extra;
|
|
|
|
}
|
2014-07-22 01:34:01 +04:00
|
|
|
|
2015-09-04 21:37:32 +03:00
|
|
|
this._logData("test_end", data);
|
|
|
|
},
|
2014-07-22 01:34:01 +04:00
|
|
|
|
2016-11-30 20:26:16 +03:00
|
|
|
assertionCount: function (test, count, minExpected=0, maxExpected=0) {
|
|
|
|
var data = {test: test,
|
|
|
|
min_expected: minExpected,
|
|
|
|
max_expected: maxExpected,
|
|
|
|
count: count};
|
|
|
|
|
|
|
|
this._logData("assertion_count", data);
|
|
|
|
},
|
|
|
|
|
2016-02-05 23:44:20 +03:00
|
|
|
suiteStart: function (tests, runinfo=null, versioninfo=null, deviceinfo=null, extra=null) {
|
2016-06-06 18:28:22 +03:00
|
|
|
var data = {tests: tests.map(x => this._testId(x))};
|
2015-09-04 21:37:32 +03:00
|
|
|
if (runinfo !== null) {
|
|
|
|
data.runinfo = runinfo;
|
|
|
|
}
|
2014-07-22 01:34:01 +04:00
|
|
|
|
2016-02-05 23:44:20 +03:00
|
|
|
if (versioninfo !== null) {
|
|
|
|
data.versioninfo = versioninfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (deviceinfo !== null) {
|
|
|
|
data.deviceinfo = deviceinfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (extra !== null) {
|
|
|
|
data.extra = extra;
|
|
|
|
}
|
|
|
|
|
2015-09-04 21:37:32 +03:00
|
|
|
this._logData("suite_start", data);
|
|
|
|
},
|
|
|
|
|
2016-02-05 23:44:20 +03:00
|
|
|
suiteEnd: function (extra=null) {
|
|
|
|
var data = {};
|
|
|
|
|
|
|
|
if (extra !== null) {
|
|
|
|
data.extra = extra;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._logData("suite_end", data);
|
2015-09-04 21:37:32 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unstructured logging functions. The "extra" parameter can always by used to
|
|
|
|
* log suite specific data. If a "stack" field is provided it is logged at the
|
|
|
|
* top level of the data object for the benefit of mozlog's formatters.
|
|
|
|
*/
|
|
|
|
log: function (level, message, extra=null) {
|
|
|
|
var data = {
|
|
|
|
level: level,
|
|
|
|
message: String(message),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (extra !== null) {
|
|
|
|
data.extra = extra;
|
|
|
|
if ("stack" in extra) {
|
|
|
|
data.stack = extra.stack;
|
|
|
|
}
|
|
|
|
}
|
2014-07-22 01:34:01 +04:00
|
|
|
|
2015-09-04 21:37:32 +03:00
|
|
|
this._logData("log", data);
|
|
|
|
},
|
2014-07-22 01:34:01 +04:00
|
|
|
|
2015-09-04 21:37:32 +03:00
|
|
|
debug: function (message, extra=null) {
|
|
|
|
this.log("DEBUG", message, extra);
|
|
|
|
},
|
2014-07-22 01:34:01 +04:00
|
|
|
|
2015-09-04 21:37:32 +03:00
|
|
|
info: function (message, extra=null) {
|
|
|
|
this.log("INFO", message, extra);
|
|
|
|
},
|
2014-07-22 01:34:01 +04:00
|
|
|
|
2015-09-04 21:37:32 +03:00
|
|
|
warning: function (message, extra=null) {
|
|
|
|
this.log("WARNING", message, extra);
|
|
|
|
},
|
2014-07-22 01:34:01 +04:00
|
|
|
|
2015-09-04 21:37:32 +03:00
|
|
|
error: function (message, extra=null) {
|
|
|
|
this.log("ERROR", message, extra);
|
|
|
|
},
|
2014-07-22 01:34:01 +04:00
|
|
|
|
2015-09-04 21:37:32 +03:00
|
|
|
critical: function (message, extra=null) {
|
|
|
|
this.log("CRITICAL", message, extra);
|
|
|
|
},
|
2014-07-22 01:34:01 +04:00
|
|
|
|
2016-02-05 23:44:20 +03:00
|
|
|
processOutput: function(thread, message) {
|
|
|
|
this._logData('process_output', {
|
|
|
|
message: message,
|
|
|
|
thread: thread,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
|
2015-09-04 21:37:32 +03:00
|
|
|
_logData: function (action, data={}) {
|
|
|
|
var allData = {
|
|
|
|
action: action,
|
|
|
|
time: Date.now(),
|
|
|
|
thread: null,
|
|
|
|
pid: null,
|
|
|
|
source: this.name
|
|
|
|
};
|
2014-07-22 01:34:01 +04:00
|
|
|
|
2015-09-04 21:37:32 +03:00
|
|
|
for (var field in data) {
|
|
|
|
allData[field] = data[field];
|
|
|
|
}
|
2014-07-22 01:34:01 +04:00
|
|
|
|
2015-09-04 21:37:32 +03:00
|
|
|
for (var fun of this._mutatorFuns) {
|
|
|
|
fun(allData);
|
|
|
|
}
|
2014-07-22 01:34:01 +04:00
|
|
|
|
2015-09-04 21:37:32 +03:00
|
|
|
this._dumpFun(allData);
|
2016-06-06 18:28:22 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_testId: function(test) {
|
|
|
|
if (Array.isArray(test)) {
|
|
|
|
return test.join(" ");
|
|
|
|
}
|
|
|
|
return test;
|
|
|
|
},
|
2015-09-04 21:37:32 +03:00
|
|
|
};
|
2014-07-22 01:34:01 +04:00
|
|
|
|
|
|
|
|
2015-09-04 21:37:32 +03:00
|
|
|
/**
|
|
|
|
* StructuredFormatter: Formatter class turning structured messages
|
|
|
|
* into human-readable messages.
|
|
|
|
*/
|
|
|
|
this.StructuredFormatter = function() {
|
|
|
|
this.testStartTimes = {};
|
|
|
|
};
|
|
|
|
|
|
|
|
StructuredFormatter.prototype = {
|
|
|
|
|
|
|
|
log: function(message) {
|
|
|
|
return message.message;
|
|
|
|
},
|
|
|
|
|
|
|
|
suite_start: function(message) {
|
|
|
|
this.suiteStartTime = message.time;
|
|
|
|
return "SUITE-START | Running " + message.tests.length + " tests";
|
|
|
|
},
|
|
|
|
|
|
|
|
test_start: function(message) {
|
|
|
|
this.testStartTimes[message.test] = new Date().getTime();
|
|
|
|
return "TEST-START | " + message.test;
|
|
|
|
},
|
|
|
|
|
|
|
|
test_status: function(message) {
|
|
|
|
var statusInfo = message.test + " | " + message.subtest +
|
|
|
|
(message.message ? " | " + message.message : "");
|
|
|
|
if (message.expected) {
|
|
|
|
return "TEST-UNEXPECTED-" + message.status + " | " + statusInfo +
|
|
|
|
" - expected: " + message.expected;
|
|
|
|
} else {
|
|
|
|
return "TEST-" + message.status + " | " + statusInfo;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
test_end: function(message) {
|
|
|
|
var startTime = this.testStartTimes[message.test];
|
|
|
|
delete this.testStartTimes[message.test];
|
|
|
|
var statusInfo = message.test + (message.message ? " | " + String(message.message) : "");
|
|
|
|
var result;
|
|
|
|
if (message.expected) {
|
|
|
|
result = "TEST-UNEXPECTED-" + message.status + " | " + statusInfo +
|
|
|
|
" - expected: " + message.expected;
|
|
|
|
} else {
|
|
|
|
return "TEST-" + message.status + " | " + statusInfo;
|
|
|
|
}
|
|
|
|
result = " | took " + message.time - startTime + "ms";
|
|
|
|
},
|
|
|
|
|
|
|
|
suite_end: function(message) {
|
|
|
|
return "SUITE-END | took " + message.time - this.suiteStartTime + "ms";
|
|
|
|
}
|
2014-07-22 01:34:01 +04:00
|
|
|
};
|
|
|
|
|