From 67d22567d482134e6539c44c3b121186bd95ca4c Mon Sep 17 00:00:00 2001 From: Naseem Date: Mon, 23 Dec 2019 19:55:15 -0500 Subject: [PATCH] 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 --- .../opentelemetry-plugin-mysql/package.json | 1 + .../test/mysql.test.ts | 6 +- .../test/testUtils.ts | 55 ------------- .../opentelemetry-plugin-pg-pool/package.json | 1 + .../test/assertionUtils.ts | 79 ------------------ .../test/pg-pool.test.ts | 41 ++++++---- .../test/testUtils.ts | 54 ------------ .../opentelemetry-plugin-pg/package.json | 1 + .../test/assertionUtils.ts | 82 ------------------- .../opentelemetry-plugin-pg/test/pg.test.ts | 17 ++-- .../opentelemetry-plugin-pg/test/testUtils.ts | 54 ------------ .../opentelemetry-plugin-redis/package.json | 1 + .../test/redis.test.ts | 11 ++- .../test/testUtils.ts | 54 ------------ .../opentelemetry-test-utils/package.json | 35 ++++++++ .../testUtils.ts} | 52 ++++++++++++ .../opentelemetry-test-utils/tsconfig.json | 10 +++ packages/opentelemetry-test-utils/tslint.json | 4 + 18 files changed, 144 insertions(+), 414 deletions(-) delete mode 100644 packages/opentelemetry-plugin-mysql/test/testUtils.ts delete mode 100644 packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/assertionUtils.ts delete mode 100644 packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/testUtils.ts delete mode 100644 packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/assertionUtils.ts delete mode 100644 packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/testUtils.ts delete mode 100644 packages/opentelemetry-plugin-redis/test/testUtils.ts create mode 100644 packages/opentelemetry-test-utils/package.json rename packages/{opentelemetry-plugin-redis/test/assertionUtils.ts => opentelemetry-test-utils/testUtils.ts} (62%) create mode 100644 packages/opentelemetry-test-utils/tsconfig.json create mode 100644 packages/opentelemetry-test-utils/tslint.json diff --git a/packages/opentelemetry-plugin-mysql/package.json b/packages/opentelemetry-plugin-mysql/package.json index 9d02fd7377..2efc94f94c 100644 --- a/packages/opentelemetry-plugin-mysql/package.json +++ b/packages/opentelemetry-plugin-mysql/package.json @@ -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", diff --git a/packages/opentelemetry-plugin-mysql/test/mysql.test.ts b/packages/opentelemetry-plugin-mysql/test/mysql.test.ts index 600b3aa9b3..cd207a7216 100644 --- a/packages/opentelemetry-plugin-mysql/test/mysql.test.ts +++ b/packages/opentelemetry-plugin-mysql/test/mysql.test.ts @@ -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'; @@ -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); @@ -66,7 +66,7 @@ describe('mysql@2.x', () => { after(function() { if (testMysqlLocally) { this.timeout(5000); - testUtils.cleanUpDocker(); + testUtils.cleanUpDocker('mysql'); } }); diff --git a/packages/opentelemetry-plugin-mysql/test/testUtils.ts b/packages/opentelemetry-plugin-mysql/test/testUtils.ts deleted file mode 100644 index a569ff57d1..0000000000 --- a/packages/opentelemetry-plugin-mysql/test/testUtils.ts +++ /dev/null @@ -1,55 +0,0 @@ -/*! - * Copyright 2019, OpenTelemetry Authors - * - * 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 - * - * https://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. - */ - -import * as childProcess from 'child_process'; -export function startDocker() { - const tasks = [ - run( - 'docker run --rm -d -e MYSQL_ROOT_PASSWORD=rootpw -e MYSQL_DATABASE=test_db -e MYSQL_USER=otel -e MYSQL_PASSWORD=secret -p 33306:3306 --name otmysql circleci/mysql:5.7' - ), - ]; - - for (let i = 0; i < tasks.length; i++) { - const task = tasks[i]; - if (task && task.code !== 0) { - console.error('Failed to start container!'); - console.error(task.output); - return false; - } - } - return true; -} - -export function cleanUpDocker() { - run('docker stop otmysql'); -} - -function run(cmd: string) { - try { - const proc = childProcess.spawnSync(cmd, { - shell: true, - }); - return { - code: proc.status, - output: proc.output - .map(v => String.fromCharCode.apply(null, v as any)) - .join(''), - }; - } catch (e) { - console.log(e); - return; - } -} diff --git a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/package.json b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/package.json index 4697636985..d02fb84ddc 100644 --- a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/package.json +++ b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/package.json @@ -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", diff --git a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/assertionUtils.ts b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/assertionUtils.ts deleted file mode 100644 index a964d47a2f..0000000000 --- a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/assertionUtils.ts +++ /dev/null @@ -1,79 +0,0 @@ -/*! - * Copyright 2019, OpenTelemetry Authors - * - * 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 - * - * https://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. - */ - -import { - SpanKind, - Attributes, - Event, - Span, - TimedEvent, -} from '@opentelemetry/types'; -import * as assert from 'assert'; -import { ReadableSpan } from '@opentelemetry/tracing'; -import { - hrTimeToMilliseconds, - hrTimeToMicroseconds, -} from '@opentelemetry/core'; - -export const assertSpan = ( - span: ReadableSpan, - kind: SpanKind, - attributes: Attributes, - events: Event[] -) => { - assert.strictEqual(span.spanContext.traceId.length, 32); - assert.strictEqual(span.spanContext.spanId.length, 16); - assert.strictEqual(span.kind, kind); - - // check all the AttributeNames fields - Object.keys(span.attributes).forEach(key => { - assert.deepStrictEqual(span.attributes[key], attributes[key]); - }); - - assert.ok(span.endTime); - assert.strictEqual(span.links.length, 0); - - assert.ok( - hrTimeToMicroseconds(span.startTime) < hrTimeToMicroseconds(span.endTime) - ); - assert.ok(hrTimeToMilliseconds(span.endTime) > 0); - - // events - assert.strictEqual( - span.events.length, - events.length, - 'Should contain same number of events' - ); - span.events.forEach((_: TimedEvent, index: number) => { - assert.deepStrictEqual(span.events[index], events[index]); - }); -}; - -// Check if sourceSpan was propagated to targetSpan -export const assertPropagation = ( - childSpan: ReadableSpan, - parentSpan: Span -) => { - const targetSpanContext = childSpan.spanContext; - const sourceSpanContext = parentSpan.context(); - assert.strictEqual(targetSpanContext.traceId, sourceSpanContext.traceId); - assert.strictEqual(childSpan.parentSpanId, sourceSpanContext.spanId); - assert.strictEqual( - targetSpanContext.traceFlags, - sourceSpanContext.traceFlags - ); - assert.notStrictEqual(targetSpanContext.spanId, sourceSpanContext.spanId); -}; diff --git a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/pg-pool.test.ts b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/pg-pool.test.ts index 73ca6650a1..28ad86113a 100644 --- a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/pg-pool.test.ts +++ b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/pg-pool.test.ts @@ -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(); @@ -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', () => { @@ -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(); @@ -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 { @@ -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(); }); }); @@ -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; @@ -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'); diff --git a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/testUtils.ts b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/testUtils.ts deleted file mode 100644 index eec866fc41..0000000000 --- a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/testUtils.ts +++ /dev/null @@ -1,54 +0,0 @@ -/*! - * Copyright 2019, OpenTelemetry Authors - * - * 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 - * - * https://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. - */ - -import * as childProcess from 'child_process'; -export function startDocker() { - const tasks = [ - run('docker run -d -p 54320:5432 --name otpostgres postgres:alpine'), - ]; - - for (let i = 0; i < tasks.length; i++) { - const task = tasks[i]; - if (task && task.code !== 0) { - console.error('Failed to start container!'); - console.error(task.output); - return false; - } - } - return true; -} - -export function cleanUpDocker() { - run('docker stop otpostgres'); - run('docker rm otpostgres'); -} - -function run(cmd: string) { - try { - const proc = childProcess.spawnSync(cmd, { - shell: true, - }); - return { - code: proc.status, - output: proc.output - .map(v => String.fromCharCode.apply(null, v as any)) - .join(''), - }; - } catch (e) { - console.log(e); - return; - } -} diff --git a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/package.json b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/package.json index 34f685c53e..43b3ccb16f 100644 --- a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/package.json +++ b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/package.json @@ -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", diff --git a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/assertionUtils.ts b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/assertionUtils.ts deleted file mode 100644 index 2c81918305..0000000000 --- a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/assertionUtils.ts +++ /dev/null @@ -1,82 +0,0 @@ -/*! - * Copyright 2019, OpenTelemetry Authors - * - * 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 - * - * https://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. - */ - -import { - SpanKind, - Attributes, - Event, - Span, - Status, -} from '@opentelemetry/types'; -import * as assert from 'assert'; -import { PostgresPlugin } from '../src'; -import { ReadableSpan } from '@opentelemetry/tracing'; -import { - hrTimeToMilliseconds, - hrTimeToMicroseconds, -} from '@opentelemetry/core'; -import { AttributeNames } from '../src/enums'; - -export const assertSpan = ( - span: ReadableSpan, - kind: SpanKind, - attributes: Attributes, - events: Event[], - status: Status -) => { - assert.strictEqual(span.spanContext.traceId.length, 32); - assert.strictEqual(span.spanContext.spanId.length, 16); - assert.strictEqual(span.kind, kind); - - assert.strictEqual( - span.attributes[AttributeNames.COMPONENT], - PostgresPlugin.COMPONENT - ); - assert.ok(span.endTime); - assert.strictEqual(span.links.length, 0); - - assert.ok( - hrTimeToMicroseconds(span.startTime) < hrTimeToMicroseconds(span.endTime) - ); - assert.ok(hrTimeToMilliseconds(span.endTime) > 0); - - // attributes - assert.deepStrictEqual(span.attributes, attributes); - - // events - assert.deepStrictEqual(span.events, events); - - assert.strictEqual(span.status.code, status.code); - if (status.message) { - assert.strictEqual(span.status.message, status.message); - } -}; - -// Check if sourceSpan was propagated to targetSpan -export const assertPropagation = ( - childSpan: ReadableSpan, - parentSpan: Span -) => { - const targetSpanContext = childSpan.spanContext; - const sourceSpanContext = parentSpan.context(); - assert.strictEqual(targetSpanContext.traceId, sourceSpanContext.traceId); - assert.strictEqual(childSpan.parentSpanId, sourceSpanContext.spanId); - assert.strictEqual( - targetSpanContext.traceFlags, - sourceSpanContext.traceFlags - ); - assert.notStrictEqual(targetSpanContext.spanId, sourceSpanContext.spanId); -}; diff --git a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/pg.test.ts b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/pg.test.ts index b260131a1b..c508f5840f 100644 --- a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/pg.test.ts +++ b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/pg.test.ts @@ -32,8 +32,7 @@ import { plugin, PostgresPlugin } from '../src'; import { AttributeNames } from '../src/enums'; import * as assert from 'assert'; import * as pg from 'pg'; -import * as assertionUtils from './assertionUtils'; -import * as testUtils from './testUtils'; +import * as testUtils from '@opentelemetry/test-utils'; const memoryExporter = new InMemorySpanExporter(); @@ -74,15 +73,9 @@ const runCallbackTest = ( const spans = memoryExporter.getFinishedSpans(); assert.strictEqual(spans.length, spansLength); const pgSpan = spans[spansIndex]; - assertionUtils.assertSpan( - pgSpan, - SpanKind.CLIENT, - attributes, - events, - status - ); + testUtils.assertSpan(pgSpan, SpanKind.CLIENT, attributes, events, status); if (span) { - assertionUtils.assertPropagation(pgSpan, span); + testUtils.assertPropagation(pgSpan, span); } }; @@ -103,7 +96,7 @@ describe('pg@7.x', () => { } tracer.addSpanProcessor(new SimpleSpanProcessor(memoryExporter)); if (testPostgresLocally) { - testUtils.startDocker(); + testUtils.startDocker('postgres'); } client = new pg.Client(CONFIG); @@ -116,7 +109,7 @@ describe('pg@7.x', () => { after(async () => { if (testPostgresLocally) { - testUtils.cleanUpDocker(); + testUtils.cleanUpDocker('postgres'); } await client.end(); }); diff --git a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/testUtils.ts b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/testUtils.ts deleted file mode 100644 index eec866fc41..0000000000 --- a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/testUtils.ts +++ /dev/null @@ -1,54 +0,0 @@ -/*! - * Copyright 2019, OpenTelemetry Authors - * - * 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 - * - * https://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. - */ - -import * as childProcess from 'child_process'; -export function startDocker() { - const tasks = [ - run('docker run -d -p 54320:5432 --name otpostgres postgres:alpine'), - ]; - - for (let i = 0; i < tasks.length; i++) { - const task = tasks[i]; - if (task && task.code !== 0) { - console.error('Failed to start container!'); - console.error(task.output); - return false; - } - } - return true; -} - -export function cleanUpDocker() { - run('docker stop otpostgres'); - run('docker rm otpostgres'); -} - -function run(cmd: string) { - try { - const proc = childProcess.spawnSync(cmd, { - shell: true, - }); - return { - code: proc.status, - output: proc.output - .map(v => String.fromCharCode.apply(null, v as any)) - .join(''), - }; - } catch (e) { - console.log(e); - return; - } -} diff --git a/packages/opentelemetry-plugin-redis/package.json b/packages/opentelemetry-plugin-redis/package.json index 1c1f7226c0..f9c910fd4c 100644 --- a/packages/opentelemetry-plugin-redis/package.json +++ b/packages/opentelemetry-plugin-redis/package.json @@ -43,6 +43,7 @@ }, "devDependencies": { "@opentelemetry/node": "^0.3.1", + "@opentelemetry/test-utils": "^0.3.1", "@opentelemetry/tracing": "^0.3.1", "@types/mocha": "^5.2.7", "@types/node": "^12.6.9", diff --git a/packages/opentelemetry-plugin-redis/test/redis.test.ts b/packages/opentelemetry-plugin-redis/test/redis.test.ts index 72d6730a69..422cfa995a 100644 --- a/packages/opentelemetry-plugin-redis/test/redis.test.ts +++ b/packages/opentelemetry-plugin-redis/test/redis.test.ts @@ -23,8 +23,7 @@ import { NodeTracer } from '@opentelemetry/node'; import { plugin, RedisPlugin } from '../src'; import * as redisTypes from 'redis'; import { NoopLogger } from '@opentelemetry/core'; -import * as dockerUtils from './testUtils'; -import * as assertionUtils from './assertionUtils'; +import * as testUtils from '@opentelemetry/test-utils'; import { SpanKind, Status, CanonicalCode } from '@opentelemetry/types'; import { AttributeNames } from '../src/enums'; @@ -64,7 +63,7 @@ describe('redis@2.x', () => { } if (shouldTestLocal) { - dockerUtils.startDocker(); + testUtils.startDocker('redis'); } redis = require('redis'); @@ -74,7 +73,7 @@ describe('redis@2.x', () => { after(() => { if (shouldTestLocal) { - dockerUtils.cleanUpDocker(); + testUtils.cleanUpDocker('redis'); } }); @@ -174,14 +173,14 @@ describe('redis@2.x', () => { endedSpans[0].name, `redis-${operation.command}` ); - assertionUtils.assertSpan( + testUtils.assertSpan( endedSpans[0], SpanKind.CLIENT, attributes, [], okStatus ); - assertionUtils.assertPropagation(endedSpans[0], span); + testUtils.assertPropagation(endedSpans[0], span); done(); }); }); diff --git a/packages/opentelemetry-plugin-redis/test/testUtils.ts b/packages/opentelemetry-plugin-redis/test/testUtils.ts deleted file mode 100644 index a40b0b4aab..0000000000 --- a/packages/opentelemetry-plugin-redis/test/testUtils.ts +++ /dev/null @@ -1,54 +0,0 @@ -/*! - * Copyright 2019, OpenTelemetry Authors - * - * 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 - * - * https://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. - */ - -import * as childProcess from 'child_process'; -export function startDocker() { - const tasks = [ - run('docker run -d -p 63790:6379 --name otjsredis redis:alpine'), - ]; - - for (let i = 0; i < tasks.length; i++) { - const task = tasks[i]; - if (task && task.code !== 0) { - console.error('Failed to start container!'); - console.error(task.output); - return false; - } - } - return true; -} - -export function cleanUpDocker() { - run('docker stop otjsredis'); - run('docker rm otjsredis'); -} - -function run(cmd: string) { - try { - const proc = childProcess.spawnSync(cmd, { - shell: true, - }); - return { - code: proc.status, - output: proc.output - .map(v => String.fromCharCode.apply(null, v as any)) - .join(''), - }; - } catch (e) { - console.log(e); - return; - } -} diff --git a/packages/opentelemetry-test-utils/package.json b/packages/opentelemetry-test-utils/package.json new file mode 100644 index 0000000000..80a42ddd19 --- /dev/null +++ b/packages/opentelemetry-test-utils/package.json @@ -0,0 +1,35 @@ +{ + "name": "@opentelemetry/test-utils", + "version": "0.3.1", + "description": "Test utilities.", + "main": "build/testUtils.js", + "scripts": { + "check": "gts check", + "compile": "tsc -p .", + "fix": "gts fix", + "precompile": "tsc --version", + "prepare": "npm run compile" + }, + "repository": "open-telemetry/opentelemetry-js", + "keywords": [ + "opentelemetry", + "test-utils" + ], + "author": "OpenTelemetry Authors", + "license": "Apache-2.0", + "bugs": { + "url": "https://github.com/open-telemetry/opentelemetry-js/issues" + }, + "homepage": "https://github.com/open-telemetry/opentelemetry-js#readme", + "devDependencies": { + "@opentelemetry/core": "^0.3.1", + "@opentelemetry/tracing": "^0.3.1", + "@opentelemetry/types": "^0.3.1", + "gts": "^1.1.2", + "ts-node": "^8.5.4", + "tslint-consistent-codestyle": "^1.16.0", + "tslint-microsoft-contrib": "^6.2.0", + "typescript": "3.7.4" + }, + "dependencies": {} +} diff --git a/packages/opentelemetry-plugin-redis/test/assertionUtils.ts b/packages/opentelemetry-test-utils/testUtils.ts similarity index 62% rename from packages/opentelemetry-plugin-redis/test/assertionUtils.ts rename to packages/opentelemetry-test-utils/testUtils.ts index fdb517c7a0..f0eb0aa702 100644 --- a/packages/opentelemetry-plugin-redis/test/assertionUtils.ts +++ b/packages/opentelemetry-test-utils/testUtils.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +import * as childProcess from 'child_process'; import { SpanKind, Attributes, @@ -28,6 +29,57 @@ import { hrTimeToMicroseconds, } from '@opentelemetry/core'; +export function startDocker(db: 'redis' | 'mysql' | 'postgres') { + let dockerRunCmd; + switch (db) { + case 'redis': + dockerRunCmd = `docker run -d -p 63790:6379 --name ot${db} ${db}:alpine`; + break; + + case 'mysql': + dockerRunCmd = `docker run --rm -d -e MYSQL_ROOT_PASSWORD=rootpw -e MYSQL_DATABASE=test_db -e MYSQL_USER=otel -e MYSQL_PASSWORD=secret -p 33306:3306 --name ot${db} circleci/${db}:5.7`; + break; + + case 'postgres': + dockerRunCmd = `docker run -d -p 54320:5432 --name ot${db} ${db}:alpine`; + break; + } + + const tasks = [run(dockerRunCmd)]; + + for (let i = 0; i < tasks.length; i++) { + const task = tasks[i]; + if (task && task.code !== 0) { + console.error('Failed to start container!'); + console.error(task.output); + return false; + } + } + return true; +} + +export function cleanUpDocker(db: 'redis' | 'mysql' | 'postgres') { + run(`docker stop ot${db}`); + run(`docker rm ot${db}`); +} + +function run(cmd: string) { + try { + const proc = childProcess.spawnSync(cmd, { + shell: true, + }); + return { + code: proc.status, + output: proc.output + .map(v => String.fromCharCode.apply(null, v as any)) + .join(''), + }; + } catch (e) { + console.log(e); + return; + } +} + export const assertSpan = ( span: ReadableSpan, kind: SpanKind, diff --git a/packages/opentelemetry-test-utils/tsconfig.json b/packages/opentelemetry-test-utils/tsconfig.json new file mode 100644 index 0000000000..eba808d7cd --- /dev/null +++ b/packages/opentelemetry-test-utils/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../tsconfig.base", + "compilerOptions": { + "rootDir": ".", + "outDir": "build" + }, + "include": [ + "*.ts" + ] +} diff --git a/packages/opentelemetry-test-utils/tslint.json b/packages/opentelemetry-test-utils/tslint.json new file mode 100644 index 0000000000..0710b135d0 --- /dev/null +++ b/packages/opentelemetry-test-utils/tslint.json @@ -0,0 +1,4 @@ +{ + "rulesDirectory": ["node_modules/tslint-microsoft-contrib"], + "extends": ["../../tslint.base.js", "./node_modules/tslint-consistent-codestyle"] +}