Skip to content

Commit cde9361

Browse files
[test optimization] [SDTEST-1243] Add tag whenever the test service is provided by the user (#5191)
1 parent 1b7c421 commit cde9361

File tree

10 files changed

+239
-15
lines changed

10 files changed

+239
-15
lines changed

integration-tests/cucumber/cucumber.spec.js

+35-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ const {
4242
DI_DEBUG_ERROR_FILE_SUFFIX,
4343
DI_DEBUG_ERROR_SNAPSHOT_ID_SUFFIX,
4444
DI_DEBUG_ERROR_LINE_SUFFIX,
45-
TEST_RETRY_REASON
45+
TEST_RETRY_REASON,
46+
DD_TEST_IS_USER_PROVIDED_SERVICE
4647
} = require('../../packages/dd-trace/src/plugins/util/test')
4748
const { DD_HOST_CPU_COUNT } = require('../../packages/dd-trace/src/plugins/util/env')
4849

@@ -206,6 +207,7 @@ versions.forEach(version => {
206207
assert.equal(testModuleId.toString(10), testModuleEventContent.test_module_id.toString(10))
207208
assert.equal(testSessionId.toString(10), testSessionEventContent.test_session_id.toString(10))
208209
assert.equal(meta[TEST_SOURCE_FILE].startsWith('ci-visibility/features'), true)
210+
assert.equal(meta[DD_TEST_IS_USER_PROVIDED_SERVICE], 'false')
209211
// Can read DD_TAGS
210212
assert.propertyVal(meta, 'test.customtag', 'customvalue')
211213
assert.propertyVal(meta, 'test.customtag2', 'customvalue2')
@@ -228,7 +230,8 @@ versions.forEach(version => {
228230
env: {
229231
...envVars,
230232
DD_TAGS: 'test.customtag:customvalue,test.customtag2:customvalue2',
231-
DD_TEST_SESSION_NAME: 'my-test-session'
233+
DD_TEST_SESSION_NAME: 'my-test-session',
234+
DD_SERVICE: undefined
232235
},
233236
stdio: 'pipe'
234237
}
@@ -1996,5 +1999,35 @@ versions.forEach(version => {
19961999
})
19972000
})
19982001
})
2002+
2003+
it('sets _dd.test.is_user_provided_service to true if DD_SERVICE is used', (done) => {
2004+
const eventsPromise = receiver
2005+
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), (payloads) => {
2006+
const events = payloads.flatMap(({ payload }) => payload.events)
2007+
2008+
const tests = events.filter(event => event.type === 'test').map(event => event.content)
2009+
tests.forEach(test => {
2010+
assert.equal(test.meta[DD_TEST_IS_USER_PROVIDED_SERVICE], 'true')
2011+
})
2012+
})
2013+
2014+
childProcess = exec(
2015+
runTestsCommand,
2016+
{
2017+
cwd,
2018+
env: {
2019+
...getCiVisAgentlessConfig(receiver.port),
2020+
DD_SERVICE: 'my-service'
2021+
},
2022+
stdio: 'pipe'
2023+
}
2024+
)
2025+
2026+
childProcess.on('exit', () => {
2027+
eventsPromise.then(() => {
2028+
done()
2029+
}).catch(done)
2030+
})
2031+
})
19992032
})
20002033
})

integration-tests/cypress/cypress.spec.js

+42-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ const {
3737
TEST_CODE_OWNERS,
3838
TEST_SESSION_NAME,
3939
TEST_LEVEL_EVENT_TYPES,
40-
TEST_RETRY_REASON
40+
TEST_RETRY_REASON,
41+
DD_TEST_IS_USER_PROVIDED_SERVICE
4142
} = require('../../packages/dd-trace/src/plugins/util/test')
4243
const { DD_HOST_CPU_COUNT } = require('../../packages/dd-trace/src/plugins/util/env')
4344
const { ERROR_MESSAGE } = require('../../packages/dd-trace/src/constants')
@@ -326,6 +327,7 @@ moduleTypes.forEach(({
326327
assert.equal(testSessionId.toString(10), testSessionEventContent.test_session_id.toString(10))
327328
assert.equal(meta[TEST_SOURCE_FILE].startsWith('cypress/e2e/'), true)
328329
// Can read DD_TAGS
330+
assert.propertyVal(meta, DD_TEST_IS_USER_PROVIDED_SERVICE, 'false')
329331
assert.propertyVal(meta, 'test.customtag', 'customvalue')
330332
assert.propertyVal(meta, 'test.customtag2', 'customvalue2')
331333
assert.exists(metrics[DD_HOST_CPU_COUNT])
@@ -345,7 +347,8 @@ moduleTypes.forEach(({
345347
...restEnvVars,
346348
CYPRESS_BASE_URL: `http://localhost:${webAppPort}`,
347349
DD_TAGS: 'test.customtag:customvalue,test.customtag2:customvalue2',
348-
DD_TEST_SESSION_NAME: 'my-test-session'
350+
DD_TEST_SESSION_NAME: 'my-test-session',
351+
DD_SERVICE: undefined
349352
},
350353
stdio: 'pipe'
351354
}
@@ -1691,5 +1694,42 @@ moduleTypes.forEach(({
16911694
})
16921695
})
16931696
}
1697+
1698+
it('sets _dd.test.is_user_provided_service to true if DD_SERVICE is used', (done) => {
1699+
const receiverPromise = receiver
1700+
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), payloads => {
1701+
const events = payloads.flatMap(({ payload }) => payload.events)
1702+
1703+
const testEvents = events.filter(event => event.type === 'test')
1704+
1705+
testEvents.forEach(({ content: { meta } }) => {
1706+
assert.propertyVal(meta, DD_TEST_IS_USER_PROVIDED_SERVICE, 'true')
1707+
})
1708+
}, 25000)
1709+
1710+
const {
1711+
NODE_OPTIONS, // NODE_OPTIONS dd-trace config does not work with cypress
1712+
...restEnvVars
1713+
} = getCiVisEvpProxyConfig(receiver.port)
1714+
1715+
childProcess = exec(
1716+
testCommand,
1717+
{
1718+
cwd,
1719+
env: {
1720+
...restEnvVars,
1721+
CYPRESS_BASE_URL: `http://localhost:${webAppPort}`,
1722+
DD_SERVICE: 'my-service'
1723+
},
1724+
stdio: 'pipe'
1725+
}
1726+
)
1727+
1728+
childProcess.on('exit', () => {
1729+
receiverPromise.then(() => {
1730+
done()
1731+
}).catch(done)
1732+
})
1733+
})
16941734
})
16951735
})

integration-tests/jest/jest.spec.js

+35-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ const {
3939
DI_DEBUG_ERROR_PREFIX,
4040
DI_DEBUG_ERROR_FILE_SUFFIX,
4141
DI_DEBUG_ERROR_SNAPSHOT_ID_SUFFIX,
42-
DI_DEBUG_ERROR_LINE_SUFFIX
42+
DI_DEBUG_ERROR_LINE_SUFFIX,
43+
DD_TEST_IS_USER_PROVIDED_SERVICE
4344
} = require('../../packages/dd-trace/src/plugins/util/test')
4445
const { DD_HOST_CPU_COUNT } = require('../../packages/dd-trace/src/plugins/util/env')
4546
const { ERROR_MESSAGE } = require('../../packages/dd-trace/src/constants')
@@ -179,6 +180,7 @@ describe('jest CommonJS', () => {
179180
tests.forEach(testEvent => {
180181
assert.equal(testEvent.meta[TEST_SOURCE_FILE].startsWith('ci-visibility/test/ci-visibility-test'), true)
181182
assert.exists(testEvent.metrics[TEST_SOURCE_START])
183+
assert.equal(testEvent.meta[DD_TEST_IS_USER_PROVIDED_SERVICE], 'false')
182184
// Can read DD_TAGS
183185
assert.propertyVal(testEvent.meta, 'test.customtag', 'customvalue')
184186
assert.propertyVal(testEvent.meta, 'test.customtag2', 'customvalue2')
@@ -199,7 +201,8 @@ describe('jest CommonJS', () => {
199201
env: {
200202
...envVars,
201203
DD_TAGS: 'test.customtag:customvalue,test.customtag2:customvalue2',
202-
DD_TEST_SESSION_NAME: 'my-test-session'
204+
DD_TEST_SESSION_NAME: 'my-test-session',
205+
DD_SERVICE: undefined
203206
},
204207
stdio: 'pipe'
205208
})
@@ -2905,4 +2908,34 @@ describe('jest CommonJS', () => {
29052908
})
29062909
})
29072910
})
2911+
2912+
it('sets _dd.test.is_user_provided_service to true if DD_SERVICE is used', (done) => {
2913+
const eventsPromise = receiver
2914+
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), (payloads) => {
2915+
const events = payloads.flatMap(({ payload }) => payload.events)
2916+
const tests = events.filter(event => event.type === 'test').map(event => event.content)
2917+
tests.forEach(test => {
2918+
assert.equal(test.meta[DD_TEST_IS_USER_PROVIDED_SERVICE], 'true')
2919+
})
2920+
})
2921+
2922+
childProcess = exec(
2923+
runTestsWithCoverageCommand,
2924+
{
2925+
cwd,
2926+
env: {
2927+
...getCiVisEvpProxyConfig(receiver.port),
2928+
TESTS_TO_RUN: 'test/ci-visibility-test',
2929+
DD_SERVICE: 'my-service'
2930+
},
2931+
stdio: 'inherit'
2932+
}
2933+
)
2934+
2935+
childProcess.on('exit', () => {
2936+
eventsPromise.then(() => {
2937+
done()
2938+
}).catch(done)
2939+
})
2940+
})
29082941
})

integration-tests/mocha/mocha.spec.js

+39-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ const {
4141
DI_DEBUG_ERROR_FILE_SUFFIX,
4242
DI_DEBUG_ERROR_SNAPSHOT_ID_SUFFIX,
4343
DI_DEBUG_ERROR_LINE_SUFFIX,
44-
TEST_RETRY_REASON
44+
TEST_RETRY_REASON,
45+
DD_TEST_IS_USER_PROVIDED_SERVICE
4546
} = require('../../packages/dd-trace/src/plugins/util/test')
4647
const { DD_HOST_CPU_COUNT } = require('../../packages/dd-trace/src/plugins/util/env')
4748
const { ERROR_MESSAGE } = require('../../packages/dd-trace/src/constants')
@@ -174,6 +175,7 @@ describe('mocha CommonJS', function () {
174175
tests.forEach(testEvent => {
175176
assert.equal(testEvent.meta[TEST_SOURCE_FILE].startsWith('ci-visibility/test/ci-visibility-test'), true)
176177
assert.exists(testEvent.metrics[TEST_SOURCE_START])
178+
assert.equal(testEvent.meta[DD_TEST_IS_USER_PROVIDED_SERVICE], 'false')
177179
// Can read DD_TAGS
178180
assert.propertyVal(testEvent.meta, 'test.customtag', 'customvalue')
179181
assert.propertyVal(testEvent.meta, 'test.customtag2', 'customvalue2')
@@ -194,7 +196,8 @@ describe('mocha CommonJS', function () {
194196
env: {
195197
...envVars,
196198
DD_TAGS: 'test.customtag:customvalue,test.customtag2:customvalue2',
197-
DD_TEST_SESSION_NAME: 'my-test-session'
199+
DD_TEST_SESSION_NAME: 'my-test-session',
200+
DD_SERVICE: undefined
198201
},
199202
stdio: 'pipe'
200203
})
@@ -2520,4 +2523,38 @@ describe('mocha CommonJS', function () {
25202523
})
25212524
})
25222525
})
2526+
2527+
it('sets _dd.test.is_user_provided_service to true if DD_SERVICE is used', (done) => {
2528+
const eventsPromise = receiver
2529+
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), (payloads) => {
2530+
const events = payloads.flatMap(({ payload }) => payload.events)
2531+
2532+
const tests = events.filter(event => event.type === 'test').map(event => event.content)
2533+
tests.forEach(test => {
2534+
assert.equal(test.meta[DD_TEST_IS_USER_PROVIDED_SERVICE], 'true')
2535+
})
2536+
})
2537+
2538+
childProcess = exec(
2539+
runTestsWithCoverageCommand,
2540+
{
2541+
cwd,
2542+
env: {
2543+
...getCiVisAgentlessConfig(receiver.port),
2544+
TESTS_TO_RUN: JSON.stringify([
2545+
'./test/ci-visibility-test.js',
2546+
'./test/ci-visibility-test-2.js'
2547+
]),
2548+
DD_SERVICE: 'my-service'
2549+
},
2550+
stdio: 'inherit'
2551+
}
2552+
)
2553+
2554+
childProcess.on('exit', () => {
2555+
eventsPromise.then(() => {
2556+
done()
2557+
}).catch(done)
2558+
})
2559+
})
25232560
})

integration-tests/playwright/playwright.spec.js

+34-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ const {
2525
TEST_CODE_OWNERS,
2626
TEST_SESSION_NAME,
2727
TEST_LEVEL_EVENT_TYPES,
28-
TEST_RETRY_REASON
28+
TEST_RETRY_REASON,
29+
DD_TEST_IS_USER_PROVIDED_SERVICE
2930
} = require('../../packages/dd-trace/src/plugins/util/test')
3031
const { DD_HOST_CPU_COUNT } = require('../../packages/dd-trace/src/plugins/util/env')
3132
const { ERROR_MESSAGE } = require('../../packages/dd-trace/src/constants')
@@ -147,6 +148,7 @@ versions.forEach((version) => {
147148
assert.equal(
148149
testEvent.content.meta[TEST_SOURCE_FILE].startsWith('ci-visibility/playwright-tests/'), true
149150
)
151+
assert.equal(testEvent.content.meta[DD_TEST_IS_USER_PROVIDED_SERVICE], 'false')
150152
// Can read DD_TAGS
151153
assert.propertyVal(testEvent.content.meta, 'test.customtag', 'customvalue')
152154
assert.propertyVal(testEvent.content.meta, 'test.customtag2', 'customvalue2')
@@ -176,7 +178,8 @@ versions.forEach((version) => {
176178
...envVars,
177179
PW_BASE_URL: `http://localhost:${webAppPort}`,
178180
DD_TAGS: 'test.customtag:customvalue,test.customtag2:customvalue2',
179-
DD_TEST_SESSION_NAME: 'my-test-session'
181+
DD_TEST_SESSION_NAME: 'my-test-session',
182+
DD_SERVICE: undefined
180183
},
181184
stdio: 'pipe'
182185
}
@@ -848,5 +851,34 @@ versions.forEach((version) => {
848851
})
849852
})
850853
}
854+
855+
it('sets _dd.test.is_user_provided_service to true if DD_SERVICE is used', (done) => {
856+
const receiverPromise = receiver
857+
.gatherPayloadsMaxTimeout(({ url }) => url === '/api/v2/citestcycle', (payloads) => {
858+
const events = payloads.flatMap(({ payload }) => payload.events)
859+
860+
const tests = events.filter(event => event.type === 'test').map(event => event.content)
861+
tests.forEach(test => {
862+
assert.equal(test.meta[DD_TEST_IS_USER_PROVIDED_SERVICE], 'true')
863+
})
864+
})
865+
866+
childProcess = exec(
867+
'./node_modules/.bin/playwright test -c playwright.config.js',
868+
{
869+
cwd,
870+
env: {
871+
...getCiVisAgentlessConfig(receiver.port),
872+
PW_BASE_URL: `http://localhost:${webAppPort}`,
873+
DD_SERVICE: 'my-service'
874+
},
875+
stdio: 'pipe'
876+
}
877+
)
878+
879+
childProcess.on('exit', () => {
880+
receiverPromise.then(() => done()).catch(done)
881+
})
882+
})
851883
})
852884
})

integration-tests/vitest/vitest.spec.js

+38-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ const {
3030
DI_DEBUG_ERROR_FILE_SUFFIX,
3131
DI_DEBUG_ERROR_SNAPSHOT_ID_SUFFIX,
3232
DI_DEBUG_ERROR_LINE_SUFFIX,
33-
TEST_RETRY_REASON
33+
TEST_RETRY_REASON,
34+
DD_TEST_IS_USER_PROVIDED_SERVICE
3435
} = require('../../packages/dd-trace/src/plugins/util/test')
3536
const { DD_HOST_CPU_COUNT } = require('../../packages/dd-trace/src/plugins/util/env')
3637

@@ -160,6 +161,7 @@ versions.forEach((version) => {
160161
testEvents.forEach(test => {
161162
assert.equal(test.content.meta[TEST_COMMAND], 'vitest run')
162163
assert.exists(test.content.metrics[DD_HOST_CPU_COUNT])
164+
assert.equal(test.content.meta[DD_TEST_IS_USER_PROVIDED_SERVICE], 'false')
163165
})
164166

165167
testSuiteEvents.forEach(testSuite => {
@@ -180,7 +182,8 @@ versions.forEach((version) => {
180182
env: {
181183
...getCiVisAgentlessConfig(receiver.port),
182184
NODE_OPTIONS: '--import dd-trace/register.js -r dd-trace/ci/init', // ESM requires more flags
183-
DD_TEST_SESSION_NAME: 'my-test-session'
185+
DD_TEST_SESSION_NAME: 'my-test-session',
186+
DD_SERVICE: undefined
184187
},
185188
stdio: 'pipe'
186189
}
@@ -1298,5 +1301,38 @@ versions.forEach((version) => {
12981301
})
12991302
})
13001303
})
1304+
1305+
it('sets _dd.test.is_user_provided_service to true if DD_SERVICE is used', (done) => {
1306+
const eventsPromise = receiver
1307+
.gatherPayloadsMaxTimeout(({ url }) => url === '/api/v2/citestcycle', payloads => {
1308+
const events = payloads.flatMap(({ payload }) => payload.events)
1309+
1310+
const tests = events.filter(event => event.type === 'test').map(test => test.content)
1311+
tests.forEach(test => {
1312+
assert.equal(test.meta[DD_TEST_IS_USER_PROVIDED_SERVICE], 'true')
1313+
})
1314+
})
1315+
1316+
childProcess = exec(
1317+
'./node_modules/.bin/vitest run',
1318+
{
1319+
cwd,
1320+
env: {
1321+
...getCiVisAgentlessConfig(receiver.port),
1322+
TEST_DIR: 'ci-visibility/vitest-tests/early-flake-detection*',
1323+
NODE_OPTIONS: '--import dd-trace/register.js -r dd-trace/ci/init',
1324+
DD_SERVICE: 'my-service'
1325+
},
1326+
stdio: 'pipe'
1327+
}
1328+
)
1329+
1330+
childProcess.on('exit', (exitCode) => {
1331+
eventsPromise.then(() => {
1332+
assert.equal(exitCode, 1)
1333+
done()
1334+
}).catch(done)
1335+
})
1336+
})
13011337
})
13021338
})

0 commit comments

Comments
 (0)