2014-07-17 09:34:26 +02:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
2014-07-17 09:34:26 +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");
|
2014-11-04 18:05:29 +01:00
|
|
|
var sinon = require("sinon");
|
2015-11-12 08:56:23 +01:00
|
|
|
var path = require("path");
|
2014-11-04 18:05:29 +01:00
|
|
|
|
2018-08-20 17:17:24 +02:00
|
|
|
var NR_TEST_UTILS = require("nr-test-utils");
|
2014-07-17 09:34:26 +02:00
|
|
|
|
2018-08-20 17:17:24 +02:00
|
|
|
var api = NR_TEST_UTILS.require("@node-red/runtime/lib/api");
|
|
|
|
var runtime = NR_TEST_UTILS.require("@node-red/runtime");
|
|
|
|
|
|
|
|
var redNodes = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes");
|
|
|
|
var storage = NR_TEST_UTILS.require("@node-red/runtime/lib/storage");
|
|
|
|
var settings = NR_TEST_UTILS.require("@node-red/runtime/lib/settings");
|
2018-12-04 16:59:43 +01:00
|
|
|
var util = NR_TEST_UTILS.require("@node-red/util");
|
|
|
|
|
2018-08-20 17:17:24 +02:00
|
|
|
var log = NR_TEST_UTILS.require("@node-red/util").log;
|
2020-12-02 10:25:10 +01:00
|
|
|
var i18n = NR_TEST_UTILS.require("@node-red/util").i18n;
|
2015-11-12 08:56:23 +01:00
|
|
|
|
|
|
|
describe("runtime", function() {
|
2015-11-11 23:11:02 +01:00
|
|
|
afterEach(function() {
|
|
|
|
if (console.log.restore) {
|
|
|
|
console.log.restore();
|
|
|
|
}
|
|
|
|
})
|
2015-10-11 21:37:11 +02:00
|
|
|
|
2014-11-04 18:05:29 +01:00
|
|
|
before(function() {
|
2018-08-20 17:17:24 +02:00
|
|
|
process.env.NODE_RED_HOME = NR_TEST_UTILS.resolve("node-red");
|
2014-11-04 18:05:29 +01:00
|
|
|
});
|
|
|
|
after(function() {
|
2015-11-12 08:56:23 +01:00
|
|
|
delete process.env.NODE_RED_HOME;
|
2014-11-04 18:05:29 +01:00
|
|
|
});
|
2018-04-23 15:24:51 +02:00
|
|
|
function mockUtil(metrics) {
|
2020-12-02 10:25:10 +01:00
|
|
|
sinon.stub(log,"log",function(){})
|
|
|
|
sinon.stub(log,"warn",function(){})
|
|
|
|
sinon.stub(log,"info",function(){})
|
|
|
|
sinon.stub(log,"trace",function(){})
|
|
|
|
sinon.stub(log,"metric",function(){ return !!metrics })
|
|
|
|
sinon.stub(log,"_",function(){ return "abc"})
|
|
|
|
sinon.stub(i18n,"registerMessageCatalog",function(){ return Promise.resolve()})
|
|
|
|
}
|
|
|
|
function unmockUtil() {
|
|
|
|
log.log.restore && log.log.restore();
|
|
|
|
log.warn.restore && log.warn.restore();
|
|
|
|
log.info.restore && log.info.restore();
|
|
|
|
log.trace.restore && log.trace.restore();
|
|
|
|
log.metric.restore && log.metric.restore();
|
|
|
|
log._.restore && log._.restore();
|
|
|
|
i18n.registerMessageCatalog.restore && i18n.registerMessageCatalog.restore();
|
2018-04-23 15:24:51 +02:00
|
|
|
}
|
2015-11-11 23:11:02 +01:00
|
|
|
describe("init", function() {
|
|
|
|
beforeEach(function() {
|
2015-11-12 08:56:23 +01:00
|
|
|
sinon.stub(log,"init",function() {});
|
|
|
|
sinon.stub(settings,"init",function() {});
|
2015-11-24 23:38:42 +01:00
|
|
|
sinon.stub(redNodes,"init",function() {})
|
2020-12-02 10:25:10 +01:00
|
|
|
mockUtil();
|
2015-11-11 23:11:02 +01:00
|
|
|
});
|
|
|
|
afterEach(function() {
|
2015-11-12 08:56:23 +01:00
|
|
|
log.init.restore();
|
|
|
|
settings.init.restore();
|
2015-11-24 23:38:42 +01:00
|
|
|
redNodes.init.restore();
|
2020-12-02 10:25:10 +01:00
|
|
|
unmockUtil();
|
2015-11-11 23:11:02 +01:00
|
|
|
})
|
2015-10-11 21:37:11 +02:00
|
|
|
|
2015-11-11 23:11:02 +01:00
|
|
|
it("initialises components", function() {
|
2020-12-02 10:25:10 +01:00
|
|
|
runtime.init({testSettings: true, httpAdminRoot:"/"});
|
2016-10-10 14:27:43 +02:00
|
|
|
settings.init.called.should.be.true();
|
|
|
|
redNodes.init.called.should.be.true();
|
2015-11-11 23:11:02 +01:00
|
|
|
});
|
2015-10-11 21:37:11 +02:00
|
|
|
|
2015-11-12 08:56:23 +01:00
|
|
|
it("returns version", function() {
|
2020-12-02 10:25:10 +01:00
|
|
|
runtime.init({testSettings: true, httpAdminRoot:"/"});
|
2018-08-20 17:17:24 +02:00
|
|
|
return runtime.version().then(version => {
|
2018-09-21 15:06:16 +02:00
|
|
|
/^\d+\.\d+\.\d+(-.*)?$/.test(version).should.be.true();
|
2018-08-20 17:17:24 +02:00
|
|
|
});
|
|
|
|
|
2015-11-11 23:11:02 +01:00
|
|
|
|
2015-11-12 08:56:23 +01:00
|
|
|
})
|
2014-11-04 18:05:29 +01:00
|
|
|
});
|
2015-10-11 21:37:11 +02:00
|
|
|
|
2015-03-21 18:42:06 +01:00
|
|
|
describe("start",function() {
|
|
|
|
var storageInit;
|
|
|
|
var settingsLoad;
|
|
|
|
var redNodesInit;
|
|
|
|
var redNodesLoad;
|
|
|
|
var redNodesCleanModuleList;
|
|
|
|
var redNodesGetNodeList;
|
|
|
|
var redNodesLoadFlows;
|
2015-10-11 21:37:11 +02:00
|
|
|
var redNodesStartFlows;
|
2018-04-23 03:45:30 +02:00
|
|
|
var redNodesLoadContextsPlugin;
|
2015-10-11 21:37:11 +02:00
|
|
|
|
2015-03-21 18:42:06 +01:00
|
|
|
beforeEach(function() {
|
2018-04-24 16:01:49 +02:00
|
|
|
storageInit = sinon.stub(storage,"init",function(settings) {return Promise.resolve();});
|
2015-03-21 18:42:06 +01:00
|
|
|
redNodesInit = sinon.stub(redNodes,"init", function() {});
|
2018-04-24 16:01:49 +02:00
|
|
|
redNodesLoad = sinon.stub(redNodes,"load", function() {return Promise.resolve()});
|
2015-03-21 18:42:06 +01:00
|
|
|
redNodesCleanModuleList = sinon.stub(redNodes,"cleanModuleList",function(){});
|
2018-04-24 16:01:49 +02:00
|
|
|
redNodesLoadFlows = sinon.stub(redNodes,"loadFlows",function() {return Promise.resolve()});
|
2015-10-11 21:37:11 +02:00
|
|
|
redNodesStartFlows = sinon.stub(redNodes,"startFlows",function() {});
|
2018-07-30 00:47:19 +02:00
|
|
|
redNodesLoadContextsPlugin = sinon.stub(redNodes,"loadContextsPlugin",function() {return Promise.resolve()});
|
2020-12-02 10:25:10 +01:00
|
|
|
mockUtil();
|
2015-03-21 18:42:06 +01:00
|
|
|
});
|
|
|
|
afterEach(function() {
|
|
|
|
storageInit.restore();
|
|
|
|
redNodesInit.restore();
|
|
|
|
redNodesLoad.restore();
|
2015-10-11 21:37:11 +02:00
|
|
|
redNodesGetNodeList.restore();
|
2015-03-21 18:42:06 +01:00
|
|
|
redNodesCleanModuleList.restore();
|
|
|
|
redNodesLoadFlows.restore();
|
2015-10-11 21:37:11 +02:00
|
|
|
redNodesStartFlows.restore();
|
2018-04-23 03:45:30 +02:00
|
|
|
redNodesLoadContextsPlugin.restore();
|
2020-12-02 10:25:10 +01:00
|
|
|
unmockUtil();
|
2015-03-21 18:42:06 +01:00
|
|
|
});
|
|
|
|
it("reports errored/missing modules",function(done) {
|
2015-03-30 22:49:20 +02:00
|
|
|
redNodesGetNodeList = sinon.stub(redNodes,"getNodeList", function(cb) {
|
2015-03-21 18:42:06 +01:00
|
|
|
return [
|
|
|
|
{ err:"errored",name:"errName" }, // error
|
|
|
|
{ module:"module",enabled:true,loaded:false,types:["typeA","typeB"]} // missing
|
2015-03-30 22:49:20 +02:00
|
|
|
].filter(cb);
|
2015-03-21 18:42:06 +01:00
|
|
|
});
|
2020-12-02 10:25:10 +01:00
|
|
|
runtime.init({testSettings: true, httpAdminRoot:"/", load:function() { return Promise.resolve();}});
|
2018-04-23 15:24:51 +02:00
|
|
|
// sinon.stub(console,"log");
|
2015-11-11 23:11:02 +01:00
|
|
|
runtime.start().then(function() {
|
2018-04-23 15:24:51 +02:00
|
|
|
// console.log.restore();
|
2015-03-21 18:42:06 +01:00
|
|
|
try {
|
2016-10-10 14:27:43 +02:00
|
|
|
storageInit.calledOnce.should.be.true();
|
|
|
|
redNodesInit.calledOnce.should.be.true();
|
|
|
|
redNodesLoad.calledOnce.should.be.true();
|
|
|
|
redNodesLoadFlows.calledOnce.should.be.true();
|
2015-10-11 21:37:11 +02:00
|
|
|
|
2020-12-02 10:25:10 +01:00
|
|
|
log.warn.calledWithMatch("Failed to register 1 node type");
|
|
|
|
log.warn.calledWithMatch("Missing node modules");
|
|
|
|
log.warn.calledWithMatch(" - module: typeA, typeB");
|
2016-10-10 14:27:43 +02:00
|
|
|
redNodesCleanModuleList.calledOnce.should.be.true();
|
2015-03-21 18:42:06 +01:00
|
|
|
done();
|
|
|
|
} catch(err) {
|
|
|
|
done(err);
|
|
|
|
}
|
2018-04-23 15:24:51 +02:00
|
|
|
}).catch(err=>{done(err)});
|
2015-03-21 18:42:06 +01:00
|
|
|
});
|
|
|
|
it("initiates load of missing modules",function(done) {
|
2015-03-30 22:49:20 +02:00
|
|
|
redNodesGetNodeList = sinon.stub(redNodes,"getNodeList", function(cb) {
|
2015-03-21 18:42:06 +01:00
|
|
|
return [
|
|
|
|
{ err:"errored",name:"errName" }, // error
|
|
|
|
{ err:"errored",name:"errName" }, // error
|
|
|
|
{ module:"module",enabled:true,loaded:false,types:["typeA","typeB"]}, // missing
|
|
|
|
{ module:"node-red",enabled:true,loaded:false,types:["typeC","typeD"]} // missing
|
2015-03-30 22:49:20 +02:00
|
|
|
].filter(cb);
|
2015-03-21 18:42:06 +01:00
|
|
|
});
|
2018-04-24 16:01:49 +02:00
|
|
|
var serverInstallModule = sinon.stub(redNodes,"installModule",function(name) { return Promise.resolve({nodes:[]});});
|
2020-12-02 10:25:10 +01:00
|
|
|
runtime.init({testSettings: true, autoInstallModules:true, httpAdminRoot:"/", load:function() { return Promise.resolve();}});
|
2015-11-11 23:11:02 +01:00
|
|
|
sinon.stub(console,"log");
|
|
|
|
runtime.start().then(function() {
|
|
|
|
console.log.restore();
|
2015-03-21 18:42:06 +01:00
|
|
|
try {
|
2020-12-02 10:25:10 +01:00
|
|
|
log.warn.calledWithMatch("Failed to register 2 node types");
|
|
|
|
log.warn.calledWithMatch("Missing node modules");
|
|
|
|
log.warn.calledWithMatch(" - module: typeA, typeB");
|
|
|
|
log.warn.calledWithMatch(" - node-red: typeC, typeD");
|
2016-10-10 14:27:43 +02:00
|
|
|
redNodesCleanModuleList.calledOnce.should.be.false();
|
|
|
|
serverInstallModule.calledOnce.should.be.true();
|
2015-03-21 18:42:06 +01:00
|
|
|
serverInstallModule.calledWithMatch("module");
|
|
|
|
done();
|
|
|
|
} catch(err) {
|
|
|
|
done(err);
|
|
|
|
} finally {
|
|
|
|
serverInstallModule.restore();
|
|
|
|
}
|
2018-04-23 15:24:51 +02:00
|
|
|
}).catch(err=>{done(err)});
|
2015-03-21 18:42:06 +01:00
|
|
|
});
|
|
|
|
it("reports errored modules when verbose is enabled",function(done) {
|
2015-03-30 22:49:20 +02:00
|
|
|
redNodesGetNodeList = sinon.stub(redNodes,"getNodeList", function(cb) {
|
2015-03-21 18:42:06 +01:00
|
|
|
return [
|
|
|
|
{ err:"errored",name:"errName" } // error
|
2015-03-30 22:49:20 +02:00
|
|
|
].filter(cb);
|
2015-03-21 18:42:06 +01:00
|
|
|
});
|
2020-12-02 10:25:10 +01:00
|
|
|
runtime.init({testSettings: true, verbose:true, httpAdminRoot:"/", load:function() { return Promise.resolve();}});
|
2015-11-11 23:11:02 +01:00
|
|
|
sinon.stub(console,"log");
|
|
|
|
runtime.start().then(function() {
|
|
|
|
console.log.restore();
|
2015-03-21 18:42:06 +01:00
|
|
|
try {
|
2020-12-02 10:25:10 +01:00
|
|
|
log.warn.neverCalledWithMatch("Failed to register 1 node type");
|
|
|
|
log.warn.calledWithMatch("[errName] errored");
|
2015-03-21 18:42:06 +01:00
|
|
|
done();
|
|
|
|
} catch(err) {
|
|
|
|
done(err);
|
|
|
|
}
|
2018-04-23 15:24:51 +02:00
|
|
|
}).catch(err=>{done(err)});
|
2015-03-21 18:42:06 +01:00
|
|
|
});
|
2015-10-11 21:37:11 +02:00
|
|
|
|
2015-03-21 18:42:06 +01:00
|
|
|
it("reports runtime metrics",function(done) {
|
2018-07-30 00:47:19 +02:00
|
|
|
var stopFlows = sinon.stub(redNodes,"stopFlows",function() { return Promise.resolve();} );
|
2015-03-21 18:42:06 +01:00
|
|
|
redNodesGetNodeList = sinon.stub(redNodes,"getNodeList", function() {return []});
|
2020-12-02 10:25:10 +01:00
|
|
|
unmockUtil();
|
|
|
|
mockUtil(true);
|
2018-12-04 16:59:43 +01:00
|
|
|
runtime.init(
|
|
|
|
{testSettings: true, runtimeMetricInterval:200, httpAdminRoot:"/", load:function() { return Promise.resolve();}},
|
|
|
|
{},
|
2020-12-02 10:25:10 +01:00
|
|
|
undefined);
|
2018-12-04 16:59:43 +01:00
|
|
|
// sinon.stub(console,"log");
|
2015-11-11 23:11:02 +01:00
|
|
|
runtime.start().then(function() {
|
2018-12-04 16:59:43 +01:00
|
|
|
// console.log.restore();
|
2015-03-21 18:42:06 +01:00
|
|
|
setTimeout(function() {
|
|
|
|
try {
|
2020-12-02 10:25:10 +01:00
|
|
|
log.log.args.should.have.lengthOf(3);
|
|
|
|
log.log.args[0][0].should.have.property("event","runtime.memory.rss");
|
|
|
|
log.log.args[1][0].should.have.property("event","runtime.memory.heapTotal");
|
|
|
|
log.log.args[2][0].should.have.property("event","runtime.memory.heapUsed");
|
2015-03-21 18:42:06 +01:00
|
|
|
done();
|
|
|
|
} catch(err) {
|
|
|
|
done(err);
|
|
|
|
} finally {
|
2015-11-11 23:11:02 +01:00
|
|
|
runtime.stop();
|
2015-03-21 18:42:06 +01:00
|
|
|
stopFlows.restore();
|
|
|
|
}
|
2015-11-11 23:11:02 +01:00
|
|
|
},300);
|
2018-04-23 15:24:51 +02:00
|
|
|
}).catch(err=>{done(err)});
|
2015-10-11 21:37:11 +02:00
|
|
|
});
|
|
|
|
|
2015-11-11 23:11:02 +01:00
|
|
|
|
2015-03-21 18:42:06 +01:00
|
|
|
});
|
2015-10-11 21:37:11 +02:00
|
|
|
|
2018-05-30 03:24:27 +02:00
|
|
|
it("stops components", function(done) {
|
2018-07-30 00:47:19 +02:00
|
|
|
var stopFlows = sinon.stub(redNodes,"stopFlows",function() { return Promise.resolve();} );
|
|
|
|
var closeContextsPlugin = sinon.stub(redNodes,"closeContextsPlugin",function() { return Promise.resolve();} );
|
2018-05-30 03:24:27 +02:00
|
|
|
runtime.stop().then(function(){
|
|
|
|
stopFlows.called.should.be.true();
|
|
|
|
closeContextsPlugin.called.should.be.true();
|
2018-07-30 00:47:19 +02:00
|
|
|
stopFlows.restore();
|
|
|
|
closeContextsPlugin.restore();
|
2018-05-30 03:24:27 +02:00
|
|
|
done();
|
|
|
|
}).catch(function(err){
|
|
|
|
stopFlows.restore();
|
|
|
|
closeContextsPlugin.restore();
|
2018-07-30 00:47:19 +02:00
|
|
|
return done(err)
|
2018-05-30 03:24:27 +02:00
|
|
|
});
|
2014-11-04 18:05:29 +01:00
|
|
|
});
|
2014-07-17 09:34:26 +02:00
|
|
|
});
|