-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathdi-logs-writer.spec.js
More file actions
108 lines (83 loc) · 2.97 KB
/
Copy pathdi-logs-writer.spec.js
File metadata and controls
108 lines (83 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
'use strict'
const assert = require('node:assert/strict')
const { describe, it, beforeEach, afterEach } = require('mocha')
const context = describe
const sinon = require('sinon')
const nock = require('nock')
require('../../../../../dd-trace/test/setup/core')
const DynamicInstrumentationLogsWriter = require('../../../../src/ci-visibility/exporters/agentless/di-logs-writer')
const log = require('../../../../src/log')
describe('Test Visibility DI Writer', () => {
beforeEach(() => {
nock.cleanAll()
process.env.DD_API_KEY = '1'
})
afterEach(() => {
delete process.env.DD_API_KEY
sinon.restore()
})
context('agentless', () => {
it('can send logs to the logs intake', (done) => {
const scope = nock('http://www.example.com')
.post('/api/v2/logs', body => {
assert.deepStrictEqual(body, [{ message: 'test' }, { message: 'test2' }])
return true
})
.reply(202)
const logsWriter = new DynamicInstrumentationLogsWriter({ url: 'http://www.example.com' })
logsWriter.append({ message: 'test' })
logsWriter.append({ message: 'test2' })
logsWriter.flush(() => {
scope.done()
done()
})
})
it('logs an error if the request fails', (done) => {
const logErrorSpy = sinon.spy(log, 'error')
const scope = nock('http://www.example.com')
.post('/api/v2/logs')
.reply(500)
const logsWriter = new DynamicInstrumentationLogsWriter({ url: 'http://www.example.com' })
logsWriter.append({ message: 'test5' })
logsWriter.append({ message: 'test6' })
logsWriter.flush(() => {
assert.strictEqual(logErrorSpy.called, true)
scope.done()
done()
})
})
})
context('agent based', () => {
it('can send logs to the debugger endpoint in the agent', (done) => {
delete process.env.DD_API_KEY
const scope = nock('http://www.example.com')
.post('/debugger/v1/input', body => {
assert.deepStrictEqual(body, [{ message: 'test3' }, { message: 'test4' }])
return true
})
.reply(202)
const logsWriter = new DynamicInstrumentationLogsWriter({ url: 'http://www.example.com', isAgentProxy: true })
logsWriter.append({ message: 'test3' })
logsWriter.append({ message: 'test4' })
logsWriter.flush(() => {
scope.done()
done()
})
})
it('logs an error if the request fails', (done) => {
delete process.env.DD_API_KEY
const logErrorSpy = sinon.spy(log, 'error')
const scope = nock('http://www.example.com')
.post('/debugger/v1/input')
.reply(500)
const logsWriter = new DynamicInstrumentationLogsWriter({ url: 'http://www.example.com', isAgentProxy: true })
logsWriter.append({ message: 'test5' })
logsWriter.append({ message: 'test6' })
logsWriter.flush(() => {
assert.strictEqual(logErrorSpy.called, true)
scope.done()
done()
})
})
})
})