Files
cloudron-box/test.js
Girish Ramakrishnan a0d9f7fe75 Update packages
2022-01-13 16:25:43 -08:00

90 lines
3.0 KiB
JavaScript

#!/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'),
grepit = require('grepit'),
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 found = grepit(pattern, path.join(LOG_FOLDER, fileName));
if (found.length === 0) return callback('not found');
callback();
}, 250);
}
describe('Daemon', function () {
this.timeout(5000);
after(function (done) {
fs.rmSync(LOG_FOLDER, { recursive: true, force: true });
server.stop(done);
});
it('can start', function (done) {
server.start({ port: PORT, logFolder: LOG_FOLDER }, function (error) {
expect(error).to.not.be.ok();
done();
});
});
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);
});
});
});