Skip to content

Commit

Permalink
test: more Unified runner operations (#2730)
Browse files Browse the repository at this point in the history
Adds a number of operations, makes use of chai expect instead of boolean
for assertions to track stack traces, adds session and bucket entity
types. Passes utilClient through to operation functions which are
inlined into the operations map to get type checking to work.
Modifies url helper function to make multipleMongoses detection work.
  • Loading branch information
nbbeeken authored and ljhaywar committed Nov 9, 2021
1 parent 9f0e81e commit b40749f
Show file tree
Hide file tree
Showing 10 changed files with 791 additions and 699 deletions.
16 changes: 9 additions & 7 deletions test/functional/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ describe('Connection - functional', function () {
metadata: { requires: { topology: 'single' } },

test: function (done) {
var configuration = this.configuration;
var user = 'testConnectGoodAuth',
password = 'password';
const configuration = this.configuration;
const username = 'testConnectGoodAuth';
const password = 'password';

const setupClient = configuration.newClient();

Expand All @@ -155,14 +155,14 @@ describe('Connection - functional', function () {
expect(err).to.not.exist;
var db = client.db(configuration.db);

db.addUser(user, password, function (err) {
db.addUser(username, password, function (err) {
expect(err).to.not.exist;
client.close(restOfTest);
});
});

function restOfTest() {
const testClient = configuration.newClient(configuration.url(user, password));
const testClient = configuration.newClient(configuration.url({ username, password }));
testClient.connect(
connectionTester(configuration, 'testConnectGoodAuth', function (client) {
client.close(done);
Expand All @@ -176,7 +176,7 @@ describe('Connection - functional', function () {
metadata: { requires: { topology: 'single' } },

test: function (done) {
var configuration = this.configuration;
const configuration = this.configuration;
const username = 'testConnectGoodAuthAsOption';
const password = 'password';

Expand Down Expand Up @@ -211,7 +211,9 @@ describe('Connection - functional', function () {

test: function (done) {
var configuration = this.configuration;
const client = configuration.newClient(configuration.url('slithy', 'toves'));
const client = configuration.newClient(
configuration.url({ username: 'slithy', password: 'toves' })
);
client.connect(function (err, client) {
expect(err).to.exist;
expect(client).to.not.exist;
Expand Down
4 changes: 2 additions & 2 deletions test/functional/spec-runner/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
function resolveConnectionString(configuration, spec, context) {
const isShardedEnvironment = configuration.topologyType === 'Sharded';
const useMultipleMongoses = spec && !!spec.useMultipleMongoses;
const user = context && context.user;
const username = context && context.user;
const password = context && context.password;
const authSource = context && context.authSource;
const connectionString =
isShardedEnvironment && !useMultipleMongoses
? `mongodb://${configuration.host}:${configuration.port}/${
configuration.db
}?directConnection=false${authSource ? '&authSource=${authSource}' : ''}`
: configuration.url(user, password, { authSource });
: configuration.url({ username, password, authSource });
return connectionString;
}

Expand Down
75 changes: 66 additions & 9 deletions test/functional/unified-spec-runner/entities.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { MongoClient, Db, Collection, GridFSBucket, Document } from '../../../src/index';
import { ReadConcern } from '../../../src/read_concern';
import { WriteConcern } from '../../../src/write_concern';
import { ReadPreference } from '../../../src/read_preference';
import { ClientSession } from '../../../src/sessions';
import { ChangeStream } from '../../../src/change_stream';
import type { ClientEntity, EntityDescription } from './schema';
Expand All @@ -8,13 +11,17 @@ import type {
CommandSucceededEvent
} from '../../../src/cmap/events';
import { patchCollectionOptions, patchDbOptions } from './unified-utils';
import { TestConfiguration } from './unified.test';
import { expect } from 'chai';
import { TestConfiguration } from './runner';

interface UnifiedChangeStream extends ChangeStream {
eventCollector: InstanceType<typeof import('../../tools/utils')['EventCollector']>;
}

interface UnifiedClientSession extends ClientSession {
client: UnifiedMongoClient;
}

export type CommandEvent = CommandStartedEvent | CommandSucceededEvent | CommandFailedEvent;

export class UnifiedMongoClient extends MongoClient {
Expand Down Expand Up @@ -85,7 +92,7 @@ export type Entity =
| UnifiedMongoClient
| Db
| Collection
| ClientSession
| UnifiedClientSession
| UnifiedChangeStream
| GridFSBucket
| Document; // Results from operations
Expand All @@ -112,7 +119,7 @@ export class EntitiesMap<E = Entity> extends Map<string, E> {
mapOf(type: 'client'): EntitiesMap<UnifiedMongoClient>;
mapOf(type: 'db'): EntitiesMap<Db>;
mapOf(type: 'collection'): EntitiesMap<Collection>;
mapOf(type: 'session'): EntitiesMap<ClientSession>;
mapOf(type: 'session'): EntitiesMap<UnifiedClientSession>;
mapOf(type: 'bucket'): EntitiesMap<GridFSBucket>;
mapOf(type: 'stream'): EntitiesMap<UnifiedChangeStream>;
mapOf(type: EntityTypeId): EntitiesMap<Entity> {
Expand All @@ -126,13 +133,13 @@ export class EntitiesMap<E = Entity> extends Map<string, E> {
getEntity(type: 'client', key: string, assertExists?: boolean): UnifiedMongoClient;
getEntity(type: 'db', key: string, assertExists?: boolean): Db;
getEntity(type: 'collection', key: string, assertExists?: boolean): Collection;
getEntity(type: 'session', key: string, assertExists?: boolean): ClientSession;
getEntity(type: 'session', key: string, assertExists?: boolean): UnifiedClientSession;
getEntity(type: 'bucket', key: string, assertExists?: boolean): GridFSBucket;
getEntity(type: 'stream', key: string, assertExists?: boolean): UnifiedChangeStream;
getEntity(type: EntityTypeId, key: string, assertExists = true): Entity {
const entity = this.get(key);
if (!entity) {
if (assertExists) throw new Error(`Entity ${key} does not exist`);
if (assertExists) throw new Error(`Entity '${key}' does not exist`);
return;
}
const ctor = ENTITY_CTORS.get(type);
Expand Down Expand Up @@ -163,7 +170,8 @@ export class EntitiesMap<E = Entity> extends Map<string, E> {
const map = new EntitiesMap();
for (const entity of entities ?? []) {
if ('client' in entity) {
const client = new UnifiedMongoClient(config.url(), entity.client);
const uri = config.url({ useMultipleMongoses: entity.client.useMultipleMongoses });
const client = new UnifiedMongoClient(uri, entity.client);
await client.connect();
map.set(entity.client.id, client);
} else if ('database' in entity) {
Expand All @@ -181,11 +189,60 @@ export class EntitiesMap<E = Entity> extends Map<string, E> {
);
map.set(entity.collection.id, collection);
} else if ('session' in entity) {
map.set(entity.session.id, null);
const client = map.getEntity('client', entity.session.client);

const options = Object.create(null);

if (entity.session.sessionOptions?.causalConsistency) {
options.causalConsistency = entity.session.sessionOptions?.causalConsistency;
}

if (entity.session.sessionOptions?.defaultTransactionOptions) {
options.defaultTransactionOptions = Object.create(null);
const defaultOptions = entity.session.sessionOptions.defaultTransactionOptions;
if (defaultOptions.readConcern) {
options.defaultTransactionOptions.readConcern = ReadConcern.fromOptions(
defaultOptions.readConcern
);
}
if (defaultOptions.writeConcern) {
options.defaultTransactionOptions.writeConcern = WriteConcern.fromOptions(
defaultOptions
);
}
if (defaultOptions.readPreference) {
options.defaultTransactionOptions.readPreference = ReadPreference.fromOptions(
defaultOptions.readPreference
);
}
if (typeof defaultOptions.maxCommitTimeMS === 'number') {
options.defaultTransactionOptions.maxCommitTimeMS = defaultOptions.maxCommitTimeMS;
}
}

const session = client.startSession(options) as UnifiedClientSession;
// targetedFailPoint operations need to access the client the session came from
session.client = client;

map.set(entity.session.id, session);
} else if ('bucket' in entity) {
map.set(entity.bucket.id, null);
const db = map.getEntity('db', entity.bucket.database);

const options = Object.create(null);

if (entity.bucket.bucketOptions?.bucketName) {
options.bucketName = entity.bucket.bucketOptions?.bucketName;
}
if (entity.bucket.bucketOptions?.chunkSizeBytes) {
options.chunkSizeBytes = entity.bucket.bucketOptions?.chunkSizeBytes;
}
if (entity.bucket.bucketOptions?.readPreference) {
options.readPreference = entity.bucket.bucketOptions?.readPreference;
}

map.set(entity.bucket.id, new GridFSBucket(db, options));
} else if ('stream' in entity) {
map.set(entity.stream.id, null);
throw new Error(`Unsupported Entity ${JSON.stringify(entity)}`);
} else {
throw new Error(`Unsupported Entity ${JSON.stringify(entity)}`);
}
Expand Down
Loading

0 comments on commit b40749f

Please sign in to comment.