Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FABCN-410] Use new lifecycle for fv/e2e tests #158

Merged
merged 3 commits into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 89 additions & 6 deletions test/e2e/scenario.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const util = require('util');
const fs = require('fs-extra');
const path = require('path');

const { shell: runcmds , getTLSArgs } = require('toolchain');
const { shell: runcmds , getTLSArgs, getPeerAddresses } = require('toolchain');
const Ajv = require('ajv');
const ip = require('ip');

Expand Down Expand Up @@ -66,33 +66,116 @@ const queryFunctions = async () => {


const instantiateChaincode = async () => {
const endorsementPolicy = '"OR (\'Org1MSP.member\', \'Org2MSP.member\')"';
const queryInstalled = util.format(
'peer lifecycle chaincode queryinstalled --output json'
);
const sequence = 1;

const approveChaincode = util.format(
'peer lifecycle chaincode approveformyorg -o %s %s -C %s -n %s -v %s --init-required --package-id %s --sequence %d --signature-policy %s',
'orderer.example.com:7050',
getTLSArgs(),
CHANNEL_NAME,
'mysmartcontract',
'v0',
'%s', // To be filled in for each org
sequence,
endorsementPolicy
);

const outputs = await runcmds([
util.format(
'docker exec %s %s',
'org1_cli',
queryInstalled
),
util.format(
'docker exec %s %s',
'org2_cli',
queryInstalled
),
]);

const packageIdOrg1 = findPackageId(outputs[0], 'mysmartcontract_v0');
const packageIdOrg2 = findPackageId(outputs[1], 'mysmartcontract_v0');

// Approve the chaincode and commit
await runcmds([
util.format('docker exec org1_cli peer chaincode instantiate -o %s %s -l node -C %s -n %s -v v0 -c %s -P %s',
util.format('docker exec %s %s',
'org1_cli',
util.format(approveChaincode, packageIdOrg1)
),
util.format('docker exec %s %s',
'org2_cli',
util.format(approveChaincode, packageIdOrg2)
),
util.format('docker exec org1_cli peer lifecycle chaincode commit -o %s %s -C %s -n %s -v %s --init-required --sequence %d --signature-policy %s %s',
'orderer.example.com:7050',
getTLSArgs(),
CHANNEL_NAME,
'mysmartcontract',
'\'{"Args":["UpdateValues:setup"]}\'',
'\'OR ("Org1MSP.member")\'')
'v0',
sequence,
endorsementPolicy,
getPeerAddresses()
)
]);
await delay(3000);

// Invoke init function
await runcmds([
util.format('docker exec org1_cli peer chaincode invoke %s -C %s -n %s -c %s --isInit --waitForEvent',
getTLSArgs(),
CHANNEL_NAME,
'mysmartcontract',
'\'{"Args":["UpdateValues:setup"]}\''
)
]);
};

const findPackageId = (queryOutput, label) => {
const output = JSON.parse(queryOutput);

const cc = output.installed_chaincodes.filter((chaincode) => chaincode.label === label);
if (cc.length !== 1) {
throw new Error('Failed to find installed chaincode');
}

return cc[0].package_id;
};

const installChaincode = async () => {
const peerInstall = util.format(
'peer chaincode install -l node -n %s -v v0 -p %s',
const packageChaincode = util.format(
'peer lifecycle chaincode package %s -l node --label %s_v0 -p %s',
'/tmp/scenario.tar.gz',
'mysmartcontract',
// the test folder containing scenario is mapped to /opt/gopath/src/github.com/chaincode
'/opt/gopath/src/github.com/chaincode/scenario'
);
const peerInstall = util.format(
'peer lifecycle chaincode install %s',
'/tmp/scenario.tar.gz'
);
const npmrc = path.join(__dirname, '..', '..', 'test', 'chaincodes','scenario', '.npmrc');

await runcmds([
`echo "registry=http://${ip.address()}:4873" > ${npmrc}`,
util.format(
'docker exec %s %s',
'org1_cli',
packageChaincode
),
util.format(
'docker exec %s %s',
'org1_cli',
peerInstall
),
util.format(
'docker exec %s %s',
'org2_cli',
packageChaincode
),
util.format(
'docker exec %s %s',
'org2_cli',
Expand Down
52 changes: 46 additions & 6 deletions test/fv/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,25 @@ const getTLSArgs = () => {

return '';
};
const getPeerAddresses = () => {
if (tls) {
return '--peerAddresses peer0.org1.example.com:7051 --tlsRootCertFile ' + org1CA +
' --peerAddresses peer0.org2.example.com:8051 --tlsRootCertFile ' + org2CA;
} else {
return '--peerAddresses peer0.org1.example.com:7051' +
' --peerAddresses peer0.org2.example.com:8051';
}
};
const findPackageId = (queryOutput, label) => {
const output = JSON.parse(queryOutput);

const cc = output.installed_chaincodes.filter((chaincode) => chaincode.label === label);
if (cc.length !== 1) {
throw new Error('Failed to find installed chaincode');
}

return cc[0].package_id;
};

// Increase the timeouts on zLinux!
const arch = require('os').arch();
Expand All @@ -33,20 +51,42 @@ async function install(ccName) {
try {
fs.writeFileSync(npmrc, `registry=http://${ip.address()}:4873`);
const folderName = '/opt/gopath/src/github.com/chaincode/' + ccName;
const cmd = `docker exec %s peer chaincode install -l node -n ${ccName} -v v0 -p ${folderName} --connTimeout 60s`;
await exec(util.format(cmd, 'org1_cli'));
await exec(util.format(cmd, 'org2_cli'));
const packageCmd = `docker exec %s peer lifecycle chaincode package -l node /tmp/${ccName}.tar.gz -p ${folderName} --label ${ccName}_v0`;
await exec(util.format(packageCmd, 'org1_cli'));
await exec(util.format(packageCmd, 'org2_cli'));

const installCmd = `docker exec %s peer lifecycle chaincode install /tmp/${ccName}.tar.gz --connTimeout 60s`;
await exec(util.format(installCmd, 'org1_cli'));
await exec(util.format(installCmd, 'org2_cli'));
} finally {
fs.unlinkSync(npmrc);
}
}

async function instantiate(ccName, func, args) {
const cmd = `docker exec org1_cli peer chaincode instantiate ${getTLSArgs()} -o orderer.example.com:7050 -l node -C mychannel -n ${ccName} -v v0 -c '${printArgs(func, args)}' -P 'OR ("Org1MSP.member")'`;
console.log(cmd)
const res = await exec(cmd);
const orgs = ['org1', 'org2'];
const endorsementPolicy = '\'OR ("Org1MSP.member")\'';

for (const org of orgs) {
const queryInstalledCmd = `docker exec ${org}_cli peer lifecycle chaincode queryinstalled --output json`;
const res = await exec(queryInstalledCmd);
const pkgId = findPackageId(res.stdout.toString(), ccName + '_v0');
const approveCmd = `docker exec ${org}_cli peer lifecycle chaincode approveformyorg ${getTLSArgs()} -o orderer.example.com:7050` +
` -C mychannel -n ${ccName} -v v0 --init-required --sequence 1 --package-id ${pkgId} --signature-policy ${endorsementPolicy}`;

await exec(approveCmd);
}

const commitCmd = `docker exec org1_cli peer lifecycle chaincode commit ${getTLSArgs()} -o orderer.example.com:7050` +
` -C mychannel -n ${ccName} -v v0 --init-required --sequence 1 --signature-policy ${endorsementPolicy} ${getPeerAddresses()}`;

console.log(commitCmd);
await exec(commitCmd);
await new Promise(resolve => setTimeout(resolve, 5000));

const initCmd = `docker exec org1_cli peer chaincode invoke ${getTLSArgs()} -o orderer.example.com:7050 -C mychannel -n ${ccName} --isInit -c '${printArgs(func, args)}' --waitForEvent`;
const res = await exec(initCmd);

return res;
}

Expand Down
6 changes: 4 additions & 2 deletions tools/toolchain/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const {shell} = require('./shell/cmd');
const getTLSArgs = require('./utils').getTLSArgs;
const {getTLSArgs, getPeerAddresses} = require('./utils');

module.exports.shell = shell;

module.exports.getTLSArgs = getTLSArgs;
module.exports.getTLSArgs = getTLSArgs;
module.exports.getPeerAddresses = getPeerAddresses;
8 changes: 4 additions & 4 deletions tools/toolchain/network/crypto-material/configtx.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,20 @@ Capabilities:
# Channel capabilities apply to both the orderers and the peers and must be
# supported by both. Set the value of the capability to true to require it.
Channel: &ChannelCapabilities
V1_1: true
V2_0: true

# Orderer capabilities apply only to the orderers, and may be safely
# manipulated without concern for upgrading peers. Set the value of the
# capability to true to require it.
Orderer: &OrdererCapabilities
V1_1: true
V2_0: true

# Application capabilities apply only to the peer network, and may be
# safely manipulated without concern for upgrading orderers. Set the value
# of the capability to true to require it.
Application: &ApplicationCapabilities
V1_2: true
V1_1: false
V2_0: true

################################################################################
#
# CHANNEL
Expand Down
12 changes: 12 additions & 0 deletions tools/toolchain/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const ordererCA = '/etc/hyperledger/config/crypto-config/ordererOrganizations/example.com/tlsca/tlsca.example.com-cert.pem';
const org1CA = '/etc/hyperledger/config/crypto-config/peerOrganizations/org1.example.com/tlsca/tlsca.org1.example.com-cert.pem';
const org2CA = '/etc/hyperledger/config/crypto-config/peerOrganizations/org2.example.com/tlsca/tlsca.org2.example.com-cert.pem';

const tls = process.env.TLS && process.env.TLS.toLowerCase() === 'true' ? true : false;

Expand All @@ -11,3 +13,13 @@ exports.getTLSArgs = () => {

return '';
};

exports.getPeerAddresses = () => {
if (tls) {
return '--peerAddresses peer0.org1.example.com:7051 --tlsRootCertFile ' + org1CA +
' --peerAddresses peer0.org2.example.com:8051 --tlsRootCertFile ' + org2CA;
} else {
return '--peerAddresses peer0.org1.example.com:7051' +
' --peerAddresses peer0.org2.example.com:8051';
}
};