Skip to content

Commit

Permalink
feat: test-utils (#644)
Browse files Browse the repository at this point in the history
* feat: test-utils

* fix: add check,fix scripts

* feat: use test-utils package in pg pool plugin

* feat: include assertionUtils in test-utils package

And use for redis and pg

* fix: install required packages

* fix: gts
  • Loading branch information
naseemkullah authored and mayurkale22 committed Dec 24, 2019
1 parent c829a9c commit 67d2256
Show file tree
Hide file tree
Showing 18 changed files with 144 additions and 414 deletions.
1 change: 1 addition & 0 deletions packages/opentelemetry-plugin-mysql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
},
"devDependencies": {
"@opentelemetry/node": "^0.3.1",
"@opentelemetry/test-utils": "^0.3.1",
"@opentelemetry/tracing": "^0.3.1",
"@types/mocha": "^5.2.7",
"@types/mysql": "^2.15.4",
Expand Down
6 changes: 3 additions & 3 deletions packages/opentelemetry-plugin-mysql/test/mysql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
import * as assert from 'assert';
import * as mysql from 'mysql';
import { MysqlPlugin, plugin } from '../src';
import * as testUtils from './testUtils';
import * as testUtils from '@opentelemetry/test-utils';
import { AttributeNames } from '../src/enums';
import { CanonicalCode } from '@opentelemetry/types';

Expand Down Expand Up @@ -54,7 +54,7 @@ describe('mysql@2.x', () => {
}
tracer.addSpanProcessor(new SimpleSpanProcessor(memoryExporter));
if (testMysqlLocally) {
testUtils.startDocker();
testUtils.startDocker('mysql');
// wait 15 seconds for docker container to start
this.timeout(20000);
setTimeout(done, 15000);
Expand All @@ -66,7 +66,7 @@ describe('mysql@2.x', () => {
after(function() {
if (testMysqlLocally) {
this.timeout(5000);
testUtils.cleanUpDocker();
testUtils.cleanUpDocker('mysql');
}
});

Expand Down
55 changes: 0 additions & 55 deletions packages/opentelemetry-plugin-mysql/test/testUtils.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
},
"devDependencies": {
"@opentelemetry/plugin-pg": "^0.3.1",
"@opentelemetry/test-utils": "^0.3.1",
"@types/mocha": "^5.2.7",
"@types/node": "^12.6.9",
"@types/pg": "^7.11.2",
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,21 @@ import {
InMemorySpanExporter,
SimpleSpanProcessor,
} from '@opentelemetry/tracing';
import { SpanKind, Attributes, TimedEvent, Span } from '@opentelemetry/types';
import {
SpanKind,
Attributes,
TimedEvent,
Span,
CanonicalCode,
Status,
} from '@opentelemetry/types';
import { plugin as pgPlugin, PostgresPlugin } from '@opentelemetry/plugin-pg';
import { plugin, PostgresPoolPlugin } from '../src';
import { AttributeNames } from '../src/enums';
import * as assert from 'assert';
import * as pg from 'pg';
import * as pgPool from 'pg-pool';
import * as assertionUtils from './assertionUtils';
import * as testUtils from './testUtils';
import * as testUtils from '@opentelemetry/test-utils';

const memoryExporter = new InMemorySpanExporter();

Expand Down Expand Up @@ -65,18 +71,23 @@ const DEFAULT_PG_ATTRIBUTES = {
[AttributeNames.DB_USER]: CONFIG.user,
};

const okStatus: Status = {
code: CanonicalCode.OK,
};

const runCallbackTest = (
parentSpan: Span,
attributes: Attributes,
events: TimedEvent[],
status: Status = okStatus,
spansLength = 1,
spansIndex = 0
) => {
const spans = memoryExporter.getFinishedSpans();
assert.strictEqual(spans.length, spansLength);
const pgSpan = spans[spansIndex];
assertionUtils.assertSpan(pgSpan, SpanKind.CLIENT, attributes, events);
assertionUtils.assertPropagation(pgSpan, parentSpan);
testUtils.assertSpan(pgSpan, SpanKind.CLIENT, attributes, events, status);
testUtils.assertPropagation(pgSpan, parentSpan);
};

describe('pg-pool@2.x', () => {
Expand All @@ -97,14 +108,14 @@ describe('pg-pool@2.x', () => {
pool = new pgPool(CONFIG);
tracer.addSpanProcessor(new SimpleSpanProcessor(memoryExporter));
if (testPostgresLocally) {
testUtils.startDocker();
testUtils.startDocker('postgres');
}
done();
});

after(function(done) {
if (testPostgresLocally) {
testUtils.cleanUpDocker();
testUtils.cleanUpDocker('postgres');
}
pool.end(() => {
done();
Expand Down Expand Up @@ -144,11 +155,11 @@ describe('pg-pool@2.x', () => {
const span = tracer.startSpan('test span');
await tracer.withSpan(span, async () => {
const client = await pool.connect();
runCallbackTest(span, pgPoolattributes, events, 1, 0);
runCallbackTest(span, pgPoolattributes, events, okStatus, 1, 0);
assert.ok(client, 'pool.connect() returns a promise');
try {
await client.query('SELECT NOW()');
runCallbackTest(span, pgAttributes, events, 2, 1);
runCallbackTest(span, pgAttributes, events, okStatus, 2, 1);
} catch (e) {
throw e;
} finally {
Expand All @@ -175,13 +186,13 @@ describe('pg-pool@2.x', () => {
}
release();
assert.ok(client);
runCallbackTest(parentSpan, pgPoolattributes, events, 1, 0);
runCallbackTest(parentSpan, pgPoolattributes, events, okStatus, 1, 0);
client.query('SELECT NOW()', (err, ret) => {
if (err) {
return done(err);
}
assert.ok(ret);
runCallbackTest(parentSpan, pgAttributes, events, 2, 1);
runCallbackTest(parentSpan, pgAttributes, events, okStatus, 2, 1);
done();
});
});
Expand All @@ -205,8 +216,8 @@ describe('pg-pool@2.x', () => {
await tracer.withSpan(span, async () => {
try {
const result = await pool.query('SELECT NOW()');
runCallbackTest(span, pgPoolattributes, events, 2, 0);
runCallbackTest(span, pgAttributes, events, 2, 1);
runCallbackTest(span, pgPoolattributes, events, okStatus, 2, 0);
runCallbackTest(span, pgAttributes, events, okStatus, 2, 1);
assert.ok(result, 'pool.query() returns a promise');
} catch (e) {
throw e;
Expand All @@ -230,8 +241,8 @@ describe('pg-pool@2.x', () => {
if (err) {
return done(err);
}
runCallbackTest(parentSpan, pgPoolattributes, events, 2, 0);
runCallbackTest(parentSpan, pgAttributes, events, 2, 1);
runCallbackTest(parentSpan, pgPoolattributes, events, okStatus, 2, 0);
runCallbackTest(parentSpan, pgAttributes, events, okStatus, 2, 1);
done();
});
assert.strictEqual(resNoPromise, undefined, 'No promise is returned');
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"access": "public"
},
"devDependencies": {
"@opentelemetry/test-utils": "^0.3.1",
"@opentelemetry/node": "^0.3.1",
"@opentelemetry/tracing": "^0.3.1",
"@types/mocha": "^5.2.7",
Expand Down
Loading

0 comments on commit 67d2256

Please sign in to comment.