forked from googleapis/cloud-trace-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-index.ts
103 lines (88 loc) · 3.44 KB
/
test-index.ts
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
/**
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
import './override-gcp-metadata';
import { TraceAgent } from '../src/trace-api';
var assert = require('assert');
var nock = require('nock');
var nocks = require('./nocks'/*.js*/);
var trace = require('../..');
var disabledAgent: TraceAgent = trace.get();
describe('index.js', function() {
it('should get a disabled agent with `Trace.get`', function() {
assert.ok(!disabledAgent.isActive()); // ensure it's disabled first
let ranInRootSpan = false;
disabledAgent.runInRootSpan({ name: '' }, (span) => {
assert.strictEqual(span, null);
ranInRootSpan = true;
});
assert.ok(ranInRootSpan);
assert.strictEqual(disabledAgent.enhancedDatabaseReportingEnabled(), false);
assert.strictEqual(disabledAgent.getCurrentContextId(), null);
assert.strictEqual(disabledAgent.getWriterProjectId(), null);
assert.strictEqual(disabledAgent.createChildSpan({ name: '' }), null);
assert.strictEqual(disabledAgent.getResponseTraceContext('', false), '');
const fn = () => {};
assert.strictEqual(disabledAgent.wrap(fn), fn);
// TODO(kjin): Figure out how to test wrapEmitter
});
describe('in valid environment', function() {
var agent;
before(function() {
agent = trace.start({ projectId: '0', forceNewAgent_: true });
});
it('should get the agent with `Trace.get`', function() {
assert.strictEqual(agent, trace.get());
});
it('should throw an error if `start` is called on an active agent',
function() {
assert.throws(trace.start, Error);
});
it('should set agent on global object', function() {
assert.equal(global._google_trace_agent, agent);
});
});
it('should stop if TraceWriter fails to initialize', function(done) {
var envProjectId = process.env.GCLOUD_PROJECT;
delete process.env.GCLOUD_PROJECT;
nock.disableNetConnect();
var scope = nock('http://metadata.google.internal')
.get('/computeMetadata/v1/project/project-id')
.times(1)
.reply(404, 'foo');
var agent = trace.start({logLevel: 0, forceNewAgent_: true});
setTimeout(function() {
assert.ok(!agent.isActive());
scope.done();
process.env.GCLOUD_PROJECT = envProjectId;
done();
}, 500);
});
it('should allow project ID to be read after discovery', function(done) {
var envProjectId = process.env.GCLOUD_PROJECT;
delete process.env.GCLOUD_PROJECT;
nocks.projectId(function() { return 'project1'; });
nocks.hostname(function() { return 'host1'; });
nocks.instanceId(function() { return 'instance1'; });
var agent = trace.start({logLevel: 0, forceNewAgent_: true});
setTimeout(function() {
assert.strictEqual(agent.getWriterProjectId(), 'project1');
process.env.GCLOUD_PROJECT = envProjectId;
done();
}, 500);
});
});
export default {};