From e182649adc38051d36b5815aa2e9ea7a7c9a1cb1 Mon Sep 17 00:00:00 2001 From: Leila Lali Date: Tue, 8 Dec 2020 08:43:58 -0800 Subject: [PATCH] Fixed Schema compare integration tests by adding retry (#13649) --- .../integration-tests/src/test/dacpac.test.ts | 19 +++++---- .../src/test/schemaCompare.test.ts | 39 +++++++++---------- .../integration-tests/src/test/utils.ts | 30 +++++++++++++- .../src/test/schemaCompare.test.ts | 6 +-- .../src/test/schemaCompareDialog.test.ts | 2 +- .../schema-compare/src/test/utils.test.ts | 2 +- 6 files changed, 61 insertions(+), 37 deletions(-) diff --git a/extensions/integration-tests/src/test/dacpac.test.ts b/extensions/integration-tests/src/test/dacpac.test.ts index db9f9afda1..bc6e221754 100644 --- a/extensions/integration-tests/src/test/dacpac.test.ts +++ b/extensions/integration-tests/src/test/dacpac.test.ts @@ -17,19 +17,21 @@ import { promisify } from 'util'; const retryCount = 24; // 2 minutes const dacpac1: string = path.join(__dirname, '../../testData/Database1.dacpac'); -suite('Dacpac integration test suite', () => { + + +suite('Dacpac integration test suite @DacFx@', () => { suiteSetup(async function () { await utils.sleep(5000); // To ensure the providers are registered. console.log(`Start dacpac tests`); }); test('Deploy and extract dacpac @UNSTABLE@', async function () { + this.timeout(5 * 60 * 1000); const server = await getStandaloneServer(); - await utils.connectToServer(server); + const connectionId = await utils.connectToServer(server); + assert(connectionId, `Failed to connect to "${server.serverName}"`); - const nodes = await azdata.objectexplorer.getActiveConnectionNodes(); - const index = nodes.findIndex(node => node.nodePath.includes(server.serverName)); - const ownerUri = await azdata.connection.getUriForConnection(nodes[index].connectionId); + const ownerUri = await azdata.connection.getUriForConnection(connectionId); const now = new Date(); const databaseName = 'ADS_deployDacpac_' + now.getTime().toString(); @@ -70,12 +72,13 @@ suite('Dacpac integration test suite', () => { const bacpac1: string = path.join(__dirname, '..', '..', 'testData', 'Database1.bacpac'); test('Import and export bacpac @UNSTABLE@', async function () { + this.timeout(5 * 60 * 1000); const server = await getStandaloneServer(); await utils.connectToServer(server); - const nodes = await azdata.objectexplorer.getActiveConnectionNodes(); - const index = nodes.findIndex(node => node.nodePath.includes(server.serverName)); - const ownerUri = await azdata.connection.getUriForConnection(nodes[index].connectionId); + const connectionId = await utils.connectToServer(server); + assert(connectionId, `Failed to connect to "${server.serverName}"`); + const ownerUri = await azdata.connection.getUriForConnection(connectionId); const now = new Date(); const databaseName = 'ADS_importBacpac_' + now.getTime().toString(); diff --git a/extensions/integration-tests/src/test/schemaCompare.test.ts b/extensions/integration-tests/src/test/schemaCompare.test.ts index e3fc57e531..8cd758edbc 100644 --- a/extensions/integration-tests/src/test/schemaCompare.test.ts +++ b/extensions/integration-tests/src/test/schemaCompare.test.ts @@ -12,7 +12,7 @@ import * as os from 'os'; import * as fs from 'fs'; import * as path from 'path'; import * as assert from 'assert'; -import { getStandaloneServer } from './testConfig'; +import { getStandaloneServer, TestServerProfile } from './testConfig'; import { promisify } from 'util'; let schemaCompareService: mssql.ISchemaCompareService; @@ -25,7 +25,7 @@ const SERVER_CONNECTION_TIMEOUT: number = 3000; const retryCount = 24; // 2 minutes const folderPath = path.join(os.tmpdir(), 'SchemaCompareTest'); -suite('Schema compare integration test suite', () => { +suite('Schema compare integration test suite @DacFx@', () => { suiteSetup(async function () { let attempts: number = 20; while (attempts > 0) { @@ -40,6 +40,7 @@ suite('Schema compare integration test suite', () => { console.log(`Start schema compare tests`); }); test('Schema compare dacpac to dacpac comparison and scmp @UNSTABLE@', async function () { + this.timeout(5 * 60 * 1000); assert(schemaCompareService, 'Schema Compare Service Provider is not available'); const now = new Date(); const operationId = 'testOperationId_' + now.getTime().toString(); @@ -82,16 +83,9 @@ suite('Schema compare integration test suite', () => { assert(openScmpResult.targetEndpointInfo.packageFilePath === target.packageFilePath, `Expected: target packageFilePath to be ${target.packageFilePath}, Actual: ${openScmpResult.targetEndpointInfo.packageFilePath}`); }); test('Schema compare database to database comparison, script generation, and scmp @UNSTABLE@', async function () { + this.timeout(5 * 60 * 1000); let server = await getStandaloneServer(); - await utils.connectToServer(server, SERVER_CONNECTION_TIMEOUT); - - let nodes = await azdata.objectexplorer.getActiveConnectionNodes(); - assert(nodes.length > 0, `Expecting at least one active connection, actual: ${nodes.length}`); - - let index = nodes.findIndex(node => node.nodePath.includes(server.serverName)); - assert(index !== -1, `Failed to find server: "${server.serverName}" in OE tree`); - - const ownerUri = await azdata.connection.getUriForConnection(nodes[index].connectionId); + const ownerUri = await getConnectionUri(server); const now = new Date(); const operationId = 'testOperationId_' + now.getTime().toString(); @@ -161,16 +155,9 @@ suite('Schema compare integration test suite', () => { } }); test('Schema compare dacpac to database comparison, script generation, and scmp @UNSTABLE@', async function () { + this.timeout(5 * 60 * 1000); let server = await getStandaloneServer(); - await utils.connectToServer(server, SERVER_CONNECTION_TIMEOUT); - - let nodes = await azdata.objectexplorer.getActiveConnectionNodes(); - assert(nodes.length > 0, `Expecting at least one active connection, actual: ${nodes.length}`); - - let index = nodes.findIndex(node => node.nodePath.includes(server.serverName)); - assert(index !== -1, `Failed to find server: "${server.serverName}" in OE tree`); - - const ownerUri = await azdata.connection.getUriForConnection(nodes[index].connectionId); + const ownerUri = await getConnectionUri(server); const now = new Date(); const operationId = 'testOperationId_' + now.getTime().toString(); const targetDB: string = 'ads_schemaCompare_targetDB_' + now.getTime().toString(); @@ -228,6 +215,7 @@ suite('Schema compare integration test suite', () => { } }); test('Schema compare dacpac to dacpac comparison with include exclude @UNSTABLE@', async function () { + this.timeout(5 * 60 * 1000); assert(schemaCompareService, 'Schema Compare Service Provider is not available'); const operationId = 'testOperationId_' + new Date().getTime().toString(); @@ -284,6 +272,17 @@ suite('Schema compare integration test suite', () => { }); }); +async function getConnectionUri(server: TestServerProfile): Promise { + + // Connext to server + let connectionId = await utils.connectToServer(server, SERVER_CONNECTION_TIMEOUT); + assert(connectionId, `Failed to connect to "${server.serverName}"`); + + // Get connection uri + const ownerUri = await azdata.connection.getUriForConnection(connectionId); + return ownerUri; +} + function assertIncludeExcludeResult(result: mssql.SchemaCompareIncludeExcludeResult, expectedSuccess: boolean, expectedBlockingDependenciesLength: number, expectedAffectedDependenciesLength: number): void { assert(result.success === expectedSuccess, `Operation success should have been ${expectedSuccess}. Actual: ${result.success}`); if (result.blockingDependencies) { diff --git a/extensions/integration-tests/src/test/utils.ts b/extensions/integration-tests/src/test/utils.ts index 920af801c1..e23d30686c 100644 --- a/extensions/integration-tests/src/test/utils.ts +++ b/extensions/integration-tests/src/test/utils.ts @@ -35,9 +35,15 @@ export async function connectToServer(connectionInfo: TestConnectionInfo, timeou options: {} }; await ensureConnectionViewOpened(); - let result = await azdata.connection.connect(connectionProfile); - assert(result.connected, `Failed to connect to "${connectionProfile.serverName}", error code: ${result.errorCode}, error message: ${result.errorMessage}`); + // Try connecting 3 times + let result = await retryFunction( + async () => { + let connection = await azdata.connection.connect(connectionProfile); + assert(connection?.connected, `Failed to connect to "${connectionProfile.serverName}", error code: ${connection.errorCode}, error message: ${connection.errorMessage}`); + return connection; + + }, 3); //workaround //wait for OE to load await pollTimeout(async () => { @@ -180,6 +186,26 @@ export async function runQuery(query: string, ownerUri: string): Promise(fn: () => Promise, retryCount: number): Promise { + let attempts: number = 1; + while (attempts <= retryCount) { + try { + return await fn(); + } + catch (e) { + console.error(`utils.retryFunction: Attempt #${attempts} from ${retryCount} failed. Error: ${e}`); + if (attempts === retryCount) { + throw e; + } + } + + await sleep(10000); + attempts++; + } + throw new Error(`utils.retryFunction: Failed after ${attempts} attempts`); +} + + export async function assertThrowsAsync(fn: () => Promise, msg: string): Promise { let f = () => { // Empty diff --git a/extensions/schema-compare/src/test/schemaCompare.test.ts b/extensions/schema-compare/src/test/schemaCompare.test.ts index 62c150a74d..f894743588 100644 --- a/extensions/schema-compare/src/test/schemaCompare.test.ts +++ b/extensions/schema-compare/src/test/schemaCompare.test.ts @@ -27,11 +27,7 @@ before(function (): void { testContext = createContext(); }); -afterEach(function (): void { - sinon.restore(); -}); - -describe('SchemaCompareMainWindow.start', function (): void { +describe('SchemaCompareMainWindow.start @DacFx@', function (): void { before(() => { mockExtensionContext = TypeMoq.Mock.ofType(); mockExtensionContext.setup(x => x.extensionPath).returns(() => ''); diff --git a/extensions/schema-compare/src/test/schemaCompareDialog.test.ts b/extensions/schema-compare/src/test/schemaCompareDialog.test.ts index 91e662aeed..aaeeac0eaa 100644 --- a/extensions/schema-compare/src/test/schemaCompareDialog.test.ts +++ b/extensions/schema-compare/src/test/schemaCompareDialog.test.ts @@ -25,7 +25,7 @@ before(function (): void { testContext = createContext(); }); -describe('SchemaCompareDialog.openDialog', function (): void { +describe('SchemaCompareDialog.openDialog @DacFx@', function (): void { before(() => { mockExtensionContext = TypeMoq.Mock.ofType(); mockExtensionContext.setup(x => x.extensionPath).returns(() => ''); diff --git a/extensions/schema-compare/src/test/utils.test.ts b/extensions/schema-compare/src/test/utils.test.ts index 2f6cc4aa80..af3ca9bd89 100644 --- a/extensions/schema-compare/src/test/utils.test.ts +++ b/extensions/schema-compare/src/test/utils.test.ts @@ -20,7 +20,7 @@ import * as sinon from 'sinon'; let testContext: TestContext; -describe('utils: Tests to verify getEndpointName', function (): void { +describe('utils: Tests to verify getEndpointName @DacFx@', function (): void { afterEach(() => { sinon.restore(); });