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-postgres/opentelemetry-plugin-pg/package.json b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/package.json index 2e722b3096..ea027a18b7 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", "@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/test/pg.test.ts b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/pg.test.ts index b260131a1b..bec13efe79 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 @@ -33,7 +33,7 @@ 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(); @@ -103,7 +103,7 @@ describe('pg@7.x', () => { } tracer.addSpanProcessor(new SimpleSpanProcessor(memoryExporter)); if (testPostgresLocally) { - testUtils.startDocker(); + testUtils.startDocker('postgres'); } client = new pg.Client(CONFIG); @@ -116,7 +116,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..ec68cbfad7 100644 --- a/packages/opentelemetry-plugin-redis/test/redis.test.ts +++ b/packages/opentelemetry-plugin-redis/test/redis.test.ts @@ -23,7 +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 dockerUtils from '@opentelemetry/test-utils'; import * as assertionUtils from './assertionUtils'; import { SpanKind, Status, CanonicalCode } from '@opentelemetry/types'; import { AttributeNames } from '../src/enums'; @@ -64,7 +64,7 @@ describe('redis@2.x', () => { } if (shouldTestLocal) { - dockerUtils.startDocker(); + dockerUtils.startDocker('redis'); } redis = require('redis'); @@ -74,7 +74,7 @@ describe('redis@2.x', () => { after(() => { if (shouldTestLocal) { - dockerUtils.cleanUpDocker(); + dockerUtils.cleanUpDocker('redis'); } }); 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..e5bd4c0c98 --- /dev/null +++ b/packages/opentelemetry-test-utils/package.json @@ -0,0 +1,29 @@ +{ + "name": "@opentelemetry/test-utils", + "version": "0.3.1", + "description": "Test utilities.", + "main": "build/testUtils.js", + "scripts": { + "precompile": "tsc --version", + "compile": "tsc -p .", + "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": { + "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" + } +} diff --git a/packages/opentelemetry-plugin-mysql/test/testUtils.ts b/packages/opentelemetry-test-utils/testUtils.ts similarity index 64% rename from packages/opentelemetry-plugin-mysql/test/testUtils.ts rename to packages/opentelemetry-test-utils/testUtils.ts index a569ff57d1..111fa751da 100644 --- a/packages/opentelemetry-plugin-mysql/test/testUtils.ts +++ b/packages/opentelemetry-test-utils/testUtils.ts @@ -15,12 +15,23 @@ */ 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' - ), - ]; +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]; @@ -33,8 +44,9 @@ export function startDocker() { return true; } -export function cleanUpDocker() { - run('docker stop otmysql'); +export function cleanUpDocker(db: 'redis' | 'mysql' | 'postgres') { + run(`docker stop ot${db}`); + run(`docker rm ot${db}`); } function run(cmd: string) { 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"] +}