-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathtest.js
More file actions
169 lines (140 loc) · 6.13 KB
/
test.js
File metadata and controls
169 lines (140 loc) · 6.13 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* (c) Copyright IBM Corp. 2021
* (c) Copyright Instana Inc. and contributors 2020
*/
'use strict';
const { expect } = require('chai');
const path = require('path');
const fs = require('fs');
const os = require('os');
const semver = require('semver');
const config = require('../../../core/test/config');
const { retry, runCommandSync } = require('../../../core/test/test_util');
const ProcessControls = require('../test_util/ProcessControls');
const globalAgent = require('../globalAgent');
const { execSync } = require('child_process');
const tmpFolder = path.join(os.tmpdir(), 'native-module-retry', process.pid.toString());
// NOTE: we skip this test on the prerelease pipeline because
// we add prereleases when the official Node.js release happens.
// This test runs only on stable, even-numbered Node.js versions
const majorVersion = parseInt(process.versions.node.split('.')[0], 10);
const isOddVersion = majorVersion % 2 !== 0;
const isPrerelease = Boolean(semver.prerelease(process.versions.node));
const mochaSuiteFn = isOddVersion || isPrerelease ? describe.skip : describe;
// Test suite for verifying the fallback mechanism for loading native add-ons.
mochaSuiteFn('retry loading native addons', function () {
this.timeout(config.getTestTimeout() * 5);
globalAgent.setUpCleanUpHooks();
const agentControls = globalAgent.instance;
const metricAddonsTestConfigs = [
{
name: 'event-loop-stats',
check: ([aggregated]) => {
const libuv = aggregated.libuv;
expect(libuv).to.exist;
expect(libuv).to.be.an('object');
expect(libuv.statsSupported).to.be.true;
expect(libuv.min).to.be.a('number');
expect(libuv.max).to.be.a('number');
expect(libuv.sum).to.be.a('number');
expect(libuv.lag).to.be.a('number');
}
},
{
name: 'gcstats.js',
check: ([aggregated]) => {
// The for loop above ensures that the first metric POST that had the gc payload
// had gc.statsSupported == undefined. Now we check that at some point, the supported flag changed to true.
const gc = aggregated.gc;
expect(gc).to.exist;
expect(gc).to.be.an('object');
expect(gc.statsSupported).to.be.true;
expect(gc.minorGcs).to.exist;
expect(gc.majorGcs).to.exist;
}
}
];
metricAddonsTestConfigs.forEach(async opts => {
describe(`${opts.name}: expect metrics to show up`, function () {
let controls;
const check = async copiedBinaryPath => {
const targetDirectory = 'precompiled';
const directoryPath = path.join(copiedBinaryPath, targetDirectory);
['binding.gyp', 'build', 'package.json', 'src'].forEach(file => {
expect(fs.existsSync(path.join(directoryPath, file))).to.be.true;
});
if (directoryPath.includes('event-loop-stats')) {
expect(fs.existsSync(path.join(directoryPath, 'src', 'eventLoopStats.cc'))).to.be.true;
expect(fs.existsSync(path.join(directoryPath, 'src', 'eventLoopStats.js'))).to.be.true;
expect(fs.existsSync(path.join(directoryPath, 'build', 'Release', 'eventLoopStats.node'))).to.be.true;
}
if (directoryPath.includes('gcstats.js')) {
expect(fs.existsSync(path.join(directoryPath, 'src', 'gcstats.cc'))).to.be.true;
expect(fs.existsSync(path.join(directoryPath, 'src', 'gcstats.js'))).to.be.true;
expect(fs.existsSync(path.join(directoryPath, 'build', 'Release', 'gcstats.node'))).to.be.true;
}
};
before(async () => {
execSync(`rm -rf ${tmpFolder}`, { cwd: __dirname, stdio: 'inherit' });
execSync(`mkdir -p ${tmpFolder}`, { cwd: __dirname, stdio: 'inherit' });
execSync(`cp app.js ${tmpFolder}/`, { cwd: __dirname, stdio: 'inherit' });
// eslint-disable-next-line no-console
console.log('Running npm install in', tmpFolder);
execSync('rm -rf node_modules', { cwd: tmpFolder, stdio: 'inherit' });
execSync('./preinstall.sh', { cwd: __dirname, stdio: 'inherit' });
runCommandSync(`npm install --no-save --no-package-lock --prefix ./ ${__dirname}/core.tgz`, tmpFolder);
runCommandSync(
`npm install --no-save --no-package-lock --prefix ./ ${__dirname}/shared-metrics.tgz`,
tmpFolder
);
runCommandSync(`npm install --no-save --no-package-lock --prefix ./ ${__dirname}/collector.tgz`, tmpFolder);
// Remove the target c++ module
execSync(`rm -rf node_modules/${opts.name}`, { cwd: tmpFolder, stdio: 'inherit' });
if (fs.existsSync(path.join(tmpFolder, 'node_modules', opts.name))) {
throw new Error('The module should not be present at this point');
}
// Currently we do not ship darwin prebuilds via shared-metrics
// See package.json
execSync(
`cp -R ../../../shared-metrics/addons/darwin ${tmpFolder}/node_modules/@instana/shared-metrics/addons/`,
{
cwd: __dirname,
stdio: 'inherit'
}
);
controls = new ProcessControls({
appPath: path.join(tmpFolder, 'app'),
agentControls,
useGlobalAgent: true,
env: {
DEV_PATH: __dirname
}
});
await controls.startAndWaitForAgentConnection();
});
beforeEach(async () => {
await agentControls.clearReceivedTraceData();
});
after(async () => {
await controls.stop();
});
afterEach(async () => {
await controls.clearIpcMessages();
});
describe(opts.name, () => {
it('metrics from native add-ons should become available at some point', async () => {
await retry(async () => {
const [aggregatedMetrics, events] = await Promise.all([
agentControls.getAggregatedMetrics(controls.getPid()),
agentControls.getEvents()
]);
await opts.check([aggregatedMetrics, events]);
});
});
it('should successfully copy the precompiled binaries', async () => {
await check(path.join(tmpFolder, 'node_modules', opts.name));
});
});
});
});
});