Skip to content

Commit

Permalink
test: improved local test running (#1441)
Browse files Browse the repository at this point in the history
- Added node script to add terminal aliases (helps to easily run test groups)
- Integrated mocha watch
- Removed redundant sh helper scripts
- See mochajs/mocha#5149

---------

Co-authored-by: Arya <[email protected]>
  • Loading branch information
kirrg001 and aryamohanan authored Nov 12, 2024
1 parent d4999de commit f60f3e0
Show file tree
Hide file tree
Showing 33 changed files with 145 additions and 170 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 # Run the 'collector' scope with watch mode
runcollector-nw # Run the 'collector' scope without watch mode
```

### 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
3 changes: 3 additions & 0 deletions packages/collector/test/apps/agentStubControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ class AgentStubControls {
}

async reset() {
// eslint-disable-next-line no-console
console.log(`[AgentStubControls] reset ${this.agentPort}`);

return retry(async () => {
await fetch(`http://127.0.0.1:${this.agentPort}/`, {
method: 'DELETE',
Expand Down
Loading

0 comments on commit f60f3e0

Please sign in to comment.