Skip to content

Commit 38e9a8e

Browse files
authored
tests: refactor config tests service name (#3777)
1 parent 1287fd2 commit 38e9a8e

File tree

14 files changed

+274
-298
lines changed

14 files changed

+274
-298
lines changed

test/config.test.js

-289
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,13 @@
66

77
'use strict';
88

9-
var cp = require('child_process');
10-
var fs = require('fs');
119
var IncomingMessage = require('http').IncomingMessage;
12-
var os = require('os');
13-
var path = require('path');
1410
var util = require('util');
1511

1612
var test = require('tape');
1713

1814
const Agent = require('../lib/agent');
1915
const { MockAPMServer } = require('./_mock_apm_server');
20-
const { MockLogger } = require('./_mock_logger');
2116
const { NoopApmClient } = require('../lib/apm-client/noop-apm-client');
2217
const { ENV_TABLE } = require('../lib/config/schema');
2318
const config = require('../lib/config/config');
@@ -103,290 +98,6 @@ test('should overwrite option property active by ELASTIC_APM_ACTIVE', function (
10398
t.end();
10499
});
105100

106-
test('invalid serviceName => inactive', function (t) {
107-
const logger = new MockLogger();
108-
const agent = new Agent();
109-
110-
agent.start(
111-
Object.assign({}, agentOptsNoopTransport, {
112-
serviceName: 'foo&bar',
113-
logger,
114-
}),
115-
);
116-
117-
const error = logger.calls.find((log) => log.type === 'error');
118-
t.ok(
119-
error && error.message.indexOf('serviceName') !== -1,
120-
'there was a log.error mentioning "serviceName"',
121-
);
122-
t.strictEqual(agent._conf.active, false, 'active is false');
123-
agent.destroy();
124-
t.end();
125-
});
126-
127-
test('valid serviceName => active', function (t) {
128-
var agent = new Agent();
129-
agent.start(
130-
Object.assign({}, agentOptsNoopTransport, {
131-
serviceName: 'fooBAR0123456789_- ',
132-
}),
133-
);
134-
t.strictEqual(agent._conf.active, true);
135-
agent.destroy();
136-
t.end();
137-
});
138-
139-
test('serviceName/serviceVersion zero-conf: valid', function (t) {
140-
cp.execFile(
141-
process.execPath,
142-
['index.js'],
143-
{
144-
timeout: 3000,
145-
cwd: path.join(__dirname, 'fixtures', 'pkg-zero-conf-valid'),
146-
},
147-
function (err, stdout, stderr) {
148-
t.error(err, 'no error running index.js: ' + err);
149-
t.equal(stderr, '', 'no stderr');
150-
const lines = stdout.trim().split('\n');
151-
const conf = JSON.parse(lines[lines.length - 1]);
152-
t.equal(
153-
conf.serviceName,
154-
'validName',
155-
'serviceName was inferred from package.json',
156-
);
157-
t.equal(
158-
conf.serviceVersion,
159-
'1.2.3',
160-
'serviceVersion was inferred from package.json',
161-
);
162-
t.end();
163-
},
164-
);
165-
});
166-
167-
test('serviceName/serviceVersion zero-conf: cwd is outside package tree', function (t) {
168-
const indexJs = path.join(
169-
__dirname,
170-
'fixtures',
171-
'pkg-zero-conf-valid',
172-
'index.js',
173-
);
174-
cp.execFile(
175-
process.execPath,
176-
[indexJs],
177-
{
178-
timeout: 3000,
179-
// Set CWD to outside of the package tree to test whether the agent
180-
// package.json searching uses `require.main`.
181-
cwd: '/',
182-
},
183-
function (err, stdout, stderr) {
184-
t.error(err, 'no error running index.js: ' + err);
185-
t.equal(stderr, '', 'no stderr');
186-
const lines = stdout.trim().split('\n');
187-
const conf = JSON.parse(lines[lines.length - 1]);
188-
t.equal(
189-
conf.serviceName,
190-
'validName',
191-
'serviceName was inferred from package.json',
192-
);
193-
t.equal(
194-
conf.serviceVersion,
195-
'1.2.3',
196-
'serviceVersion was inferred from package.json',
197-
);
198-
t.end();
199-
},
200-
);
201-
});
202-
203-
test('serviceName/serviceVersion zero-conf: no "name" in package.json', function (t) {
204-
cp.execFile(
205-
process.execPath,
206-
['index.js'],
207-
{
208-
timeout: 3000,
209-
cwd: path.join(__dirname, 'fixtures', 'pkg-zero-conf-noname'),
210-
},
211-
function (err, stdout, stderr) {
212-
t.error(err, 'no error running index.js: ' + err);
213-
t.equal(stderr, '', 'no stderr');
214-
const lines = stdout.trim().split('\n');
215-
const conf = JSON.parse(lines[lines.length - 1]);
216-
t.equal(
217-
conf.serviceName,
218-
'unknown-nodejs-service',
219-
'serviceName is the `unknown-{service.agent.name}-service` zero-conf fallback',
220-
);
221-
t.equal(
222-
conf.serviceVersion,
223-
'1.2.3',
224-
'serviceVersion was inferred from package.json',
225-
);
226-
t.end();
227-
},
228-
);
229-
});
230-
231-
// A package.json#name that uses a scoped npm name, e.g. @ns/name, should get
232-
// a normalized serviceName='ns-name'.
233-
test('serviceName/serviceVersion zero-conf: namespaced package name', function (t) {
234-
cp.execFile(
235-
process.execPath,
236-
['index.js'],
237-
{
238-
timeout: 3000,
239-
cwd: path.join(__dirname, 'fixtures', 'pkg-zero-conf-nsname'),
240-
},
241-
function (err, stdout, stderr) {
242-
t.error(err, 'no error running index.js: ' + err);
243-
t.equal(stderr, '', 'no stderr');
244-
const lines = stdout.trim().split('\n');
245-
const conf = JSON.parse(lines[lines.length - 1]);
246-
t.equal(
247-
conf.serviceName,
248-
'ns-name',
249-
'serviceName was inferred and normalized from package.json',
250-
);
251-
t.equal(
252-
conf.serviceVersion,
253-
'1.2.3',
254-
'serviceVersion was inferred from package.json',
255-
);
256-
t.end();
257-
},
258-
);
259-
});
260-
261-
test('serviceName/serviceVersion zero-conf: a package name that requires sanitization', function (t) {
262-
cp.execFile(
263-
process.execPath,
264-
['index.js'],
265-
{
266-
timeout: 3000,
267-
cwd: path.join(__dirname, 'fixtures', 'pkg-zero-conf-sanitize'),
268-
},
269-
function (err, stdout, stderr) {
270-
t.error(err, 'no error running index.js: ' + err);
271-
t.equal(stderr, '', 'no stderr');
272-
const lines = stdout.trim().split('\n');
273-
const conf = JSON.parse(lines[lines.length - 1]);
274-
// serviceName sanitization changes any disallowed char to an underscore.
275-
// The pkg-zero-conf-sanitize/package.json has a name starting with the
276-
// 7 characters that an npm package name can have, but a serviceName
277-
// cannot.
278-
// "name": "~*.!'()validNpmName"
279-
t.equal(
280-
conf.serviceName,
281-
'_______validNpmName',
282-
'serviceName was inferred and sanitized from package.json',
283-
);
284-
t.equal(
285-
conf.serviceVersion,
286-
'1.2.3',
287-
'serviceVersion was inferred from package.json',
288-
);
289-
t.end();
290-
},
291-
);
292-
});
293-
294-
test('serviceName/serviceVersion zero-conf: weird "name" in package.json', function (t) {
295-
cp.execFile(
296-
process.execPath,
297-
['index.js'],
298-
{
299-
timeout: 3000,
300-
cwd: path.join(__dirname, 'fixtures', 'pkg-zero-conf-weird'),
301-
},
302-
function (err, stdout, stderr) {
303-
t.error(err, 'no error running index.js: ' + err);
304-
t.equal(stderr, '', 'no stderr');
305-
const lines = stdout.trim().split('\n');
306-
const logs = lines.map((l) => JSON.parse(l));
307-
const logWarn = logs.find((log) => log['log.level'] === 'warn');
308-
t.ok(
309-
logWarn['log.level'] === 'warn' &&
310-
logWarn.message.indexOf('serviceName') !== -1,
311-
'there is a log.warn about "serviceName"',
312-
);
313-
const conf = JSON.parse(
314-
// Filter out log lines from the APM agent itself. We just want the
315-
// `console.log(...)` from the index.js script.
316-
lines.filter((ln) => ln.indexOf('"log.level":') === -1)[0],
317-
);
318-
t.equal(
319-
conf.serviceName,
320-
'unknown-nodejs-service',
321-
'serviceName is the `unknown-{service.agent.name}-service` zero-conf fallback',
322-
);
323-
t.equal(
324-
conf.serviceVersion,
325-
'1.2.3',
326-
'serviceVersion was inferred from package.json',
327-
);
328-
t.end();
329-
},
330-
);
331-
});
332-
333-
test('serviceName/serviceVersion zero-conf: no package.json to find', function (t) {
334-
// To test the APM agent's fallback serviceName, we need to execute
335-
// a script in a dir that has no package.json in its dir, or any dir up
336-
// from it (we assume/hope that `os.tmpdir()` works for that).
337-
const dir = os.tmpdir();
338-
const script = path.resolve(dir, 'elastic-apm-node-zero-conf-test-script.js');
339-
// Avoid Windows '\' path separators that are interpreted as escapes when
340-
// interpolated into the script content below.
341-
const agentDir = path
342-
.resolve(__dirname, '..')
343-
.replace(new RegExp('\\' + path.win32.sep, 'g'), path.posix.sep);
344-
function setupPkgEnv() {
345-
fs.writeFileSync(
346-
script,
347-
`
348-
const apm = require('${agentDir}').start({
349-
disableSend: true
350-
})
351-
console.log(JSON.stringify(apm._conf))
352-
`,
353-
);
354-
t.comment(`created ${script}`);
355-
}
356-
function teardownPkgEnv() {
357-
fs.unlinkSync(script);
358-
t.comment(`removed ${script}`);
359-
}
360-
361-
setupPkgEnv();
362-
cp.execFile(
363-
process.execPath,
364-
[script],
365-
{
366-
timeout: 3000,
367-
cwd: dir,
368-
},
369-
function (err, stdout, stderr) {
370-
t.error(err, 'no error running script: ' + err);
371-
t.equal(stderr, '', 'no stderr');
372-
const lines = stdout.trim().split('\n');
373-
const conf = JSON.parse(
374-
// Filter out log lines from the APM agent itself. We just want the
375-
// `console.log(...)` from the index.js script.
376-
lines.filter((ln) => ln.indexOf('"log.level":') === -1)[0],
377-
);
378-
t.equal(
379-
conf.serviceName,
380-
'unknown-nodejs-service',
381-
'serviceName is the `unknown-{service.agent.name}-service` zero-conf fallback',
382-
);
383-
t.equal(conf.serviceVersion, undefined, 'serviceVersion is undefined');
384-
teardownPkgEnv();
385-
t.end();
386-
},
387-
);
388-
});
389-
390101
var captureBodyTests = [
391102
{ value: 'off', errors: '[REDACTED]', transactions: '[REDACTED]' },
392103
{ value: 'transactions', errors: '[REDACTED]', transactions: 'test' },

0 commit comments

Comments
 (0)