Skip to content
This repository was archived by the owner on Jun 2, 2022. It is now read-only.

Commit 800766c

Browse files
committed
chore: run tests on random port
1 parent 3d90fb9 commit 800766c

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

demo/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ if (process.env.SNYK_TRIGGER_EXTRA_VULN) {
2121
// create a server with a known vulnerability
2222
const http = require('http');
2323
const st = require('st');
24-
const PORT = process.env.PORT || 3000;
24+
const ENV_PORT = process.env.PORT;
25+
const PORT = ENV_PORT !== undefined ? ENV_PORT : 3000;
2526

2627
const server = http.createServer(
2728
st({
@@ -31,6 +32,7 @@ const server = http.createServer(
3132
})
3233
);
3334

34-
server.listen(PORT, () => console.log(`Demo server started, hit http://localhost:${PORT}/hello.txt to try it`));
35+
server.listen(PORT, () => console.log(
36+
`Demo server started, hit http://localhost:${server.address().port}/hello.txt to try it`));
3537

3638
module.exports = server;

test/disable.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ test('agent can be disabled', async (t) => {
1414
process.env.SNYK_HOMEBASE_URL = 'http://localhost:7000/api/v1/beacon';
1515
process.env.SNYK_BEACON_INTERVAL_MS = BEACON_INTERVAL_MS;
1616
process.env.SNYK_RUNTIME_AGENT_DISABLE = 'yes please';
17+
// 0: let the OS pick a free port
18+
process.env.PORT = 0;
1719

1820
// bring up the demo server
1921
const demoApp = require('../demo');
22+
const port = demoApp.address().port;
2023

2124
// wait to let the agent go through a cycle
2225
await sleep(BEACON_INTERVAL_MS);
2326

2427
// trigger the vuln method
25-
await needle.get('http://localhost:3000/hello.txt');
28+
await needle.get(`http://localhost:${port}/hello.txt`);
2629

2730
// wait to let the agent go through a cycle
2831
await sleep(BEACON_INTERVAL_MS);
@@ -33,6 +36,7 @@ test('agent can be disabled', async (t) => {
3336
delete process.env.SNYK_HOMEBASE_URL;
3437
delete process.env.SNYK_BEACON_INTERVAL_MS;
3538
delete process.env.SNYK_RUNTIME_AGENT_DISABLE;
39+
delete process.env.PORT;
3640

3741
await new Promise((resolve) => demoApp.close(resolve));
3842
});

test/e2e.test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,18 @@ test('demo app reports a vuln method when called', async (t) => {
150150
process.env.SNYK_BEACON_INTERVAL_MS = BEACON_INTERVAL_MS;
151151
process.env.SNYK_SNAPSHOT_INTERVAL_MS = SNAPSHOT_INTERVAL_MS;
152152
process.env.SNYK_TRIGGER_EXTRA_VULN = true;
153+
// 0: let the OS pick a free port
154+
process.env.PORT = 0;
153155

154156
// bring up the demo server
155157
const demoApp = require('../demo');
158+
const port = demoApp.address().port;
156159

157160
// wait to let the agent go through a cycle
158161
await sleep(BEACON_INTERVAL_MS);
159162

160163
// trigger the vuln method
161-
await needle.get('http://localhost:3000/hello.txt');
164+
await needle.get(`http://localhost:${port}/hello.txt`);
162165

163166
// wait to let the agent go through a cycle
164167
await sleep(BEACON_INTERVAL_MS);
@@ -167,7 +170,7 @@ test('demo app reports a vuln method when called', async (t) => {
167170
await sleep(SNAPSHOT_INTERVAL_MS - BEACON_INTERVAL_MS * 2);
168171

169172
// trigger the vuln method again
170-
await needle.get('http://localhost:3000/hello.txt');
173+
await needle.get(`http://localhost:${port}/hello.txt`);
171174

172175
// wait to let the agent go through another cycle with a new snapshot
173176
await sleep(BEACON_INTERVAL_MS);
@@ -182,6 +185,7 @@ test('demo app reports a vuln method when called', async (t) => {
182185
delete process.env.SNYK_BEACON_INTERVAL_MS;
183186
delete process.env.SNYK_SNAPSHOT_INTERVAL_MS;
184187
delete process.env.SNYK_TRIGGER_EXTRA_VULN;
188+
delete process.env.PORT;
185189

186190
await new Promise((resolve) => demoApp.close(resolve));
187191
});

test/failures.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ test('node agent does not crash the demo app', async (t) => {
77
const BEACON_INTERVAL_MS = 1000;
88
process.env.SNYK_HOMEBASE_URL = 'http://localhost:8000/api/v1/beacon';
99
process.env.SNYK_BEACON_INTERVAL_MS = BEACON_INTERVAL_MS;
10+
// 0: let the OS pick a free port
11+
process.env.PORT = 0;
1012

1113
// bring up the demo server, will fail on periodic tasks
1214
const demoApp = proxyquire('../demo', {
@@ -24,6 +26,7 @@ test('node agent does not crash the demo app', async (t) => {
2426

2527
delete process.env.SNYK_HOMEBASE_URL;
2628
delete process.env.SNYK_BEACON_INTERVAL_MS;
29+
delete process.env.PORT;
2730

2831
await new Promise((resolve) => demoApp.close(resolve));
2932
});
@@ -33,6 +36,8 @@ test('node agent does not crash the demo app', async (t) => {
3336
process.env.SNYK_HOMEBASE_ORIGIN = 'http://localhost:-1';
3437
process.env.SNYK_BEACON_INTERVAL_MS = BEACON_INTERVAL_MS;
3538
process.env.SNYK_SNAPSHOT_INTERVAL_MS = 200;
39+
// 0: let the OS pick a free port
40+
process.env.PORT = 0;
3641

3742
// bring up the demo server, will fail on any outgoing request
3843
const demoApp = require('../demo');
@@ -43,6 +48,7 @@ test('node agent does not crash the demo app', async (t) => {
4348
delete process.env.SNYK_HOMEBASE_ORIGIN;
4449
delete process.env.SNYK_SNAPSHOT_INTERVAL_MS;
4550
delete process.env.SNYK_BEACON_INTERVAL_MS;
51+
delete process.env.PORT;
4652

4753
await new Promise((resolve) => demoApp.close(resolve));
4854
});

0 commit comments

Comments
 (0)