Skip to content

Commit

Permalink
[FABN-1397] add ts-cucumber test suite
Browse files Browse the repository at this point in the history
New typescript based sceanrio test suite that:
- uses CLI to create/join channels
- replaces javascript 'network feature'
- replaces javascript 'gateway feature'

Change-Id: If4cd589afc0c81bef13d3e4a330e8c8d8159a076
Signed-off-by: [email protected] <[email protected]>
  • Loading branch information
nklincoln committed Oct 14, 2019
1 parent 3d908fd commit 424dae7
Show file tree
Hide file tree
Showing 51 changed files with 4,598 additions and 60 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@ sonar-project.properties
#test files
test/temp/**
!test/temp/.noignore
# - crypto material
test/fixtures/crypto-material/crypto-config/
test/fixtures/crypto-material/channel-config/
test/fixtures/crypto-material/config-base/twoorgs.genesis.block
test/fixtures/fabricca/enroll*.*
test/fixtures/fabricca/test*.*
# - ts:scenario crypto material
test/ts-fixtures/crypto-material/crypto-config/
test/ts-fixtures/crypto-material/channel-config/
test/ts-fixtures/crypto-material/config-base/twoorgs.genesis.block
# - generated typescript files
test/typescript/**/*.js
test/typescript/**/*.js.map

Expand Down
1 change: 1 addition & 0 deletions build/tasks/certs.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ gulp.task('get-crypto-binaries-s390', shell.task(
// - shell command to run the required test file scripts
gulp.task('generate-test-certs', shell.task(
'./test/fixtures/crypto-material/generateAll.sh ' + binariesPath + '/bin;' +
'./test/ts-fixtures/crypto-material/generateAll.sh ' + binariesPath + '/bin;' +
'./test/fixtures/fabricca/generateCSR.sh;')
);

Expand Down
29 changes: 15 additions & 14 deletions build/tasks/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,12 @@ gulp.task('clean-up', () => {
});

gulp.task('docker-clean', shell.task([
// stop and remove chaincode docker instances
// stop and remove docker containers
'docker kill $(docker ps -aq)',
'docker rm $(docker ps -aq) -f',

// remove chaincode images so that they get rebuilt during test
'docker rmi $(docker images | grep "^dev-" | awk \'{print $3}\')',

// clean up all the containers created by docker-compose
'docker-compose -f test/fixtures/docker-compose/docker-compose-tls-level-db.yaml -p node down',
'docker-compose -f test/fixtures/docker-compose/docker-compose-tls.yaml -p node down',
'docker ps -a'
'docker rmi $(docker images | grep "^dev-" | awk \'{print $3}\')'
], {
verbose: true, // so we can see the docker command output
ignoreErrors: true // kill, rm, and rmi may fail because the containers may have been cleaned up or not exist
Expand Down Expand Up @@ -140,8 +135,10 @@ gulp.task('test-headless', shell.task('npx gulp run-test-headless'));
// Only run Mocha unit tests
gulp.task('test-mocha', shell.task('npx nyc --check-coverage --lines 92 --functions 90 --branches 70 gulp run-test-mocha'));

// Only run scenario tests
gulp.task('test-cucumber', shell.task('npx nyc npm run test:cucumber'));
// Only run javascript scenario tests
gulp.task('test:cucumber', shell.task('npx nyc npm run test:cucumber'));
// Only run typescript scenario tests
gulp.task('test:ts-cucumber', shell.task('npx nyc npm run test:ts-cucumber'));

// Definition of Mocha (unit) test suites
gulp.task('run-test-mocha', (done) => {
Expand Down Expand Up @@ -186,10 +183,14 @@ gulp.task('mocha-fabric-protos',
// Test to run all unit tests
gulp.task('test-tape', shell.task('npx nyc gulp run-tape-unit'));

// Definition of Cucumber (scenario) test suite
gulp.task('run-test-cucumber', shell.task(
// Definition of Javascript Cucumber (scenario) test suite
gulp.task('run-test:cucumber', shell.task(
'export HFC_LOGGING=\'' + cucumber_log + '\'; npm run test:cucumber'
));
// Definition of Typescript Cucumber (scenario) test suite
gulp.task('run-test:ts-cucumber', shell.task(
'export HFC_LOGGING=\'' + cucumber_log + '\'; npm run test:ts-cucumber'
));

// Run e2e and scenario tests with code coverage
gulp.task('test-fv-scenario', shell.task('npx nyc --check-coverage --lines 92 --functions 90 --branches 70 gulp run-test-fv-scenario'));
Expand All @@ -198,17 +199,17 @@ gulp.task('test-fv-scenario', shell.task('npx nyc --check-coverage --lines 92 --
gulp.task('test-fv-only', shell.task('npx nyc --check-coverage --lines 92 --functions 90 --branches 70 gulp run-tape-e2e'));

gulp.task('run-test-fv-scenario', (done) => {
const tasks = ['run-tape-e2e', 'docker-clean', 'run-test-cucumber'];
const tasks = ['run-tape-e2e', 'docker-clean', 'run-test:cucumber', 'docker-clean', 'run-test:ts-cucumber'];
runSequence(...tasks, done);
});

gulp.task('run-test-scenario', (done) => {
const tasks = ['compile', 'run-test-cucumber'];
const tasks = ['compile', 'run-test:cucumber', 'docker-clean', 'run-test:ts-cucumber'];
runSequence(...tasks, done);
});

gulp.task('run-test-merge', (done) => {
const tasks = ['clean-up', 'docker-clean', 'pre-test', 'compile', 'run-test-cucumber'];
const tasks = ['clean-up', 'docker-clean', 'pre-test', 'compile', 'run-test:cucumber', 'docker-clean', 'run-test:ts-cucumber'];
runSequence(...tasks, done);
});

Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
"test:ca-client": "npm run coverage -- 'fabric-ca-client/test/**/*.{js,ts}'",
"test:client": "npm run coverage -- 'fabric-client/test/**/*.{js,ts}'",
"test:network": "npm run compile-src && npm run coverage -- 'fabric-network/test/**/*.{js,ts}'",
"test:protos": "npm run coverage -- 'fabric-protos/test/**/*.{js,ts}",
"test:protos": "npm run coverage -- 'fabric-protos/test/**/*.{js,ts}",
"test:cucumber": "cucumber-js ./test/scenario/features/*.feature",
"test:all": "nyc npm run unit-test:all",
"test:ts-cucumber": "cucumber-js ./test/ts-scenario/features/*.feature --require './test/ts-scenario/steps/**/*.ts' --require './test/ts-scenario/support/**/*.ts' --require-module ts-node/register --format progress-bar",
"test:ts-cucumber-tagged": "cucumber-js ./test/ts-scenario/features/*.feature --require './test/ts-scenario/steps/**/*.ts' --require './test/ts-scenario/support/**/*.ts' --require-module ts-node/register --format progress-bar --tags @deprecated",
"test:all": "nyc npm run unit-test:all",
"unit-test:all": "npm run unit-test -- 'fabric-common/test/**/*.{js,ts}' && npm run unit-test -- 'fabric-ca-client/test/**/*.{js,ts}' && npm run unit-test -- 'fabric-client/test/**/*.{js,ts}' && npm run unit-test -- 'fabric-network/test/**/*.{js,ts}'",
"unit-test": "mocha --require ts-node/register --exclude 'fabric-client/test/data/**'",
"compile": "npm run compile-src && npm run compile-test",
Expand All @@ -38,6 +40,7 @@
"@cloudant/cloudant": "^2.1.0",
"@types/chai": "^4.2.1",
"@types/chai-as-promised": "^7.1.2",
"@types/cucumber": "4.0.5",
"@types/fs-extra": "^8.0.0",
"@types/mocha": "^5.2.7",
"@types/rimraf": "^2.0.2",
Expand Down Expand Up @@ -84,6 +87,7 @@
"run-sequence": "^2.2.1",
"sinon": "6.1.3",
"sinon-chai": "^3.3.0",
"strip-ansi": "5.2.0",
"tap-colorize": "^1.2.0",
"tape": "^4.5.1",
"tape-promise": "^3.0.0",
Expand Down
22 changes: 0 additions & 22 deletions test/scenario/features/discovery.feature

This file was deleted.

22 changes: 0 additions & 22 deletions test/scenario/features/network_api.feature

This file was deleted.

10 changes: 10 additions & 0 deletions test/ts-fixtures/chaincode/node/fabcar/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* SPDX-License-Identifier: Apache-2.0
*/

'use strict';

const FabCar = require('./lib/fabcar');

module.exports.FabCar = FabCar;
module.exports.contracts = [FabCar];
155 changes: 155 additions & 0 deletions test/ts-fixtures/chaincode/node/fabcar/lib/fabcar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* SPDX-License-Identifier: Apache-2.0
*/

'use strict';

const {Contract} = require('fabric-contract-api');

class FabCar extends Contract {

async initLedger(ctx) {
console.info('============= START : Initialize Ledger ==========='); // eslint-disable-line
const cars = [
{
color: 'blue',
make: 'Toyota',
model: 'Prius',
owner: 'Tomoko',
},
{
color: 'red',
make: 'Ford',
model: 'Mustang',
owner: 'Brad',
},
{
color: 'green',
make: 'Hyundai',
model: 'Tucson',
owner: 'Jin Soo',
},
{
color: 'yellow',
make: 'Volkswagen',
model: 'Passat',
owner: 'Max',
},
{
color: 'black',
make: 'Tesla',
model: 'S',
owner: 'Adriana',
},
{
color: 'purple',
make: 'Peugeot',
model: '205',
owner: 'Michel',
},
{
color: 'white',
make: 'Chery',
model: 'S22L',
owner: 'Aarav',
},
{
color: 'violet',
make: 'Fiat',
model: 'Punto',
owner: 'Pari',
},
{
color: 'indigo',
make: 'Tata',
model: 'Nano',
owner: 'Valeria',
},
{
color: 'brown',
make: 'Holden',
model: 'Barina',
owner: 'Shotaro',
},
];

for (let i = 0; i < cars.length; i++) {
cars[i].docType = 'car';
await ctx.stub.putState('CAR' + i, Buffer.from(JSON.stringify(cars[i])));
console.info('Added <--> ', cars[i]); // eslint-disable-line
}
console.info('============= END : Initialize Ledger ==========='); // eslint-disable-line
}

async queryCar(ctx, carNumber) {
const carAsBytes = await ctx.stub.getState(carNumber); // get the car from chaincode state
if (!carAsBytes || carAsBytes.length === 0) {
throw new Error(`${carNumber} does not exist`);
}
console.log(carAsBytes.toString()); // eslint-disable-line
return carAsBytes.toString();
}

async createCar(ctx, carNumber, make, model, color, owner) {
console.info('============= START : Create Car ==========='); // eslint-disable-line

const car = {
color,
docType: 'car',
make,
model,
owner,
};

await ctx.stub.putState(carNumber, Buffer.from(JSON.stringify(car)));
console.info('============= END : Create Car ==========='); // eslint-disable-line
}

async queryAllCars(ctx) {
const startKey = 'CAR0';
const endKey = 'CAR999';

const iterator = await ctx.stub.getStateByRange(startKey, endKey);

const allResults = [];
let collect = true;
while (collect) {
const res = await iterator.next();

if (res.value && res.value.value.toString()) {

const Key = res.value.key;
let Record;
try {
Record = JSON.parse(res.value.value.toString('utf8'));
} catch (err) {
console.log(err); // eslint-disable-line
Record = res.value.value.toString('utf8');
}
allResults.push({Key, Record});
}
if (res.done) {
await iterator.close();
collect = false;
}
}
return JSON.stringify(allResults);
}

async changeCarOwner(ctx, carNumber, newOwner) {
console.info('============= START : changeCarOwner ==========='); // eslint-disable-line

const carAsBytes = await ctx.stub.getState(carNumber); // get the car from chaincode state
if (!carAsBytes || carAsBytes.length === 0) {
throw new Error(`${carNumber} does not exist`);
}
const car = JSON.parse(carAsBytes.toString());
car.owner = newOwner;

await ctx.stub.putState(carNumber, Buffer.from(JSON.stringify(car)));
console.info('============= END : changeCarOwner ==========='); // eslint-disable-line
}

}

module.exports = FabCar;
Empty file.
47 changes: 47 additions & 0 deletions test/ts-fixtures/chaincode/node/fabcar/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "fabcar",
"version": "1.0.0",
"description": "FabCar contract implemented in JavaScript",
"main": "index.js",
"engines": {
"node": ">=8",
"npm": ">=5"
},
"scripts": {
"lint": "eslint .",
"pretest": "npm run lint",
"test": "nyc mocha --recursive",
"start": "fabric-chaincode-node start"
},
"engineStrict": true,
"author": "Hyperledger",
"license": "Apache-2.0",
"dependencies": {
"fabric-contract-api": "unstable",
"fabric-shim": "unstable"
},
"devDependencies": {
"chai": "^4.1.2",
"eslint": "^4.19.1",
"mocha": "^5.2.0",
"nyc": "^12.0.2",
"sinon": "^6.0.0",
"sinon-chai": "^3.2.0"
},
"nyc": {
"exclude": [
"coverage/**",
"test/**"
],
"reporter": [
"text-summary",
"html"
],
"all": true,
"check-coverage": true,
"statements": 100,
"branches": 100,
"functions": 100,
"lines": 100
}
}
10 changes: 10 additions & 0 deletions test/ts-fixtures/chaincode/node/fabcarUpgrade/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* SPDX-License-Identifier: Apache-2.0
*/

'use strict';

const FabCar = require('./lib/fabcar');

module.exports.FabCar = FabCar;
module.exports.contracts = [FabCar];
Loading

0 comments on commit 424dae7

Please sign in to comment.