Skip to content

Commit

Permalink
better e2e testing
Browse files Browse the repository at this point in the history
  • Loading branch information
juan-fernandez committed Oct 8, 2024
1 parent a033fb3 commit 1857aed
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 29 deletions.
55 changes: 44 additions & 11 deletions integration-tests/automatic-log-submission.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,43 @@ describe('test visibility automatic log submission', () => {
testFrameworks.forEach(({ name, command }) => {
context(`with ${name}`, () => {
it('can automatically submit logs', (done) => {
const receiverPromise = receiver
let logIds, testIds

const logsPromise = receiver
.gatherPayloadsMaxTimeout(({ url }) => url.includes('/api/v2/logs'), payloads => {
const logMessages = payloads.flatMap(({ logMessage }) => logMessage)
const [url] = payloads.flatMap(({ url }) => url)

assert.equal(url, '/api/v2/logs?dd-api-key=1&ddsource=winston&service=my-service')
assert.equal(logMessages.length, 1)
const [{ dd, level, message }] = logMessages
assert.equal(logMessages.length, 2)

logMessages.forEach(({ dd, level }) => {
assert.equal(level, 'info')
assert.equal(dd.service, 'my-service')
assert.hasAllKeys(dd, ['trace_id', 'span_id', 'service'])
})

assert.includeMembers(logMessages.map(({ message }) => message), [
'Hello simple log!',
'sum function being called'
])

logIds = {
logSpanId: logMessages[0].dd.span_id,
logTraceId: logMessages[0].dd.trace_id
}
})

assert.equal(level, 'info')
assert.equal(message, 'Hello simple log!')
assert.equal(dd.service, 'my-service')
assert.hasAllKeys(dd, ['trace_id', 'span_id', 'service'])
}).catch(done)
const eventsPromise = receiver
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), (payloads) => {
const events = payloads.flatMap(({ payload }) => payload.events)
const testEventContent = events.find(event => event.type === 'test').content

testIds = {
testSpanId: testEventContent.span_id.toString(),
testTraceId: testEventContent.trace_id.toString()
}
})

childProcess = exec(command,
{
Expand All @@ -96,11 +119,21 @@ describe('test visibility automatic log submission', () => {
}
)
childProcess.on('exit', () => {
receiverPromise.then(() => {
Promise.all([logsPromise, eventsPromise]).then(() => {
const { logSpanId, logTraceId } = logIds
const { testSpanId, testTraceId } = testIds
assert.include(testOutput, 'Hello simple log!')
assert.include(testOutput, 'span_id')
assert.include(testOutput, 'sum function being called')
// cucumber has `cucumber.step`, and that's the active span, not the test.
// logs are queried by trace id, so it should be OK
if (name !== 'cucumber') {
assert.include(testOutput, `"span_id":"${testSpanId}"`)
assert.equal(logSpanId, testSpanId)
}
assert.include(testOutput, `"trace_id":"${testTraceId}"`)
assert.equal(logTraceId, testTraceId)
done()
})
}).catch(done)
})

childProcess.stdout.on('data', (chunk) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { createLogger, format, transports } = require('winston')

module.exports = createLogger({
level: 'info',
exitOnError: false,
format: format.json(),
transports: [
new transports.Console()
]
})
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
const { expect } = require('chai')
const { createLogger, format, transports } = require('winston')
const { When, Then } = require('@cucumber/cucumber')

const logger = createLogger({
level: 'info',
exitOnError: false,
format: format.json(),
transports: [
new transports.Console()
]
})
const logger = require('./logger')
const sum = require('./sum')

Then('I should have made a log', async function () {
expect(true).to.equal(true)
expect(sum(1, 2)).to.equal(3)
})

When('we run a test', async function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const logger = require('./logger')

module.exports = function (a, b) {
logger.log('info', 'sum function being called')
return a + b
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
const { createLogger, format, transports } = require('winston')
const { expect } = require('chai')

const logger = createLogger({
level: 'info',
exitOnError: false,
format: format.json(),
transports: [
new transports.Console()
]
})
const logger = require('./logger')
const sum = require('./sum')

describe('test', () => {
it('should return true', () => {
logger.log('info', 'Hello simple log!')

expect(true).to.be.true
expect(sum(1, 2)).to.equal(3)
})
})
10 changes: 10 additions & 0 deletions integration-tests/ci-visibility/automatic-log-submission/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { createLogger, format, transports } = require('winston')

module.exports = createLogger({
level: 'info',
exitOnError: false,
format: format.json(),
transports: [
new transports.Console()
]
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const logger = require('./logger')

module.exports = function (a, b) {
logger.log('info', 'sum function being called')
return a + b
}

0 comments on commit 1857aed

Please sign in to comment.