Skip to content

Commit

Permalink
[FAB-16527] Use Verdaccio in FV/scenario tests
Browse files Browse the repository at this point in the history
The current test process packages the npm modules
into tarballs, copies them into the chaincode, and
then adds dependencies to the tarballs.

A local npm repository (Verdaccio) is much more
effective at testing the code as if it was the
"real world". The npm modules can be published to
Verdaccio, and the chaincode build process can
pull them from Verdaccio.

Signed-off-by: Simon Stone <sstone1@uk.ibm.com>
Change-Id: I7c789867bca311afb18e3b52c9ece11954c768ad
  • Loading branch information
Simon Stone committed Sep 10, 2019
1 parent 2c39852 commit 6946d91
Show file tree
Hide file tree
Showing 16 changed files with 3,026 additions and 267 deletions.
5 changes: 4 additions & 1 deletion build/test/scenario.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const path = require('path');
const getTLSArgs = require('./utils').getTLSArgs;

const Ajv = require('ajv');
const ip = require('ip');

const execFile = util.promisify(require('child_process').execFile);
const CHANNEL_NAME = 'mychannel';
Expand Down Expand Up @@ -105,9 +106,10 @@ gulp.task('st-install_chaincode', () => {
// the test folder containing scenario is mapped to /opt/gopath/src/github.com/chaincode
'/opt/gopath/src/github.com/chaincode/scenario'
);

const npmrc = path.join(__dirname, '..', '..', 'test', 'scenario', '.npmrc');
return gulp.src('*.js', {read: false})
.pipe(shell([
`echo "registry=http://${ip.address()}:4873" > ${npmrc}`,
util.format(
'docker exec %s %s',
'org1_cli',
Expand All @@ -118,6 +120,7 @@ gulp.task('st-install_chaincode', () => {
'org2_cli',
peerInstall
),
`rm -f ${npmrc}`
]));
});

Expand Down
58 changes: 7 additions & 51 deletions build/test/setup.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/*
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
*/

const gulp = require('gulp');
const shell = require('gulp-shell');
const fs = require('fs-extra');
const path = require('path');
const util = require('util');
const merge = require('merge-stream');
const delay = require('delay');
const log = require('fancy-log');
const getTLSArgs = require('./utils').getTLSArgs;
Expand All @@ -14,7 +19,6 @@ const dockerCfgPath = '/etc/hyperledger/config';
const dockerCfgTxPath = '/etc/hyperledger/configtx';
const channelName = 'mychannel';
const tls = require('./utils').tls;
const version = require(path.join(__dirname, '../../package.json')).version;

const arch = process.arch;
const release = require(path.join(__dirname, '../../package.json')).testFabricVersion;
Expand Down Expand Up @@ -145,55 +149,7 @@ gulp.task('generate-config', gulp.series('cli-ready', () => {
}));
}));

gulp.task('local-publish', gulp.series('generate-config', () => {
return gulp.src('*.js', {read: false})
.pipe(shell([
util.format('npm pack %s', path.join(__dirname, '../../fabric-contract-api')),
util.format('npm pack %s', path.join(__dirname, '../../fabric-shim')),
util.format('npm pack %s', path.join(__dirname, '../../fabric-shim-crypto'))
]));
}));

gulp.task('copy-published-to-chaincode', gulp.series('local-publish', () => {

const streams = [];

// Copy to fv tests
const fvPath = path.join(__dirname, '../../test/fv');

let dirContents = fs.readdirSync(fvPath);
dirContents = dirContents.filter(c => c.match(/.*.js/) && c !== 'utils.js');

const chaincodeNames = dirContents.map(n => n.replace('.js', ''));
for (const c in chaincodeNames) {
const name = chaincodeNames[c];
const directory = `test/fv/${name}`;

fs.ensureDirSync(path.join(fvPath, name));

const stream = gulp.src([
path.join(process.cwd(), `fabric-contract-api-${version}.tgz`),
path.join(process.cwd(), `fabric-shim-${version}.tgz`),
path.join(process.cwd(), `fabric-shim-crypto-${version}.tgz`),
])
.pipe(gulp.dest(directory));
streams.push(stream);
}

// copy to scenario test
const stream = gulp.src([
path.join(process.cwd(), `fabric-contract-api-${version}.tgz`),
path.join(process.cwd(), `fabric-shim-${version}.tgz`),
path.join(process.cwd(), `fabric-shim-crypto-${version}.tgz`),
])
.pipe(gulp.dest(path.join(__dirname, '../../test/scenario')));

streams.push(stream);

return merge(...streams);
}));

gulp.task('docker-setup', gulp.series('copy-published-to-chaincode', () => {
gulp.task('docker-setup', gulp.series('generate-config', () => {
const composeFile = tls ? 'docker-compose-tls.yaml' : 'docker-compose.yaml';

console.log(`################\nUsing docker compose file: ${composeFile}\n################`); // eslint-disable-line no-console
Expand Down
86 changes: 86 additions & 0 deletions build/verdaccio/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#
# This is the config file used for the docker images.
# It allows all users to do anything, so don't use it on production systems.
#
# Do not configure host and port under `listen` in this file
# as it will be ignored when using docker.
# see https://verdaccio.org/docs/en/docker#docker-and-custom-port-configuration
#
# Look here for more config file examples:
# https://github.com/verdaccio/verdaccio/tree/master/conf
#

# path to a directory with all packages
storage: /verdaccio/storage/data
# path to a directory with plugins to include
plugins: /verdaccio/plugins

web:
# WebUI is enabled as default, if you want disable it, just uncomment this line
#enable: false
title: Verdaccio
# comment out to disable gravatar support
# gravatar: false
# by default packages are ordercer ascendant (asc|desc)
# sort_packages: asc

auth:
htpasswd:
file: /verdaccio/storage/htpasswd
# Maximum amount of users allowed to register, defaults to "+infinity".
# You can set this to -1 to disable registration.
# max_users: 1000

security:
api:
jwt:
sign:
expiresIn: 60d
notBefore: 1
web:
sign:
expiresIn: 7d
notBefore: 1

# a list of other known repositories we can talk to
uplinks:
npmjs:
url: https://registry.npmjs.org/

packages:
'@*/*':
# scoped packages
access: $all
publish: $all
unpublish: $all
proxy: npmjs

'fabric-*':
access: $all
publish: $all
unpublish: $all

'**':
# allow all users (including non-authenticated users) to read and
# publish all packages
#
# you can specify usernames/groupnames (depending on your auth plugin)
# and three keywords: "$all", "$anonymous", "$authenticated"
access: $all

# allow all known users to publish/publish packages
# (anyone can register by default, remember?)
publish: $all
unpublish: $all

# if package is not available locally, proxy requests to 'npmjs' registry
proxy: npmjs

middlewares:
audit:
enabled: true

# log settings
logs:
- { type: stdout, format: pretty, level: http }
#- {type: file, path: verdaccio.log, level: info}
39 changes: 39 additions & 0 deletions build/verdaccio/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
*/

'use strict';

const gulp = require('gulp');
const shell = require('gulp-shell');
const util = require('util');

gulp.task('verdaccio-start', () => {
const commands = [
'docker rm -f verdaccio || true',
util.format('docker run -d -p 4873:4873 -v %s/config.yaml:/verdaccio/conf/config.yaml --name verdaccio verdaccio/verdaccio', __dirname),
'sleep 5', // verdaccio takes a while to start
'npm config delete //localhost:4873/:_authToken',
'npm-cli-login -u testuser -p testpass -e testuser@example.org -r http://localhost:4873',
'sleep 5' // avoid "jwt not active" error
];
const npm_packages = ['fabric-contract-api', 'fabric-shim', 'fabric-shim-crypto'];
for (const npm_package of npm_packages) {
const packageJSON = require(`../../${npm_package}/package.json`);
const npm_tag = packageJSON.tag;
commands.push(util.format('npm publish --registry http://localhost:4873 %s --tag %s', npm_package, npm_tag));
commands.push(util.format('npm view --registry http://localhost:4873 %s', npm_package));
}
return gulp.src('*.js', {read: false})
.pipe(shell(commands));
});

gulp.task('verdaccio-stop', () => {
const commands = [
util.format('docker rm -f verdaccio || true')
];
return gulp.src('*.js', {read: false})
.pipe(shell(commands));
});
Loading

0 comments on commit 6946d91

Please sign in to comment.