Skip to content

Commit 67d2256

Browse files
naseemkullahmayurkale22
authored andcommitted
feat: test-utils (#644)
* 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
1 parent c829a9c commit 67d2256

File tree

18 files changed

+144
-414
lines changed

18 files changed

+144
-414
lines changed

packages/opentelemetry-plugin-mysql/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
},
4242
"devDependencies": {
4343
"@opentelemetry/node": "^0.3.1",
44+
"@opentelemetry/test-utils": "^0.3.1",
4445
"@opentelemetry/tracing": "^0.3.1",
4546
"@types/mocha": "^5.2.7",
4647
"@types/mysql": "^2.15.4",

packages/opentelemetry-plugin-mysql/test/mysql.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
import * as assert from 'assert';
2525
import * as mysql from 'mysql';
2626
import { MysqlPlugin, plugin } from '../src';
27-
import * as testUtils from './testUtils';
27+
import * as testUtils from '@opentelemetry/test-utils';
2828
import { AttributeNames } from '../src/enums';
2929
import { CanonicalCode } from '@opentelemetry/types';
3030

@@ -54,7 +54,7 @@ describe('[email protected]', () => {
5454
}
5555
tracer.addSpanProcessor(new SimpleSpanProcessor(memoryExporter));
5656
if (testMysqlLocally) {
57-
testUtils.startDocker();
57+
testUtils.startDocker('mysql');
5858
// wait 15 seconds for docker container to start
5959
this.timeout(20000);
6060
setTimeout(done, 15000);
@@ -66,7 +66,7 @@ describe('[email protected]', () => {
6666
after(function() {
6767
if (testMysqlLocally) {
6868
this.timeout(5000);
69-
testUtils.cleanUpDocker();
69+
testUtils.cleanUpDocker('mysql');
7070
}
7171
});
7272

packages/opentelemetry-plugin-mysql/test/testUtils.ts

Lines changed: 0 additions & 55 deletions
This file was deleted.

packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
},
4848
"devDependencies": {
4949
"@opentelemetry/plugin-pg": "^0.3.1",
50+
"@opentelemetry/test-utils": "^0.3.1",
5051
"@types/mocha": "^5.2.7",
5152
"@types/node": "^12.6.9",
5253
"@types/pg": "^7.11.2",

packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/assertionUtils.ts

Lines changed: 0 additions & 79 deletions
This file was deleted.

packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/pg-pool.test.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,21 @@ import {
2020
InMemorySpanExporter,
2121
SimpleSpanProcessor,
2222
} from '@opentelemetry/tracing';
23-
import { SpanKind, Attributes, TimedEvent, Span } from '@opentelemetry/types';
23+
import {
24+
SpanKind,
25+
Attributes,
26+
TimedEvent,
27+
Span,
28+
CanonicalCode,
29+
Status,
30+
} from '@opentelemetry/types';
2431
import { plugin as pgPlugin, PostgresPlugin } from '@opentelemetry/plugin-pg';
2532
import { plugin, PostgresPoolPlugin } from '../src';
2633
import { AttributeNames } from '../src/enums';
2734
import * as assert from 'assert';
2835
import * as pg from 'pg';
2936
import * as pgPool from 'pg-pool';
30-
import * as assertionUtils from './assertionUtils';
31-
import * as testUtils from './testUtils';
37+
import * as testUtils from '@opentelemetry/test-utils';
3238

3339
const memoryExporter = new InMemorySpanExporter();
3440

@@ -65,18 +71,23 @@ const DEFAULT_PG_ATTRIBUTES = {
6571
[AttributeNames.DB_USER]: CONFIG.user,
6672
};
6773

74+
const okStatus: Status = {
75+
code: CanonicalCode.OK,
76+
};
77+
6878
const runCallbackTest = (
6979
parentSpan: Span,
7080
attributes: Attributes,
7181
events: TimedEvent[],
82+
status: Status = okStatus,
7283
spansLength = 1,
7384
spansIndex = 0
7485
) => {
7586
const spans = memoryExporter.getFinishedSpans();
7687
assert.strictEqual(spans.length, spansLength);
7788
const pgSpan = spans[spansIndex];
78-
assertionUtils.assertSpan(pgSpan, SpanKind.CLIENT, attributes, events);
79-
assertionUtils.assertPropagation(pgSpan, parentSpan);
89+
testUtils.assertSpan(pgSpan, SpanKind.CLIENT, attributes, events, status);
90+
testUtils.assertPropagation(pgSpan, parentSpan);
8091
};
8192

8293
describe('[email protected]', () => {
@@ -97,14 +108,14 @@ describe('[email protected]', () => {
97108
pool = new pgPool(CONFIG);
98109
tracer.addSpanProcessor(new SimpleSpanProcessor(memoryExporter));
99110
if (testPostgresLocally) {
100-
testUtils.startDocker();
111+
testUtils.startDocker('postgres');
101112
}
102113
done();
103114
});
104115

105116
after(function(done) {
106117
if (testPostgresLocally) {
107-
testUtils.cleanUpDocker();
118+
testUtils.cleanUpDocker('postgres');
108119
}
109120
pool.end(() => {
110121
done();
@@ -144,11 +155,11 @@ describe('[email protected]', () => {
144155
const span = tracer.startSpan('test span');
145156
await tracer.withSpan(span, async () => {
146157
const client = await pool.connect();
147-
runCallbackTest(span, pgPoolattributes, events, 1, 0);
158+
runCallbackTest(span, pgPoolattributes, events, okStatus, 1, 0);
148159
assert.ok(client, 'pool.connect() returns a promise');
149160
try {
150161
await client.query('SELECT NOW()');
151-
runCallbackTest(span, pgAttributes, events, 2, 1);
162+
runCallbackTest(span, pgAttributes, events, okStatus, 2, 1);
152163
} catch (e) {
153164
throw e;
154165
} finally {
@@ -175,13 +186,13 @@ describe('[email protected]', () => {
175186
}
176187
release();
177188
assert.ok(client);
178-
runCallbackTest(parentSpan, pgPoolattributes, events, 1, 0);
189+
runCallbackTest(parentSpan, pgPoolattributes, events, okStatus, 1, 0);
179190
client.query('SELECT NOW()', (err, ret) => {
180191
if (err) {
181192
return done(err);
182193
}
183194
assert.ok(ret);
184-
runCallbackTest(parentSpan, pgAttributes, events, 2, 1);
195+
runCallbackTest(parentSpan, pgAttributes, events, okStatus, 2, 1);
185196
done();
186197
});
187198
});
@@ -205,8 +216,8 @@ describe('[email protected]', () => {
205216
await tracer.withSpan(span, async () => {
206217
try {
207218
const result = await pool.query('SELECT NOW()');
208-
runCallbackTest(span, pgPoolattributes, events, 2, 0);
209-
runCallbackTest(span, pgAttributes, events, 2, 1);
219+
runCallbackTest(span, pgPoolattributes, events, okStatus, 2, 0);
220+
runCallbackTest(span, pgAttributes, events, okStatus, 2, 1);
210221
assert.ok(result, 'pool.query() returns a promise');
211222
} catch (e) {
212223
throw e;
@@ -230,8 +241,8 @@ describe('[email protected]', () => {
230241
if (err) {
231242
return done(err);
232243
}
233-
runCallbackTest(parentSpan, pgPoolattributes, events, 2, 0);
234-
runCallbackTest(parentSpan, pgAttributes, events, 2, 1);
244+
runCallbackTest(parentSpan, pgPoolattributes, events, okStatus, 2, 0);
245+
runCallbackTest(parentSpan, pgAttributes, events, okStatus, 2, 1);
235246
done();
236247
});
237248
assert.strictEqual(resNoPromise, undefined, 'No promise is returned');

packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/testUtils.ts

Lines changed: 0 additions & 54 deletions
This file was deleted.

packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"access": "public"
4545
},
4646
"devDependencies": {
47+
"@opentelemetry/test-utils": "^0.3.1",
4748
"@opentelemetry/node": "^0.3.1",
4849
"@opentelemetry/tracing": "^0.3.1",
4950
"@types/mocha": "^5.2.7",

0 commit comments

Comments
 (0)