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

Clean up emulator check in Cosmos Test #2547

Merged
merged 10 commits into from
Jul 21, 2020
70 changes: 41 additions & 29 deletions libraries/botbuilder-azure/tests/cosmosDbPartitionedStorage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { MockMode, usingNock } = require('./mockHelper');
const nock = require('nock');
const fs = require('fs');
const https = require('https');
const fetch = require('node-fetch');

/**
* READ THIS BEFORE EDITING THESE TESTS
Expand All @@ -29,9 +30,10 @@ const emulatorPath = 'C:/Program Files/Azure Cosmos DB Emulator/CosmosDB.Emulato

// Endpoint and authKey for the CosmosDB Emulator running locally
let containerIdSuffix = 0;
const emulatorEndpoint = 'https://localhost:8081'
const getSettings = () => {
return {
cosmosDbEndpoint: 'https://localhost:8081',
cosmosDbEndpoint: emulatorEndpoint,
authKey: 'C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==',
databaseId: 'CosmosPartitionedStorageTestDb',
containerId: `CosmosPartitionedStorageTestContainer-${ containerIdSuffix++ }`,
Expand All @@ -41,11 +43,19 @@ const getSettings = () => {
};
};

const checkEmulator = () => {
if (!fs.existsSync(emulatorPath)) {
console.warn('This test requires CosmosDB Emulator! go to https://aka.ms/documentdb-emulator-docs to download and install.');
var canConnectToEmulator = undefined;
const checkEmulator = async () => {
// We don't want to check for this multiple times, due to waiting on fetch() timeouts when connection fails
if (canConnectToEmulator === undefined) {
try {
await fetch(emulatorEndpoint);
canConnectToEmulator = true;
} catch (err) {
canConnectToEmulator = false;
console.warn(`Unable to connect to Cosmos Emulator at ${ emulatorEndpoint }. Running tests against Nock recordings.`);
}
}
return true;
return canConnectToEmulator;
};

var storage = new CosmosDbPartitionedStorage(getSettings());
Expand All @@ -54,28 +64,43 @@ var storage = new CosmosDbPartitionedStorage(getSettings());
const cleanup = async () => {
nock.cleanAll();
nock.enableNetConnect();
let settings = getSettings();
let client = new CosmosClient({ endpoint: settings.cosmosDbEndpoint, key: settings.authKey, agent: new https.Agent({ rejectUnauthorized: false }) });
try {
await client.database(settings.databaseId).delete();
} catch (err) { }

await checkEmulator();

const settings = getSettings();

if (canConnectToEmulator)
{
let client = new CosmosClient({ endpoint: settings.cosmosDbEndpoint, key: settings.authKey, agent: new https.Agent({ rejectUnauthorized: false }) });
try {
await client.database(settings.databaseId).delete();
} catch (err) { }
}
};

// called before each test
const prep = async () => {
nock.cleanAll();
await checkEmulator();

let settings = getSettings();

if (mode !== MockMode.lockdown) {
nock.enableNetConnect();
} else {
nock.disableNetConnect();
}
let client = new CosmosClient({ endpoint: settings.cosmosDbEndpoint, key: settings.authKey, agent: new https.Agent({ rejectUnauthorized: false }) });
// This throws if the db is already created. We want to always create it if it doesn't exist,
// so leaving this here should help prevent failures if the tests change in the future
try {
await client.databases.create({ id: settings.databaseId });
} catch (err) { }

if (canConnectToEmulator) {
let client = new CosmosClient({ endpoint: settings.cosmosDbEndpoint, key: settings.authKey, agent: new https.Agent({ rejectUnauthorized: false }) });

// This throws if the db is already created. We want to always create it if it doesn't exist,
// so leaving this here should help prevent failures if the tests change in the future
try {
await client.databases.create({ id: settings.databaseId });
} catch (err) { }
}

storage = new CosmosDbPartitionedStorage(settings);
};

Expand Down Expand Up @@ -142,7 +167,6 @@ describe('CosmosDbPartitionedStorage - Base Storage Tests', function() {
afterEach('cleanup', cleanup);

it('return empty object when reading unknown key', async function() {
checkEmulator();
const { nockDone } = await usingNock(this.test, mode, options);

const testRan = await StorageBaseTests.returnEmptyObjectWhenReadingUnknownKey(storage);
Expand All @@ -153,7 +177,6 @@ describe('CosmosDbPartitionedStorage - Base Storage Tests', function() {
});

it('throws when reading null keys', async function() {
checkEmulator();
const { nockDone } = await usingNock(this.test, mode, options);

const testRan = await StorageBaseTests.handleNullKeysWhenReading(storage);
Expand All @@ -163,7 +186,6 @@ describe('CosmosDbPartitionedStorage - Base Storage Tests', function() {
});

it('throws when writing null keys', async function() {
checkEmulator();
const { nockDone } = await usingNock(this.test, mode, options);

const testRan = await StorageBaseTests.handleNullKeysWhenWriting(storage);
Expand All @@ -173,7 +195,6 @@ describe('CosmosDbPartitionedStorage - Base Storage Tests', function() {
});

it('does not throw when writing no items', async function() {
checkEmulator();
const { nockDone } = await usingNock(this.test, mode, options);

const testRan = await StorageBaseTests.doesNotThrowWhenWritingNoItems(storage);
Expand All @@ -183,7 +204,6 @@ describe('CosmosDbPartitionedStorage - Base Storage Tests', function() {
});

it('create an object', async function() {
checkEmulator();
const { nockDone } = await usingNock(this.test, mode, options);

const testRan = await StorageBaseTests.createObject(storage);
Expand All @@ -193,7 +213,6 @@ describe('CosmosDbPartitionedStorage - Base Storage Tests', function() {
});

it('handle crazy keys', async function() {
checkEmulator();
const { nockDone } = await usingNock(this.test, mode, options);

const testRan = await StorageBaseTests.handleCrazyKeys(storage);
Expand All @@ -203,7 +222,6 @@ describe('CosmosDbPartitionedStorage - Base Storage Tests', function() {
});

it('update an object', async function() {
checkEmulator();
const { nockDone } = await usingNock(this.test, mode, options);

const testRan = await StorageBaseTests.updateObject(storage);
Expand All @@ -213,7 +231,6 @@ describe('CosmosDbPartitionedStorage - Base Storage Tests', function() {
});

it('delete an object', async function() {
checkEmulator();
const { nockDone } = await usingNock(this.test, mode, options);

const testRan = await StorageBaseTests.deleteObject(storage);
Expand All @@ -223,7 +240,6 @@ describe('CosmosDbPartitionedStorage - Base Storage Tests', function() {
});

it('does not throw when deleting an unknown object', async function() {
checkEmulator();
const { nockDone } = await usingNock(this.test, mode, options);

const testRan = await StorageBaseTests.deleteUnknownObject(storage);
Expand All @@ -233,7 +249,6 @@ describe('CosmosDbPartitionedStorage - Base Storage Tests', function() {
});

it('performs batch operations', async function() {
checkEmulator();
const { nockDone } = await usingNock(this.test, mode, options);

const testRan = await StorageBaseTests.performBatchOperations(storage);
Expand All @@ -243,7 +258,6 @@ describe('CosmosDbPartitionedStorage - Base Storage Tests', function() {
});

it('proceeds through a waterfall dialog', async function() {
checkEmulator();
const { nockDone } = await usingNock(this.test, mode, options);

const testRan = await StorageBaseTests.proceedsThroughWaterfall(storage);
Expand All @@ -252,7 +266,6 @@ describe('CosmosDbPartitionedStorage - Base Storage Tests', function() {
return nockDone();
});
it('support using multiple databases', async function() {
checkEmulator();
const { nockDone } = await usingNock(this.test, mode, options);

const newDb = 'new-db';
Expand Down Expand Up @@ -282,7 +295,6 @@ describe('CosmosDbPartitionedStorage - Base Storage Tests', function() {
return nockDone();
});
it('support using multiple containers', async function() {
checkEmulator();
const { nockDone } = await usingNock(this.test, mode, options);

const newContainer = 'new-container';
Expand Down