Skip to content

Commit

Permalink
[FABN-937] Test samples: TransientMap value
Browse files Browse the repository at this point in the history
Current transientMap value is of type string
They should be []byte such as Buffer

Change-Id: I16dad96b5852d8bed318a1980d1c418777e04ee8
Signed-off-by: davidliu <david-khala@hotmail.com>
  • Loading branch information
davidkhala committed Sep 26, 2018
1 parent 2357ba8 commit fbc1b16
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 163 deletions.
4 changes: 2 additions & 2 deletions test/integration/e2e/e2eUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ function instantiateChaincodeWithId(userOrg, chaincode_id, chaincode_path, versi
const data = fs.readFileSync(path.join(__dirname, caRootsPath));
const caroots = Buffer.from(data).toString();

const badTransientMap = { 'test1': 'transientValue' }; // have a different key than what the chaincode example_cc1.go expects in Init()
const transientMap = { 'test': 'transientValue' };
const badTransientMap = { 'test1': Buffer.from('transientValue') }; // have a different key than what the chaincode example_cc1.go expects in Init()
const transientMap = { 'test': Buffer.from('transientValue') };
let tlsInfo = null;
let request = null;

Expand Down
286 changes: 125 additions & 161 deletions test/integration/upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,190 +6,154 @@

'use strict';

var tape = require('tape');
var _test = require('tape-promise').default;
var test = _test(tape);
const tape = require('tape');
const _test = require('tape-promise').default;
const test = _test(tape);

var path = require('path');
var fs = require('fs');
const path = require('path');
const fs = require('fs');

var Client = require('fabric-client');
var utils = require('fabric-client/lib/utils.js');
var testUtil = require('../unit/util.js');
var e2eUtils = require('./e2e/e2eUtils.js');
var logger = utils.getLogger('upgrade-chaincode');
const Client = require('fabric-client');
const utils = require('fabric-client/lib/utils.js');
const testUtil = require('../unit/util.js');
const e2eUtils = require('./e2e/e2eUtils.js');
const logger = utils.getLogger('upgrade-chaincode');

var client, channel, e2e, ORGS;
let client, channel, e2e, ORGS;

test('\n\n **** E R R O R T E S T I N G on upgrade call', (t) => {
test('\n\n **** E R R O R T E S T I N G on upgrade call', async (t) => {
testUtil.resetDefaults();

e2e = testUtil.END2END;
Client.addConfigFile(path.join(__dirname, './e2e/config.json'));
ORGS = Client.getConfigSetting('test-network');

var caRootsPath = ORGS.orderer.tls_cacerts;
let data = fs.readFileSync(path.join(__dirname, '/test', caRootsPath));
let caroots = Buffer.from(data).toString();
const caRootsPath = ORGS.orderer.tls_cacerts;
const data = fs.readFileSync(path.join(__dirname, '/test', caRootsPath));
const caroots = Buffer.from(data).toString();

var tx_id = null;

testUtil.setupChaincodeDeploy();

var org = 'org1';
const org = 'org1';
client = new Client();
channel = client.newChannel(e2e.channel);
var orgName = ORGS[org].name;
let tlsInfo = null;

e2eUtils.tlsEnroll(org)
.then((enrollment) => {
t.pass('Successfully retrieved TLS certificate');
tlsInfo = enrollment;
client.setTlsClientCertAndKey(tlsInfo.certificate, tlsInfo.key);
return Client.newDefaultKeyValueStore({path: testUtil.storePathForOrg(orgName)});
}).then((store) => {
client.setStateStore(store);

return testUtil.getSubmitter(client, t, true /* use peer org admin */, org);
})
.then(() => {
t.pass('Successfully enrolled user \'admin\'');

channel.addOrderer(
client.newOrderer(
ORGS.orderer.url,
{
'pem': caroots,
'ssl-target-name-override': ORGS.orderer['server-hostname']
}
)
);

var targets = [];
for (let key in ORGS[org]) {
if (ORGS[org].hasOwnProperty(key)) {
if (key.indexOf('peer1') === 0) {
let data = fs.readFileSync(path.join(__dirname, '/test', ORGS[org][key]['tls_cacerts']));
let peer = client.newPeer(
ORGS[org][key].requests,
{
pem: Buffer.from(data).toString(),
'ssl-target-name-override': ORGS[org][key]['server-hostname']
}
);
targets.push(peer);
channel.addPeer(peer);
}
const orgName = ORGS[org].name;

const transientMap = {'test': Buffer.from('transientValue')};
const tlsInfo = await e2eUtils.tlsEnroll(org);
t.pass('Successfully retrieved TLS certificate');
client.setTlsClientCertAndKey(tlsInfo.certificate, tlsInfo.key);
const store = await Client.newDefaultKeyValueStore({path: testUtil.storePathForOrg(orgName)});
client.setStateStore(store);
await testUtil.getSubmitter(client, t, true /* use peer org admin */, org);
t.pass('Successfully enrolled user \'admin\'');

channel.addOrderer(
client.newOrderer(
ORGS.orderer.url,
{
'pem': caroots,
'ssl-target-name-override': ORGS.orderer['server-hostname']
}
)
);

const targets = [];
for (const key in ORGS[org]) {
if (ORGS[org].hasOwnProperty(key)) {
if (key.indexOf('peer1') === 0) {
const data = fs.readFileSync(path.join(__dirname, '/test', ORGS[org][key]['tls_cacerts']));
const peer = client.newPeer(
ORGS[org][key].requests,
{
pem: Buffer.from(data).toString(),
'ssl-target-name-override': ORGS[org][key]['server-hostname']
}
);
targets.push(peer);
channel.addPeer(peer);
}
}
}

return channel.initialize();

})
.then(() => {
t.pass('Successfully initialized channel');
tx_id = client.newTransactionID();

// send proposal to endorser
var request = {
chaincodeId : e2e.chaincodeId,
chaincodeVersion : 'v1',
fcn: 'init',
args: ['a', '500', 'b', '600'],
txId: tx_id,
transientMap: { 'test': 'transientValue' }
};

return channel.sendUpgradeProposal(request);

}).then((results) => {
testUtil.checkResults(results, 'version already exists', t);

return Promise.resolve(true);

}, (err) => {
t.fail('This should not have thrown an Error ::'+ err);
return Promise.resolve(true);
}).then(() => {
tx_id = client.newTransactionID();

// send proposal to endorser
var request = {
chaincodeId: 'dummy',
chaincodeVersion: 'v1',
fcn: 'init',
args: ['a', '500', 'b', '600'],
txId: tx_id,
transientMap: { 'test': 'transientValue' }
};

return channel.sendUpgradeProposal(request);

}).then((results) => {
testUtil.checkResults(results, 'cannot get package for chaincode', t);

return Promise.resolve(true);

}).then(() => {
tx_id = client.newTransactionID();

// send proposal to endorser
var request = {
chaincodeId: e2e.chaincodeId,
chaincodeVersion: 'v333333333',
fcn: 'init',
args: ['a', '500', 'b', '600'],
txId: tx_id,
transientMap: { 'test': 'transientValue' }
};

return channel.sendUpgradeProposal(request);

}).then((results) => {
testUtil.checkResults(results, 'cannot get package for chaincode', t);
t.end();
}).catch((err) => {
t.fail('Got an Error along the way :: '+ err);
t.end();
});
await channel.initialize();


t.pass('Successfully initialized channel');
let tx_id = client.newTransactionID();

// send proposal to endorser
let request = {
chaincodeId: e2e.chaincodeId,
chaincodeVersion: 'v1',
fcn: 'init',
args: ['a', '500', 'b', '600'],
txId: tx_id,
transientMap
};

let results = await channel.sendUpgradeProposal(request);

testUtil.checkResults(results, 'version already exists', t);


tx_id = client.newTransactionID();

// send proposal to endorser
request = {
chaincodeId: 'dummy',
chaincodeVersion: 'v1',
fcn: 'init',
args: ['a', '500', 'b', '600'],
txId: tx_id,
transientMap
};

results = await channel.sendUpgradeProposal(request);

testUtil.checkResults(results, 'cannot get package for chaincode', t);


tx_id = client.newTransactionID();

// send proposal to endorser
request = {
chaincodeId: e2e.chaincodeId,
chaincodeVersion: 'v333333333',
fcn: 'init',
args: ['a', '500', 'b', '600'],
txId: tx_id,
transientMap
};

results = await channel.sendUpgradeProposal(request);

testUtil.checkResults(results, 'cannot get package for chaincode', t);
t.end();
});

test('\n\n **** Testing re-initializing states during upgrade ****', (t) => {
test('\n\n **** Testing re-initializing states during upgrade ****', async (t) => {

let VER = 'v3';
const VER = 'v3';

e2eUtils.installChaincode('org1', testUtil.CHAINCODE_UPGRADE_PATH_V2, null, VER, 'golang', t, true)
.then(() => {
return e2eUtils.installChaincode('org2', testUtil.CHAINCODE_UPGRADE_PATH_V2, null, VER, 'golang', t, true);
}, (err) => {
t.fail('Failed to install chaincode in peers of organization "org1". ' + err.stack ? err.stack : err);
t.end();
}).then(() => {
return e2eUtils.instantiateChaincode('org1', testUtil.CHAINCODE_UPGRADE_PATH_V2, VER, 'golang', true, true, t);
}).then(() => {
const fcn = 'query';
const args = ['b'];
const expectedResult = '1000';
const targets = []; // empty array, meaning client will get the peers from the channel
const chaincodeId = testUtil.END2END.chaincodeId;
logger.debug('Successfully upgraded chaincode to version v3');
return e2eUtils.queryChaincode('org1', VER, targets, fcn, args, expectedResult, chaincodeId, t);

}).then((result) => {
if(result){
t.pass('Successfully query chaincode on the channel after re-initializing chaincode states during upgrade');
t.end();
}
else {
t.fail('Failed to query chaincode to verify re-initialized state information');
t.end();
}
}, (err) => {
t.fail('Failed to query chaincode on the channel. ' + err.stack ? err.stack : err);
await e2eUtils.installChaincode('org1', testUtil.CHAINCODE_UPGRADE_PATH_V2, null, VER, 'golang', t, true);
await e2eUtils.installChaincode('org2', testUtil.CHAINCODE_UPGRADE_PATH_V2, null, VER, 'golang', t, true);
await e2eUtils.instantiateChaincode('org1', testUtil.CHAINCODE_UPGRADE_PATH_V2, VER, 'golang', true, true, t);
const fcn = 'query';
const args = ['b'];
const expectedResult = '1000';
const targets = []; // empty array, meaning client will get the peers from the channel
const chaincodeId = testUtil.END2END.chaincodeId;
logger.debug('Successfully upgraded chaincode to version v3');
const result = await e2eUtils.queryChaincode('org1', VER, targets, fcn, args, expectedResult, chaincodeId, t);

if (result) {
t.pass('Successfully query chaincode on the channel after re-initializing chaincode states during upgrade');
t.end();
}).catch((err) => {
t.fail('Test failed due to unexpected reasons. ' + err.stack ? err.stack : err);
}
else {
t.fail('Failed to query chaincode to verify re-initialized state information');
t.end();
});
}
});

0 comments on commit fbc1b16

Please sign in to comment.