This repository has been archived by the owner on Jun 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
common.js
194 lines (176 loc) · 7.14 KB
/
common.js
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
'use strict';
const child_process = require('child_process');
const fs = require('fs');
const os = require('os');
const osMap = {
'aix': 'AIX',
'darwin': 'Darwin',
'linux': 'Linux',
'sunos': 'SunOS',
'win32': 'Windows',
'os390': 'OS/390',
};
const REPORT_SECTIONS = [
'Node Report',
'JavaScript Stack Trace',
'JavaScript Heap',
'System Information'
];
const reNewline = '(?:\\r*\\n)';
exports.findReports = (pid) => {
// Default filenames are of the form node-report.<date>.<time>.<pid>.<seq>.txt
const format = '^node-report\\.\\d+\\.\\d+\\.' + pid + '\\.\\d+\\.txt$';
const filePattern = new RegExp(format);
const files = fs.readdirSync('.');
return files.filter((file) => filePattern.test(file));
};
exports.isAIX = () => {
return process.platform === 'aix';
};
exports.isPPC = () => {
return process.arch.startsWith('ppc');
};
exports.isSunOS = () => {
return process.platform === 'sunos';
};
exports.isWindows = () => {
return process.platform === 'win32';
};
exports.validate = (t, report, options) => {
t.test('Validating ' + report, (t) => {
fs.readFile(report, (err, data) => { this.validateContent(data, t, options)})
})
}
exports.validateContent = function validateContent(data, t, options) {
const pid = options ? options.pid : process.pid;
const reportContents = data.toString();
const nodeComponents = Object.keys(process.versions);
const expectedVersions = options ?
options.expectedVersions || nodeComponents :
nodeComponents;
const expectedException = options.expectedException;
if (options.expectedException) {
REPORT_SECTIONS.push('JavaScript Exception Details');
}
let plan = REPORT_SECTIONS.length + nodeComponents.length + 5;
if (options.commandline) plan++;
if (options.expectedException) plan++;
const glibcRE = /\(glibc:\s([\d.]+)/;
const nodeReportSection = getSection(reportContents, 'Node Report');
const sysInfoSection = getSection(reportContents, 'System Information');
const exceptionSection = getSection(reportContents, 'JavaScript Exception Details');
const libcPath = getLibcPath(sysInfoSection);
const libcVersion = getLibcVersion(libcPath);
if (glibcRE.test(nodeReportSection) && libcVersion) plan++;
t.plan(plan);
// Check all sections are present
REPORT_SECTIONS.forEach((section) => {
t.match(reportContents, new RegExp('==== ' + section),
'Checking report contains ' + section + ' section');
});
// Check report header section
t.match(nodeReportSection, new RegExp('Process ID: ' + pid),
'Node Report header section contains expected process ID');
if (options && options.expectNodeVersion === false) {
t.match(nodeReportSection, /Unable to determine Node.js version/,
'Node Report header section contains expected Node.js version');
} else {
t.match(nodeReportSection,
new RegExp('Node.js version: ' + process.version),
'Node Report header section contains expected Node.js version');
}
if (options && options.expectedException) {
t.match(exceptionSection, new RegExp('Uncaught Error: ' + options.expectedException),
'Node Report JavaScript Exception contains expected message');
}
if (options && options.commandline) {
if (this.isWindows()) {
// On Windows we need to strip double quotes from the command line in
// the report, and escape backslashes in the regex comparison string.
t.match(nodeReportSection.replace(/"/g,''),
new RegExp('Command line: '
+ (options.commandline).replace(/\\/g,'\\\\')),
'Checking report contains expected command line');
} else {
t.match(nodeReportSection,
new RegExp('Command line: ' + options.commandline),
'Checking report contains expected command line');
}
}
nodeComponents.forEach((c) => {
if (c !== 'node') {
if (expectedVersions.indexOf(c) === -1) {
t.notMatch(nodeReportSection,
new RegExp(c + ': ' + process.versions[c]),
'Node Report header section does not contain ' + c + ' version');
} else {
t.match(nodeReportSection,
new RegExp(c + ': ' + process.versions[c]),
'Node Report header section contains expected ' + c + ' version');
}
}
});
const nodereportMetadata = require('../package.json');
t.match(nodeReportSection,
new RegExp('node-report version: ' + nodereportMetadata.version),
'Node Report header section contains expected Node Report version');
const os = require('os');
if (this.isWindows()) {
t.match(nodeReportSection,
new RegExp('Machine: ' + os.hostname(), 'i'), // ignore case on Windows
'Checking machine name in report header section contains os.hostname()');
} else if (this.isAIX()) {
t.match(nodeReportSection,
new RegExp('Machine: ' + os.hostname().split('.')[0]), // truncate on AIX
'Checking machine name in report header section contains os.hostname()');
} else {
t.match(nodeReportSection,
new RegExp('Machine: ' + os.hostname()),
'Checking machine name in report header section contains os.hostname()');
}
const osName = osMap[os.platform()];
const osVersion = nodeReportSection.match(/OS version: .*(?:\r*\n)/);
if (this.isWindows()) {
t.match(osVersion,
new RegExp('OS version: ' + osName), 'Checking OS version');
} else if (this.isAIX() && !os.release().includes('.')) {
// For Node.js prior to os.release() fix for AIX:
// https://github.com/nodejs/node/pull/10245
t.match(osVersion,
new RegExp('OS version: ' + osName + ' \\d+.' + os.release()),
'Checking OS version');
} else {
t.match(osVersion,
new RegExp('OS version: ' + osName + ' .*' + os.release()),
'Checking OS version');
}
// Check report System Information section
// If the report contains a glibc version, check it against libc.so.6
const glibcMatch = glibcRE.exec(nodeReportSection);
if (glibcMatch != null && libcVersion) {
t.equal(glibcMatch[1], libcVersion,
'Checking reported runtime glibc version against ' + libcPath);
}
// Find a line which ends with "/api.node" or "\api.node" (Unix or
// Windows paths) to see if the library for node report was loaded.
t.match(sysInfoSection, / .*(\/|\\)api\.node/,
'System Information section contains node-report library.');
};
const getLibcPath = (section) => {
const libcMatch = /\n\s+(\/.*\/libc.so.6)\b/.exec(section);
return (libcMatch != null ? libcMatch[1] : undefined);
};
const getLibcVersion = (path) => {
if (!path) {
return undefined;
}
const child = child_process.spawnSync('strings', [path], {encoding: 'utf8'});
const match = /GNU C Library.*\bversion ([\d.]+)\b/.exec(child.stdout);
return (match != null ? match[1] : undefined);
};
const getSection = exports.getSection = (report, section) => {
const re = new RegExp('==== ' + section + ' =+' + reNewline + '+([\\S\\s]+?)'
+ reNewline + '+={80}' + reNewline);
const match = re.exec(report);
return match ? match[1] : '';
};