1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Move node install/remove from server component to node engine

This commit is contained in:
Nick O'Leary 2015-11-08 14:06:36 +00:00
parent 1c45bc615f
commit 985875cc75
7 changed files with 291 additions and 304 deletions

View File

@ -15,7 +15,6 @@
**/ **/
var redNodes = require("../nodes"); var redNodes = require("../nodes");
var comms = require("../comms"); var comms = require("../comms");
var server = require("../server");
var log = require("../log"); var log = require("../log");
var i18n = require("../i18n"); var i18n = require("../i18n");
@ -50,15 +49,14 @@ module.exports = {
res.status(400).json({error:"module_already_loaded", message:"Module already loaded"}); res.status(400).json({error:"module_already_loaded", message:"Module already loaded"});
return; return;
} }
promise = server.installModule(node.module); promise = redNodes.installModule(node.module);
} else if (node.file) {
promise = server.installNode(node.file);
} else { } else {
log.audit({event: "nodes.install",module:node.module,error:"invalid_request"},req); log.audit({event: "nodes.install",module:node.module,error:"invalid_request"},req);
res.status(400).json({error:"invalid_request", message:"Invalid request"}); res.status(400).json({error:"invalid_request", message:"Invalid request"});
return; return;
} }
promise.then(function(info) { promise.then(function(info) {
comms.publish("node/added",info.nodes,false);
if (node.module) { if (node.module) {
log.audit({event: "nodes.install",module:node.module},req); log.audit({event: "nodes.install",module:node.module},req);
res.json(redNodes.getModuleInfo(node.module)); res.json(redNodes.getModuleInfo(node.module));
@ -95,10 +93,11 @@ module.exports = {
res.status(404).end(); res.status(404).end();
return; return;
} else { } else {
promise = server.uninstallModule(mod); promise = redNodes.uninstallModule(mod);
} }
promise.then(function() { promise.then(function(list) {
comms.publish("node/removed",list,false);
log.audit({event: "nodes.remove",module:mod},req); log.audit({event: "nodes.remove",module:mod},req);
res.status(204).end(); res.status(204).end();
}).otherwise(function(err) { }).otherwise(function(err) {

View File

@ -13,12 +13,23 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
**/ **/
var when = require("when");
var path = require("path");
var fs = require("fs");
var registry = require("./registry"); var registry = require("./registry");
var credentials = require("./credentials"); var credentials = require("./credentials");
var flows = require("./flows"); var flows = require("./flows");
var Node = require("./Node"); var Node = require("./Node");
var log = require("../log"); var log = require("../log");
var events = require("../events");
var child_process = require('child_process');
var settings;
/** /**
* Registers a node constructor * Registers a node constructor
* @param type - the string type name * @param type - the string type name
@ -54,6 +65,7 @@ function createNode(node,def) {
} }
function init(_settings,storage) { function init(_settings,storage) {
settings = _settings;
credentials.init(storage); credentials.init(storage);
flows.init(_settings,storage); flows.init(_settings,storage);
registry.init(_settings); registry.init(_settings);
@ -106,6 +118,116 @@ function disableNode(id) {
return registry.disableNode(id); return registry.disableNode(id);
} }
function installModule(module) {
//TODO: ensure module is 'safe'
return when.promise(function(resolve,reject) {
if (/[\s;]/.test(module)) {
reject(new Error(log._("server.install.invalid")));
return;
}
if (registry.getModuleInfo(module)) {
// TODO: nls
var err = new Error("Module already loaded");
err.code = "module_already_loaded";
reject(err);
return;
}
log.info(log._("server.install.installing",{name: module}));
var installDir = settings.userDir || process.env.NODE_RED_HOME || ".";
var child = child_process.exec('npm install --production '+module,
{
cwd: installDir
},
function(err, stdin, stdout) {
if (err) {
var lookFor404 = new RegExp(" 404 .*"+module+"$","m");
if (lookFor404.test(stdout)) {
log.warn(log._("server.install.install-failed-not-found",{name:module}));
var e = new Error();
e.code = 404;
reject(e);
} else {
log.warn(log._("server.install.install-failed-long",{name:module}));
log.warn("------------------------------------------");
log.warn(err.toString());
log.warn("------------------------------------------");
reject(new Error(log._("server.install.install-failed")));
}
} else {
log.info(log._("server.install.installed",{name:module}));
resolve(registry.addModule(module).then(reportAddedModules));
}
}
);
});
}
function reportAddedModules(info) {
//comms.publish("node/added",info.nodes,false);
if (info.nodes.length > 0) {
log.info(log._("server.added-types"));
for (var i=0;i<info.nodes.length;i++) {
for (var j=0;j<info.nodes[i].types.length;j++) {
log.info(" - "+
(info.nodes[i].module?info.nodes[i].module+":":"")+
info.nodes[i].types[j]+
(info.nodes[i].err?" : "+info.nodes[i].err:"")
);
}
}
}
return info;
}
function reportRemovedModules(removedNodes) {
//comms.publish("node/removed",removedNodes,false);
log.info(log._("server.removed-types"));
for (var j=0;j<removedNodes.length;j++) {
for (var i=0;i<removedNodes[j].types.length;i++) {
log.info(" - "+(removedNodes[j].module?removedNodes[j].module+":":"")+removedNodes[j].types[i]);
}
}
return removedNodes;
}
function uninstallModule(module) {
return when.promise(function(resolve,reject) {
if (/[\s;]/.test(module)) {
reject(new Error(log._("server.install.invalid")));
return;
}
var installDir = settings.userDir || process.env.NODE_RED_HOME || ".";
var moduleDir = path.join(installDir,"node_modules",module);
if (!fs.existsSync(moduleDir)) {
return reject(new Error(log._("server.install.uninstall-failed",{name:module})));
}
var list = removeModule(module);
log.info(log._("server.install.uninstalling",{name:module}));
var child = child_process.exec('npm remove '+module,
{
cwd: installDir
},
function(err, stdin, stdout) {
if (err) {
log.warn(log._("server.install.uninstall-failed-long",{name:module}));
log.warn("------------------------------------------");
log.warn(err.toString());
log.warn("------------------------------------------");
reject(new Error(log._("server.install.uninstall-failed",{name:module})));
} else {
log.info(log._("server.install.uninstalled",{name:module}));
reportRemovedModules(list);
resolve(list);
}
}
);
});
}
module.exports = { module.exports = {
// Lifecycle // Lifecycle
init: init, init: init,
@ -116,9 +238,8 @@ module.exports = {
getNode: flows.get, getNode: flows.get,
eachNode: flows.eachNode, eachNode: flows.eachNode,
addFile: registry.addFile, installModule: installModule,
addModule: registry.addModule, uninstallModule: uninstallModule,
removeModule: removeModule,
enableNode: registry.enableNode, enableNode: registry.enableNode,
disableNode: disableNode, disableNode: disableNode,

View File

@ -36,12 +36,6 @@ function load(defaultNodesDir,disableNodePathScan) {
return loader.load(defaultNodesDir,disableNodePathScan); return loader.load(defaultNodesDir,disableNodePathScan);
} }
function addFile(file) {
var info = "node-red/"+path.basename(file).replace(/^\d+-/,"").replace(/\.js$/,"");
return loader.addFile(file).then(function() {
return registry.getNodeInfo(info);
});
}
function addModule(module) { function addModule(module) {
return loader.addModule(module).then(function() { return loader.addModule(module).then(function() {
return registry.getModuleInfo(module); return registry.getModuleInfo(module);
@ -79,7 +73,6 @@ module.exports = {
enableNode: enableNodeSet, enableNode: enableNodeSet,
disableNode: registry.disableNodeSet, disableNode: registry.disableNodeSet,
addFile: addFile,
addModule: addModule, addModule: addModule,
removeModule: registry.removeModule, removeModule: registry.removeModule,

View File

@ -16,9 +16,6 @@
var express = require('express'); var express = require('express');
var when = require('when'); var when = require('when');
var child_process = require('child_process');
var path = require("path");
var fs = require("fs");
var redNodes = require("./nodes"); var redNodes = require("./nodes");
var comms = require("./comms"); var comms = require("./comms");
@ -94,7 +91,7 @@ function start() {
if (missingModules.hasOwnProperty(i)) { if (missingModules.hasOwnProperty(i)) {
log.warn(" - "+i+": "+missingModules[i].join(", ")); log.warn(" - "+i+": "+missingModules[i].join(", "));
if (settings.autoInstallModules && i != "node-red") { if (settings.autoInstallModules && i != "node-red") {
serverAPI.installModule(i).otherwise(function(err) { redNodes.installModule(i).otherwise(function(err) {
// Error already reported. Need the otherwise handler // Error already reported. Need the otherwise handler
// to stop the error propagating any further // to stop the error propagating any further
}); });
@ -115,128 +112,6 @@ function start() {
}); });
} }
function reportAddedModules(info) {
comms.publish("node/added",info.nodes,false);
if (info.nodes.length > 0) {
log.info(log._("server.added-types"));
for (var i=0;i<info.nodes.length;i++) {
for (var j=0;j<info.nodes[i].types.length;j++) {
log.info(" - "+
(info.nodes[i].module?info.nodes[i].module+":":"")+
info.nodes[i].types[j]+
(info.nodes[i].err?" : "+info.nodes[i].err:"")
);
}
}
}
return info;
}
function reportRemovedModules(removedNodes) {
comms.publish("node/removed",removedNodes,false);
log.info(log._("server.removed-types"));
for (var j=0;j<removedNodes.length;j++) {
for (var i=0;i<removedNodes[j].types.length;i++) {
log.info(" - "+(removedNodes[j].module?removedNodes[j].module+":":"")+removedNodes[j].types[i]);
}
}
return removedNodes;
}
function installNode(file) {
return when.promise(function(resolve,reject) {
resolve(redNodes.addFile(file).then(function(info) {
var module = redNodes.getModuleInfo(info.module);
module.nodes = module.nodes.filter(function(d) {
return d.id==info.id;
});
return reportAddedModules(module);
}));
});
}
function installModule(module) {
//TODO: ensure module is 'safe'
return when.promise(function(resolve,reject) {
if (/[\s;]/.test(module)) {
reject(new Error(log._("server.install.invalid")));
return;
}
if (redNodes.getModuleInfo(module)) {
// TODO: nls
var err = new Error("Module already loaded");
err.code = "module_already_loaded";
reject(err);
return;
}
log.info(i18n._("server.install.installing",{name: module}));
var installDir = settings.userDir || process.env.NODE_RED_HOME || ".";
var child = child_process.exec('npm install --production '+module,
{
cwd: installDir
},
function(err, stdin, stdout) {
if (err) {
var lookFor404 = new RegExp(" 404 .*"+module+"$","m");
if (lookFor404.test(stdout)) {
log.warn(log._("server.install.install-failed-not-found",{name:module}));
var e = new Error();
e.code = 404;
reject(e);
} else {
log.warn(log._("server.install.install-failed-long",{name:module}));
log.warn("------------------------------------------");
log.warn(err.toString());
log.warn("------------------------------------------");
reject(new Error(log._("server.install.install-failed")));
}
} else {
log.info(log._("server.install.installed",{name:module}));
resolve(redNodes.addModule(module).then(reportAddedModules));
}
}
);
});
}
function uninstallModule(module) {
return when.promise(function(resolve,reject) {
if (/[\s;]/.test(module)) {
reject(new Error(log._("server.install.invalid")));
return;
}
var installDir = settings.userDir || process.env.NODE_RED_HOME || ".";
var moduleDir = path.join(installDir,"node_modules",module);
if (!fs.existsSync(moduleDir)) {
return reject(new Error(log._("server.install.uninstall-failed",{name:module})));
}
var list = redNodes.removeModule(module);
log.info(log._("server.install.uninstalling",{name:module}));
var child = child_process.exec('npm remove '+module,
{
cwd: installDir
},
function(err, stdin, stdout) {
if (err) {
log.warn(log._("server.install.uninstall-failed-long",{name:module}));
log.warn("------------------------------------------");
log.warn(err.toString());
log.warn("------------------------------------------");
reject(new Error(log._("server.install.uninstall-failed",{name:module})));
} else {
log.info(log._("server.install.uninstalled",{name:module}));
reportRemovedModules(list);
resolve(list);
}
}
);
});
}
function reportMetrics() { function reportMetrics() {
var memUsage = process.memoryUsage(); var memUsage = process.memoryUsage();
@ -271,12 +146,6 @@ var serverAPI = module.exports = {
start: start, start: start,
stop: stop, stop: stop,
reportAddedModules: reportAddedModules,
reportRemovedModules: reportRemovedModules,
installModule: installModule,
uninstallModule: uninstallModule,
installNode: installNode,
get app() { return app }, get app() { return app },
get nodeApp() { return nodeApp }, get nodeApp() { return nodeApp },
get server() { return server } get server() { return server }

View File

@ -22,7 +22,6 @@ var sinon = require('sinon');
var when = require('when'); var when = require('when');
var redNodes = require("../../../red/nodes"); var redNodes = require("../../../red/nodes");
var server = require("../../../red/server");
var settings = require("../../../red/settings"); var settings = require("../../../red/settings");
var nodes = require("../../../red/api/nodes"); var nodes = require("../../../red/api/nodes");
@ -213,7 +212,7 @@ describe("nodes api", function() {
name:"foo", name:"foo",
nodes:[{id:"123"}] nodes:[{id:"123"}]
}); });
var installModule = sinon.stub(server,'installModule', function() { var installModule = sinon.stub(redNodes,'installModule', function() {
return when.resolve({id:"123"}); return when.resolve({id:"123"});
}); });
@ -242,7 +241,7 @@ describe("nodes api", function() {
var getModuleInfo = sinon.stub(redNodes,'getModuleInfo',function(id) { var getModuleInfo = sinon.stub(redNodes,'getModuleInfo',function(id) {
return {nodes:{id:"123"}}; return {nodes:{id:"123"}};
}); });
var installModule = sinon.stub(server,'installModule', function() { var installModule = sinon.stub(redNodes,'installModule', function() {
return when.resolve({id:"123"}); return when.resolve({id:"123"});
}); });
@ -268,7 +267,7 @@ describe("nodes api", function() {
var getModuleInfo = sinon.stub(redNodes,'getModuleInfo',function(id) { var getModuleInfo = sinon.stub(redNodes,'getModuleInfo',function(id) {
return null; return null;
}); });
var installModule = sinon.stub(server,'installModule', function() { var installModule = sinon.stub(redNodes,'installModule', function() {
return when.reject(new Error("test error")); return when.reject(new Error("test error"));
}); });
@ -294,7 +293,7 @@ describe("nodes api", function() {
var getModuleInfo = sinon.stub(redNodes,'getModuleInfo',function(id) { var getModuleInfo = sinon.stub(redNodes,'getModuleInfo',function(id) {
return null; return null;
}); });
var installModule = sinon.stub(server,'installModule', function() { var installModule = sinon.stub(redNodes,'installModule', function() {
var err = new Error("test error"); var err = new Error("test error");
err.code = 404; err.code = 404;
return when.reject(err); return when.reject(err);
@ -344,7 +343,7 @@ describe("nodes api", function() {
var getModuleInfo = sinon.stub(redNodes,'getModuleInfo',function(id) { var getModuleInfo = sinon.stub(redNodes,'getModuleInfo',function(id) {
return {nodes:[{id:"123"}]}; return {nodes:[{id:"123"}]};
}); });
var uninstallModule = sinon.stub(server,'uninstallModule', function() { var uninstallModule = sinon.stub(redNodes,'uninstallModule', function() {
return when.resolve({id:"123"}); return when.resolve({id:"123"});
}); });
@ -398,7 +397,7 @@ describe("nodes api", function() {
var getModuleInfo = sinon.stub(redNodes,'getModuleInfo',function(id) { var getModuleInfo = sinon.stub(redNodes,'getModuleInfo',function(id) {
return {nodes:[{id:"123"}]}; return {nodes:[{id:"123"}]};
}); });
var uninstallModule = sinon.stub(server,'uninstallModule', function() { var uninstallModule = sinon.stub(redNodes,'uninstallModule', function() {
return when.reject(new Error("test error")); return when.reject(new Error("test error"));
}); });

View File

@ -19,9 +19,11 @@ var fs = require('fs-extra');
var path = require('path'); var path = require('path');
var when = require("when"); var when = require("when");
var sinon = require('sinon'); var sinon = require('sinon');
var child_process = require('child_process');
var index = require("../../../red/nodes/index"); var index = require("../../../red/nodes/index");
var flows = require("../../../red/nodes/flows"); var flows = require("../../../red/nodes/flows");
var registry = require("../../../red/nodes/registry");
describe("red/nodes/index", function() { describe("red/nodes/index", function() {
before(function() { before(function() {
@ -275,4 +277,155 @@ describe("red/nodes/index", function() {
}); });
}); });
}); });
describe("installs module", function() {
it("rejects invalid module names", function(done) {
var promises = [];
promises.push(index.installModule("this_wont_exist "));
promises.push(index.installModule("this_wont_exist;no_it_really_wont"));
when.settle(promises).then(function(results) {
results[0].state.should.be.eql("rejected");
results[1].state.should.be.eql("rejected");
done();
});
});
it("rejects when npm returns a 404", function(done) {
var exec = sinon.stub(child_process,"exec",function(cmd,opt,cb) {
cb(new Error(),""," 404 this_wont_exist");
});
index.installModule("this_wont_exist").otherwise(function(err) {
err.code.should.be.eql(404);
done();
}).finally(function() {
exec.restore();
});
});
it("rejects with generic error", function(done) {
var exec = sinon.stub(child_process,"exec",function(cmd,opt,cb) {
cb(new Error("test_error"),"","");
});
index.installModule("this_wont_exist").then(function() {
done(new Error("Unexpected success"));
}).otherwise(function(err) {
done();
}).finally(function() {
exec.restore();
});
});
it("succeeds when module is found", function(done) {
var nodeInfo = {nodes:{module:"foo",types:["a"]}};
var exec = sinon.stub(child_process,"exec",function(cmd,opt,cb) {
cb(null,"","");
});
var addModule = sinon.stub(registry,"addModule",function(md) {
return when.resolve(nodeInfo);
});
index.installModule("this_wont_exist").then(function(info) {
info.should.eql(nodeInfo);
// commsMessages.should.have.length(1);
// commsMessages[0].topic.should.equal("node/added");
// commsMessages[0].msg.should.eql(nodeInfo.nodes);
done();
}).otherwise(function(err) {
done(err);
}).finally(function() {
exec.restore();
addModule.restore();
});
});
it.skip("reports added modules", function() {
var nodes = {nodes:[
{types:["a"]},
{module:"foo",types:["b"]},
{types:["c"],err:"error"}
]};
var result = index.reportAddedModules(nodes);
result.should.equal(nodes);
commsMessages.should.have.length(1);
commsMessages[0].topic.should.equal("node/added");
commsMessages[0].msg.should.eql(nodes.nodes);
});
});
describe("uninstalls module", function() {
it("rejects invalid module names", function(done) {
var promises = [];
promises.push(index.uninstallModule("this_wont_exist "));
promises.push(index.uninstallModule("this_wont_exist;no_it_really_wont"));
when.settle(promises).then(function(results) {
results[0].state.should.be.eql("rejected");
results[1].state.should.be.eql("rejected");
done();
});
});
it("rejects with generic error", function(done) {
var nodeInfo = [{module:"foo",types:["a"]}];
var removeModule = sinon.stub(registry,"removeModule",function(md) {
return when.resolve(nodeInfo);
});
var exec = sinon.stub(child_process,"exec",function(cmd,opt,cb) {
cb(new Error("test_error"),"","");
});
index.uninstallModule("this_wont_exist").then(function() {
done(new Error("Unexpected success"));
}).otherwise(function(err) {
done();
}).finally(function() {
exec.restore();
removeModule.restore();
});
});
it("succeeds when module is found", function(done) {
var nodeInfo = [{module:"foo",types:["a"]}];
var removeModule = sinon.stub(registry,"removeModule",function(md) {
return nodeInfo;
});
var getModuleInfo = sinon.stub(registry,"getModuleInfo",function(md) {
return {nodes:[]};
});
var exec = sinon.stub(child_process,"exec",function(cmd,opt,cb) {
cb(null,"","");
});
var exists = sinon.stub(require('fs'),"existsSync", function(fn) { return true; });
index.uninstallModule("this_wont_exist").then(function(info) {
info.should.eql(nodeInfo);
// commsMessages.should.have.length(1);
// commsMessages[0].topic.should.equal("node/removed");
// commsMessages[0].msg.should.eql(nodeInfo);
done();
}).otherwise(function(err) {
done(err);
}).finally(function() {
exec.restore();
removeModule.restore();
exists.restore();
getModuleInfo.restore();
});
});
it.skip("reports removed modules", function() {
var nodes = [
{types:["a"]},
{module:"foo",types:["b"]},
{types:["c"],err:"error"}
];
var result = server.reportRemovedModules(nodes);
result.should.equal(nodes);
commsMessages.should.have.length(1);
commsMessages[0].topic.should.equal("node/removed");
commsMessages[0].msg.should.eql(nodes);
});
});
}); });

View File

@ -16,8 +16,6 @@
var should = require("should"); var should = require("should");
var when = require("when"); var when = require("when");
var sinon = require("sinon"); var sinon = require("sinon");
var child_process = require('child_process');
var fs = require("fs");
var comms = require("../../red/comms"); var comms = require("../../red/comms");
var redNodes = require("../../red/nodes"); var redNodes = require("../../red/nodes");
@ -143,7 +141,7 @@ describe("red/server", function() {
{ module:"node-red",enabled:true,loaded:false,types:["typeC","typeD"]} // missing { module:"node-red",enabled:true,loaded:false,types:["typeC","typeD"]} // missing
].filter(cb); ].filter(cb);
}); });
var serverInstallModule = sinon.stub(server,"installModule",function(name) { return when.resolve();}); var serverInstallModule = sinon.stub(redNodes,"installModule",function(name) { return when.resolve();});
server.init({},{testSettings: true, autoInstallModules:true, httpAdminRoot:"/", load:function() { return when.resolve();}}); server.init({},{testSettings: true, autoInstallModules:true, httpAdminRoot:"/", load:function() { return when.resolve();}});
server.start().then(function() { server.start().then(function() {
try { try {
@ -241,149 +239,4 @@ describe("red/server", function() {
commsStop.restore(); commsStop.restore();
stopFlows.restore(); stopFlows.restore();
}); });
it("reports added modules", function() {
var nodes = {nodes:[
{types:["a"]},
{module:"foo",types:["b"]},
{types:["c"],err:"error"}
]};
var result = server.reportAddedModules(nodes);
result.should.equal(nodes);
commsMessages.should.have.length(1);
commsMessages[0].topic.should.equal("node/added");
commsMessages[0].msg.should.eql(nodes.nodes);
});
it("reports removed modules", function() {
var nodes = [
{types:["a"]},
{module:"foo",types:["b"]},
{types:["c"],err:"error"}
];
var result = server.reportRemovedModules(nodes);
result.should.equal(nodes);
commsMessages.should.have.length(1);
commsMessages[0].topic.should.equal("node/removed");
commsMessages[0].msg.should.eql(nodes);
});
describe("installs module", function() {
it("rejects invalid module names", function(done) {
var promises = [];
promises.push(server.installModule("this_wont_exist "));
promises.push(server.installModule("this_wont_exist;no_it_really_wont"));
when.settle(promises).then(function(results) {
results[0].state.should.be.eql("rejected");
results[1].state.should.be.eql("rejected");
done();
});
});
it("rejects when npm returns a 404", function(done) {
var exec = sinon.stub(child_process,"exec",function(cmd,opt,cb) {
cb(new Error(),""," 404 this_wont_exist");
});
server.installModule("this_wont_exist").otherwise(function(err) {
err.code.should.be.eql(404);
done();
}).finally(function() {
exec.restore();
});
});
it("rejects with generic error", function(done) {
var exec = sinon.stub(child_process,"exec",function(cmd,opt,cb) {
cb(new Error("test_error"),"","");
});
server.installModule("this_wont_exist").then(function() {
done(new Error("Unexpected success"));
}).otherwise(function(err) {
done();
}).finally(function() {
exec.restore();
});
});
it("succeeds when module is found", function(done) {
var nodeInfo = {nodes:{module:"foo",types:["a"]}};
var exec = sinon.stub(child_process,"exec",function(cmd,opt,cb) {
cb(null,"","");
});
var addModule = sinon.stub(redNodes,"addModule",function(md) {
return when.resolve(nodeInfo);
});
server.installModule("this_wont_exist").then(function(info) {
info.should.eql(nodeInfo);
commsMessages.should.have.length(1);
commsMessages[0].topic.should.equal("node/added");
commsMessages[0].msg.should.eql(nodeInfo.nodes);
done();
}).otherwise(function(err) {
done(err);
}).finally(function() {
exec.restore();
addModule.restore();
});
});
});
describe("uninstalls module", function() {
it("rejects invalid module names", function(done) {
var promises = [];
promises.push(server.uninstallModule("this_wont_exist "));
promises.push(server.uninstallModule("this_wont_exist;no_it_really_wont"));
when.settle(promises).then(function(results) {
results[0].state.should.be.eql("rejected");
results[1].state.should.be.eql("rejected");
done();
});
});
it("rejects with generic error", function(done) {
var nodeInfo = [{module:"foo",types:["a"]}];
var removeModule = sinon.stub(redNodes,"removeModule",function(md) {
return when.resolve(nodeInfo);
});
var exec = sinon.stub(child_process,"exec",function(cmd,opt,cb) {
cb(new Error("test_error"),"","");
});
server.uninstallModule("this_wont_exist").then(function() {
done(new Error("Unexpected success"));
}).otherwise(function(err) {
done();
}).finally(function() {
exec.restore();
removeModule.restore();
});
});
it("succeeds when module is found", function(done) {
var nodeInfo = [{module:"foo",types:["a"]}];
var removeModule = sinon.stub(redNodes,"removeModule",function(md) {
return nodeInfo;
});
var exec = sinon.stub(child_process,"exec",function(cmd,opt,cb) {
cb(null,"","");
});
var exists = sinon.stub(fs,"existsSync", function(fn) { return true; });
server.uninstallModule("this_wont_exist").then(function(info) {
info.should.eql(nodeInfo);
commsMessages.should.have.length(1);
commsMessages[0].topic.should.equal("node/removed");
commsMessages[0].msg.should.eql(nodeInfo);
done();
}).otherwise(function(err) {
done(err);
}).finally(function() {
exec.restore();
removeModule.restore();
exists.restore();
});
});
});
}); });