|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { |
| 4 | + FakeAgent, |
| 5 | + hookFile, |
| 6 | + createSandbox, |
| 7 | + curlAndAssertMessage |
| 8 | +} = require('../../../../integration-tests/helpers') |
| 9 | +const { spawn } = require('child_process') |
| 10 | +const { assert } = require('chai') |
| 11 | + |
| 12 | +describe('esm', () => { |
| 13 | + let agent |
| 14 | + let proc |
| 15 | + let sandbox |
| 16 | + |
| 17 | + withVersions('azure-functions', '@azure/functions', version => { |
| 18 | + before(async function () { |
| 19 | + this.timeout(50000) |
| 20 | + sandbox = await createSandbox([`@azure/functions@${version}`, 'azure-functions-core-tools@4'], false, |
| 21 | + ['./packages/datadog-plugin-azure-functions/test/integration-test/fixtures/*']) |
| 22 | + }) |
| 23 | + |
| 24 | + after(async function () { |
| 25 | + this.timeout(50000) |
| 26 | + await sandbox.remove() |
| 27 | + }) |
| 28 | + |
| 29 | + beforeEach(async () => { |
| 30 | + agent = await new FakeAgent().start() |
| 31 | + }) |
| 32 | + |
| 33 | + afterEach(async () => { |
| 34 | + proc && proc.kill('SIGINT') |
| 35 | + await agent.stop() |
| 36 | + }) |
| 37 | + |
| 38 | + it('is instrumented', async () => { |
| 39 | + const envArgs = { |
| 40 | + PATH: `${sandbox.folder}/node_modules/azure-functions-core-tools/bin:${process.env.PATH}` |
| 41 | + } |
| 42 | + proc = await spawnPluginIntegrationTestProc(sandbox.folder, 'func', ['start'], agent.port, undefined, envArgs) |
| 43 | + |
| 44 | + return curlAndAssertMessage(agent, 'http://127.0.0.1:7071/api/httptest', ({ headers, payload }) => { |
| 45 | + assert.propertyVal(headers, 'host', `127.0.0.1:${agent.port}`) |
| 46 | + assert.isArray(payload) |
| 47 | + assert.strictEqual(payload.length, 1) |
| 48 | + assert.isArray(payload[0]) |
| 49 | + assert.strictEqual(payload[0].length, 1) |
| 50 | + assert.propertyVal(payload[0][0], 'name', 'azure-functions.invoke') |
| 51 | + }) |
| 52 | + }).timeout(50000) |
| 53 | + }) |
| 54 | +}) |
| 55 | + |
| 56 | +async function spawnPluginIntegrationTestProc (cwd, command, args, agentPort, stdioHandler, additionalEnvArgs = {}) { |
| 57 | + let env = { |
| 58 | + NODE_OPTIONS: `--loader=${hookFile}`, |
| 59 | + DD_TRACE_AGENT_PORT: agentPort |
| 60 | + } |
| 61 | + env = { ...env, ...additionalEnvArgs } |
| 62 | + return spawnProc(command, args, { |
| 63 | + cwd, |
| 64 | + env |
| 65 | + }, stdioHandler) |
| 66 | +} |
| 67 | + |
| 68 | +function spawnProc (command, args, options = {}, stdioHandler, stderrHandler) { |
| 69 | + const proc = spawn(command, args, { ...options, stdio: 'pipe' }) |
| 70 | + return new Promise((resolve, reject) => { |
| 71 | + proc |
| 72 | + .on('error', reject) |
| 73 | + .on('exit', code => { |
| 74 | + if (code !== 0) { |
| 75 | + reject(new Error(`Process exited with status code ${code}.`)) |
| 76 | + } |
| 77 | + resolve() |
| 78 | + }) |
| 79 | + |
| 80 | + proc.stdout.on('data', data => { |
| 81 | + if (stdioHandler) { |
| 82 | + stdioHandler(data) |
| 83 | + } |
| 84 | + // eslint-disable-next-line no-console |
| 85 | + if (!options.silent) console.log(data.toString()) |
| 86 | + |
| 87 | + if (data.toString().includes('http://localhost:7071/api/httptest')) { |
| 88 | + resolve(proc) |
| 89 | + } |
| 90 | + }) |
| 91 | + |
| 92 | + proc.stderr.on('data', data => { |
| 93 | + if (stderrHandler) { |
| 94 | + stderrHandler(data) |
| 95 | + } |
| 96 | + // eslint-disable-next-line no-console |
| 97 | + if (!options.silent) console.error(data.toString()) |
| 98 | + }) |
| 99 | + }) |
| 100 | +} |
0 commit comments