2015-11-09 12:29:48 +01:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
2015-11-09 12:29:48 +01: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");
|
2015-11-09 17:52:14 +01:00
|
|
|
var path = require("path");
|
2020-08-13 16:54:54 +02:00
|
|
|
var fs = require('fs-extra');
|
2018-01-24 23:56:54 +01:00
|
|
|
var EventEmitter = require('events');
|
2015-11-09 12:29:48 +01:00
|
|
|
|
2018-08-20 17:17:24 +02:00
|
|
|
var NR_TEST_UTILS = require("nr-test-utils");
|
|
|
|
|
|
|
|
var installer = NR_TEST_UTILS.require("@node-red/registry/lib/installer");
|
|
|
|
var registry = NR_TEST_UTILS.require("@node-red/registry/lib/index");
|
|
|
|
var typeRegistry = NR_TEST_UTILS.require("@node-red/registry/lib/registry");
|
2020-12-02 10:25:10 +01:00
|
|
|
const { events, exec, log } = NR_TEST_UTILS.require("@node-red/util");
|
2015-11-09 12:29:48 +01:00
|
|
|
|
|
|
|
describe('nodes/registry/installer', function() {
|
|
|
|
|
2018-04-23 15:24:51 +02:00
|
|
|
var mockLog = {
|
|
|
|
log: sinon.stub(),
|
|
|
|
debug: sinon.stub(),
|
|
|
|
trace: sinon.stub(),
|
|
|
|
warn: sinon.stub(),
|
|
|
|
info: sinon.stub(),
|
|
|
|
metric: sinon.stub(),
|
2020-08-13 16:54:54 +02:00
|
|
|
_: function(msg) { return msg }
|
2018-04-23 15:24:51 +02:00
|
|
|
}
|
|
|
|
|
2020-12-02 10:25:10 +01:00
|
|
|
var execResponse;
|
|
|
|
|
2018-10-19 00:49:47 +02:00
|
|
|
beforeEach(function() {
|
2021-04-09 12:22:57 +02:00
|
|
|
sinon.stub(exec,"run").callsFake(() => execResponse || Promise.resolve(""))
|
2020-12-02 10:25:10 +01:00
|
|
|
installer.init({})
|
2015-11-09 12:29:48 +01:00
|
|
|
});
|
2020-12-02 10:25:10 +01:00
|
|
|
|
2015-11-09 17:52:14 +01:00
|
|
|
afterEach(function() {
|
2020-12-02 10:25:10 +01:00
|
|
|
execResponse = null;
|
2015-11-14 21:27:04 +01:00
|
|
|
if (registry.addModule.restore) {
|
|
|
|
registry.addModule.restore();
|
|
|
|
}
|
|
|
|
if (registry.removeModule.restore) {
|
|
|
|
registry.removeModule.restore();
|
|
|
|
}
|
|
|
|
if (typeRegistry.removeModule.restore) {
|
|
|
|
typeRegistry.removeModule.restore();
|
|
|
|
}
|
|
|
|
if (registry.getModuleInfo.restore) {
|
|
|
|
registry.getModuleInfo.restore();
|
|
|
|
}
|
2017-01-22 00:46:44 +01:00
|
|
|
if (typeRegistry.getModuleInfo.restore) {
|
|
|
|
typeRegistry.getModuleInfo.restore();
|
|
|
|
}
|
2015-11-14 21:27:04 +01:00
|
|
|
|
2020-08-13 16:54:54 +02:00
|
|
|
if (fs.statSync.restore) {
|
|
|
|
fs.statSync.restore();
|
2015-11-14 21:27:04 +01:00
|
|
|
}
|
2020-12-02 10:25:10 +01:00
|
|
|
exec.run.restore();
|
2015-11-14 21:27:04 +01:00
|
|
|
});
|
2015-11-09 12:29:48 +01:00
|
|
|
|
|
|
|
describe("installs module", function() {
|
2020-06-03 11:45:28 +02:00
|
|
|
it("rejects module name that includes version", function(done) {
|
|
|
|
installer.installModule("module@version",null,null).catch(function(err) {
|
|
|
|
err.code.should.be.eql('invalid_module_name');
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
it("rejects missing module name", function(done) {
|
|
|
|
installer.installModule("",null,null).catch(function(err) {
|
|
|
|
err.code.should.be.eql('invalid_module_name');
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
it("rejects null module name", function(done) {
|
|
|
|
installer.installModule(null,null,null).catch(function(err) {
|
|
|
|
err.code.should.be.eql('invalid_module_name');
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
it("rejects invalid url", function(done) {
|
|
|
|
installer.installModule("module",null,"abc").catch(function(err) {
|
|
|
|
err.code.should.be.eql('invalid_module_url');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-11-09 12:29:48 +01:00
|
|
|
it("rejects when npm returns a 404", function(done) {
|
2018-10-19 00:49:47 +02:00
|
|
|
var res = {
|
|
|
|
code: 1,
|
|
|
|
stdout:"",
|
|
|
|
stderr:" 404 this_wont_exist"
|
|
|
|
}
|
|
|
|
var p = Promise.reject(res);
|
|
|
|
p.catch((err)=>{});
|
2020-12-02 10:25:10 +01:00
|
|
|
execResponse = p;
|
2018-04-24 16:01:49 +02:00
|
|
|
installer.installModule("this_wont_exist").catch(function(err) {
|
2018-04-23 15:24:51 +02:00
|
|
|
err.should.have.property("code",404);
|
2015-11-09 12:29:48 +01:00
|
|
|
done();
|
2020-06-03 11:45:28 +02:00
|
|
|
}).catch(done);
|
2015-11-09 12:29:48 +01:00
|
|
|
});
|
2017-01-22 00:46:44 +01:00
|
|
|
it("rejects when npm does not find specified version", function(done) {
|
2018-10-19 00:49:47 +02:00
|
|
|
var res = {
|
|
|
|
code: 1,
|
|
|
|
stdout:"",
|
|
|
|
stderr:" version not found: this_wont_exist@0.1.2"
|
|
|
|
}
|
|
|
|
var p = Promise.reject(res);
|
|
|
|
p.catch((err)=>{});
|
2020-12-02 10:25:10 +01:00
|
|
|
execResponse = p;
|
2021-04-09 12:22:57 +02:00
|
|
|
sinon.stub(typeRegistry,"getModuleInfo").callsFake(function() {
|
2017-01-22 00:46:44 +01:00
|
|
|
return {
|
|
|
|
version: "0.1.1"
|
|
|
|
}
|
|
|
|
});
|
2018-04-24 16:01:49 +02:00
|
|
|
installer.installModule("this_wont_exist","0.1.2").catch(function(err) {
|
2017-01-22 00:46:44 +01:00
|
|
|
err.code.should.be.eql(404);
|
|
|
|
done();
|
2020-06-03 11:45:28 +02:00
|
|
|
}).catch(done);
|
2017-01-22 00:46:44 +01:00
|
|
|
});
|
|
|
|
it("rejects when update requested to existing version", function(done) {
|
2021-04-09 12:22:57 +02:00
|
|
|
sinon.stub(typeRegistry,"getModuleInfo").callsFake(function() {
|
2017-01-22 00:46:44 +01:00
|
|
|
return {
|
2020-11-25 20:07:30 +01:00
|
|
|
user: true,
|
2017-01-22 00:46:44 +01:00
|
|
|
version: "0.1.1"
|
|
|
|
}
|
|
|
|
});
|
2018-04-24 16:01:49 +02:00
|
|
|
installer.installModule("this_wont_exist","0.1.1").catch(function(err) {
|
2017-01-22 00:46:44 +01:00
|
|
|
err.code.should.be.eql('module_already_loaded');
|
|
|
|
done();
|
2020-06-03 11:45:28 +02:00
|
|
|
}).catch(done);
|
2017-01-22 00:46:44 +01:00
|
|
|
});
|
2019-11-11 10:25:36 +01:00
|
|
|
it("rejects when update requested to existing version and url", function(done) {
|
2021-04-09 12:22:57 +02:00
|
|
|
sinon.stub(typeRegistry,"getModuleInfo").callsFake(function() {
|
2019-11-11 10:25:36 +01:00
|
|
|
return {
|
2020-11-25 20:07:30 +01:00
|
|
|
user: true,
|
2019-11-11 10:25:36 +01:00
|
|
|
version: "0.1.1"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
installer.installModule("this_wont_exist","0.1.1","https://example/foo-0.1.1.tgz").catch(function(err) {
|
|
|
|
err.code.should.be.eql('module_already_loaded');
|
|
|
|
done();
|
2020-06-03 11:45:28 +02:00
|
|
|
}).catch(done);
|
2019-11-11 10:25:36 +01:00
|
|
|
});
|
2015-11-09 12:29:48 +01:00
|
|
|
it("rejects with generic error", function(done) {
|
2018-10-19 00:49:47 +02:00
|
|
|
var res = {
|
|
|
|
code: 1,
|
|
|
|
stdout:"",
|
|
|
|
stderr:" kaboom!"
|
|
|
|
}
|
|
|
|
var p = Promise.reject(res);
|
|
|
|
p.catch((err)=>{});
|
2020-12-02 10:25:10 +01:00
|
|
|
execResponse = p;
|
2015-11-09 12:29:48 +01:00
|
|
|
installer.installModule("this_wont_exist").then(function() {
|
|
|
|
done(new Error("Unexpected success"));
|
2020-06-03 11:45:28 +02:00
|
|
|
}).catch(err => {
|
|
|
|
// Expected result
|
|
|
|
done()
|
2015-11-09 12:29:48 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
it("succeeds when module is found", function(done) {
|
|
|
|
var nodeInfo = {nodes:{module:"foo",types:["a"]}};
|
2018-10-19 00:49:47 +02:00
|
|
|
|
|
|
|
var res = {
|
|
|
|
code: 0,
|
|
|
|
stdout:"",
|
|
|
|
stderr:""
|
|
|
|
}
|
|
|
|
var p = Promise.resolve(res);
|
|
|
|
p.catch((err)=>{});
|
2020-12-02 10:25:10 +01:00
|
|
|
execResponse = p;
|
2018-10-19 00:49:47 +02:00
|
|
|
|
2021-04-09 12:22:57 +02:00
|
|
|
var addModule = sinon.stub(registry,"addModule").callsFake(function(md) {
|
2020-11-30 15:38:48 +01:00
|
|
|
return Promise.resolve(nodeInfo);
|
2015-11-09 12:29:48 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
installer.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();
|
2020-06-03 11:45:28 +02:00
|
|
|
}).catch(done);
|
2015-11-09 12:29:48 +01:00
|
|
|
});
|
2015-11-09 17:52:14 +01:00
|
|
|
it("rejects when non-existant path is provided", function(done) {
|
2018-07-27 08:40:55 +02:00
|
|
|
this.timeout(20000);
|
2018-04-26 13:32:05 +02:00
|
|
|
var resourcesDir = path.resolve(path.join(__dirname,"resources","local","TestNodeModule","node_modules","NonExistant"));
|
2015-11-09 17:52:14 +01:00
|
|
|
installer.installModule(resourcesDir).then(function() {
|
|
|
|
done(new Error("Unexpected success"));
|
2018-04-24 16:01:49 +02:00
|
|
|
}).catch(function(err) {
|
2017-03-06 16:28:23 +01:00
|
|
|
if (err.hasOwnProperty("code")) {
|
|
|
|
err.code.should.eql(404);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
else {
|
2017-03-06 18:40:09 +01:00
|
|
|
console.log("ERRROR::"+err.toString()+"::");
|
|
|
|
err.toString().should.eql("Error: Install failed");
|
2017-03-06 16:28:23 +01:00
|
|
|
done();
|
|
|
|
}
|
2015-11-09 17:52:14 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
it("succeeds when path is valid node-red module", function(done) {
|
|
|
|
var nodeInfo = {nodes:{module:"foo",types:["a"]}};
|
2021-04-09 12:22:57 +02:00
|
|
|
var addModule = sinon.stub(registry,"addModule").callsFake(function(md) {
|
2020-11-30 15:38:48 +01:00
|
|
|
return Promise.resolve(nodeInfo);
|
2015-11-09 17:52:14 +01:00
|
|
|
});
|
2018-04-26 13:32:05 +02:00
|
|
|
var resourcesDir = path.resolve(path.join(__dirname,"resources","local","TestNodeModule","node_modules","TestNodeModule"));
|
2018-10-19 00:49:47 +02:00
|
|
|
|
|
|
|
var res = {
|
|
|
|
code: 0,
|
|
|
|
stdout:"",
|
|
|
|
stderr:""
|
|
|
|
}
|
|
|
|
var p = Promise.resolve(res);
|
|
|
|
p.catch((err)=>{});
|
2020-12-02 10:25:10 +01:00
|
|
|
execResponse = p;
|
2015-11-09 17:52:14 +01:00
|
|
|
installer.installModule(resourcesDir).then(function(info) {
|
|
|
|
info.should.eql(nodeInfo);
|
|
|
|
done();
|
2020-06-03 11:45:28 +02:00
|
|
|
}).catch(done);
|
2015-11-09 17:52:14 +01:00
|
|
|
});
|
2019-11-11 10:25:36 +01:00
|
|
|
it("succeeds when url is valid node-red module", function(done) {
|
|
|
|
var nodeInfo = {nodes:{module:"foo",types:["a"]}};
|
|
|
|
|
|
|
|
var res = {
|
|
|
|
code: 0,
|
|
|
|
stdout:"",
|
|
|
|
stderr:""
|
|
|
|
}
|
|
|
|
var p = Promise.resolve(res);
|
|
|
|
p.catch((err)=>{});
|
2020-12-02 10:25:10 +01:00
|
|
|
execResponse = p;
|
2019-11-11 10:25:36 +01:00
|
|
|
|
2021-04-09 12:22:57 +02:00
|
|
|
var addModule = sinon.stub(registry,"addModule").callsFake(function(md) {
|
2020-11-30 15:38:48 +01:00
|
|
|
return Promise.resolve(nodeInfo);
|
2019-11-11 10:25:36 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
installer.installModule("this_wont_exist",null,"https://example/foo-0.1.1.tgz").then(function(info) {
|
|
|
|
info.should.eql(nodeInfo);
|
|
|
|
done();
|
2020-06-03 11:45:28 +02:00
|
|
|
}).catch(done);
|
2019-11-11 10:25:36 +01:00
|
|
|
});
|
2015-11-09 17:52:14 +01:00
|
|
|
|
2015-11-09 12:29:48 +01:00
|
|
|
});
|
|
|
|
describe("uninstalls module", function() {
|
|
|
|
it("rejects invalid module names", function(done) {
|
|
|
|
var promises = [];
|
2020-11-30 15:38:48 +01:00
|
|
|
var rejectedCount = 0;
|
|
|
|
|
|
|
|
promises.push(installer.uninstallModule("this_wont_exist ").catch(() => {rejectedCount++}));
|
|
|
|
promises.push(installer.uninstallModule("this_wont_exist;no_it_really_wont").catch(() => {rejectedCount++}));
|
|
|
|
Promise.all(promises).then(function() {
|
|
|
|
rejectedCount.should.eql(2);
|
2015-11-09 12:29:48 +01:00
|
|
|
done();
|
2020-11-30 15:38:48 +01:00
|
|
|
}).catch(done);
|
2015-11-09 12:29:48 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("rejects with generic error", function(done) {
|
|
|
|
var nodeInfo = [{module:"foo",types:["a"]}];
|
2021-04-09 12:22:57 +02:00
|
|
|
var removeModule = sinon.stub(registry,"removeModule").callsFake(function(md) {
|
2020-11-30 15:38:48 +01:00
|
|
|
return Promise.resolve(nodeInfo);
|
2015-11-09 12:29:48 +01:00
|
|
|
});
|
2018-10-19 00:49:47 +02:00
|
|
|
var res = {
|
|
|
|
code: 1,
|
|
|
|
stdout:"",
|
|
|
|
stderr:"error"
|
|
|
|
}
|
|
|
|
var p = Promise.reject(res);
|
|
|
|
p.catch((err)=>{});
|
2020-12-02 10:25:10 +01:00
|
|
|
execResponse = p;
|
2015-11-09 12:29:48 +01:00
|
|
|
|
|
|
|
installer.uninstallModule("this_wont_exist").then(function() {
|
|
|
|
done(new Error("Unexpected success"));
|
2020-06-03 11:45:28 +02:00
|
|
|
}).catch(err => {
|
|
|
|
// Expected result
|
|
|
|
done()
|
2015-11-09 12:29:48 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
it("succeeds when module is found", function(done) {
|
|
|
|
var nodeInfo = [{module:"foo",types:["a"]}];
|
2021-04-09 12:22:57 +02:00
|
|
|
var removeModule = sinon.stub(typeRegistry,"removeModule").callsFake(function(md) {
|
2015-11-09 12:29:48 +01:00
|
|
|
return nodeInfo;
|
|
|
|
});
|
2021-04-09 12:22:57 +02:00
|
|
|
var getModuleInfo = sinon.stub(registry,"getModuleInfo").callsFake(function(md) {
|
2015-11-09 12:29:48 +01:00
|
|
|
return {nodes:[]};
|
|
|
|
});
|
2018-10-19 00:49:47 +02:00
|
|
|
var res = {
|
|
|
|
code: 0,
|
|
|
|
stdout:"",
|
|
|
|
stderr:""
|
|
|
|
}
|
|
|
|
var p = Promise.resolve(res);
|
|
|
|
p.catch((err)=>{});
|
2020-12-02 10:25:10 +01:00
|
|
|
execResponse = p;
|
2015-11-09 12:29:48 +01:00
|
|
|
|
2021-04-09 12:22:57 +02:00
|
|
|
sinon.stub(fs,"statSync").callsFake(function(fn) { return {}; });
|
2015-11-09 12:29:48 +01:00
|
|
|
|
|
|
|
installer.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();
|
2020-06-03 11:45:28 +02:00
|
|
|
}).catch(done);
|
2015-11-09 12:29:48 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|