Skip to content

Commit

Permalink
[FAB-11059] establish mocha tests
Browse files Browse the repository at this point in the history
This change:
- adds a new mocha test entrance point in the main gulp file
- changes the gulp 'test' items to ensure that the original tests are run, and then the mocha tests are run
- adds the first mocha test for fabric-ca-client
- removes the tape test that the above replaces
- add the ability to run the mocha unit test at the package level
- adds code coverage enforcement on the mocha unit tests
- adds the required packages to dev-dependancies
- adds an npm ignore file to prevent publishing of test folder in fabric-ca-client

Change-Id: I556520c982eeef25270eee518b5f35b0da81ee06
Signed-off-by: Nick Lincoln <[email protected]>
  • Loading branch information
nklincoln committed Jul 17, 2018
1 parent 68e7724 commit 30ad986
Show file tree
Hide file tree
Showing 13 changed files with 998 additions and 679 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"env": {
"es6": true,
"node": true
"node": true,
"mocha": true
},
"extends": "eslint:recommended",
"parserOptions": {
Expand Down
7 changes: 4 additions & 3 deletions build/tasks/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
#
# SPDX-License-Identifier: Apache-2.0
*/
var gulp = require('gulp');
var eslint = require('gulp-eslint');
const gulp = require('gulp');
const eslint = require('gulp-eslint');

gulp.task('lint', function () {
gulp.task('lint', () => {
return gulp.src([
'**/*.js',
'fabric-client/**/*.js',
'fabric-ca-client/lib/*.js',
'!fabric-ca-client/coverage/**',
'!test/typescript/*.js',
'!node_modules/**',
'!fabric-client/node_modules/**',
Expand Down
198 changes: 108 additions & 90 deletions build/tasks/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@
*/
'use strict';

var gulp = require('gulp');
var tape = require('gulp-tape');
var tapColorize = require('tap-colorize');
var addsrc = require('gulp-add-src');

var fs = require('fs-extra');
var path = require('path');
var os = require('os');
var util = require('util');
var shell = require('gulp-shell');
var testConstants = require('../../test/unit/constants.js');
const addsrc = require('gulp-add-src');
const gulp = require('gulp');
const mocha = require('gulp-mocha');
const tape = require('gulp-tape');

const tapColorize = require('tap-colorize');

const fs = require('fs-extra');
const path = require('path');
const os = require('os');
const util = require('util');
const shell = require('gulp-shell');
const testConstants = require('../../test/unit/constants.js');

// by default for running the tests print debug to a file
var debugPath = path.join(testConstants.tempdir, 'test-log/debug.log');
const debugPath = path.join(testConstants.tempdir, 'test-log/debug.log');
process.env.HFC_LOGGING = util.format('{"debug":"%s"}', escapeWindowsPath(debugPath));

function escapeWindowsPath(p) {
Expand All @@ -30,12 +32,12 @@ console.log('\n####################################################');
console.log(util.format('# debug log: %s', debugPath));
console.log('####################################################\n');

let arch = process.arch;
const arch = process.arch;
const release = require(path.join(__dirname, '../../fabric-client/package.json')).version;
const thirparty_release = require(path.join(__dirname, '../../fabric-client/package.json')).thirdparty;
let dockerImageTag = '';
let thirdpartyImageTag = '';
let docker_arch = '';
let release = require(path.join(__dirname, '../../fabric-client/package.json')).version;
let thirparty_release = require(path.join(__dirname, '../../fabric-client/package.json')).thirdparty;

// this is a release build, need to build the proper docker image tag
// to run the tests against the corresponding fabric released docker images
Expand All @@ -59,7 +61,7 @@ if (!/-snapshot/.test(release)) {
process.env.DOCKER_IMG_TAG = dockerImageTag;
process.env.THIRDPARTY_IMG_TAG = thirdpartyImageTag;

gulp.task('pre-test', function() {
gulp.task('pre-test', () => {
return gulp.src([
'fabric-client/lib/**/*.js',
'fabric-ca-client/lib/FabricCAClientImpl.js',
Expand All @@ -69,7 +71,7 @@ gulp.task('pre-test', function() {
]);
});

gulp.task('clean-up', function() {
gulp.task('clean-up', () => {
// some tests create temporary files or directories
// they are all created in the same temp folder
fs.removeSync(testConstants.tempdir);
Expand Down Expand Up @@ -113,80 +115,96 @@ gulp.task('test-headless', shell.task(
'./node_modules/nyc/bin/nyc.js gulp run-test-headless'
));

gulp.task('run-test', ['clean-up', 'lint', 'pre-test', 'compile', 'docker-ready', 'ca'], function() {
// use individual tests to control the sequence they get executed
// first run the ca-tests that tests all the member registration
// and enrollment scenarios (good and bad calls). Then the rest
// of the tests will re-use the same key value store that has
// saved the user certificates so they can interact with the
// network
return gulp.src(shouldRunPKCS11Tests([
'test/unit/config.js', // needs to be first
'test/unit/**/*.js',
'!test/unit/constants.js',
'!test/unit/util.js',
'!test/unit/logger.js',
// channel: mychannel, chaincode: e2enodecc:v0
'test/integration/nodechaincode/e2e.js',
// channel: mychannel, chaincode: end2endnodesdk:v0/v1
'test/integration/e2e.js',
'test/integration/query.js',
'test/integration/fabric-ca-affiliation-service-tests.js',
'test/integration/fabric-ca-identity-service-tests.js',
'test/integration/fabric-ca-certificate-service-tests.js',
'test/integration/fabric-ca-services-tests.js',
'test/integration/client.js',
'test/integration/orderer-channel-tests.js',
'test/integration/cloudant-fabricca-tests.js',
'test/integration/couchdb-fabricca-tests.js',
'test/integration/fileKeyValueStore-fabricca-tests.js',
'test/integration/install.js',
'test/integration/events.js',
'test/integration/channel-event-hub.js',
// channel: mychannel, chaincode: end2endnodesdk:v3
'test/integration/upgrade.js',
'test/integration/get-config.js',
// channel: mychanneltx, chaincode: end2endnodesdk:v0
'test/integration/create-configtx-channel.js',
'test/integration/e2e/join-channel.js',
'test/integration/instantiate.js',
'test/integration/e2e/invoke-transaction.js',
'test/integration/e2e/query.js',
'test/integration/invoke.js',
'test/integration/network-config.js',
'test/integration/only-admin.js',
'test/integration/discovery.js',
'test/integration/grpc.js',
'test/integration/perf/orderer.js',
'test/integration/perf/peer.js'
]))
.pipe(addsrc.append(
'test/unit/logger.js' // put this to the last so the debugging levels are not mixed up
))
.pipe(tape({
reporter: tapColorize()
}));
});
gulp.task('run-test', ['run-full'],
() => {
return gulp.src(['./fabric-ca-client/test/**/*.js', './fabric-client/test/**/*.js'], { read: false })
.pipe(mocha({ reporter: 'list', exit: true }));
}
);

gulp.task('run-test-headless', ['clean-up', 'lint', 'pre-test', 'ca'], function() {
// this is needed to avoid a problem in tape-promise with adding
// too many listeners
// to the "unhandledRejection" event
process.setMaxListeners(0);

return gulp.src(shouldRunPKCS11Tests([
'test/unit/**/*.js',
'!test/unit/constants.js',
'!test/unit/util.js',
'!test/unit/logger.js'
]))
.pipe(addsrc.append(
'test/unit/logger.js' // put this to the last so the debugging levels are not mixed up
))
.pipe(tape({
reporter: tapColorize()
}));
});
gulp.task('run-test-headless', ['run-headless'],
() => {
return gulp.src(['./fabric-ca-client/test/**/*.js', './fabric-client/test/**/*.js'], { read: false })
.pipe(mocha({ reporter: 'list', exit: true }));
}
);

gulp.task('run-full', ['clean-up', 'lint', 'pre-test', 'compile', 'docker-ready', 'ca'],
() => {
// use individual tests to control the sequence they get executed
// first run the ca-tests that tests all the member registration
// and enrollment scenarios (good and bad calls). Then the rest
// of the tests will re-use the same key value store that has
// saved the user certificates so they can interact with the
// network
return gulp.src(shouldRunPKCS11Tests([
'test/unit/config.js', // needs to be first
'test/unit/**/*.js',
'!test/unit/constants.js',
'!test/unit/util.js',
'!test/unit/logger.js',
// channel: mychannel, chaincode: e2enodecc:v0
'test/integration/nodechaincode/e2e.js',
// channel: mychannel, chaincode: end2endnodesdk:v0/v1
'test/integration/e2e.js',
'test/integration/query.js',
'test/integration/fabric-ca-affiliation-service-tests.js',
'test/integration/fabric-ca-identity-service-tests.js',
'test/integration/fabric-ca-certificate-service-tests.js',
'test/integration/fabric-ca-services-tests.js',
'test/integration/client.js',
'test/integration/orderer-channel-tests.js',
'test/integration/cloudant-fabricca-tests.js',
'test/integration/couchdb-fabricca-tests.js',
'test/integration/fileKeyValueStore-fabricca-tests.js',
'test/integration/install.js',
'test/integration/events.js',
'test/integration/channel-event-hub.js',
// channel: mychannel, chaincode: end2endnodesdk:v3
'test/integration/upgrade.js',
'test/integration/get-config.js',
// channel: mychanneltx, chaincode: end2endnodesdk:v0
'test/integration/create-configtx-channel.js',
'test/integration/e2e/join-channel.js',
'test/integration/instantiate.js',
'test/integration/e2e/invoke-transaction.js',
'test/integration/e2e/query.js',
'test/integration/invoke.js',
'test/integration/network-config.js',
'test/integration/only-admin.js',
'test/integration/discovery.js',
'test/integration/grpc.js',
'test/integration/perf/orderer.js',
'test/integration/perf/peer.js'
]))
.pipe(addsrc.append(
'test/unit/logger.js' // put this to the last so the debugging levels are not mixed up
))
.pipe(tape({
reporter: tapColorize()
}));
});

gulp.task('run-headless', ['clean-up', 'lint', 'pre-test', 'ca'],
() => {
// this is needed to avoid a problem in tape-promise with adding
// too many listeners
// to the "unhandledRejection" event
process.setMaxListeners(0);

return gulp.src(shouldRunPKCS11Tests([
'test/unit/**/*.js',
'!test/unit/constants.js',
'!test/unit/util.js',
'!test/unit/logger.js'
]))
.pipe(addsrc.append(
'test/unit/logger.js' // put this to the last so the debugging levels are not mixed up
))
.pipe(tape({
reporter: tapColorize()
}));
});

// currently only the x64 CI jobs are configured with SoftHSM
// disable the pkcs11.js test for s390 or other jobs
Expand Down
26 changes: 26 additions & 0 deletions fabric-ca-client/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# OS generated files #
.DS_Store

# Node Files #
/node_modules
npm-debug.log

# Coverage #
/coverage

# Test Files #
/test
26 changes: 6 additions & 20 deletions fabric-ca-client/lib/AffiliationService.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ class AffiliationService {
}
checkRegistrar(registrar);

let signingIdentity = registrar.getSigningIdentity();
if (!signingIdentity) {
throw new Error('Can not get signingIdentity from registrar');
}
const signingIdentity = registrar.getSigningIdentity();

let url = 'affiliations';
if (req.force === true) {
Expand Down Expand Up @@ -91,10 +88,7 @@ class AffiliationService {
}
checkRegistrar(registrar);

let signingIdentity = registrar.getSigningIdentity();
if (!signingIdentity) {
throw new Error('Can not get signingIdentity from registrar');
}
const signingIdentity = registrar.getSigningIdentity();

const url = 'affiliations/' + affiliation;
return this.client.get(url, signingIdentity);
Expand All @@ -110,10 +104,7 @@ class AffiliationService {
getAll(registrar) {
checkRegistrar(registrar);

let signingIdentity = registrar.getSigningIdentity();
if (!signingIdentity) {
throw new Error('Can not get signingIdentity from registrar');
}
const signingIdentity = registrar.getSigningIdentity();

return this.client.get('affiliations', signingIdentity);
}
Expand All @@ -134,12 +125,10 @@ class AffiliationService {
if (!req.name || typeof req.name !== 'string') {
throw new Error('Missing required argument "req.name", or argument "req.name" is not a valid string');
}

checkRegistrar(registrar);

let signingIdentity = registrar.getSigningIdentity();
if (!signingIdentity) {
throw new Error('Can not get signingIdentity from registrar');
}
const signingIdentity = registrar.getSigningIdentity();

let url = 'affiliations/' + req.name;
if (req.force === true) {
Expand Down Expand Up @@ -170,10 +159,7 @@ class AffiliationService {
}
checkRegistrar(registrar);

let signingIdentity = registrar.getSigningIdentity();
if (!signingIdentity) {
throw new Error('Can not get signingIdentity from registrar');
}
const signingIdentity = registrar.getSigningIdentity();

let url = 'affiliations/' + affiliation;
if (req.force === true) {
Expand Down
13 changes: 11 additions & 2 deletions fabric-ca-client/lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,21 @@
'use strict';

function checkRegistrar(registrar) {
if (typeof registrar === 'undefined' || registrar === null) {

if (!registrar) {
throw new Error('Missing required argument "registrar"');
}

if (!registrar.constructor || registrar.constructor.name !== 'User') {
throw new Error('Argument "registrar" must be an instance of the class "User" but is of type: ' + registrar.constructor.name);
}

if (typeof registrar.getSigningIdentity !== 'function') {
throw new Error('Argument "registrar" must be an instance of the class "User", but is found to be missing a method "getSigningIdentity()"');
throw new Error('Argument "registrar" is found to be missing a method "getSigningIdentity()"');
}

if (!registrar.getSigningIdentity()) {
throw new Error('Can not get signingIdentity from registrar');
}
}

Expand Down
Loading

0 comments on commit 30ad986

Please sign in to comment.