Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test optimization] [SDTEST-1243] Add tag whenever the test service is provided by the user #5191

Merged
merged 9 commits into from
Feb 5, 2025
37 changes: 35 additions & 2 deletions integration-tests/cucumber/cucumber.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ const {
DI_DEBUG_ERROR_FILE_SUFFIX,
DI_DEBUG_ERROR_SNAPSHOT_ID_SUFFIX,
DI_DEBUG_ERROR_LINE_SUFFIX,
TEST_RETRY_REASON
TEST_RETRY_REASON,
DD_TEST_IS_USER_PROVIDED_SERVICE
} = require('../../packages/dd-trace/src/plugins/util/test')
const { DD_HOST_CPU_COUNT } = require('../../packages/dd-trace/src/plugins/util/env')

Expand Down Expand Up @@ -206,6 +207,7 @@ versions.forEach(version => {
assert.equal(testModuleId.toString(10), testModuleEventContent.test_module_id.toString(10))
assert.equal(testSessionId.toString(10), testSessionEventContent.test_session_id.toString(10))
assert.equal(meta[TEST_SOURCE_FILE].startsWith('ci-visibility/features'), true)
assert.equal(meta[DD_TEST_IS_USER_PROVIDED_SERVICE], 'false')
// Can read DD_TAGS
assert.propertyVal(meta, 'test.customtag', 'customvalue')
assert.propertyVal(meta, 'test.customtag2', 'customvalue2')
Expand All @@ -228,7 +230,8 @@ versions.forEach(version => {
env: {
...envVars,
DD_TAGS: 'test.customtag:customvalue,test.customtag2:customvalue2',
DD_TEST_SESSION_NAME: 'my-test-session'
DD_TEST_SESSION_NAME: 'my-test-session',
DD_SERVICE: undefined
},
stdio: 'pipe'
}
Expand Down Expand Up @@ -1996,5 +1999,35 @@ versions.forEach(version => {
})
})
})

it('sets _dd.test.is_user_provided_service to true if DD_SERVICE is used', (done) => {
const eventsPromise = receiver
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), (payloads) => {
const events = payloads.flatMap(({ payload }) => payload.events)

const tests = events.filter(event => event.type === 'test').map(event => event.content)
tests.forEach(test => {
assert.equal(test.meta[DD_TEST_IS_USER_PROVIDED_SERVICE], 'true')
})
})

childProcess = exec(
runTestsCommand,
{
cwd,
env: {
...getCiVisAgentlessConfig(receiver.port),
DD_SERVICE: 'my-service'
},
stdio: 'pipe'
}
)

childProcess.on('exit', () => {
eventsPromise.then(() => {
done()
}).catch(done)
})
})
})
})
44 changes: 42 additions & 2 deletions integration-tests/cypress/cypress.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ const {
TEST_CODE_OWNERS,
TEST_SESSION_NAME,
TEST_LEVEL_EVENT_TYPES,
TEST_RETRY_REASON
TEST_RETRY_REASON,
DD_TEST_IS_USER_PROVIDED_SERVICE
} = require('../../packages/dd-trace/src/plugins/util/test')
const { DD_HOST_CPU_COUNT } = require('../../packages/dd-trace/src/plugins/util/env')
const { ERROR_MESSAGE } = require('../../packages/dd-trace/src/constants')
Expand Down Expand Up @@ -326,6 +327,7 @@ moduleTypes.forEach(({
assert.equal(testSessionId.toString(10), testSessionEventContent.test_session_id.toString(10))
assert.equal(meta[TEST_SOURCE_FILE].startsWith('cypress/e2e/'), true)
// Can read DD_TAGS
assert.propertyVal(meta, DD_TEST_IS_USER_PROVIDED_SERVICE, 'false')
assert.propertyVal(meta, 'test.customtag', 'customvalue')
assert.propertyVal(meta, 'test.customtag2', 'customvalue2')
assert.exists(metrics[DD_HOST_CPU_COUNT])
Expand All @@ -345,7 +347,8 @@ moduleTypes.forEach(({
...restEnvVars,
CYPRESS_BASE_URL: `http://localhost:${webAppPort}`,
DD_TAGS: 'test.customtag:customvalue,test.customtag2:customvalue2',
DD_TEST_SESSION_NAME: 'my-test-session'
DD_TEST_SESSION_NAME: 'my-test-session',
DD_SERVICE: undefined
},
stdio: 'pipe'
}
Expand Down Expand Up @@ -1691,5 +1694,42 @@ moduleTypes.forEach(({
})
})
}

it('sets _dd.test.is_user_provided_service to true if DD_SERVICE is used', (done) => {
const receiverPromise = receiver
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), payloads => {
const events = payloads.flatMap(({ payload }) => payload.events)

const testEvents = events.filter(event => event.type === 'test')

testEvents.forEach(({ content: { meta } }) => {
assert.propertyVal(meta, DD_TEST_IS_USER_PROVIDED_SERVICE, 'true')
})
}, 25000)

const {
NODE_OPTIONS, // NODE_OPTIONS dd-trace config does not work with cypress
...restEnvVars
} = getCiVisEvpProxyConfig(receiver.port)

childProcess = exec(
testCommand,
{
cwd,
env: {
...restEnvVars,
CYPRESS_BASE_URL: `http://localhost:${webAppPort}`,
DD_SERVICE: 'my-service'
},
stdio: 'pipe'
}
)

childProcess.on('exit', () => {
receiverPromise.then(() => {
done()
}).catch(done)
})
})
})
})
37 changes: 35 additions & 2 deletions integration-tests/jest/jest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ const {
DI_DEBUG_ERROR_PREFIX,
DI_DEBUG_ERROR_FILE_SUFFIX,
DI_DEBUG_ERROR_SNAPSHOT_ID_SUFFIX,
DI_DEBUG_ERROR_LINE_SUFFIX
DI_DEBUG_ERROR_LINE_SUFFIX,
DD_TEST_IS_USER_PROVIDED_SERVICE
} = require('../../packages/dd-trace/src/plugins/util/test')
const { DD_HOST_CPU_COUNT } = require('../../packages/dd-trace/src/plugins/util/env')
const { ERROR_MESSAGE } = require('../../packages/dd-trace/src/constants')
Expand Down Expand Up @@ -179,6 +180,7 @@ describe('jest CommonJS', () => {
tests.forEach(testEvent => {
assert.equal(testEvent.meta[TEST_SOURCE_FILE].startsWith('ci-visibility/test/ci-visibility-test'), true)
assert.exists(testEvent.metrics[TEST_SOURCE_START])
assert.equal(testEvent.meta[DD_TEST_IS_USER_PROVIDED_SERVICE], 'false')
// Can read DD_TAGS
assert.propertyVal(testEvent.meta, 'test.customtag', 'customvalue')
assert.propertyVal(testEvent.meta, 'test.customtag2', 'customvalue2')
Expand All @@ -199,7 +201,8 @@ describe('jest CommonJS', () => {
env: {
...envVars,
DD_TAGS: 'test.customtag:customvalue,test.customtag2:customvalue2',
DD_TEST_SESSION_NAME: 'my-test-session'
DD_TEST_SESSION_NAME: 'my-test-session',
DD_SERVICE: undefined
},
stdio: 'pipe'
})
Expand Down Expand Up @@ -2905,4 +2908,34 @@ describe('jest CommonJS', () => {
})
})
})

it('sets _dd.test.is_user_provided_service to true if DD_SERVICE is used', (done) => {
const eventsPromise = receiver
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), (payloads) => {
const events = payloads.flatMap(({ payload }) => payload.events)
const tests = events.filter(event => event.type === 'test').map(event => event.content)
tests.forEach(test => {
assert.equal(test.meta[DD_TEST_IS_USER_PROVIDED_SERVICE], 'true')
})
})

childProcess = exec(
runTestsWithCoverageCommand,
{
cwd,
env: {
...getCiVisEvpProxyConfig(receiver.port),
TESTS_TO_RUN: 'test/ci-visibility-test',
DD_SERVICE: 'my-service'
},
stdio: 'inherit'
}
)

childProcess.on('exit', () => {
eventsPromise.then(() => {
done()
}).catch(done)
})
})
})
41 changes: 39 additions & 2 deletions integration-tests/mocha/mocha.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ const {
DI_DEBUG_ERROR_FILE_SUFFIX,
DI_DEBUG_ERROR_SNAPSHOT_ID_SUFFIX,
DI_DEBUG_ERROR_LINE_SUFFIX,
TEST_RETRY_REASON
TEST_RETRY_REASON,
DD_TEST_IS_USER_PROVIDED_SERVICE
} = require('../../packages/dd-trace/src/plugins/util/test')
const { DD_HOST_CPU_COUNT } = require('../../packages/dd-trace/src/plugins/util/env')
const { ERROR_MESSAGE } = require('../../packages/dd-trace/src/constants')
Expand Down Expand Up @@ -174,6 +175,7 @@ describe('mocha CommonJS', function () {
tests.forEach(testEvent => {
assert.equal(testEvent.meta[TEST_SOURCE_FILE].startsWith('ci-visibility/test/ci-visibility-test'), true)
assert.exists(testEvent.metrics[TEST_SOURCE_START])
assert.equal(testEvent.meta[DD_TEST_IS_USER_PROVIDED_SERVICE], 'false')
// Can read DD_TAGS
assert.propertyVal(testEvent.meta, 'test.customtag', 'customvalue')
assert.propertyVal(testEvent.meta, 'test.customtag2', 'customvalue2')
Expand All @@ -194,7 +196,8 @@ describe('mocha CommonJS', function () {
env: {
...envVars,
DD_TAGS: 'test.customtag:customvalue,test.customtag2:customvalue2',
DD_TEST_SESSION_NAME: 'my-test-session'
DD_TEST_SESSION_NAME: 'my-test-session',
DD_SERVICE: undefined
},
stdio: 'pipe'
})
Expand Down Expand Up @@ -2520,4 +2523,38 @@ describe('mocha CommonJS', function () {
})
})
})

it('sets _dd.test.is_user_provided_service to true if DD_SERVICE is used', (done) => {
const eventsPromise = receiver
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), (payloads) => {
const events = payloads.flatMap(({ payload }) => payload.events)

const tests = events.filter(event => event.type === 'test').map(event => event.content)
tests.forEach(test => {
assert.equal(test.meta[DD_TEST_IS_USER_PROVIDED_SERVICE], 'true')
})
})

childProcess = exec(
runTestsWithCoverageCommand,
{
cwd,
env: {
...getCiVisAgentlessConfig(receiver.port),
TESTS_TO_RUN: JSON.stringify([
'./test/ci-visibility-test.js',
'./test/ci-visibility-test-2.js'
]),
DD_SERVICE: 'my-service'
},
stdio: 'inherit'
}
)

childProcess.on('exit', () => {
eventsPromise.then(() => {
done()
}).catch(done)
})
})
})
36 changes: 34 additions & 2 deletions integration-tests/playwright/playwright.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ const {
TEST_CODE_OWNERS,
TEST_SESSION_NAME,
TEST_LEVEL_EVENT_TYPES,
TEST_RETRY_REASON
TEST_RETRY_REASON,
DD_TEST_IS_USER_PROVIDED_SERVICE
} = require('../../packages/dd-trace/src/plugins/util/test')
const { DD_HOST_CPU_COUNT } = require('../../packages/dd-trace/src/plugins/util/env')
const { ERROR_MESSAGE } = require('../../packages/dd-trace/src/constants')
Expand Down Expand Up @@ -147,6 +148,7 @@ versions.forEach((version) => {
assert.equal(
testEvent.content.meta[TEST_SOURCE_FILE].startsWith('ci-visibility/playwright-tests/'), true
)
assert.equal(testEvent.content.meta[DD_TEST_IS_USER_PROVIDED_SERVICE], 'false')
// Can read DD_TAGS
assert.propertyVal(testEvent.content.meta, 'test.customtag', 'customvalue')
assert.propertyVal(testEvent.content.meta, 'test.customtag2', 'customvalue2')
Expand Down Expand Up @@ -176,7 +178,8 @@ versions.forEach((version) => {
...envVars,
PW_BASE_URL: `http://localhost:${webAppPort}`,
DD_TAGS: 'test.customtag:customvalue,test.customtag2:customvalue2',
DD_TEST_SESSION_NAME: 'my-test-session'
DD_TEST_SESSION_NAME: 'my-test-session',
DD_SERVICE: undefined
},
stdio: 'pipe'
}
Expand Down Expand Up @@ -848,5 +851,34 @@ versions.forEach((version) => {
})
})
}

it('sets _dd.test.is_user_provided_service to true if DD_SERVICE is used', (done) => {
const receiverPromise = receiver
.gatherPayloadsMaxTimeout(({ url }) => url === '/api/v2/citestcycle', (payloads) => {
const events = payloads.flatMap(({ payload }) => payload.events)

const tests = events.filter(event => event.type === 'test').map(event => event.content)
tests.forEach(test => {
assert.equal(test.meta[DD_TEST_IS_USER_PROVIDED_SERVICE], 'true')
})
})

childProcess = exec(
'./node_modules/.bin/playwright test -c playwright.config.js',
{
cwd,
env: {
...getCiVisAgentlessConfig(receiver.port),
PW_BASE_URL: `http://localhost:${webAppPort}`,
DD_SERVICE: 'my-service'
},
stdio: 'pipe'
}
)

childProcess.on('exit', () => {
receiverPromise.then(() => done()).catch(done)
})
})
})
})
40 changes: 38 additions & 2 deletions integration-tests/vitest/vitest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ const {
DI_DEBUG_ERROR_FILE_SUFFIX,
DI_DEBUG_ERROR_SNAPSHOT_ID_SUFFIX,
DI_DEBUG_ERROR_LINE_SUFFIX,
TEST_RETRY_REASON
TEST_RETRY_REASON,
DD_TEST_IS_USER_PROVIDED_SERVICE
} = require('../../packages/dd-trace/src/plugins/util/test')
const { DD_HOST_CPU_COUNT } = require('../../packages/dd-trace/src/plugins/util/env')

Expand Down Expand Up @@ -160,6 +161,7 @@ versions.forEach((version) => {
testEvents.forEach(test => {
assert.equal(test.content.meta[TEST_COMMAND], 'vitest run')
assert.exists(test.content.metrics[DD_HOST_CPU_COUNT])
assert.equal(test.content.meta[DD_TEST_IS_USER_PROVIDED_SERVICE], 'false')
})

testSuiteEvents.forEach(testSuite => {
Expand All @@ -180,7 +182,8 @@ versions.forEach((version) => {
env: {
...getCiVisAgentlessConfig(receiver.port),
NODE_OPTIONS: '--import dd-trace/register.js -r dd-trace/ci/init', // ESM requires more flags
DD_TEST_SESSION_NAME: 'my-test-session'
DD_TEST_SESSION_NAME: 'my-test-session',
DD_SERVICE: undefined
},
stdio: 'pipe'
}
Expand Down Expand Up @@ -1298,5 +1301,38 @@ versions.forEach((version) => {
})
})
})

it('sets _dd.test.is_user_provided_service to true if DD_SERVICE is used', (done) => {
const eventsPromise = receiver
.gatherPayloadsMaxTimeout(({ url }) => url === '/api/v2/citestcycle', payloads => {
const events = payloads.flatMap(({ payload }) => payload.events)

const tests = events.filter(event => event.type === 'test').map(test => test.content)
tests.forEach(test => {
assert.equal(test.meta[DD_TEST_IS_USER_PROVIDED_SERVICE], 'true')
})
})

childProcess = exec(
'./node_modules/.bin/vitest run',
{
cwd,
env: {
...getCiVisAgentlessConfig(receiver.port),
TEST_DIR: 'ci-visibility/vitest-tests/early-flake-detection*',
NODE_OPTIONS: '--import dd-trace/register.js -r dd-trace/ci/init',
DD_SERVICE: 'my-service'
},
stdio: 'pipe'
}
)

childProcess.on('exit', (exitCode) => {
eventsPromise.then(() => {
assert.equal(exitCode, 1)
done()
}).catch(done)
})
})
})
})
Loading
Loading