refactor + fix bug watch_delay on json + add user mgmt tests + change tests order/remove toxic flag

This commit is contained in:
Unitech 2018-09-23 19:28:13 +02:00
parent 8c99e439a4
commit cb093237a7
21 changed files with 124 additions and 98 deletions

View File

@ -649,7 +649,7 @@ class API {
/**
* Commander.js tricks
*/
var app_conf = Config.transCMDToConf(opts);
var app_conf = Config.filterOptions(opts);
var appConf = {};
var ignoreFileArray = [];
@ -857,10 +857,12 @@ class API {
var apps_info = [];
var that = this;
/**
* Get File configuration
*/
if (typeof(cb) === 'undefined' && typeof(pipe) === 'function') {
cb = pipe;
}
if (typeof(file) === 'object') {
config = file;
} else if (pipe === 'pipe') {
@ -889,18 +891,19 @@ class API {
}
}
/**
* Alias some optional fields
*/
if (config.deploy)
deployConf = config.deploy;
if (config.apps)
appConf = config.apps;
else if (config.pm2)
appConf = config.pm2;
else
appConf = config;
if (!Array.isArray(appConf))
appConf = [appConf]; //convert to array
appConf = [appConf];
if ((appConf = Common.verifyConfs(appConf)) instanceof Error)
return cb ? cb(appConf) : that.exitCli(conf.ERROR_EXIT);
@ -1421,7 +1424,7 @@ class API {
* (nodeArgs -> node_args)
*/
_handleAttributeUpdate (opts) {
var conf = Config.transCMDToConf(opts);
var conf = Config.filterOptions(opts);
var that = this;
if (typeof(conf.name) != 'string')

View File

@ -157,6 +157,11 @@
"docDefault": "True",
"docDescription": "Enable or disable auto restart after process failure"
},
"watch_delay": {
"type": "number",
"docDefault": "True",
"docDescription": "Restart delay on file change detected"
},
"watch": {
"type": [
"boolean",

View File

@ -627,7 +627,7 @@ Common.verifyConfs = function(appConfs) {
* Checks + Resolve UID/GID
* comes from pm2 --uid <> --gid <> or --user
*/
if ((app.uid || app.gid) && app.force !== true) {
if ((app.uid || app.gid || app.user) && app.force !== true) {
// 1/ Check if windows
if (cst.IS_WINDOWS === true) {
Common.printError(cst.PREFIX_MSG_ERR + '--uid and --git does not works on windows');
@ -635,10 +635,10 @@ Common.verifyConfs = function(appConfs) {
}
// 2/ Verify that user is root (todo: verify if other has right)
// if (process.getuid && process.getuid() !== 0) {
// Common.printError(cst.PREFIX_MSG_ERR + 'To use --uid and --gid please run pm2 as root');
// return new Error('To use UID and GID please run PM2 as root');
// }
if (process.env.NODE_ENV != 'test' && process.getuid && process.getuid() !== 0) {
Common.printError(cst.PREFIX_MSG_ERR + 'To use --uid and --gid please run pm2 as root');
return new Error('To use UID and GID please run PM2 as root');
}
// 3/ Resolve user info via /etc/password
var passwd = require('./tools/passwd.js')
@ -650,10 +650,10 @@ Common.verifyConfs = function(appConfs) {
return new Error(e);
}
var user_info = users[app.uid]
var user_info = users[app.uid || app.user]
if (!user_info) {
Common.printError(`${cst.PREFIX_MSG_ERR} User ${app.uid} cannot be found`);
return new Error(`${cst.PREFIX_MSG_ERR} User ${app.uid} cannot be found`);
Common.printError(`${cst.PREFIX_MSG_ERR} User ${app.uid || app.user} cannot be found`);
return new Error(`${cst.PREFIX_MSG_ERR} User ${app.uid || app.user} cannot be found`);
}
app.env.HOME = user_info.homedir

View File

@ -56,21 +56,21 @@ var Config = module.exports = {
};
/**
* Transform commander options to app config.
* @param {Commander} cmd
* @returns {{}}
* Filter / Alias options
*/
Config.transCMDToConf = function(cmd){
var conf = {}, defines = this.schema;
// Wrap.
for(var k in defines){
var aliases = defines[k].alias;
Config.filterOptions = function(cmd) {
var conf = {};
var schema = this.schema;
for (var key in schema) {
var aliases = schema[key].alias;
aliases && aliases.forEach(function(alias){
//if (cmd[alias]) {
conf[k] || (conf[k] = cmd[alias]);
//}
if (typeof(cmd[alias]) !== 'undefined') {
conf[key] || (conf[key] = cmd[alias]);
}
});
}
return conf;
};

View File

@ -91,7 +91,7 @@
"main": "index.js",
"types": "types/index.d.ts",
"scripts": {
"test": "bash test/e2e.sh && bash test/unit.sh"
"test": "bash test/unit.sh && bash test/e2e.sh"
},
"keywords": [
"cli",

View File

@ -7,14 +7,6 @@ source "${SRC}/e2e/include.sh"
set -e
set -o verbose
# MODULES
bash ./test/e2e/modules/get-set.sh
spec "Configuration system working"
bash ./test/e2e/modules/module.sh
spec "module system"
bash ./test/e2e/modules/module-safeguard.sh
spec "module safeguard system (--safe)"
# CLI
bash ./test/e2e/cli/reload.sh
spec "Reload"
@ -141,4 +133,12 @@ spec "Logging path set to null"
bash ./test/e2e/logs/log-json.sh
spec "Logging directly to file in json"
# MODULES
bash ./test/e2e/modules/get-set.sh
spec "Configuration system working"
bash ./test/e2e/modules/module.sh
spec "module system"
bash ./test/e2e/modules/module-safeguard.sh
spec "module safeguard system (--safe)"
$pm2 kill

View File

@ -1,7 +0,0 @@
var http = require('http');
http.createServer(function(req, res) {
res.writeHead(200);
res.end('hey');
}).listen(8010);
console.log("edit")

View File

@ -250,12 +250,11 @@ describe('API checks', function() {
var pm2;
after(function(done) {
pm2.destroy(done);
pm2.kill(done);
});
it('should create new custom PM2 instance', function() {
pm2 = new PM2.custom({
independent : true,
daemon_mode : true
});
should.exists(pm2.pm2_home);
@ -291,12 +290,11 @@ describe('API checks', function() {
var pm2;
after(function(done) {
pm2.destroy(done);
pm2.kill(done);
});
it('should create new custom PM2 instance', function() {
pm2 = new PM2.custom({
independent : true,
daemon_mode : false
});

View File

@ -10,12 +10,11 @@ process.chdir(__dirname);
describe('Cluster programmatic tests', function() {
var pm2 = new PM2.custom({
cwd : '../fixtures',
independent : true
cwd : '../fixtures'
});
after(function(done) {
pm2.destroy(done)
pm2.kill(done)
});
describe('Start with different instances number parameter', function() {
@ -150,6 +149,7 @@ describe('Cluster programmatic tests', function() {
pm2.start({
script : './echo.js',
listen_timeout : 1000,
exec_mode: 'cluster',
instances : 1,
name : 'echo'
}, done);
@ -163,7 +163,7 @@ describe('Cluster programmatic tests', function() {
});
});
it('should take listen timeout into account', function(done) {
it('should take listen_timeout into account', function(done) {
var called = false;
var plan = new Plan(3, done);

View File

@ -8,12 +8,11 @@ describe('Modules programmatic testing', function() {
var pm2;
after(function(done) {
pm2.destroy(done);
pm2.kill(done);
});
it('should instanciate PM2', function() {
pm2 = new PM2.custom({
independent : true,
cwd : '../fixtures'
});
});

View File

@ -49,10 +49,10 @@ describe('PM2 programmatic calls', function() {
var procs = [];
var bus = null;
var pm2 = new PM2.custom({ independent : true });
var pm2 = new PM2.custom({ });
after(function(done) {
pm2.destroy(done);
pm2.kill(done);
});
before(function(done) {

View File

@ -14,12 +14,11 @@ describe('Wait ready / Graceful start / restart', function() {
process.exit(0);
var pm2 = new PM2.custom({
cwd : '../fixtures/listen-timeout/',
independent : true
cwd : '../fixtures/listen-timeout/'
});
after(function(done) {
pm2.destroy(done)
pm2.kill(done)
});
describe('(FORK) Listen timeout feature', function() {

View File

@ -4,12 +4,11 @@ var should = require('should');
describe('Call PM2 inside PM2', function() {
var pm2 = new PM2.custom({
independent : true,
cwd : __dirname + '/../fixtures/inside'
});
after(function(done) {
pm2.destroy(function(){
pm2.kill(function(){
done();
});
});

View File

@ -12,12 +12,11 @@ describe('Max memory restart programmatic', function() {
var proc1 = null;
var procs = [];
var pm2 = new PM2.custom({
cwd : __dirname + '/../fixtures/json-reload/',
independent : true
cwd : __dirname + '/../fixtures/json-reload/'
});
after(function(done) {
pm2.destroy(done)
pm2.kill(done)
});
afterEach(function(done) {

View File

@ -9,12 +9,11 @@ var cst = require('../../constants.js');
describe('Misc commands', function() {
var pm2 = new PM2.custom({
independent : true,
cwd : __dirname + '/../fixtures'
});
after(function(done) {
pm2.destroy(done);
pm2.kill(done);
});
before(function(done) {

View File

@ -18,7 +18,6 @@ describe('Modules programmatic testing', function() {
it('should instanciate PM2', function() {
pm2 = new PM2.custom({
//independent : true,
//daemon_mode : true
});

View File

@ -6,12 +6,11 @@ describe('Modules programmatic testing', function() {
var pm2;
after(function(done) {
pm2.destroy(done);
pm2.kill(done);
});
it('should instanciate PM2', function() {
pm2 = new PM2.custom({
independent : true,
daemon_mode : true
});
});
@ -26,8 +25,7 @@ describe('Modules programmatic testing', function() {
});
});
it('should run post install command', function(done)
{
it('should run post install command', function(done) {
var fs = require('fs');
var ec = {};
ec.dependencies = new Array();
@ -51,30 +49,6 @@ describe('Modules programmatic testing', function() {
});
});
it('should install (update) a module with uid option', function(done) {
pm2.install('pm2-server-monit', {
uid : process.env.USER
}, function(err, apps) {
should(err).eql(null);
should(apps.length).eql(1);
var pm2_env = apps[0].pm2_env;
should.exist(pm2_env);
should(pm2_env.uid).eql(process.env.USER);
done();
});
});
it('should have uid option via pm2 list', function(done) {
pm2.list(function(err, apps) {
should(err).eql(null);
should(apps.length).eql(1);
var pm2_env = apps[0].pm2_env;
should.exist(pm2_env);
should(pm2_env.uid).eql(process.env.USER);
done();
});
});
it('should uninstall all modules', function(done) {
pm2.uninstall('all', function(err, apps) {
done();

View File

@ -7,13 +7,12 @@ describe('Signal kill (+delayed)', function() {
var proc1 = null;
var pm2 = new PM2.custom({
independent : true,
cwd : __dirname + '/../fixtures'
});
after(function(done) {
pm2.delete('all', function(err, ret) {
pm2.destroy(done);
pm2.kill(done);
});
});

View File

@ -0,0 +1,60 @@
process.env.NODE_ENV = 'test'
process.chdir(__dirname);
var PM2 = require('../..');
var should = require('should');
describe('User management', function() {
before(function(done) {
PM2.delete('all', function() { done() });
});
after(function(done) {
PM2.kill(done);
});
it('should fail with unknown user', function(done) {
PM2.start('./../fixtures/child.js', {
user: 'toto'
},function(err) {
should(err.message).match(/cannot be found/)
PM2.list(function(err, list) {
should(err).be.null();
should(list.length).eql(0);
done();
});
});
})
it('should succeed with known user', function(done) {
PM2.start('./../fixtures/child.js', {
user: process.env.USER
},function(err) {
should(err).be.null();
PM2.list(function(err, list) {
should(err).be.null();
should(list.length).eql(1);
should.exist(list[0].pm2_env.uid)
should.exist(list[0].pm2_env.gid)
PM2.delete('all', done)
});
});
})
it('should succeed with known user via uid field', function(done) {
PM2.start('./../fixtures/child.js', {
uid: process.env.USER
},function(err) {
should(err).be.null();
PM2.list(function(err, list) {
should(err).be.null();
should.exist(list[0].pm2_env.uid)
should.exist(list[0].pm2_env.gid)
should(list.length).eql(1);
PM2.delete('all', done)
});
});
})
})

View File

@ -47,7 +47,6 @@ function errShouldBeNull(err) {
describe('Watcher', function() {
var pm2 = new PM2.custom({
independent : true,
cwd : __dirname + '/../fixtures/watcher'
});

View File

@ -33,9 +33,6 @@ $pm2 uninstall all
# fi
cd test/programmatic
mocha --exit --opts ./mocha.opts ./god.mocha.js
spec "God test"
mocha --exit --opts ./mocha.opts ./programmatic.js
spec "Programmatic test"
@ -90,6 +87,10 @@ spec "Configuration system working"
mocha --exit --opts ./mocha.opts ./id.mocha.js
spec "Uniqueness id for each process"
mocha --exit --opts ./mocha.opts ./god.mocha.js
spec "God test"
#
# Interface testing
#