Add support of subflow env var

This commit is contained in:
Hiroyasu Nishiyama
2019-01-26 23:15:20 +09:00
parent 4baaaa8d59
commit a413f3cded
13 changed files with 835 additions and 24 deletions

View File

@@ -0,0 +1,408 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* 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 functionNode = require("nr-test-utils").require("@node-red/nodes/core/core/80-function.js");
var helper = require("node-red-node-test-helper");
// Notice:
// - nodes should have x, y, z property when defining subflow.
describe('subflow', function() {
before(function(done) {
helper.startServer(done);
});
after(function(done) {
helper.stopServer(done);
});
afterEach(function() {
helper.unload();
});
it('should define subflow', function(done) {
var flow = [
{id:"t1", type:"tab"},
{id:"n1", z:"t1", type:"subflow:s1", wires:[["n2"]]},
{id:"n2", z:"t1", type:"helper", wires:[]},
// Subflow
{id:"s1", type:"subflow", name:"Subflow", info:"",
in:[{wires:[ {id:"s1-n1"} ]}],
out:[{wires:[ {id:"s1-n1", port:0} ]}]},
{id:"s1-n1", z:"s1", type:"function",
func:"return msg;", wires:[]}
];
helper.load(functionNode, flow, function() {
done();
});
});
it('should pass data to/from subflow', function(done) {
var flow = [
{id:"t0", type:"tab", label:"", disabled:false, info:""},
{id:"n1", x:10, y:10, z:"t0", type:"subflow:s1", wires:[["n2"]]},
{id:"n2", x:10, y:10, z:"t0", type:"helper", wires:[]},
// Subflow
{id:"s1", type:"subflow", name:"Subflow", info:"",
in:[{
x:10, y:10,
wires:[ {id:"s1-n1"} ]
}],
out:[{
x:10, y:10,
wires:[ {id:"s1-n1", port:0} ]
}]
},
{id:"s1-n1", x:10, y:10, z:"s1", type:"function",
func:"msg.payload = msg.payload+'bar'; return msg;", wires:[]}
];
helper.load(functionNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
msg.should.have.property("payload", "foobar");
done();
});
n1.receive({payload:"foo"});
});
});
it('should pass data to/from nested subflow', function(done) {
var flow = [
{id:"t0", type:"tab", label:"", disabled:false, info:""},
{id:"n1", x:10, y:10, z:"t0", type:"subflow:s1", wires:[["n2"]]},
{id:"n2", x:10, y:10, z:"t0", type:"helper", wires:[]},
// Subflow1
{id:"s1", type:"subflow", name:"Subflow1", info:"",
in:[{
x:10, y:10,
wires:[ {id:"s1-n1"} ]
}],
out:[{
x:10, y:10,
wires:[ {id:"s1-n2", port:0} ]
}]
},
{id:"s1-n1", x:10, y:10, z:"s1", type:"subflow:s2",
wires:[["s1-n2"]]},
{id:"s1-n2", x:10, y:10, z:"s1", type:"function",
func:"msg.payload = msg.payload+'baz'; return msg;", wires:[]},
// Subflow2
{id:"s2", type:"subflow", name:"Subflow2", info:"",
in:[{
x:10, y:10,
wires:[ {id:"s2-n1"} ]
}],
out:[{
x:10, y:10,
wires:[ {id:"s2-n1", port:0} ]
}]
},
{id:"s2-n1", x:10, y:10, z:"s2", type:"function",
func:"msg.payload=msg.payload+'bar'; return msg;", wires:[]}
];
helper.load(functionNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
msg.should.have.property("payload", "foobarbaz");
done();
});
n1.receive({payload:"foo"});
});
});
it('should access env var of subflow template', function(done) {
var flow = [
{id:"t0", type:"tab", label:"", disabled:false, info:""},
{id:"n1", x:10, y:10, z:"t0", type:"subflow:s1", wires:[["n2"]]},
{id:"n2", x:10, y:10, z:"t0", type:"helper", wires:[]},
// Subflow
{id:"s1", type:"subflow", name:"Subflow", info:"",
env: [
{name: "K", type: "T", value: "V",
info: {
name: "K",
label: "",
value: "V",
type: "T",
target_type: "env var",
"target": "K"
}}
],
in:[{
x:10, y:10,
wires:[ {id:"s1-n1"} ]
}],
out:[{
x:10, y:10,
wires:[ {id:"s1-n1", port:0} ]
}]
},
{id:"s1-n1", x:10, y:10, z:"s1", type:"function",
func:"msg.V = env.get('K'); msg.T = env.get('K_type'); msg.I = env.get('K_info'); return msg;",
wires:[]}
];
helper.load(functionNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
try {
msg.should.have.property("V", "V");
msg.should.have.property("T", "T");
msg.should.have.property("I");
done();
}
catch (e) {
console.log(e);
done(e);
}
});
n1.receive({payload:"foo"});
});
});
it('should access env var of subflow instance', function(done) {
var flow = [
{id:"t0", type:"tab", label:"", disabled:false, info:""},
{id:"n1", x:10, y:10, z:"t0", type:"subflow:s1",
env: [
{name: "K", type: "T", value: "V",
info: {
name: "K",
label: "",
value: "V",
type: "T",
target_type: "env var",
"target": "K"
}}
],
wires:[["n2"]]},
{id:"n2", x:10, y:10, z:"t0", type:"helper", wires:[]},
// Subflow
{id:"s1", type:"subflow", name:"Subflow", info:"",
in:[{
x:10, y:10,
wires:[ {id:"s1-n1"} ]
}],
out:[{
x:10, y:10,
wires:[ {id:"s1-n1", port:0} ]
}]
},
{id:"s1-n1", x:10, y:10, z:"s1", type:"function",
func:"msg.V = env.get('K'); msg.T = env.get('K_type'); msg.I = env.get('K_info'); return msg;",
wires:[]}
];
helper.load(functionNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
try {
msg.should.have.property("V", "V");
msg.should.have.property("T", "T");
msg.should.have.property("I");
done();
}
catch (e) {
console.log(e);
done(e);
}
});
n1.receive({payload:"foo"});
});
});
it('should overwrite env var of subflow template by env var of subflow instance', function(done) {
var flow = [
{id:"t0", type:"tab", label:"", disabled:false, info:""},
{id:"n1", x:10, y:10, z:"t0", type:"subflow:s1",
env: [
{name: "K", type: "T", value: "V",
info: {
name: "K",
label: "",
value: "V",
type: "T",
target_type: "env var",
"target": "K"
}}
],
wires:[["n2"]]},
{id:"n2", x:10, y:10, z:"t0", type:"helper", wires:[]},
// Subflow
{id:"s1", type:"subflow", name:"Subflow", info:"",
env: [
{name: "K", type: "TT", value: "TV",
info: {
name: "K",
label: "",
value: "TV",
type: "TT",
target_type: "env var",
"target": "K"
}}
],
in:[{
x:10, y:10,
wires:[ {id:"s1-n1"} ]
}],
out:[{
x:10, y:10,
wires:[ {id:"s1-n1", port:0} ]
}]
},
{id:"s1-n1", x:10, y:10, z:"s1", type:"function",
func:"msg.V = env.get('K'); msg.T = env.get('K_type'); msg.I = env.get('K_info'); return msg;",
wires:[]}
];
helper.load(functionNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
try {
msg.should.have.property("V", "V");
msg.should.have.property("T", "T");
msg.should.have.property("I");
done();
}
catch (e) {
console.log(e);
done(e);
}
});
n1.receive({payload:"foo"});
});
});
it('should access env var of parent subflow template', function(done) {
var flow = [
{id:"t0", type:"tab", label:"", disabled:false, info:""},
{id:"n1", x:10, y:10, z:"t0", type:"subflow:s1", wires:[["n2"]]},
{id:"n2", x:10, y:10, z:"t0", type:"helper", wires:[]},
// Subflow1
{id:"s1", type:"subflow", name:"Subflow1", info:"",
env: [
{name: "K", type: "T", value: "V",
info: {
name: "K",
label: "",
value: "V",
type: "T",
target_type: "env var",
"target": "K"
}}
],
in:[{
x:10, y:10,
wires:[ {id:"s1-n1"} ]
}],
out:[{
x:10, y:10,
wires:[ {id:"s1-n2", port:0} ]
}]
},
{id:"s1-n1", x:10, y:10, z:"s1", type:"subflow:s2",
wires:[["s1-n2"]]},
{id:"s1-n2", x:10, y:10, z:"s1", type:"function",
func:"return msg;", wires:[]},
// Subflow2
{id:"s2", type:"subflow", name:"Subflow2", info:"",
in:[{
x:10, y:10,
wires:[ {id:"s2-n1"} ]
}],
out:[{
x:10, y:10,
wires:[ {id:"s2-n1", port:0} ]
}]
},
{id:"s2-n1", x:10, y:10, z:"s2", type:"function",
func:"msg.V = env.get('K'); return msg;",
wires:[]}
];
helper.load(functionNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
msg.should.have.property("V", "V");
done();
});
n1.receive({payload:"foo"});
});
});
it('should access env var of parent subflow instance', function(done) {
var flow = [
{id:"t0", type:"tab", label:"", disabled:false, info:""},
{id:"n1", x:10, y:10, z:"t0", type:"subflow:s1",
env: [
{name: "K", type: "T", value: "V",
info: {
name: "K",
label: "",
value: "V",
type: "T",
target_type: "env var",
"target": "K"
}}
],
wires:[["n2"]]},
{id:"n2", x:10, y:10, z:"t0", type:"helper", wires:[]},
// Subflow1
{id:"s1", type:"subflow", name:"Subflow1", info:"",
in:[{
x:10, y:10,
wires:[ {id:"s1-n1"} ]
}],
out:[{
x:10, y:10,
wires:[ {id:"s1-n2", port:0} ]
}]
},
{id:"s1-n1", x:10, y:10, z:"s1", type:"subflow:s2",
wires:[["s1-n2"]]},
{id:"s1-n2", x:10, y:10, z:"s1", type:"function",
func:"return msg;", wires:[]},
// Subflow2
{id:"s2", type:"subflow", name:"Subflow2", info:"",
in:[{
x:10, y:10,
wires:[ {id:"s2-n1"} ]
}],
out:[{
x:10, y:10,
wires:[ {id:"s2-n1", port:0} ]
}]
},
{id:"s2-n1", x:10, y:10, z:"s2", type:"function",
func:"msg.V = env.get('K'); return msg;",
wires:[]}
];
helper.load(functionNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
msg.should.have.property("V", "V");
done();
});
n1.receive({payload:"foo"});
});
});
});

View File

@@ -67,10 +67,12 @@ describe('Subflow', function() {
this.foo = n.foo;
this.handled = 0;
this.stopped = false;
this.received = null;
currentNodes[node.id] = node;
this.on('input',function(msg) {
// console.log(this.id,msg.payload);
node.handled++;
node.received = msg.payload;
node.send(msg);
});
this.on('close',function() {
@@ -170,6 +172,34 @@ describe('Subflow', function() {
}
util.inherits(TestAsyncNode,Node);
var TestEnvNode = function(n) {
Node.call(this,n);
this._index = createCount++;
this.scope = n.scope;
this.foo = n.foo;
var node = this;
this.stopped = false;
this.received = null;
currentNodes[node.id] = node;
this.on('input',function(msg) {
var val = node.getenv("__KEY__");
node.received = val;
node.send({payload: val});
});
this.on('close',function() {
node.stopped = true;
stoppedNodes[node.id] = node;
delete currentNodes[node.id];
});
this.__updateWires = this.updateWires;
this.updateWires = function(newWires) {
rewiredNodes[node.id] = node;
node.newWires = newWires;
node.__updateWires(newWires);
};
}
util.inherits(TestEnvNode,Node);
before(function() {
getType = sinon.stub(typeRegistry,"get",function(type) {
if (type=="test") {
@@ -178,6 +208,8 @@ describe('Subflow', function() {
return TestErrorNode;
} else if (type=="testStatus"){
return TestStatusNode;
} else if (type=="testEnv"){
return TestEnvNode;
} else {
return TestAsyncNode;
}
@@ -533,4 +565,128 @@ describe('Subflow', function() {
});
describe("#env var", function() {
it("can access process env var", function(done) {
var config = flowUtils.parseConfig([
{id:"t1",type:"tab"},
{id:"1",x:10,y:10,z:"t1",type:"test",foo:"t1.1",wires:["2"]},
{id:"2",x:10,y:10,z:"t1",type:"subflow:sf1",wires:["3"]},
{id:"3",x:10,y:10,z:"t1",type:"test",foo:"t1.3",wires:[]},
{id:"sf1",type:"subflow",name:"Subflow 2",info:"",
"in":[ {wires:[{id:"sf1-1"}]} ],
"out":[ {wires:[{id:"sf1-2",port:0}]} ]},
{id:"sf1-1",type:"test",z:"sf1",foo:"sf1.1",x:166,y:99,wires:[["sf1-2"]]},
{id:"sf1-2",type:"testEnv",z:"sf1",foo:"sf1-cn",x:166,y:99,wires:[[]]}
]);
var flow = Flow.create({
getSetting: k=> process.env[k],
handleError: (a,b,c) => { console.log(a,b,c); }
},config,config.flows["t1"]);
flow.start();
process.env["__KEY__"] = "__VAL__";
currentNodes["1"].receive({payload: "test"});
currentNodes["3"].should.have.a.property("received", "__VAL__");
flow.stop().then(function() {
done();
});
});
it("can access subflow env var", function(done) {
var config = flowUtils.parseConfig([
{id:"t1",type:"tab"},
{id:"1",x:10,y:10,z:"t1",type:"test",foo:"t1.1",wires:["2"]},
{id:"2",x:10,y:10,z:"t1",type:"subflow:sf1",wires:["3"]},
{id:"3",x:10,y:10,z:"t1",type:"test",foo:"t1.3",wires:[]},
{id:"sf1",type:"subflow",name:"Subflow 2",info:"",
"in":[ {wires:[{id:"sf1-1"}]} ],
"out":[ {wires:[{id:"sf1-2",port:0}]} ]},
{id:"sf1-1",type:"test",z:"sf1",foo:"sf1.1",x:166,y:99,wires:[["sf1-2"]]},
{id:"sf1-2",type:"testEnv",z:"sf1",foo:"sf1.2",x:166,y:99,wires:[[]]}
]);
var flow = Flow.create({
getSetting: k=> process.env[k],
handleError: (a,b,c) => { console.log(a,b,c); }
},config,config.flows["t1"]);
flow.start();
var testenv_node = null;
for (var n in currentNodes) {
var node = currentNodes[n];
if (node.type === "testEnv") {
testenv_node = node;
break;
}
}
process.env["__KEY__"] = "__VAL0__";
testenv_node.setenv("__KEY__", "__VAL1__");
currentNodes["1"].receive({payload: "test"});
currentNodes["3"].should.have.a.property("received", "__VAL1__");
flow.stop().then(function() {
done();
});
});
it("can access nested subflow env var", function(done) {
var config = flowUtils.parseConfig([
{id:"t1",type:"tab"},
{id:"1",x:10,y:10,z:"t1",type:"test",foo:"t1.1",wires:["2"]},
{id:"2",x:10,y:10,z:"t1",type:"subflow:sf1",wires:["3"]},
{id:"3",x:10,y:10,z:"t1",type:"test",foo:"t1.3",wires:[]},
{id:"sf1",type:"subflow",name:"Subflow 1",info:"",
in:[{wires:[{id:"sf1-1"}]}],
out:[{wires:[{id:"sf1-2",port:0}]}]},
{id:"sf2",type:"subflow",name:"Subflow 2",info:"",
in:[{wires:[{id:"sf2-1"}]}],
out:[{wires:[{id:"sf2-2",port:0}]}]},
{id:"sf1-1",type:"test",z:"sf1",foo:"sf1.1",x:166,y:99,wires:[["sf1-2"]]},
{id:"sf1-2",type:"subflow:sf2",z:"sf1",x:166,y:99,wires:[[]]},
{id:"sf2-1",type:"test",z:"sf2",foo:"sf2.1",x:166,y:99,wires:[["sf2-2"]]},
{id:"sf2-2",type:"testEnv",z:"sf2",foo:"sf2.2",x:166,y:99,wires:[[]]},
]);
var flow = Flow.create({
getSetting: k=> process.env[k],
handleError: (a,b,c) => { console.log(a,b,c); }
},config,config.flows["t1"]);
flow.start();
var node_sf1_1 = null;
var node_sf2_1 = null;
var testenv_node = null;
for (var n in currentNodes) {
var node = currentNodes[n];
if (node.foo === "sf1.1") {
node_sf1_1 = node;
}
if (node.foo === "sf2.1") {
node_sf2_1 = node;
}
}
process.env["__KEY__"] = "__VAL0__";
currentNodes["1"].receive({payload: "test"});
currentNodes["3"].should.have.a.property("received", "__VAL0__");
node_sf1_1.setenv("__KEY__", "__VAL1__");
currentNodes["1"].receive({payload: "test"});
currentNodes["3"].should.have.a.property("received", "__VAL1__");
node_sf2_1.setenv("__KEY__", "__VAL2__");
currentNodes["1"].receive({payload: "test"});
currentNodes["3"].should.have.a.property("received", "__VAL2__");
flow.stop().then(function() {
done();
});
});
});
});