From 42341e74f6bdb924078d628069218fbcafc44a6e Mon Sep 17 00:00:00 2001
From: Michael Kalygin <michael.kalygin@gmail.com>
Date: Sun, 25 Aug 2024 19:16:38 +0400
Subject: [PATCH] test: add e2e tests for --update-env option

---
 test/e2e.sh                        |  0
 test/e2e/cli/env-refresh.sh        | 95 ++++++++++++++++++++++++++++++
 test/fixtures/update-env.config.js | 22 +++++++
 test/fixtures/update-env.js        |  5 ++
 test/programmatic/env_switching.js |  2 +-
 5 files changed, 123 insertions(+), 1 deletion(-)
 mode change 100644 => 100755 test/e2e.sh
 create mode 100644 test/fixtures/update-env.config.js
 create mode 100644 test/fixtures/update-env.js

diff --git a/test/e2e.sh b/test/e2e.sh
old mode 100644
new mode 100755
diff --git a/test/e2e/cli/env-refresh.sh b/test/e2e/cli/env-refresh.sh
index 418c9e2f5..110f3d2c4 100644
--- a/test/e2e/cli/env-refresh.sh
+++ b/test/e2e/cli/env-refresh.sh
@@ -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"
diff --git a/test/fixtures/update-env.config.js b/test/fixtures/update-env.config.js
new file mode 100644
index 000000000..550bdaf6f
--- /dev/null
+++ b/test/fixtures/update-env.config.js
@@ -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',
+      },
+    },
+  ],
+};
diff --git a/test/fixtures/update-env.js b/test/fixtures/update-env.js
new file mode 100644
index 000000000..0654665b9
--- /dev/null
+++ b/test/fixtures/update-env.js
@@ -0,0 +1,5 @@
+setInterval(() => {
+  const { SH, PM, SH_PM } = process.env;
+
+  console.log(`SH=${SH} PM=${PM} SH_PM=${SH_PM}`);
+}, 100);
diff --git a/test/programmatic/env_switching.js b/test/programmatic/env_switching.js
index 39eaf3a62..da1a9d61f 100644
--- a/test/programmatic/env_switching.js
+++ b/test/programmatic/env_switching.js
@@ -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