Skip to content

Commit

Permalink
wrap fabric test into basic-network, prepare for adding new fabric-ca…
Browse files Browse the repository at this point in the history
… network tests. blockchain-desktop#171

Signed-off-by: Yi DENG <[email protected]>
  • Loading branch information
dengyi9 committed Mar 14, 2019
1 parent 1382181 commit fda746d
Showing 1 changed file with 188 additions and 170 deletions.
358 changes: 188 additions & 170 deletions test/util/fabric.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,202 +10,220 @@ const path = require('path');
// 1. start fabric network
// 2. call client functions
// 3. stop fabric network, clean up

function initFabricNetwork() {
logger.info('Shuting down old Fabric Network.');
let buf = execSync('cd fabric/v1.1/basic-network && ./teardown.sh');
logger.debug(buf.toString());

logger.info('Initiating Fabric Network.');
buf = execSync('cd fabric/v1.1/fabcar && ./startFabric.sh');
logger.debug(buf.toString());
}

function clearFabricNetwork() {
// FIXME: 关闭网络需处理异步问题。 直接关闭会导致异步测试未结束,而网络被关闭。
// 当前解决方案,在initFabricNetwork()中关闭网络

// logger.info('clearing Fabric Network.');
// const buf = execSync('cd fabric/v1.1/basic-network && ./teardown.sh');
// logger.debug(buf.toString());
}

beforeAll(() => {
jest.setTimeout(10000);
initFabricNetwork();
});

afterAll(clearFabricNetwork);

// 注意, config.db文件中的"path"字段,对应fabric-node-sdk的用户私钥仓库路径,需根据测试环境配置,
// 目前只处理fabric v1.1-basic-network的例子,处理fabric v1.3需同时考虑sdk版本升级,以及启动脚本的调整等
const configDbForTest = new Datastore({
filename: path.join(__dirname, '../resources/persistence/config.db'),
autoload: true,
});

describe('Fabric Client Basic', () => {
it('instantiate client.', () =>
getFabricClientSingletonHelper(configDbForTest)
.then((client) => {
expect(client)
.not
.toBeNull();
}));

it('query chaincode', () => {
const clientPromise = getFabricClientSingletonHelper(configDbForTest);
return clientPromise.then((client) => {
logger.info('OK. Got client. client.channel: ', client.channels);
return client.queryCc('fabcar', 'queryAllCars', null, 'mychannel')
.then((result) => {
logger.info('query result: ', result);
expect(result).not.toBeNull();
})
.catch((err) => {
logger.error(err);
throw new Error();
});
});
describe('fabric v1.1 basic-network', () => {
function initFabricNetwork() {
logger.info('Shuting down old Fabric Network.');
let buf = execSync('cd fabric/v1.1/basic-network && ./teardown.sh');
logger.debug(buf.toString());

logger.info('Initiating Fabric Network.');
buf = execSync('cd fabric/v1.1/fabcar && ./startFabric.sh');
logger.debug(buf.toString());
}

function clearFabricNetwork() {
// FIXME: 关闭网络需处理异步问题。 直接关闭会导致异步测试未结束,而网络被关闭。
// 当前解决方案,在initFabricNetwork()中关闭网络

// logger.info('clearing Fabric Network.');
// const buf = execSync('cd fabric/v1.1/basic-network && ./teardown.sh');
// logger.debug(buf.toString());
}

beforeAll(() => {
jest.setTimeout(15000);
initFabricNetwork();
});
});

describe('Fabric Client Advanced', () => {
describe('invoke chaincode', () => {
// FIXME: invoke 后测试进程无法正常结束,可能存在连接或其他调用未断开。待查因解决
it('invoke for one peer', () => getFabricClientSingletonHelper(configDbForTest)
.then(client => client.invokeCc('fabcar',
'changeCarOwner',
['CAR0', 'newPerson'],
'mychannel',
[],
[],
[])
.then((result) => {
logger.info('invoke result: ', result);
expect(result).not.toBeNull();
return Promise.resolve();
}))
.catch((err) => {
logger.info('catch invoke reject');
logger.info(err);
throw err;
}));
afterAll(clearFabricNetwork);

it('invoke for multiple peers', () => {
// TODO: to be implemented
});
// 注意, config.db文件中的"path"字段,对应fabric-node-sdk的用户私钥仓库路径,需根据测试环境配置,
// 目前只处理fabric v1.1-basic-network的例子,处理fabric v1.3需同时考虑sdk版本升级,以及启动脚本的调整等
const configDbForTest = new Datastore({
filename: path.join(__dirname, '../resources/persistence/config.db'),
autoload: true,
});

describe('manage chaincode', () => {
it('install chaincode', () => getFabricClientSingletonHelper(configDbForTest)
.then(client =>
// FIXME: 需处理GOPATH依赖的问题

client.installCc('fabcar', 'yetAnotherfabcar', '2.0')
describe('Fabric Client Basic', () => {
it('instantiate client.', () =>
getFabricClientSingletonHelper(configDbForTest)
.then((client) => {
expect(client)
.not
.toBeNull();
}));

it('query chaincode', () => {
const clientPromise = getFabricClientSingletonHelper(configDbForTest);
return clientPromise.then((client) => {
logger.info('OK. Got client. client.channel: ', client.channels);
return client.queryCc('fabcar', 'queryAllCars', null, 'mychannel')
.then((result) => {
logger.info('install chaincode result: ', result);
expect(result).not.toBeNull();
}),
)
.catch((err) => {
logger.info('install chaincode error: ', err);
throw err;
}));
logger.info('query result: ', result);
expect(result)
.not
.toBeNull();
})
.catch((err) => {
logger.error(err);
throw new Error();
});
});
});
});

it('instantiate chaincode', () => {
const ccName = 'instFabcar';
return getFabricClientSingletonHelper(configDbForTest)
.then(client => client.installCc('fabcar', ccName, '1.0')
describe('Fabric Client Advanced', () => {
describe('invoke chaincode', () => {
// FIXME: invoke 后测试进程无法正常结束,可能存在连接或其他调用未断开。待查因解决
it('invoke for one peer', () => getFabricClientSingletonHelper(configDbForTest)
.then(client => client.invokeCc('fabcar',
'changeCarOwner',
['CAR0', 'newPerson'],
'mychannel',
[],
[],
[])
.then((result) => {
logger.info('invoke result: ', result);
expect(result)
.not
.toBeNull();
return Promise.resolve(client);
return Promise.resolve();
}))
.then((client) => {
jest.setTimeout(100000);
return client.instantiateOrUpgradeCc(true, 'mychannel', ccName, '1.0', null, null)
.catch((err) => {
logger.info('catch invoke reject');
logger.info(err);
throw err;
}));

it('invoke for multiple peers', () => {
// TODO: to be implemented
});
});

describe('manage chaincode', () => {
it('install chaincode', () => getFabricClientSingletonHelper(configDbForTest)
.then(client =>
// FIXME: 需处理GOPATH依赖的问题

client.installCc('fabcar', 'yetAnotherfabcar', '2.0')
.then((result) => {
logger.info('instantiateOrUpgradeCc result: ', result);
logger.info('install chaincode result: ', result);
expect(result)
.not
.toBeNull();
jest.setTimeout(30000);
});
})
}),
)
.catch((err) => {
logger.info('instantiate chaincode error: ', err);
logger.info('install chaincode error: ', err);
throw err;
});
});
}));

it('upgrade chaincode', () => {
jest.setTimeout(120000);
const ccName = 'upgradeFabcar';
return getFabricClientSingletonHelper(configDbForTest)
.then(client => client.installCc('fabcar', ccName, '1.0')
.then(() => client.instantiateOrUpgradeCc(true, 'mychannel', ccName, '1.0', null, null))
// 等待链码实例化容器启动,否则升级链码会失败
.then(() => new Promise(resolve => setTimeout(resolve, 10000)))
.then(() => client.installCc('fabcar', ccName, '2.0'))
.then(() => client.instantiateOrUpgradeCc(false, 'mychannel', ccName, '2.0', null, null)
it('instantiate chaincode', () => {
const ccName = 'instFabcar';
return getFabricClientSingletonHelper(configDbForTest)
.then(client => client.installCc('fabcar', ccName, '1.0')
.then((result) => {
logger.info('UpgradeCc result: ', result);
expect(result).not.toBeNull();
jest.setTimeout(30000);
})))
.catch((err) => {
logger.info('instantiate chaincode error: ', err);
throw err;
});
expect(result)
.not
.toBeNull();
return Promise.resolve(client);
}))
.then((client) => {
jest.setTimeout(100000);
return client.instantiateOrUpgradeCc(true, 'mychannel', ccName, '1.0', null, null)
.then((result) => {
logger.info('instantiateOrUpgradeCc result: ', result);
expect(result)
.not
.toBeNull();
jest.setTimeout(30000);
});
})
.catch((err) => {
logger.info('instantiate chaincode error: ', err);
throw err;
});
});

it('upgrade chaincode', () => {
jest.setTimeout(120000);
const ccName = 'upgradeFabcar';
return getFabricClientSingletonHelper(configDbForTest)
.then(client => client.installCc('fabcar', ccName, '1.0')
.then(() => client.instantiateOrUpgradeCc(true, 'mychannel', ccName, '1.0', null, null))
// 等待链码实例化容器启动,否则升级链码会失败
.then(() => new Promise(resolve => setTimeout(resolve, 10000)))
.then(() => client.installCc('fabcar', ccName, '2.0'))
.then(() => client.instantiateOrUpgradeCc(false, 'mychannel', ccName, '2.0', null, null)
.then((result) => {
logger.info('UpgradeCc result: ', result);
expect(result)
.not
.toBeNull();
jest.setTimeout(30000);
})))
.catch((err) => {
logger.info('instantiate chaincode error: ', err);
throw err;
});
});
});
});


it('query block info', () => getFabricClientSingletonHelper(configDbForTest)
.then(client => client.queryBlock(0, 'mychannel')
.then((result) => {
logger.info('query block result: ', result);
expect(result)
.not
.toBeNull();
}))
.catch((err) => {
throw err;
}));

describe('manage channel', () => {
it('create channel', () =>
// FIXME: 创建通道,目前依赖contenct类中,复制configtx.yaml文件到指定目录下,需考虑如何测试这一步
getFabricClientSingletonHelper(configDbForTest)
.then(client => client.createChannel('createtestchannel', 'OneOrgChannel')
.then((result) => {
logger.info('createChannel result: ', result);
expect(result).not.toBeNull();
}))
.catch((err) => {
throw err;
}),
);
it('query block info', () => getFabricClientSingletonHelper(configDbForTest)
.then(client => client.queryBlock(0, 'mychannel')
.then((result) => {
logger.info('query block result: ', result);
expect(result)
.not
.toBeNull();
}))
.catch((err) => {
throw err;
}));

it('join peer to channel', () => {
const chanName = 'jointestchannel';
return getFabricClientSingletonHelper(configDbForTest)
.then(client => client.createChannel(chanName, 'OneOrgChannel')
.then((result) => {
logger.info('createChannel result: ', result);
expect(result).not.toBeNull();
})
.then(() => client.joinChannel(chanName)
describe('manage channel', () => {
it('create channel', () =>
// FIXME: 创建通道,目前依赖contenct类中,复制configtx.yaml文件到指定目录下,需考虑如何测试这一步
getFabricClientSingletonHelper(configDbForTest)
.then(client => client.createChannel('createtestchannel', 'OneOrgChannel')
.then((result) => {
logger.info('joinChannel result: ', result);
expect(result).not.toBeNull();
})))
.catch((err) => {
throw err;
});
logger.info('createChannel result: ', result);
expect(result)
.not
.toBeNull();
}))
.catch((err) => {
throw err;
}),
);

it('join peer to channel', () => {
const chanName = 'jointestchannel';
return getFabricClientSingletonHelper(configDbForTest)
.then(client => client.createChannel(chanName, 'OneOrgChannel')
.then((result) => {
logger.info('createChannel result: ', result);
expect(result)
.not
.toBeNull();
})
.then(() => client.joinChannel(chanName)
.then((result) => {
logger.info('joinChannel result: ', result);
expect(result)
.not
.toBeNull();
})))
.catch((err) => {
throw err;
});
});
});
});
});

describe('fabric v1.3 fabric-ca network', () => {
// TODO
});

0 comments on commit fda746d

Please sign in to comment.