SQL Database Project - Deploy db to docker (#16406)

Added a new command to deploy the project to docker
This commit is contained in:
Leila Lali
2021-08-12 13:24:16 -07:00
committed by GitHub
parent 57e11c7b5c
commit 32a71a2de6
18 changed files with 1111 additions and 92 deletions

View File

@@ -0,0 +1,222 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as should from 'should';
import * as sinon from 'sinon';
import * as baselines from '../baselines/baselines';
import * as testUtils from '../testUtils';
import { DeployService } from '../../models/deploy/deployService';
import { Project } from '../../models/project';
import * as vscode from 'vscode';
import * as azdata from 'azdata';
import * as childProcess from 'child_process';
import { AppSettingType, IDeployProfile } from '../../models/deploy/deployProfile';
let fse = require('fs-extra');
let path = require('path');
export interface TestContext {
outputChannel: vscode.OutputChannel;
}
export const mockConnectionResult: azdata.ConnectionResult = {
connected: true,
connectionId: 'id',
errorMessage: '',
errorCode: 0
};
export const mockFailedConnectionResult: azdata.ConnectionResult = {
connected: false,
connectionId: 'id',
errorMessage: 'Failed to connect',
errorCode: 0
};
export function createContext(): TestContext {
return {
outputChannel: {
name: '',
append: () => { },
appendLine: () => { },
clear: () => { },
show: () => { },
hide: () => { },
dispose: () => { }
}
};
}
let sandbox: sinon.SinonSandbox;
describe('deploy service', function (): void {
before(async function (): Promise<void> {
await baselines.loadBaselines();
});
afterEach(function () {
sandbox.restore();
sinon.restore();
});
beforeEach(() => {
sandbox = sinon.createSandbox();
});
it('Should deploy a database to docker container successfully', async function (): Promise<void> {
const testContext = createContext();
const deployProfile: IDeployProfile = {
appSettingType: AppSettingType.AzureFunction,
appSettingFile: '',
deploySettings: undefined,
envVariableName: '',
localDbSetting: {
dbName: 'test',
password: 'PLACEHOLDER',
port: 1433,
serverName: 'localhost',
userName: 'sa'
}
};
const projFilePath = await testUtils.createTestSqlProjFile(baselines.newProjectFileBaseline);
const project1 = await Project.openProject(vscode.Uri.file(projFilePath).fsPath);
const deployService = new DeployService(testContext.outputChannel);
sandbox.stub(azdata.connection, 'connect').returns(Promise.resolve(mockConnectionResult));
sandbox.stub(azdata.connection, 'getUriForConnection').returns(Promise.resolve('connection'));
sandbox.stub(azdata.tasks, 'startBackgroundOperation').callThrough();
sandbox.stub(childProcess, 'exec').yields(undefined, 'id');
let connection = await deployService.deploy(deployProfile, project1);
should(connection).equals('connection');
});
it('Should retry connecting to the server', async function (): Promise<void> {
const testContext = createContext();
const localDbSettings = {
dbName: 'test',
password: 'PLACEHOLDER',
port: 1433,
serverName: 'localhost',
userName: 'sa'
};
const deployService = new DeployService(testContext.outputChannel);
let connectionStub = sandbox.stub(azdata.connection, 'connect');
connectionStub.onFirstCall().returns(Promise.resolve(mockFailedConnectionResult));
connectionStub.onSecondCall().returns(Promise.resolve(mockConnectionResult));
sandbox.stub(azdata.connection, 'getUriForConnection').returns(Promise.resolve('connection'));
sandbox.stub(azdata.tasks, 'startBackgroundOperation').callThrough();
sandbox.stub(childProcess, 'exec').yields(undefined, 'id');
let connection = await deployService.getConnection(localDbSettings, false, 'master', 2);
should(connection).equals('connection');
});
it('Should update app settings successfully', async function (): Promise<void> {
const testContext = createContext();
const projFilePath = await testUtils.createTestSqlProjFile(baselines.newProjectFileBaseline);
const project1 = await Project.openProject(vscode.Uri.file(projFilePath).fsPath);
const jsondData =
{
IsEncrypted: false,
Values: {
AzureWebJobsStorage: 'UseDevelopmentStorage=true',
FUNCTIONS_WORKER_RUNTIME: 'dotnet'
}
};
let settingContent = JSON.stringify(jsondData, undefined, 4);
const expected =
{
IsEncrypted: false,
Values: {
AzureWebJobsStorage: 'UseDevelopmentStorage=true',
FUNCTIONS_WORKER_RUNTIME: 'dotnet',
SQLConnectionString: 'Data Source=localhost,1433;Initial Catalog=test;User id=sa;Password=PLACEHOLDER;'
}
};
const filePath = path.join(project1.projectFolderPath, 'local.settings.json');
await fse.writeFile(filePath, settingContent);
const deployProfile: IDeployProfile = {
appSettingType: AppSettingType.AzureFunction,
appSettingFile: filePath,
deploySettings: undefined,
envVariableName: 'SQLConnectionString',
localDbSetting: {
dbName: 'test',
password: 'PLACEHOLDER',
port: 1433,
serverName: 'localhost',
userName: 'sa'
}
};
const deployService = new DeployService(testContext.outputChannel);
sandbox.stub(childProcess, 'exec').yields(undefined, 'id');
await deployService.updateAppSettings(deployProfile);
let newContent = JSON.parse(fse.readFileSync(filePath, 'utf8'));
should(newContent).deepEqual(expected);
});
it('Should update app settings using connection uri if there are no local settings', async function (): Promise<void> {
const testContext = createContext();
const projFilePath = await testUtils.createTestSqlProjFile(baselines.newProjectFileBaseline);
const project1 = await Project.openProject(vscode.Uri.file(projFilePath).fsPath);
const jsondData =
{
IsEncrypted: false,
Values: {
AzureWebJobsStorage: 'UseDevelopmentStorage=true',
FUNCTIONS_WORKER_RUNTIME: 'dotnet'
}
};
let settingContent = JSON.stringify(jsondData, undefined, 4);
const expected =
{
IsEncrypted: false,
Values: {
AzureWebJobsStorage: 'UseDevelopmentStorage=true',
FUNCTIONS_WORKER_RUNTIME: 'dotnet',
SQLConnectionString: 'connectionString'
}
};
const filePath = path.join(project1.projectFolderPath, 'local.settings.json');
await fse.writeFile(filePath, settingContent);
const deployProfile: IDeployProfile = {
appSettingType: AppSettingType.AzureFunction,
appSettingFile: filePath,
deploySettings: {
connectionUri: 'connection',
databaseName: 'test',
serverName: 'test'
},
envVariableName: 'SQLConnectionString',
localDbSetting: undefined
};
const deployService = new DeployService(testContext.outputChannel);
let connection = new azdata.connection.ConnectionProfile();
sandbox.stub(azdata.connection, 'getConnection').returns(Promise.resolve(connection));
sandbox.stub(childProcess, 'exec').yields(undefined, 'id');
sandbox.stub(azdata.connection, 'getConnectionString').returns(Promise.resolve('connectionString'));
await deployService.updateAppSettings(deployProfile);
let newContent = JSON.parse(fse.readFileSync(filePath, 'utf8'));
should(newContent).deepEqual(expected);
});
it('Should clean a list of docker images successfully', async function (): Promise<void> {
const testContext = createContext();
const deployService = new DeployService(testContext.outputChannel);
let process = sandbox.stub(childProcess, 'exec').yields(undefined, `
id
id2
id3`);
await deployService.cleanDockerObjects(`docker ps -q -a --filter label=test`, ['docker stop', 'docker rm']);
should(process.calledThrice);
});
});

View File

@@ -17,16 +17,18 @@ import { Project } from '../../models/project';
import { ProjectsController } from '../../controllers/projectController';
import { IDeploySettings } from '../../models/IDeploySettings';
import { emptySqlDatabaseProjectTypeId } from '../../common/constants';
import { mockDacFxOptionsResult } from '../testContext';
import { createContext, mockDacFxOptionsResult, TestContext } from '../testContext';
let testContext: TestContext;
describe('Publish Database Dialog', () => {
before(async function (): Promise<void> {
await templates.loadTemplates(path.join(__dirname, '..', '..', '..', 'resources', 'templates'));
await baselines.loadBaselines();
testContext = createContext();
});
it('Should open dialog successfully ', async function (): Promise<void> {
const projController = new ProjectsController();
const projController = new ProjectsController(testContext.outputChannel);
const projFileDir = path.join(os.tmpdir(), `TestProject_${new Date().getTime()}`);
const projFilePath = await projController.createNewProject({
@@ -43,7 +45,7 @@ describe('Publish Database Dialog', () => {
});
it('Should create default database name correctly ', async function (): Promise<void> {
const projController = new ProjectsController();
const projController = new ProjectsController(testContext.outputChannel);
const projFolder = `TestProject_${new Date().getTime()}`;
const projFileDir = path.join(os.tmpdir(), projFolder);

View File

@@ -13,17 +13,24 @@ import { NetCoreTool, DBProjectConfigurationKey, NetCoreInstallLocationKey, NetC
import { getQuotedPath } from '../common/utils';
import { isNullOrUndefined } from 'util';
import { generateTestFolderPath } from './testUtils';
import { createContext, TestContext } from './testContext';
let testContext: TestContext;
describe('NetCoreTool: Net core tests', function (): void {
afterEach(function (): void {
sinon.restore();
});
beforeEach(function (): void {
testContext = createContext();
});
it('Should override dotnet default value with settings', async function (): Promise<void> {
try {
// update settings and validate
await vscode.workspace.getConfiguration(DBProjectConfigurationKey).update(NetCoreInstallLocationKey, 'test value path', true);
const netcoreTool = new NetCoreTool();
const netcoreTool = new NetCoreTool(testContext.outputChannel);
sinon.stub(netcoreTool, 'showInstallDialog').returns(Promise.resolve());
should(netcoreTool.netcoreInstallLocation).equal('test value path'); // the path in settings should be taken
should(await netcoreTool.findOrInstallNetCore()).equal(false); // dotnet can not be present at dummy path in settings
@@ -35,7 +42,7 @@ describe('NetCoreTool: Net core tests', function (): void {
});
it('Should find right dotnet default paths', async function (): Promise<void> {
const netcoreTool = new NetCoreTool();
const netcoreTool = new NetCoreTool(testContext.outputChannel);
sinon.stub(netcoreTool, 'showInstallDialog').returns(Promise.resolve());
await netcoreTool.findOrInstallNetCore();
@@ -53,7 +60,7 @@ describe('NetCoreTool: Net core tests', function (): void {
});
it('should run a command successfully', async function (): Promise<void> {
const netcoreTool = new NetCoreTool();
const netcoreTool = new NetCoreTool(testContext.outputChannel);
const dummyFile = path.join(await generateTestFolderPath(), 'dummy.dacpac');
const outputChannel = vscode.window.createOutputChannel('db project test');

View File

@@ -51,7 +51,7 @@ describe('ProjectsController', function (): void {
describe('project controller operations', function (): void {
describe('Project file operations and prompting', function (): void {
it('Should create new sqlproj file with correct values', async function (): Promise<void> {
const projController = new ProjectsController();
const projController = new ProjectsController(testContext.outputChannel);
const projFileDir = path.join(os.tmpdir(), `TestProject_${new Date().getTime()}`);
const projFilePath = await projController.createNewProject({
@@ -67,7 +67,7 @@ describe('ProjectsController', function (): void {
});
it('Should create new sqlproj file with correct specified target platform', async function (): Promise<void> {
const projController = new ProjectsController();
const projController = new ProjectsController(testContext.outputChannel);
const projFileDir = path.join(os.tmpdir(), `TestProject_${new Date().getTime()}`);
const projTargetPlatform = SqlTargetPlatform.sqlAzure; // default is SQL Server 2019
@@ -89,7 +89,7 @@ describe('ProjectsController', function (): void {
for (const name of ['', ' ', undefined]) {
const showInputBoxStub = sinon.stub(vscode.window, 'showInputBox').resolves(name);
const showErrorMessageSpy = sinon.spy(vscode.window, 'showErrorMessage');
const projController = new ProjectsController();
const projController = new ProjectsController(testContext.outputChannel);
const project = new Project('FakePath');
should(project.files.length).equal(0);
@@ -105,7 +105,7 @@ describe('ProjectsController', function (): void {
const tableName = 'table1';
sinon.stub(vscode.window, 'showInputBox').resolves(tableName);
const spy = sinon.spy(vscode.window, 'showErrorMessage');
const projController = new ProjectsController();
const projController = new ProjectsController(testContext.outputChannel);
const project = await testUtils.createTestProject(baselines.newProjectFileBaseline);
should(project.files.length).equal(0, 'There should be no files');
@@ -121,7 +121,7 @@ describe('ProjectsController', function (): void {
const folderName = 'folder1';
const stub = sinon.stub(vscode.window, 'showInputBox').resolves(folderName);
const projController = new ProjectsController();
const projController = new ProjectsController(testContext.outputChannel);
const project = await testUtils.createTestProject(baselines.newProjectFileBaseline);
const projectRoot = new ProjectRootTreeItem(project);
@@ -141,7 +141,7 @@ describe('ProjectsController', function (): void {
const folderName = 'folder1';
const stub = sinon.stub(vscode.window, 'showInputBox').resolves(folderName);
const projController = new ProjectsController();
const projController = new ProjectsController(testContext.outputChannel);
const project = await testUtils.createTestProject(baselines.openProjectFileBaseline);
const projectRoot = new ProjectRootTreeItem(project);
@@ -180,7 +180,7 @@ describe('ProjectsController', function (): void {
const setupResult = await setupDeleteExcludeTest(proj);
const scriptEntry = setupResult[0], projTreeRoot = setupResult[1], preDeployEntry = setupResult[2], postDeployEntry = setupResult[3], noneEntry = setupResult[4];
const projController = new ProjectsController();
const projController = new ProjectsController(testContext.outputChannel);
await projController.delete(createWorkspaceTreeItem(projTreeRoot.children.find(x => x.friendlyName === 'UpperFolder')!.children[0]) /* LowerFolder */);
await projController.delete(createWorkspaceTreeItem(projTreeRoot.children.find(x => x.friendlyName === 'anotherScript.sql')!));
@@ -206,7 +206,7 @@ describe('ProjectsController', function (): void {
it('Should delete database references', async function (): Promise<void> {
// setup - openProject baseline has a system db reference to master
const proj = await testUtils.createTestProject(baselines.openProjectFileBaseline);
const projController = new ProjectsController();
const projController = new ProjectsController(testContext.outputChannel);
sinon.stub(vscode.window, 'showWarningMessage').returns(<any>Promise.resolve(constants.yesString));
// add dacpac reference
@@ -241,7 +241,7 @@ describe('ProjectsController', function (): void {
const setupResult = await setupDeleteExcludeTest(proj);
const scriptEntry = setupResult[0], projTreeRoot = setupResult[1], preDeployEntry = setupResult[2], postDeployEntry = setupResult[3], noneEntry = setupResult[4];
const projController = new ProjectsController();
const projController = new ProjectsController(testContext.outputChannel);
await projController.exclude(createWorkspaceTreeItem(<FolderNode>projTreeRoot.children.find(x => x.friendlyName === 'UpperFolder')!.children[0]) /* LowerFolder */);
await projController.exclude(createWorkspaceTreeItem(<FileNode>projTreeRoot.children.find(x => x.friendlyName === 'anotherScript.sql')!));
@@ -272,7 +272,7 @@ describe('ProjectsController', function (): void {
const upperFolder = projTreeRoot.children.find(x => x.friendlyName === 'UpperFolder')!;
const lowerFolder = upperFolder.children.find(x => x.friendlyName === 'LowerFolder')!;
const projController = new ProjectsController();
const projController = new ProjectsController(testContext.outputChannel);
// Exclude files under LowerFolder
await projController.exclude(createWorkspaceTreeItem(<FileNode>lowerFolder.children.find(x => x.friendlyName === 'someScript.sql')!));
@@ -296,7 +296,7 @@ describe('ProjectsController', function (): void {
const folderPath = await testUtils.generateTestFolderPath();
const sqlProjPath = await testUtils.createTestSqlProjFile(baselines.newProjectFileBaseline, folderPath);
const treeProvider = new SqlDatabaseProjectTreeViewProvider();
const projController = new ProjectsController();
const projController = new ProjectsController(testContext.outputChannel);
const project = await Project.openProject(vscode.Uri.file(sqlProjPath).fsPath);
treeProvider.load([project]);
@@ -319,7 +319,7 @@ describe('ProjectsController', function (): void {
const preDeployScriptName = 'PreDeployScript1.sql';
const postDeployScriptName = 'PostDeployScript1.sql';
const projController = new ProjectsController();
const projController = new ProjectsController(testContext.outputChannel);
const project = await testUtils.createTestProject(baselines.newProjectFileBaseline);
sinon.stub(vscode.window, 'showInputBox').resolves(preDeployScriptName);
@@ -337,7 +337,7 @@ describe('ProjectsController', function (): void {
it('Should change target platform', async function (): Promise<void> {
sinon.stub(vscode.window, 'showQuickPick').resolves({ label: SqlTargetPlatform.sqlAzure });
const projController = new ProjectsController();
const projController = new ProjectsController(testContext.outputChannel);
const sqlProjPath = await testUtils.createTestSqlProjFile(baselines.openProjectFileBaseline);
const project = await Project.openProject(sqlProjPath);
should(project.getProjectTargetVersion()).equal(constants.targetPlatformToVersion.get(SqlTargetPlatform.sqlServer2019));
@@ -447,7 +447,7 @@ describe('ProjectsController', function (): void {
it('Should create list of all files and folders correctly', async function (): Promise<void> {
const testFolderPath = await testUtils.createDummyFileStructure();
const projController = new ProjectsController();
const projController = new ProjectsController(testContext.outputChannel);
const fileList = await projController.generateList(testFolderPath);
should(fileList.length).equal(15); // Parent folder + 2 files under parent folder + 2 directories with 5 files each
@@ -459,7 +459,7 @@ describe('ProjectsController', function (): void {
let testFolderPath = await testUtils.generateTestFolderPath();
testFolderPath += '_nonexistentFolder'; // Modify folder path to point to a nonexistent location
const projController = new ProjectsController();
const projController = new ProjectsController(testContext.outputChannel);
await projController.generateList(testFolderPath);
should(spy.calledOnce).be.true('showErrorMessage should have been called');
@@ -521,7 +521,7 @@ describe('ProjectsController', function (): void {
let importPath;
let model: ImportDataModel = { connectionUri: 'My Id', database: 'My Database', projName: projectName, filePath: folderPath, version: '1.0.0.0', extractTarget: mssql.ExtractTarget['file'] };
const projController = new ProjectsController();
const projController = new ProjectsController(testContext.outputChannel);
projController.setFilePath(model);
importPath = model.filePath;
@@ -534,7 +534,7 @@ describe('ProjectsController', function (): void {
let importPath;
let model: ImportDataModel = { connectionUri: 'My Id', database: 'My Database', projName: projectName, filePath: folderPath, version: '1.0.0.0', extractTarget: mssql.ExtractTarget['schemaObjectType'] };
const projController = new ProjectsController();
const projController = new ProjectsController(testContext.outputChannel);
projController.setFilePath(model);
importPath = model.filePath;
@@ -591,7 +591,7 @@ describe('ProjectsController', function (): void {
it('Should not allow adding circular project references', async function (): Promise<void> {
const projPath1 = await testUtils.createTestSqlProjFile(baselines.openProjectFileBaseline);
const projPath2 = await testUtils.createTestSqlProjFile(baselines.newProjectFileBaseline);
const projController = new ProjectsController();
const projController = new ProjectsController(testContext.outputChannel);
const project1 = await Project.openProject(vscode.Uri.file(projPath1).fsPath);
const project2 = await Project.openProject(vscode.Uri.file(projPath2).fsPath);
@@ -623,7 +623,7 @@ describe('ProjectsController', function (): void {
it('Should add dacpac references as relative paths', async function (): Promise<void> {
const projFilePath = await testUtils.createTestSqlProjFile(baselines.newProjectFileBaseline);
const projController = new ProjectsController();
const projController = new ProjectsController(testContext.outputChannel);
const project1 = await Project.openProject(vscode.Uri.file(projFilePath).fsPath);
const showErrorMessageSpy = sinon.spy(vscode.window, 'showErrorMessage');

View File

@@ -12,6 +12,7 @@ import * as mssql from '../../../mssql/src/mssql';
export interface TestContext {
context: vscode.ExtensionContext;
dacFxService: TypeMoq.IMock<mssql.IDacFxService>;
outputChannel: vscode.OutputChannel;
}
export const mockDacFxResult = {
@@ -147,7 +148,16 @@ export function createContext(): TestContext {
secrets: undefined as any,
extension: undefined as any
},
dacFxService: TypeMoq.Mock.ofType(MockDacFxService)
dacFxService: TypeMoq.Mock.ofType(MockDacFxService),
outputChannel: {
name: '',
append: () => { },
appendLine: () => { },
clear: () => { },
show: () => { },
hide: () => { },
dispose: () => { }
}
};
}