feat: enable starting and switching storage configs in tests

This commit is contained in:
Jonathan Rainville 2019-06-10 16:08:24 -04:00
parent 48aaceef7c
commit c39000c51b
8 changed files with 153 additions and 94 deletions

View File

@ -150,6 +150,10 @@ export class ProcessManager {
self.events.setCommandHandler('processes:stop', (name, cb) => {
let process = self.processes[name];
if (!process) {
// Process was never started
return cb();
}
cb = cb || function noop() {};
if (![ProcessState.Running, ProcessState.Errored].includes(process.state)) {
return cb(__(`The ${name} process is already ${process.state.toLowerCase()}.`));

View File

@ -1,6 +1,7 @@
import { __ } from 'embark-i18n';
import { deconstructUrl, prepareContractsConfig, buildUrl } from 'embark-utils';
import { deconstructUrl, prepareContractsConfig, buildUrl, recursiveMerge } from 'embark-utils';
import deepEqual from 'deep-equal';
import cloneDeep from 'lodash.clonedeep';
const async = require('async');
const web3Utils = require('web3-utils');
@ -15,6 +16,7 @@ class Test {
this.logger = options.logger;
this.ipc = options.ipc;
this.configObj = options.config;
this.originalConfigObj = cloneDeep(options.config);
this.ready = true;
this.firstRunConfig = true;
this.error = false;
@ -177,8 +179,9 @@ class Test {
Object.keys(this.moduleConfigs).forEach(moduleName => {
options[moduleName] = options[moduleName] || {};
if (!deepEqual(options[moduleName], this.moduleConfigs[moduleName])) {
restartModules.push(function (paraCb) {
self.events.request(`config:${moduleName}Config:set`, options[moduleName], true, () => {
this.moduleConfigs[moduleName] = options[moduleName];
restartModules.push((paraCb) => {
self.events.request(`config:${moduleName}Config:set`, recursiveMerge({}, self.originalConfigObj[`${moduleName}Config`], options[moduleName]), () => {
self.events.request(`module:${moduleName}:reset`, paraCb);
});
});

View File

@ -4,8 +4,8 @@ export function last(array: any) {
return array[array.length - 1];
}
export function recursiveMerge(target: any, source: any) {
return merge.recursive(target, source);
export function recursiveMerge() {
return merge.recursive(...arguments);
}
export function compact(array: any) {

View File

@ -58,16 +58,8 @@ var Config = function(options) {
this.registerEvents();
};
Config.prototype.setConfig = function(configName, newConfig, recursive, cb) {
if (typeof recursive === 'function') {
cb = recursive;
recursive = false;
}
if (recursive) {
this[configName] = recursiveMerge(this[configName], newConfig);
} else {
this[configName] = newConfig;
}
Config.prototype.setConfig = function(configName, newConfig, cb) {
this[configName] = newConfig;
cb();
};

View File

@ -273,19 +273,14 @@ class Engine {
}
storageService(_options) {
// Register both modules as they are responsible for making sure if they need to start or not
async.parallel([
(next) => {
if (!this.config.storageConfig.available_providers.includes("ipfs")) {
return next();
}
this.events.once("ipfs:process:started", next);
(paraCb) => {
this.events.once("ipfs:process:started", paraCb);
this.registerModule('ipfs');
},
(next) => {
if (!this.config.storageConfig.available_providers.includes("swarm")) {
return next();
}
this.events.once("swarm:process:started", next);
(paraCb) => {
this.events.once("swarm:process:started", paraCb);
this.registerModule('swarm');
}
], (err) => {
@ -293,7 +288,22 @@ class Engine {
console.error(__("Error starting storage process(es): %s", err));
}
this.registerModule('storage', {plugins: this.plugins});
this.events.setCommandHandler("module:storage:reset", (cb) => {
async.parallel([
(paraCb) => {
this.events.request("module:ipfs:reset", paraCb);
},
(paraCb) => {
this.events.request("module:swarm:reset", paraCb);
},
(paraCb) => {
this.events.request("module:storageJS:reset", paraCb);
}
], cb);
});
});
}
web3Service(options) {

View File

@ -15,7 +15,7 @@ class IPFS {
this.events = embark.events;
this.buildDir = options.buildDir;
this.embarkConfig = embark.config.embarkConfig;
this.storageConfig = embark.config.storageConfig;
this.config = embark.config;
this.namesystemConfig = embark.config.namesystemConfig;
this.embark = embark;
this.fs = embark.fs;
@ -25,44 +25,64 @@ class IPFS {
this.storageProcessesLauncher = null;
this.usingRunningNode = false;
this.modulesPath = dappPath(embark.config.embarkConfig.generationDir, constants.dappArtifacts.symlinkDir);
this.registered = false;
this.webServerConfig = embark.config.webServerConfig;
this.blockchainConfig = embark.config.blockchainConfig;
this.embark.events.setCommandHandler("module:ipfs:reset", (cb) => {
this.events.request("processes:stop", "ipfs", (err) => {
if (err) {
this.logger.error(__('Error stopping IPFS process'), err);
}
this.init(cb);
});
});
this.init();
}
init(callback = () => {}) {
if (!this.isIpfsStorageEnabledInTheConfig()) {
return this.events.emit("ipfs:process:started", null, false);
this.events.emit("ipfs:process:started", null, false);
return callback();
}
if (!this.registered) {
this.registered = true;
this.setServiceCheck();
this.registerUploadCommand();
this.listenToCommands();
this.registerConsoleCommands();
this.events.request("processes:register", "ipfs", {
launchFn: (cb) => {
if(this.usingRunningNode) {
return cb(__("IPFS process is running in a separate process and cannot be started by Embark."));
}
this.startProcess((err, newProcessStarted) => {
this.addStorageProviderToEmbarkJS();
this.addObjectToConsole();
this.events.emit("ipfs:process:started", err, newProcessStarted);
cb();
});
},
stopFn: (cb) => {
if(this.usingRunningNode) {
return cb(__("IPFS process is running in a separate process and cannot be stopped by Embark."));
}
this.stopProcess(cb);
}
});
}
this.setServiceCheck();
this.registerUploadCommand();
this.listenToCommands();
this.registerConsoleCommands();
this.events.request("processes:register", "ipfs", {
launchFn: (cb) => {
if(this.usingRunningNode) {
return cb(__("IPFS process is running in a separate process and cannot be started by Embark."));
}
this.startProcess((err, newProcessStarted) => {
this.addStorageProviderToEmbarkJS();
this.addObjectToConsole();
this.events.emit("ipfs:process:started", err, newProcessStarted);
cb();
});
},
stopFn: (cb) => {
if(this.usingRunningNode) {
return cb(__("IPFS process is running in a separate process and cannot be stopped by Embark."));
}
this.stopProcess(cb);
}
});
this.events.request("processes:launch", "ipfs", (err, msg) => {
if (err) {
return this.logger.error(err);
this.logger.error(err);
return callback(err);
}
if (msg) {
this.logger.info(msg);
}
callback();
});
}
@ -110,11 +130,11 @@ class IPFS {
}
_getNodeUrlConfig() {
if (this.storageConfig.upload.provider === 'ipfs') {
return this.storageConfig.upload;
if (this.config.storageConfig.upload.provider === 'ipfs') {
return this.config.storageConfig.upload;
}
for (let connection of this.storageConfig.dappConnection) {
for (let connection of this.config.storageConfig.dappConnection) {
if (connection.provider === 'ipfs') {
return connection;
}
@ -184,7 +204,7 @@ class IPFS {
this.storageProcessesLauncher = new StorageProcessesLauncher({
logger: self.logger,
events: self.events,
storageConfig: self.storageConfig,
storageConfig: self.config.storageConfig,
webServerConfig: self.webServerConfig,
blockchainConfig: self.blockchainConfig,
corsParts: self.embark.config.corsParts,
@ -207,8 +227,8 @@ class IPFS {
this.embark.registerUploadCommand('ipfs', (cb) => {
let upload_ipfs = new UploadIPFS({
buildDir: self.buildDir || 'dist/',
storageConfig: self.storageConfig,
configIpfsBin: self.storageConfig.ipfs_bin || "ipfs",
storageConfig: self.config.storageConfig,
configIpfsBin: self.config.storageConfig.ipfs_bin || "ipfs",
env: this.embark.env
});
@ -244,7 +264,7 @@ class IPFS {
}
isIpfsStorageEnabledInTheConfig() {
let {enabled, available_providers, dappConnection, upload} = this.storageConfig;
let {enabled, available_providers, dappConnection, upload} = this.config.storageConfig;
return (enabled || this.embark.currentContext.includes(constants.contexts.upload)) &&
(
available_providers.includes('ipfs') &&

View File

@ -13,12 +13,15 @@ class Storage {
this.embark.events.once("module:storage:ready", cb);
});
this.embark.events.setCommandHandler("module:storage:reset", (cb) => {
this.embark.events.setCommandHandler("module:storageJS:reset", (cb) => {
if (!this.isEnabled()) {
return cb();
}
this.ready = false;
this.addSetProviders(cb);
});
if (!embark.config.storageConfig.enabled) {
if (!this.isEnabled()) {
this.ready = true;
return;
}
@ -27,6 +30,10 @@ class Storage {
this.addSetProviders(() => {});
}
isEnabled() {
return !!this.embark.config.storageConfig.enabled;
}
handleUploadCommand() {
const self = this;
this.embark.events.setCommandHandler('storage:upload', (cb) => {

View File

@ -14,9 +14,9 @@ class Swarm {
this.logger = embark.logger;
this.events = embark.events;
this.buildDir = embark.config.buildDir;
this.storageConfig = embark.config.storageConfig;
this.host = this.storageConfig.host;
this.port = this.storageConfig.port;
this.config = embark.config;
this.host = this.config.storageConfig.host;
this.port = this.config.storageConfig.port;
this.embark = embark;
this.fs = embark.fs;
this.isServiceRegistered = false;
@ -25,16 +25,32 @@ class Swarm {
this.storageProcessesLauncher = null;
this.usingRunningNode = false;
this.modulesPath = dappPath(embark.config.embarkConfig.generationDir, constants.dappArtifacts.symlinkDir);
this.registered = false;
this.webServerConfig = embark.config.webServerConfig;
this.blockchainConfig = embark.config.blockchainConfig;
const cantDetermineUrl = this.storageConfig.upload.provider !== 'swarm' && !this.storageConfig.dappConnection.some(connection => connection.provider === 'swarm');
this.embark.events.setCommandHandler("module:swarm:reset", (cb) => {
this.events.request("processes:stop", "swarm", (err) => {
if (err) {
this.logger.error(__('Error stopping Swarm process'), err);
}
this.init(cb);
});
});
this.init();
}
init(callback = () => {}) {
const cantDetermineUrl = this.config.storageConfig.upload.provider !== 'swarm' && !this.config.storageConfig.dappConnection.some(connection => connection.provider === 'swarm');
if (this.isSwarmEnabledInTheConfig() && cantDetermineUrl) {
console.warn('\n===== Swarm module will not be loaded =====');
console.warn(`Swarm is enabled in the config, however the config is not setup to provide a URL for swarm and therefore the Swarm module will not be loaded. Please either change the ${'config/storage > upload'.bold} setting to Swarm or add the Swarm config to the ${'config/storage > dappConnection'.bold} array. Please see ${'https://embark.status.im/docs/storage_configuration.html'.underline} for more information.\n`);
return this.events.emit("swarm:process:started", null, false);
this.events.emit("swarm:process:started", null, false);
return callback();
}
if (!this.isSwarmEnabledInTheConfig()) {
this.embark.registerConsoleCommand({
@ -44,45 +60,52 @@ class Swarm {
cb();
}
});
return this.events.emit("swarm:process:started", null, false);
this.events.emit("swarm:process:started", null, false);
return callback();
}
this.providerUrl = buildUrl(this.storageConfig.upload.protocol, this.storageConfig.upload.host, this.storageConfig.upload.port);
this.providerUrl = buildUrl(this.config.storageConfig.upload.protocol, this.config.storageConfig.upload.host, this.config.storageConfig.upload.port);
this.getUrl = this.storageConfig.upload.getUrl || this.providerUrl + '/bzz:/';
this.getUrl = this.config.storageConfig.upload.getUrl || this.providerUrl + '/bzz:/';
this.swarm = new SwarmAPI({gateway: this.providerUrl});
this.setServiceCheck();
this.registerUploadCommand();
this.listenToCommands();
this.registerConsoleCommands();
this.events.request("processes:register", "swarm", {
launchFn: (cb) => {
if(this.usingRunningNode) {
return cb(__("Swarm process is running in a separate process and cannot be started by Embark."));
if (!this.registered) {
this.registered = true;
this.setServiceCheck();
this.registerUploadCommand();
this.listenToCommands();
this.registerConsoleCommands();
this.events.request("processes:register", "swarm", {
launchFn: (cb) => {
if(this.usingRunningNode) {
return cb(__("Swarm process is running in a separate process and cannot be started by Embark."));
}
this.startProcess((err, newProcessStarted) => {
this.addProviderToEmbarkJS();
this.addObjectToConsole();
this.events.emit("swarm:process:started", err, newProcessStarted);
cb();
});
},
stopFn: (cb) => {
if(this.usingRunningNode) {
return cb(__("Swarm process is running in a separate process and cannot be stopped by Embark."));
}
this.stopProcess(cb);
}
this.startProcess((err, newProcessStarted) => {
this.addProviderToEmbarkJS();
this.addObjectToConsole();
this.events.emit("swarm:process:started", err, newProcessStarted);
cb();
});
},
stopFn: (cb) => {
if(this.usingRunningNode) {
return cb(__("Swarm process is running in a separate process and cannot be stopped by Embark."));
}
this.stopProcess(cb);
}
});
});
}
this.events.request("processes:launch", "swarm", (err, msg) => {
if (err) {
return this.logger.error(err);
this.logger.error(err);
return callback(err);
}
if (msg) {
this.logger.info(msg);
}
callback();
});
}
@ -150,7 +173,7 @@ class Swarm {
this.storageProcessesLauncher = new StorageProcessesLauncher({
logger: self.logger,
events: self.events,
storageConfig: self.storageConfig,
storageConfig: self.config.storageConfig,
webServerConfig: self.webServerConfig,
corsParts: self.embark.config.corsParts,
blockchainConfig: self.blockchainConfig,
@ -174,7 +197,7 @@ class Swarm {
this.embark.registerUploadCommand('swarm', (cb) => {
let upload_swarm = new UploadSwarm({
buildDir: self.buildDir || 'dist/',
storageConfig: self.storageConfig,
storageConfig: self.config.storageConfig,
providerUrl: self.providerUrl,
swarm: self.swarm,
env: self.embark.env
@ -212,7 +235,7 @@ class Swarm {
}
isSwarmEnabledInTheConfig() {
let {enabled, available_providers, dappConnection, upload} = this.storageConfig;
let {enabled, available_providers, dappConnection, upload} = this.config.storageConfig;
return (enabled || this.embark.currentContext.includes(constants.contexts.upload)) &&
available_providers.includes('swarm') &&
(