Skip to content

Commit

Permalink
Generate local Fabric with correct port configuration - unit test cha…
Browse files Browse the repository at this point in the history
…nges (contributes to IBM-Blockchain#776)

Signed-off-by: Simon Stone <sstone1@uk.ibm.com>
  • Loading branch information
Simon Stone committed Apr 24, 2019
1 parent 14e2942 commit f4bc2b5
Show file tree
Hide file tree
Showing 147 changed files with 2,502 additions and 1,174 deletions.
2 changes: 2 additions & 0 deletions client/test/commands/addWalletCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ describe('AddWalletCommand', () => {
const wallets: Array<any> = vscode.workspace.getConfiguration().get('fabric.wallets');
wallets.length.should.equal(1);
wallets[0].should.deep.equal({
managedWallet: false,
name: path.basename(uri.fsPath),
walletPath: uri.fsPath
});
Expand Down Expand Up @@ -141,6 +142,7 @@ describe('AddWalletCommand', () => {
const wallets: Array<any> = vscode.workspace.getConfiguration().get('fabric.wallets');
wallets.length.should.equal(1);
wallets[0].should.deep.equal({
managedWallet: false,
name: 'someWalletName',
walletPath: uri.fsPath
});
Expand Down
17 changes: 1 addition & 16 deletions client/test/commands/createNewIdentityCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ import { UserInputUtil } from '../../src/commands/UserInputUtil';
import { FabricWallet } from '../../src/fabric/FabricWallet';
import { FabricRuntimeManager } from '../../src/fabric/FabricRuntimeManager';
import { FabricRuntimeConnection } from '../../src/fabric/FabricRuntimeConnection';
import { FabricConnectionFactory } from '../../src/fabric/FabricConnectionFactory';
import { ExtensionCommands } from '../../ExtensionCommands';
import { VSCodeBlockchainOutputAdapter } from '../../src/logging/VSCodeBlockchainOutputAdapter';
import { LogType } from '../../src/logging/OutputAdapter';
import { IFabricWallet } from '../../src/fabric/IFabricWallet';
import { BlockchainRuntimeExplorerProvider } from '../../src/explorer/runtimeOpsExplorer';
import { BlockchainTreeItem } from '../../src/explorer/model/BlockchainTreeItem';
import * as myExtension from '../../src/extension';
Expand Down Expand Up @@ -66,10 +64,8 @@ describe('createNewIdentityCommand', () => {
const testFabricWallet: FabricWallet = new FabricWallet('/some/path');
walletExistsStub = mySandBox.stub(testFabricWallet, 'exists').resolves(false);
importIdentityStub = mySandBox.stub(testFabricWallet, 'importIdentity').resolves();
runtimeManager.gatewayWallet = testFabricWallet as IFabricWallet;

mockFabricRuntimeConnection = sinon.createStubInstance(FabricRuntimeConnection);
mockFabricRuntimeConnection.connect.resolves();
mockFabricRuntimeConnection.getAllCertificateAuthorityNames.returns(['ca.name']);
mockFabricRuntimeConnection.getAllOrdererNames.returns([]);
mockFabricRuntimeConnection.getAllPeerNames.returns([]);
Expand All @@ -78,8 +74,7 @@ describe('createNewIdentityCommand', () => {
certificate: 'this is a certificate',
privateKey: 'this is a private Key'
});
mockFabricRuntimeConnection.disconnect.resolves();
mySandBox.stub(FabricConnectionFactory, 'createFabricRuntimeConnection').returns(mockFabricRuntimeConnection);
mockFabricRuntimeConnection.getWallet.withArgs('ca.name').resolves(testFabricWallet);
mySandBox.stub(runtimeManager, 'getConnection').resolves((mockFabricRuntimeConnection as any));

executeCommandStub = mySandBox.stub(vscode.commands, 'executeCommand');
Expand All @@ -106,11 +101,9 @@ describe('createNewIdentityCommand', () => {
caChoseStub.should.have.been.calledOnce;
inputBoxStub.should.have.been.calledOnce;
walletExistsStub.should.have.been.calledOnceWithExactly(identityName);
mockFabricRuntimeConnection.connect.should.have.been.calledOnce;
mockFabricRuntimeConnection.register.should.have.been.calledOnce;
mockFabricRuntimeConnection.enroll.should.have.been.calledOnce;
importIdentityStub.should.have.been.calledOnce;
mockFabricRuntimeConnection.disconnect.should.have.been.calledOnce;

executeCommandStub.getCall(3).should.have.been.calledWith(ExtensionCommands.REFRESH_WALLETS);

Expand All @@ -127,11 +120,9 @@ describe('createNewIdentityCommand', () => {
inputBoxStub.should.have.been.calledOnce;
caChoseStub.should.not.have.been.called;
walletExistsStub.should.have.been.calledOnceWithExactly(identityName);
mockFabricRuntimeConnection.connect.should.have.been.calledOnce;
mockFabricRuntimeConnection.register.should.have.been.calledOnce;
mockFabricRuntimeConnection.enroll.should.have.been.calledOnce;
importIdentityStub.should.have.been.calledOnce;
mockFabricRuntimeConnection.disconnect.should.have.been.calledOnce;

executeCommandStub.getCall(3).should.have.been.calledWith(ExtensionCommands.REFRESH_WALLETS);

Expand All @@ -153,11 +144,9 @@ describe('createNewIdentityCommand', () => {
inputBoxStub.should.have.been.calledTwice;
walletExistsStub.getCall(0).should.have.been.calledWith(identityName);
walletExistsStub.getCall(1).should.have.been.calledWith(secondIdentityName);
mockFabricRuntimeConnection.connect.should.have.been.calledTwice;
mockFabricRuntimeConnection.register.should.have.been.calledTwice;
mockFabricRuntimeConnection.enroll.should.have.been.calledTwice;
importIdentityStub.should.have.been.calledTwice;
mockFabricRuntimeConnection.disconnect.should.have.been.calledTwice;

executeCommandStub.getCall(3).should.have.been.calledWith(ExtensionCommands.REFRESH_WALLETS);
executeCommandStub.getCall(5).should.have.been.calledWith(ExtensionCommands.REFRESH_WALLETS);
Expand All @@ -180,11 +169,9 @@ describe('createNewIdentityCommand', () => {
caChoseStub.should.have.been.calledOnce;
inputBoxStub.should.have.been.calledOnce;
walletExistsStub.should.have.been.calledOnceWithExactly(identityName);
mockFabricRuntimeConnection.connect.should.have.been.calledOnce;
mockFabricRuntimeConnection.register.should.have.been.calledOnce;
mockFabricRuntimeConnection.enroll.should.have.been.calledOnce;
importIdentityStub.should.have.been.calledOnce;
mockFabricRuntimeConnection.disconnect.should.have.been.calledOnce;

executeCommandStub.getCall(3).should.have.been.calledWith(ExtensionCommands.START_FABRIC);
executeCommandStub.getCall(4).should.have.been.calledWith(ExtensionCommands.REFRESH_WALLETS);
Expand Down Expand Up @@ -233,7 +220,6 @@ describe('createNewIdentityCommand', () => {
caChoseStub.should.have.been.calledOnce;
inputBoxStub.should.have.been.calledOnce;
walletExistsStub.should.have.been.calledOnceWithExactly(identityName);
mockFabricRuntimeConnection.connect.should.not.have.been.called;
logSpy.should.have.been.calledTwice;
logSpy.getCall(0).should.have.been.calledWith(LogType.INFO, undefined, 'createNewIdentity');
logSpy.getCall(1).should.have.been.calledWith(LogType.ERROR, `An identity called ${identityName} already exists in the runtime wallet`, `An identity called ${identityName} already exists in the runtime wallet`);
Expand All @@ -247,7 +233,6 @@ describe('createNewIdentityCommand', () => {

inputBoxStub.should.have.been.calledOnce;
walletExistsStub.should.have.been.calledOnceWithExactly(identityName);
mockFabricRuntimeConnection.connect.should.have.been.calledOnce;
mockFabricRuntimeConnection.register.should.have.been.calledOnce;
importIdentityStub.should.not.have.been.called;
logSpy.should.have.been.calledTwice;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import { VSCodeBlockchainOutputAdapter } from '../../src/logging/VSCodeBlockchai
import { Reporter } from '../../src/util/Reporter';
import { ExtensionUtil } from '../../src/util/ExtensionUtil';
import { LogType } from '../../src/logging/OutputAdapter';
import * as yeoman from 'yeoman-environment';
import { ExtensionCommands } from '../../ExtensionCommands';
import { YeomanUtil } from '../../src/util/YeomanUtil';

chai.use(sinonChai);

Expand Down Expand Up @@ -199,12 +199,7 @@ describe('CreateSmartContractProjectCommand', () => {
}

it('should show error message if we fail to create a smart contract', async () => {
const mockEnv: any = {
registerStub: sinon.stub().throws(new Error('such error')),
lookup: sinon.stub().yields(new Error('such error')),
run: sinon.stub().yields(new Error('such error'))
};
mySandBox.stub(yeoman, 'createEnv').returns(mockEnv);
mySandBox.stub(YeomanUtil, 'run').rejects(new Error('such error'));
quickPickStub.onCall(0).resolves({ label: 'JavaScript', type: LanguageType.CONTRACT });
quickPickStub.onCall(1).resolves(UserInputUtil.OPEN_IN_NEW_WINDOW);

Expand Down
49 changes: 33 additions & 16 deletions client/test/commands/exportConnectionProfileCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import { NodesTreeItem } from '../../src/explorer/runtimeOps/NodesTreeItem';
import { LogType } from '../../src/logging/OutputAdapter';
import { PeerTreeItem } from '../../src/explorer/runtimeOps/PeerTreeItem';
import { ExtensionCommands } from '../../ExtensionCommands';
import * as fs from 'fs-extra';
import { FabricRuntimeUtil } from '../../src/fabric/FabricRuntimeUtil';

// tslint:disable no-unused-expression
describe('exportConnectionProfileCommand', () => {
Expand All @@ -43,6 +45,8 @@ describe('exportConnectionProfileCommand', () => {
let nodes: NodesTreeItem;
let peerTreeItem: PeerTreeItem;
let logSpy: sinon.SinonSpy;
let ensureDirStub: sinon.SinonStub;
let copyFileStub: sinon.SinonStub;

before(async () => {
await TestUtil.setupTests();
Expand All @@ -59,7 +63,7 @@ describe('exportConnectionProfileCommand', () => {
sandbox = sinon.createSandbox();
await ExtensionUtil.activateExtension();
await connectionRegistry.clear();
await runtimeManager.add();
await runtimeManager.initialize();
runtime = runtimeManager.getRuntime();
sandbox.stub(runtime, 'isRunning').resolves(true);
const provider: BlockchainRuntimeExplorerProvider = myExtension.getBlockchainRuntimeExplorerProvider();
Expand All @@ -73,23 +77,36 @@ describe('exportConnectionProfileCommand', () => {
};
workspaceFolderStub = sandbox.stub(UserInputUtil, 'getWorkspaceFolders').returns([workspaceFolder]);
logSpy = sandbox.spy(VSCodeBlockchainOutputAdapter.instance(), 'log');
sandbox.stub(FabricRuntimeManager.instance(), 'getGatewayRegistryEntries').resolves([
{
name: 'local_fabric',
managedRuntime: true,
connectionProfilePath: '/tmp/doggo.json'
}
]);
ensureDirStub = sandbox.stub(fs, 'ensureDir');
copyFileStub = sandbox.stub(fs, 'copyFile');
});

afterEach(async () => {
sandbox.restore();
});

it('should export the connection profile by right clicking on a peer in the runtime ops tree', async () => {
const exportStub: sinon.SinonStub = sandbox.stub(runtime, 'exportConnectionProfile').resolves();
await vscode.commands.executeCommand(ExtensionCommands.EXPORT_CONNECTION_PROFILE, peerTreeItem);
exportStub.should.have.been.called.calledOnceWith(VSCodeBlockchainOutputAdapter.instance(), workspaceFolder.uri.fsPath);
const targetDirectory: string = path.resolve(workspaceFolder.uri.fsPath, FabricRuntimeUtil.LOCAL_FABRIC);
const targetFile: string = path.resolve(targetDirectory, 'connection.json');
ensureDirStub.should.have.been.called.calledOnceWithExactly(targetDirectory);
copyFileStub.should.have.been.called.calledOnceWithExactly('/tmp/doggo.json', targetFile);
logSpy.should.have.been.calledWith(LogType.SUCCESS, 'Successfully exported connection profile to ' + path.join(workspaceFolder.uri.fsPath, runtime.getName()));
});

it('should export the connection profile when called from the command palette', async () => {
const exportStub: sinon.SinonStub = sandbox.stub(runtime, 'exportConnectionProfile').resolves();
await vscode.commands.executeCommand(ExtensionCommands.EXPORT_CONNECTION_PROFILE);
exportStub.should.have.been.called.calledOnceWith(VSCodeBlockchainOutputAdapter.instance(), workspaceFolder.uri.fsPath);
const targetDirectory: string = path.resolve(workspaceFolder.uri.fsPath, FabricRuntimeUtil.LOCAL_FABRIC);
const targetFile: string = path.resolve(targetDirectory, 'connection.json');
ensureDirStub.should.have.been.called.calledOnceWithExactly(targetDirectory);
copyFileStub.should.have.been.called.calledOnceWithExactly('/tmp/doggo.json', targetFile);
logSpy.should.have.been.calledWith(LogType.SUCCESS, 'Successfully exported connection profile to ' + path.join(workspaceFolder.uri.fsPath, runtime.getName()));
});

Expand All @@ -107,26 +124,28 @@ describe('exportConnectionProfileCommand', () => {
workspaceFolderStub.returns([workspaceFolder, workspaceFolder2]);
sandbox.stub(UserInputUtil, 'showWorkspaceQuickPickBox').resolves({ label: workspaceFolder2.name, data: workspaceFolder2 });

const exportStub: sinon.SinonStub = sandbox.stub(runtime, 'exportConnectionProfile').resolves();
await vscode.commands.executeCommand(ExtensionCommands.EXPORT_CONNECTION_PROFILE);
exportStub.should.have.been.called.calledOnceWith(VSCodeBlockchainOutputAdapter.instance(), workspaceFolder2.uri.fsPath);
const targetDirectory: string = path.resolve(workspaceFolder2.uri.fsPath, FabricRuntimeUtil.LOCAL_FABRIC);
const targetFile: string = path.resolve(targetDirectory, 'connection.json');
ensureDirStub.should.have.been.called.calledOnceWithExactly(targetDirectory);
copyFileStub.should.have.been.called.calledOnceWithExactly('/tmp/doggo.json', targetFile);
logSpy.should.have.been.calledWith(LogType.SUCCESS, 'Successfully exported connection profile to ' + path.join(workspaceFolder2.uri.fsPath, runtime.getName()));
});

it('should handle undefined workspace folders', async () => {

workspaceFolderStub.returns(null);
const exportStub: sinon.SinonStub = sandbox.stub(runtime, 'exportConnectionProfile').resolves();
await vscode.commands.executeCommand(ExtensionCommands.EXPORT_CONNECTION_PROFILE);
exportStub.should.not.have.been.called;
ensureDirStub.should.not.have.been.called;
copyFileStub.should.not.have.been.called;
logSpy.should.have.been.calledWith(LogType.ERROR, 'A folder must be open to export connection profile to');
});

it('should handle empty workspace folders', async () => {
workspaceFolderStub.returns([]);
const exportStub: sinon.SinonStub = sandbox.stub(runtime, 'exportConnectionProfile').resolves();
await vscode.commands.executeCommand(ExtensionCommands.EXPORT_CONNECTION_PROFILE);
exportStub.should.not.have.been.called;
ensureDirStub.should.not.have.been.called;
copyFileStub.should.not.have.been.called;
logSpy.should.have.been.calledWith(LogType.ERROR, 'A folder must be open to export connection profile to');
});

Expand All @@ -144,16 +163,14 @@ describe('exportConnectionProfileCommand', () => {
workspaceFolderStub.returns([workspaceFolder, workspaceFolder2]);
sandbox.stub(UserInputUtil, 'showWorkspaceQuickPickBox').resolves();

const exportStub: sinon.SinonStub = sandbox.stub(runtime, 'exportConnectionProfile').resolves();
await vscode.commands.executeCommand(ExtensionCommands.EXPORT_CONNECTION_PROFILE);
exportStub.should.not.have.been.called;
ensureDirStub.should.not.have.been.called;
copyFileStub.should.not.have.been.called;
});

it('should not print the successSpy if there was an error', async () => {

const exportStub: sinon.SinonStub = sandbox.stub(runtime, 'exportConnectionProfile').rejects({ message: 'something bad happened' });
ensureDirStub.rejects(new Error('such error'));
await vscode.commands.executeCommand(ExtensionCommands.EXPORT_CONNECTION_PROFILE);
exportStub.should.have.been.called.calledOnceWith(VSCodeBlockchainOutputAdapter.instance(), workspaceFolder.uri.fsPath);

logSpy.should.have.been.calledWith(LogType.ERROR, 'Issue exporting connection profile, see output channel for more information');
logSpy.should.not.have.been.calledWith(LogType.SUCCESS);
Expand Down
10 changes: 6 additions & 4 deletions client/test/commands/openFabricRuntimeTerminal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('openFabricRuntimeTerminal', () => {
let sandbox: sinon.SinonSandbox;
const connectionRegistry: FabricGatewayRegistry = FabricGatewayRegistry.instance();
const runtimeManager: FabricRuntimeManager = FabricRuntimeManager.instance();
let runtime: FabricRuntime;
let mockRuntime: sinon.SinonStubbedInstance<FabricRuntime>;
let mockTerminal: any;
let createTerminalStub: sinon.SinonStub;
let nodes: NodesTreeItem;
Expand All @@ -56,9 +56,11 @@ describe('openFabricRuntimeTerminal', () => {
sandbox = sinon.createSandbox();
await ExtensionUtil.activateExtension();
await connectionRegistry.clear();
await runtimeManager.add();
runtime = runtimeManager.getRuntime();
sandbox.stub(runtime, 'isRunning').resolves(true);
await runtimeManager.initialize();
mockRuntime = sinon.createStubInstance(FabricRuntime);
sandbox.stub(runtimeManager, 'getRuntime').returns(mockRuntime);
mockRuntime.getName.returns(FabricRuntimeUtil.LOCAL_FABRIC);
mockRuntime.getPeerContainerName.resolves('fabricvscodelocalfabric_peer0.org1.example.com');
const provider: BlockchainRuntimeExplorerProvider = myExtension.getBlockchainRuntimeExplorerProvider();
const allChildren: BlockchainTreeItem[] = await provider.getChildren();
nodes = allChildren[2] as NodesTreeItem;
Expand Down
2 changes: 1 addition & 1 deletion client/test/commands/restartFabricRuntime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('restartFabricRuntime', () => {
sandbox = sinon.createSandbox();
await ExtensionUtil.activateExtension();
await connectionRegistry.clear();
await runtimeManager.add();
await runtimeManager.initialize();
runtime = runtimeManager.getRuntime();
});

Expand Down
Loading

0 comments on commit f4bc2b5

Please sign in to comment.