Skip to content

Commit 38f67f3

Browse files
authored
test: reduce usage of get-port dependency (#5848)
1 parent b7e4f8e commit 38f67f3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+166
-215
lines changed

integration-tests/appsec/data-collection.spec.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const { assert } = require('chai')
4-
const getPort = require('get-port')
54
const path = require('path')
65
const Axios = require('axios')
76

@@ -12,16 +11,12 @@ const {
1211
} = require('../helpers')
1312

1413
describe('ASM Data collection', () => {
15-
let axios, sandbox, cwd, appPort, appFile, agent, proc
14+
let axios, sandbox, cwd, appFile, agent, proc
1615

1716
before(async () => {
1817
sandbox = await createSandbox(['express'])
19-
appPort = await getPort()
2018
cwd = sandbox.folder
2119
appFile = path.join(cwd, 'appsec/data-collection/index.js')
22-
axios = Axios.create({
23-
baseURL: `http://localhost:${appPort}`
24-
})
2520
})
2621

2722
after(async () => {
@@ -33,7 +28,6 @@ describe('ASM Data collection', () => {
3328
agent = await new FakeAgent().start()
3429

3530
const env = {
36-
APP_PORT: appPort,
3731
DD_TRACE_AGENT_PORT: agent.port,
3832
DD_APPSEC_ENABLED: true,
3933
DD_APPSEC_RULES: path.join(cwd, 'appsec', 'data-collection', 'data-collection-rules.json')
@@ -46,6 +40,7 @@ describe('ASM Data collection', () => {
4640
}
4741

4842
proc = await spawnProc(appFile, { cwd, env, execArgv: [] })
43+
axios = Axios.create({ baseURL: proc.url })
4944
})
5045

5146
afterEach(async () => {

integration-tests/appsec/data-collection/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ tracer.init({
77
const express = require('express')
88

99
const app = express()
10-
const port = process.env.APP_PORT || 3000
1110

1211
app.get('/', (req, res) => {
1312
// Content headers
@@ -22,6 +21,6 @@ app.get('/', (req, res) => {
2221
res.end('end')
2322
})
2423

25-
app.listen(port, () => {
26-
process.send({ port })
24+
const server = app.listen(process.env.APP_PORT || 0, () => {
25+
process.send?.({ port: server.address().port })
2726
})

integration-tests/appsec/esm-app/index.mjs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import Module from 'node:module'
66
import './worker.mjs'
77

88
const app = express()
9-
const port = process.env.APP_PORT || 3000
109

1110
app.get('/cmdi-vulnerable', (req, res) => {
1211
childProcess.execSync(`ls ${req.query.args}`)
@@ -16,8 +15,8 @@ app.get('/cmdi-vulnerable', (req, res) => {
1615

1716
app.use('/more', (await import('./more.mjs')).default)
1817

19-
app.listen(port, () => {
20-
process.send({ port })
18+
const server = app.listen(process.env.APP_PORT || 0, () => {
19+
process.send?.({ port: server.address().port })
2120
})
2221

2322
Module.register('./custom-noop-hooks.mjs', {

integration-tests/appsec/esm-security-controls/index.mjs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import sanitizeDefault from './sanitizer-default.mjs'
77
import { validate, validateNotConfigured } from './validator.mjs'
88

99
const app = express()
10-
const port = process.env.APP_PORT || 3000
1110

1211
app.get('/cmdi-s-secure', (req, res) => {
1312
const command = sanitize(req.query.command)
@@ -64,6 +63,6 @@ app.get('/cmdi-iv-secure', (req, res) => {
6463
res.end()
6564
})
6665

67-
app.listen(port, () => {
68-
process.send({ port })
66+
const server = app.listen(process.env.APP_PORT || 0, () => {
67+
process.send?.({ port: server.address().port })
6968
})

integration-tests/appsec/graphql.spec.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict'
22

3-
const getPort = require('get-port')
43
const { assert } = require('chai')
54
const path = require('path')
65
const axios = require('axios')
@@ -12,22 +11,20 @@ const {
1211
} = require('../helpers')
1312

1413
describe('graphql', () => {
15-
let sandbox, cwd, agent, webFile, proc, appPort
14+
let sandbox, cwd, agent, webFile, proc
1615

1716
before(async function () {
1817
sandbox = await createSandbox(['@apollo/server', 'graphql', 'koalas'])
1918
cwd = sandbox.folder
2019
webFile = path.join(cwd, 'graphql/index.js')
21-
appPort = await getPort()
2220
})
2321

2422
beforeEach(async () => {
2523
agent = await new FakeAgent().start()
2624
proc = await spawnProc(webFile, {
2725
cwd,
2826
env: {
29-
AGENT_PORT: agent.port,
30-
APP_PORT: appPort
27+
AGENT_PORT: agent.port
3128
}
3229
})
3330
})
@@ -54,7 +51,7 @@ describe('graphql', () => {
5451
})
5552

5653
await axios({
57-
url: `http://localhost:${appPort}/graphql`,
54+
url: `${proc.url}/graphql`,
5855
method: 'post',
5956
headers: {
6057
'Content-type': 'application/json'
@@ -116,7 +113,7 @@ describe('graphql', () => {
116113
})
117114

118115
await axios({
119-
url: `http://localhost:${appPort}/graphql`,
116+
url: `${proc.url}/graphql`,
120117
method: 'post',
121118
headers: {
122119
'Content-type': 'application/json'

integration-tests/appsec/iast.esm-security-controls.spec.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,17 @@
22

33
const { createSandbox, spawnProc, FakeAgent } = require('../helpers')
44
const path = require('path')
5-
const getPort = require('get-port')
65
const Axios = require('axios')
76
const { assert } = require('chai')
87

98
describe('ESM Security controls', () => {
10-
let axios, sandbox, cwd, appPort, appFile, agent, proc
9+
let axios, sandbox, cwd, appFile, agent, proc
1110

1211
before(async function () {
1312
this.timeout(process.platform === 'win32' ? 90000 : 30000)
1413
sandbox = await createSandbox(['express@4']) // TODO: Remove pinning once our tests support Express v5
15-
appPort = await getPort()
1614
cwd = sandbox.folder
1715
appFile = path.join(cwd, 'appsec', 'esm-security-controls', 'index.mjs')
18-
19-
axios = Axios.create({
20-
baseURL: `http://localhost:${appPort}`
21-
})
2216
})
2317

2418
after(async function () {
@@ -34,7 +28,6 @@ describe('ESM Security controls', () => {
3428
cwd,
3529
env: {
3630
DD_TRACE_AGENT_PORT: agent.port,
37-
APP_PORT: appPort,
3831
DD_IAST_ENABLED: 'true',
3932
DD_IAST_REQUEST_SAMPLING: '100',
4033
// eslint-disable-next-line no-multi-str
@@ -45,6 +38,8 @@ describe('ESM Security controls', () => {
4538
NODE_OPTIONS: nodeOptions
4639
}
4740
})
41+
42+
axios = Axios.create({ baseURL: proc.url })
4843
})
4944

5045
afterEach(async () => {

integration-tests/appsec/iast.esm.spec.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,17 @@
22

33
const { createSandbox, spawnProc, FakeAgent } = require('../helpers')
44
const path = require('path')
5-
const getPort = require('get-port')
65
const Axios = require('axios')
76
const { assert } = require('chai')
87

98
describe('ESM', () => {
10-
let axios, sandbox, cwd, appPort, appFile, agent, proc
9+
let axios, sandbox, cwd, appFile, agent, proc
1110

1211
before(async function () {
1312
this.timeout(process.platform === 'win32' ? 90000 : 30000)
1413
sandbox = await createSandbox(['express'])
15-
appPort = await getPort()
1614
cwd = sandbox.folder
1715
appFile = path.join(cwd, 'appsec', 'esm-app', 'index.mjs')
18-
19-
axios = Axios.create({
20-
baseURL: `http://localhost:${appPort}`
21-
})
2216
})
2317

2418
after(async function () {
@@ -39,12 +33,13 @@ describe('ESM', () => {
3933
cwd,
4034
env: {
4135
DD_TRACE_AGENT_PORT: agent.port,
42-
APP_PORT: appPort,
4336
DD_IAST_ENABLED: 'true',
4437
DD_IAST_REQUEST_SAMPLING: '100',
4538
NODE_OPTIONS: nodeOptions
4639
}
4740
})
41+
42+
axios = Axios.create({ baseURL: proc.url })
4843
})
4944

5045
afterEach(async () => {

integration-tests/appsec/index.spec.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
'use strict'
22

3-
const getPort = require('get-port')
43
const path = require('path')
54
const Axios = require('axios')
65
const { assert } = require('chai')
76
const msgpack = require('@msgpack/msgpack')
87
const { createSandbox, FakeAgent, spawnProc } = require('../helpers')
98

109
describe('RASP', () => {
11-
let axios, sandbox, cwd, appPort, appFile, agent, proc, stdioHandler
10+
let axios, sandbox, cwd, appFile, agent, proc, stdioHandler
1211

1312
function stdOutputHandler (data) {
1413
stdioHandler && stdioHandler(data)
1514
}
1615

1716
before(async () => {
1817
sandbox = await createSandbox(['express', 'axios'])
19-
appPort = await getPort()
2018
cwd = sandbox.folder
2119
appFile = path.join(cwd, 'appsec/rasp/index.js')
22-
axios = Axios.create({
23-
baseURL: `http://localhost:${appPort}`
24-
})
2520
})
2621

2722
after(async () => {
@@ -40,13 +35,13 @@ describe('RASP', () => {
4035
execArgv,
4136
env: {
4237
DD_TRACE_AGENT_PORT: agent.port,
43-
APP_PORT: appPort,
4438
DD_APPSEC_ENABLED: true,
4539
DD_APPSEC_RASP_ENABLED: true,
4640
DD_APPSEC_RULES: path.join(cwd, 'appsec/rasp/rasp_rules.json'),
4741
DD_APPSEC_RASP_COLLECT_REQUEST_BODY: collectRequestBody
4842
}
4943
}, stdOutputHandler, stdOutputHandler)
44+
axios = Axios.create({ baseURL: proc.url })
5045
})
5146

5247
afterEach(async () => {

integration-tests/debugger/re-evaluation.spec.js

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
'use strict'
22

3-
const { EOL } = require('node:os')
4-
const { spawn } = require('node:child_process')
53
const { randomUUID } = require('node:crypto')
64
const assert = require('node:assert')
75

8-
const getPort = require('get-port')
96
const Axios = require('axios')
107

11-
const { createSandbox, FakeAgent, assertObjectContains } = require('../helpers')
8+
const { createSandbox, FakeAgent, assertObjectContains, spawnProc } = require('../helpers')
129
const { generateProbeConfig } = require('../../packages/dd-trace/test/debugger/devtools_client/utils')
1310

1411
// A race condition exists where the tracer receives a probe via RC, before Node.js has had a chance to load all the JS
@@ -50,48 +47,22 @@ describe('Dynamic Instrumentation Probe Re-Evaluation', function () {
5047

5148
function genTestsForSourceFile (sourceFile) {
5249
return function () {
53-
let rcConfig, appPort, agent, proc, axios
50+
let rcConfig, agent, proc, axios
5451

5552
beforeEach(async function () {
5653
rcConfig = {
5754
product: 'LIVE_DEBUGGING',
5855
id: `logProbe_${randomUUID()}`,
5956
config: generateProbeConfig({ sourceFile, line: 4 })
6057
}
61-
appPort = await getPort()
6258
agent = await new FakeAgent().start()
63-
proc = spawn(
64-
process.execPath,
65-
['--import', 'dd-trace/initialize.mjs', sourceFile],
66-
{
67-
cwd: sandbox.folder,
68-
env: {
69-
APP_PORT: appPort,
70-
DD_DYNAMIC_INSTRUMENTATION_ENABLED: true,
71-
DD_TRACE_AGENT_PORT: agent.port,
72-
DD_TRACE_DEBUG: process.env.DD_TRACE_DEBUG // inherit to make debugging the sandbox easier
73-
}
74-
}
75-
)
76-
proc
77-
.on('exit', (code) => {
78-
if (code !== 0) {
79-
throw new Error(`Child process exited with code ${code}`)
80-
}
81-
})
82-
.on('error', (error) => {
83-
throw error
84-
})
85-
proc.stdout.on('data', log.bind(null, '[child process stdout]'))
86-
proc.stderr.on('data', log.bind(null, '[child process stderr]'))
87-
axios = Axios.create({
88-
baseURL: `http://localhost:${appPort}`
89-
})
59+
proc = undefined
9060
})
9161

9262
afterEach(async function () {
9363
proc?.kill(0)
9464
await agent?.stop()
65+
axios = undefined
9566
})
9667

9768
for (let attempt = 1; attempt <= 5; attempt++) {
@@ -138,13 +109,24 @@ describe('Dynamic Instrumentation Probe Re-Evaluation', function () {
138109
})
139110

140111
agent.addRemoteConfig(rcConfig)
112+
113+
spawnProc(sourceFile, {
114+
cwd: sandbox.folder,
115+
env: {
116+
NODE_OPTIONS: '--import dd-trace/initialize.mjs',
117+
DD_DYNAMIC_INSTRUMENTATION_ENABLED: 'true',
118+
DD_TRACE_AGENT_PORT: agent.port,
119+
DD_TRACE_DEBUG: process.env.DD_TRACE_DEBUG, // inherit to make debugging the sandbox easier
120+
DD_REMOTE_CONFIG_POLL_INTERVAL_SECONDS: 0.1
121+
}
122+
}).then(_proc => {
123+
proc = _proc
124+
// Possible race condition, in case axios.get() is called in the test before it's created here. But we have
125+
// to start the test quickly in order to test the re-evaluation of the probe.
126+
axios = Axios.create({ baseURL: proc.url })
127+
})
141128
})
142129
}
143130
}
144131
}
145132
})
146-
147-
function log (prefix, data) {
148-
const msg = data.toString().trim().split(EOL).map((line) => `${prefix} ${line}`).join(EOL)
149-
console.log(msg) // eslint-disable-line no-console
150-
}

integration-tests/debugger/source-map-support.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('Dynamic Instrumentation', function () {
1717
t.agent.on('debugger-input', ({ payload: [{ debugger: { snapshot: { probe: { location } } } }] }) => {
1818
assert.deepEqual(location, {
1919
file: 'target-app/source-map-support/typescript.ts',
20-
lines: ['10']
20+
lines: ['11']
2121
})
2222
done()
2323
})

0 commit comments

Comments
 (0)