node-bunyan-lite/test/dtrace.test.js

78 lines
2.4 KiB
JavaScript
Raw Normal View History

/*
* Copyright (c) 2012 Trent Mick. All rights reserved.
*
* 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;
// Determine if we can run the dtrace tests.
var dtracePlats = ['sunos', 'darwin', 'freebsd'];
var runDtraceTests = true;
try {
2013-03-29 00:42:32 +00:00
require('dtrace-provider')
} catch (e) {
2013-03-29 00:42:32 +00:00
console.log('# skip dtrace tests: no dtrace-provider module');
runDtraceTests = false;
}
if (!runDtraceTests) {
2013-03-29 00:42:32 +00:00
/* pass through */
} 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;
} 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;
} 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;
}
if (runDtraceTests) {
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);
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();
});
});
} /* end of `if (runDtraceTests)` */