From cb093237a7bd92549b439f02e8a08a89b750f2ef Mon Sep 17 00:00:00 2001 From: Unitech Date: Sun, 23 Sep 2018 19:28:13 +0200 Subject: [PATCH] refactor + fix bug watch_delay on json + add user mgmt tests + change tests order/remove toxic flag --- lib/API.js | 15 +++-- lib/API/schema.json | 5 ++ lib/Common.js | 16 ++--- lib/tools/Config.js | 22 +++---- package.json | 2 +- test/e2e.sh | 16 ++--- test/fixtures/watcher/server-watch.js | 7 --- test/programmatic/api.mocha.js | 6 +- test/programmatic/cluster.mocha.js | 8 +-- test/programmatic/conf_update.mocha.js | 3 +- test/programmatic/env_switching.js | 4 +- test/programmatic/graceful.mocha.js | 5 +- test/programmatic/inside.mocha.js | 3 +- test/programmatic/max_memory_limit.js | 5 +- test/programmatic/misc_commands.js | 3 +- test/programmatic/module_retrocompat.mocha.js | 1 - test/programmatic/modules.mocha.js | 30 +--------- test/programmatic/signals.js | 3 +- test/programmatic/user_management.mocha.js | 60 +++++++++++++++++++ test/programmatic/watcher.js | 1 - test/unit.sh | 7 ++- 21 files changed, 124 insertions(+), 98 deletions(-) delete mode 100644 test/fixtures/watcher/server-watch.js create mode 100644 test/programmatic/user_management.mocha.js diff --git a/lib/API.js b/lib/API.js index a391faa6..1982e50f 100644 --- a/lib/API.js +++ b/lib/API.js @@ -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') diff --git a/lib/API/schema.json b/lib/API/schema.json index 58e43bdb..ea6469ca 100644 --- a/lib/API/schema.json +++ b/lib/API/schema.json @@ -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", diff --git a/lib/Common.js b/lib/Common.js index 50a7d089..49921090 100644 --- a/lib/Common.js +++ b/lib/Common.js @@ -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 diff --git a/lib/tools/Config.js b/lib/tools/Config.js index 4059e101..08869b85 100644 --- a/lib/tools/Config.js +++ b/lib/tools/Config.js @@ -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; }; diff --git a/package.json b/package.json index eb2af292..01feb0f3 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/e2e.sh b/test/e2e.sh index fab86a06..791b374c 100644 --- a/test/e2e.sh +++ b/test/e2e.sh @@ -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 diff --git a/test/fixtures/watcher/server-watch.js b/test/fixtures/watcher/server-watch.js deleted file mode 100644 index 7d1c9d7f..00000000 --- a/test/fixtures/watcher/server-watch.js +++ /dev/null @@ -1,7 +0,0 @@ -var http = require('http'); - -http.createServer(function(req, res) { - res.writeHead(200); - res.end('hey'); -}).listen(8010); -console.log("edit") \ No newline at end of file diff --git a/test/programmatic/api.mocha.js b/test/programmatic/api.mocha.js index 47c137d8..bdbf99fb 100644 --- a/test/programmatic/api.mocha.js +++ b/test/programmatic/api.mocha.js @@ -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 }); diff --git a/test/programmatic/cluster.mocha.js b/test/programmatic/cluster.mocha.js index dbe6af7d..3eefaac9 100644 --- a/test/programmatic/cluster.mocha.js +++ b/test/programmatic/cluster.mocha.js @@ -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); diff --git a/test/programmatic/conf_update.mocha.js b/test/programmatic/conf_update.mocha.js index 8ec781af..b1bf8465 100644 --- a/test/programmatic/conf_update.mocha.js +++ b/test/programmatic/conf_update.mocha.js @@ -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' }); }); diff --git a/test/programmatic/env_switching.js b/test/programmatic/env_switching.js index 0f0c4309..560d33a1 100644 --- a/test/programmatic/env_switching.js +++ b/test/programmatic/env_switching.js @@ -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) { diff --git a/test/programmatic/graceful.mocha.js b/test/programmatic/graceful.mocha.js index 01d8c0d1..8d7d669a 100644 --- a/test/programmatic/graceful.mocha.js +++ b/test/programmatic/graceful.mocha.js @@ -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() { diff --git a/test/programmatic/inside.mocha.js b/test/programmatic/inside.mocha.js index f25addff..ff809470 100644 --- a/test/programmatic/inside.mocha.js +++ b/test/programmatic/inside.mocha.js @@ -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(); }); }); diff --git a/test/programmatic/max_memory_limit.js b/test/programmatic/max_memory_limit.js index a5d8d0e6..d8e2117b 100644 --- a/test/programmatic/max_memory_limit.js +++ b/test/programmatic/max_memory_limit.js @@ -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) { diff --git a/test/programmatic/misc_commands.js b/test/programmatic/misc_commands.js index ac80e5b8..43065a14 100644 --- a/test/programmatic/misc_commands.js +++ b/test/programmatic/misc_commands.js @@ -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) { diff --git a/test/programmatic/module_retrocompat.mocha.js b/test/programmatic/module_retrocompat.mocha.js index 3c8ac5f7..632e7634 100644 --- a/test/programmatic/module_retrocompat.mocha.js +++ b/test/programmatic/module_retrocompat.mocha.js @@ -18,7 +18,6 @@ describe('Modules programmatic testing', function() { it('should instanciate PM2', function() { pm2 = new PM2.custom({ - //independent : true, //daemon_mode : true }); diff --git a/test/programmatic/modules.mocha.js b/test/programmatic/modules.mocha.js index 32363d7f..7ac68c43 100644 --- a/test/programmatic/modules.mocha.js +++ b/test/programmatic/modules.mocha.js @@ -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(); diff --git a/test/programmatic/signals.js b/test/programmatic/signals.js index fad65206..c4f7e07d 100644 --- a/test/programmatic/signals.js +++ b/test/programmatic/signals.js @@ -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); }); }); diff --git a/test/programmatic/user_management.mocha.js b/test/programmatic/user_management.mocha.js new file mode 100644 index 00000000..db5fbaa7 --- /dev/null +++ b/test/programmatic/user_management.mocha.js @@ -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) + }); + }); + }) +}) diff --git a/test/programmatic/watcher.js b/test/programmatic/watcher.js index 41d96e63..67df7fe7 100644 --- a/test/programmatic/watcher.js +++ b/test/programmatic/watcher.js @@ -47,7 +47,6 @@ function errShouldBeNull(err) { describe('Watcher', function() { var pm2 = new PM2.custom({ - independent : true, cwd : __dirname + '/../fixtures/watcher' }); diff --git a/test/unit.sh b/test/unit.sh index 1fb7143d..d9a2a716 100644 --- a/test/unit.sh +++ b/test/unit.sh @@ -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 #