Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(shell-api): Fix invalid regular expression error in db.currentOp() MONGOSH-1703 #2187

Merged
merged 23 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions packages/e2e-tests/test/e2e-current-op.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { expect } from 'chai';
import {
skipIfApiStrict,
startSharedTestServer,
} from '../../../testing/integration-testing-hooks';
import { TestShell } from './test-shell';

describe('e2e currentOp', function () {
skipIfApiStrict();
gagik marked this conversation as resolved.
Show resolved Hide resolved
afterEach(TestShell.cleanup);

const testServer = startSharedTestServer();

context('with 2 shells', function () {
let helperShell: TestShell;
let currentOpShell: TestShell;

const OPERATION_TIME = 1_000;
gagik marked this conversation as resolved.
Show resolved Hide resolved
const CURRENT_OP_WAIT_TIME = 100;
this.timeout(OPERATION_TIME * 2);

beforeEach(async function () {
helperShell = TestShell.start({
args: [await testServer.connectionString()],
});
currentOpShell = TestShell.start({
args: [await testServer.connectionString()],
});
await helperShell.waitForPrompt();

// Insert a dummy object so find commands will actually run with the delay.
await helperShell.executeLine('db.coll.insertOne({})');
});

it('should return the correct operation', async function () {
const regexOperation = helperShell.executeLine(
`db.coll.find({$where: function() { sleep(${OPERATION_TIME}) }}).projection({test: 1})`
);
helperShell.assertNoErrors();
await currentOpShell.executeLine(`sleep(${CURRENT_OP_WAIT_TIME})`);
gagik marked this conversation as resolved.
Show resolved Hide resolved
let currentOpCall = await currentOpShell.executeLine(`db.currentOp()`);

currentOpShell.assertNoErrors();
expect(currentOpCall).to.include("find: 'coll'");
expect(currentOpCall).to.include(
`filter: { '$where': Code('function() { sleep(${OPERATION_TIME}) }') }`
);
expect(currentOpCall).to.include('projection: { test: 1 }');

console.log(currentOpCall);

await regexOperation;

currentOpCall = await currentOpShell.executeLine(`db.currentOp()`);

currentOpShell.assertNoErrors();
expect(currentOpCall).not.to.include("find: 'coll'");
expect(currentOpCall).not.to.include(
`filter: { '$where': Code('function() { sleep(${OPERATION_TIME}) }') }`
);
expect(currentOpCall).not.to.include('projection: { test: 1 }');
gagik marked this conversation as resolved.
Show resolved Hide resolved
});

it('should work when the operation contains regex', async function () {
const regExpString = '^(?i)\\\\Qchho0842\\E';
// The values from currentOp removes redundant escapes such as \E
gagik marked this conversation as resolved.
Show resolved Hide resolved
const simplifiedRegExpString = '^(?i)\\\\Qchho0842E';

void helperShell.executeLine(
`db.coll.find({$where: function() { sleep(${OPERATION_TIME}) }}).projection({re: BSONRegExp('${regExpString}')})`
);
helperShell.assertNoErrors();

await currentOpShell.executeLine(`sleep(${CURRENT_OP_WAIT_TIME})`);

const currentOpCall = await currentOpShell.executeLine(`db.currentOp()`);
currentOpShell.assertNoErrors();

expect(currentOpCall).to.include(
`projection: { re: BSONRegExp('${simplifiedRegExpString}', '') }`
);
});
});
});
15 changes: 8 additions & 7 deletions packages/shell-api/src/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1796,10 +1796,11 @@ describe('Database', function () {
},
});

const READ_PREFERENCE = {
const AGGREGATE_OPTIONS = {
gagik marked this conversation as resolved.
Show resolved Hide resolved
$readPreference: {
mode: 'primaryPreferred',
},
bsonRegExp: true,
};

beforeEach(function () {
Expand All @@ -1824,7 +1825,7 @@ describe('Database', function () {
})
);
expect(serviceProvider.aggregateDb.firstCall.args[2]).to.deep.equal(
READ_PREFERENCE
AGGREGATE_OPTIONS
);
}
});
Expand All @@ -1846,7 +1847,7 @@ describe('Database', function () {
})
);
expect(serviceProvider.aggregateDb.firstCall.args[2]).to.deep.equal(
READ_PREFERENCE
AGGREGATE_OPTIONS
);
});
});
Expand All @@ -1868,7 +1869,7 @@ describe('Database', function () {
);
expect(matchStage).to.deep.equals({ $match: {} });
expect(serviceProvider.aggregateDb.firstCall.args[2]).to.deep.equal(
READ_PREFERENCE
AGGREGATE_OPTIONS
);
});
});
Expand All @@ -1890,7 +1891,7 @@ describe('Database', function () {
);
expect(matchStage).to.deep.equals({ $match: {} });
expect(serviceProvider.aggregateDb.firstCall.args[2]).to.deep.equal(
READ_PREFERENCE
AGGREGATE_OPTIONS
);
});
});
Expand All @@ -1914,7 +1915,7 @@ describe('Database', function () {
);
expect(matchStage).to.deep.equals({ $match: {} });
expect(serviceProvider.aggregateDb.firstCall.args[2]).to.deep.equal(
READ_PREFERENCE
AGGREGATE_OPTIONS
);
});
}
Expand Down Expand Up @@ -1945,7 +1946,7 @@ describe('Database', function () {
$match: { waitingForLock: true },
});
expect(serviceProvider.aggregateDb.firstCall.args[2]).to.deep.equal(
READ_PREFERENCE
AGGREGATE_OPTIONS
);
});

Expand Down
5 changes: 4 additions & 1 deletion packages/shell-api/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,10 @@ export default class Database extends ShellApiWithMongoClass {
}

const adminDb = this.getSiblingDB('admin');
const aggregateOptions = { $readPreference: { mode: 'primaryPreferred' } };
const aggregateOptions = {
$readPreference: { mode: 'primaryPreferred' },
bsonRegExp: true,
gagik marked this conversation as resolved.
Show resolved Hide resolved
};

try {
const cursor = await adminDb.aggregate(pipeline, aggregateOptions);
Expand Down
Loading