Skip to content

Commit

Permalink
fix: #3688 test adaptation + pm2 serve --port option
Browse files Browse the repository at this point in the history
  • Loading branch information
Unitech committed May 27, 2018
1 parent bb094b4 commit f024968
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 29 deletions.
5 changes: 3 additions & 2 deletions bin/pm2
Original file line number Diff line number Diff line change
Expand Up @@ -929,9 +929,10 @@ commander.command('deepUpdate')
//
commander.command('serve [path] [port]')
.alias('expose')
.option('--port [port]', 'specify port to listen to')
.description('serve a directory over http via port')
.action(function (path, port) {
pm2.serve(path, port, commander);
.action(function (path, port, cmd) {
pm2.serve(path, port || cmd.port, commander);
});

commander.command('examples')
Expand Down
3 changes: 2 additions & 1 deletion lib/Common.js
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,8 @@ Common.verifyConfs = function(appConfs){
// Check Exec mode
checkExecMode(app);

if (app.exec_mode != 'cluster_mode' && !app.instances)
if (app.exec_mode != 'cluster_mode' &&
!app.instances && typeof(app.merge_logs) == 'undefined')
app.merge_logs = true;

// Render an app name if not existing.
Expand Down
29 changes: 15 additions & 14 deletions test/e2e/cli/serve.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,51 @@ SRC=$(cd $(dirname "$0"); pwd)
source "${SRC}/../include.sh"

cd $file_path/serve

PORT=8081
PORT_2=8082
echo "################## PM2 SERVE ###################"

$pm2 serve
$pm2 serve --port $PORT
should 'should have started serving dir' 'online' 1

curl http://localhost:8080/ > /tmp/tmp_out.txt
curl http://localhost:$PORT/ > /tmp/tmp_out.txt
OUT=`cat /tmp/tmp_out.txt | grep -o "good shit" | wc -l`
[ $OUT -eq 1 ] || fail "should have served index file under /"
success "should have served index file under /"

curl http://localhost:8080/index.html > /tmp/tmp_out.txt
curl http://localhost:$PORT/index.html > /tmp/tmp_out.txt
OUT=`cat /tmp/tmp_out.txt | grep -o "good shit" | wc -l`
[ $OUT -eq 1 ] || fail "should have served index file under /index.html"
success "should have served index file under /index.html"

echo "Shutting down the server"
$pm2 delete all

curl http://localhost:8080/index.html > /tmp/tmp_out.txt
curl http://localhost:$PORT/index.html > /tmp/tmp_out.txt
OUT=`cat /tmp/tmp_out.txt | grep -o "good shit" | wc -l`
[ $OUT -eq 0 ] || fail "should be offline"
success "should be offline"

$pm2 serve . 8000
$pm2 serve . $PORT_2
should 'should have started serving dir' 'online' 1

curl http://localhost:8000/index.html > /tmp/tmp_out.txt
curl http://localhost:$PORT_2/index.html > /tmp/tmp_out.txt
OUT=`cat /tmp/tmp_out.txt | grep -o "good shit" | wc -l`
[ $OUT -eq 1 ] || fail "should be listening on port 8000"
success "should be listening on port 8000"
[ $OUT -eq 1 ] || fail "should be listening on port $PORT_2"
success "should be listening on port $PORT_2"

$pm2 delete all

$pm2 serve . 8000 --name frontend
$pm2 serve . $PORT_2 --name frontend
should 'should have started serving dir' 'online' 1
should 'should have custom name' 'frontend' 7

curl http://localhost:8000/index.html > /tmp/tmp_out.txt
curl http://localhost:$PORT_2/index.html > /tmp/tmp_out.txt
OUT=`cat /tmp/tmp_out.txt | grep -o "good shit" | wc -l`
[ $OUT -eq 1 ] || fail "should be listening on port 8000"
success "should be listening on port 8000"
[ $OUT -eq 1 ] || fail "should be listening on port $PORT_2"
success "should be listening on port $PORT_2"

curl http://localhost:8000/yolo.html > /tmp/tmp_out.txt
curl http://localhost:$PORT_2/yolo.html > /tmp/tmp_out.txt
OUT=`cat /tmp/tmp_out.txt | grep -o "your file doesnt exist" | wc -l`
[ $OUT -eq 1 ] || fail "should have served custom 404 file"
success "should have served custom 404 file"
Expand Down
10 changes: 5 additions & 5 deletions test/e2e/misc/cron-system.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ spec "Should cron restart echo.js"

$pm2 delete all

> mock-0.log
> mock.log

$pm2 start cron/mock-cron.js -o mock.log
sleep 3
should 'should app been restarted when cron in fork mode' 'restart_time: 0' 0
cat mock-0.log | grep "SIGINT"
cat mock.log | grep "SIGINT"
spec "1# Should cron exit call SIGINT handler"

$pm2 delete all

$pm2 start cron/mock-cron.js -o mock.log -i 1
sleep 3
should 'should app been restarted when cron in cluster mode' 'restart_time: 0' 0
cat mock-0.log | grep "SIGINT"
cat mock.log | grep "SIGINT"
spec "2# Should cron exit call SIGINT handler"

$pm2 delete all
Expand All @@ -38,7 +38,7 @@ $pm2 delete all
$pm2 start cron/mock-cron-no-exit.js -o mock.log
sleep 3
should 'should app been restarted' 'restart_time: 0' 0
cat mock-0.log | grep "SIGINT"
cat mock.log | grep "SIGINT"
spec "3# Should cron exit call SIGINT handler"


Expand All @@ -52,7 +52,7 @@ spec "Should cron restart delayed sigint"

sleep 100

cat cron-0.log | grep "SIGINT cb called"
cat cron.log | grep "SIGINT cb called"
spec "Should cron exit call SIGINT handler"

should 'should app been restarted' 'restart_time: 1' 1
6 changes: 3 additions & 3 deletions test/e2e/misc/inside-pm2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ echo -e "\033[1mRunning tests:\033[0m"
####################################################################

TEST_VARIABLE='hello1' $pm2 start startProcessInsidePm2.json
>inside-out-1.log
>inside-out.log

sleep 1

Expand All @@ -25,12 +25,12 @@ $pm2 list
should 'child process should be started' 'pm_id: 1' 2
should 'restarted status should be zero' "restart_time: 0" 2

grep "hello1" inside-out-1.log &> /dev/null
grep "hello1" inside-out.log &> /dev/null
spec "Child should have hello1 variable"

TEST_VARIABLE='hello2' $pm2 restart "insideProcess" --update-env
sleep 1
grep "hello2" inside-out-1.log &> /dev/null
grep "hello2" inside-out.log &> /dev/null
spec "Child should have hello2 variable after restart"

# Call bash script that restarts app
Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/path-resolution/ecosystem3.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
/**
* Application configuration section
* http://pm2.keymetrics.io/docs/usage/application-declaration/
*/
apps : [
{
name : "test-cluster",
script : "./echo.js",
out_file : 'echo-out.log',
error_file : 'echo-err.log',
instances: 4
}
]
}
3 changes: 2 additions & 1 deletion test/programmatic/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ describe('Programmatic log feature test', function() {
pm2.start({
script: './echo.js',
error_file : 'error-echo.log',
out_file : 'out-echo.log'
out_file : 'out-echo.log',
merge_logs: false
}, function(err, procs) {
should(err).be.null();

Expand Down
19 changes: 16 additions & 3 deletions test/programmatic/path_resolution.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var PM2 = require('../..');
var should = require('should');

describe('Path resolution in configuration file', function() {
this.timeout(4000)
before(function(done) {
PM2.delete('all', function() { done() } );
});
Expand All @@ -16,19 +17,31 @@ describe('Path resolution in configuration file', function() {

it('should resolve paths (home)', function(done) {
PM2.start('./path-resolution/ecosystem.config.js', function(err, proc) {
should(proc[0].pm2_env.pm_err_log_path).eql(path.join(process.env.HOME, 'echo-err-0.log'));
should(proc[0].pm2_env.pm_out_log_path).eql(path.join(process.env.HOME, 'echo-out-0.log'));
should(proc[0].pm2_env.pm_err_log_path).eql(path.join(process.env.HOME, 'echo-err.log'));
should(proc[0].pm2_env.pm_out_log_path).eql(path.join(process.env.HOME, 'echo-out.log'));
should(proc[0].pm2_env.pm_pid_path).eql(path.join(process.env.HOME, 'echo-pid.log'));
done();
});
});

it('should resolve paths (local)', function(done) {
PM2.start('./path-resolution/ecosystem2.config.js', function(err, proc) {
should(proc[0].pm2_env.pm_err_log_path).eql(path.join(process.cwd(), 'echo-err.log'));
should(proc[0].pm2_env.pm_out_log_path).eql(path.join(process.cwd(), 'echo-out.log'));
should(proc[0].pm2_env.pm_pid_path).eql(path.join(process.cwd(), 'echo-pid.log'));
done();
});
});

it('should auto prefix log path on cluster mode', function(done) {
PM2.start('./path-resolution/ecosystem3.config.js', function(err, proc) {
should(proc[0].pm2_env.pm_err_log_path).eql(path.join(process.cwd(), 'echo-err-0.log'));
should(proc[0].pm2_env.pm_out_log_path).eql(path.join(process.cwd(), 'echo-out-0.log'));
should(proc[0].pm2_env.pm_pid_path).eql(path.join(process.cwd(), 'echo-pid.log'));

should(proc[1].pm2_env.pm_err_log_path).eql(path.join(process.cwd(), 'echo-err-1.log'));
should(proc[1].pm2_env.pm_out_log_path).eql(path.join(process.cwd(), 'echo-out-1.log'));
done();
});
});

});

0 comments on commit f024968

Please sign in to comment.