fix: stringify possible passed object via env + fix unit test retry strategy #4080 #4079

This commit is contained in:
Unitech 2018-12-19 15:04:28 +01:00
parent 5b633f0f52
commit f0c465db55
5 changed files with 88 additions and 10 deletions

View File

@ -534,17 +534,18 @@ Common.mergeEnvironmentVariables = function(app_env, env_name, deploy_conf) {
env : {}
}
// Stringify possible object
for (var key in app.env) {
if (typeof app.env[key] == 'object') {
app.env[key] = JSON.stringify(app.env[key]);
}
}
/**
* Extra configuration update
*/
util._extend(new_conf, app)
for (var key in app.env) {
if (typeof app.env[key] == 'object') {
new_conf[key] = JSON.stringify(app.env[key]);
}
}
if (env_name) {
// First merge variables from deploy.production.env object as least priority.
if (deploy_conf && deploy_conf[env_name] && deploy_conf[env_name]['env']) {

View File

@ -0,0 +1,4 @@
setInterval(function() {
console.log(process.env.JSONTEST);
}, 50);

View File

@ -0,0 +1,22 @@
module.exports = {
apps : [{
script: 'echo.js',
env: {
JSONTEST: { si: 'si' }
},
env_production: {
NODE_ENV: 'production'
}
}],
deploy : {
production : {
user : 'node',
host : '212.83.163.1',
ref : 'origin/master',
repo : 'git@github.com:repo.git',
path : '/var/www/production',
'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env production'
}
}
};

View File

@ -0,0 +1,48 @@
var PM2 = require('../../..');
var should = require('should');
describe('Programmatic log feature test', function() {
var proc1 = null;
var procs = [];
var pm2 = new PM2.custom({
cwd : __dirname + '/../fixtures/json-env-passing'
});
before(function(done) {
pm2.delete('all', function() {
done();
});
});
after(function(done) {
pm2.delete('all', function() {
pm2.disconnect(done);
});
});
it('should start a process with object as environment variable', function(done) {
pm2.start({
script: 'echo.js',
env: {
NORMAL: 'STR',
JSONTEST: { si: 'si' }
},
env_production: {
NODE_ENV: 'production'
}
}, function(err, procs) {
should(err).be.null()
should(procs.length).eql(1)
done()
})
})
it('should retrieve environment variable stringified', function(done) {
pm2.list((err, procs) => {
should(procs[0].pm2_env.JSONTEST).eql('{"si":"si"}')
should(procs[0].pm2_env.NORMAL).eql('STR')
done()
})
})
})

View File

@ -10,8 +10,9 @@ function reset {
}
function runUnitTest {
echo "[~] Starting test $1"
START=$(date +%s)
mocha --exit --opts ./mocha.opts $1
mocha --exit --bail --opts ./mocha.opts $1
RET=$?
if [ $RET -ne 0 ];
@ -21,12 +22,12 @@ function runUnitTest {
echo $STR >> unit_time
reset
mocha --exit --opts ./mocha.opts $1
mocha --bail --exit --opts ./mocha.opts $1
RET=$?
if [ $RET -ne 0 ];
then
echo -e "######## \033[31m ✘ $1\033[0m"
echo -e "######## TEST ✘ $1 FAILED TWICE!!"
exit 1
fi
fi
@ -49,7 +50,7 @@ cd test/programmatic
# Abort script at first error
set -e
# set -e
runUnitTest ./programmatic.js
runUnitTest ./instances.mocha.js
@ -80,6 +81,8 @@ runUnitTest ./configuration.mocha.js
runUnitTest ./id.mocha.js
runUnitTest ./god.mocha.js
runUnitTest ./issues/json_env_passing_4080.mocha.js
cd ../interface
runUnitTest ./bus.spec.mocha.js