Smoke tests (#9814)

* move

* add inital test; need basic sqllite connection

* before sqlite

* sqlite

* add smoke tests

* working tests

* fix app names

* fix quick open

* fix smoke tests

* add win32 smoke tests

* fix smoke test

* fix win32 smoke

* no continue

* continue on error

* add vscode smokes

* remove vscode tests

* continue on error

* allow sqlite to use relative paths

* add linux smoke tests

* fix build files

* use dispatch instead of select

* fix linux build again

* fix darwin

* get select working

* try and use screen shots

* screen shots

* remove smoke tests linux

* try vscodes sqlite

* fix compile

* fix webpack

* fix deps

* try this again

* try force a rebuild

* try npm rebuild

* add sqlite to be rebuilt

* distro

* try vscode sqlite again

* revert changes to driver and simplify edits

* fix compile

* fix imports

* move sqlite out

* remove unneeded change

* add extensions path

* fix web tests

* no continue on error
This commit is contained in:
Anthony Dresser
2020-04-03 00:01:32 -07:00
committed by GitHub
parent 589de854d5
commit 6e6649d006
33 changed files with 234 additions and 244 deletions

View File

@@ -0,0 +1,165 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import 'mocha';
import * as vscode from 'vscode';
import * as azdata from 'azdata';
import * as mssql from '../../../mssql';
import * as utils from '../utils';
import * as uuid from '../uuid';
import { isTestSetupCompleted } from '../testContext';
import assert = require('assert');
import { getStandaloneServer, TestServerProfile } from '../testConfig';
let cmsService: mssql.ICmsService;
let server: TestServerProfile;
let connectionId: string;
let ownerUri: string;
const SERVER_CONNECTION_TIMEOUT: number = 3000;
const TEST_CMS_NAME = `adsTestCms_${uuid.v4().asHex()}`;
const TEST_CMS_GROUP = `adsTestCmsGroup_${uuid.v4().asHex()}`;
const TEST_CMS_SERVER = `adsTestCmsServer_${uuid.v4().asHex()}`;
const TEST_CMS_REG_SERVER = `adsTestCmsRegisteredServer_${uuid.v4().asHex()}`;
if (isTestSetupCompleted()) {
suite('CMS integration test suite', () => {
setup(async function () {
// Set up CMS provider
if (!cmsService) {
cmsService = ((await vscode.extensions.getExtension(mssql.extension.name).activate() as mssql.IExtension)).cmsService;
assert(cmsService !== undefined);
}
// Set up connection
if (!server) {
server = await getStandaloneServer();
connectionId = await utils.connectToServer(server, SERVER_CONNECTION_TIMEOUT);
ownerUri = await azdata.connection.getConnectionString(connectionId, true);
console.log('Start CMS tests');
}
if (!ownerUri) {
ownerUri = await azdata.connection.getConnectionString(connectionId, true);
}
});
test('Create CMS Server', async function () {
// Should fail
await utils.assertThrowsAsync(
async () => await cmsService.createCmsServer(undefined, 'test_description', undefined, ownerUri),
'Cannot add a CMS server without a name or connection');
let connection = {
serverName: server.serverName,
userName: server.userName,
password: server.password,
authenticationType: server.authenticationTypeName,
database: server.database,
provider: server.provider,
version: server.version,
engineType: server.engineType,
options: {}
};
// Should create a CMS Server without an error
await cmsService.createCmsServer(TEST_CMS_NAME, 'test_description', connection, ownerUri);
});
test('Add and delete registered group to/from CMS server', async function () {
await utils.assertThrowsAsync(
async () => await cmsService.addServerGroup(ownerUri, '', undefined, 'test_description'),
'Cannot add a server group without a name');
// Should create a server group
let result = await cmsService.addServerGroup(ownerUri, '', TEST_CMS_GROUP, 'test_description');
assert(result === true, `Server group ${TEST_CMS_GROUP} was not added to CMS server successfully`);
let existingRegisteredServerGroupCount = (await cmsService.getRegisteredServers(ownerUri, '')).registeredServerGroups.length;
// Shouldn't be able to create a new server group with same name
await utils.assertThrowsAsync(
async () => await cmsService.addServerGroup(ownerUri, '', TEST_CMS_GROUP, 'test_description'),
'Cannot add a server group with existing name');
let cmsResources = await cmsService.getRegisteredServers(ownerUri, '');
assert(cmsResources.registeredServerGroups.length === existingRegisteredServerGroupCount,
`Unexpected number of Registered Server Groups after attempting to add group that already exists. Groups : [${cmsResources.registeredServerGroups.map(g => g.name).join(', ')}]`);
// Should remove the server group we added above
let deleteResult = await cmsService.removeServerGroup(ownerUri, '', TEST_CMS_GROUP);
assert(deleteResult === true, `Server group ${TEST_CMS_GROUP} was not removed successfully`);
cmsResources = await cmsService.getRegisteredServers(ownerUri, '');
assert(cmsResources.registeredServerGroups.find(g => g.name === TEST_CMS_GROUP) === undefined,
`The server group ${TEST_CMS_GROUP} was not removed successfully. Groups : [${cmsResources.registeredServerGroups.map(g => g.name).join(', ')}]`);
});
test('Add and delete registered server to/from CMS server', async function () {
await utils.assertThrowsAsync(
async () => cmsService.addRegisteredServer(ownerUri, '', undefined, 'test_description', undefined),
'Cannot add a registered without a name or connection');
let server = await getStandaloneServer('2019');
let connection = {
serverName: server.serverName,
userName: server.userName,
password: server.password,
authenticationType: server.authenticationTypeName,
database: server.database,
provider: server.provider,
version: server.version,
engineType: server.engineType,
options: {}
};
// Should create a registered server
let result = await cmsService.addRegisteredServer(ownerUri, '', TEST_CMS_SERVER, 'test_description', connection);
assert(result === true, `Registered server ${TEST_CMS_SERVER} was not added to CMS server successfully`);
// Shouldn't be able to create a new registered server with same name
await utils.assertThrowsAsync(
async () => await cmsService.addRegisteredServer(ownerUri, '', TEST_CMS_SERVER, 'test_description', connection),
'Cannot add a registered server with existing name');
// Should remove the registered server we added above
let deleteResult = await cmsService.removeRegisteredServer(ownerUri, '', TEST_CMS_SERVER);
assert(deleteResult === true, `Registered server ${TEST_CMS_SERVER} was not removed correctly`);
});
test('Add and delete registered server to/from server group', async function () {
// Should create a server group
let result = await cmsService.addServerGroup(ownerUri, '', TEST_CMS_GROUP, 'test_description');
assert(result === true, `Server group ${TEST_CMS_GROUP} was not created successfully`);
// Make sure server group is created
let cmsResources = await cmsService.getRegisteredServers(ownerUri, '');
assert(cmsResources.registeredServerGroups.find(g => g.name === TEST_CMS_GROUP),
`Registered Server Group ${TEST_CMS_GROUP} was not found after being added. Groups : [${cmsResources.registeredServerGroups.map(g => g.name).join(', ')}]`);
// Should create a registered server under the group
let server = await getStandaloneServer('2019');
let connection = {
serverName: server.serverName,
userName: server.userName,
password: server.password,
authenticationType: server.authenticationTypeName,
database: server.database,
provider: server.provider,
version: server.version,
engineType: server.engineType,
options: {}
};
let relativePath = cmsResources.registeredServerGroups[0].relativePath;
result = await cmsService.addRegisteredServer(ownerUri, relativePath, TEST_CMS_REG_SERVER, 'test_description', connection);
assert(result === true, `Registered server ${TEST_CMS_REG_SERVER} was not added to server group successfully`);
// Should remove the server group we added above
let deleteResult = await cmsService.removeServerGroup(ownerUri, '', TEST_CMS_GROUP);
assert(deleteResult === true, `Server group ${TEST_CMS_GROUP} was not deleted from CMS server successfully`);
});
});
}

View File

@@ -0,0 +1,118 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import 'mocha';
import * as azdata from 'azdata';
import * as utils from '../utils';
import * as path from 'path';
import * as fs from 'fs';
import * as os from 'os';
import * as mssql from '../../../mssql';
import * as vscode from 'vscode';
import { isTestSetupCompleted } from '../testContext';
import { getStandaloneServer } from '../testConfig';
import * as assert from 'assert';
import { promisify } from 'util';
const retryCount = 24; // 2 minutes
const dacpac1: string = path.join(__dirname, '../testData/Database1.dacpac');
if (isTestSetupCompleted()) {
suite('Dacpac integration test suite', () => {
suiteSetup(async function () {
await utils.sleep(5000); // To ensure the providers are registered.
console.log(`Start dacpac tests`);
});
test('Deploy and extract dacpac', async function () {
const server = await getStandaloneServer();
await utils.connectToServer(server);
const nodes = <azdata.objectexplorer.ObjectExplorerNode[]>await azdata.objectexplorer.getActiveConnectionNodes();
const index = nodes.findIndex(node => node.nodePath.includes(server.serverName));
const ownerUri = await azdata.connection.getUriForConnection(nodes[index].connectionId);
const now = new Date();
const databaseName = 'ADS_deployDacpac_' + now.getTime().toString();
try {
const dacfxService = ((await vscode.extensions.getExtension(mssql.extension.name).activate() as mssql.IExtension)).dacFx;
assert(dacfxService, 'DacFx Service Provider is not available');
// Deploy dacpac
const deployResult = await dacfxService.deployDacpac(dacpac1, databaseName, false, ownerUri, azdata.TaskExecutionMode.execute);
await utils.assertDatabaseCreationResult(databaseName, ownerUri, retryCount);
const dbConnectionId = await utils.connectToServer({
serverName: server.serverName,
database: databaseName,
userName: server.userName,
password: server.password,
authenticationTypeName: server.authenticationTypeName,
providerName: server.providerName
});
const dbConnectionOwnerUri = await azdata.connection.getUriForConnection(dbConnectionId);
await utils.assertTableCreationResult('dbo', 'Table1', dbConnectionOwnerUri, retryCount);
await utils.assertTableCreationResult('dbo', 'Table2', dbConnectionOwnerUri, retryCount);
assert(deployResult.success === true && deployResult.errorMessage === '', `Deploy dacpac should succeed Expected: there should be no error. Actual Error message: "${deployResult.errorMessage}"`);
// Extract dacpac
const folderPath = path.join(os.tmpdir(), 'DacFxTest');
if (!(await promisify(fs.exists)(folderPath))) {
await fs.promises.mkdir(folderPath);
}
const packageFilePath = path.join(folderPath, `${databaseName}.dacpac`);
const extractResult = await dacfxService.extractDacpac(databaseName, packageFilePath, databaseName, '1.0.0.0', ownerUri, azdata.TaskExecutionMode.execute);
await utils.assertFileGenerationResult(packageFilePath, retryCount);
assert(extractResult.success === true && extractResult.errorMessage === '', `Extract dacpac should succeed. Expected: there should be no error. Actual Error message: "${extractResult.errorMessage}"`);
} finally {
await utils.deleteDB(server, databaseName, ownerUri);
}
});
const bacpac1: string = path.join(__dirname, '..', 'testData', 'Database1.bacpac');
test('Import and export bacpac', async function () {
const server = await getStandaloneServer();
await utils.connectToServer(server);
const nodes = <azdata.objectexplorer.ObjectExplorerNode[]>await azdata.objectexplorer.getActiveConnectionNodes();
const index = nodes.findIndex(node => node.nodePath.includes(server.serverName));
const ownerUri = await azdata.connection.getUriForConnection(nodes[index].connectionId);
const now = new Date();
const databaseName = 'ADS_importBacpac_' + now.getTime().toString();
try {
let dacfxService = ((await vscode.extensions.getExtension(mssql.extension.name).activate() as mssql.IExtension)).dacFx;
assert(dacfxService, 'DacFx Service Provider is not available');
// Import bacpac
const importResult = await dacfxService.importBacpac(bacpac1, databaseName, ownerUri, azdata.TaskExecutionMode.execute);
await utils.assertDatabaseCreationResult(databaseName, ownerUri, retryCount);
const dbConnectionId = await utils.connectToServer({
serverName: server.serverName,
database: databaseName,
userName: server.userName,
password: server.password,
authenticationTypeName: server.authenticationTypeName,
providerName: server.providerName
});
const dbConnectionOwnerUri = await azdata.connection.getUriForConnection(dbConnectionId);
await utils.assertTableCreationResult('dbo', 'Table1', dbConnectionOwnerUri, retryCount, true);
await utils.assertTableCreationResult('dbo', 'Table2', dbConnectionOwnerUri, retryCount, true);
assert(importResult.success === true && importResult.errorMessage === '', `Expected: Import bacpac should succeed and there should be no error. Actual Error message: "${importResult.errorMessage}"`);
// Export bacpac
const folderPath = path.join(os.tmpdir(), 'DacFxTest');
if (!(await promisify(fs.exists)(folderPath))) {
await fs.promises.mkdir(folderPath);
}
const packageFilePath = path.join(folderPath, `${databaseName}.bacpac`);
const exportResult = await dacfxService.exportBacpac(databaseName, packageFilePath, ownerUri, azdata.TaskExecutionMode.execute);
await utils.assertFileGenerationResult(packageFilePath, retryCount);
assert(exportResult.success === true && exportResult.errorMessage === '', `Expected: Export bacpac should succeed and there should be no error. Actual Error message: "${exportResult.errorMessage}"`);
} finally {
await utils.deleteDB(server, databaseName, ownerUri);
}
});
});
}

View File

@@ -0,0 +1,62 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as testRunner from 'vscode/lib/testrunner';
import { SuiteType, getSuiteType } from 'adstest';
import * as path from 'path';
const suite = getSuiteType();
const options: any = {
ui: 'tdd',
useColors: true,
timeout: 600000
};
if (suite === SuiteType.Stress) {
options.timeout = 7200000; // 2 hours
// StressRuntime sets the default run time in stress/perf mode for those suites. By default ensure that there is sufficient timeout available.
// if ADS_TEST_TIMEOUT is also defined then that value overrides this calculated timeout value. User needs to ensure that ADS_TEST_GREP > StressRuntime if
// both are set.
if (process.env.StressRuntime) {
options.timeout = (120 + 1.2 * parseInt(process.env.StressRuntime)) * 1000; // allow sufficient timeout based on StressRuntime setting
console.log(`setting options.timeout to: ${options.timeout} based on process.env.StressRuntime value of ${process.env.StressRuntime} seconds`);
}
}
// set relevant mocha options from the environment
if (process.env.ADS_TEST_GREP) {
options.grep = process.env.ADS_TEST_GREP;
console.log(`setting options.grep to: ${options.grep}`);
}
if (process.env.ADS_TEST_INVERT_GREP) {
const value = parseInt(process.env.ADS_TEST_INVERT_GREP);
options.invert = Boolean(value);
console.log(`setting options.invert to: ${options.invert}`);
}
if (process.env.ADS_TEST_TIMEOUT) {
options.timeout = parseInt(process.env.ADS_TEST_TIMEOUT);
console.log(`setting options.timeout to: ${options.timeout}`);
}
if (process.env.ADS_TEST_RETRIES) {
options.retries = parseInt(process.env.ADS_TEST_RETRIES);
console.log(`setting options.retries to: ${options.retries}`);
}
if (process.env.BUILD_ARTIFACTSTAGINGDIRECTORY) {
console.log(`environment variable BUILD_ARTIFACTSTAGINGDIRECTORY is set to ${process.env.BUILD_ARTIFACTSTAGINGDIRECTORY} so configuring multiple reporters for test results.\n For this to work the ${process.env.BUILD_ARTIFACTSTAGINGDIRECTORY} must be fully qualified directory and must exist`);
options.reporter = 'mocha-multi-reporters';
options.reporterOptions = {
reporterEnabled: 'spec, mocha-junit-reporter',
mochaJunitReporterReporterOptions: {
testsuitesTitle: `${suite} Tests ${process.platform}`,
mochaFile: path.join(process.env.BUILD_ARTIFACTSTAGINGDIRECTORY, `test-results/${process.platform}-${suite.toLowerCase().replace(/[^\w]/g, '-')}-results.xml`)
}
};
}
testRunner.configure(options);
export = testRunner;

View File

@@ -0,0 +1,463 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import 'mocha';
import * as assert from 'assert';
import * as azdata from 'azdata';
import * as vscode from 'vscode';
import { isTestSetupCompleted } from '../testContext';
import { sqlNotebookContent, writeNotebookToFile, sqlKernelMetadata, getFileName, pySparkNotebookContent, pySparkKernelMetadata, pythonKernelMetadata, sqlNotebookMultipleCellsContent, notebookContentForCellLanguageTest, sqlKernelSpec, pythonKernelSpec, pySparkKernelSpec, CellTypes } from '../notebook.util';
import { getConfigValue, EnvironmentVariable_PYTHON_PATH, TestServerProfile, getStandaloneServer } from '../testConfig';
import { connectToServer, sleep, testServerProfileToIConnectionProfile } from '../utils';
import * as fs from 'fs';
import { stressify } from 'adstest';
import { isNullOrUndefined, promisify } from 'util';
if (isTestSetupCompleted()) {
suite('Notebook integration test suite', function () {
setup(async function () {
console.log(`Start "${this.currentTest.title}"`);
let server = await getStandaloneServer();
assert(server && server.serverName, 'No server could be found');
await connectToServer(server, 6000);
});
teardown(async function () {
await (new NotebookTester()).cleanup(this.currentTest.title);
});
test('Sql NB test @UNSTABLE@', async function () {
await (new NotebookTester()).sqlNbTest(this.test.title);
});
test('Sql NB multiple cells test @UNSTABLE@', async function () {
await (new NotebookTester()).sqlNbMultipleCellsTest(this.test.title);
});
test('Sql NB run cells above and below test', async function () {
await (new NotebookTester()).sqlNbRunCellsAboveBelowTest(this.test.title);
});
test('Clear cell output - SQL notebook', async function () {
await (new NotebookTester()).sqlNbClearOutputs(this.test.title);
});
test('Clear all outputs - SQL notebook ', async function () {
await (new NotebookTester()).sqlNbClearAllOutputs(this.test.title);
});
test('sql language test', async function () {
await (new NotebookTester()).sqlLanguageTest(this.test.title);
});
// TODO: Need to make this test more reliable.
test('should not be dirty after saving notebook test @UNSTABLE@', async function () {
await (new NotebookTester().shouldNotBeDirtyAfterSavingNotebookTest(this.test.title));
});
if (process.env['RUN_PYTHON3_TEST'] === '1') {
test('Python3 notebook test', async function () {
await (new NotebookTester()).python3NbTest(this.test.title);
});
test('Clear all outputs - Python3 notebook ', async function () {
await (new NotebookTester()).python3ClearAllOutputs(this.test.title);
});
test('python language test', async function () {
await (new NotebookTester()).pythonLanguageTest(this.test.title);
});
test('Change kernel different provider SQL to Python to SQL', async function () {
await (new NotebookTester()).sqlNbChangeKernelDifferentProviderTest(this.test.title);
});
test('Change kernel different provider Python to SQL to Python', async function () {
await (new NotebookTester()).pythonChangeKernelDifferentProviderTest(this.test.title);
});
test('Change kernel same provider Python to PySpark to Python', async function () {
await (new NotebookTester()).pythonChangeKernelSameProviderTest(this.test.title);
});
}
if (process.env['RUN_PYSPARK_TEST'] === '1') {
test('PySpark notebook test', async function () {
await (new NotebookTester()).pySparkNbTest(this.test.title);
});
}
/* After https://github.com/microsoft/azuredatastudio/issues/5598 is fixed, enable these tests.
test('scala language test', async function () {
await (new NotebookTester()).scalaLanguageTest(this.test.title);
});
test('empty language test', async function () {
await (new NotebookTester()).emptyLanguageTest(this.test.title);
});
test('cplusplus language test', async function () {
await (new NotebookTester()).cplusplusLanguageTest(this.test.title);
});
*/
});
}
class NotebookTester {
private static ParallelCount = 1;
invocationCount: number = 0;
@stressify({ dop: NotebookTester.ParallelCount })
async pySparkNbTest(title: string): Promise<void> {
let notebook = await this.openNotebook(pySparkNotebookContent, pySparkKernelMetadata, title + this.invocationCount++);
await this.runCell(notebook);
let cellOutputs = notebook.document.cells[0].contents.outputs;
let sparkResult = (<azdata.nb.IStreamResult>cellOutputs[3]).text;
assert(sparkResult === '2', `Expected spark result: 2, Actual: ${sparkResult}`);
}
@stressify({ dop: NotebookTester.ParallelCount })
async python3ClearAllOutputs(title: string): Promise<void> {
let notebook = await this.openNotebook(pySparkNotebookContent, pythonKernelMetadata, title + this.invocationCount++);
await this.runCell(notebook);
await this.verifyClearAllOutputs(notebook);
}
@stressify({ dop: NotebookTester.ParallelCount })
async python3NbTest(title: string): Promise<void> {
let notebook = await this.openNotebook(pySparkNotebookContent, pythonKernelMetadata, title + this.invocationCount++);
await this.runCell(notebook);
let cellOutputs = notebook.document.cells[0].contents.outputs;
console.log('Got cell outputs ---');
if (cellOutputs) {
cellOutputs.forEach(o => console.log(JSON.stringify(o, undefined, '\t')));
}
let result = (<azdata.nb.IExecuteResult>cellOutputs[0]).data['text/plain'];
assert(result === '2', `Expected python result: 2, Actual: ${result}`);
}
@stressify({ dop: NotebookTester.ParallelCount })
async sqlNbClearAllOutputs(title: string): Promise<void> {
let notebook = await this.openNotebook(sqlNotebookContent, sqlKernelMetadata, title + this.invocationCount++);
await this.runCell(notebook);
await this.verifyClearAllOutputs(notebook);
}
async sqlNbClearOutputs(title: string): Promise<void> {
let notebook = await this.openNotebook(sqlNotebookContent, sqlKernelMetadata, title + this.invocationCount++);
await this.runCell(notebook);
await this.verifyClearOutputs(notebook);
}
@stressify({ dop: NotebookTester.ParallelCount })
async sqlNbMultipleCellsTest(title: string): Promise<void> {
let notebook = await this.openNotebook(sqlNotebookMultipleCellsContent, sqlKernelMetadata, title + this.invocationCount++);
await this.runCells(notebook);
const expectedOutput0 = '(1 row affected)';
for (let i = 0; i < 3; i++) {
let cellOutputs = notebook.document.cells[i].contents.outputs;
console.log(`Got cell outputs --- ${i}`);
if (cellOutputs) {
cellOutputs.forEach(console.log);
}
assert(cellOutputs.length === 3, `Expected length: 3, Actual: '${cellOutputs.length}'`);
let actualOutput0 = (<azdata.nb.IDisplayData>cellOutputs[0]).data['text/html'];
console.log('Got first output');
assert(actualOutput0 === expectedOutput0, `Expected row count: '${expectedOutput0}', Actual: '${actualOutput0}'`);
const executeResult = cellOutputs[2] as azdata.nb.IExecuteResult;
assert(Object.keys(executeResult).includes('data'), `Execute result did not include data key. It included ${Object.keys(executeResult)}`);
const applicationDataResource = executeResult.data['application/vnd.dataresource+json'];
assert(Object.keys(applicationDataResource).includes('data'), `Execute result did not include data key. It included ${Object.keys(applicationDataResource)}`);
const actualOutput2 = applicationDataResource.data[0];
assert(actualOutput2[0] === i.toString(), `Expected result: ${i.toString()}, Actual: '${actualOutput2[0]}'`);
console.log('Sql multiple cells NB done');
}
}
async sqlNbRunCellsAboveBelowTest(title: string): Promise<void> {
let notebook = await this.openNotebook(sqlNotebookMultipleCellsContent, sqlKernelMetadata, title + this.invocationCount++);
// When running all cells above a cell, ensure that only cells preceding current cell have output
await this.runCells(notebook, true, undefined, notebook.document.cells[1]);
assert(notebook.document.cells[0].contents.outputs.length === 3, `Expected length: '3', Actual: '${notebook.document.cells[0].contents.outputs.length}'`);
assert(notebook.document.cells[1].contents.outputs.length === 0, `Expected length: '0', Actual: '${notebook.document.cells[1].contents.outputs.length}'`);
assert(notebook.document.cells[2].contents.outputs.length === 0, `Expected length: '0', Actual: '${notebook.document.cells[2].contents.outputs.length}'`);
await notebook.clearAllOutputs();
// When running all cells below a cell, ensure that current cell and cells after have output
await this.runCells(notebook, undefined, true, notebook.document.cells[1]);
assert(notebook.document.cells[0].contents.outputs.length === 0, `Expected length: '0', Actual: '${notebook.document.cells[0].contents.outputs.length}'`);
assert(notebook.document.cells[1].contents.outputs.length === 3, `Expected length: '3', Actual: '${notebook.document.cells[1].contents.outputs.length}'`);
assert(notebook.document.cells[2].contents.outputs.length === 3, `Expected length: '3', Actual: '${notebook.document.cells[2].contents.outputs.length}'`);
}
@stressify({ dop: NotebookTester.ParallelCount })
async sqlNbTest(title: string): Promise<void> {
let notebook = await this.openNotebook(sqlNotebookContent, sqlKernelMetadata, title + this.invocationCount++, true);
await this.runCell(notebook);
const expectedOutput0 = '(1 row affected)';
let cellOutputs = notebook.document.cells[0].contents.outputs;
console.log('Got cell outputs ---');
if (cellOutputs) {
cellOutputs.forEach(o => console.log(o));
}
assert(cellOutputs.length === 3, `Expected length: 3, Actual: ${cellOutputs.length}`);
let actualOutput0 = (<azdata.nb.IDisplayData>cellOutputs[0]).data['text/html'];
console.log('Got first output');
assert(actualOutput0 === expectedOutput0, `Expected row count: ${expectedOutput0}, Actual: ${actualOutput0}`);
let actualOutput2 = (<azdata.nb.IExecuteResult>cellOutputs[2]).data['application/vnd.dataresource+json'].data[0];
assert(actualOutput2[0] === '1', `Expected result: 1, Actual: '${actualOutput2[0]}'`);
}
async sqlNbChangeKernelDifferentProviderTest(title: string): Promise<void> {
let notebook = await this.openNotebook(sqlNotebookContent, sqlKernelMetadata, title);
await this.runCell(notebook);
assert(notebook.document.providerId === 'sql', `Expected providerId to be sql, Actual: ${notebook.document.providerId}`);
assert(notebook.document.kernelSpec.name === 'SQL', `Expected first kernel name: SQL, Actual: ${notebook.document.kernelSpec.name}`);
let kernelChanged = await notebook.changeKernel(pythonKernelSpec);
assert(notebook.document.providerId === 'jupyter', `Expected providerId to be jupyter, Actual: ${notebook.document.providerId}`);
assert(kernelChanged && notebook.document.kernelSpec.name === 'python3', `Expected second kernel name: python3, Actual: ${notebook.document.kernelSpec.name}`);
kernelChanged = await notebook.changeKernel(sqlKernelSpec);
assert(notebook.document.providerId === 'sql', `Expected providerId to be sql, Actual: ${notebook.document.providerId}`);
assert(kernelChanged && notebook.document.kernelSpec.name === 'SQL', `Expected third kernel name: SQL, Actual: ${notebook.document.kernelSpec.name}`);
}
async shouldNotBeDirtyAfterSavingNotebookTest(title: string): Promise<void> {
// Given a notebook that's been edited (in this case, open notebook runs the 1st cell and adds an output)
let notebook = await this.openNotebook(sqlNotebookContent, sqlKernelMetadata, title);
await this.runCell(notebook);
assert(notebook.document.providerId === 'sql', `Expected providerId to be sql, Actual: ${notebook.document.providerId}`);
assert(notebook.document.kernelSpec.name === 'SQL', `Expected first kernel name: SQL, Actual: ${notebook.document.kernelSpec.name}`);
assert(notebook.document.isDirty === true, 'Notebook should be dirty after edit');
// When I save it, it should no longer be dirty
let saved = await notebook.document.save();
assert(saved === true, 'Expect initial save to succeed');
// Note: need to sleep after save as the change events happen after save
// We need to give back the thread or the event won't have been drained.
// This is consistent with VSCode APIs, so keeping as-is
await sleep(100);
assert(notebook.document.isDirty === false, 'Notebook should not be dirty after initial save');
// And when I edit again, should become dirty
let edited = await notebook.edit(builder => {
builder.insertCell({
cell_type: CellTypes.Code,
source: ''
});
});
assert(edited === true, 'Expect edit to succeed');
await sleep(100);
assert(notebook.document.isDirty === true, 'Notebook should be dirty after edit');
// Finally on 2nd save it should no longer be dirty
saved = await notebook.document.save();
await sleep(100);
assert(saved === true, 'Expect save after edit to succeed');
assert(notebook.document.isDirty === false, 'Notebook should not be dirty after 2nd save');
}
async pythonChangeKernelDifferentProviderTest(title: string): Promise<void> {
let notebook = await this.openNotebook(pySparkNotebookContent, pythonKernelMetadata, title);
await this.runCell(notebook);
assert(notebook.document.providerId === 'jupyter', `Expected providerId to be jupyter, Actual: ${notebook.document.providerId}`);
assert(notebook.document.kernelSpec.name === 'python3', `Expected first kernel name: python3, Actual: ${notebook.document.kernelSpec.name}`);
let kernelChanged = await notebook.changeKernel(sqlKernelSpec);
assert(notebook.document.providerId === 'sql', `Expected providerId to be sql, Actual: ${notebook.document.providerId}`);
assert(kernelChanged && notebook.document.kernelSpec.name === 'SQL', `Expected second kernel name: SQL, Actual: ${notebook.document.kernelSpec.name}`);
kernelChanged = await notebook.changeKernel(pythonKernelSpec);
assert(notebook.document.providerId === 'jupyter', `Expected providerId to be jupyter, Actual: ${notebook.document.providerId}`);
assert(kernelChanged && notebook.document.kernelSpec.name === 'python3', `Expected third kernel name: python3, Actual: ${notebook.document.kernelSpec.name}`);
}
async pythonChangeKernelSameProviderTest(title: string): Promise<void> {
let notebook = await this.openNotebook(pySparkNotebookContent, pythonKernelMetadata, title);
await this.runCell(notebook);
assert(notebook.document.providerId === 'jupyter', `Expected providerId to be jupyter, Actual: ${notebook.document.providerId}`);
assert(notebook.document.kernelSpec.name === 'python3', `Expected first kernel name: python3, Actual: ${notebook.document.kernelSpec.name}`);
let kernelChanged = await notebook.changeKernel(pySparkKernelSpec);
assert(notebook.document.providerId === 'jupyter', `Expected providerId to be jupyter, Actual: ${notebook.document.providerId}`);
assert(kernelChanged && notebook.document.kernelSpec.name === 'pysparkkernel', `Expected second kernel name: pysparkkernel, Actual: ${notebook.document.kernelSpec.name}`);
kernelChanged = await notebook.changeKernel(pythonKernelSpec);
assert(notebook.document.providerId === 'jupyter', `Expected providerId to be jupyter, Actual: ${notebook.document.providerId}`);
assert(kernelChanged && notebook.document.kernelSpec.name === 'python3', `Expected third kernel name: python3, Actual: ${notebook.document.kernelSpec.name}`);
}
async scalaLanguageTest(title: string): Promise<void> {
let language = 'scala';
await this.cellLanguageTest(notebookContentForCellLanguageTest, title + this.invocationCount++, language, {
'kernelspec': {
'name': '',
'display_name': ''
},
'language_info': {
name: language,
version: '',
mimetype: ''
}
});
}
async cplusplusLanguageTest(title: string): Promise<void> {
let language = 'cplusplus';
await this.cellLanguageTest(notebookContentForCellLanguageTest, title + this.invocationCount++, language, {
'kernelspec': {
'name': '',
'display_name': ''
},
'language_info': {
name: language,
version: '',
mimetype: ''
}
});
}
async emptyLanguageTest(title: string): Promise<void> {
let language = '';
await this.cellLanguageTest(notebookContentForCellLanguageTest, title + this.invocationCount++, language, {
'kernelspec': {
'name': language,
'display_name': ''
},
'language_info': {
name: language,
version: '',
mimetype: 'x-scala'
}
});
}
async sqlLanguageTest(title: string): Promise<void> {
let language = 'sql';
await this.cellLanguageTest(notebookContentForCellLanguageTest, title + this.invocationCount++, language, {
'kernelspec': {
'name': language,
'display_name': language.toUpperCase()
},
'language_info': {
'name': language,
'version': '',
'mimetype': ''
}
});
}
async pythonLanguageTest(title: string): Promise<void> {
let language = 'python';
await this.cellLanguageTest(notebookContentForCellLanguageTest, title + this.invocationCount++, language, {
'kernelspec': {
'name': 'python3',
'display_name': 'Python 3'
},
'language_info': {
'name': language,
'version': '',
'mimetype': ''
}
});
}
async cleanup(testName: string): Promise<void> {
try {
let fileName = getFileName(testName + this.invocationCount++);
if (await promisify(fs.exists)(fileName)) {
await fs.promises.unlink(fileName);
console.log(`"${fileName}" is deleted.`);
}
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
}
catch (err) {
console.log(err);
}
finally {
console.log(`"${testName}" is done`);
}
}
async openNotebook(content: azdata.nb.INotebookContents, kernelMetadata: any, testName: string, connectToDifferentServer?: boolean): Promise<azdata.nb.NotebookEditor> {
let notebookConfig = vscode.workspace.getConfiguration('notebook');
notebookConfig.update('pythonPath', getConfigValue(EnvironmentVariable_PYTHON_PATH), 1);
let server: TestServerProfile;
if (!connectToDifferentServer) {
server = await getStandaloneServer();
assert(server && server.serverName, 'No server could be found in openNotebook');
await connectToServer(server, 6000);
}
let notebookJson = Object.assign({}, content, { metadata: kernelMetadata });
let uri = writeNotebookToFile(notebookJson, testName);
console.log('Notebook uri ' + uri);
let nbShowOptions: azdata.nb.NotebookShowOptions;
if (server) {
nbShowOptions = { connectionProfile: testServerProfileToIConnectionProfile(server) };
}
let notebook = await azdata.nb.showNotebookDocument(uri, nbShowOptions);
return notebook;
}
async runCells(notebook: azdata.nb.NotebookEditor, runCellsAbove?: boolean, runCellsBelow?: boolean, currentCell?: azdata.nb.NotebookCell) {
assert(notebook !== undefined && notebook !== null, 'Expected notebook object is defined');
let ran;
if (runCellsAbove) {
ran = await notebook.runAllCells(undefined, currentCell);
} else if (runCellsBelow) {
ran = await notebook.runAllCells(currentCell, undefined);
} else {
ran = await notebook.runAllCells();
}
assert(ran, 'Notebook runCell should succeed');
}
async runCell(notebook: azdata.nb.NotebookEditor, cell?: azdata.nb.NotebookCell) {
if (isNullOrUndefined(cell)) {
cell = notebook.document.cells[0];
}
let ran = await notebook.runCell(cell);
assert(ran, 'Notebook runCell should succeed');
}
async verifyClearAllOutputs(notebook: azdata.nb.NotebookEditor): Promise<void> {
let cellWithOutputs = notebook.document.cells.find(cell => cell.contents && cell.contents.outputs && cell.contents.outputs.length > 0);
assert(cellWithOutputs !== undefined, 'Could not find notebook cells with outputs');
console.log('Before clearing cell outputs');
let clearedOutputs = await notebook.clearAllOutputs();
let cells = notebook.document.cells;
cells.forEach(cell => {
assert(cell.contents && cell.contents.outputs && cell.contents.outputs.length === 0, `Expected Output: 0, Actual: '${cell.contents.outputs.length}'`);
});
assert(clearedOutputs, 'Outputs of all the code cells from Python notebook should be cleared');
console.log('After clearing cell outputs');
}
async verifyClearOutputs(notebook: azdata.nb.NotebookEditor): Promise<void> {
let cellWithOutputs = notebook.document.cells[0].contents && notebook.document.cells[0].contents.outputs && notebook.document.cells[0].contents.outputs.length > 0;
assert(cellWithOutputs === true, 'Expected first cell to have outputs');
let clearedOutputs = await notebook.clearOutput(notebook.document.cells[0]);
let firstCell = notebook.document.cells[0];
assert(firstCell.contents && firstCell.contents.outputs && firstCell.contents.outputs.length === 0, `Expected Output: 0, Actual: '${firstCell.contents.outputs.length}'`);
assert(clearedOutputs, 'Outputs of requested code cell should be cleared');
}
async cellLanguageTest(content: azdata.nb.INotebookContents, testName: string, languageConfigured: string, metadataInfo: any) {
let notebookJson = Object.assign({}, content, { metadata: metadataInfo });
let uri = writeNotebookToFile(notebookJson, testName);
let notebook = await azdata.nb.showNotebookDocument(uri);
await notebook.document.save();
let languageInNotebook = notebook.document.cells[0].contents.metadata.language;
assert(languageInNotebook === languageConfigured, `Expected cell language is: ${languageConfigured}, Actual: ${languageInNotebook}`);
}
}

View File

@@ -0,0 +1,171 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import 'mocha';
import * as azdata from 'azdata';
import { isTestSetupCompleted } from '../testContext';
import { getBdcServer, TestServerProfile, getAzureServer, getStandaloneServer } from '../testConfig';
import { connectToServer, createDB, deleteDB, DefaultConnectTimeoutInMs, asyncTimeout } from '../utils';
import * as assert from 'assert';
import { stressify } from 'adstest';
if (isTestSetupCompleted()) {
suite('Object Explorer integration suite', () => {
test.skip('BDC instance node label test', async function () {
return await (new ObjectExplorerTester()).bdcNodeLabelTest();
});
test('Standalone instance node label test', async function () {
return await (new ObjectExplorerTester()).standaloneNodeLabelTest();
});
test('Azure SQL DB instance node label test @UNSTABLE@', async function () {
return await (new ObjectExplorerTester()).sqlDbNodeLabelTest();
});
test.skip('BDC instance context menu test', async function () {
return await (new ObjectExplorerTester()).bdcContextMenuTest();
});
test('Azure SQL DB context menu test @UNSTABLE@', async function () {
return await (new ObjectExplorerTester()).sqlDbContextMenuTest();
});
test('Standalone database context menu test', async function () {
return await (new ObjectExplorerTester()).standaloneContextMenuTest();
});
});
}
class ObjectExplorerTester {
private static ParallelCount = 1;
@stressify({ dop: ObjectExplorerTester.ParallelCount })
async bdcNodeLabelTest(): Promise<void> {
const expectedNodeLabel = ['Databases', 'Security', 'Server Objects'];
const server = await getBdcServer();
return await this.verifyOeNode(server, DefaultConnectTimeoutInMs, expectedNodeLabel);
}
@stressify({ dop: ObjectExplorerTester.ParallelCount })
async standaloneNodeLabelTest(): Promise<void> {
if (process.platform === 'win32') {
const expectedNodeLabel = ['Databases', 'Security', 'Server Objects'];
const server = await getStandaloneServer();
return await this.verifyOeNode(server, DefaultConnectTimeoutInMs, expectedNodeLabel);
}
}
@stressify({ dop: ObjectExplorerTester.ParallelCount })
async sqlDbNodeLabelTest(): Promise<void> {
const expectedNodeLabel = ['Databases', 'Security'];
const server = await getAzureServer();
return await this.verifyOeNode(server, DefaultConnectTimeoutInMs, expectedNodeLabel);
}
@stressify({ dop: ObjectExplorerTester.ParallelCount })
async sqlDbContextMenuTest(): Promise<void> {
const server = await getAzureServer();
const expectedActions = ['Manage', 'New Query', 'New Notebook', 'Disconnect', 'Delete Connection', 'Refresh', 'Data-tier Application wizard', 'Launch Profiler'];
return await this.verifyContextMenu(server, expectedActions);
}
@stressify({ dop: ObjectExplorerTester.ParallelCount })
async standaloneContextMenuTest(): Promise<void> {
const server = await getStandaloneServer();
let expectedActions: string[] = [];
// Generate Scripts and Properties come from the admin-tool-ext-win extension which is for Windows only, so the item won't show up on non-Win32 platforms
if (process.platform === 'win32') {
expectedActions = ['Manage', 'New Query', 'New Notebook', 'Refresh', 'Backup', 'Restore', 'Data-tier Application wizard', 'Schema Compare', 'Import wizard', 'Generate Scripts...', 'Properties'];
}
else {
expectedActions = ['Manage', 'New Query', 'New Notebook', 'Refresh', 'Backup', 'Restore', 'Data-tier Application wizard', 'Schema Compare', 'Import wizard'];
}
return await this.verifyDBContextMenu(server, DefaultConnectTimeoutInMs, expectedActions);
}
@stressify({ dop: ObjectExplorerTester.ParallelCount })
async bdcContextMenuTest(): Promise<void> {
const server = await getBdcServer();
let expectedActions: string[];
// Properties comes from the admin-tool-ext-win extension which is for Windows only, so the item won't show up on non-Win32 platforms
if (process.platform === 'win32') {
expectedActions = ['Manage', 'New Query', 'New Notebook', 'Disconnect', 'Delete Connection', 'Refresh', 'Data-tier Application wizard', 'Launch Profiler', 'Properties'];
}
else {
expectedActions = ['Manage', 'New Query', 'New Notebook', 'Disconnect', 'Delete Connection', 'Refresh', 'Data-tier Application wizard', 'Launch Profiler'];
}
return await this.verifyContextMenu(server, expectedActions);
}
async verifyContextMenu(server: TestServerProfile, expectedActions: string[]): Promise<void> {
await connectToServer(server, DefaultConnectTimeoutInMs);
const nodes = <azdata.objectexplorer.ObjectExplorerNode[]>await azdata.objectexplorer.getActiveConnectionNodes();
assert(nodes.length > 0, `Expecting at least one active connection, actual: ${nodes.length}`);
const index = nodes.findIndex(node => node.nodePath.includes(server.serverName));
assert(index !== -1, `Failed to find server: "${server.serverName}" in OE tree`);
const node = nodes[index];
const actions = await azdata.objectexplorer.getNodeActions(node.connectionId, node.nodePath);
const expectedString = expectedActions.join(',');
const actualString = actions.join(',');
return assert(expectedActions.length === actions.length && expectedString === actualString, `Expected actions: "${expectedString}", Actual actions: "${actualString}"`);
}
async verifyOeNode(server: TestServerProfile, timeout: number, expectedNodeLabel: string[]): Promise<void> {
await connectToServer(server, timeout);
const nodes = <azdata.objectexplorer.ObjectExplorerNode[]>await azdata.objectexplorer.getActiveConnectionNodes();
assert(nodes.length > 0, `Expecting at least one active connection, actual: ${nodes.length}`);
const index = nodes.findIndex(node => node.nodePath.includes(server.serverName));
assert(index !== -1, `Failed to find server: "${server.serverName}" in OE tree`);
// TODO: #7146 HDFS isn't always filled in by the call to getChildren since it's loaded asynchronously. To avoid this test being flaky just removing
// the node for now if it exists until a proper fix can be made.
let children: azdata.objectexplorer.ObjectExplorerNode[];
try {
children = await asyncTimeout(nodes[index].getChildren(), timeout);
} catch (e) {
return assert.fail('getChildren() timed out...', e);
}
const nonHDFSChildren = children.filter(c => c.label !== 'HDFS');
const actualLabelsString = nonHDFSChildren.map(c => c.label).join(',');
const expectedLabelString = expectedNodeLabel.join(',');
return assert(expectedNodeLabel.length === nonHDFSChildren.length && expectedLabelString === actualLabelsString, `Expected node label: "${expectedLabelString}", Actual: "${actualLabelsString}"`);
}
async verifyDBContextMenu(server: TestServerProfile, timeoutinMS: number, expectedActions: string[]): Promise<void> {
await connectToServer(server, timeoutinMS);
const nodes = <azdata.objectexplorer.ObjectExplorerNode[]>await azdata.objectexplorer.getActiveConnectionNodes();
assert(nodes.length > 0, `Expecting at least one active connection, actual: ${nodes.length}`);
const 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 dbName: string = 'ads_test_VerifyDBContextMenu_' + new Date().getTime().toString();
try {
await createDB(dbName, ownerUri);
const serverNode = nodes[index];
const children = await serverNode.getChildren();
assert(children[0].label === 'Databases', `Expected Databases node. Actual ${children[0].label}`);
const databasesFolder = children[0];
const databases = await databasesFolder.getChildren();
assert(databases.length > 2, `No database present, can not test further`); // System Databses folder and at least one database
const actions = await azdata.objectexplorer.getNodeActions(databases[1].connectionId, databases[1].nodePath);
const expectedString = expectedActions.join(',');
const actualString = actions.join(',');
return assert(expectedActions.length === actions.length && expectedString === actualString, `Expected actions: "${expectedString}", Actual actions: "${actualString}"`);
}
finally {
await deleteDB(server, dbName, ownerUri);
}
}
}

View File

@@ -0,0 +1,371 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import 'mocha';
import * as azdata from 'azdata';
import * as vscode from 'vscode';
import * as utils from '../utils';
import * as mssql from '../../../mssql';
import * as os from 'os';
import * as fs from 'fs';
const path = require('path');
import { isTestSetupCompleted } from '../testContext';
import * as assert from 'assert';
import { getStandaloneServer } from '../testConfig';
import { stressify } from 'adstest';
import { promisify } from 'util';
let schemaCompareService: mssql.ISchemaCompareService;
let dacfxService: mssql.IDacFxService;
let schemaCompareTester: SchemaCompareTester;
const dacpac1: string = path.join(__dirname, '..','testData', 'Database1.dacpac');
const dacpac2: string = path.join(__dirname, '..', 'testData', 'Database2.dacpac');
const includeExcludeSourceDacpac: string = path.join(__dirname, '..', 'testData', 'SchemaCompareIncludeExcludeSource.dacpac');
const includeExcludeTargetDacpac: string = path.join(__dirname, '..', 'testData', 'SchemaCompareIncludeExcludeTarget.dacpac');
const SERVER_CONNECTION_TIMEOUT: number = 3000;
const retryCount = 24; // 2 minutes
const folderPath = path.join(os.tmpdir(), 'SchemaCompareTest');
if (isTestSetupCompleted()) {
suite('Schema compare integration test suite', () => {
suiteSetup(async function () {
let attempts: number = 20;
while (attempts > 0) {
schemaCompareService = ((await vscode.extensions.getExtension(mssql.extension.name).activate() as mssql.IExtension)).schemaCompare;
if (schemaCompareService) {
break;
}
attempts--;
await utils.sleep(1000); // To ensure the providers are registered.
}
dacfxService = ((await vscode.extensions.getExtension(mssql.extension.name).activate() as mssql.IExtension)).dacFx;
schemaCompareTester = new SchemaCompareTester();
console.log(`Start schema compare tests`);
});
test('Schema compare dacpac to dacpac comparison and scmp', async function () {
await schemaCompareTester.SchemaCompareDacpacToDacpac();
});
test('Schema compare database to database comparison, script generation, and scmp', async function () {
await schemaCompareTester.SchemaCompareDatabaseToDatabase();
});
test('Schema compare dacpac to database comparison, script generation, and scmp', async function () {
await schemaCompareTester.SchemaCompareDacpacToDatabase();
});
test('Schema compare dacpac to dacpac comparison with include exclude', async function () {
await schemaCompareTester.SchemaCompareIncludeExcludeDacpacToDacpac();
});
});
}
class SchemaCompareTester {
private static ParallelCount = 1;
@stressify({ dop: SchemaCompareTester.ParallelCount })
async SchemaCompareDacpacToDacpac(): Promise<void> {
assert(schemaCompareService, 'Schema Compare Service Provider is not available');
const now = new Date();
const operationId = 'testOperationId_' + now.getTime().toString();
let source: mssql.SchemaCompareEndpointInfo = {
endpointType: mssql.SchemaCompareEndpointType.Dacpac,
packageFilePath: dacpac1,
serverDisplayName: '',
serverName: '',
databaseName: '',
ownerUri: '',
connectionDetails: undefined
};
let target: mssql.SchemaCompareEndpointInfo = {
endpointType: mssql.SchemaCompareEndpointType.Dacpac,
packageFilePath: dacpac2,
serverDisplayName: '',
serverName: '',
databaseName: '',
ownerUri: '',
connectionDetails: undefined
};
let schemaCompareResult = await schemaCompareService.schemaCompare(operationId, source, target, azdata.TaskExecutionMode.execute, null);
this.assertSchemaCompareResult(schemaCompareResult, operationId, 4);
// save to scmp
const filepath = path.join(folderPath, `ads_schemaCompare_${now.getTime().toString()}.scmp`);
if (!(await promisify(fs.exists)(folderPath))) {
await fs.promises.mkdir(folderPath);
}
const saveScmpResult = await schemaCompareService.schemaCompareSaveScmp(source, target, azdata.TaskExecutionMode.execute, null, filepath, [], []);
assert(saveScmpResult.success && !saveScmpResult.errorMessage, `Save scmp should succeed. Expected: there should be no error. Actual Error message: "${saveScmpResult.errorMessage}`);
assert(await promisify(fs.exists)(filepath), `File ${filepath} is expected to be present`);
// open scmp
const openScmpResult = await schemaCompareService.schemaCompareOpenScmp(filepath);
assert(openScmpResult.success && !openScmpResult.errorMessage, `Open scmp should succeed. Expected: there should be no error. Actual Error message: "${openScmpResult.errorMessage}`);
assert(openScmpResult.sourceEndpointInfo.packageFilePath === source.packageFilePath, `Expected: source packageFilePath to be ${source.packageFilePath}, Actual: ${openScmpResult.sourceEndpointInfo.packageFilePath}`);
assert(openScmpResult.targetEndpointInfo.packageFilePath === target.packageFilePath, `Expected: target packageFilePath to be ${target.packageFilePath}, Actual: ${openScmpResult.targetEndpointInfo.packageFilePath}`);
}
@stressify({ dop: SchemaCompareTester.ParallelCount })
async SchemaCompareDatabaseToDatabase(): Promise<void> {
let server = await getStandaloneServer();
await utils.connectToServer(server, SERVER_CONNECTION_TIMEOUT);
let nodes = <azdata.objectexplorer.ObjectExplorerNode[]>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 now = new Date();
const operationId = 'testOperationId_' + now.getTime().toString();
const sourceDB: string = 'ads_schemaCompare_sourceDB_' + now.getTime().toString();
const targetDB: string = 'ads_schemaCompare_targetDB_' + now.getTime().toString();
try {
assert(dacfxService, 'DacFx Service Provider is not available');
let result1 = await dacfxService.deployDacpac(dacpac1, sourceDB, true, ownerUri, azdata.TaskExecutionMode.execute);
let result2 = await dacfxService.deployDacpac(dacpac2, targetDB, true, ownerUri, azdata.TaskExecutionMode.execute);
assert(result1.success === true, 'Deploy source database should succeed');
assert(result2.success === true, 'Deploy target database should succeed');
await utils.assertDatabaseCreationResult(sourceDB, ownerUri, retryCount);
await utils.assertDatabaseCreationResult(targetDB, ownerUri, retryCount);
assert(schemaCompareService, 'Schema Compare Service Provider is not available');
let source: mssql.SchemaCompareEndpointInfo = {
endpointType: mssql.SchemaCompareEndpointType.Database,
packageFilePath: '',
serverDisplayName: '',
serverName: server.serverName,
databaseName: sourceDB,
ownerUri: ownerUri,
connectionDetails: undefined
};
let target: mssql.SchemaCompareEndpointInfo = {
endpointType: mssql.SchemaCompareEndpointType.Database,
packageFilePath: '',
serverDisplayName: '',
serverName: server.serverName,
databaseName: targetDB,
ownerUri: ownerUri,
connectionDetails: undefined
};
let schemaCompareResult = await schemaCompareService.schemaCompare(operationId, source, target, azdata.TaskExecutionMode.execute, null);
this.assertSchemaCompareResult(schemaCompareResult, operationId, 4);
let status = await schemaCompareService.schemaCompareGenerateScript(schemaCompareResult.operationId, server.serverName, targetDB, azdata.TaskExecutionMode.script);
// TODO : add wait for tasks to complete
// script generation might take too long and the 'success' status does not mean that script is created.
await this.assertScriptGenerationResult(status, target.serverName, target.databaseName);
// save to scmp
const filepath = path.join(folderPath, `ads_schemaCompare_${now.getTime().toString()}.scmp`);
if (!(await promisify(fs.exists)(folderPath))) {
await fs.promises.mkdir(folderPath);
}
const saveScmpResult = await schemaCompareService.schemaCompareSaveScmp(source, target, azdata.TaskExecutionMode.execute, null, filepath, [], []);
assert(saveScmpResult.success && !saveScmpResult.errorMessage, `Save scmp should succeed. Expected: there should be no error. Actual Error message: "${saveScmpResult.errorMessage}`);
assert(promisify(fs.exists)(filepath), `File ${filepath} is expected to be present`);
// open scmp
const openScmpResult = await schemaCompareService.schemaCompareOpenScmp(filepath);
assert(openScmpResult.success && !openScmpResult.errorMessage, `Open scmp should succeed. Expected: there should be no error. Actual Error message: "${openScmpResult.errorMessage}`);
assert(openScmpResult.sourceEndpointInfo.databaseName === source.databaseName, `Expected: source database to be ${source.databaseName}, Actual: ${openScmpResult.sourceEndpointInfo.databaseName}`);
assert(openScmpResult.targetEndpointInfo.databaseName === target.databaseName, `Expected: target database to be ${target.databaseName}, Actual: ${openScmpResult.targetEndpointInfo.databaseName}`);
await fs.promises.unlink(filepath);
}
finally {
await utils.deleteDB(server, sourceDB, ownerUri);
await utils.deleteDB(server, targetDB, ownerUri);
}
}
@stressify({ dop: SchemaCompareTester.ParallelCount })
async SchemaCompareDacpacToDatabase(): Promise<void> {
let server = await getStandaloneServer();
await utils.connectToServer(server, SERVER_CONNECTION_TIMEOUT);
let nodes = <azdata.objectexplorer.ObjectExplorerNode[]>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 now = new Date();
const operationId = 'testOperationId_' + now.getTime().toString();
const targetDB: string = 'ads_schemaCompare_targetDB_' + now.getTime().toString();
try {
assert(dacfxService, 'DacFx Service Provider is not available');
let result = await dacfxService.deployDacpac(dacpac2, targetDB, true, ownerUri, azdata.TaskExecutionMode.execute);
assert(result.success === true, 'Deploy database 2 (target) should succeed');
let source: mssql.SchemaCompareEndpointInfo = {
endpointType: mssql.SchemaCompareEndpointType.Dacpac,
packageFilePath: dacpac1,
serverDisplayName: '',
serverName: '',
databaseName: '',
ownerUri: ownerUri,
connectionDetails: undefined
};
let target: mssql.SchemaCompareEndpointInfo = {
endpointType: mssql.SchemaCompareEndpointType.Database,
packageFilePath: '',
serverDisplayName: '',
serverName: server.serverName,
databaseName: targetDB,
ownerUri: ownerUri,
connectionDetails: undefined
};
assert(schemaCompareService, 'Schema Compare Service Provider is not available');
let schemaCompareResult = await schemaCompareService.schemaCompare(operationId, source, target, azdata.TaskExecutionMode.execute, null);
this.assertSchemaCompareResult(schemaCompareResult, operationId, 4);
let status = await schemaCompareService.schemaCompareGenerateScript(schemaCompareResult.operationId, server.serverName, targetDB, azdata.TaskExecutionMode.script);
await this.assertScriptGenerationResult(status, target.serverName, target.databaseName);
// save to scmp
const filepath = path.join(folderPath, `ads_schemaCompare_${now.getTime().toString()}.scmp`);
if (!(await promisify(fs.exists)(folderPath))) {
await fs.promises.mkdir(folderPath);
}
const saveScmpResult = await schemaCompareService.schemaCompareSaveScmp(source, target, azdata.TaskExecutionMode.execute, null, filepath, [], []);
assert(saveScmpResult.success && !saveScmpResult.errorMessage, `Save scmp should succeed. Expected: there should be no error. Actual Error message: "${saveScmpResult.errorMessage}`);
assert(await promisify(fs.exists)(filepath), `File ${filepath} is expected to be present`);
// open scmp
const openScmpResult = await schemaCompareService.schemaCompareOpenScmp(filepath);
assert(openScmpResult.success && !openScmpResult.errorMessage, `Open scmp should succeed. Expected: there should be no error. Actual Error message: "${openScmpResult.errorMessage}`);
assert(openScmpResult.sourceEndpointInfo.packageFilePath === source.packageFilePath, `Expected: source packageFilePath to be ${source.packageFilePath}, Actual: ${openScmpResult.sourceEndpointInfo.packageFilePath}`);
assert(openScmpResult.targetEndpointInfo.databaseName === target.databaseName, `Expected: target database to be ${target.databaseName}, Actual: ${openScmpResult.targetEndpointInfo.databaseName}`);
}
finally {
await utils.deleteDB(server, targetDB, ownerUri);
}
}
@stressify({ dop: SchemaCompareTester.ParallelCount })
async SchemaCompareIncludeExcludeDacpacToDacpac(): Promise<void> {
assert(schemaCompareService, 'Schema Compare Service Provider is not available');
const operationId = 'testOperationId_' + new Date().getTime().toString();
let source: mssql.SchemaCompareEndpointInfo = {
endpointType: mssql.SchemaCompareEndpointType.Dacpac,
packageFilePath: includeExcludeSourceDacpac,
serverDisplayName: '',
serverName: '',
databaseName: '',
ownerUri: '',
connectionDetails: undefined
};
let target: mssql.SchemaCompareEndpointInfo = {
endpointType: mssql.SchemaCompareEndpointType.Dacpac,
packageFilePath: includeExcludeTargetDacpac,
serverDisplayName: '',
serverName: '',
databaseName: '',
ownerUri: '',
connectionDetails: undefined
};
const deploymentOptionsResult = await schemaCompareService.schemaCompareGetDefaultOptions();
let deploymentOptions = deploymentOptionsResult.defaultDeploymentOptions;
const schemaCompareResult = await schemaCompareService.schemaCompare(operationId, source, target, azdata.TaskExecutionMode.execute, deploymentOptions);
this.assertSchemaCompareResult(schemaCompareResult, operationId, 5);
// try to exclude table t2 and it should fail because a dependency is still included
const t2Difference = schemaCompareResult.differences.find(e => e.sourceValue && e.sourceValue[1] === 't2' && e.name === 'SqlTable');
assert(t2Difference !== undefined, 'The difference Table t2 should be found. Should not be undefined');
const excludeResult = await schemaCompareService.schemaCompareIncludeExcludeNode(operationId, t2Difference, false, azdata.TaskExecutionMode.execute);
this.assertIncludeExcludeResult(excludeResult, false, 1, 0);
assert(excludeResult.blockingDependencies[0].sourceValue[1] === 'v1', `Blocking dependency should be view v1. Actual: ${excludeResult.blockingDependencies[0].sourceValue[1]}`);
// Exclude the view v1 that t2 was a dependency for and it should succeed and t2 should also be excluded
const v1Difference = schemaCompareResult.differences.find(e => e.sourceValue && e.sourceValue[1] === 'v1' && e.name === 'SqlView');
assert(v1Difference !== undefined, 'The difference View v1 should be found. Should not be undefined');
const excludeResult2 = await schemaCompareService.schemaCompareIncludeExcludeNode(operationId, v1Difference, false, azdata.TaskExecutionMode.execute);
this.assertIncludeExcludeResult(excludeResult2, true, 0, 1);
assert(excludeResult2.affectedDependencies[0].sourceValue[1] === 't2', `Table t2 should be the affected dependency. Actual: ${excludeResult2.affectedDependencies[0].sourceValue[1]}`);
assert(excludeResult2.affectedDependencies[0].included === false, 'Table t2 should be excluded as a result of excluding v1. Actual: true');
// including the view v1 should also include the table t2
const includeResult = await schemaCompareService.schemaCompareIncludeExcludeNode(operationId, v1Difference, true, azdata.TaskExecutionMode.execute);
this.assertIncludeExcludeResult(includeResult, true, 0, 1);
assert(includeResult.affectedDependencies[0].sourceValue[1] === 't2', `Table t2 should be the affected dependency. Actual: ${includeResult.affectedDependencies[0].sourceValue[1]}`);
assert(includeResult.affectedDependencies[0].included === true, 'Table t2 should be included as a result of including v1. Actual: false');
// excluding views from the comparison should make it so t2 can be excluded
deploymentOptions.excludeObjectTypes.push(mssql.SchemaObjectType.Views);
await schemaCompareService.schemaCompare(operationId, source, target, azdata.TaskExecutionMode.execute, deploymentOptions);
const excludeResult3 = await schemaCompareService.schemaCompareIncludeExcludeNode(operationId, t2Difference, false, azdata.TaskExecutionMode.execute);
this.assertIncludeExcludeResult(excludeResult3, true, 0, 0);
}
private 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) {
assert(result.blockingDependencies.length === expectedBlockingDependenciesLength, `Expected ${expectedBlockingDependenciesLength} blocking dependencies. Actual: ${result.blockingDependencies}`);
} else if (expectedBlockingDependenciesLength !== 0) {
throw new Error(`ExpectedBlockingDependencies length was ${expectedBlockingDependenciesLength} but blockingDependencies was undefined`);
}
if (result.affectedDependencies) {
assert(result.affectedDependencies.length === expectedAffectedDependenciesLength, `Expected ${expectedAffectedDependenciesLength} affected dependencies. Actual: ${result.affectedDependencies}`);
} else if (expectedAffectedDependenciesLength !== 0) {
throw new Error(`ExpectedAffectedDependencies length was ${expectedAffectedDependenciesLength} but affectedDependencies was undefined`);
}
}
private assertSchemaCompareResult(schemaCompareResult: mssql.SchemaCompareResult, operationId: string, expectedDifferenceCount: number): void {
assert(schemaCompareResult.areEqual === false, `Expected: the schemas are not to be equal Actual: Equal`);
assert(schemaCompareResult.errorMessage === null, `Expected: there should be no error. Actual Error message: "${schemaCompareResult.errorMessage}"`);
assert(schemaCompareResult.success === true, `Expected: success in schema compare, Actual: Failure`);
assert(schemaCompareResult.differences.length === expectedDifferenceCount, `Expected: ${expectedDifferenceCount} differences. Actual differences: "${schemaCompareResult.differences.length}"`);
assert(schemaCompareResult.operationId === operationId, `Operation Id Expected to be same as passed. Expected : ${operationId}, Actual ${schemaCompareResult.operationId}`);
}
private async assertScriptGenerationResult(resultstatus: azdata.ResultStatus, server: string, database: string): Promise<void> {
// TODO add more validation
assert(resultstatus.success === true, `Expected: success true Actual: "${resultstatus.success}" Error Message: "${resultstatus.errorMessage}`);
const taskService = azdata.dataprotocol.getProvider<azdata.TaskServicesProvider>('MSSQL', azdata.DataProviderType.TaskServicesProvider);
const tasks = await taskService.getAllTasks({ listActiveTasksOnly: true });
let foundTask: azdata.TaskInfo;
tasks.tasks.forEach(t => {
if (t.serverName === server && t.databaseName === database && t.taskExecutionMode === azdata.TaskExecutionMode.script) {
foundTask = t;
}
});
assert(foundTask, 'Could not find Script task');
assert(foundTask.isCancelable, 'The task should be cancellable');
if (foundTask.status !== azdata.TaskStatus.Succeeded) {
// wait for all tasks completion before exiting test and cleaning up db otherwise tasks fail
let retry = 10;
let allCompleted = false;
while (retry > 0 && !allCompleted) {
retry--;
await utils.sleep(1000);
allCompleted = true;
let tasks = await taskService.getAllTasks({ listActiveTasksOnly: true });
tasks.tasks.forEach(t => {
if (t.status !== azdata.TaskStatus.Succeeded) {
allCompleted = false;
}
});
}
// TODO: add proper validation for task completion to ensure all tasks successfully complete before exiting test
assert(tasks !== null && tasks.tasks.length > 0, 'Tasks should still show in list. This is to ensure that the tasks actually complete.');
}
}
}