-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathtest.js
More file actions
117 lines (97 loc) · 3.58 KB
/
Copy pathtest.js
File metadata and controls
117 lines (97 loc) · 3.58 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
109
110
111
112
113
114
115
116
117
/*
* (c) Copyright IBM Corp. 2021
* (c) Copyright Instana Inc. and contributors 2021
*/
'use strict';
const { expect } = require('chai');
const { spawn } = require('child_process');
const _ = require('lodash');
const portfinder = require('../test_util/portfinder');
const config = require('../../../core/test/config');
const { delay, retry } = require('../../../core/test/test_util');
const ProcessControls = require('../test_util/ProcessControls');
const globalAgent = require('../globalAgent');
const agentControls = globalAgent.instance;
let controls;
describe('collector/src/immediate', function () {
globalAgent.setUpCleanUpHooks();
this.timeout(config.getTestTimeout());
describe('an application instrumented via NODE_OPTIONS', () => {
before(async () => {
controls = new ProcessControls({
useGlobalAgent: true,
dirname: __dirname,
cwd: __dirname,
env: {
NODE_OPTIONS: '--require ../../src/immediate'
}
});
await controls.startAndWaitForAgentConnection();
});
beforeEach(async () => {
await agentControls.clearReceivedTraceData();
});
after(async () => {
await controls.stop();
});
afterEach(async () => {
await controls.clearIpcMessages();
});
it('should have connected to the agent', async () => {
// Make sure the npm process did not report to the agent even after waiting for that to happen.
const pidsThatContactedTheAgent = await agentControls.getDiscoveries();
expect(pidsThatContactedTheAgent[controls.getPid()]).to.exist;
});
});
describe('exclude processes from auto instrumentation', () => {
let npmProcess;
let appUnderTestPid;
afterEach(() => {
if (npmProcess) {
npmProcess.kill();
}
if (appUnderTestPid) {
process.kill(appUnderTestPid);
}
});
it('should ignore npm but instrument the actual application', async () => {
const appPort = portfinder();
const env = _.assign({}, process.env, {
NODE_OPTIONS: '--require ../../src/immediate',
INSTANA_AGENT_PORT: agentControls.agentPort,
INSTANA_LOG_LEVEL: 'info',
APP_PORT: appPort
});
// Start a Node.js application via npm start, this will start two Node.js processes: npm and the actual
// application under test (as a child process of npm).
npmProcess = spawn('npm', ['start'], {
shell: true,
cwd: __dirname,
stdio: config.getAppStdio(),
env
});
// Wait until the application under test is up.
appUnderTestPid = await retry(() =>
fetch(`http://localhost:${appPort}/pid`, {
url: `http://localhost:${appPort}/pid`,
method: 'GET',
headers: {
'X-INSTANA-L': '0'
}
}).then(response => {
return response.json();
})
);
const npmPid = npmProcess.pid;
// Make sure the application under test contacted the agent.
await agentControls.waitUntilAppIsCompletelyInitialized(appUnderTestPid);
// Also give the npm process time to report to the agent. (We expect it does not report to the agent but we do not
// want the test to pass only because it did not _yet_ report to the agent.)
await delay(500);
// Make sure the npm process did not report to the agent even after waiting for that to happen.
const pidsThatContactedTheAgent = await agentControls.getDiscoveries();
expect(pidsThatContactedTheAgent[appUnderTestPid]).to.exist;
expect(pidsThatContactedTheAgent[npmPid]).to.not.exist;
});
});
});