2015-06-22 09:26:38 +02:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
2015-06-22 09:26:38 +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 sinon = require("sinon");
|
2018-03-02 05:41:16 +01:00
|
|
|
var helper = require("node-red-node-test-helper");
|
2019-07-10 00:32:22 +02:00
|
|
|
var execNode = require("nr-test-utils").require("@node-red/nodes/core/function/90-exec.js");
|
2017-04-10 17:06:19 +02:00
|
|
|
var osType = require("os").type();
|
2015-06-22 09:26:38 +02:00
|
|
|
|
|
|
|
var child_process = require('child_process');
|
|
|
|
|
|
|
|
describe('exec node', function() {
|
|
|
|
|
2015-07-06 00:02:10 +02:00
|
|
|
beforeEach(function(done) {
|
2015-06-22 09:26:38 +02:00
|
|
|
helper.startServer(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function(done) {
|
|
|
|
helper.unload().then(function() {
|
|
|
|
helper.stopServer(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be loaded with any defaults', function(done) {
|
|
|
|
var flow = [{id:"n1", type:"exec", name: "exec1"}];
|
|
|
|
helper.load(execNode, flow, function() {
|
|
|
|
var n1 = helper.getNode("n1");
|
|
|
|
n1.should.have.property("name", "exec1");
|
|
|
|
n1.should.have.property("cmd", "");
|
|
|
|
n1.should.have.property("append", "");
|
2021-04-01 00:50:00 +02:00
|
|
|
n1.should.have.property("addpay","payload");
|
2016-04-27 23:32:58 +02:00
|
|
|
n1.should.have.property("timer",0);
|
2017-04-13 12:36:54 +02:00
|
|
|
n1.should.have.property("oldrc","false");
|
2021-06-16 23:38:34 +02:00
|
|
|
n1.should.have.property("execOpt", {encoding:'binary', maxBuffer:10000000, windowsHide: false});
|
|
|
|
n1.should.have.property("spawnOpt", {windowsHide:false});
|
2015-06-22 09:26:38 +02:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-18 13:40:08 +02:00
|
|
|
describe('calling exec', function() {
|
|
|
|
|
|
|
|
it('should exec a simple command', function(done) {
|
2017-04-13 12:36:54 +02:00
|
|
|
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo", addpay:false, append:"", oldrc:"false"},
|
2016-04-18 13:40:08 +02:00
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
2021-04-09 12:22:57 +02:00
|
|
|
var spy = sinon.stub(child_process, 'exec').callsFake(
|
2017-03-29 22:44:58 +02:00
|
|
|
function(arg1, arg2, arg3, arg4) {
|
2017-03-02 14:34:01 +01:00
|
|
|
// arg3(error,stdout,stderr);
|
|
|
|
arg3(null,arg1,arg1.toUpperCase());
|
|
|
|
});
|
2016-04-18 13:40:08 +02:00
|
|
|
|
2017-02-07 21:13:05 +01:00
|
|
|
helper.load(execNode, flow, function() {
|
|
|
|
var n1 = helper.getNode("n1");
|
|
|
|
var n2 = helper.getNode("n2");
|
|
|
|
var n3 = helper.getNode("n3");
|
|
|
|
var n4 = helper.getNode("n4");
|
|
|
|
var received = 0;
|
2017-03-02 14:34:01 +01:00
|
|
|
var messages = [null,null,null];
|
2017-02-07 21:13:05 +01:00
|
|
|
var completeTest = function() {
|
2017-04-13 12:36:54 +02:00
|
|
|
received = received + 1;
|
2017-03-02 14:34:01 +01:00
|
|
|
if (received < 3) {
|
2017-02-07 21:13:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-03-29 22:44:58 +02:00
|
|
|
try {
|
2017-02-07 21:13:05 +01:00
|
|
|
var msg = messages[0];
|
|
|
|
msg.should.have.property("payload");
|
|
|
|
msg.payload.should.be.a.String();
|
|
|
|
msg.payload.should.equal("echo");
|
2017-11-02 17:55:44 +01:00
|
|
|
msg.should.have.property("rc");
|
|
|
|
msg.rc.should.have.property("code",0);
|
2016-10-11 15:49:48 +02:00
|
|
|
|
2017-02-07 21:13:05 +01:00
|
|
|
msg = messages[1];
|
|
|
|
msg.should.have.property("payload");
|
2017-04-13 12:36:54 +02:00
|
|
|
msg.payload.should.be.a.String();
|
2017-02-07 21:13:05 +01:00
|
|
|
msg.payload.should.equal("ECHO");
|
2017-11-02 17:55:44 +01:00
|
|
|
msg.should.have.property("rc");
|
|
|
|
msg.rc.should.have.property("code",0);
|
2017-03-02 14:34:01 +01:00
|
|
|
|
|
|
|
msg = messages[2];
|
|
|
|
msg.should.have.property("payload");
|
|
|
|
msg.payload.should.have.property("code",0);
|
|
|
|
|
2017-02-07 21:13:05 +01:00
|
|
|
child_process.exec.restore();
|
|
|
|
done();
|
2017-11-02 17:55:44 +01:00
|
|
|
}
|
|
|
|
catch(err) {
|
2017-02-07 21:13:05 +01:00
|
|
|
child_process.exec.restore();
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
n2.on("input", function(msg) {
|
|
|
|
messages[0] = msg;
|
|
|
|
completeTest();
|
|
|
|
});
|
|
|
|
n3.on("input", function(msg) {
|
|
|
|
messages[1] = msg;
|
|
|
|
completeTest();
|
2016-04-18 13:40:08 +02:00
|
|
|
});
|
2017-03-02 14:34:01 +01:00
|
|
|
n4.on("input", function(msg) {
|
|
|
|
messages[2] = msg;
|
|
|
|
completeTest();
|
2016-04-18 13:40:08 +02:00
|
|
|
});
|
|
|
|
n1.receive({payload:"and"});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-03-22 08:19:55 +01:00
|
|
|
it('should exec a simple command with appended value from message', function (done) {
|
2021-04-01 00:50:00 +02:00
|
|
|
var flow = [{id:"n1", type:"exec", wires:[["n2"]], command:"echo", addpay:"topic", append:"more", oldrc:"false"},
|
2021-03-22 08:19:55 +01:00
|
|
|
{id:"n2", type:"helper"}];
|
|
|
|
helper.load(execNode, flow, function () {
|
|
|
|
var n1 = helper.getNode("n1");
|
|
|
|
var n2 = helper.getNode("n2");
|
|
|
|
n2.on("input", function (msg) {
|
2021-04-01 00:50:00 +02:00
|
|
|
try {
|
2021-03-22 08:19:55 +01:00
|
|
|
msg.should.have.property("payload");
|
|
|
|
msg.payload.should.be.a.String();
|
|
|
|
msg.payload.should.equal("bar more\n");
|
|
|
|
done();
|
2021-04-01 00:50:00 +02:00
|
|
|
} catch(err) {
|
|
|
|
done(err)
|
|
|
|
}
|
2021-03-22 08:19:55 +01:00
|
|
|
});
|
|
|
|
n1.receive({payload:"foo", topic:"bar"});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-18 13:40:08 +02:00
|
|
|
it('should exec a simple command with extra parameters', function(done) {
|
2021-04-01 00:50:00 +02:00
|
|
|
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo", addpay:"payload", append:"more", oldrc:"false"},
|
2016-04-18 13:40:08 +02:00
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
2021-04-09 12:22:57 +02:00
|
|
|
var spy = sinon.stub(child_process, 'exec').callsFake(
|
2017-03-29 22:44:58 +02:00
|
|
|
function(arg1, arg2, arg3, arg4) {
|
2016-04-18 13:40:08 +02:00
|
|
|
//console.log(arg1);
|
|
|
|
// arg3(error,stdout,stderr);
|
|
|
|
arg3(null,arg1,arg1.toUpperCase());
|
|
|
|
});
|
|
|
|
|
|
|
|
helper.load(execNode, flow, function() {
|
|
|
|
var n1 = helper.getNode("n1");
|
|
|
|
var n2 = helper.getNode("n2");
|
|
|
|
var n3 = helper.getNode("n3");
|
|
|
|
var n4 = helper.getNode("n4");
|
2016-10-11 15:49:48 +02:00
|
|
|
var received = 0;
|
|
|
|
var messages = [null,null];
|
|
|
|
var completeTest = function() {
|
|
|
|
received++;
|
|
|
|
if (received < 2) {
|
|
|
|
return;
|
|
|
|
}
|
2017-02-07 21:13:05 +01:00
|
|
|
try {
|
2016-10-11 15:49:48 +02:00
|
|
|
var msg = messages[0];
|
|
|
|
msg.should.have.property("payload");
|
|
|
|
msg.payload.should.be.a.String();
|
|
|
|
msg.payload.should.equal("echo and more");
|
2017-11-02 17:55:44 +01:00
|
|
|
msg.should.have.property("rc");
|
|
|
|
msg.rc.should.have.property("code",0);
|
2016-10-11 15:49:48 +02:00
|
|
|
|
|
|
|
msg = messages[1];
|
|
|
|
msg.should.have.property("payload");
|
|
|
|
msg.payload.should.be.a.String();
|
|
|
|
msg.payload.should.equal("ECHO AND MORE");
|
2017-11-02 17:55:44 +01:00
|
|
|
msg.should.have.property("rc");
|
|
|
|
msg.rc.should.have.property("code",0);
|
2016-10-11 15:49:48 +02:00
|
|
|
child_process.exec.restore();
|
|
|
|
done();
|
2017-11-02 17:55:44 +01:00
|
|
|
}
|
|
|
|
catch(err) {
|
2016-10-11 15:49:48 +02:00
|
|
|
child_process.exec.restore();
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
};
|
2016-04-18 13:40:08 +02:00
|
|
|
n2.on("input", function(msg) {
|
2016-10-11 15:49:48 +02:00
|
|
|
messages[0] = msg;
|
|
|
|
completeTest();
|
2016-04-18 13:40:08 +02:00
|
|
|
});
|
|
|
|
n3.on("input", function(msg) {
|
2016-10-11 15:49:48 +02:00
|
|
|
messages[1] = msg;
|
|
|
|
completeTest();
|
2016-04-18 13:40:08 +02:00
|
|
|
});
|
|
|
|
n1.receive({payload:"and"});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to return a binary buffer', function(done) {
|
2017-04-13 12:36:54 +02:00
|
|
|
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo", addpay:true, append:"more", oldrc:"false"},
|
2016-04-18 13:40:08 +02:00
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
2021-04-09 12:22:57 +02:00
|
|
|
var spy = sinon.stub(child_process, 'exec').callsFake(
|
2017-03-29 22:44:58 +02:00
|
|
|
function(arg1, arg2, arg3, arg4) {
|
2016-04-18 13:40:08 +02:00
|
|
|
//console.log(arg1);
|
|
|
|
// arg3(error,stdout,stderr);
|
2018-10-24 14:45:34 +02:00
|
|
|
arg3("error",Buffer.from([0x01,0x02,0x03,0x88]),Buffer.from([0x01,0x02,0x03,0x88]));
|
2016-04-18 13:40:08 +02:00
|
|
|
});
|
|
|
|
helper.load(execNode, flow, function() {
|
|
|
|
var n1 = helper.getNode("n1");
|
|
|
|
var n2 = helper.getNode("n2");
|
|
|
|
var n3 = helper.getNode("n3");
|
|
|
|
var n4 = helper.getNode("n4");
|
|
|
|
n2.on("input", function(msg) {
|
2016-04-23 18:09:46 +02:00
|
|
|
//console.log("n2",msg);
|
2016-10-10 14:27:43 +02:00
|
|
|
try {
|
|
|
|
msg.should.have.property("payload");
|
|
|
|
Buffer.isBuffer(msg.payload).should.be.true();
|
|
|
|
msg.payload.length.should.equal(4);
|
|
|
|
child_process.exec.restore();
|
|
|
|
done();
|
|
|
|
} catch(err) {
|
2016-10-11 14:25:19 +02:00
|
|
|
child_process.exec.restore();
|
2016-10-10 14:27:43 +02:00
|
|
|
done(err);
|
|
|
|
}
|
2016-04-18 13:40:08 +02:00
|
|
|
});
|
2016-04-23 18:09:46 +02:00
|
|
|
n1.receive({});
|
2016-04-18 13:40:08 +02:00
|
|
|
});
|
|
|
|
});
|
2016-04-27 23:32:58 +02:00
|
|
|
|
|
|
|
it('should be able to timeout a long running command', function(done) {
|
2017-04-10 17:06:19 +02:00
|
|
|
var flow;
|
|
|
|
if (osType === "Windows_NT") {
|
|
|
|
// Although Windows timeout command is equivalent to sleep, this cannot be used because it promptly outputs a message.
|
2017-04-13 12:36:54 +02:00
|
|
|
flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"ping", addpay:false, append:"192.0.2.0 -n 1 -w 1000 > NUL", timer:"0.3", oldrc:"false"},
|
2017-04-10 17:06:19 +02:00
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
2017-11-02 17:55:44 +01:00
|
|
|
}
|
|
|
|
else {
|
2017-04-13 12:36:54 +02:00
|
|
|
flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"sleep", addpay:false, append:"1", timer:"0.3", oldrc:"false"},
|
2017-04-10 17:06:19 +02:00
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
|
|
|
}
|
2016-04-27 23:32:58 +02:00
|
|
|
helper.load(execNode, flow, function() {
|
|
|
|
var n1 = helper.getNode("n1");
|
|
|
|
var n2 = helper.getNode("n2");
|
|
|
|
var n3 = helper.getNode("n3");
|
|
|
|
var n4 = helper.getNode("n4");
|
|
|
|
n4.on("input", function(msg) {
|
2017-11-02 17:55:44 +01:00
|
|
|
try {
|
|
|
|
msg.should.have.property("payload");
|
|
|
|
msg.payload.should.have.property("signal","SIGTERM");
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
catch(err) { done(err); }
|
2017-04-13 12:48:33 +02:00
|
|
|
});
|
2016-04-27 23:32:58 +02:00
|
|
|
n1.receive({});
|
|
|
|
});
|
|
|
|
});
|
2017-02-07 21:13:05 +01:00
|
|
|
|
|
|
|
it('should be able to kill a long running command', function(done) {
|
2017-04-10 17:06:19 +02:00
|
|
|
var flow;
|
|
|
|
if (osType === "Windows_NT") {
|
2017-04-13 12:36:54 +02:00
|
|
|
flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"ping", addpay:false, append:"192.0.2.0 -n 1 -w 1000 > NUL", timer:"2", oldrc:"false"},
|
2017-04-10 17:06:19 +02:00
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
2017-11-02 17:55:44 +01:00
|
|
|
}
|
|
|
|
else {
|
2017-04-13 12:36:54 +02:00
|
|
|
flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"sleep", addpay:false, append:"1", timer:"2", oldrc:"false"},
|
2017-02-07 21:13:05 +01:00
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
2017-04-10 17:06:19 +02:00
|
|
|
}
|
2017-02-07 21:13:05 +01:00
|
|
|
helper.load(execNode, flow, function() {
|
|
|
|
var n1 = helper.getNode("n1");
|
|
|
|
var n2 = helper.getNode("n2");
|
|
|
|
var n3 = helper.getNode("n3");
|
|
|
|
var n4 = helper.getNode("n4");
|
2017-11-02 17:55:44 +01:00
|
|
|
n2.on("input", function(msg) {
|
|
|
|
try {
|
|
|
|
msg.should.have.property("rc");
|
|
|
|
msg.rc.should.have.property("code",null);
|
|
|
|
msg.rc.should.have.property("signal","SIGTERM");
|
|
|
|
} catch(err) { done(err); }
|
|
|
|
});
|
2017-02-07 21:13:05 +01:00
|
|
|
n4.on("input", function(msg) {
|
2017-03-02 14:41:01 +01:00
|
|
|
try {
|
|
|
|
msg.should.have.property("payload");
|
|
|
|
msg.payload.should.have.property("signal","SIGTERM");
|
|
|
|
done();
|
|
|
|
}
|
2017-11-02 17:55:44 +01:00
|
|
|
catch(err) { done(err); }
|
2017-02-07 21:13:05 +01:00
|
|
|
});
|
|
|
|
setTimeout(function() {
|
2017-03-06 16:27:29 +01:00
|
|
|
n1.receive({kill:""});
|
|
|
|
},150);
|
|
|
|
n1.receive({});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to kill a long running command - SIGINT', function(done) {
|
2017-04-10 17:06:19 +02:00
|
|
|
var flow;
|
2017-04-13 12:36:54 +02:00
|
|
|
var sig = "SIGINT";
|
2017-04-10 17:06:19 +02:00
|
|
|
if (osType === "Windows_NT") {
|
2017-04-13 12:36:54 +02:00
|
|
|
flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"ping", addpay:false, append:"192.0.2.0 -n 1 -w 1000 > NUL", timer:"2", oldrc:"false"},
|
2017-04-10 17:06:19 +02:00
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
|
|
|
} else {
|
2017-04-13 12:36:54 +02:00
|
|
|
flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"sleep", addpay:false, append:"1", timer:"2", oldrc:"false"},
|
2017-03-06 16:27:29 +01:00
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
2017-04-10 17:06:19 +02:00
|
|
|
}
|
2017-03-06 16:27:29 +01:00
|
|
|
helper.load(execNode, flow, function() {
|
|
|
|
var n1 = helper.getNode("n1");
|
|
|
|
var n2 = helper.getNode("n2");
|
|
|
|
var n3 = helper.getNode("n3");
|
|
|
|
var n4 = helper.getNode("n4");
|
2017-11-02 17:55:44 +01:00
|
|
|
n2.on("input", function(msg) {
|
|
|
|
try {
|
|
|
|
msg.should.have.property("rc");
|
|
|
|
msg.rc.should.have.property("code",null);
|
|
|
|
msg.rc.should.have.property("signal","SIGINT");
|
|
|
|
} catch(err) { done(err); }
|
|
|
|
});
|
2017-03-06 16:27:29 +01:00
|
|
|
n4.on("input", function(msg) {
|
|
|
|
try {
|
|
|
|
msg.should.have.property("payload");
|
2017-04-13 12:36:54 +02:00
|
|
|
msg.payload.should.have.property("signal",sig);
|
2017-03-06 16:27:29 +01:00
|
|
|
done();
|
2017-11-02 17:55:44 +01:00
|
|
|
} catch(err) { done(err); }
|
2017-03-06 16:27:29 +01:00
|
|
|
});
|
|
|
|
setTimeout(function() {
|
2017-03-06 17:25:38 +01:00
|
|
|
n1.receive({kill:"SIGINT"});
|
2017-02-07 21:13:05 +01:00
|
|
|
},150);
|
|
|
|
n1.receive({});
|
|
|
|
});
|
|
|
|
});
|
2017-03-02 14:41:01 +01:00
|
|
|
|
2017-03-02 14:34:01 +01:00
|
|
|
it('should return the rc for a failing command', function(done) {
|
2017-04-13 12:36:54 +02:00
|
|
|
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"error", addpay:false, append:"", oldrc:"false"},
|
2017-03-02 14:34:01 +01:00
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
2021-04-09 12:22:57 +02:00
|
|
|
var spy = sinon.stub(child_process, 'exec').callsFake(
|
2017-03-29 22:44:58 +02:00
|
|
|
function(arg1, arg2, arg3, arg4) {
|
2017-03-02 14:34:01 +01:00
|
|
|
//console.log(arg1);
|
|
|
|
// arg3(error,stdout,stderr);
|
|
|
|
arg3({code: 1},arg1,arg1.toUpperCase());
|
|
|
|
});
|
|
|
|
helper.load(execNode, flow, function() {
|
|
|
|
var n1 = helper.getNode("n1");
|
|
|
|
var n2 = helper.getNode("n2");
|
|
|
|
var n3 = helper.getNode("n3");
|
|
|
|
var n4 = helper.getNode("n4");
|
|
|
|
var received = 0;
|
|
|
|
var messages = [null,null,null];
|
|
|
|
var completeTest = function() {
|
|
|
|
received++;
|
|
|
|
if (received < 3) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-29 22:44:58 +02:00
|
|
|
try {
|
2017-03-02 14:34:01 +01:00
|
|
|
var msg = messages[0];
|
|
|
|
msg.should.have.property("payload");
|
|
|
|
msg.payload.should.be.a.String();
|
|
|
|
msg.payload.should.equal("error");
|
2017-11-02 17:55:44 +01:00
|
|
|
msg.should.have.property("rc");
|
|
|
|
msg.rc.should.have.property("code",1);
|
|
|
|
msg.rc.should.have.property("message",undefined);
|
2017-03-02 14:34:01 +01:00
|
|
|
|
|
|
|
msg = messages[1];
|
|
|
|
msg.should.have.property("payload");
|
2017-04-13 12:36:54 +02:00
|
|
|
msg.payload.should.be.a.String();
|
2017-03-02 14:34:01 +01:00
|
|
|
msg.payload.should.equal("ERROR");
|
|
|
|
|
|
|
|
msg = messages[2];
|
|
|
|
msg.should.have.property("payload");
|
|
|
|
msg.payload.should.have.property("code",1);
|
|
|
|
|
|
|
|
child_process.exec.restore();
|
|
|
|
done();
|
2017-11-02 17:55:44 +01:00
|
|
|
}
|
|
|
|
catch(err) {
|
2017-03-02 14:34:01 +01:00
|
|
|
child_process.exec.restore();
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
n2.on("input", function(msg) {
|
|
|
|
messages[0] = msg;
|
|
|
|
completeTest();
|
|
|
|
});
|
|
|
|
n3.on("input", function(msg) {
|
|
|
|
messages[1] = msg;
|
|
|
|
completeTest();
|
|
|
|
});
|
|
|
|
n4.on("input", function(msg) {
|
|
|
|
messages[2] = msg;
|
|
|
|
completeTest();
|
|
|
|
});
|
|
|
|
n1.receive({payload:"and"});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-22 09:18:21 +02:00
|
|
|
it('should preserve existing properties on msg object', function(done) {
|
|
|
|
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo", addpay:false, append:"", oldrc:"false"},
|
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
2021-04-09 12:22:57 +02:00
|
|
|
var spy = sinon.stub(child_process, 'exec').callsFake(
|
2018-10-22 09:18:21 +02:00
|
|
|
function(arg1, arg2, arg3, arg4) {
|
|
|
|
// arg3(error,stdout,stderr);
|
|
|
|
arg3(null,arg1,arg1.toUpperCase());
|
|
|
|
});
|
|
|
|
|
|
|
|
helper.load(execNode, flow, function() {
|
|
|
|
var n1 = helper.getNode("n1");
|
|
|
|
var n2 = helper.getNode("n2");
|
|
|
|
var n3 = helper.getNode("n3");
|
|
|
|
var n4 = helper.getNode("n4");
|
|
|
|
var received = 0;
|
|
|
|
var messages = [null,null,null];
|
|
|
|
var completeTest = function() {
|
|
|
|
received = received + 1;
|
|
|
|
if (received < 3) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
var msg = messages[0];
|
|
|
|
msg.should.have.property("payload");
|
|
|
|
msg.payload.should.be.a.String();
|
|
|
|
msg.payload.should.equal("echo");
|
|
|
|
msg.should.have.property("rc");
|
|
|
|
msg.rc.should.have.property("code",0);
|
|
|
|
msg.should.have.property("foo","bar");
|
|
|
|
|
|
|
|
msg = messages[1];
|
|
|
|
msg.should.have.property("payload");
|
|
|
|
msg.payload.should.be.a.String();
|
|
|
|
msg.payload.should.equal("ECHO");
|
|
|
|
msg.should.have.property("rc");
|
|
|
|
msg.rc.should.have.property("code",0);
|
|
|
|
msg.should.have.property("foo","bar");
|
|
|
|
|
|
|
|
msg = messages[2];
|
|
|
|
msg.should.have.property("payload");
|
|
|
|
msg.payload.should.have.property("code",0);
|
|
|
|
msg.should.have.property("foo","bar");
|
|
|
|
|
|
|
|
child_process.exec.restore();
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
catch(err) {
|
|
|
|
child_process.exec.restore();
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
n2.on("input", function(msg) {
|
|
|
|
messages[0] = msg;
|
|
|
|
completeTest();
|
|
|
|
});
|
|
|
|
n3.on("input", function(msg) {
|
|
|
|
messages[1] = msg;
|
|
|
|
completeTest();
|
|
|
|
});
|
|
|
|
n4.on("input", function(msg) {
|
|
|
|
messages[2] = msg;
|
|
|
|
completeTest();
|
|
|
|
});
|
|
|
|
n1.receive({payload:"and", foo:"bar"});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should preserve existing properties on msg object for a failing command', function(done) {
|
|
|
|
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"error", addpay:false, append:"", oldrc:"false"},
|
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
2021-04-09 12:22:57 +02:00
|
|
|
var spy = sinon.stub(child_process, 'exec').callsFake(
|
2018-10-22 09:18:21 +02:00
|
|
|
function(arg1, arg2, arg3, arg4) {
|
|
|
|
// arg3(error,stdout,stderr);
|
|
|
|
arg3({code: 1},arg1,arg1.toUpperCase());
|
|
|
|
});
|
|
|
|
helper.load(execNode, flow, function() {
|
|
|
|
var n1 = helper.getNode("n1");
|
|
|
|
var n2 = helper.getNode("n2");
|
|
|
|
var n3 = helper.getNode("n3");
|
|
|
|
var n4 = helper.getNode("n4");
|
|
|
|
var received = 0;
|
|
|
|
var messages = [null,null,null];
|
|
|
|
var completeTest = function() {
|
|
|
|
received++;
|
|
|
|
if (received < 3) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
var msg = messages[0];
|
|
|
|
msg.should.have.property("payload");
|
|
|
|
msg.payload.should.be.a.String();
|
|
|
|
msg.payload.should.equal("error");
|
|
|
|
msg.should.have.property("rc");
|
|
|
|
msg.rc.should.have.property("code",1);
|
|
|
|
msg.rc.should.have.property("message",undefined);
|
|
|
|
msg.should.have.property("foo",null);
|
|
|
|
|
|
|
|
msg = messages[1];
|
|
|
|
msg.should.have.property("payload");
|
|
|
|
msg.payload.should.be.a.String();
|
|
|
|
msg.payload.should.equal("ERROR");
|
|
|
|
msg.should.have.property("foo",null);
|
|
|
|
|
|
|
|
msg = messages[2];
|
|
|
|
msg.should.have.property("payload");
|
|
|
|
msg.payload.should.have.property("code",1);
|
|
|
|
msg.should.have.property("foo",null);
|
|
|
|
|
|
|
|
child_process.exec.restore();
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
catch(err) {
|
|
|
|
child_process.exec.restore();
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
n2.on("input", function(msg) {
|
|
|
|
messages[0] = msg;
|
|
|
|
completeTest();
|
|
|
|
});
|
|
|
|
n3.on("input", function(msg) {
|
|
|
|
messages[1] = msg;
|
|
|
|
completeTest();
|
|
|
|
});
|
|
|
|
n4.on("input", function(msg) {
|
|
|
|
messages[2] = msg;
|
|
|
|
completeTest();
|
|
|
|
});
|
|
|
|
n1.receive({payload:"and", foo:null});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-18 13:40:08 +02:00
|
|
|
});
|
|
|
|
|
2016-04-23 18:09:46 +02:00
|
|
|
describe('calling spawn', function() {
|
2016-04-18 13:40:08 +02:00
|
|
|
|
|
|
|
it('should spawn a simple command', function(done) {
|
2017-04-10 17:06:19 +02:00
|
|
|
var flow;
|
|
|
|
var expected;
|
|
|
|
if (osType === "Windows_NT") {
|
|
|
|
// Need to use cmd to spawn a process because Windows echo command is a built-in command and cannot be spawned.
|
2017-04-19 23:09:53 +02:00
|
|
|
flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"cmd /C echo", addpay:true, append:"", useSpawn:"true", oldrc:"false"},
|
2016-04-18 13:40:08 +02:00
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
2017-04-10 17:06:19 +02:00
|
|
|
expected = "hello world\r\n";
|
|
|
|
} else {
|
2017-04-19 23:09:53 +02:00
|
|
|
flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo", addpay:true, append:"", useSpawn:"true", oldrc:"false"},
|
2017-04-10 17:06:19 +02:00
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
|
|
|
expected = "hello world\n";
|
|
|
|
}
|
2016-04-18 13:40:08 +02:00
|
|
|
var events = require('events');
|
|
|
|
|
|
|
|
helper.load(execNode, flow, function() {
|
|
|
|
var n1 = helper.getNode("n1");
|
|
|
|
var n2 = helper.getNode("n2");
|
|
|
|
var n3 = helper.getNode("n3");
|
|
|
|
var n4 = helper.getNode("n4");
|
2021-04-28 14:40:17 +02:00
|
|
|
var payload = "";
|
2016-04-18 13:40:08 +02:00
|
|
|
n2.on("input", function(msg) {
|
|
|
|
//console.log(msg);
|
2016-10-10 14:27:43 +02:00
|
|
|
try {
|
|
|
|
msg.should.have.property("payload");
|
|
|
|
msg.payload.should.be.a.String();
|
2021-04-28 14:40:17 +02:00
|
|
|
payload += msg.payload;
|
|
|
|
if (payload.endsWith("\n")) {
|
|
|
|
payload.should.equal(expected);
|
|
|
|
done();
|
|
|
|
}
|
2016-10-10 14:27:43 +02:00
|
|
|
}
|
2017-11-02 17:55:44 +01:00
|
|
|
catch(err) { done(err); }
|
2016-04-18 13:40:08 +02:00
|
|
|
});
|
|
|
|
n1.receive({payload:"hello world"});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should spawn a simple command with a non string payload parameter', function(done) {
|
2017-04-10 17:06:19 +02:00
|
|
|
var flow;
|
|
|
|
var expected;
|
|
|
|
if (osType === "Windows_NT") {
|
2017-04-19 23:09:53 +02:00
|
|
|
flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"cmd /C echo", addpay:true, append:" deg C", useSpawn:"true", oldrc:"false"},
|
2017-04-10 17:06:19 +02:00
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
|
|
|
expected = "12345 deg C\r\n";
|
|
|
|
} else {
|
2017-04-19 23:09:53 +02:00
|
|
|
flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo", addpay:true, append:" deg C", useSpawn:"true", oldrc:"false"},
|
2016-04-18 13:40:08 +02:00
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
2017-04-10 17:06:19 +02:00
|
|
|
expected = "12345 deg C\n";
|
|
|
|
}
|
2021-04-28 14:40:17 +02:00
|
|
|
var payload = "";
|
2016-04-18 13:40:08 +02:00
|
|
|
|
|
|
|
helper.load(execNode, flow, function() {
|
|
|
|
var n1 = helper.getNode("n1");
|
|
|
|
var n2 = helper.getNode("n2");
|
|
|
|
var n3 = helper.getNode("n3");
|
|
|
|
var n4 = helper.getNode("n4");
|
|
|
|
n2.on("input", function(msg) {
|
|
|
|
//console.log(msg);
|
2017-11-02 17:55:44 +01:00
|
|
|
try {
|
|
|
|
msg.should.have.property("payload");
|
|
|
|
msg.payload.should.be.a.String();
|
2021-04-28 14:40:17 +02:00
|
|
|
payload += msg.payload;
|
|
|
|
if (payload.endsWith("\n")) {
|
|
|
|
payload.should.equal(expected);
|
|
|
|
done();
|
|
|
|
}
|
2017-11-02 17:55:44 +01:00
|
|
|
}
|
|
|
|
catch(err) { done(err); }
|
2016-04-18 13:40:08 +02:00
|
|
|
});
|
|
|
|
n1.receive({payload:12345});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-23 18:09:46 +02:00
|
|
|
it('should spawn a simple command and return binary buffer', function(done) {
|
2017-04-10 17:06:19 +02:00
|
|
|
var flow;
|
|
|
|
if (osType === "Windows_NT") {
|
2017-04-19 23:09:53 +02:00
|
|
|
flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"cmd /C echo", addpay:true, append:"", useSpawn:"true", oldrc:"false"},
|
2016-04-18 13:40:08 +02:00
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
2017-04-10 17:06:19 +02:00
|
|
|
} else {
|
2017-04-19 23:09:53 +02:00
|
|
|
flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo", addpay:true, append:"", useSpawn:"true", oldrc:"false"},
|
2017-04-10 17:06:19 +02:00
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
|
|
|
}
|
2016-04-23 18:09:46 +02:00
|
|
|
|
2016-04-18 13:40:08 +02:00
|
|
|
helper.load(execNode, flow, function() {
|
|
|
|
var n1 = helper.getNode("n1");
|
|
|
|
var n2 = helper.getNode("n2");
|
|
|
|
var n3 = helper.getNode("n3");
|
|
|
|
var n4 = helper.getNode("n4");
|
2016-04-23 18:09:46 +02:00
|
|
|
n2.on("input", function(msg) {
|
2016-10-10 14:27:43 +02:00
|
|
|
try {
|
|
|
|
msg.should.have.property("payload");
|
|
|
|
Buffer.isBuffer(msg.payload).should.be.true();
|
2017-04-19 23:09:53 +02:00
|
|
|
if (osType === "Windows_NT") {
|
|
|
|
msg.payload.length.should.equalOneOf(6,8);
|
|
|
|
} else {
|
|
|
|
msg.payload.length.should.equal(7);
|
|
|
|
}
|
2016-10-10 14:27:43 +02:00
|
|
|
done();
|
|
|
|
}
|
2017-11-02 17:55:44 +01:00
|
|
|
catch(err) { done(err); }
|
2016-04-23 18:09:46 +02:00
|
|
|
});
|
2018-10-24 14:45:34 +02:00
|
|
|
n1.receive({payload:Buffer.from([0x01,0x02,0x03,0x88])});
|
2016-04-23 18:09:46 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-10-10 14:27:43 +02:00
|
|
|
it('should work if passed multiple words to spawn command', function(done) {
|
2017-04-10 17:06:19 +02:00
|
|
|
var flow;
|
|
|
|
var expected;
|
|
|
|
if (osType === "Windows_NT") {
|
2017-04-19 23:09:53 +02:00
|
|
|
flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"cmd /C echo this now works", addpay:false, append:"", useSpawn:"true", oldrc:"false"},
|
2017-04-10 17:06:19 +02:00
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
|
|
|
expected = "this now works\r\n";
|
|
|
|
} else {
|
2017-04-19 23:09:53 +02:00
|
|
|
flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo this now works", addpay:false, append:"", useSpawn:"true", oldrc:"false"},
|
2016-04-23 18:09:46 +02:00
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
2017-04-10 17:06:19 +02:00
|
|
|
expected = "this now works\n";
|
|
|
|
}
|
2016-04-23 18:09:46 +02:00
|
|
|
helper.load(execNode, flow, function() {
|
|
|
|
var n1 = helper.getNode("n1");
|
|
|
|
var n2 = helper.getNode("n2");
|
|
|
|
var n3 = helper.getNode("n3");
|
|
|
|
var n4 = helper.getNode("n4");
|
2016-10-11 15:49:48 +02:00
|
|
|
var received = 0;
|
|
|
|
var messages = [null,null];
|
|
|
|
var completeTest = function() {
|
|
|
|
received++;
|
|
|
|
if (received < 2) {
|
|
|
|
return;
|
|
|
|
}
|
2017-02-07 21:13:05 +01:00
|
|
|
try {
|
2016-10-11 15:49:48 +02:00
|
|
|
var msg = messages[0];
|
2016-10-10 14:27:43 +02:00
|
|
|
msg.should.have.property("payload");
|
2016-10-11 15:49:48 +02:00
|
|
|
msg.payload.should.be.a.String();
|
2017-04-10 17:06:19 +02:00
|
|
|
msg.payload.should.equal(expected);
|
2016-10-11 12:16:10 +02:00
|
|
|
|
2016-10-11 15:49:48 +02:00
|
|
|
msg = messages[1];
|
|
|
|
msg.should.have.property("payload");
|
2016-10-11 12:16:10 +02:00
|
|
|
should.exist(msg.payload);
|
2017-04-13 12:36:54 +02:00
|
|
|
msg.payload.should.have.property("code",0);
|
2016-10-10 14:27:43 +02:00
|
|
|
done();
|
2017-11-02 17:55:44 +01:00
|
|
|
}
|
|
|
|
catch(err) {
|
2016-10-10 14:27:43 +02:00
|
|
|
done(err);
|
|
|
|
}
|
2016-10-11 15:49:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
n2.on("input", function(msg) {
|
2021-04-28 14:40:17 +02:00
|
|
|
var payload = msg.payload;
|
|
|
|
if (messages[0]) {
|
|
|
|
messages[0].payload += payload;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
messages[0] = msg;
|
|
|
|
}
|
|
|
|
if (payload.endsWith("\n")) {
|
|
|
|
completeTest();
|
|
|
|
}
|
2016-10-11 15:49:48 +02:00
|
|
|
});
|
|
|
|
n4.on("input", function(msg) {
|
|
|
|
messages[1] = msg;
|
|
|
|
completeTest();
|
2016-04-23 18:09:46 +02:00
|
|
|
});
|
2016-10-11 15:49:48 +02:00
|
|
|
n1.receive({payload:null,fred:123});
|
2016-04-18 13:40:08 +02:00
|
|
|
});
|
|
|
|
});
|
2015-06-22 09:26:38 +02:00
|
|
|
|
2017-04-13 12:36:54 +02:00
|
|
|
it('should return an error for a bad command', function(done) {
|
|
|
|
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"madeupcommandshouldfail", addpay:false, append:"", useSpawn:"true", oldrc:"false"},
|
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
|
|
|
helper.load(execNode, flow, function() {
|
|
|
|
var n1 = helper.getNode("n1");
|
|
|
|
var n2 = helper.getNode("n2");
|
|
|
|
var n3 = helper.getNode("n3");
|
|
|
|
var n4 = helper.getNode("n4");
|
|
|
|
n4.on("input", function(msg) {
|
2017-11-02 17:55:44 +01:00
|
|
|
try {
|
|
|
|
msg.should.have.property("payload");
|
|
|
|
msg.payload.should.have.property("code");
|
|
|
|
msg.payload.code.should.be.below(0);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
catch(err) { done(err); }
|
2016-04-23 18:09:46 +02:00
|
|
|
});
|
2017-04-13 12:36:54 +02:00
|
|
|
n1.receive({payload:null});
|
2016-04-23 18:09:46 +02:00
|
|
|
});
|
2017-04-13 12:36:54 +02:00
|
|
|
});
|
2016-04-23 18:09:46 +02:00
|
|
|
|
|
|
|
it('should return an error for a failing command', function(done) {
|
2017-04-10 17:06:19 +02:00
|
|
|
var flow;
|
|
|
|
var expected;
|
2017-11-17 18:29:33 +01:00
|
|
|
var expectedFound = false;
|
2017-04-10 17:06:19 +02:00
|
|
|
if (osType === "Windows_NT") {
|
|
|
|
// Cannot use mkdir because Windows mkdir command automatically creates non-existent directories.
|
2017-04-13 12:36:54 +02:00
|
|
|
flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"ping /foo/bar/doo/dah", addpay:false, append:"", useSpawn:"true", oldrc:"false"},
|
2017-04-10 17:06:19 +02:00
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
|
|
|
expected = "IP address must be specified.";
|
|
|
|
} else {
|
2017-04-13 12:36:54 +02:00
|
|
|
flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"mkdir /foo/bar/doo/dah", addpay:false, append:"", useSpawn:"true", oldrc:"false"},
|
2017-04-10 17:06:19 +02:00
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
2017-11-02 17:55:44 +01:00
|
|
|
expected = ' directory';
|
2017-04-10 17:06:19 +02:00
|
|
|
}
|
2016-04-23 18:09:46 +02:00
|
|
|
helper.load(execNode, flow, function() {
|
|
|
|
var n1 = helper.getNode("n1");
|
|
|
|
var n2 = helper.getNode("n2");
|
|
|
|
var n3 = helper.getNode("n3");
|
|
|
|
var n4 = helper.getNode("n4");
|
|
|
|
n3.on("input", function(msg) {
|
2017-11-02 17:55:44 +01:00
|
|
|
try {
|
|
|
|
msg.should.have.property("payload");
|
|
|
|
msg.payload.should.be.a.String();
|
2017-11-17 18:29:33 +01:00
|
|
|
if (msg.payload.indexOf(expected) >= 0) {
|
|
|
|
// The error text on the stderr stream might get sent in more than one piece.
|
|
|
|
// We only need to know that it occurred before the return code is sent,
|
|
|
|
// as checked below in node n4.
|
|
|
|
expectedFound = true;
|
|
|
|
}
|
2017-11-02 17:55:44 +01:00
|
|
|
}
|
|
|
|
catch(err) { done(err); }
|
2016-04-23 18:09:46 +02:00
|
|
|
});
|
|
|
|
n4.on("input", function(msg) {
|
2017-11-02 17:55:44 +01:00
|
|
|
try {
|
2017-11-17 18:29:33 +01:00
|
|
|
expectedFound.should.be.true;
|
2017-11-02 17:55:44 +01:00
|
|
|
msg.should.have.property("payload");
|
|
|
|
msg.payload.should.have.property("code",1);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
catch(err) { done(err); }
|
2016-04-23 18:09:46 +02:00
|
|
|
});
|
|
|
|
n1.receive({payload:null});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-27 23:32:58 +02:00
|
|
|
it('should be able to timeout a long running command', function(done) {
|
2017-04-10 17:06:19 +02:00
|
|
|
var flow;
|
|
|
|
if (osType === "Windows_NT") {
|
2017-04-19 23:09:53 +02:00
|
|
|
flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"ping", addpay:false, append:"192.0.2.0 -n 1 -w 1000", timer:"0.3", useSpawn:"true", oldrc:"false"},
|
2017-04-10 17:06:19 +02:00
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
|
|
|
} else {
|
2017-04-13 12:36:54 +02:00
|
|
|
flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"sleep", addpay:false, append:"1", timer:"0.3", useSpawn:"true", oldrc:"false"},
|
2016-04-27 23:32:58 +02:00
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
2017-04-10 17:06:19 +02:00
|
|
|
}
|
2016-04-27 23:32:58 +02:00
|
|
|
helper.load(execNode, flow, function() {
|
|
|
|
var n1 = helper.getNode("n1");
|
|
|
|
var n2 = helper.getNode("n2");
|
|
|
|
var n3 = helper.getNode("n3");
|
|
|
|
var n4 = helper.getNode("n4");
|
|
|
|
n4.on("input", function(msg) {
|
2017-11-02 17:55:44 +01:00
|
|
|
try {
|
|
|
|
msg.should.have.property("payload");
|
|
|
|
msg.payload.should.have.property("code",null);
|
|
|
|
msg.payload.should.have.property("signal","SIGTERM");
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
catch(err) { done(err); }
|
2017-04-13 12:36:54 +02:00
|
|
|
});
|
2016-04-27 23:32:58 +02:00
|
|
|
n1.receive({});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-02-07 21:13:05 +01:00
|
|
|
it('should be able to kill a long running command', function(done) {
|
2017-04-10 17:06:19 +02:00
|
|
|
var flow;
|
|
|
|
if (osType === "Windows_NT") {
|
2017-04-13 12:36:54 +02:00
|
|
|
flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"ping", addpay:false, append:"192.0.2.0 -n 1 -w 1000 > NUL", timer:"2", oldrc:"false"},
|
2017-04-10 17:06:19 +02:00
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
|
|
|
} else {
|
2017-04-13 12:36:54 +02:00
|
|
|
flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"sleep", addpay:false, append:"1", timer:"2", oldrc:"false"},
|
2017-02-07 21:13:05 +01:00
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
2017-04-10 17:06:19 +02:00
|
|
|
}
|
2017-02-07 21:13:05 +01:00
|
|
|
helper.load(execNode, flow, function() {
|
|
|
|
var n1 = helper.getNode("n1");
|
|
|
|
var n2 = helper.getNode("n2");
|
|
|
|
var n3 = helper.getNode("n3");
|
|
|
|
var n4 = helper.getNode("n4");
|
|
|
|
n4.on("input", function(msg) {
|
2017-11-02 17:55:44 +01:00
|
|
|
try {
|
|
|
|
msg.should.have.property("payload");
|
|
|
|
msg.payload.should.have.property("signal","SIGTERM");
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
catch(err) { done(err); }
|
2017-02-07 21:13:05 +01:00
|
|
|
});
|
|
|
|
setTimeout(function() {
|
2017-03-06 16:27:29 +01:00
|
|
|
n1.receive({kill:""});
|
|
|
|
},150);
|
|
|
|
n1.receive({});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-06 18:42:59 +01:00
|
|
|
it('should be able to kill a long running command - SIGINT', function(done) {
|
2017-04-10 17:06:19 +02:00
|
|
|
var flow;
|
2017-04-13 12:36:54 +02:00
|
|
|
var sig = "SIGINT";
|
2017-04-10 17:06:19 +02:00
|
|
|
if (osType === "Windows_NT") {
|
2017-04-13 12:36:54 +02:00
|
|
|
flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"ping", addpay:false, append:"192.0.2.0 -n 1 -w 1000 > NUL", timer:"2", oldrc:"false"},
|
2017-04-10 17:06:19 +02:00
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
|
|
|
} else {
|
2017-04-13 12:36:54 +02:00
|
|
|
flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"sleep", addpay:false, append:"1", timer:"2", oldrc:"false"},
|
2017-03-06 16:27:29 +01:00
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
2017-04-10 17:06:19 +02:00
|
|
|
}
|
2017-03-06 16:27:29 +01:00
|
|
|
helper.load(execNode, flow, function() {
|
|
|
|
var n1 = helper.getNode("n1");
|
|
|
|
var n2 = helper.getNode("n2");
|
|
|
|
var n3 = helper.getNode("n3");
|
|
|
|
var n4 = helper.getNode("n4");
|
|
|
|
n4.on("input", function(msg) {
|
2017-11-02 17:55:44 +01:00
|
|
|
try {
|
|
|
|
msg.should.have.property("payload");
|
|
|
|
msg.payload.should.have.property("signal",sig);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
catch(err) { done(err); }
|
2017-03-06 16:27:29 +01:00
|
|
|
});
|
|
|
|
setTimeout(function() {
|
2017-03-06 18:42:59 +01:00
|
|
|
n1.receive({kill:"SIGINT"});
|
2017-02-07 21:13:05 +01:00
|
|
|
},150);
|
|
|
|
n1.receive({});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-22 09:18:21 +02:00
|
|
|
it('should preserve existing properties on msg object', function(done) {
|
|
|
|
var flow;
|
|
|
|
var expected;
|
|
|
|
if (osType === "Windows_NT") {
|
|
|
|
flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"cmd /C echo this now works", addpay:false, append:"", useSpawn:"true", oldrc:"false"},
|
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
|
|
|
expected = "this now works\r\n";
|
|
|
|
} else {
|
|
|
|
flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo this now works", addpay:false, append:"", useSpawn:"true", oldrc:"false"},
|
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
|
|
|
expected = "this now works\n";
|
|
|
|
}
|
|
|
|
helper.load(execNode, flow, function() {
|
|
|
|
var n1 = helper.getNode("n1");
|
|
|
|
var n2 = helper.getNode("n2");
|
|
|
|
var n3 = helper.getNode("n3");
|
|
|
|
var n4 = helper.getNode("n4");
|
|
|
|
var received = 0;
|
|
|
|
var messages = [null,null];
|
|
|
|
var completeTest = function() {
|
|
|
|
received++;
|
|
|
|
if (received < 2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
var msg = messages[0];
|
|
|
|
msg.should.have.property("payload");
|
|
|
|
msg.payload.should.be.a.String();
|
|
|
|
msg.payload.should.equal(expected);
|
|
|
|
msg.should.have.property("foo",123);
|
|
|
|
|
|
|
|
msg = messages[1];
|
|
|
|
msg.should.have.property("payload");
|
|
|
|
should.exist(msg.payload);
|
|
|
|
msg.payload.should.have.property("code",0);
|
|
|
|
msg.should.have.property("foo",123);
|
|
|
|
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
catch(err) {
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
n2.on("input", function(msg) {
|
2021-04-28 14:40:17 +02:00
|
|
|
var payload = msg.payload;
|
|
|
|
if (messages[0]) {
|
|
|
|
messages[0].payload += payload;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
messages[0] = msg;
|
|
|
|
}
|
|
|
|
if (payload.endsWith("\n")) {
|
|
|
|
completeTest();
|
|
|
|
}
|
2018-10-22 09:18:21 +02:00
|
|
|
});
|
|
|
|
n4.on("input", function(msg) {
|
|
|
|
messages[1] = msg;
|
|
|
|
completeTest();
|
|
|
|
});
|
|
|
|
n1.receive({payload:null,foo:123});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should preserve existing properties on msg object for a failing command', function(done) {
|
|
|
|
var flow;
|
|
|
|
var expected;
|
|
|
|
if (osType === "Windows_NT") {
|
|
|
|
// Cannot use mkdir because Windows mkdir command automatically creates non-existent directories.
|
|
|
|
flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"ping /foo/bar/doo/dah", addpay:false, append:"", useSpawn:"true", oldrc:"false"},
|
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
|
|
|
expected = "IP address must be specified.";
|
|
|
|
} else {
|
|
|
|
flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"mkdir /foo/bar/doo/dah", addpay:false, append:"", useSpawn:"true", oldrc:"false"},
|
|
|
|
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
|
|
|
expected = ' directory';
|
|
|
|
}
|
|
|
|
helper.load(execNode, flow, function() {
|
|
|
|
var n1 = helper.getNode("n1");
|
|
|
|
var n2 = helper.getNode("n2");
|
|
|
|
var n3 = helper.getNode("n3");
|
|
|
|
var n4 = helper.getNode("n4");
|
|
|
|
var received = 0;
|
|
|
|
var messages = [null,null];
|
|
|
|
var completeTest = function() {
|
2018-10-23 12:33:00 +02:00
|
|
|
if (messages[0] === null || messages[1] === null) {
|
|
|
|
// We have not yet had responses on both ports.
|
|
|
|
return
|
2018-10-22 09:18:21 +02:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
var msg = messages[0];
|
|
|
|
msg.should.have.property("payload");
|
|
|
|
msg.payload.should.be.a.String();
|
|
|
|
msg.should.have.property("foo","baz");
|
|
|
|
|
|
|
|
msg = messages[1];
|
|
|
|
msg.should.have.property("payload");
|
|
|
|
msg.payload.should.have.property("code",1);
|
|
|
|
msg.should.have.property("foo","baz");
|
|
|
|
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
catch(err) {
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
n3.on("input", function(msg) {
|
|
|
|
messages[0] = msg;
|
|
|
|
completeTest();
|
|
|
|
});
|
|
|
|
n4.on("input", function(msg) {
|
|
|
|
messages[1] = msg;
|
|
|
|
completeTest();
|
|
|
|
});
|
|
|
|
n1.receive({payload:null,foo:"baz"});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-23 18:09:46 +02:00
|
|
|
});
|
2015-06-22 09:26:38 +02:00
|
|
|
});
|