Skip to content

Commit

Permalink
Add code coverage for unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Atsushi Neki <nekiaiken@gmail.com>
  • Loading branch information
nekia committed Dec 10, 2020
1 parent affddef commit 2835570
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .mocharc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"watch-extensions": "ts",
"recursive": true,
"spec": ["./app/test/**/*.test.ts"],
"timeout": 5000,
"timeout": 10000,
"extension": ["ts"]
}
10 changes: 9 additions & 1 deletion .nycrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,13 @@
"sourceMap": true,
"instrument": true,
"temp-dir": "app/test/.nyc_output",
"report-dir": "app/test/coverage"
"report-dir": "app/test/coverage",
"all": true,
"include": ["app"],
"exclude": ["**/e2e-test", "app/test"],
"check-coverage": false,
"branches": 80,
"lines": 80,
"functions": 80,
"statements": 80
}
4 changes: 2 additions & 2 deletions app/platform/fabric/gateway/FabricGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ export class FabricGateway {
return targets;
}

async sendRecvDiscoveryResult() {
async sendDiscoveryRequest() {
try {
await this.ds.send({
asLocalhost: this.asLocalhost,
Expand Down Expand Up @@ -456,7 +456,7 @@ export class FabricGateway {
}

if (this.ds && this.dsTargets.length) {
const result = await this.sendRecvDiscoveryResult();
const result = await this.sendDiscoveryRequest();
return result;
}

Expand Down
230 changes: 194 additions & 36 deletions app/test/FabricGateway.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,212 @@
*SPDX-License-Identifier: Apache-2.0
*/

/* tslint:disable:no-unused-expression */
import { expect, use } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import sinonChai from 'sinon-chai';
import { expect } from './expect';
import { FabricConfig } from '../platform/fabric/FabricConfig';

use(chaiAsPromised);
use(sinonChai);

// export const expect = chai.expect
import proxyquire from 'proxyquire';
import sinon from 'sinon';
import { assert } from 'console';
// import { expect } from './expect'
import { FabricGateway } from '../platform/fabric/gateway/FabricGateway';

describe('setupDiscoveryRequest', () => {
it('should return without error', async () => {
const stubSign = sinon.stub();
const { FabricGateway } = proxyquire
.noCallThru()
.load('../platform/fabric/gateway/FabricGateway', {
'fabric-common': {
DiscoveryService: function() {
// DiscoveryService (this.ds)
const stubSign = sinon.stub();
const stubGetDiscoveryResults = sinon.stub();
const stubClose = sinon.stub();

// Discoverer
const stubConnect = sinon.stub();

// logger
const stubError = sinon.stub();
const stubInfo = sinon.stub();

// Client
const stubSetTlsClientCertAndKey = sinon.stub();
const stubNewEndpoint = sinon.stub();

function getFabricGatewayInstance() {
const { FabricGateway } = proxyquire
.noCallThru()
.load('../platform/fabric/gateway/FabricGateway', {
'fabric-common': {
DiscoveryService: function() {
return {
build: sinon.stub(),
sign: stubSign,
send: sinon.stub(),
getDiscoveryResults: stubGetDiscoveryResults,
close: stubClose
};
},
Client: function() {
return {
setTlsClientCertAndKey: stubSetTlsClientCertAndKey,
newEndpoint: stubNewEndpoint
};
},
Discoverer: function() {
return {
connect: stubConnect
};
}
},
'../../../common/helper': {
helper: {
getLogger: function() {
return {
build: sinon.stub(),
sign: stubSign
error: stubError,
info: stubInfo
};
}
},
'../../../common/helper': {
helper: {
getLogger: function() {}
}
}
});

const config = new FabricConfig();
config.initialize('first-network', {
name: 'My first network',
profile: './connection-profile/first-network.json'
}
});
const config = new FabricConfig();
config.initialize('first-network', {
name: 'My first network',
profile: './connection-profile/first-network.json'
});
sinon.stub(config, 'getPeerTlsCACertsPem');

const gw = new FabricGateway(config);
gw.clientTlsIdentity = {
credentials: {
certificate: 'abc',
privateKey: 'def'
},
mspId: 'org1',
type: 'X.509'
};

const stubGetNetwork = sinon.stub(gw.gateway, 'getNetwork');
const stubGetChannel = sinon.stub();
stubGetChannel.returns({});
stubGetNetwork.returns(Promise.resolve({ getChannel: stubGetChannel }));

return gw;
}

const gw = new FabricGateway(config);
const stubGetNetwork = sinon.stub(gw.gateway, 'getNetwork');
const stubGetChannel = sinon.stub();
stubGetChannel.returns({});
stubGetNetwork.returns(Promise.resolve({ getChannel: stubGetChannel }));
function resetAllStubs() {
// DiscoveryService (this.ds)
stubSign.reset();
stubGetDiscoveryResults.reset();
stubClose.reset();

// Discoverer
stubConnect.reset();

// logger
stubError.reset();
stubInfo.reset();

// Client
stubSetTlsClientCertAndKey.reset();
stubNewEndpoint.reset();
}

describe('setupDiscoveryRequest', () => {
let gw: FabricGateway;

before(() => {
gw = getFabricGatewayInstance();
});

beforeEach(() => {
resetAllStubs();
});

it('should return without error', async () => {
await gw.setupDiscoveryRequest('testChannel');
expect(stubSign.calledOnce).to.be.equal(true);
});

it('should throw error if fail to sign', async () => {
stubSign.throws();
await gw.setupDiscoveryRequest('testChannel');
expect(stubError.calledOnce).to.be.equal(true);
});
});

describe('getDiscoveryServiceTarget', () => {
let gw: FabricGateway;

before(() => {
gw = getFabricGatewayInstance();
});

beforeEach(() => {
resetAllStubs();
});

it('should return without error', async () => {
const targetArray = await gw.getDiscoveryServiceTarget();
expect(stubSetTlsClientCertAndKey.calledOnceWith('abc', 'def')).to.be.equal(
true
);
expect(stubNewEndpoint.calledOnce).to.be.equal(true);
expect(stubConnect.calledOnce).to.be.equal(true);
expect(targetArray.length).to.be.equal(1);
});

it('should return without error when not assigned client TLS config', async () => {
gw.clientTlsIdentity = undefined;
const targetArray = await gw.getDiscoveryServiceTarget();
expect(stubSetTlsClientCertAndKey.calledOnceWith()).to.be.equal(true);
expect(stubNewEndpoint.calledOnce).to.be.equal(true);
expect(stubConnect.calledOnce).to.be.equal(true);
expect(targetArray.length).to.be.equal(1);
});
});

describe('sendDiscoveryRequest', () => {
let gw: FabricGateway;

before(async () => {
gw = getFabricGatewayInstance();
await gw.setupDiscoveryRequest('testChannel');
});

beforeEach(() => {
resetAllStubs();
});

it('should return without error', async () => {
stubGetDiscoveryResults.returns(Promise.resolve());
await gw.sendDiscoveryRequest();
expect(stubError.called).be.equal(false);
});

it('should throw error when failed to call getDiscoveryResults()', async () => {
stubGetDiscoveryResults.throws();
await gw.sendDiscoveryRequest();
expect(stubError.called).be.equal(true);
expect(stubClose.calledOnce).be.equal(true);
});
});

describe('getDiscoveryResult', () => {
let gw: FabricGateway;
let stubSetupDiscoveryRequest: sinon.SinonStub;

before(async () => {
gw = getFabricGatewayInstance();
await gw.setupDiscoveryRequest('testChannel');
stubSetupDiscoveryRequest = sinon.stub(gw, 'setupDiscoveryRequest');
});

beforeEach(() => {
resetAllStubs();
stubSetupDiscoveryRequest.reset();
});

it('should return without error', async () => {
await gw.getDiscoveryResult('testChannel');
expect(stubSetupDiscoveryRequest.calledOnce).be.equal(false);
});

it('should return without error if discover service has not been allocated yet', async () => {
gw.ds = null;
await gw.getDiscoveryResult('testChannel');
expect(stubSetupDiscoveryRequest.calledOnce).be.equal(true);
});
});
12 changes: 12 additions & 0 deletions app/test/expect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
*SPDX-License-Identifier: Apache-2.0
*/

import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import sinonChai from 'sinon-chai';

chai.use(chaiAsPromised);
chai.use(sinonChai);

export const expect = chai.expect;
14 changes: 4 additions & 10 deletions ci/azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,22 @@ jobs:
- template: install_deps.yml
- checkout: self
- script: |
npm config set prefix ~/npm && npm install -g mocha
npm install chai && npm install
./build_docker_image.sh -d
docker-compose -f docker-compose-testdb.yaml up -d
cd app/test && npm install
npm config set prefix ~/npm
npm install
npm run test
cd ../../client && npm install
cd ./client && npm install
echo "--------> npm tests with code coverage"
npm run test:ci -- -u --coverage && npm run build
displayName: Run Tests With Coverage Report
- script: |
cd client
wget https://raw.github.com/eriwen/lcov-to-cobertura-xml/master/lcov_cobertura/lcov_cobertura.py
python lcov_cobertura.py ./coverage/lcov.info
mv $(System.DefaultWorkingDirectory)/app/test/*.js $(System.DefaultWorkingDirectory)/client
displayName: Create Cobertura Report
- script: npx cobertura-merge -o output.xml package1=$(System.DefaultWorkingDirectory)/app/test/coverage/cobertura-coverage.xml package2=$(System.DefaultWorkingDirectory)/client/coverage.xml
displayName: Merge Cobertura Reports
- task: PublishCodeCoverageResults@1
inputs:
codeCoverageTool: 'Cobertura'
summaryFileLocation: $(System.DefaultWorkingDirectory)/output.xml
summaryFileLocation: $(System.DefaultWorkingDirectory)/client/coverage.xml
pathToSources: $(System.DefaultWorkingDirectory)/client

- job: SanityChecks_v1_4
Expand Down
4 changes: 1 addition & 3 deletions main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,17 @@ function print_help() {
function do_install() {
VERBOSE=${VERBOSE:+-ddd}
npm install $VERBOSE
(cd app/test && npm install $VERBOSE)
(cd client && npm install $VERBOSE && npm run build)
}

function do_test() {
(cd app/test && npm run test)
(npm run test)
(cd client && npm run test:ci -- -u --coverage)
}

function do_clean() {
rm -rf node_modules
rm -rf client/node_modules client/build client/coverage
rm -rf app/test/node_modules
}

# Get subcommand
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@
"e2e-gui-test-v1-run": "cd client/e2e-test; ./gui-e2e-test-start.sh -1",
"e2e-gui-test-v2-run": "cd client/e2e-test; ./gui-e2e-test-start.sh -2",
"e2e-gui-test-v1": "run-s e2e-test-build-src e2e-gui-test-v1-run",
"e2e-gui-test-v2": "run-s e2e-test-build-src e2e-gui-test-v2-run"
"e2e-gui-test-v2": "run-s e2e-test-build-src e2e-gui-test-v2-run",
"test": "nyc mocha",
"test:watch": "mocha -w --reporter min"
}
}

0 comments on commit 2835570

Please sign in to comment.