Skip to content

Commit 83ea160

Browse files
committed
test: add unit tests for --update-env option
1 parent 38d7782 commit 83ea160

File tree

2 files changed

+111
-8
lines changed

2 files changed

+111
-8
lines changed

test/programmatic/env_switching.js

Lines changed: 111 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,117 @@ describe('PM2 programmatic calls', function() {
130130
});
131131
});
132132

133-
// it('should start a script and NODE_ENV have right value', function(done) {
134-
// pm2.start(json_declaration_simple, function(err, data) {
135-
// proc1 = data[0];
136-
// should(err).be.null;
137-
// proc1.pm2_env['NODE_ENV'].should.eql(json_declaration.env.NODE_ENV);
138-
// done();
139-
// });
140-
// });
133+
/**
134+
* Ensuring that environment update works correct when reloading with JSON config.
135+
*
136+
* Related issue:
137+
* https://github.com/Unitech/pm2/issues/3192
138+
*/
139+
describe('with updateEnv option', () => {
140+
const env = {
141+
shell: {
142+
initial: {
143+
SH: 'shell_initial',
144+
SH_PM: 'shell_initial',
145+
},
146+
updated: {
147+
SH: 'shell_updated',
148+
SH_PM: 'shell_updated',
149+
},
150+
},
151+
pm2: {
152+
initial: {
153+
PM: 'pm2_initial',
154+
SH_PM: 'pm2_initia',
155+
},
156+
updated: {
157+
PM: 'pm2_updated',
158+
SH_PM: 'pm2_updated',
159+
},
160+
},
161+
};
162+
163+
const configInitial = {
164+
name: 'child-update-env',
165+
script: './../fixtures/env-switching/child.js',
166+
instances: '2',
167+
env: {
168+
NODE_ENV: 'test',
169+
...env.pm2.initial,
170+
},
171+
};
172+
173+
const configUpdated = {
174+
...configInitial,
175+
env: {
176+
NODE_ENV: 'test',
177+
...env.pm2.updated,
178+
},
179+
};
180+
181+
it('should inject shell environment, then inject config environment on start', (done) => {
182+
Object.assign(process.env, env.shell.initial);
183+
184+
pm2.start(configInitial, (err, data) => {
185+
try {
186+
const pm2Env = data[0] ? data[0].pm2_env || {} : {};
187+
should(err).be.null();
188+
should(pm2Env.SH).eql(env.shell.initial.SH);
189+
should(pm2Env.PM).eql(env.pm2.initial.PM);
190+
should(pm2Env.SH_PM).eql(env.pm2.initial.SH_PM);
191+
done();
192+
} catch (err) {
193+
done(err);
194+
}
195+
});
196+
});
197+
198+
it('should inject only config environment on restart when disabled', (done) => {
199+
Object.assign(process.env, env.shell.updated);
200+
201+
pm2.restart(configUpdated, { updateEnv: false }, (err) => {
202+
should(err).be.null();
203+
204+
pm2.list((err, data) => {
205+
try {
206+
const pm2Env = data.find(proc => proc.name === configInitial.name).pm2_env;
207+
should(err).be.null();
208+
should(pm2Env.SH).eql(env.shell.initial.SH);
209+
should(pm2Env.PM).eql(env.pm2.updated.PM);
210+
should(pm2Env.SH_PM).eql(env.pm2.updated.SH_PM);
211+
done();
212+
} catch (err) {
213+
done(err);
214+
}
215+
});
216+
});
217+
});
141218

219+
it('should inject shell environment, then inject config environment on start when endabled', (done) => {
220+
Object.assign(process.env, env.shell.updated);
221+
222+
pm2.restart(configUpdated, { updateEnv: true }, (err) => {
223+
should(err).be.null();
224+
225+
pm2.list((err, data) => {
226+
try {
227+
const pm2Env = data.find(proc => proc.name === configInitial.name).pm2_env;
228+
should(err).be.null();
229+
should(pm2Env.SH).eql(env.shell.updated.SH);
230+
should(pm2Env.PM).eql(env.pm2.updated.PM);
231+
should(pm2Env.SH_PM).eql(env.pm2.updated.SH_PM);
232+
done();
233+
} catch (err) {
234+
done(err);
235+
}
236+
});
237+
});
238+
});
142239

240+
it('should delete all processes', (done) => {
241+
pm2.delete('all', (err, ret) => {
242+
done();
243+
});
244+
});
245+
});
143246
});

test/unit.sh

100644100755
File mode changed.

0 commit comments

Comments
 (0)