2012-11-01 06:52:53 +00:00
|
|
|
/*
|
2016-03-17 05:32:52 +00:00
|
|
|
* Copyright 2016 Trent Mick
|
2012-11-01 06:52:53 +00:00
|
|
|
*
|
|
|
|
* If available, test dtrace support.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var spawn = require('child_process').spawn;
|
|
|
|
var format = require('util').format;
|
|
|
|
|
|
|
|
var bunyan = require('../lib/bunyan');
|
|
|
|
|
2013-01-07 19:18:12 +00:00
|
|
|
// node-tap API
|
|
|
|
if (require.cache[__dirname + '/tap4nodeunit.js'])
|
2013-03-29 00:42:32 +00:00
|
|
|
delete require.cache[__dirname + '/tap4nodeunit.js'];
|
2013-01-07 19:18:12 +00:00
|
|
|
var tap4nodeunit = require('./tap4nodeunit.js');
|
|
|
|
var after = tap4nodeunit.after;
|
|
|
|
var before = tap4nodeunit.before;
|
|
|
|
var test = tap4nodeunit.test;
|
|
|
|
|
2012-11-01 06:52:53 +00:00
|
|
|
|
|
|
|
// Determine if we can run the dtrace tests.
|
|
|
|
var dtracePlats = ['sunos', 'darwin', 'freebsd'];
|
|
|
|
var runDtraceTests = true;
|
|
|
|
try {
|
2016-03-17 05:32:52 +00:00
|
|
|
require('dtrace-provider');
|
2012-11-01 06:52:53 +00:00
|
|
|
} catch (e) {
|
2013-03-29 00:42:32 +00:00
|
|
|
console.log('# skip dtrace tests: no dtrace-provider module');
|
|
|
|
runDtraceTests = false;
|
2012-11-01 06:52:53 +00:00
|
|
|
}
|
|
|
|
if (!runDtraceTests) {
|
2013-03-29 00:42:32 +00:00
|
|
|
/* pass through */
|
2012-11-01 06:52:53 +00:00
|
|
|
} else if (dtracePlats.indexOf(process.platform) === -1) {
|
2013-03-29 00:42:32 +00:00
|
|
|
console.log('# skip dtrace tests: not on a platform with dtrace');
|
|
|
|
runDtraceTests = false;
|
2012-11-01 06:52:53 +00:00
|
|
|
} else if (process.env.SKIP_DTRACE) {
|
2013-03-29 00:42:32 +00:00
|
|
|
console.log('# skip dtrace tests: SKIP_DTRACE envvar set');
|
|
|
|
runDtraceTests = false;
|
2012-11-01 06:52:53 +00:00
|
|
|
} else if (process.getgid() !== 0) {
|
2013-03-29 00:42:32 +00:00
|
|
|
console.log('# skip dtrace tests: gid is not 0, run with `sudo`');
|
|
|
|
runDtraceTests = false;
|
2012-11-01 06:52:53 +00:00
|
|
|
}
|
|
|
|
if (runDtraceTests) {
|
|
|
|
|
|
|
|
|
2012-11-01 20:05:56 +00:00
|
|
|
test('basic dtrace', function (t) {
|
2013-03-29 00:42:32 +00:00
|
|
|
var argv = ['dtrace', '-Z', '-x', 'strsize=4k', '-qn',
|
|
|
|
'bunyan$target:::log-*{printf("%s", copyinstr(arg0))}',
|
|
|
|
'-c', format('node %s/log-some.js', __dirname)];
|
|
|
|
var dtrace = spawn(argv[0], argv.slice(1));
|
|
|
|
//console.error('ARGV: %j', argv);
|
2016-01-29 06:47:30 +00:00
|
|
|
//console.error('CMD: %s', argv.join(' '));
|
2012-11-01 20:05:56 +00:00
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
var traces = [];
|
|
|
|
dtrace.stdout.on('data', function (data) {
|
|
|
|
//console.error('DTRACE STDOUT:', data.toString());
|
|
|
|
traces.push(data.toString());
|
|
|
|
});
|
|
|
|
dtrace.stderr.on('data', function (data) {
|
|
|
|
console.error('DTRACE STDERR:', data.toString());
|
|
|
|
});
|
|
|
|
dtrace.on('exit', function (code) {
|
|
|
|
t.notOk(code, 'dtrace exited cleanly');
|
|
|
|
traces = traces.join('').split('\n')
|
|
|
|
.filter(function (t) { return t.trim().length })
|
|
|
|
.map(function (t) { return JSON.parse(t) });
|
|
|
|
t.equal(traces.length, 2, 'got 2 log records');
|
|
|
|
if (traces.length) {
|
|
|
|
t.equal(traces[0].level, bunyan.DEBUG);
|
|
|
|
t.equal(traces[0].foo, 'bar');
|
|
|
|
t.equal(traces[1].level, bunyan.TRACE);
|
|
|
|
t.equal(traces[1].msg, 'hi at trace');
|
|
|
|
}
|
|
|
|
t.end();
|
|
|
|
});
|
2012-11-01 06:52:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2016-03-17 05:32:52 +00:00
|
|
|
/*
|
|
|
|
* Run a logger that logs a couple records every second.
|
|
|
|
* Then run `bunyan -p PID` to capture.
|
|
|
|
* Let those run for a few seconds to ensure dtrace has time to attach and
|
|
|
|
* capture something.
|
|
|
|
*/
|
|
|
|
test('bunyan -p', function (t) {
|
|
|
|
var p = spawn('node', [__dirname + '/log-some-loop.js']);
|
|
|
|
|
|
|
|
var bunyanP = spawn('node',
|
|
|
|
[__dirname + '/../bin/bunyan', '-p', String(p.pid), '-0']);
|
|
|
|
var traces = [];
|
|
|
|
bunyanP.stdout.on('data', function (data) {
|
|
|
|
//console.error('BUNYAN -P STDOUT:', data.toString());
|
|
|
|
traces.push(data.toString());
|
|
|
|
});
|
|
|
|
bunyanP.stderr.on('data', function (data) {
|
|
|
|
console.error('BUNYAN -P STDERR:', data.toString());
|
|
|
|
});
|
|
|
|
bunyanP.on('exit', function (code) {
|
|
|
|
traces = traces.join('').split('\n')
|
|
|
|
.filter(function (t) { return t.trim().length })
|
|
|
|
.map(function (t) { return JSON.parse(t) });
|
|
|
|
t.ok(traces.length >= 3, 'got >=3 log records: ' + traces.length);
|
|
|
|
if (traces.length >= 3) {
|
|
|
|
if (traces[0].level !== bunyan.DEBUG) {
|
|
|
|
traces.shift();
|
|
|
|
}
|
|
|
|
t.equal(traces[0].level, bunyan.DEBUG);
|
|
|
|
t.equal(traces[0].foo, 'bar');
|
|
|
|
t.equal(traces[1].level, bunyan.TRACE);
|
|
|
|
t.equal(traces[1].msg, 'hi at trace');
|
|
|
|
}
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Give it a few seconds to ensure we get some traces.
|
|
|
|
setTimeout(function () {
|
|
|
|
p.kill();
|
|
|
|
bunyanP.kill();
|
2016-10-06 22:07:44 +00:00
|
|
|
}, 5000);
|
2016-03-17 05:32:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-11-01 06:52:53 +00:00
|
|
|
} /* end of `if (runDtraceTests)` */
|