Skip to content

Commit

Permalink
test: update other tests
Browse files Browse the repository at this point in the history
foo
  • Loading branch information
kjin committed Mar 20, 2018
1 parent c37750b commit fa33f25
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 201 deletions.
2 changes: 1 addition & 1 deletion test/plugins/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ function assertDurationCorrect(expectedDuration, predicate) {

function runInTransaction(fn) {
testTraceAgent.runInRootSpan({ name: 'outer' }, function(span) {
fn(function() {
return fn(function() {
assert.strictEqual(span.type, SpanDataType.ROOT);
span.endSpan();
});
Expand Down
6 changes: 6 additions & 0 deletions test/test-agent-stopped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ var assert = require('assert');
var http = require('http');
var nock = require('nock');
var trace = require('../..');
var pluginLoader = require('../src/trace-plugin-loader').pluginLoader;
var PluginLoaderState = require('../src/trace-plugin-loader').PluginLoaderState;

describe('test-agent-stopped', function() {
var agent;
Expand All @@ -47,6 +49,10 @@ describe('test-agent-stopped', function() {
process.env.GCLOUD_PROJECT = savedProject;
});

it('deactivates the plugin loader', () => {
assert.notStrictEqual(pluginLoader.get()!.state, PluginLoaderState.ACTIVATED);
});

describe('express', function() {
it('should not break if no project number is found', function(done) {
assert.ok(!agent.isActive());
Expand Down
4 changes: 2 additions & 2 deletions test/test-config-credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ describe('Credentials Configuration', () => {
before(() => {
savedProject = process.env.GCLOUD_PROJECT;
process.env.GCLOUD_PROJECT = '0';
trace.setTraceWriterEnabled(true);
trace.setTraceWriter();
disableNetConnect();
});

after(() => {
process.env.GCLOUD_PROJECT = savedProject;
trace.setTraceWriterEnabled(false);
trace.setTraceWriter(trace.TestTraceWriter);
enableNetConnect();
});

Expand Down
96 changes: 44 additions & 52 deletions test/test-config-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,73 +14,65 @@
* limitations under the License.
*/

'use strict';
import {Logger} from '@google-cloud/common';
import * as assert from 'assert';

import * as pluginLoader from '../src/trace-plugin-loader';
import {defaultConfig} from '../src/config';
import {PluginLoader, PluginLoaderConfig} from '../src/trace-plugin-loader';

var assert = require('assert');
var shimmer = require('shimmer');
var trace = require('../..');
import * as trace from './trace';

var instrumentedModules = [
'connect', 'express', 'generic-pool', 'grpc', 'hapi', 'http', 'http2',
'https', 'knex', 'koa', 'mongodb-core', 'mysql', 'mysql2', 'pg', 'redis',
'restify',
];
describe('Configuration: Plugins', () => {
const instrumentedModules = Object.keys(defaultConfig.plugins);
let plugins: {[pluginName: string]: string}|null;

describe('plugin configuration', function() {
var plugins;
class ConfigTestPluginLoader extends PluginLoader {
constructor(logger: Logger, config: PluginLoaderConfig) {
super(logger, config);
plugins = config.plugins;
}
}

before(function() {
// When the plugin loader is activated, save the incoming value of plugins.
shimmer.wrap(pluginLoader, 'activate', function(original) {
return function(logger, config) {
plugins = config.plugins;
return original.apply(this, arguments);
};
});
before(() => {
trace.setPluginLoader(ConfigTestPluginLoader);
});

after(function() {
shimmer.unwrap(pluginLoader, 'activate');
after(() => {
trace.setPluginLoader(trace.TestPluginLoader);
});

afterEach(function() {
afterEach(() => {
plugins = null;
});

it('should have correct defaults', function() {
trace.start({forceNewAgent_: true});
assert.strictEqual(JSON.stringify(Object.keys(plugins)),
JSON.stringify(instrumentedModules));
for (var i = 0; i < instrumentedModules.length; i++) {
var name = instrumentedModules[i];
assert.ok(plugins[name].indexOf('plugin-' + name + '.js') !== -1);
}
it('should have correct defaults', () => {
trace.start();
assert.ok(plugins);
assert.strictEqual(
JSON.stringify(Object.keys(plugins!)),
JSON.stringify(instrumentedModules));
instrumentedModules.forEach(
e => assert.ok(plugins![e].includes(`plugin-${e}.js`)));
});

it('should handle empty object', function() {
trace.start({forceNewAgent_: true, plugins: {}});
assert.strictEqual(JSON.stringify(Object.keys(plugins)),
JSON.stringify(instrumentedModules));
assert.ok(instrumentedModules.every(function(e) {
return plugins[e].indexOf('plugin-' + e + '.js') !== -1;
}));
it('should handle empty object', () => {
trace.start({plugins: {}});
assert.ok(plugins);
assert.strictEqual(
JSON.stringify(Object.keys(plugins!)),
JSON.stringify(instrumentedModules));
instrumentedModules.forEach(
e => assert.ok(plugins![e].includes(`plugin-${e}.js`)));
});

it('should overwrite builtin plugins correctly', function() {
trace.start({forceNewAgent_: true, plugins: {
express: 'foo'
}});
assert.strictEqual(JSON.stringify(Object.keys(plugins)),
JSON.stringify(instrumentedModules));
assert.ok(instrumentedModules.filter(function(e) {
return e !== 'express';
}).every(function(e) {
return plugins[e].indexOf('plugin-' + e + '.js') !== -1;
}));
assert.strictEqual(plugins.express, 'foo');
it('should overwrite builtin plugins correctly', () => {
trace.start({plugins: {express: 'foo'}});
assert.ok(plugins);
assert.strictEqual(
JSON.stringify(Object.keys(plugins!)),
JSON.stringify(instrumentedModules));
instrumentedModules.filter(e => e !== 'express')
.forEach(e => assert.ok(plugins![e].includes(`plugin-${e}.js`)));
assert.strictEqual(plugins!.express, 'foo');
});
});

export default {};
16 changes: 8 additions & 8 deletions test/test-grpc-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@
*/
'use strict';

// Trace agent must be started out of the loop over gRPC versions,
// because express can't be re-patched.
var agent = require('../..').start({
projectId: '0',
samplingRate: 0
});

var common = require('./plugins/common'/*.js*/);

var assert = require('assert');
var express = require('./plugins/fixtures/express4');
var http = require('http');

var versions = {
Expand Down Expand Up @@ -49,14 +57,6 @@ function requestAndSendHTTPStatus(res, expectedReqs) {
}, expectedReqs);
}

// Trace agent must be started out of the loop over gRPC versions,
// because express can't be re-patched.
var agent = require('../..').start({
projectId: '0',
samplingRate: 0
});
var express = require('./plugins/fixtures/express4');

Object.keys(versions).forEach(function(version) {
var grpc;
var httpLogCount;
Expand Down
5 changes: 5 additions & 0 deletions test/test-trace-cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@ describe('test-trace-cluster', () => {
let axios: typeof axiosModule;
let express: typeof expressModule;
before(() => {
trace.setPluginLoader();
trace.start();
express = require('express');
axios = require('axios');
});

after(() => {
trace.setPluginLoader(trace.TestPluginLoader);
});

it('should not interfere with express span', async () => {
if (cluster.isMaster) {
await new Promise(resolve => {
Expand Down
5 changes: 5 additions & 0 deletions test/test-trace-web-frameworks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,15 @@ const FRAMEWORKS: WebFrameworkConstructor[] = [
describe('Web framework tracing', () => {
let axios: typeof axiosModule;
before(() => {
trace.setPluginLoader();
trace.start({ignoreUrls: [/ignore-me/]});
axios = require('axios');
});

after(() => {
trace.setPluginLoader(trace.TestPluginLoader);
});

FRAMEWORKS.forEach((webFrameworkConstructor) => {
const commonName = webFrameworkConstructor.commonName;
const expectedTopStackFrame = webFrameworkConstructor.expectedTopStackFrame;
Expand Down
4 changes: 0 additions & 4 deletions test/test-unpatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ describe('index.js', function() {
});
}

it('should wrap/unwrap module._load on start/stop', function() {
wrapTest(require('module'), '_load');
});

it('should wrap/unwrap http on start/stop', function() {
var http = require('http');
wrapTest(http, 'request');
Expand Down
Loading

0 comments on commit fa33f25

Please sign in to comment.