|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const assert = require('node:assert/strict') |
| 4 | + |
| 5 | +const { spawn } = require('child_process') |
| 6 | +const { describe, it } = require('mocha') |
| 7 | +const { |
| 8 | + FakeAgent, |
| 9 | + hookFile, |
| 10 | + sandboxCwd, |
| 11 | + useSandbox, |
| 12 | + curlAndAssertMessage, |
| 13 | + assertObjectContains, |
| 14 | +} = require('../../../../integration-tests/helpers') |
| 15 | +const { withVersions } = require('../../../dd-trace/test/setup/mocha') |
| 16 | + |
| 17 | +describe('esm', () => { |
| 18 | + let agent |
| 19 | + let proc |
| 20 | + |
| 21 | + withVersions('azure-durable-functions', 'durable-functions', version => { |
| 22 | + useSandbox([ |
| 23 | + `durable-functions@${version}`, |
| 24 | + '@azure/functions', |
| 25 | + 'azure-functions-core-tools@4', |
| 26 | + ], |
| 27 | + false, |
| 28 | + ['./packages/datadog-plugin-azure-durable-functions/test/integration-test/*', |
| 29 | + './packages/datadog-plugin-azure-durable-functions/test/fixtures/*', |
| 30 | + ]) |
| 31 | + |
| 32 | + beforeEach(async () => { |
| 33 | + agent = await new FakeAgent().start() |
| 34 | + }) |
| 35 | + |
| 36 | + afterEach(async () => { |
| 37 | + // after each test, kill process and wait for exit before continuing |
| 38 | + if (proc) { |
| 39 | + proc.kill('SIGINT') |
| 40 | + await new Promise(resolve => proc.on('exit', resolve)) |
| 41 | + } |
| 42 | + await agent.stop() |
| 43 | + }) |
| 44 | + |
| 45 | + it('is instrumented', async () => { |
| 46 | + proc = await spawnPluginIntegrationTestProc(agent.port) |
| 47 | + return await curlAndAssertMessage(agent, 'http://127.0.0.1:7071/api/httptest', ({ headers, payload }) => { |
| 48 | + assert.strictEqual(headers.host, `127.0.0.1:${agent.port}`) |
| 49 | + assert.ok(Array.isArray(payload)) |
| 50 | + |
| 51 | + // should expect spans for http.request, activity.hola, entity.counter.add_n, entity.counter.get_count |
| 52 | + assert.strictEqual(payload.length, 4) |
| 53 | + |
| 54 | + for (const maybeArray of payload) { |
| 55 | + assert.ok(Array.isArray(maybeArray)) |
| 56 | + } |
| 57 | + |
| 58 | + const [maybeHttpSpan, maybeHolaActivity, maybeAddNEntity, maybeGetCountEntity] = payload |
| 59 | + |
| 60 | + assert.strictEqual(maybeHttpSpan.length, 2) |
| 61 | + assert.strictEqual(maybeHttpSpan[0].resource, 'GET /api/httptest') |
| 62 | + |
| 63 | + assert.strictEqual(maybeHolaActivity.length, 1) |
| 64 | + assertObjectContains(maybeHolaActivity[0], { |
| 65 | + name: 'azure.functions.invoke', |
| 66 | + resource: 'Activity hola', |
| 67 | + meta: { |
| 68 | + 'aas.function.trigger': 'Activity', |
| 69 | + 'aas.function.name': 'hola', |
| 70 | + }, |
| 71 | + }) |
| 72 | + |
| 73 | + assert.strictEqual(maybeAddNEntity.length, 1) |
| 74 | + assertObjectContains(maybeAddNEntity[0], { |
| 75 | + name: 'azure.functions.invoke', |
| 76 | + resource: 'Entity counter add_n', |
| 77 | + meta: { |
| 78 | + 'aas.function.trigger': 'Entity', |
| 79 | + 'aas.function.name': 'counter', |
| 80 | + 'aas.function.operation': 'add_n', |
| 81 | + }, |
| 82 | + }) |
| 83 | + |
| 84 | + assert.strictEqual(maybeGetCountEntity.length, 1) |
| 85 | + assertObjectContains(maybeGetCountEntity[0], { |
| 86 | + name: 'azure.functions.invoke', |
| 87 | + resource: 'Entity counter get_count', |
| 88 | + meta: { |
| 89 | + 'aas.function.trigger': 'Entity', |
| 90 | + 'aas.function.name': 'counter', |
| 91 | + 'aas.function.operation': 'get_count', |
| 92 | + }, |
| 93 | + }) |
| 94 | + }) |
| 95 | + }).timeout(60_000) |
| 96 | + }) |
| 97 | +}) |
| 98 | + |
| 99 | +/** |
| 100 | + * - spawns process for azure func start commands |
| 101 | + * - connects to azurite (running in container) |
| 102 | + * then runs the durable function locally |
| 103 | + */ |
| 104 | +async function spawnPluginIntegrationTestProc (agentPort) { |
| 105 | + const cwd = sandboxCwd() |
| 106 | + const env = { |
| 107 | + NODE_OPTIONS: `--loader=${hookFile}`, |
| 108 | + DD_TRACE_AGENT_PORT: agentPort, |
| 109 | + DD_TRACE_DISABLED_PLUGINS: 'amqplib,amqp10,rhea,net', |
| 110 | + PATH: `${cwd}/node_modules/azure-functions-core-tools/bin:${process.env.PATH}`, |
| 111 | + } |
| 112 | + |
| 113 | + const options = { cwd, env } |
| 114 | + |
| 115 | + const proc = await spawnProc('func', ['start'], options) |
| 116 | + return proc |
| 117 | +} |
| 118 | + |
| 119 | +function spawnProc (command, args, options = {}) { |
| 120 | + const proc = spawn(command, args, { ...options, stdio: 'pipe' }) |
| 121 | + return new Promise((resolve, reject) => { |
| 122 | + proc |
| 123 | + .on('error', reject) |
| 124 | + .on('exit', code => { |
| 125 | + if (code !== 0) { |
| 126 | + reject(new Error(`Process exited with status code ${code}.`)) |
| 127 | + } |
| 128 | + resolve() |
| 129 | + }) |
| 130 | + |
| 131 | + proc.stdout.on('data', data => { |
| 132 | + // eslint-disable-next-line no-console |
| 133 | + if (!options.silent) console.log(data.toString()) |
| 134 | + |
| 135 | + if (data.toString().includes('Host lock lease acquired by instance')) { |
| 136 | + resolve(proc) |
| 137 | + } |
| 138 | + }) |
| 139 | + |
| 140 | + proc.stderr.on('data', data => { |
| 141 | + // eslint-disable-next-line no-console |
| 142 | + if (!options.silent) console.error(data.toString()) |
| 143 | + }) |
| 144 | + }) |
| 145 | +} |
0 commit comments