Merge pull request #497 from embark-framework/features/test-connect-node

Connect to a node in tests
This commit is contained in:
Iuri Matias 2018-06-07 18:59:27 -04:00 committed by GitHub
commit 9f0ab86c2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 108 additions and 62 deletions

View File

@ -83,7 +83,7 @@ class Blockchain {
} }
], cb); ], cb);
} else { } else {
throw new Error("contracts config error: unknown deployment type " + this.contractsConfig.deployment.type); throw new Error(__("contracts config error: unknown deployment type %s", this.contractsConfig.deployment.type));
} }
} }

View File

@ -2,11 +2,11 @@ const async = require('async');
const Engine = require('../core/engine.js'); const Engine = require('../core/engine.js');
const TestLogger = require('./test_logger.js'); const TestLogger = require('./test_logger.js');
const Web3 = require('web3'); const Web3 = require('web3');
const utils = require('../utils/utils');
const constants = require('../constants'); const constants = require('../constants');
const Events = require('../core/events'); const Events = require('../core/events');
const cloneDeep = require('clone-deep'); const cloneDeep = require('clone-deep');
const AccountParser = require('../contracts/accountParser'); const AccountParser = require('../contracts/accountParser');
const Provider = require('../contracts/provider');
function getSimulator() { function getSimulator() {
try { try {
@ -29,7 +29,7 @@ function getSimulator() {
class Test { class Test {
constructor(options) { constructor(options) {
this.options = options || {}; this.options = options || {};
this.simOptions = this.options.simulatorOptions || {}; this.simOptions = {};
this.contracts = {}; this.contracts = {};
this.events = new Events(); this.events = new Events();
this.ready = true; this.ready = true;
@ -37,42 +37,29 @@ class Test {
this.compiledContracts = {}; this.compiledContracts = {};
this.web3 = new Web3(); this.web3 = new Web3();
this.initWeb3Provider();
this.engine = new Engine({
env: this.options.env || 'test',
// TODO: config will need to detect if this is a obj
embarkConfig: this.options.embarkConfig || 'embark.json',
interceptLogs: false
});
this.engine.init({
logger: new TestLogger({logLevel: 'debug'})
});
this.versions_default = this.engine.config.contractsConfig.versions;
const deploymentConfig = this.engine.config.contractsConfig.versions;
// Reset contract config to nothing to make sure we deploy only what we want
this.engine.config.contractsConfig = {contracts: {}, versions: this.versions_default, deployment: deploymentConfig};
this.engine.startService("libraryManager");
this.engine.startService("codeRunner");
this.initDeployServices();
this.engine.startService("codeGenerator");
} }
initWeb3Provider() { initWeb3Provider(callback) {
if (this.simOptions.node) { if (this.simOptions.host) {
this.web3.setProvider(new this.web3.providers.HttpProvider(this.simOptions.node)); const providerOptions = {
} else { web3: this.web3,
if (this.simOptions.accounts) { accountsConfig: this.simOptions.accounts,
this.simOptions.accounts = this.simOptions.accounts.map((account) => { logger: this.engine.logger,
return {balance: account.hexBalance, secretKey: account.privateKey}; isDev: false,
}); web3Endpoint: 'http://' + this.simOptions.host + ':' + this.simOptions.port
} };
this.sim = getSimulator(); this.provider = new Provider(providerOptions);
this.web3.setProvider(this.sim.provider(this.simOptions)); return this.provider.startWeb3Provider(callback);
} }
if (this.simOptions.accounts) {
this.simOptions.accounts = this.simOptions.accounts.map((account) => {
return {balance: account.hexBalance, secretKey: account.privateKey};
});
}
this.sim = getSimulator();
this.web3.setProvider(this.sim.provider(this.simOptions));
callback();
} }
initDeployServices() { initDeployServices() {
@ -87,10 +74,39 @@ class Test {
init(callback) { init(callback) {
const self = this; const self = this;
this.engine.contractsManager.build(() => {
self.builtContracts = cloneDeep(self.engine.contractsManager.contracts); this.initWeb3Provider((err) => {
self.compiledContracts = cloneDeep(self.engine.contractsManager.compiledContracts); if (err) {
callback(); return callback(err);
}
this.engine = new Engine({
env: this.options.env || 'test',
// TODO: config will need to detect if this is a obj
embarkConfig: this.options.embarkConfig || 'embark.json',
interceptLogs: false
});
this.engine.init({
logger: new TestLogger({logLevel: 'debug'})
});
this.versions_default = this.engine.config.contractsConfig.versions;
// Reset contract config to nothing to make sure we deploy only what we want
this.engine.config.contractsConfig = {
contracts: {},
versions: this.versions_default
};
this.engine.startService("libraryManager");
this.engine.startService("codeRunner");
this.initDeployServices();
this.engine.startService("codeGenerator");
this.engine.contractsManager.build(() => {
self.builtContracts = cloneDeep(self.engine.contractsManager.contracts);
self.compiledContracts = cloneDeep(self.engine.contractsManager.compiledContracts);
callback();
});
}); });
} }
@ -104,6 +120,7 @@ class Test {
} }
config(options, callback) { config(options, callback) {
const self = this;
if (!callback) { if (!callback) {
callback = function () { callback = function () {
}; };
@ -111,30 +128,59 @@ class Test {
if (!options.contracts) { if (!options.contracts) {
throw new Error(__('No contracts specified in the options')); throw new Error(__('No contracts specified in the options'));
} }
this.options = utils.recursiveMerge(this.options, options); self.ready = false;
this.simOptions = this.options.simulatorOptions || {};
this.ready = false;
if (this.options.deployment && this.options.deployment.accounts) { async.waterfall([
// Account setup function checkDeploymentOptions(next) {
this.simOptions.accounts = AccountParser.parseAccountsConfig(this.options.deployment.accounts, this.web3); if (!options.deployment) {
this.initWeb3Provider(); if (!self.simOptions.host && !self.simOptions.accounts) {
this.initDeployServices(); return next();
} }
self.simOptions = {};
// Reset contracts } else {
this.engine.contractsManager.contracts = cloneDeep(this.builtContracts); self.simOptions = {};
this.engine.contractsManager.compiledContracts = cloneDeep(this.compiledContracts); let resetServices = false;
if (options.deployment.accounts) {
this._deploy(options, (err, accounts) => { // Account setup
this.ready = true; self.simOptions.accounts = AccountParser.parseAccountsConfig(options.deployment.accounts, self.web3);
this.events.emit('ready'); resetServices = true;
if (err) { }
console.error(err.red); if (options.deployment.host && options.deployment.port && options.deployment.type) {
return callback(err); if (options.deployment.type !== 'rpc') {
throw new Error(__("contracts config error: unknown deployment type %s", options.deployment.type));
}
Object.assign(self.simOptions, {host: options.deployment.host, port: options.deployment.port});
resetServices = true;
}
if (!resetServices) {
return next();
}
}
self.initWeb3Provider((err) => {
if (err) {
return next(err);
}
self.initDeployServices();
next();
});
},
function resetContracts(next) {
self.engine.contractsManager.contracts = cloneDeep(self.builtContracts);
self.engine.contractsManager.compiledContracts = cloneDeep(self.compiledContracts);
next();
},
function deploy(next) {
self._deploy(options, (err, accounts) => {
self.ready = true;
self.events.emit('ready');
if (err) {
console.error(err.red);
return next(err);
}
next(null, accounts);
});
} }
callback(null, accounts); ], callback);
});
} }
_deploy(config, callback) { _deploy(config, callback) {