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

caliper-ethereum : Add new tests for connectorFactory and ethereum-connector #1560

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 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
6 changes: 3 additions & 3 deletions packages/caliper-ethereum/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"scripts": {
"pretest": "npm run licchk",
"licchk": "license-check-and-add",
"test": "npm run lint",
"test": "npm run lint && npm run nyc",
"lint": "npx eslint .",
"nyc": "nyc --reporter=text --reporter=clover mocha --recursive -t 10000"
},
Expand All @@ -28,9 +28,9 @@
},
"devDependencies": {
"eslint": "^5.16.0",
"license-check-and-add": "2.3.6",
"mocha": "3.4.2",
"nyc": "11.1.0",
"license-check-and-add": "2.3.6"
"nyc": "11.1.0"
},
"license-check-and-add-config": {
"folder": ".",
Expand Down
51 changes: 51 additions & 0 deletions packages/caliper-ethereum/test/connectorFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

Sweetdevil144 marked this conversation as resolved.
Show resolved Hide resolved
'use strict';

const chai = require('chai');
const expect = chai.expect;
const ConfigUtil = require('@hyperledger/caliper-core').ConfigUtil;
const path = require('path');

const { ConnectorFactory } = require('../lib/connectorFactory');
const EthereumConnector = require('../lib/ethereum-connector');

describe('ConnectorFactory', function () {
let tempConfigFilePath;

beforeEach(() => {
tempConfigFilePath = path.resolve(
__dirname,
'./utils/networkconfig.json'
);
ConfigUtil.set(ConfigUtil.keys.NetworkConfig, tempConfigFilePath);
});

const workerIndices = [0, 1, 2];
workerIndices.forEach((workerIndex) => {
it(`should create an instance of EthereumConnector with workerIndex ${workerIndex}`, async function () {
const connector = await ConnectorFactory(workerIndex);
expect(connector).to.be.an.instanceof(EthereumConnector);
expect(connector.workerIndex).to.equal(workerIndex);
});
});

it('should handle -1 for the manager process', async function () {
const workerIndex = -1;
const connector = await ConnectorFactory(workerIndex);
expect(connector).to.be.an.instanceof(EthereumConnector);
expect(connector.workerIndex).to.equal(workerIndex);
});
});
81 changes: 81 additions & 0 deletions packages/caliper-ethereum/test/ethereum-connector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const path = require('path');
const expect = require('chai').expect;
const ConfigUtil = require('@hyperledger/caliper-core').ConfigUtil;
const EthereumConnector = require('../lib/ethereum-connector');

describe('EthereumConnector', function() {
let tempConfigFilePath;

beforeEach(() => {
tempConfigFilePath = path.resolve(__dirname, './utils/networkconfig.json');
ConfigUtil.set(ConfigUtil.keys.NetworkConfig, tempConfigFilePath);
});


describe('EthereumConnector.installSmartContract', () => {
Sweetdevil144 marked this conversation as resolved.
Show resolved Hide resolved
it('should throw an error when the specified contract path does not exist', async () => {
const invalidConfig = {
contracts: {
nonexistent: {
path: 'src/simple/simple.sol'
}
}
};
const ethereumConnector = new EthereumConnector(invalidConfig);

try {
await ethereumConnector.installSmartContract();
} catch (err) {
expect(err.message).to.contain('Cannot find module');
Sweetdevil144 marked this conversation as resolved.
Show resolved Hide resolved
Sweetdevil144 marked this conversation as resolved.
Show resolved Hide resolved
}
});
});

describe('EthereumConnector.init', () => {
it('should throw an error when the specified contract path does not exist', async () => {
const ethereumConnector = new EthereumConnector(0, 'ethereum');
const contractDetails = {
path: 'src/simple/nonexistent.sol'
};

try {
await ethereumConnector.init(contractDetails);
} catch (err) {
expect(err.message).to.contain('connection not open');
Sweetdevil144 marked this conversation as resolved.
Show resolved Hide resolved
}
});
});
});

describe('EthereumConnector.checkConfig()', function () {
Sweetdevil144 marked this conversation as resolved.
Show resolved Hide resolved
beforeEach(() => {
const invalidConfig = path.resolve(
__dirname,
'./utils/invalidconfig.json'
);
ConfigUtil.set(ConfigUtil.keys.NetworkConfig, invalidConfig);
});
it('should throw an error for an incorrect url path', function () {
const invalidConfig = path.resolve(
__dirname,
'./utils/invalidUrlConfig.json'
);
expect(() => new EthereumConnector(invalidConfig)).to.throw();
Sweetdevil144 marked this conversation as resolved.
Show resolved Hide resolved
});
});
22 changes: 22 additions & 0 deletions packages/caliper-ethereum/test/utils/invalidUrlConfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
Sweetdevil144 marked this conversation as resolved.
Show resolved Hide resolved
"caliper": {
"blockchain": "ethereum",
"command" : {
"start": "docker-compose -f config/docker-compose.yml up -d && sleep 30s",
"end" : "docker-compose -f config/docker-compose.yml down"
}
},
"ethereum": {
"url": 123,
"contractDeployerAddress": "0xc0A8e4D217eB85b812aeb1226fAb6F588943C2C2",
"contractDeployerAddressPassword": "password",
"fromAddress": "0xc0A8e4D217eB85b812aeb1226fAb6F588943C2C2",
"fromAddressPassword": "password",
"transactionConfirmationBlocks": 2,
"contracts": {
"simple": {
"path": "src/simple/simple.json"
}
}
}
}
22 changes: 22 additions & 0 deletions packages/caliper-ethereum/test/utils/networkconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"caliper": {
"blockchain": "ethereum",
"command" : {
"start": "docker-compose -f config/docker-compose.yml up -d && sleep 30s",
"end" : "docker-compose -f config/docker-compose.yml down"
}
},
"ethereum": {
"url": "ws://localhost:8546",
"contractDeployerAddress": "0xc0A8e4D217eB85b812aeb1226fAb6F588943C2C2",
"contractDeployerAddressPassword": "password",
"fromAddress": "0xc0A8e4D217eB85b812aeb1226fAb6F588943C2C2",
"fromAddressPassword": "password",
"transactionConfirmationBlocks": 2,
"contracts": {
"simple": {
"path": "src/simple/simple.json"
}
}
}
}