Skip to content

Commit

Permalink
test: improved local test running
Browse files Browse the repository at this point in the history
- node script to add aliases
- integrate mocha watch
- removed redundant sh helper scripts
  • Loading branch information
kirrg001 committed Nov 12, 2024
1 parent d4999de commit e80ee65
Show file tree
Hide file tree
Showing 31 changed files with 125 additions and 168 deletions.
26 changes: 22 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,33 @@ Install the [`aws-cli`](https://docs.aws.amazon.com/cli/latest/userguide/getting

## Executing Tests Locally

Some of the tests require infrastructure components (databases etc.) to run locally. The easiest way to run all required components locally is to use Docker and on top of this [Docker Compose](https://docs.docker.com/compose/). Start the script `node bin/start-test-containers.js` to run all containers (not recommended).

Instead:
Some of the tests require infrastructure components (databases etc.) to run locally. The easiest way to run required components locally is to use Docker and on top of this [Docker Compose](https://docs.docker.com/compose/).

```sh
node bin/start-test-containers.js --mongo --redis
```

It's not recommended to run all tests using `bin/run-tests.sh` - it takes too long. Take a look at the root package.json and run a specific test group or run e.g. `bin/run-collector-tests.sh` with the mocha `.only` attribute.
### Using terminal aliases

Add aliases to your terminal:

```sh
node bin/add-test-aliases.js bash|zsh
```

Add a mocha `.only` on the test you would like to execute and then run the target scope:

```sh
runcollector
runcollector-nw (no watch)
```

### Manually

```sh
bin/run-tests.sh --scope=@instana/collector
bin/run-tests.sh --scope=@instana/collector --watch
```

If you want to see the Node.js collector's debug output while running the tests, make sure the environment variable `WITH_STDOUT` is set to a non-empty string.

Expand Down
62 changes: 62 additions & 0 deletions bin/add-test-aliases.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* (c) Copyright IBM Corp. 2024
*/

'use strict';

const { execSync } = require('child_process');
const fs = require('fs');
const os = require('os');

const addAliasIfNotExists = (aliasCommand, configFile) => {
let fileContent = '';
try {
fileContent = fs.readFileSync(configFile, 'utf8');
} catch (err) {
console.error(`Could not read ${configFile}: ${err.message}`);
return;
}

if (!fileContent.includes(aliasCommand)) {
fs.appendFileSync(configFile, `\n${aliasCommand}\n`);
console.log(`Added alias: ${aliasCommand}`);
} else {
console.log(`Alias already exists: ${aliasCommand}`);
}
};

const output = execSync('lerna list --json', { encoding: 'utf-8' });
const packages = JSON.parse(output);
const scopeNames = packages.map(pkg => pkg.name);
const shellArg = process.argv[2];

if (!shellArg) {
console.error('Error: Please specify either "bash" or "zsh".');
process.exit(1);
}

let configFile;
if (shellArg === 'bash') {
configFile = `${os.homedir()}/.bashrc`;
} else if (shellArg === 'zsh') {
configFile = `${os.homedir()}/.zshrc`;
} else {
console.error('Error: Invalid argument. Please specify "bash" or "zsh".');
process.exit(1);
}

scopeNames.forEach(scope => {
const cleanedScope = scope.replace('@instana/', '');

const watchAlias = `alias run${cleanedScope}='bin/run-tests.sh --scope=${scope} --watch'`;
const nwAlias = `alias run${cleanedScope}-nw='bin/run-tests.sh --scope=${scope}'`;

addAliasIfNotExists(watchAlias, configFile);
addAliasIfNotExists(nwAlias, configFile);
});

console.log('Aliases added. Please run the following command to apply the changes:');
console.log(` source ${configFile}`);
console.log('Alternatively, restart your terminal.');

console.log('Done');
11 changes: 0 additions & 11 deletions bin/run-autoprofile-tests.sh

This file was deleted.

10 changes: 0 additions & 10 deletions bin/run-aws-lambda-auto-wrap.sh

This file was deleted.

9 changes: 0 additions & 9 deletions bin/run-azure-container-services-tests.sh

This file was deleted.

11 changes: 0 additions & 11 deletions bin/run-collector-tests.sh

This file was deleted.

11 changes: 0 additions & 11 deletions bin/run-core-tests.sh

This file was deleted.

11 changes: 0 additions & 11 deletions bin/run-fargate-tests.sh

This file was deleted.

11 changes: 0 additions & 11 deletions bin/run-google-cloud-run-tests.sh

This file was deleted.

11 changes: 0 additions & 11 deletions bin/run-lambda-tests.sh

This file was deleted.

11 changes: 0 additions & 11 deletions bin/run-metrics-util-tests.sh

This file was deleted.

11 changes: 0 additions & 11 deletions bin/run-otel-exporter-tests.sh

This file was deleted.

9 changes: 0 additions & 9 deletions bin/run-otel-sampler-tests.sh

This file was deleted.

10 changes: 0 additions & 10 deletions bin/run-serverless-collector-tests.sh

This file was deleted.

11 changes: 0 additions & 11 deletions bin/run-serverless-tests.sh

This file was deleted.

11 changes: 0 additions & 11 deletions bin/run-shared-metrics-tests.sh

This file was deleted.

29 changes: 27 additions & 2 deletions bin/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,35 @@

#######################################
# (c) Copyright IBM Corp. 2021
# (c) Copyright Instana Inc. and contributors 2018
#######################################

set -eo pipefail

npx lerna run test:debug --stream
WATCH=false
SCOPE=""

for arg in "$@"; do
case $arg in
--watch)
WATCH=true
shift
;;
--scope=*)
SCOPE="${arg#*=}"
shift
;;
*)
shift
;;
esac
done

if [ "$WATCH" = true ]; then
export npm_config_watch="--watch"
else
export npm_config_watch=""
fi

echo "Running tests with $SCOPE and $npm_config_watch:"
npx lerna exec --scope="$SCOPE" "npm run test:debug"

2 changes: 1 addition & 1 deletion packages/autoprofile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"audit": "npm audit --omit=dev",
"node_modules:exists": "mkdir -p node_modules",
"install": "node-gyp-build",
"test": "mocha --config=test/.mocharc.js --require test/hooks.js --sort $(find test -iname '*test.js' -not -path '*node_modules*')",
"test": "mocha $npm_config_watch --config=test/.mocharc.js --require test/hooks.js --sort $(find test -iname '*test.js' -not -path '*node_modules*')",
"test:debug": "WITH_STDOUT=true npm run test",
"test:ci": "echo \"******* Files to be tested:\n $CI_AUTOPROFILE_TEST_FILES\" && if [ -z \"${CI_AUTOPROFILE_TEST_FILES}\" ]; then echo \"No test files have been assigned to this CircleCI executor.\"; else mocha --config=test/.mocharc.js --reporter mocha-multi-reporters --reporter-options configFile=reporter-config.json --require test/hooks.js --sort ${CI_AUTOPROFILE_TEST_FILES}; fi",
"lint": "eslint lib test",
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-fargate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"scripts": {
"audit": "npm audit --omit=dev",
"node_modules:exists": "mkdir -p node_modules",
"test": "mocha --config=test/.mocharc.js --reporter mocha-multi-reporters --reporter-options configFile=reporter-config.json --sort $(find test -iname '*test.js')",
"test": "mocha $npm_config_watch --config=test/.mocharc.js --reporter mocha-multi-reporters --reporter-options configFile=reporter-config.json --sort $(find test -iname '*test.js')",
"test:debug": "WITH_STDOUT=true npm run test",
"test:ci": "mocha --config=test/.mocharc.js --reporter mocha-multi-reporters --reporter-options configFile=reporter-config.json 'test/**/*test.js'",
"lint": "eslint src test images",
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-lambda-auto-wrap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"scripts": {
"audit": "npm audit --omit=dev",
"node_modules:exists": "mkdir -p node_modules",
"test": "mocha --config=test/.mocharc.js --sort $(find test -iname '*test.js')",
"test": "mocha $npm_config_watch --config=test/.mocharc.js --sort $(find test -iname '*test.js')",
"test:debug": "WITH_STDOUT=true npm run test",
"test:ci": "mocha --config=test/.mocharc.js --reporter mocha-multi-reporters --reporter-options configFile=reporter-config.json 'test/**/*test.js'",
"lint": "eslint src",
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-lambda/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"scripts": {
"audit": "npm audit --omit=dev",
"node_modules:exists": "mkdir -p node_modules",
"test": "mocha --config=test/.mocharc.js --sort $(find test -iname '*test.js')",
"test": "mocha $npm_config_watch --config=test/.mocharc.js --sort $(find test -iname '*test.js')",
"test:debug": "WITH_STDOUT=true INSTANA_DEBUG=true npm run test",
"test:ci": "mocha --config=test/.mocharc.js --reporter mocha-multi-reporters --reporter-options configFile=reporter-config.json 'test/**/*test.js'",
"lint": "eslint src test lambdas",
Expand Down
2 changes: 1 addition & 1 deletion packages/azure-container-services/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"scripts": {
"audit": "npm audit --omit=dev",
"node_modules:exists": "mkdir -p node_modules",
"test": "mocha --sort $(find test -iname '*test.js')",
"test": "mocha $npm_config_watch --config=test/.mocharc.js --sort $(find test -iname '*test.js')",
"test:debug": "WITH_STDOUT=true npm run test",
"test:ci": "mocha --config=test/.mocharc.js --reporter mocha-multi-reporters --reporter-options configFile=reporter-config.json 'test/**/*test.js'",
"lint": "eslint src test images",
Expand Down
2 changes: 1 addition & 1 deletion packages/collector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"scripts": {
"audit": "npm audit --omit=dev",
"test": "USE_OPENTRACING_DEBUG_IMPL=true mocha --config=test/.mocharc.js --require test/hooks.js --sort $(find test -iname '*test.js')",
"test": "USE_OPENTRACING_DEBUG_IMPL=true mocha $npm_config_watch --config=test/.mocharc.js --require test/hooks.js --sort $(find test -iname '*test.js')",
"test:debug": "WITH_STDOUT=true npm run test",
"test:ci:general": "mocha --config=test/.mocharc.js --reporter mocha-multi-reporters --reporter-options configFile=reporter-config.json --require test/hooks.js 'test/**/*test.js' --exclude 'test/tracing/**/*test.js'",
"test:ci:tracing:frameworks": "mocha --config=test/.mocharc.js --reporter mocha-multi-reporters --reporter-options configFile=reporter-config.json --require test/hooks.js 'test/tracing/frameworks/**/*test.js'",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"scripts": {
"audit": "npm audit --omit=dev",
"test": "USE_OPENTRACING_DEBUG_IMPL=true mocha --config=test/.mocharc.js --reporter mocha-multi-reporters --reporter-options configFile=reporter-config.json --sort $(find test -iname '*test.js')",
"test": "USE_OPENTRACING_DEBUG_IMPL=true mocha $npm_config_watch --config=test/.mocharc.js --reporter mocha-multi-reporters --reporter-options configFile=reporter-config.json --sort $(find test -iname '*test.js')",
"test:debug": "WITH_STDOUT=true npm run test",
"test:ci": "mocha --config=test/.mocharc.js --reporter mocha-multi-reporters --reporter-options configFile=reporter-config.json 'test/**/*test.js'",
"lint": "eslint src test",
Expand Down
2 changes: 1 addition & 1 deletion packages/google-cloud-run/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"scripts": {
"audit": "npm audit --omit=dev",
"node_modules:exists": "mkdir -p node_modules",
"test": "mocha --config=test/.mocharc.js --sort $(find test -iname '*test.js')",
"test": "mocha $npm_config_watch --config=test/.mocharc.js --sort $(find test -iname '*test.js')",
"test:debug": "WITH_STDOUT=true npm run test",
"test:ci": "mocha --config=test/.mocharc.js --reporter mocha-multi-reporters --reporter-options configFile=reporter-config.json 'test/**/*test.js'",
"lint": "eslint src test images",
Expand Down
2 changes: 1 addition & 1 deletion packages/metrics-util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"scripts": {
"audit": "npm audit --omit=dev",
"node_modules:exists": "mkdir -p node_modules",
"test": "mocha --sort $(find test -iname '*test.js')",
"test": "mocha $npm_config_watch --config=test/.mocharc.js --sort $(find test -iname '*test.js')",
"test:debug": "WITH_STDOUT=true npm run test",
"test:ci": "mocha --config=test/.mocharc.js --reporter mocha-multi-reporters --reporter-options configFile=reporter-config.json 'test/**/*test.js'",
"lint": "eslint src test",
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-exporter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"audit": "npm audit --omit=dev",
"start": "node test/app.js",
"debug": "node --inspect-brk test/app.js",
"test": "WITH_STDOUT=true mocha --config=test/.mocharc.js --sort $(find test -iname '*test.js')",
"test": "WITH_STDOUT=true mocha $npm_config_watch --config=test/.mocharc.js --sort $(find test -iname '*test.js')",
"test:debug": "WITH_STDOUT=true npm run test",
"test:ci": "mocha --config=test/.mocharc.js --reporter mocha-multi-reporters --reporter-options configFile=reporter-config.json 'test/**/*test.js'",
"lint": "eslint src test",
Expand Down
Loading

0 comments on commit e80ee65

Please sign in to comment.