2014-08-01 15:17:32 +02:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
2014-08-01 15:17:32 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
var should = require("should");
|
|
|
|
var path = require('path');
|
2017-03-06 18:40:09 +01:00
|
|
|
var os = require('os');
|
2014-08-01 15:17:32 +02:00
|
|
|
var fs = require('fs-extra');
|
2015-03-24 18:43:47 +01:00
|
|
|
var sinon = require('sinon');
|
2018-08-20 17:17:24 +02:00
|
|
|
var tailNode = require("nr-test-utils").require("@node-red/nodes/core/storage/28-tail.js");
|
2018-03-02 05:41:16 +01:00
|
|
|
var helper = require("node-red-node-test-helper");
|
2014-08-01 15:17:32 +02:00
|
|
|
|
2015-03-24 18:43:47 +01:00
|
|
|
describe('tail Node', function() {
|
2014-08-22 13:44:40 +02:00
|
|
|
|
2015-03-24 18:43:47 +01:00
|
|
|
var wait = 150;
|
2014-08-07 15:55:34 +02:00
|
|
|
var resourcesDir = path.join(__dirname,"..","..","..","resources");
|
|
|
|
var fileToTail = path.join(resourcesDir,"28-tail-test-file.txt");
|
2014-08-22 13:44:40 +02:00
|
|
|
|
2014-08-07 13:03:03 +02:00
|
|
|
beforeEach(function(done) {
|
2014-08-28 00:29:26 +02:00
|
|
|
fs.writeFileSync(fileToTail, "Tail message line 1\nTail message line 2\n");
|
2014-08-07 13:03:03 +02:00
|
|
|
helper.startServer(done);
|
2014-08-01 15:17:32 +02:00
|
|
|
});
|
2014-08-22 13:44:40 +02:00
|
|
|
|
2014-08-01 15:17:32 +02:00
|
|
|
afterEach(function(done) {
|
2014-08-28 00:29:26 +02:00
|
|
|
helper.unload().then(function() {
|
|
|
|
fs.unlinkSync(fileToTail);
|
|
|
|
helper.stopServer(done);
|
|
|
|
});
|
2014-08-01 15:17:32 +02:00
|
|
|
});
|
|
|
|
|
2017-03-06 18:40:09 +01:00
|
|
|
if (os.type() !== "Windows_NT") {
|
|
|
|
it('should be loaded', function(done) {
|
|
|
|
var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":true, "filename":fileToTail}];
|
|
|
|
helper.load(tailNode, flow, function() {
|
|
|
|
var tailNode1 = helper.getNode("tailNode1");
|
|
|
|
tailNode1.should.have.property('name', 'tailNode');
|
|
|
|
done();
|
|
|
|
});
|
2014-08-01 15:17:32 +02:00
|
|
|
});
|
2014-08-22 13:44:40 +02:00
|
|
|
|
2017-03-06 18:40:09 +01:00
|
|
|
it('should tail a file', function(done) {
|
|
|
|
var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":true, "filename":fileToTail, "wires":[["helperNode1"]]},
|
|
|
|
{id:"helperNode1", type:"helper", wires:[]}];
|
|
|
|
helper.load(tailNode, flow, function() {
|
|
|
|
var tailNode1 = helper.getNode("tailNode1");
|
|
|
|
var helperNode1 = helper.getNode("helperNode1");
|
|
|
|
var inputCounter = 0;
|
|
|
|
helperNode1.on("input", function(msg) {
|
|
|
|
//console.log(msg);
|
|
|
|
msg.should.have.property('topic', fileToTail);
|
|
|
|
msg.payload.should.equal("Tail message line " + (++inputCounter + 2));
|
|
|
|
if (inputCounter === 2) {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
setTimeout( function() {
|
|
|
|
fs.appendFileSync(fileToTail, "Tail message line 3\n");
|
|
|
|
fs.appendFileSync(fileToTail, "Tail message line 4\n");
|
|
|
|
},wait);
|
2014-08-01 15:17:32 +02:00
|
|
|
});
|
|
|
|
});
|
2014-08-22 13:44:40 +02:00
|
|
|
|
2017-03-06 18:40:09 +01:00
|
|
|
it('should work in non-split mode', function(done) {
|
|
|
|
var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":false, "filename":fileToTail, "wires":[["helperNode1"]]},
|
|
|
|
{id:"helperNode1", type:"helper", wires:[]}];
|
|
|
|
helper.load(tailNode, flow, function() {
|
|
|
|
var tailNode1 = helper.getNode("tailNode1");
|
|
|
|
var helperNode1 = helper.getNode("helperNode1");
|
|
|
|
helperNode1.on("input", function(msg) {
|
|
|
|
//console.log(msg);
|
|
|
|
msg.should.have.property('topic', fileToTail);
|
|
|
|
msg.payload.should.equal("Tail message line 5\nTail message line 6\n");
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
setTimeout( function() {
|
|
|
|
fs.appendFileSync(fileToTail, "Tail message line 5\nTail message line 6\n");
|
|
|
|
},wait);
|
2014-08-01 15:17:32 +02:00
|
|
|
});
|
2014-08-22 13:44:40 +02:00
|
|
|
});
|
|
|
|
|
2017-03-06 18:40:09 +01:00
|
|
|
it('should work in binary mode', function(done) {
|
|
|
|
var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "filetype":"binary", "filename":fileToTail, "wires":[["helperNode1"]]},
|
|
|
|
{id:"helperNode1", type:"helper", wires:[]}];
|
|
|
|
helper.load(tailNode, flow, function() {
|
|
|
|
var tailNode1 = helper.getNode("tailNode1");
|
|
|
|
var helperNode1 = helper.getNode("helperNode1");
|
|
|
|
helperNode1.on("input", function(msg) {
|
|
|
|
//console.log(msg);
|
|
|
|
msg.should.have.property('topic', fileToTail);
|
|
|
|
msg.payload.toString().should.equal("Tail message line 7\nTail message line 8\n");
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
setTimeout( function() {
|
|
|
|
fs.appendFileSync(fileToTail, "Tail message line 7\nTail message line 8\n");
|
|
|
|
},wait);
|
2016-02-25 09:52:43 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-06 18:40:09 +01:00
|
|
|
it('should handle a non-existent file', function(done) {
|
|
|
|
fs.writeFileSync(fileToTail, "Tail message line.\n");
|
|
|
|
var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":true, "filename":fileToTail, "wires":[["helperNode1"]]},
|
|
|
|
{id:"helperNode1", type:"helper", wires:[]}];
|
|
|
|
helper.load(tailNode, flow, function() {
|
|
|
|
var tailNode1 = helper.getNode("tailNode1");
|
|
|
|
var helperNode1 = helper.getNode("helperNode1");
|
|
|
|
helperNode1.on("input", function(msg) {
|
|
|
|
msg.should.have.property('topic', fileToTail);
|
|
|
|
msg.payload.should.equal("Tail message line");
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
setTimeout(function() {
|
|
|
|
fs.unlinkSync(fileToTail);
|
|
|
|
},500);
|
|
|
|
setTimeout( function() {
|
|
|
|
fs.writeFile(fileToTail, "Tail message line\n");
|
|
|
|
},1000);
|
2014-08-28 00:29:26 +02:00
|
|
|
});
|
|
|
|
});
|
2017-03-06 18:40:09 +01:00
|
|
|
}
|
2015-03-24 18:43:47 +01:00
|
|
|
|
2018-04-23 13:37:26 +02:00
|
|
|
it('should throw an error if run on Windows', function() {
|
2015-03-24 18:43:47 +01:00
|
|
|
// Stub os platform so we can make it look like windows
|
|
|
|
var os = require('os');
|
2017-03-06 18:40:09 +01:00
|
|
|
var spy = sinon.stub(os, 'platform', function(arg) { return("windows"); });
|
2015-03-24 18:43:47 +01:00
|
|
|
|
|
|
|
/*jshint immed: false */
|
2018-04-23 13:37:26 +02:00
|
|
|
try {
|
|
|
|
(function() { tailNode("1234"); }).should.throw();
|
|
|
|
} catch (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
os.platform.restore();
|
|
|
|
}
|
2015-03-24 18:43:47 +01:00
|
|
|
});
|
|
|
|
|
2014-08-28 01:22:12 +02:00
|
|
|
/*
|
2014-08-27 11:41:25 +02:00
|
|
|
it('tail should handle file truncation', function(done) {
|
2014-08-22 13:44:40 +02:00
|
|
|
var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":true, "filename":fileToTail, "wires":[["helperNode1"]]},
|
|
|
|
{id:"helperNode1", type:"helper", wires:[]}];
|
|
|
|
helper.load(tailNode, flow, function() {
|
|
|
|
var tailNode1 = helper.getNode("tailNode1");
|
|
|
|
var helperNode1 = helper.getNode("helperNode1");
|
|
|
|
var inputCounter = 0;
|
|
|
|
var warned = false;
|
|
|
|
tailNode1.on("log", function(msg) {
|
|
|
|
if (msg.level == "warn") { warned = true; }
|
|
|
|
});
|
|
|
|
helperNode1.on("input", function(msg) {
|
2014-08-28 00:40:17 +02:00
|
|
|
console.log("inputCounter =",inputCounter);
|
|
|
|
console.log(msg);
|
2014-08-22 13:44:40 +02:00
|
|
|
msg.should.have.property('topic', fileToTail);
|
2014-08-28 00:29:26 +02:00
|
|
|
inputCounter++;
|
|
|
|
if (inputCounter === 1) {
|
2016-10-10 14:27:43 +02:00
|
|
|
warned.should.be.false();
|
2014-08-28 00:29:26 +02:00
|
|
|
msg.payload.should.equal("Tail message line append");
|
|
|
|
} else if (inputCounter === 2) {
|
|
|
|
msg.payload.should.equal("Tail message line truncate");
|
|
|
|
} else {
|
|
|
|
msg.payload.should.equal("Tail message line append "+inputCounter);
|
|
|
|
}
|
2015-03-24 18:43:47 +01:00
|
|
|
|
2014-08-28 00:29:26 +02:00
|
|
|
if (inputCounter === 5) {
|
|
|
|
setTimeout(function() {
|
2016-10-10 14:27:43 +02:00
|
|
|
warned.should.be.true();
|
2014-08-28 00:29:26 +02:00
|
|
|
done();
|
|
|
|
},100);
|
|
|
|
}
|
2014-08-22 13:44:40 +02:00
|
|
|
});
|
2014-08-28 00:40:17 +02:00
|
|
|
var actions = [
|
|
|
|
function() { fs.appendFileSync(fileToTail, "Tail message line append\n");},
|
|
|
|
function() { fs.writeFileSync(fileToTail, "Tail message line truncate\n");},
|
|
|
|
function() { fs.appendFileSync(fileToTail, "Tail message line append 3\n");},
|
|
|
|
function() { fs.appendFileSync(fileToTail, "Tail message line append 4\n");},
|
|
|
|
function() { fs.appendFileSync(fileToTail, "Tail message line append 5\n");}
|
|
|
|
];
|
2015-03-24 18:43:47 +01:00
|
|
|
|
2014-08-28 00:40:17 +02:00
|
|
|
function processAction() {
|
|
|
|
var action = actions.shift();
|
|
|
|
action();
|
|
|
|
if (actions.length > 0) {
|
|
|
|
setTimeout(function() {
|
|
|
|
processAction();
|
|
|
|
},250);
|
|
|
|
}
|
|
|
|
}
|
2014-08-22 13:44:40 +02:00
|
|
|
setTimeout( function() {
|
2014-08-28 00:40:17 +02:00
|
|
|
processAction();
|
2015-03-24 18:43:47 +01:00
|
|
|
},wait);
|
2014-08-01 15:17:32 +02:00
|
|
|
});
|
|
|
|
});
|
2014-08-28 01:22:12 +02:00
|
|
|
*/
|
2014-08-22 13:44:40 +02:00
|
|
|
|
2014-08-01 15:17:32 +02:00
|
|
|
});
|