syslog: restructure code
This commit is contained in:
86
syslog/syslog-test.js
Normal file
86
syslog/syslog-test.js
Normal file
@@ -0,0 +1,86 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/* global it:false */
|
||||
/* global describe:false */
|
||||
/* global after:false */
|
||||
|
||||
'use strict';
|
||||
|
||||
const dgram = require('dgram'),
|
||||
expect = require('expect.js'),
|
||||
fs = require('fs'),
|
||||
path = require('path'),
|
||||
os = require('os'),
|
||||
server = require('./server.js');
|
||||
|
||||
const PORT = 5678;
|
||||
const LOG_FOLDER = path.join(os.tmpdir(), '/cloudron-syslog-test/');
|
||||
|
||||
function sendMessage(message, callback) {
|
||||
const client = dgram.createSocket('udp4');
|
||||
|
||||
client.send(message, PORT, 'localhost', function (error) {
|
||||
if (error) return callback(error);
|
||||
|
||||
client.close();
|
||||
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
function verifyMessage(pattern, fileName, callback) {
|
||||
// give the server some time to write to disk
|
||||
setTimeout(function () {
|
||||
const data = fs.readFileSync(path.join(LOG_FOLDER, fileName), { encoding: 'utf8' });
|
||||
const found = data.match(new RegExp(pattern));
|
||||
return callback(found === null ? new Error('not found') : null);
|
||||
}, 250);
|
||||
}
|
||||
|
||||
describe('Daemon', function () {
|
||||
this.timeout(5000);
|
||||
|
||||
after(async function () {
|
||||
fs.rmSync(LOG_FOLDER, { recursive: true, force: true });
|
||||
await server.stop();
|
||||
});
|
||||
|
||||
it('can start', async function () {
|
||||
await server.start({ port: PORT, logFolder: LOG_FOLDER });
|
||||
});
|
||||
|
||||
it('handle good message', function (done) {
|
||||
// IETF (RFC 5424) message, with structured data and chained hostnames
|
||||
const ietfLine = '<110>1 2009-05-03T14:00:39.529966+02:00 host.example.org/relay.example.org testapp 2138 - [exampleSDID@32473 iut="3" eventSource="Application" eventID="1011"][exampleSDID@32474 iut="4" eventSource="Application" eventID="1012"][ssign VER="0111" RSID="1" SG="0" SPRI="0" GBC="2" FMN="1" CNT="7" HB="K6wzcombEvKJ+UTMcn9bPryAeaU= zrkDcIeaDluypaPCY8WWzwHpPok= zgrWOdpx16ADc7UmckyIFY53icE= XfopJ+S8/hODapiBBCgVQaLqBKg= J67gKMFl/OauTC20ibbydwIlJC8= M5GziVgB6KPY3ERU1HXdSi2vtdw= Wxd/lU7uG/ipEYT9xeqnsfohyH0=" SIGN="AKBbX4J7QkrwuwdbV7Taujk2lvOf8gCgC62We1QYfnrNHz7FzAvdySuMyfM="] BOMAn application event log entry';
|
||||
|
||||
sendMessage(ietfLine, function (error) {
|
||||
expect(error).to.not.be.ok();
|
||||
|
||||
verifyMessage(/An application event log entry/, 'testapp/app.log', done);
|
||||
});
|
||||
});
|
||||
|
||||
it('ignores invalid message', function (done) {
|
||||
const invalidLine = 'foobar';
|
||||
|
||||
sendMessage(invalidLine, function (error) {
|
||||
expect(error).to.not.be.ok();
|
||||
|
||||
verifyMessage(/foobar/, 'testapp/app.log', function (error) {
|
||||
expect(error).to.be.ok();
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('can handle message with :', function (done) {
|
||||
// this is what we see from docker syslog
|
||||
const message = '<30>1 2018-06-24T22:22:53Z my.test.com testapp 26599 testapp - This: contains two : colons';
|
||||
|
||||
sendMessage(message, function (error) {
|
||||
expect(error).to.not.be.ok();
|
||||
|
||||
verifyMessage(/This: contains two : colons/, 'testapp/app.log', done);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user