diff --git a/packages/opentelemetry-test-utils/package.json b/packages/opentelemetry-test-utils/package.json new file mode 100644 index 00000000000..689416fac11 --- /dev/null +++ b/packages/opentelemetry-test-utils/package.json @@ -0,0 +1,32 @@ +{ + "name": "@opentelemetry/test-utils", + "version": "0.31.0", + "description": "Test utilities.", + "main": "build/src/index.js", + "scripts": { + "precompile": "tsc --version", + "compile": "tsc -p .", + "prepare": "npm run compile" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/open-telemetry/opentelemetry-js.git" + }, + "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-test-utils/testUtils.ts b/packages/opentelemetry-test-utils/testUtils.ts new file mode 100644 index 00000000000..111fa751dad --- /dev/null +++ b/packages/opentelemetry-test-utils/testUtils.ts @@ -0,0 +1,67 @@ +/*! + * 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(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; + } +} diff --git a/packages/opentelemetry-test-utils/tsconfig.json b/packages/opentelemetry-test-utils/tsconfig.json new file mode 100644 index 00000000000..eba808d7cd1 --- /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 00000000000..0710b135d07 --- /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"] +}