Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
test(integration): added tracing test
Browse files Browse the repository at this point in the history
  • Loading branch information
10xLaCroixDrinker committed Feb 1, 2024
1 parent 8aca459 commit a298c2c
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 227 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ test-results

# sample prod setup
prod-sample/nginx/origin-statics
prod-sample/*/tmp
*.pem
*.crt
*.key
Expand Down
37 changes: 30 additions & 7 deletions __tests__/integration/helpers/testRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* permissions and limitations under the License.
*/

const path = require('path');
const childProcess = require('child_process');
const path = require('node:path');
const fs = require('node:fs/promises');
const childProcess = require('node:child_process');
const { remote } = require('webdriverio');
const fs = require('fs-extra');
const yaml = require('js-yaml');

const { waitUntilServerIsUp } = require('./wait');
Expand All @@ -28,6 +28,8 @@ const deepMergeObjects = require('../../../src/server/utils/deepMergeObjects');
const prodSampleDir = path.resolve('./prod-sample/');
const pathToDockerComposeTestFile = path.resolve(prodSampleDir, 'docker-compose.test.yml');

let logWatcherDuplex;

const setUpTestRunner = async ({
oneAppLocalPortToUse,
oneAppMetricsLocalPortToUse,
Expand All @@ -36,7 +38,7 @@ const setUpTestRunner = async ({
const pathToBaseDockerComposeFile = path.resolve(prodSampleDir, 'docker-compose.yml');
const seleniumServerPort = getRandomPortNumber();
// create docker compose file from base with changes needed for tests
const baseDockerComposeFileContents = yaml.load(fs.readFileSync(pathToBaseDockerComposeFile, 'utf8'));
const baseDockerComposeFileContents = yaml.load(await fs.readFile(pathToBaseDockerComposeFile, 'utf8'));
const testDockerComposeFileContents = deepMergeObjects(
baseDockerComposeFileContents,
{
Expand Down Expand Up @@ -65,20 +67,40 @@ const setUpTestRunner = async ({
delete testDockerComposeFileContents.services['selenium-chrome'].entrypoint;
}

fs.writeFileSync(pathToDockerComposeTestFile, yaml.dump(testDockerComposeFileContents));
await fs.writeFile(pathToDockerComposeTestFile, yaml.dump(testDockerComposeFileContents));

const dockerComposeUpCommand = `docker compose -f ${pathToDockerComposeTestFile} up --abort-on-container-exit --force-recreate`;
const dockerComposeUpProcess = childProcess.spawn(`${dockerComposeUpCommand}`, { shell: true });
const serverStartupTimeout = 90000;

const logWatcherDuplex = createLogWatcherDuplex();
logWatcherDuplex = createLogWatcherDuplex();
// logWatcherDuplex enables the testing of logs without preventing logging to stdout and stderr
dockerComposeUpProcess.stdout.pipe(logWatcherDuplex);
dockerComposeUpProcess.stderr.pipe(logWatcherDuplex);

// uncomment this line in order to view full logs for debugging
// logWatcherDuplex.pipe(process.stdout);

const traceFilePath = path.resolve(prodSampleDir, 'otel-collector', 'tmp', 'traces.jsonl');
await fs.mkdir(path.dirname(traceFilePath), { recursive: true });
await fs.writeFile(traceFilePath, '');
await fs.chmod(traceFilePath, 0o666);
const traceTail = childProcess.spawn('tail', ['-f', traceFilePath]);

// tail has a line character limit before it splits the chunks, but we need
// each trace in a single line for parsing
let currentTraceChunk = '';
traceTail.stdout.on('data', (chunk) => {
currentTraceChunk += chunk;
if (currentTraceChunk.endsWith('\n')) {
logWatcherDuplex.write(currentTraceChunk);
// uncomment this line in order to view full traces for debugging
// process.stdout.write(currentTraceChunk);
currentTraceChunk = '';
}
});
logWatcherDuplex.on('close', () => traceTail.kill());

try {
await Promise.all([
oneAppLocalPortToUse ? waitUntilServerIsUp(`https://localhost:${oneAppLocalPortToUse}/success`, serverStartupTimeout) : Promise.resolve(),
Expand Down Expand Up @@ -118,7 +140,8 @@ const setUpTestRunner = async ({
};

const tearDownTestRunner = async ({ browser } = {}) => {
fs.removeSync(pathToDockerComposeTestFile);
if (logWatcherDuplex) logWatcherDuplex.destroy();
await fs.rm(pathToDockerComposeTestFile);
if (browser) { await browser.deleteSession(); }

const dockerComposeDownProcess = childProcess.spawnSync('docker-compose down', { shell: true, cwd: prodSampleDir });
Expand Down
Loading

0 comments on commit a298c2c

Please sign in to comment.