Skip to content

Commit

Permalink
feat: test-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
naseemkullah committed Dec 22, 2019
1 parent d8c8509 commit edcdb4a
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
32 changes: 32 additions & 0 deletions packages/opentelemetry-test-utils/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
67 changes: 67 additions & 0 deletions packages/opentelemetry-test-utils/testUtils.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
10 changes: 10 additions & 0 deletions packages/opentelemetry-test-utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../tsconfig.base",
"compilerOptions": {
"rootDir": ".",
"outDir": "build"
},
"include": [
"*.ts"
]
}
4 changes: 4 additions & 0 deletions packages/opentelemetry-test-utils/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"rulesDirectory": ["node_modules/tslint-microsoft-contrib"],
"extends": ["../../tslint.base.js", "./node_modules/tslint-consistent-codestyle"]
}

0 comments on commit edcdb4a

Please sign in to comment.