Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
onurtemizkan authored Dec 5, 2024
1 parent bd89bcc commit 5cd3b7c
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,45 @@
*/

// Use postgres from an ES module:
// node --experimental-loader=@opentelemetry/instrumentation/hook.mjs pg-esm.mjs
// node --experimental-loader=@opentelemetry/instrumentation/hook.mjs use-pg.mjs

import { trace } from '@opentelemetry/api';
import { createTestNodeSdk } from '@opentelemetry/contrib-test-utils';
import assert from 'assert';

import { PgInstrumentation } from '../../build/src/index.js';

const CONFIG = {
user: process.env.POSTGRES_USER || 'postgres',

Check failure

Code scanning / CodeQL

Hard-coded credentials Critical test

The hard-coded value "postgres" is used as
user name
.
password: process.env.POSTGRES_PASSWORD || 'postgres',

Check failure

Code scanning / CodeQL

Hard-coded credentials Critical test

The hard-coded value "postgres" is used as
password
.
database: process.env.POSTGRES_DB || 'postgres',
host: process.env.POSTGRES_HOST || 'localhost',
port: process.env.POSTGRES_PORT
? parseInt(process.env.POSTGRES_PORT, 10)
: 54320,
};

const sdk = createTestNodeSdk({
serviceName: 'use-pg',
instrumentations: [
new PgInstrumentation()
]
})
instrumentations: [new PgInstrumentation()],
});
sdk.start();

import pg from 'pg';

const client = new pg.Client();
const client = new pg.Client(CONFIG);

client.connect();
await new Promise(resolve => setTimeout(resolve, 4000));
await client.connect();

client.query('SELECT NOW()', (err, res) => {
console.log(err, res);
client.end();
});
const tracer = trace.getTracer();

await tracer.startActiveSpan('test-span', async (span) => {
const res = await client.query('SELECT NOW()');

assert.ok(res);
span.end();
});

await client.end();
await sdk.shutdown();
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ const CONFIG = {
? parseInt(process.env.POSTGRES_PORT, 10)
: 54320,
maxClient: 1,
idleTimeoutMillis: 10000,
idleTimeoutMillis: 30000,

};

const DEFAULT_PGPOOL_ATTRIBUTES = {
Expand Down Expand Up @@ -125,7 +126,7 @@ describe('pg-pool', () => {
const testPostgresLocally = process.env.RUN_POSTGRES_TESTS_LOCAL; // For local: spins up local postgres db via docker
const shouldTest = testPostgres || testPostgresLocally; // Skips these tests if false (default)

before(function () {
before(function (done) {
const skip = () => {
// this.skip() workaround
// https://github.com/mochajs/mocha/issues/2683#issuecomment-375629901
Expand All @@ -150,6 +151,7 @@ describe('pg-pool', () => {

const pgPool = require('pg-pool');
pool = new pgPool(CONFIG);
setTimeout(done, 3000);
});

after(done => {
Expand Down
58 changes: 58 additions & 0 deletions plugins/node/opentelemetry-instrumentation-pg/test/pg.esm.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import * as assert from 'assert';
import * as testUtils from '@opentelemetry/contrib-test-utils';

describe('pg ESM usage', () => {
const testPostgres = process.env.RUN_POSTGRES_TESTS; // For CI: assumes local postgres db is already available
const testPostgresLocally = process.env.RUN_POSTGRES_TESTS_LOCAL; // For local: spins up local postgres db via docker
const shouldTest = testPostgres || testPostgresLocally; // Skips these tests if false (default)

before(async function () {
const skip = () => {
// this.skip() workaround
// https://github.com/mochajs/mocha/issues/2683#issuecomment-375629901
this.test!.parent!.pending = true;
this.skip();
};

if (!shouldTest) {
skip();
}

if (testPostgresLocally) {
testUtils.startDocker('postgres');
}
});

after(async () => {
if (testPostgresLocally) {
testUtils.cleanUpDocker('postgres');
}
});

it('should work with ESM usage', async () => {
await testUtils.runTestFixture({
cwd: __dirname,
argv: ['fixtures/use-pg.mjs'],
env: {
NODE_OPTIONS:
'--experimental-loader=@opentelemetry/instrumentation/hook.mjs',
NODE_NO_WARNINGS: '1',
},
checkResult: (err) => {
assert.ifError(err);
},
checkCollector: (collector: testUtils.TestCollector) => {
const spans = collector.sortedSpans;

assert.strictEqual(spans.length, 3);

assert.strictEqual(spans[0].name, 'pg.connect');
assert.strictEqual(spans[0].kind, 3);
assert.strictEqual(spans[1].name, 'test-span');
assert.strictEqual(spans[1].kind, 1);
assert.strictEqual(spans[2].name, 'pg.query:SELECT postgres');
assert.strictEqual(spans[2].kind, 3);
},
});
});
});
25 changes: 5 additions & 20 deletions plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,15 @@ describe('pg', () => {

postgres = require('pg');
client = new postgres.Client(CONFIG);

await new Promise<void>(resolve => setTimeout(resolve, 3000));

await client.connect();
});

after(async () => {
await new Promise<void>(resolve => setTimeout(resolve, 3000));

if (testPostgresLocally) {
testUtils.cleanUpDocker('postgres');
}
Expand Down Expand Up @@ -1086,24 +1091,4 @@ describe('pg', () => {
});
});
});

it('should work with ESM usage', async () => {
await testUtils.runTestFixture({
cwd: __dirname,
argv: ['fixtures/use-pg.mjs'],
env: {
NODE_OPTIONS:
'--experimental-loader=@opentelemetry/instrumentation/hook.mjs',
NODE_NO_WARNINGS: '1',
},
checkResult: (err, stdout, stderr) => {
assert.ifError(err);
},
checkCollector: (collector: testUtils.TestCollector) => {
const spans = collector.sortedSpans;

assert.strictEqual(spans.length, 2);
},
});
});
});

0 comments on commit 5cd3b7c

Please sign in to comment.