Skip to content

Commit

Permalink
[FABCN-351] CORE_PEER_ADDRESS issue fixed (#252)
Browse files Browse the repository at this point in the history
Signed-off-by: Kestutis Gudynas <44440041+kemi04@users.noreply.github.com>
  • Loading branch information
kemi04 authored Dec 15, 2020
1 parent 6b2b671 commit 845d689
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
1 change: 1 addition & 0 deletions libraries/fabric-shim/lib/chaincode.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class Shim {
delete optsCpy['module-path'];

const url = parsePeerUrl(opts['peer.address']);

if (isTLS()) {
logger.debug('TLS enabled');
optsCpy.pem = fs.readFileSync(process.env.CORE_PEER_TLS_ROOTCERT_FILE).toString();
Expand Down
7 changes: 6 additions & 1 deletion libraries/fabric-shim/lib/cmds/startCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,13 @@ exports.getArgs = function (yargs) {
argv['chaincode-id-name'] = argv.chaincodeIdName;
argv['module-path'] = argv.modulePath;

// eslint-disable-next-line eqeqeq
if (argv.CORE_PEER_ADDRESS != null) {
argv['peer.address'] = argv.CORE_PEER_ADDRESS;
}

required.forEach((argName) => {
if (!argv.hasOwnProperty(argName)) { // eslint-disable-line no-prototype-builtins
if (!argv.hasOwnProperty(argName) || typeof(argv[argName]) === 'undefined') { // eslint-disable-line no-prototype-builtins
throw new Error('Missing required argument ' + argName);
}
});
Expand Down
56 changes: 52 additions & 4 deletions libraries/fabric-shim/test/unit/cmds/chaincode.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,21 @@ describe('chaincode cmd', () => {
};

const mockYargsParser = sinon.stub().returns({
chaincodeIdName: 'Jeremy',
'CORE_PEER_ADDRESS': 'address',
modulePath: '/home/andy/my/super/contract'
});

const yp = chaincodeStartCommand.__get__('YargsParser');
chaincodeStartCommand.__set__('YargsParser', mockYargsParser);

process.argv = ['node', 'test.js', '--chaincode-id-name', 'mycc'];
process.argv = ['node', 'test.js', '--CORE_PEER_ADDRESS', 'address'];

expect(() => {
chaincodeStartCommand.getArgs(myYargs);
}).to.throw(/Missing required argument peer.address/);
}).to.throw(/Missing required argument chaincode-id-name/);

sinon.assert.calledOnce(mockYargsParser);
sinon.assert.calledWith(mockYargsParser, ['--chaincode-id-name', 'mycc'], {
sinon.assert.calledWith(mockYargsParser, ['--CORE_PEER_ADDRESS', 'address'], {
default: {
'grpc.max_send_message_length': -1,
'grpc.max_receive_message_length': -1,
Expand All @@ -172,5 +172,53 @@ describe('chaincode cmd', () => {

chaincodeStartCommand.__set__('YargsParser', yp);
});

it ('should use yargs parser on process.argv, CORE_PEER_ADDRESS should override peer.address', () => {
const myYargs = {
argv: {
$0: 'index.js',
someArg: 'hello world'
},
};

const mockYargsParser = sinon.stub().returns({
chaincodeIdName: 'Jeremy',
modulePath: '/home/andy/my/super/contract',
'peer.address': 'some addr',
'CORE_PEER_ADDRESS': 'localhost:7052'
});

const yp = chaincodeStartCommand.__get__('YargsParser');
chaincodeStartCommand.__set__('YargsParser', mockYargsParser);

process.argv = ['node', 'test.js', '--peer.address', 'localhost:7051', '--chaincode-id-name', 'mycc',
'--CORE_PEER_ADDRESS', 'localhost:7052'];

const args = chaincodeStartCommand.getArgs(myYargs);

sinon.assert.calledOnce(mockYargsParser);
sinon.assert.calledWith(mockYargsParser, ['--peer.address', 'localhost:7051', '--chaincode-id-name', 'mycc',
'--CORE_PEER_ADDRESS', 'localhost:7052'], {
default: {
'grpc.max_send_message_length': -1,
'grpc.max_receive_message_length': -1,
'grpc.keepalive_time_ms': 110000,
'grpc.http2.min_time_between_pings_ms': 110000,
'grpc.keepalive_timeout_ms': 20000,
'grpc.http2.max_pings_without_data': 0,
'grpc.keepalive_permit_without_calls': 1,
'module-path': process.cwd()
},
configuration: {
'dot-notation': false
},
envPrefix: 'CORE'
});
expect(args['chaincode-id-name']).to.deep.equal('Jeremy');
expect(args['peer.address']).to.deep.equal('localhost:7052');
expect(args['module-path']).to.deep.equal('/home/andy/my/super/contract');

chaincodeStartCommand.__set__('YargsParser', yp);
});
});
});

0 comments on commit 845d689

Please sign in to comment.