Skip to content

Commit

Permalink
test: add e2e tests for --update-env option
Browse files Browse the repository at this point in the history
  • Loading branch information
mkalygin committed Aug 25, 2024
1 parent 83ea160 commit 42341e7
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 1 deletion.
Empty file modified test/e2e.sh
100644 → 100755
Empty file.
95 changes: 95 additions & 0 deletions test/e2e/cli/env-refresh.sh
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,98 @@ $pm2 kill
$pm2 l
NODE_PATH='/test2' $pm2 start local_require.js -i 1
should 'should have loaded the right globalPaths' 'restart_time: 0' 1

#
# Ensuring that environment update works correctly when reloading with JSON config.
#
# Related issue:
# https://github.com/Unitech/pm2/issues/3192
#

# start with config
SH=shell_initial SH_PM=shell_initial $pm2 start update-env.config.js --env initial
>out-env.log

sleep 0.5
grep "SH=shell_initial PM=pm2_initial SH_PM=pm2_initial" out-env.log &> /dev/null
spec "should inject shell environment, then inject config environment on start with config"

# restart config without --update-env
$pm2 delete all
SH=shell_initial SH_PM=shell_initial $pm2 start update-env.config.js --env initial
SH=shell_updated SH_PM=shell_updated $pm2 restart update-env.config.js --env updated
>out-env.log

sleep 0.5
grep "SH=shell_updated PM=pm2_updated SH_PM=pm2_updated" out-env.log &> /dev/null
spec "should inject shell environment, then inject config environment on restart with config and without --update-env option"

# reload config without --update-env
$pm2 delete all
SH=shell_initial SH_PM=shell_initial $pm2 start update-env.config.js --env initial
SH=shell_updated SH_PM=shell_updated $pm2 reload update-env.config.js --env updated
>out-env.log

sleep 0.5
grep "SH=shell_updated PM=pm2_updated SH_PM=pm2_updated" out-env.log &> /dev/null
spec "should inject shell environment, then inject config environment on reload with config and without --update-env option"

# restart config with --update-env
$pm2 delete all
SH=shell_initial SH_PM=shell_initial $pm2 start update-env.config.js --env initial
SH=shell_updated SH_PM=shell_updated $pm2 restart update-env.config.js --env updated --update-env
>out-env.log

sleep 0.5
grep "SH=shell_updated PM=pm2_updated SH_PM=pm2_updated" out-env.log &> /dev/null
spec "should inject shell environment, then inject config environment on restart with config and with --update-env option"

# reload config with --update-env
$pm2 delete all
SH=shell_initial SH_PM=shell_initial $pm2 start update-env.config.js --env initial
SH=shell_updated SH_PM=shell_updated $pm2 reload update-env.config.js --env updated --update-env
>out-env.log

sleep 0.5
grep "SH=shell_updated PM=pm2_updated SH_PM=pm2_updated" out-env.log &> /dev/null
spec "should inject shell environment, then inject config environment on reload with config and with --update-env option"

# restart pid without --update-env
$pm2 delete all
SH=shell_initial SH_PM=shell_initial $pm2 start update-env.config.js --env initial
SH=shell_updated SH_PM=shell_updated $pm2 restart update_env_app
>out-env.log

sleep 0.5
grep "SH=shell_initial PM=pm2_initial SH_PM=pm2_initial" out-env.log &> /dev/null
spec "should keep environment on restart with pid and without --update-env option"

# reload pid without --update-env
$pm2 delete all
SH=shell_initial SH_PM=shell_initial $pm2 start update-env.config.js --env initial
SH=shell_updated SH_PM=shell_updated $pm2 reload update_env_app
>out-env.log

sleep 0.5
grep "SH=shell_initial PM=pm2_initial SH_PM=pm2_initial" out-env.log &> /dev/null
spec "should keep environment on reload with pid and without --update-env option"

# restart pid with --update-env
$pm2 delete all
SH=shell_initial SH_PM=shell_initial $pm2 start update-env.config.js --env initial
SH=shell_updated SH_PM=shell_updated $pm2 restart update_env_app --update-env
>out-env.log

sleep 0.5
grep "SH=shell_updated PM=pm2_initial SH_PM=shell_updated" out-env.log &> /dev/null
spec "should inject shell environment on restart with pid and with --update-env option"

# reload pid with --update-env
$pm2 delete all
SH=shell_initial SH_PM=shell_initial $pm2 start update-env.config.js --env initial
SH=shell_updated SH_PM=shell_updated $pm2 reload update_env_app --update-env
>out-env.log

sleep 0.5
grep "SH=shell_updated PM=pm2_initial SH_PM=shell_updated" out-env.log &> /dev/null
spec "should inject shell environment on reload with pid and with --update-env option"
22 changes: 22 additions & 0 deletions test/fixtures/update-env.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = {
apps: [
{
name: 'update_env_app',
script: './update-env.js',
instances: 2,
exec_mode: 'cluster',
out_file: 'out-env.log',
merge_logs: true,
env_initial: {
NODE_ENV: 'test',
PM: 'pm2_initial',
SH_PM: 'pm2_initial',
},
env_updated: {
NODE_ENV: 'test',
PM: 'pm2_updated',
SH_PM: 'pm2_updated',
},
},
],
};
5 changes: 5 additions & 0 deletions test/fixtures/update-env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
setInterval(() => {
const { SH, PM, SH_PM } = process.env;

console.log(`SH=${SH} PM=${PM} SH_PM=${SH_PM}`);
}, 100);
2 changes: 1 addition & 1 deletion test/programmatic/env_switching.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe('PM2 programmatic calls', function() {
});

/**
* Ensuring that environment update works correct when reloading with JSON config.
* Ensuring that environment update works correctly when reloading with JSON config.
*
* Related issue:
* https://github.com/Unitech/pm2/issues/3192
Expand Down

0 comments on commit 42341e7

Please sign in to comment.