Skip to content

Commit 6946d91

Browse files
author
Simon Stone
committed
[FAB-16527] Use Verdaccio in FV/scenario tests
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
1 parent 2c39852 commit 6946d91

16 files changed

Lines changed: 3026 additions & 267 deletions

File tree

build/test/scenario.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const path = require('path');
1515
const getTLSArgs = require('./utils').getTLSArgs;
1616

1717
const Ajv = require('ajv');
18+
const ip = require('ip');
1819

1920
const execFile = util.promisify(require('child_process').execFile);
2021
const CHANNEL_NAME = 'mychannel';
@@ -105,9 +106,10 @@ gulp.task('st-install_chaincode', () => {
105106
// the test folder containing scenario is mapped to /opt/gopath/src/github.com/chaincode
106107
'/opt/gopath/src/github.com/chaincode/scenario'
107108
);
108-
109+
const npmrc = path.join(__dirname, '..', '..', 'test', 'scenario', '.npmrc');
109110
return gulp.src('*.js', {read: false})
110111
.pipe(shell([
112+
`echo "registry=http://${ip.address()}:4873" > ${npmrc}`,
111113
util.format(
112114
'docker exec %s %s',
113115
'org1_cli',
@@ -118,6 +120,7 @@ gulp.task('st-install_chaincode', () => {
118120
'org2_cli',
119121
peerInstall
120122
),
123+
`rm -f ${npmrc}`
121124
]));
122125
});
123126

build/test/setup.js

Lines changed: 7 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
/*
2+
# Copyright IBM Corp. All Rights Reserved.
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
*/
6+
17
const gulp = require('gulp');
28
const shell = require('gulp-shell');
39
const fs = require('fs-extra');
410
const path = require('path');
511
const util = require('util');
6-
const merge = require('merge-stream');
712
const delay = require('delay');
813
const log = require('fancy-log');
914
const getTLSArgs = require('./utils').getTLSArgs;
@@ -14,7 +19,6 @@ const dockerCfgPath = '/etc/hyperledger/config';
1419
const dockerCfgTxPath = '/etc/hyperledger/configtx';
1520
const channelName = 'mychannel';
1621
const tls = require('./utils').tls;
17-
const version = require(path.join(__dirname, '../../package.json')).version;
1822

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

148-
gulp.task('local-publish', gulp.series('generate-config', () => {
149-
return gulp.src('*.js', {read: false})
150-
.pipe(shell([
151-
util.format('npm pack %s', path.join(__dirname, '../../fabric-contract-api')),
152-
util.format('npm pack %s', path.join(__dirname, '../../fabric-shim')),
153-
util.format('npm pack %s', path.join(__dirname, '../../fabric-shim-crypto'))
154-
]));
155-
}));
156-
157-
gulp.task('copy-published-to-chaincode', gulp.series('local-publish', () => {
158-
159-
const streams = [];
160-
161-
// Copy to fv tests
162-
const fvPath = path.join(__dirname, '../../test/fv');
163-
164-
let dirContents = fs.readdirSync(fvPath);
165-
dirContents = dirContents.filter(c => c.match(/.*.js/) && c !== 'utils.js');
166-
167-
const chaincodeNames = dirContents.map(n => n.replace('.js', ''));
168-
for (const c in chaincodeNames) {
169-
const name = chaincodeNames[c];
170-
const directory = `test/fv/${name}`;
171-
172-
fs.ensureDirSync(path.join(fvPath, name));
173-
174-
const stream = gulp.src([
175-
path.join(process.cwd(), `fabric-contract-api-${version}.tgz`),
176-
path.join(process.cwd(), `fabric-shim-${version}.tgz`),
177-
path.join(process.cwd(), `fabric-shim-crypto-${version}.tgz`),
178-
])
179-
.pipe(gulp.dest(directory));
180-
streams.push(stream);
181-
}
182-
183-
// copy to scenario test
184-
const stream = gulp.src([
185-
path.join(process.cwd(), `fabric-contract-api-${version}.tgz`),
186-
path.join(process.cwd(), `fabric-shim-${version}.tgz`),
187-
path.join(process.cwd(), `fabric-shim-crypto-${version}.tgz`),
188-
])
189-
.pipe(gulp.dest(path.join(__dirname, '../../test/scenario')));
190-
191-
streams.push(stream);
192-
193-
return merge(...streams);
194-
}));
195-
196-
gulp.task('docker-setup', gulp.series('copy-published-to-chaincode', () => {
152+
gulp.task('docker-setup', gulp.series('generate-config', () => {
197153
const composeFile = tls ? 'docker-compose-tls.yaml' : 'docker-compose.yaml';
198154

199155
console.log(`################\nUsing docker compose file: ${composeFile}\n################`); // eslint-disable-line no-console

build/verdaccio/config.yaml

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#
2+
# This is the config file used for the docker images.
3+
# It allows all users to do anything, so don't use it on production systems.
4+
#
5+
# Do not configure host and port under `listen` in this file
6+
# as it will be ignored when using docker.
7+
# see https://verdaccio.org/docs/en/docker#docker-and-custom-port-configuration
8+
#
9+
# Look here for more config file examples:
10+
# https://github.com/verdaccio/verdaccio/tree/master/conf
11+
#
12+
13+
# path to a directory with all packages
14+
storage: /verdaccio/storage/data
15+
# path to a directory with plugins to include
16+
plugins: /verdaccio/plugins
17+
18+
web:
19+
# WebUI is enabled as default, if you want disable it, just uncomment this line
20+
#enable: false
21+
title: Verdaccio
22+
# comment out to disable gravatar support
23+
# gravatar: false
24+
# by default packages are ordercer ascendant (asc|desc)
25+
# sort_packages: asc
26+
27+
auth:
28+
htpasswd:
29+
file: /verdaccio/storage/htpasswd
30+
# Maximum amount of users allowed to register, defaults to "+infinity".
31+
# You can set this to -1 to disable registration.
32+
# max_users: 1000
33+
34+
security:
35+
api:
36+
jwt:
37+
sign:
38+
expiresIn: 60d
39+
notBefore: 1
40+
web:
41+
sign:
42+
expiresIn: 7d
43+
notBefore: 1
44+
45+
# a list of other known repositories we can talk to
46+
uplinks:
47+
npmjs:
48+
url: https://registry.npmjs.org/
49+
50+
packages:
51+
'@*/*':
52+
# scoped packages
53+
access: $all
54+
publish: $all
55+
unpublish: $all
56+
proxy: npmjs
57+
58+
'fabric-*':
59+
access: $all
60+
publish: $all
61+
unpublish: $all
62+
63+
'**':
64+
# allow all users (including non-authenticated users) to read and
65+
# publish all packages
66+
#
67+
# you can specify usernames/groupnames (depending on your auth plugin)
68+
# and three keywords: "$all", "$anonymous", "$authenticated"
69+
access: $all
70+
71+
# allow all known users to publish/publish packages
72+
# (anyone can register by default, remember?)
73+
publish: $all
74+
unpublish: $all
75+
76+
# if package is not available locally, proxy requests to 'npmjs' registry
77+
proxy: npmjs
78+
79+
middlewares:
80+
audit:
81+
enabled: true
82+
83+
# log settings
84+
logs:
85+
- { type: stdout, format: pretty, level: http }
86+
#- {type: file, path: verdaccio.log, level: info}

build/verdaccio/index.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
# Copyright IBM Corp. All Rights Reserved.
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
'use strict';
8+
9+
const gulp = require('gulp');
10+
const shell = require('gulp-shell');
11+
const util = require('util');
12+
13+
gulp.task('verdaccio-start', () => {
14+
const commands = [
15+
'docker rm -f verdaccio || true',
16+
util.format('docker run -d -p 4873:4873 -v %s/config.yaml:/verdaccio/conf/config.yaml --name verdaccio verdaccio/verdaccio', __dirname),
17+
'sleep 5', // verdaccio takes a while to start
18+
'npm config delete //localhost:4873/:_authToken',
19+
'npm-cli-login -u testuser -p testpass -e testuser@example.org -r http://localhost:4873',
20+
'sleep 5' // avoid "jwt not active" error
21+
];
22+
const npm_packages = ['fabric-contract-api', 'fabric-shim', 'fabric-shim-crypto'];
23+
for (const npm_package of npm_packages) {
24+
const packageJSON = require(`../../${npm_package}/package.json`);
25+
const npm_tag = packageJSON.tag;
26+
commands.push(util.format('npm publish --registry http://localhost:4873 %s --tag %s', npm_package, npm_tag));
27+
commands.push(util.format('npm view --registry http://localhost:4873 %s', npm_package));
28+
}
29+
return gulp.src('*.js', {read: false})
30+
.pipe(shell(commands));
31+
});
32+
33+
gulp.task('verdaccio-stop', () => {
34+
const commands = [
35+
util.format('docker rm -f verdaccio || true')
36+
];
37+
return gulp.src('*.js', {read: false})
38+
.pipe(shell(commands));
39+
});

0 commit comments

Comments
 (0)