diff --git a/test/core/both/test-files.js b/test/core/both/test-files.js index 18f7c88b34..6f46b1a38e 100644 --- a/test/core/both/test-files.js +++ b/test/core/both/test-files.js @@ -4,14 +4,18 @@ const test = require('interface-ipfs-core') const IPFS = require('../../../src/core') +// let factory + const common = { setup: function (cb) { + // TODO change to factory const ipfs = new IPFS(require('../../utils/repo-path')) ipfs.load(() => { cb(null, ipfs) }) }, teardown: function (cb) { + // factory.teardown cb() } } diff --git a/test/core/both/test-object.js b/test/core/both/test-object.js index f5daa46cc6..4ce9e4fd0e 100644 --- a/test/core/both/test-object.js +++ b/test/core/both/test-object.js @@ -3,18 +3,17 @@ 'use strict' const test = require('interface-ipfs-core') +const IPFSFactory = require('../../utils/factory') -const IPFS = require('../../../src/core') +let factory const common = { setup: function (cb) { - const ipfs = new IPFS(require('../../utils/repo-path')) - ipfs.load(() => { - cb(null, ipfs) - }) + factory = new IPFSFactory() + cb(null, factory) }, teardown: function (cb) { - cb() + factory.dismantle(cb) } } diff --git a/test/utils/factory/default-config.json b/test/utils/factory/default-config.json new file mode 100644 index 0000000000..20e1d1b753 --- /dev/null +++ b/test/utils/factory/default-config.json @@ -0,0 +1,68 @@ +{ + "Identity": { + "PeerID": "", + "PrivKey": "" + }, + "Datastore": { + "Type": "", + "Path": "", + "StorageMax": "", + "StorageGCWatermark": 0, + "GCPeriod": "", + "Params": null, + "NoSync": false + }, + "Addresses": { + "Swarm": [ + "/ip4/127.0.0.1/tcp/0" + ], + "API": "/ip4/127.0.0.1/tcp/0", + "Gateway": "/ip4/127.0.0.1/tcp/0" + }, + "Mounts": { + "IPFS": "/ipfs", + "IPNS": "/ipns", + "FuseAllowOther": false + }, + "Version": { + "Current": "jsipfs-dev", + "Check": "error", + "CheckDate": "0001-01-01T00:00:00Z", + "CheckPeriod": "172800000000000", + "AutoUpdate": "minor" + }, + "Discovery": { + "MDNS": { + "Enabled": true, + "Interval": 10 + } + }, + "Ipns": { + "RepublishPeriod": "", + "RecordLifetime": "", + "ResolveCacheSize": 128 + }, + "Bootstrap": [], + "Tour": { + "Last": "" + }, + "Gateway": { + "HTTPHeaders": null, + "RootRedirect": "", + "Writable": false + }, + "SupernodeRouting": { + "Servers": [] + }, + "API": { + "HTTPHeaders": null + }, + "Swarm": { + "AddrFilters": null + }, + "Log": { + "MaxSizeMB": 250, + "MaxBackups": 1, + "MaxAgeDays": 0 + } +} diff --git a/test/utils/factory/index.js b/test/utils/factory/index.js new file mode 100644 index 0000000000..502369e40d --- /dev/null +++ b/test/utils/factory/index.js @@ -0,0 +1,120 @@ +'use strict' + +const fs = require('fs') +const path = require('path') +const PeerId = require('peer-id') +const isNode = require('detect-node') +const IPFSRepo = require('ipfs-repo') +const cleanRepo = require('../clean') +const IPFS = require('../../../src/core') +const series = require('run-series') + +module.exports = Factory + +function Factory () { + if (!(this instanceof Factory)) { + return new Factory() + } + + const nodes = [] + + /* yields a new started node */ + this.spawnNode = (repoPath, config, callback) => { + if (typeof repoPath === 'function') { + callback = repoPath + repoPath = undefined + } + if (typeof config === 'function') { + callback = config + config = undefined + } + + if (!repoPath) { + repoPath = '/tmp/.ipfs-' + Math.random() + .toString() + .substring(2, 8) + } + + if (!config) { + const defaultConfigPath = path.join(__dirname, 'default-config.json') + config = JSON.parse(fs.readFileSync(defaultConfigPath).toString()) + const pId = PeerId.create().toJSON() + config.Identity.PeerID = pId.id + config.Identity.PrivKey = pId.privKey + } + + // set up the repo + let store + let teardown + + if (isNode) { + store = require('fs-blob-store') + teardown = (done) => { + cleanRepo(repoPath) + done() + } + } else { + const idb = window.indexedDB || + window.mozIndexedDB || + window.webkitIndexedDB || + window.msIndexedDB + store = require('idb-plus-blob-store') + teardown = (done) => { + idb.deleteDatabase(repoPath) + idb.deleteDatabase(repoPath + '/blocks') + done() + } + } + + const repo = new IPFSRepo(repoPath, { stores: store }) + repo.teardown = teardown + + // create the IPFS node + const ipfs = new IPFS(repo) + ipfs.init({ emptyRepo: true }, (err) => { + if (err) { + return callback(err) + } + repo.config.set(config, launchNode) + }) + + function launchNode () { + ipfs.load((err) => { + if (err) { + return callback(err) + } + + ipfs.goOnline((err) => { + if (err) { + return callback(err) + } + + nodes.push({ + repo: repo, + ipfs: ipfs + }) + + callback(null, ipfs) + }) + }) + } + } + + this.dismantle = function (callback) { + series(nodes.map((node) => { + return node.ipfs.goOffline + }), clean) + + function clean (err) { + if (err) { + return callback(err) + } + series( + nodes.map((node) => { + return node.repo.teardown + }), + callback + ) + } + } +}