From 639573025f84c5db23e904da3c636923b0ee9e04 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Thu, 12 Dec 2019 14:44:25 +0000 Subject: [PATCH] refactor: use new ctl api (#2529) * feat: support the new interface core tests setup * fix: fix key rename output * fix: skip troublesome test * fix: ipfsd-ctl args handling * fix: change http api interface-core tests to new setup * fix: add reason to skip * fix: make aegir tests setup async * fix: fix ctl setup * fix: add pass option * fix: interface and cli tests ported plus https://github.com/ipfs/js-ipfs/pull/2625 * fix: support new ctl in core and http tests * fix: fix linter * chore: fix tests timeout * chore: fix tests timeout more * chore: fix webworker * chore: debug key exhcnage * chore: debug key exhcnage 2 * fix: key exchange fixed * fix: overrides correctly in pubsub tests * fix: refactor missing pieces and await clean in bitswap tests * fix: skip test that dont work in firefox * chore: bump chromedriver * fix: fix circuit relay example * fix: linter * fix: more examples --- .aegir.js | 37 +-- examples/circuit-relaying/src/app.js | 4 +- examples/circuit-relaying/test.js | 35 ++- examples/exchange-files-in-browser/test.js | 38 ++- examples/explore-ethereum-blockchain/test.js | 28 +- examples/package.json | 2 +- package.json | 5 +- src/http/api/resources/key.js | 2 +- test/cli/dht.js | 31 +- test/cli/name-pubsub.js | 49 +-- test/cli/ping.js | 43 +-- test/cli/pubsub.js | 38 +-- test/cli/swarm.js | 42 +-- test/core/bitswap.spec.js | 280 ++++-------------- test/core/block.spec.js | 28 +- test/core/bootstrap.spec.js | 112 +++---- ...circuit-relay.js => circuit-relay.spec.js} | 74 ++--- test/core/dag.spec.js | 25 +- test/core/dht.spec.js | 40 +-- test/core/files-sharding.spec.js | 66 +---- test/core/files.spec.js | 22 +- test/core/gc.spec.js | 26 +- test/core/interface.spec.js | 170 +++++------ .../{key-exchange.js => key-exchange.spec.js} | 31 +- test/core/name-pubsub.js | 32 +- test/core/name.spec.js | 78 ++--- test/core/node.js | 3 - test/core/object.spec.js | 22 +- test/core/pin.spec.js | 22 +- test/core/ping.spec.js | 134 ++------- test/core/stats.spec.js | 22 +- test/core/swarm.spec.js | 22 +- test/http-api/interface.js | 158 +++++----- test/utils/factory.js | 26 ++ test/utils/interface-common-factory.js | 134 --------- test/utils/on-and-off.js | 23 +- 36 files changed, 522 insertions(+), 1382 deletions(-) rename test/core/{circuit-relay.js => circuit-relay.spec.js} (56%) rename test/core/{key-exchange.js => key-exchange.spec.js} (57%) create mode 100644 test/utils/factory.js delete mode 100644 test/utils/interface-common-factory.js diff --git a/.aegir.js b/.aegir.js index c0e3476112..93a928004c 100644 --- a/.aegir.js +++ b/.aegir.js @@ -27,26 +27,27 @@ module.exports = { }, hooks: { node: { - pre: () => Promise.all([ - preloadNode.start(), - echoServer.start() - ]), - post: () => Promise.all([ - preloadNode.stop(), - echoServer.stop() - ]) + pre: async () => { + await preloadNode.start(), + await echoServer.start() + }, + post: async () => { + await preloadNode.stop(), + await echoServer.stop() + + } }, browser: { - pre: () => Promise.all([ - ipfsdServer.start(), - preloadNode.start(), - echoServer.start() - ]), - post: () => Promise.all([ - ipfsdServer.stop(), - preloadNode.stop(), - echoServer.stop() - ]) + pre: async () => { + await ipfsdServer.start() + await preloadNode.start() + await echoServer.start() + }, + post: async () => { + await ipfsdServer.stop() + await preloadNode.stop() + await echoServer.stop() + } } } } diff --git a/examples/circuit-relaying/src/app.js b/examples/circuit-relaying/src/app.js index 32bb634206..0732bb5a08 100644 --- a/examples/circuit-relaying/src/app.js +++ b/examples/circuit-relaying/src/app.js @@ -15,7 +15,7 @@ document.addEventListener('DOMContentLoaded', async () => { const $room = document.querySelector('#room') const $roomId = document.querySelector('#room-id') - let roomName = `default` + let roomName = 'default' const fragment = window.location.hash.substr(1) if (fragment) { roomName = fragment @@ -68,7 +68,7 @@ document.addEventListener('DOMContentLoaded', async () => { $roomId.addEventListener('keyup', (event) => { const kp = event.keyCode || event.which if (kp === 13 && $roomId.value.length > 0) { - let name = $roomId.value + const name = $roomId.value $room.innerText = name $room.setAttribute('style', 'display: inline') diff --git a/examples/circuit-relaying/test.js b/examples/circuit-relaying/test.js index 6c2e2dcb43..499abc69f4 100644 --- a/examples/circuit-relaying/test.js +++ b/examples/circuit-relaying/test.js @@ -5,11 +5,16 @@ const os = require('os') const path = require('path') const execa = require('execa') const delay = require('delay') -const DaemonFactory = require('ipfsd-ctl') -const df = DaemonFactory.create({ - type: 'js', - exec: path.resolve(`${__dirname}/../../src/cli/bin.js`), - IpfsClient: require('ipfs-http-client') +const { createFactory } = require('ipfsd-ctl') +const df = createFactory({ + ipfsModule: { + path: require.resolve('../../src'), + ref: require('../../src') + }, + ipfsHttpModule: { + path: require.resolve('ipfs-http-client'), + ref: require('ipfs-http-client') + } }) const { startServer @@ -38,19 +43,21 @@ async function testUI (url, relay, localPeerIdFile, remotePeerIdFile) { async function runTest () { const ipfsd = await df.spawn({ - initOptions: { bits: 512 }, - config: { - Addresses: { - Swarm: [ - '/ip4/127.0.0.1/tcp/0/ws' - ] - }, - Bootstrap: [], + type: 'proc', + test: true, + ipfsOptions: { relay: { enabled: true, hop: { enabled: true } + }, + config: { + Addresses: { + Swarm: [ + '/ip4/127.0.0.1/tcp/0/ws' + ] + } } } }) @@ -116,7 +123,7 @@ module.exports[pkg.name] = function (browser) { break } catch (err) { - + // ignore } await delay(1000) diff --git a/examples/exchange-files-in-browser/test.js b/examples/exchange-files-in-browser/test.js index e7545c160c..2e9f164395 100644 --- a/examples/exchange-files-in-browser/test.js +++ b/examples/exchange-files-in-browser/test.js @@ -5,12 +5,22 @@ const path = require('path') const os = require('os') const execa = require('execa') const delay = require('delay') -const DaemonFactory = require('ipfsd-ctl') -const df = DaemonFactory.create({ - type: 'js', - exec: path.resolve(`${__dirname}/../../src/cli/bin.js`), - IpfsClient: require('ipfs-http-client') -}) +const { createFactory } = require('ipfsd-ctl') +const df = createFactory({ + ipfsModule: { + path: require.resolve('../../src'), + ref: require('../../src') + }, + ipfsHttpModule: { + path: require.resolve('ipfs-http-client'), + ref: require('ipfs-http-client') + } +}, { + js: { + ipfsBin: path.resolve(`${__dirname}/../../src/cli/bin.js`) + } +} +) const { startServer } = require('../utils') @@ -35,14 +45,14 @@ async function testUI (env) { async function runTest () { const ipfsd = await df.spawn({ - initOptions: { bits: 512 }, - config: { - Addresses: { - Swarm: [ - '/ip4/127.0.0.1/tcp/0/ws' - ] - }, - Bootstrap: [] + ipfsOptions: { + config: { + Addresses: { + Swarm: [ + '/ip4/127.0.0.1/tcp/0/ws' + ] + } + } } }) diff --git a/examples/explore-ethereum-blockchain/test.js b/examples/explore-ethereum-blockchain/test.js index b56bba5154..af0c2ccc36 100644 --- a/examples/explore-ethereum-blockchain/test.js +++ b/examples/explore-ethereum-blockchain/test.js @@ -2,22 +2,26 @@ const fs = require('fs-extra') const path = require('path') -const DaemonFactory = require('ipfsd-ctl') -const df = DaemonFactory.create({ - type: 'proc', - exec: require('../../') +const { createFactory } = require('ipfsd-ctl') +const df = createFactory({ + ipfsModule: { + path: require.resolve('../../src'), + ref: require('../../src') + }, + ipfsHttpModule: { + path: require.resolve('ipfs-http-client'), + ref: require('ipfs-http-client') + } +}, { + js: { + ipfsBin: path.resolve(`${__dirname}/../../src/cli/bin.js`) + } }) async function runTest () { const ipfsd = await df.spawn({ - initOptions: { bits: 512 }, - config: { - Addresses: { - Swarm: [] - }, - Bootstrap: [] - }, - disposable: true + type: 'proc', + test: true }) const cids = [] diff --git a/examples/package.json b/examples/package.json index c55daf5abc..ed9608c395 100644 --- a/examples/package.json +++ b/examples/package.json @@ -8,7 +8,7 @@ }, "license": "MIT", "dependencies": { - "chromedriver": "^77.0.0", + "chromedriver": "^79.0.0", "delay": "^4.1.0", "execa": "^3.2.0", "fs-extra": "^8.1.0", diff --git a/package.json b/package.json index 6d2acd2849..c91552f56e 100644 --- a/package.json +++ b/package.json @@ -204,13 +204,14 @@ "execa": "^3.0.0", "form-data": "^3.0.0", "hat": "0.0.3", - "interface-ipfs-core": "^0.124.1", + "interface-ipfs-core": "^0.125.0", "ipfs-interop": "^0.1.1", - "ipfsd-ctl": "^0.47.2", + "ipfsd-ctl": "^1.0.2", "libp2p-websocket-star": "~0.10.2", "lodash": "^4.17.15", "ncp": "^2.0.0", "p-event": "^4.1.0", + "p-map": "^3.0.0", "qs": "^6.5.2", "rimraf": "^3.0.0", "sinon": "^7.4.2", diff --git a/src/http/api/resources/key.js b/src/http/api/resources/key.js index 3dbea4bd2c..9eafd808d9 100644 --- a/src/http/api/resources/key.js +++ b/src/http/api/resources/key.js @@ -26,7 +26,7 @@ exports.rename = async (request, h) => { const key = await ipfs.key.rename(oldName, newName) return h.response({ Was: key.was, - Now: key.name, + Now: key.now, Id: key.id, Overwrite: key.overwrite }) diff --git a/test/cli/dht.js b/test/cli/dht.js index 71b2012155..d593f7d079 100644 --- a/test/cli/dht.js +++ b/test/cli/dht.js @@ -3,33 +3,12 @@ 'use strict' const { expect } = require('interface-ipfs-core/src/utils/mocha') -const path = require('path') -const DaemonFactory = require('ipfsd-ctl') -const df = DaemonFactory.create({ - type: 'js', - IpfsClient: require('ipfs-http-client') -}) - +const factory = require('../utils/factory') const ipfsExec = require('../utils/ipfs-exec') -const daemonOpts = { - exec: path.resolve(`${__dirname}/../../src/cli/bin.js`), - config: { - Bootstrap: [], - Discovery: { - MDNS: { - Enabled: false - }, - webRTCStar: { - Enabled: false - } - } - }, - initOptions: { bits: 512 } -} - // TODO: unskip when DHT is enabled: https://github.com/ipfs/js-ipfs/pull/1994 describe.skip('dht', () => { + const df = factory({ type: 'js' }) const nodes = [] let ipfsA let ipfsB @@ -41,11 +20,11 @@ describe.skip('dht', () => { before(async function () { this.timeout(80 * 1000) - const ipfsdA = await df.spawn(daemonOpts) + const ipfsdA = await df.spawn() ipfsA = ipfsExec(ipfsdA.repoPath) nodes.push(ipfsdA) - const ipfsdB = await df.spawn(daemonOpts) + const ipfsdB = await df.spawn() ipfsB = ipfsExec(ipfsdB.repoPath) nodes.push(ipfsdB) }) @@ -71,7 +50,7 @@ describe.skip('dht', () => { return nodes[0].api.swarm.connect(multiaddrB) }) - after(() => Promise.all(nodes.map((node) => node.stop()))) + after(() => df.clean()) it('should be able to put a value to the dht and get it afterwards', async function () { this.timeout(60 * 1000) diff --git a/test/cli/name-pubsub.js b/test/cli/name-pubsub.js index fb7d02a645..8f6fed77cf 100644 --- a/test/cli/name-pubsub.js +++ b/test/cli/name-pubsub.js @@ -3,34 +3,12 @@ 'use strict' const { expect } = require('interface-ipfs-core/src/utils/mocha') -const path = require('path') +const factory = require('../utils/factory') const ipfsExec = require('../utils/ipfs-exec') -const DaemonFactory = require('ipfsd-ctl') -const df = DaemonFactory.create({ - type: 'js', - IpfsClient: require('ipfs-http-client') -}) - -const spawnDaemon = () => df.spawn({ - exec: path.resolve(`${__dirname}/../../src/cli/bin.js`), - args: ['--enable-namesys-pubsub'], - initOptions: { bits: 512 }, - config: { - Bootstrap: [], - Discovery: { - MDNS: { - Enabled: false - }, - webRTCStar: { - Enabled: false - } - } - } -}) - describe('name-pubsub', () => { describe('enabled', () => { + const df = factory({ type: 'js', args: ['--enable-namesys-pubsub'] }) let ipfsA let ipfsB let nodeAId @@ -44,12 +22,12 @@ describe('name-pubsub', () => { // timeout for the before step this.timeout(80 * 1000) - const nodeA = await spawnDaemon() - ipfsA = ipfsExec(nodeA.repoPath) + const nodeA = await df.spawn() + ipfsA = ipfsExec(nodeA.path) nodes.push(nodeA) - const nodeB = await spawnDaemon() - ipfsB = ipfsExec(nodeB.repoPath) + const nodeB = await df.spawn() + ipfsB = ipfsExec(nodeB.path) nodes.push(nodeB) }) @@ -71,7 +49,7 @@ describe('name-pubsub', () => { expect(out).to.eql(`connect ${bMultiaddr} success\n`) }) - after(() => Promise.all(nodes.map((node) => node.stop()))) + after(() => df.clean()) describe('pubsub commands', () => { it('should get enabled state of pubsub', async function () { @@ -114,6 +92,7 @@ describe('name-pubsub', () => { }) describe('disabled', () => { + const df = factory({ type: 'js' }) let ipfsA let node @@ -123,19 +102,11 @@ describe('name-pubsub', () => { // timeout for the before step this.timeout(80 * 1000) - node = await df.spawn({ - exec: path.resolve(`${__dirname}/../../src/cli/bin.js`), - config: {}, - initOptions: { bits: 512 } - }) + node = await df.spawn() ipfsA = ipfsExec(node.repoPath) }) - after(() => { - if (node) { - return node.stop() - } - }) + after(() => df.clean()) it('should get disabled state of pubsub', async function () { const res = await ipfsA('name pubsub state') diff --git a/test/cli/ping.js b/test/cli/ping.js index 66216fe0d2..ef59ce51e7 100644 --- a/test/cli/ping.js +++ b/test/cli/ping.js @@ -3,23 +3,8 @@ 'use strict' const { expect } = require('interface-ipfs-core/src/utils/mocha') -const DaemonFactory = require('ipfsd-ctl') +const factory = require('../utils/factory') const ipfsExec = require('../utils/ipfs-exec') -const path = require('path') -const df = DaemonFactory.create({ - type: 'js', - IpfsClient: require('ipfs-http-client') -}) - -const config = { - Bootstrap: [], - Discovery: { - MDNS: { - Enabled: - false - } - } -} describe('ping', function () { this.timeout(60 * 1000) @@ -28,15 +13,12 @@ describe('ping', function () { let bMultiaddr let ipfsdBId let cli + const df = factory({ type: 'js' }) before(async function () { this.timeout(60 * 1000) - ipfsdB = await df.spawn({ - exec: path.resolve(`${__dirname}/../../src/cli/bin.js`), - config, - initOptions: { bits: 512 } - }) + ipfsdB = await df.spawn() const peerInfo = await ipfsdB.api.id() ipfsdBId = peerInfo.id bMultiaddr = peerInfo.addresses[0] @@ -45,30 +27,17 @@ describe('ping', function () { before(async function () { this.timeout(60 * 1000) - ipfsdA = await df.spawn({ - exec: path.resolve(`${__dirname}/../../src/cli/bin.js`), - config, - initOptions: { bits: 512 } - }) + ipfsdA = await df.spawn() // Without DHT we need to have an already established connection await ipfsdA.api.swarm.connect(bMultiaddr) }) before(() => { this.timeout(60 * 1000) - cli = ipfsExec(ipfsdA.repoPath) + cli = ipfsExec(ipfsdA.path) }) - after(() => { - if (ipfsdA) { - return ipfsdA.stop() - } - }) - after(() => { - if (ipfsdB) { - return ipfsdB.stop() - } - }) + after(() => df.clean()) it('ping host', async () => { this.timeout(60 * 1000) diff --git a/test/cli/pubsub.js b/test/cli/pubsub.js index 5255946ee1..4bf49c7b3a 100644 --- a/test/cli/pubsub.js +++ b/test/cli/pubsub.js @@ -6,23 +6,11 @@ const { expect } = require('interface-ipfs-core/src/utils/mocha') const delay = require('delay') const series = require('async/series') const ipfsExec = require('../utils/ipfs-exec') -const IPFS = require('../../src') -const path = require('path') -const DaemonFactory = require('ipfsd-ctl') - -const config = { - Bootstrap: [], - Discovery: { - MDNS: { - Enabled: - false - } - } -} +const factory = require('../utils/factory') describe('pubsub', function () { this.timeout(80 * 1000) - + const df = factory() let node let ipfsdA let ipfsdB @@ -36,15 +24,7 @@ describe('pubsub', function () { before(async function () { this.timeout(60 * 1000) - const df = DaemonFactory.create({ - type: 'proc', - IpfsClient: require('ipfs-http-client') - }) - ipfsdA = await df.spawn({ - exec: IPFS, - initOptions: { bits: 512 }, - config - }) + ipfsdA = await df.spawn({ type: 'proc' }) node = ipfsdA.api }) @@ -55,17 +35,9 @@ describe('pubsub', function () { }) before(async () => { - const df = DaemonFactory.create({ - type: 'js', - IpfsClient: require('ipfs-http-client') - }) - ipfsdB = await df.spawn({ - initOptions: { bits: 512 }, - exec: path.resolve(`${__dirname}/../../src/cli/bin.js`), - config - }) + ipfsdB = await df.spawn({ type: 'js' }) httpApi = ipfsdB.api - httpApi.repoPath = ipfsdB.repoPath + httpApi.repoPath = ipfsdB.path }) after(() => { diff --git a/test/cli/swarm.js b/test/cli/swarm.js index 2b3b08f017..9fac1ceadd 100644 --- a/test/cli/swarm.js +++ b/test/cli/swarm.js @@ -4,31 +4,15 @@ const { expect } = require('interface-ipfs-core/src/utils/mocha') const sinon = require('sinon') -const ipfsExec = require('../utils/ipfs-exec') -const path = require('path') -const addrsCommand = require('../../src/cli/commands/swarm/addrs') - const multiaddr = require('multiaddr') const PeerInfo = require('peer-info') +const ipfsExec = require('../utils/ipfs-exec') const PeerId = require('peer-id') - -const DaemonFactory = require('ipfsd-ctl') -const df = DaemonFactory.create({ - type: 'js', - IpfsClient: require('ipfs-http-client') -}) - -const config = { - Bootstrap: [], - Discovery: { - MDNS: { - Enabled: - false - } - } -} +const addrsCommand = require('../../src/cli/commands/swarm/addrs') +const factory = require('../utils/factory') describe('swarm', () => { + const df = factory({ type: 'js' }) afterEach(() => { sinon.restore() }) @@ -39,31 +23,21 @@ describe('swarm', () => { let bMultiaddr let ipfsA - const nodes = [] before(async function () { // CI takes longer to instantiate the daemon, so we need to increase the // timeout for the before step this.timeout(80 * 1000) const res = await Promise.all([ - df.spawn({ - exec: path.resolve(`${__dirname}/../../src/cli/bin.js`), - config, - initOptions: { bits: 512 } - }), - df.spawn({ - exec: path.resolve(`${__dirname}/../../src/cli/bin.js`), - config, - initOptions: { bits: 512 } - }) + df.spawn(), + df.spawn() ]) - ipfsA = ipfsExec(res[0].repoPath) + ipfsA = ipfsExec(res[0].path) const id = await res[1].api.id() bMultiaddr = id.addresses[0] - nodes.push(...res) }) - after(() => Promise.all(nodes.map((node) => node.stop()))) + after(() => df.clean()) it('connect', async () => { const out = await ipfsA(`swarm connect ${bMultiaddr}`) diff --git a/test/core/bitswap.spec.js b/test/core/bitswap.spec.js index deee69150e..5ae1cb2960 100644 --- a/test/core/bitswap.spec.js +++ b/test/core/bitswap.spec.js @@ -1,257 +1,91 @@ -/* eslint max-nested-callbacks: ["error", 8] */ /* eslint-env mocha */ 'use strict' const hat = require('hat') +const pmap = require('p-map') const { expect } = require('interface-ipfs-core/src/utils/mocha') -const _ = require('lodash') -const series = require('async/series') -const waterfall = require('async/waterfall') -const parallel = require('async/parallel') const Block = require('ipfs-block') -const multiaddr = require('multiaddr') -const { isNode } = require('ipfs-utils/src/env') const multihashing = require('multihashing-async') const CID = require('cids') -const path = require('path') -const IPFSFactory = require('ipfsd-ctl') -const callbackify = require('callbackify') -const IPFSHTTPClient = require('ipfs-http-client') +const factory = require('../utils/factory') -const IPFS = require('../../src/core') - -function makeBlock (callback) { +const makeBlock = async () => { const d = Buffer.from(`IPFS is awesome ${hat()}`) + const h = await multihashing(d, 'sha2-256') - callbackify(multihashing)(d, 'sha2-256', null, (err, multihash) => { - if (err) { - return callback(err) - } - callback(null, new Block(d, new CID(multihash))) - }) -} - -function wire (targetNode, dialerNode, callback) { - targetNode.id((err, identity) => { - expect(err).to.not.exist() - const addr = identity.addresses - .map((addr) => multiaddr(addr.toString().split('ipfs')[0])) - .filter((addr) => _.includes(addr.protoNames(), 'ws'))[0] - - if (!addr) { - // Note: the browser doesn't have a websockets listening addr - return callback() - } - - const targetAddr = addr - .encapsulate(multiaddr(`/ipfs/${identity.id}`)).toString() - .replace('0.0.0.0', '127.0.0.1') - - dialerNode.swarm.connect(targetAddr, callback) - }) -} - -function connectNodes (remoteNode, inProcNode, callback) { - series([ - (cb) => wire(remoteNode, inProcNode, cb), - // need timeout so we wait for identify to happen. - // This call is just to ensure identify happened - (cb) => setTimeout(() => wire(inProcNode, remoteNode, cb), 500) - ], callback) -} - -let nodes = [] - -function addNode (fDaemon, inProcNode, callback) { - callbackify.variadic(fDaemon.spawn.bind(fDaemon))({ - exec: isNode ? path.resolve(`${__dirname}/../../src/cli/bin.js`) : './src/cli/bin.js', - initOptions: { bits: 512 }, - config: { - Addresses: { - Swarm: ['/ip4/127.0.0.1/tcp/0/ws'] - }, - Discovery: { - MDNS: { - Enabled: false - } - }, - Bootstrap: [] - }, - preload: { enabled: false } - }, (err, ipfsd) => { - expect(err).to.not.exist() - nodes.push(ipfsd) - connectNodes(ipfsd.api, inProcNode, (err) => { - callback(err, ipfsd.api) - }) - }) + return new Block(d, new CID(h)) } describe('bitswap', function () { - this.timeout(80 * 1000) - - let inProcNode // Node spawned inside this process - let fDaemon - let fInProc - - before(function () { - fDaemon = IPFSFactory.create({ - type: 'js', - IpfsClient: require('ipfs-http-client') - }) - fInProc = IPFSFactory.create({ - type: 'proc', - IpfsClient: require('ipfs-http-client') - }) - }) - - beforeEach(async function () { - this.timeout(60 * 1000) - - let config = { - Addresses: { - Swarm: [] - }, - Discovery: { - MDNS: { - Enabled: false - } - }, - Bootstrap: [] - } - - if (isNode) { - config = Object.assign({}, config, { - Addresses: { - Swarm: ['/ip4/127.0.0.1/tcp/0'] - } - }) - } - - const ipfsd = await fInProc.spawn({ - exec: IPFS, - IPFSClient: IPFSHTTPClient, - config: config, - initOptions: { bits: 512 }, - start: true, - init: true - }) - nodes.push(ipfsd) - inProcNode = ipfsd.api - }) - - afterEach(async function () { - this.timeout(80 * 1000) - await Promise.all( - nodes.map((node) => node.stop()) - ) - nodes = [] - }) + this.timeout(60 * 1000) + const df = factory() describe('transfer a block between', () => { - it('2 peers', function (done) { - this.timeout(160 * 1000) - - let remoteNode - let block - waterfall([ - (cb) => parallel([ - (cb) => makeBlock(cb), - (cb) => addNode(fDaemon, inProcNode, cb) - ], cb), - (res, cb) => { - block = res[0] - remoteNode = res[1] - cb() - }, - (cb) => remoteNode.block.put(block, cb), - (key, cb) => inProcNode.block.get(block.cid, cb), - (b, cb) => { - expect(b.data).to.eql(block.data) - cb() - } - ], done) - }) + it('2 peers', async function () { + const remote = (await df.spawn({ type: 'js' })).api + const proc = (await df.spawn({ type: 'proc' })).api + proc.swarm.connect(remote.peerId.addresses[0]) + const block = await makeBlock() - it('3 peers', function (done) { - this.timeout(160 * 1000) + await proc.block.put(block) + const b = await remote.block.get(block.cid) - let blocks - const remoteNodes = [] - - series([ - (cb) => parallel(_.range(6).map((i) => makeBlock), (err, _blocks) => { - expect(err).to.not.exist() - blocks = _blocks - cb() - }), - (cb) => addNode(fDaemon, inProcNode, (err, _ipfs) => { - remoteNodes.push(_ipfs) - cb(err) - }), - (cb) => addNode(fDaemon, inProcNode, (err, _ipfs) => { - remoteNodes.push(_ipfs) - cb(err) - }), - (cb) => connectNodes(remoteNodes[0], remoteNodes[1], cb), - (cb) => remoteNodes[0].block.put(blocks[0], cb), - (cb) => remoteNodes[0].block.put(blocks[1], cb), - (cb) => remoteNodes[1].block.put(blocks[2], cb), - (cb) => remoteNodes[1].block.put(blocks[3], cb), - (cb) => inProcNode.block.put(blocks[4], cb), - (cb) => inProcNode.block.put(blocks[5], cb), - // 3. Fetch blocks on all nodes - (cb) => parallel(_.range(6).map((i) => (cbI) => { - const check = (n, cid, callback) => { - n.block.get(cid, (err, b) => { - expect(err).to.not.exist() - expect(b).to.eql(blocks[i]) - callback() - }) - } + expect(b.data).to.eql(block.data) + await df.clean() + }) - series([ - (cbJ) => check(remoteNodes[0], blocks[i].cid, cbJ), - (cbJ) => check(remoteNodes[1], blocks[i].cid, cbJ), - (cbJ) => check(inProcNode, blocks[i].cid, cbJ) - ], cbI) - }), cb) - ], done) + it('3 peers', async () => { + const blocks = await Promise.all([...Array(6).keys()].map(() => makeBlock())) + const remote1 = (await df.spawn({ type: 'js' })).api + const remote2 = (await df.spawn({ type: 'js' })).api + const proc = (await df.spawn({ type: 'proc' })).api + proc.swarm.connect(remote1.peerId.addresses[0]) + proc.swarm.connect(remote2.peerId.addresses[0]) + remote1.swarm.connect(remote2.peerId.addresses[0]) + + await remote1.block.put(blocks[0]) + await remote1.block.put(blocks[1]) + await remote2.block.put(blocks[2]) + await remote2.block.put(blocks[3]) + await proc.block.put(blocks[4]) + await proc.block.put(blocks[5]) + + await pmap(blocks, async (block) => { + expect(await remote1.block.get(block.cid)).to.eql(block) + expect(await remote2.block.get(block.cid)).to.eql(block) + expect(await proc.block.get(block.cid)).to.eql(block) + }, { concurrency: 3 }) + await df.clean() }) }) - describe('transfer a file between', function () { - this.timeout(160 * 1000) - - it('2 peers', (done) => { + describe('transfer a file between', () => { + it('2 peers', async () => { // TODO make this test more interesting (10Mb file) // TODO remove randomness from the test const file = Buffer.from(`I love IPFS <3 ${hat()}`) - - waterfall([ - // 0. Start node - (cb) => addNode(fDaemon, inProcNode, cb), - // 1. Add file to tmp instance - (remote, cb) => { - remote.add([{ path: 'awesome.txt', content: file }], cb) - }, - // 2. Request file from local instance - (filesAdded, cb) => inProcNode.cat(filesAdded[0].hash, cb) - ], (err, data) => { - expect(err).to.not.exist() - expect(data).to.eql(file) - done() - }) + const remote = (await df.spawn({ type: 'js' })).api + const proc = (await df.spawn({ type: 'proc' })).api + proc.swarm.connect(remote.peerId.addresses[0]) + + const files = await remote.add([{ path: 'awesome.txt', content: file }]) + const data = await proc.cat(files[0].hash) + expect(data).to.eql(file) + await df.clean() }) }) describe('unwant', () => { - it('should callback with error for invalid CID input', (done) => { - inProcNode.bitswap.unwant('INVALID CID', (err) => { + it('should callback with error for invalid CID input', async () => { + const proc = (await df.spawn({ type: 'proc' })).api + try { + await proc.bitswap.unwant('INVALID CID') + } catch (err) { expect(err).to.exist() expect(err.code).to.equal('ERR_INVALID_CID') - done() - }) + } finally { + await df.clean() + } }) }) }) diff --git a/test/core/block.spec.js b/test/core/block.spec.js index 68d11fc66d..06f71f8054 100644 --- a/test/core/block.spec.js +++ b/test/core/block.spec.js @@ -4,33 +4,17 @@ const { expect } = require('interface-ipfs-core/src/utils/mocha') const hat = require('hat') - -const IPFSFactory = require('ipfsd-ctl') -const IPFS = require('../../src/core') +const factory = require('../utils/factory') describe('block', () => { - let ipfsd, ipfs - - before(async function () { - const factory = IPFSFactory.create({ - type: 'proc', - IpfsClient: require('ipfs-http-client') - }) + let ipfs + const df = factory() - ipfsd = await factory.spawn({ - exec: IPFS, - initOptions: { bits: 512 }, - config: { Bootstrap: [] }, - preload: { enabled: false } - }) - ipfs = ipfsd.api + before(async () => { + ipfs = (await df.spawn()).api }) - after(() => { - if (ipfsd) { - return ipfsd.stop() - } - }) + after(() => df.clean()) describe('get', () => { it('should callback with error for invalid CID input', (done) => { diff --git a/test/core/bootstrap.spec.js b/test/core/bootstrap.spec.js index 566ffa3b7e..97ef5f7dfe 100644 --- a/test/core/bootstrap.spec.js +++ b/test/core/bootstrap.spec.js @@ -2,43 +2,19 @@ 'use strict' const { expect } = require('interface-ipfs-core/src/utils/mocha') -const isNode = require('detect-node') -const IPFS = require('../../src') - -const DaemonFactory = require('ipfsd-ctl') -const df = DaemonFactory.create({ - type: 'proc', - IpfsClient: require('ipfs-http-client') -}) +const factory = require('../utils/factory') +const { isBrowser, isWebWorker } = require('ipfs-utils/src/env') describe('bootstrap', () => { - if (!isNode) { - return - } + const df = factory() let node - let ipfsd - before(async function () { - this.timeout(40 * 1000) - ipfsd = await df.spawn({ - exec: IPFS, - initOptions: { bits: 512 }, - config: { - Addresses: { - Swarm: ['/ip4/127.0.0.1/tcp/0'] - } - }, - preload: { enabled: false } - }) - node = ipfsd.api + before(async () => { + node = (await df.spawn({ test: false })).api }) - after(() => { - if (ipfsd) { - return ipfsd.stop() - } - }) + after(() => df.clean()) const defaultList = [ '/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z', @@ -61,60 +37,40 @@ describe('bootstrap', () => { '/dns4/node0.preload.ipfs.io/tcp/443/wss/ipfs/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic', '/dns4/node1.preload.ipfs.io/tcp/443/wss/ipfs/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6' ] + const browserList = ['/dns4/ams-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd', '/dns4/lon-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3', '/dns4/sfo-3.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM', '/dns4/sgp-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu', '/dns4/nyc-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm', '/dns4/nyc-2.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64', '/dns4/node0.preload.ipfs.io/tcp/443/wss/ipfs/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic', '/dns4/node1.preload.ipfs.io/tcp/443/wss/ipfs/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6'] - const updatedList = [ - '/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z', - '/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ', - '/ip4/104.236.179.241/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM', - '/ip4/162.243.248.213/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm', - '/ip4/128.199.219.111/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu', - '/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64', - '/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd', - '/ip4/178.62.61.185/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3', - '/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx', - '/ip6/2604:a880:1:20::1f9:9001/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z', - '/ip6/2604:a880:1:20::203:d001/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM', - '/ip6/2604:a880:0:1010::23:d001/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm', - '/ip6/2400:6180:0:d0::151:6001/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu', - '/ip6/2604:a880:800:10::4a:5001/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64', - '/ip6/2a03:b0c0:0:1010::23:1001/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd', - '/ip6/2a03:b0c0:1:d0::e7:1/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3', - '/ip6/2604:a880:1:20::1d9:6001/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx', - '/dns4/node0.preload.ipfs.io/tcp/443/wss/ipfs/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic', - '/dns4/node1.preload.ipfs.io/tcp/443/wss/ipfs/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6', - '/ip4/111.111.111.111/tcp/1001/ipfs/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb' - ] - - it('get bootstrap list', (done) => { - node.bootstrap.list((err, list) => { - expect(err).to.not.exist() + it('get bootstrap list', async () => { + const list = await node.bootstrap.list() + if (isBrowser || isWebWorker) { + expect(list.Peers).to.deep.equal(browserList) + } else { expect(list.Peers).to.deep.equal(defaultList) - done() - }) + } }) - it('add a peer to the bootstrap list', (done) => { - node.bootstrap.add('/ip4/111.111.111.111/tcp/1001/ipfs/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb', (err, res) => { - expect(err).to.not.exist() - expect(res).to.be.eql({ Peers: ['/ip4/111.111.111.111/tcp/1001/ipfs/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb'] }) - node.bootstrap.list((err, list) => { - expect(err).to.not.exist() - expect(list.Peers).to.deep.equal(updatedList) - done() - }) - }) + it('add a peer to the bootstrap list', async () => { + const peer = '/ip4/111.111.111.111/tcp/1001/ipfs/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb' + const res = await node.bootstrap.add(peer) + expect(res).to.be.eql({ Peers: [peer] }) + const list = await node.bootstrap.list() + + if (isBrowser || isWebWorker) { + expect(list.Peers).to.deep.equal(browserList.concat([peer])) + } else { + expect(list.Peers).to.deep.equal(defaultList.concat([peer])) + } }) - it('remove a peer from the bootstrap list', (done) => { - node.bootstrap.rm('/ip4/111.111.111.111/tcp/1001/ipfs/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb', (err, res) => { - expect(err).to.not.exist() - expect(res).to.be.eql({ Peers: ['/ip4/111.111.111.111/tcp/1001/ipfs/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb'] }) - node.bootstrap.list((err, list) => { - expect(err).to.not.exist() - expect(list.Peers).to.deep.equal(defaultList) - done() - }) - }) + it('remove a peer from the bootstrap list', async () => { + const peer = '/ip4/111.111.111.111/tcp/1001/ipfs/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb' + const res = await node.bootstrap.rm(peer) + expect(res).to.be.eql({ Peers: [peer] }) + const list = await node.bootstrap.list() + if (isBrowser || isWebWorker) { + expect(list.Peers).to.deep.equal(browserList) + } else { + expect(list.Peers).to.deep.equal(defaultList) + } }) it('fails if passing in a invalid multiaddr', (done) => { diff --git a/test/core/circuit-relay.js b/test/core/circuit-relay.spec.js similarity index 56% rename from test/core/circuit-relay.js rename to test/core/circuit-relay.spec.js index 751f382bd3..cc57952670 100644 --- a/test/core/circuit-relay.js +++ b/test/core/circuit-relay.spec.js @@ -6,56 +6,31 @@ const { expect } = require('interface-ipfs-core/src/utils/mocha') const waterfall = require('async/waterfall') const multiaddr = require('multiaddr') const crypto = require('crypto') -const IPFS = require('../../src') - -const DaemonFactory = require('ipfsd-ctl') -const procDf = DaemonFactory.create({ - type: 'proc', - exec: IPFS, - IpfsClient: require('ipfs-http-client') -}) - -const baseConf = { - Bootstrap: [], - Addresses: { - API: '/ip4/0.0.0.0/tcp/0', - Gateway: '/ip4/0.0.0.0/tcp/0' - }, - Discovery: { - MDNS: { - Enabled: - false - } - } -} - -const setupInProcNode = async (addrs, hop) => { - const ipfsd = await procDf.spawn({ - libp2p: { - config: { - relay: { - enabled: true, - hop: { - enabled: hop +const factory = require('../utils/factory') + +const df = factory() + +const setupInProcNode = async (type = 'proc', hop) => { + const ipfsd = await df.spawn({ + type, + ipfsOptions: { + libp2p: { + config: { + relay: { + enabled: true, + hop: { + enabled: hop + } } } } - }, - config: Object.assign({}, baseConf, { - Addresses: { - Swarm: addrs - } - }), - preload: { enabled: false } + } }) const id = await ipfsd.api.id() return { ipfsd, addrs: id.addresses } } -const wsAddr = (addrs) => addrs.map((a) => a.toString()).find((a) => a.includes('/ws')) -const tcpAddr = (addrs) => addrs.map((a) => a.toString()).find((a) => !a.includes('/ws')) - describe('circuit relay', () => { describe('A <-> R <-> B', function () { this.timeout(80 * 1000) @@ -68,24 +43,19 @@ describe('circuit relay', () => { let relayNode - let nodes before('create and connect', async () => { const res = await Promise.all([ - setupInProcNode([ - '/ip4/0.0.0.0/tcp/0', - '/ip4/0.0.0.0/tcp/0/ws' - ], true), - setupInProcNode(['/ip4/0.0.0.0/tcp/0']), - setupInProcNode(['/ip4/0.0.0.0/tcp/0/ws']) + setupInProcNode('proc', true), + setupInProcNode('js'), + setupInProcNode('js') ]) - nodes = res.map((node) => node.ipfsd) relayNode = res[0].ipfsd - nodeAAddr = tcpAddr(res[1].addrs) + nodeAAddr = res[1].addrs[0] nodeA = res[1].ipfsd.api - nodeBAddr = wsAddr(res[2].addrs) + nodeBAddr = res[2].addrs[0] nodeB = res[2].ipfsd.api nodeBCircuitAddr = `/p2p-circuit/ipfs/${multiaddr(nodeBAddr).getPeerId()}` @@ -101,7 +71,7 @@ describe('circuit relay', () => { await nodeA.swarm.connect(nodeBCircuitAddr) }) - after(() => Promise.all(nodes.map((node) => node.stop()))) + after(() => df.clean()) it('should transfer', function (done) { const data = crypto.randomBytes(128) diff --git a/test/core/dag.spec.js b/test/core/dag.spec.js index a9c184a2cd..d45419a09b 100644 --- a/test/core/dag.spec.js +++ b/test/core/dag.spec.js @@ -3,33 +3,18 @@ 'use strict' const { expect } = require('interface-ipfs-core/src/utils/mocha') -const IPFSFactory = require('ipfsd-ctl') -const IPFS = require('../../src/core') +const factory = require('../utils/factory') describe('dag', function () { this.timeout(10 * 1000) - let ipfsd, ipfs + const df = factory() + let ipfs before(async () => { - const factory = IPFSFactory.create({ - type: 'proc', - IpfsClient: require('ipfs-http-client') - }) - - ipfsd = await factory.spawn({ - exec: IPFS, - initOptions: { bits: 512 }, - config: { Bootstrap: [] }, - preload: { enabled: false } - }) - ipfs = ipfsd.api + ipfs = (await df.spawn()).api }) - after(() => { - if (ipfsd) { - return ipfsd.stop() - } - }) + after(() => df.clean()) describe('get', () => { it('should callback with error for invalid string CID input', (done) => { diff --git a/test/core/dht.spec.js b/test/core/dht.spec.js index b2e5c2d04a..751c860228 100644 --- a/test/core/dht.spec.js +++ b/test/core/dht.spec.js @@ -5,36 +5,22 @@ const { expect } = require('interface-ipfs-core/src/utils/mocha') const isNode = require('detect-node') -const IPFSFactory = require('ipfsd-ctl') -const IPFS = require('../../src/core') +const factory = require('../utils/factory') // TODO: unskip when DHT is enabled: https://github.com/ipfs/js-ipfs/pull/1994 describe.skip('dht', () => { describe('enabled', () => { + const df = factory() let ipfsd, ipfs before(async function () { this.timeout(30 * 1000) - const factory = IPFSFactory.create({ - type: 'proc', - IpfsClient: require('ipfs-http-client') - }) - - ipfsd = await factory.spawn({ - exec: IPFS, - initOptions: { bits: 512 }, - config: { Bootstrap: [] }, - preload: { enabled: false } - }) + ipfsd = await df.spawn() ipfs = ipfsd.api }) - after(() => { - if (ipfsd) { - return ipfsd.stop() - } - }) + after(() => df.clean()) describe('findprovs', () => { it('should callback with error for invalid CID input', (done) => { @@ -49,29 +35,17 @@ describe.skip('dht', () => { describe('disabled in browser', () => { if (isNode) { return } - + const df = factory() let ipfsd, ipfs before(async function (done) { this.timeout(30 * 1000) - const factory = IPFSFactory.create({ type: 'proc' }) - - ipfsd = await factory.spawn({ - exec: IPFS, - initOptions: { bits: 512 }, - config: { - Bootstrap: [] - } - }) + ipfsd = await df.spawn() ipfs = ipfsd.api }) - after(() => { - if (ipfsd) { - return ipfsd.stop() - } - }) + after(() => df.clean()) describe('put', () => { it('should error when DHT not available', async () => { diff --git a/test/core/files-sharding.spec.js b/test/core/files-sharding.spec.js index 2d82834f7f..ae2921879f 100644 --- a/test/core/files-sharding.spec.js +++ b/test/core/files-sharding.spec.js @@ -4,16 +4,11 @@ const { expect } = require('interface-ipfs-core/src/utils/mocha') const pull = require('pull-stream') +const factory = require('../utils/factory') -const IPFS = require('../../src/core') - -const DaemonFactory = require('ipfsd-ctl') -const df = DaemonFactory.create({ - type: 'proc', - IpfsClient: require('ipfs-http-client') -}) - -describe('files directory (sharding tests)', () => { +describe('files directory (sharding tests)', function () { + this.timeout(40 * 1000) + const df = factory() function createTestFiles () { const files = [] @@ -32,33 +27,11 @@ describe('files directory (sharding tests)', () => { let ipfsd before(async function () { - this.timeout(40 * 1000) - - ipfsd = await df.spawn({ - exec: IPFS, - initOptions: { bits: 512 }, - config: { - Addresses: { - Swarm: [] - }, - Bootstrap: [], - Discovery: { - MDNS: { - Enabled: false - } - } - }, - preload: { enabled: false } - }) + ipfsd = await df.spawn() ipfs = ipfsd.api }) - after(function () { - if (ipfsd) { - this.timeout(40 * 1000) - return ipfsd.stop() - } - }) + after(() => df.clean()) it('should be able to add dir without sharding', function (done) { this.timeout(70 * 1000) @@ -82,38 +55,15 @@ describe('files directory (sharding tests)', () => { let ipfsd before(async function () { - this.timeout(40 * 1000) - ipfsd = await df.spawn({ - exec: IPFS, - initOptions: { bits: 512 }, - args: ['--enable-sharding-experiment'], - config: { - Addresses: { - Swarm: [] - }, - Bootstrap: [], - Discovery: { - MDNS: { - Enabled: false - } - } - }, - preload: { enabled: false } + ipfsOptions: { EXPERIMENTAL: { sharding: true } } }) ipfs = ipfsd.api }) - after(function () { - if (ipfsd) { - this.timeout(40 * 1000) - return ipfsd.stop() - } - }) + after(() => df.clean()) it('should be able to add dir with sharding', function (done) { - this.timeout(80 * 1000) - pull( pull.values(createTestFiles()), ipfs.addPullStream(), diff --git a/test/core/files.spec.js b/test/core/files.spec.js index ea8ca2380a..260995defe 100644 --- a/test/core/files.spec.js +++ b/test/core/files.spec.js @@ -5,33 +5,19 @@ const { expect } = require('interface-ipfs-core/src/utils/mocha') const hat = require('hat') const pull = require('pull-stream') -const IPFSFactory = require('ipfsd-ctl') -const IPFS = require('../../src/core') +const factory = require('../utils/factory') describe('files', function () { this.timeout(10 * 1000) + const df = factory() let ipfsd, ipfs before(async () => { - const factory = IPFSFactory.create({ - type: 'proc', - IpfsClient: require('ipfs-http-client') - }) - - ipfsd = await factory.spawn({ - exec: IPFS, - initOptions: { bits: 512 }, - config: { Bootstrap: [] }, - preload: { enabled: false } - }) + ipfsd = await df.spawn() ipfs = ipfsd.api }) - after(() => { - if (ipfsd) { - return ipfsd.stop() - } - }) + after(() => df.clean()) describe('get', () => { it('should callback with error for invalid IPFS path input', (done) => { diff --git a/test/core/gc.spec.js b/test/core/gc.spec.js index fef9c208bb..d9b929972e 100644 --- a/test/core/gc.spec.js +++ b/test/core/gc.spec.js @@ -3,10 +3,8 @@ 'use strict' const { expect } = require('interface-ipfs-core/src/utils/mocha') -const IPFSFactory = require('ipfsd-ctl') +const factory = require('../utils/factory') const pEvent = require('p-event') -const env = require('ipfs-utils/src/env') -const IPFS = require('../../src/core') // We need to detect when a readLock or writeLock is requested for the tests // so we override the Mutex class to emit an event @@ -38,6 +36,7 @@ class MutexEmitter extends Mutex { describe('gc', function () { this.timeout(40 * 1000) + const df = factory() const fixtures = [{ path: 'test/my/path1', content: Buffer.from('path1') @@ -57,20 +56,7 @@ describe('gc', function () { let lockEmitter before(async function () { - const factory = IPFSFactory.create({ - type: 'proc', - exec: IPFS, - IpfsClient: require('ipfs-http-client') - }) - const config = { Bootstrap: [] } - - if (env.isNode) { - config.Addresses = { - Swarm: ['/ip4/127.0.0.1/tcp/0'] - } - } - - ipfsd = await factory.spawn({ config }) + ipfsd = await df.spawn() ipfs = ipfsd.api // Replace the Mutex with one that emits events when a readLock or @@ -79,11 +65,7 @@ describe('gc', function () { lockEmitter = ipfs._gcLock.mutex.emitter }) - after(() => { - if (ipfsd) { - return ipfsd.stop() - } - }) + after(() => df.clean()) const blockAddTests = [{ name: 'add', diff --git a/test/core/interface.spec.js b/test/core/interface.spec.js index bfc0cb6508..12bb06f148 100644 --- a/test/core/interface.spec.js +++ b/test/core/interface.spec.js @@ -2,46 +2,59 @@ 'use strict' const tests = require('interface-ipfs-core') -const CommonFactory = require('../utils/interface-common-factory') -const isNode = require('detect-node') +const { isNode } = require('ipfs-utils/src/env') +const merge = require('merge-options') +const { createFactory } = require('ipfsd-ctl') +const IPFS = require('../../src') -describe('interface-ipfs-core tests', function () { - this.timeout(20 * 1000) +/** @typedef { import("ipfsd-ctl").ControllerOptions } ControllerOptions */ - const defaultCommonFactory = CommonFactory.createAsync() +describe('interface-ipfs-core tests', function () { + /** @type ControllerOptions */ + const commonOptions = { + test: true, + type: 'proc', + ipfsModule: { + path: require.resolve('../../src'), + ref: IPFS + }, + ipfsHttpModule: { + path: require.resolve('ipfs-http-client'), + ref: require('ipfs-http-client') + }, + ipfsOptions: { + pass: 'ipfs-is-awesome-software' + } + } + const overrides = { + js: { + ipfsBin: './src/cli/bin.js' + } + } + const commonFactory = createFactory(commonOptions, overrides) - tests.bitswap(defaultCommonFactory, { skip: !isNode }) + tests.bitswap(commonFactory, { + skip: isNode ? null : [{ + name: 'should get the wantlist by peer ID for a different node', + reason: 'FIXME: Does not find wantlist list in the browser' + }] + }) - tests.block(defaultCommonFactory) + tests.block(commonFactory) - tests.bootstrap(defaultCommonFactory) + tests.bootstrap(commonFactory) - tests.config(defaultCommonFactory) + tests.config(commonFactory) - tests.dag(defaultCommonFactory) + tests.dag(commonFactory) - tests.dht(CommonFactory.createAsync({ - spawnOptions: { - config: { - Bootstrap: [], - Discovery: { - MDNS: { - Enabled: false - }, - webRTCStar: { - Enabled: false - } - } - }, - initOptions: { bits: 512 } - } - }), { + tests.dht(commonFactory, { skip: { reason: 'TODO: unskip when DHT is enabled: https://github.com/ipfs/js-ipfs/pull/1994' } }) - tests.filesRegular(defaultCommonFactory, { + tests.filesRegular(commonFactory, { skip: isNode ? null : [{ name: 'addFromStream', reason: 'Not designed to run in the browser' @@ -51,57 +64,27 @@ describe('interface-ipfs-core tests', function () { }] }) - tests.filesMFS(defaultCommonFactory) - - tests.key(CommonFactory.createAsync({ - spawnOptions: { - args: ['--pass ipfs-is-awesome-software'], - initOptions: { bits: 512 }, - config: { - Bootstrap: [], - Discovery: { - MDNS: { - Enabled: false - }, - webRTCStar: { - Enabled: false - } - } - } - } - })) + tests.filesMFS(commonFactory) - tests.miscellaneous(CommonFactory.createAsync({ - spawnOptions: { - args: ['--pass ipfs-is-awesome-software', '--offline'] - } - })) + tests.key(commonFactory) + + tests.miscellaneous(commonFactory) - tests.name(CommonFactory.createAsync({ - spawnOptions: { - args: ['--pass ipfs-is-awesome-software', '--offline'] + tests.name(createFactory(merge(commonOptions, { + ipfsOptions: { + offline: true } - })) - - tests.namePubsub(CommonFactory.createAsync({ - spawnOptions: { - args: ['--enable-namesys-pubsub'], - initOptions: { bits: 1024 }, - config: { - Bootstrap: [], - Discovery: { - MDNS: { - Enabled: false - }, - webRTCStar: { - Enabled: false - } - } + }), overrides)) + + tests.namePubsub(createFactory(merge(commonOptions, { + ipfsOptions: { + EXPERIMENTAL: { + ipnsPubsub: true } } - })) + }), overrides)) - tests.object(defaultCommonFactory, { + tests.object(commonFactory, { skip: [ { name: 'should respect timeout option', @@ -110,27 +93,38 @@ describe('interface-ipfs-core tests', function () { ] }) - tests.pin(defaultCommonFactory) + tests.pin(commonFactory) - tests.ping(defaultCommonFactory, { - skip: isNode ? null : { - reason: 'FIXME: ping implementation requires DHT' - } - }) + tests.ping(commonFactory) - tests.pubsub(CommonFactory.createAsync({ - spawnOptions: { - initOptions: { bits: 512 } - } - }), { - skip: isNode ? null : { - reason: 'FIXME: disabled because no swarm addresses' + tests.pubsub(createFactory(commonOptions, merge(overrides, { + go: { + args: ['--enable-pubsub-experiment'] } + })), { + skip: [ + { + name: 'should receive messages from a different node', + reason: 'https://github.com/ipfs/js-ipfs/issues/2662' + }, + { + name: 'should round trip a non-utf8 binary buffer', + reason: 'https://github.com/ipfs/js-ipfs/issues/2662' + }, + { + name: 'should receive multiple messages', + reason: 'https://github.com/ipfs/js-ipfs/issues/2662' + }, + { + name: 'should send/receive 100 messages', + reason: 'https://github.com/ipfs/js-ipfs/issues/2662' + } + ] }) - tests.repo(defaultCommonFactory) + tests.repo(commonFactory) - tests.stats(defaultCommonFactory) + tests.stats(commonFactory) - tests.swarm(defaultCommonFactory, { skip: !isNode }) + tests.swarm(commonFactory) }) diff --git a/test/core/key-exchange.js b/test/core/key-exchange.spec.js similarity index 57% rename from test/core/key-exchange.js rename to test/core/key-exchange.spec.js index 47910edb71..7c8f317ebc 100644 --- a/test/core/key-exchange.js +++ b/test/core/key-exchange.spec.js @@ -4,31 +4,24 @@ const { expect } = require('interface-ipfs-core/src/utils/mocha') const hat = require('hat') -const IPFS = require('../../src/core') +const factory = require('../utils/factory') -// This gets replaced by `create-repo-browser.js` in the browser -const createTempRepo = require('../utils/create-repo-nodejs.js') - -describe('key exchange', () => { +describe('key exchange', function () { + this.timeout(20 * 1000) + const df = factory() let ipfs - let repo let selfPem const passwordPem = hat() - before(function (done) { - this.timeout(20 * 1000) - repo = createTempRepo() - ipfs = new IPFS({ - repo: repo, - pass: hat(), - preload: { enabled: false } - }) - ipfs.on('ready', () => done()) + before(async () => { + ipfs = (await df.spawn({ + ipfsOptions: { + pass: hat() + } + })).api }) - after((done) => ipfs.stop(done)) - - after((done) => repo.teardown(done)) + after(() => df.clean()) it('exports', (done) => { ipfs.key.export('self', passwordPem, (err, pem) => { @@ -40,8 +33,6 @@ describe('key exchange', () => { }) it('imports', function (done) { - this.timeout(20 * 1000) - ipfs.key.import('clone', selfPem, passwordPem, (err, key) => { expect(err).to.not.exist() expect(key).to.exist() diff --git a/test/core/name-pubsub.js b/test/core/name-pubsub.js index 9d886d461a..43485c1e4d 100644 --- a/test/core/name-pubsub.js +++ b/test/core/name-pubsub.js @@ -9,21 +9,18 @@ const { fromB58String } = require('multihashes') const peerId = require('peer-id') const isNode = require('detect-node') const ipns = require('ipns') -const IPFS = require('../../src') const waitFor = require('../utils/wait-for') const delay = require('delay') const promisify = require('promisify-es6') -const DaemonFactory = require('ipfsd-ctl') -const df = DaemonFactory.create({ - type: 'proc', - IpfsClient: require('ipfs-http-client') -}) +const factory = require('../utils/factory') const namespace = '/record/' const ipfsRef = '/ipfs/QmPFVLPmp9zv5Z5KUqLhe2EivAGccQW2r7M7jhVJGLZoZU' describe('name-pubsub', function () { + const df = factory() + // TODO make this work in the browser and between daemon and in-proc in nodess if (!isNode) { return } @@ -34,29 +31,12 @@ describe('name-pubsub', function () { let idA let idB - const createNode = () => df.spawn({ - exec: IPFS, - args: [`--pass ${hat()}`, '--enable-namesys-pubsub'], - config: { - Bootstrap: [], - Discovery: { - MDNS: { - Enabled: false - }, - webRTCStar: { - Enabled: false - } - } - }, - preload: { enabled: false } - }) - before(async function () { this.timeout(40 * 1000) nodes = await Promise.all([ - createNode(), - createNode() + df.spawn({ type: 'proc', ipfsOptions: { pass: hat(), EXPERIMENTAL: { ipnsPubsub: true } } }), + df.spawn({ type: 'proc', ipfsOptions: { pass: hat(), EXPERIMENTAL: { ipnsPubsub: true } } }) ]) nodeA = nodes[0].api @@ -73,7 +53,7 @@ describe('name-pubsub', function () { await nodeA.swarm.connect(idB.addresses[0]) }) - after(() => Promise.all(nodes.map((node) => node.stop()))) + after(() => df.clean()) it('should publish and then resolve correctly', async function () { this.timeout(80 * 1000) diff --git a/test/core/name.spec.js b/test/core/name.spec.js index 64e143d5e8..24a148a343 100644 --- a/test/core/name.spec.js +++ b/test/core/name.spec.js @@ -15,11 +15,7 @@ const PubsubDatastore = require('../../src/core/ipns/routing/pubsub-datastore') const { Key, Errors } = require('interface-datastore') const CID = require('cids') -const DaemonFactory = require('ipfsd-ctl') -const df = DaemonFactory.create({ - type: 'proc', - IpfsClient: require('ipfs-http-client') -}) +const factory = require('../utils/factory') const ipfsRef = '/ipfs/QmPFVLPmp9zv5Z5KUqLhe2EivAGccQW2r7M7jhVJGLZoZU' @@ -37,6 +33,7 @@ const publishAndResolve = (publisher, resolver, ipfsRef, publishOpts, nodeId, re } describe('name', function () { + const df = factory() describe('republisher', function () { this.timeout(40 * 1000) let node @@ -44,10 +41,10 @@ describe('name', function () { before(async function () { ipfsd = await df.spawn({ - exec: IPFS, - args: [`--pass ${hat()}`, '--offline'], - config: { Bootstrap: [] }, - preload: { enabled: false } + ipfsOptions: { + pass: hat(), + offline: true + } }) node = ipfsd.api }) @@ -56,11 +53,7 @@ describe('name', function () { sinon.restore() }) - after(() => { - if (ipfsd) { - return ipfsd.stop() - } - }) + after(() => df.clean()) it('should republish entries after 60 seconds', function (done) { this.timeout(120 * 1000) @@ -88,7 +81,6 @@ describe('name', function () { // TODO: unskip when DHT is enabled: https://github.com/ipfs/js-ipfs/pull/1994 describe.skip('work with dht', () => { - let nodes let nodeA let nodeB let nodeC @@ -97,7 +89,7 @@ describe('name', function () { const createNode = (callback) => { df.spawn({ exec: IPFS, - args: [`--pass ${hat()}`], + args: ['--pass', hat()], config: { Bootstrap: [], Discovery: { @@ -122,7 +114,6 @@ describe('name', function () { ], (err, _nodes) => { expect(err).to.not.exist() - nodes = _nodes nodeA = _nodes[0].api nodeB = _nodes[1].api nodeC = _nodes[2].api @@ -143,11 +134,7 @@ describe('name', function () { }) }) - after(function (done) { - this.timeout(80 * 1000) - - parallel(nodes.map((node) => (cb) => node.stop(cb)), done) - }) + after(() => df.clean()) it('should publish and then resolve correctly with the default options', function (done) { this.timeout(380 * 1000) @@ -182,20 +169,9 @@ describe('name', function () { before(async function () { this.timeout(40 * 1000) ipfsd = await df.spawn({ - exec: IPFS, - args: [`--pass ${hat()}`], - config: { - Bootstrap: [], - Discovery: { - MDNS: { - Enabled: false - }, - webRTCStar: { - Enabled: false - } - } - }, - preload: { enabled: false } + ipfsOptions: { + pass: hat() + } }) node = ipfsd.api @@ -203,11 +179,7 @@ describe('name', function () { nodeId = res.id }) - after(() => { - if (ipfsd) { - return ipfsd.stop() - } - }) + after(() => df.clean()) it('should error to publish if does not receive private key', function () { return expect(node._ipns.publisher.publish(null, ipfsRef)) @@ -329,20 +301,10 @@ describe('name', function () { before(async function () { this.timeout(40 * 1000) ipfsd = await df.spawn({ - exec: IPFS, - args: [`--pass ${hat()}`, '--offline'], - config: { - Bootstrap: [], - Discovery: { - MDNS: { - Enabled: false - }, - webRTCStar: { - Enabled: false - } - } - }, - preload: { enabled: false } + ipfsOptions: { + pass: hat(), + offline: true + } }) node = ipfsd.api @@ -350,11 +312,7 @@ describe('name', function () { nodeId = res.id }) - after(() => { - if (ipfsd) { - return ipfsd.stop() - } - }) + after(() => df.clean()) it('should resolve an ipfs path correctly', async function () { const res = await node.add(fixture) diff --git a/test/core/node.js b/test/core/node.js index 35f185f1d3..427fd4ad17 100644 --- a/test/core/node.js +++ b/test/core/node.js @@ -1,10 +1,7 @@ 'use strict' -require('./circuit-relay') require('./files-regular-utils') require('./name-pubsub') -require('./key-exchange') require('./pin') require('./pin-set') -// require('./key-exchange') require('./utils') diff --git a/test/core/object.spec.js b/test/core/object.spec.js index 0cafe32f98..dec32a7669 100644 --- a/test/core/object.spec.js +++ b/test/core/object.spec.js @@ -4,35 +4,21 @@ const { expect } = require('interface-ipfs-core/src/utils/mocha') const hat = require('hat') -const IPFSFactory = require('ipfsd-ctl') +const factory = require('../utils/factory') const auto = require('async/auto') const waterfall = require('async/waterfall') -const IPFS = require('../../src/core') describe('object', function () { this.timeout(10 * 1000) + const df = factory() let ipfsd, ipfs before(async function () { - const factory = IPFSFactory.create({ - type: 'proc', - IpfsClient: require('ipfs-http-client') - }) - - ipfsd = await factory.spawn({ - exec: IPFS, - initOptions: { bits: 512 }, - config: { Bootstrap: [] }, - preload: { enabled: false } - }) + ipfsd = await df.spawn() ipfs = ipfsd.api }) - after(() => { - if (ipfsd) { - return ipfsd.stop() - } - }) + after(() => df.clean()) describe('get', () => { it('should callback with error for invalid CID input', (done) => { diff --git a/test/core/pin.spec.js b/test/core/pin.spec.js index 9d55f24aa0..fa651f0c14 100644 --- a/test/core/pin.spec.js +++ b/test/core/pin.spec.js @@ -3,33 +3,19 @@ 'use strict' const { expect } = require('interface-ipfs-core/src/utils/mocha') -const IPFSFactory = require('ipfsd-ctl') -const IPFS = require('../../src/core') +const factory = require('../utils/factory') describe('pin', function () { this.timeout(10 * 1000) + const df = factory() let ipfsd, ipfs before(async () => { - const factory = IPFSFactory.create({ - type: 'proc', - IpfsClient: require('ipfs-http-client') - }) - - ipfsd = await factory.spawn({ - exec: IPFS, - initOptions: { bits: 512 }, - config: { Bootstrap: [] }, - preload: { enabled: false } - }) + ipfsd = await df.spawn() ipfs = ipfsd.api }) - after(() => { - if (ipfsd) { - return ipfsd.stop() - } - }) + after(() => df.clean()) describe('ls', () => { it('should callback with error for invalid non-string pin type option', (done) => { diff --git a/test/core/ping.spec.js b/test/core/ping.spec.js index e6227503ce..67c5e81159 100644 --- a/test/core/ping.spec.js +++ b/test/core/ping.spec.js @@ -5,40 +5,7 @@ const { expect } = require('interface-ipfs-core/src/utils/mocha') const pull = require('pull-stream/pull') const drain = require('pull-stream/sinks/drain') const parallel = require('async/parallel') -const DaemonFactory = require('ipfsd-ctl') -const isNode = require('detect-node') -const path = require('path') - -const df = DaemonFactory.create({ - exec: path.resolve(`${__dirname}/../../src/cli/bin.js`), - IpfsClient: require('ipfs-http-client') -}) -const dfProc = DaemonFactory.create({ - exec: require('../../'), - type: 'proc', - IpfsClient: require('ipfs-http-client') -}) - -const config = { - Bootstrap: [], - Discovery: { - MDNS: { - Enabled: - false - } - } -} - -const spawnNode = ({ dht = false, type = 'js' }) => { - const args = dht ? [] : ['--offline'] - const factory = type === 'js' ? df : dfProc - - return factory.spawn({ - args, - config, - preload: { enabled: false } - }) -} +const factory = require('../utils/factory') // Determine if a ping response object is a pong, or something else, like a status message function isPong (pingResponse) { @@ -47,8 +14,7 @@ function isPong (pingResponse) { describe('ping', function () { this.timeout(60 * 1000) - - if (!isNode) return + const df = factory() describe('in-process daemon', function () { let ipfsdA @@ -58,40 +24,14 @@ describe('ping', function () { // Spawn nodes before(async function () { - this.timeout(60 * 1000) - - ipfsdA = await spawnNode({ dht: false, type: 'proc' }) - ipfsdB = await spawnNode({ dht: false }) - }) - - // Get the peer info object - before(async function () { - this.timeout(60 * 1000) - - const peerInfo = await ipfsdB.api.id() - - ipfsdBId = peerInfo.id - bMultiaddr = peerInfo.addresses[0] - }) - - // Connect the nodes - before(async function () { - this.timeout(60 * 1000) - + ipfsdA = await df.spawn({ type: 'proc' }) + ipfsdB = await df.spawn({ type: 'js' }) + ipfsdBId = ipfsdB.api.peerId.id + bMultiaddr = ipfsdB.api.peerId.addresses[0] await ipfsdA.api.swarm.connect(bMultiaddr) }) - after(async () => { - if (ipfsdB) { - await ipfsdB.stop() - } - }) - - after(async () => { - if (ipfsdA) { - await ipfsdA.stop() - } - }) + after(() => df.clean()) it('can ping via a promise without options', async () => { const res = await ipfsdA.api.ping(ipfsdBId) @@ -110,38 +50,14 @@ describe('ping', function () { // Spawn nodes before(async function () { - this.timeout(60 * 1000) - - ipfsdA = await spawnNode({ dht: false }) - ipfsdB = await spawnNode({ dht: false }) - }) - - // Get the peer info object - before(async function () { - this.timeout(60 * 1000) - - const peerInfo = await ipfsdB.api.id() - ipfsdBId = peerInfo.id - bMultiaddr = peerInfo.addresses[0] - }) - - // Connect the nodes - before(function (done) { - this.timeout(60 * 1000) - ipfsdA.api.swarm.connect(bMultiaddr, done) - }) - - after(async () => { - if (ipfsdA) { - await ipfsdA.stop() - } + ipfsdA = await df.spawn({ type: 'proc' }) + ipfsdB = await df.spawn({ type: 'js' }) + ipfsdBId = ipfsdB.api.peerId.id + bMultiaddr = ipfsdB.api.peerId.addresses[0] + await ipfsdA.api.swarm.connect(bMultiaddr) }) - after(async () => { - if (ipfsdB) { - await ipfsdB.stop() - } - }) + after(() => df.clean()) it('sends the specified number of packets', (done) => { let packetNum = 0 @@ -200,9 +116,9 @@ describe('ping', function () { before(async function () { this.timeout(60 * 1000) - ipfsdA = await spawnNode({ dht: true }) - ipfsdB = await spawnNode({ dht: true }) - ipfsdC = await spawnNode({ dht: true }) + ipfsdA = await df.spawn({ type: 'proc' }) + ipfsdB = await df.spawn({ type: 'proc' }) + ipfsdC = await df.spawn({ type: 'proc' }) }) // Get the peer info objects @@ -247,23 +163,7 @@ describe('ping', function () { }) }) - after(async () => { - if (ipfsdA) { - await ipfsdA.stop() - } - }) - - after(async () => { - if (ipfsdB) { - await ipfsdB.stop() - } - }) - - after(async () => { - if (ipfsdC) { - await ipfsdC.stop() - } - }) + after(() => df.clean()) it('if enabled uses the DHT peer routing to find peer', (done) => { let messageNum = 0 diff --git a/test/core/stats.spec.js b/test/core/stats.spec.js index e522720aa7..1c9074c922 100644 --- a/test/core/stats.spec.js +++ b/test/core/stats.spec.js @@ -4,33 +4,19 @@ const { expect } = require('interface-ipfs-core/src/utils/mocha') const pull = require('pull-stream') -const IPFSFactory = require('ipfsd-ctl') -const IPFS = require('../../src/core') +const factory = require('../utils/factory') describe('stats', function () { + const df = factory() this.timeout(10 * 1000) let ipfsd, ipfs before(async () => { - const factory = IPFSFactory.create({ - type: 'proc', - IpfsClient: require('ipfs-http-client') - }) - - ipfsd = await factory.spawn({ - exec: IPFS, - initOptions: { bits: 512 }, - config: { Bootstrap: [] }, - preload: { enabled: false } - }) + ipfsd = await df.spawn() ipfs = ipfsd.api }) - after(() => { - if (ipfsd) { - return ipfsd.stop() - } - }) + after(() => df.clean()) describe('bwPullStream', () => { it('should return erroring stream for invalid interval option', (done) => { diff --git a/test/core/swarm.spec.js b/test/core/swarm.spec.js index 40c2d23ee8..d9abb9b0cb 100644 --- a/test/core/swarm.spec.js +++ b/test/core/swarm.spec.js @@ -3,33 +3,19 @@ 'use strict' const { expect } = require('interface-ipfs-core/src/utils/mocha') -const IPFSFactory = require('ipfsd-ctl') -const IPFS = require('../../src/core') +const factory = require('../utils/factory') describe('swarm', function () { + const df = factory() this.timeout(10 * 1000) let ipfsd, ipfs before(async () => { - const factory = IPFSFactory.create({ - type: 'proc', - IpfsClient: require('ipfs-http-client') - }) - - ipfsd = await factory.spawn({ - exec: IPFS, - initOptions: { bits: 512 }, - config: { Bootstrap: [] }, - preload: { enabled: false } - }) + ipfsd = await df.spawn() ipfs = ipfsd.api }) - after(() => { - if (ipfsd) { - return ipfsd.stop() - } - }) + after(() => df.clean()) describe('peers', () => { it('should not error when passed null options', (done) => { diff --git a/test/http-api/interface.js b/test/http-api/interface.js index 93880d2791..da46842a65 100644 --- a/test/http-api/interface.js +++ b/test/http-api/interface.js @@ -2,23 +2,47 @@ 'use strict' const tests = require('interface-ipfs-core') -const CommonFactory = require('../utils/interface-common-factory') -const path = require('path') - -describe('interface-ipfs-core over ipfs-http-client tests', () => { - const defaultCommonFactory = CommonFactory.createAsync({ - factoryOptions: { exec: path.resolve(`${__dirname}/../../src/cli/bin.js`) } - }) +const merge = require('merge-options') +const { isNode } = require('ipfs-utils/src/env') +const { createFactory } = require('ipfsd-ctl') +const IPFS = require('../../src') + +/** @typedef { import("ipfsd-ctl").ControllerOptions } ControllerOptions */ + +describe('interface-ipfs-core over ipfs-http-client tests', function () { + this.timeout(20000) + /** @type ControllerOptions */ + const commonOptions = { + test: true, + type: 'js', + ipfsModule: { + path: require.resolve('../../src'), + ref: IPFS + }, + ipfsHttpModule: { + path: require.resolve('ipfs-http-client'), + ref: require('ipfs-http-client') + }, + ipfsOptions: { + pass: 'ipfs-is-awesome-software' + } + } + const overrides = { + js: { + ipfsBin: './src/cli/bin.js' + } + } + const commonFactory = createFactory(commonOptions, overrides) - tests.bitswap(defaultCommonFactory) + tests.bitswap(commonFactory) - tests.block(defaultCommonFactory) + tests.block(commonFactory) - tests.bootstrap(defaultCommonFactory) + tests.bootstrap(commonFactory) - tests.config(defaultCommonFactory) + tests.config(commonFactory) - tests.dag(defaultCommonFactory, { + tests.dag(commonFactory, { skip: [{ name: 'should get only a CID, due to resolving locally only', reason: 'Local resolve option is not implemented yet' @@ -28,82 +52,44 @@ describe('interface-ipfs-core over ipfs-http-client tests', () => { }] }) - tests.dht(CommonFactory.createAsync({ - spawnOptions: { - initOptions: { bits: 512 }, - config: { - Bootstrap: [], - Discovery: { - MDNS: { - Enabled: false - }, - webRTCStar: { - Enabled: false - } - } - }, - preload: { enabled: false } - } - }), { + tests.dht(commonFactory, { skip: { reason: 'TODO: unskip when DHT is enabled: https://github.com/ipfs/js-ipfs/pull/1994' } }) - tests.filesRegular(defaultCommonFactory) - - tests.filesMFS(defaultCommonFactory) - - tests.key(CommonFactory.createAsync({ - spawnOptions: { - args: ['--pass ipfs-is-awesome-software'], - initOptions: { bits: 512 }, - config: { - Bootstrap: [], - Discovery: { - MDNS: { - Enabled: false - }, - webRTCStar: { - Enabled: false - } - } - }, - preload: { enabled: false } - } - })) + tests.filesRegular(commonFactory, { + skip: isNode ? null : [{ + name: 'addFromStream', + reason: 'Not designed to run in the browser' + }, { + name: 'addFromFs', + reason: 'Not designed to run in the browser' + }] + }) - tests.miscellaneous(CommonFactory.createAsync({ - spawnOptions: { - args: ['--pass ipfs-is-awesome-software', '--offline'] - } - })) + tests.filesMFS(commonFactory) - tests.name(CommonFactory.createAsync({ - spawnOptions: { - args: ['--pass ipfs-is-awesome-software', '--offline'] + tests.key(commonFactory) + + tests.miscellaneous(commonFactory) + + tests.name(createFactory(merge(commonOptions, { + ipfsOptions: { + pass: 'ipfs-is-awesome-software', + offline: true } - })) - - tests.namePubsub(CommonFactory.createAsync({ - spawnOptions: { - args: ['--enable-namesys-pubsub'], - initOptions: { bits: 1024 }, - config: { - Bootstrap: [], - Discovery: { - MDNS: { - Enabled: false - }, - webRTCStar: { - Enabled: false - } - } + }), overrides)) + + tests.namePubsub(createFactory(merge(commonOptions, { + ipfsOptions: { + EXPERIMENTAL: { + ipnsPubsub: true } } - })) + }), overrides)) - tests.object(defaultCommonFactory, { + tests.object(commonFactory, { skip: [ { name: 'should respect timeout option', @@ -112,19 +98,19 @@ describe('interface-ipfs-core over ipfs-http-client tests', () => { ] }) - tests.pin(defaultCommonFactory) + tests.pin(commonFactory) - tests.ping(defaultCommonFactory) + tests.ping(commonFactory) - tests.pubsub(CommonFactory.createAsync({ - spawnOptions: { - initOptions: { bits: 512 } + tests.pubsub(createFactory(commonOptions, merge(overrides, { + go: { + args: ['--enable-pubsub-experiment'] } - })) + }))) - tests.repo(defaultCommonFactory) + tests.repo(commonFactory) - tests.stats(defaultCommonFactory) + tests.stats(commonFactory) - tests.swarm(CommonFactory.createAsync()) + tests.swarm(commonFactory) }) diff --git a/test/utils/factory.js b/test/utils/factory.js new file mode 100644 index 0000000000..01f7524c14 --- /dev/null +++ b/test/utils/factory.js @@ -0,0 +1,26 @@ +'use strict' +const { createFactory } = require('ipfsd-ctl') +const merge = require('merge-options') + +const factory = (options, overrides) => { + return createFactory( + merge({ + test: true, + type: 'proc', + ipfsModule: { + path: require.resolve('../../src'), + ref: require('../../src') + }, + ipfsHttpModule: { + path: require.resolve('ipfs-http-client'), + ref: require('ipfs-http-client') + } + }, options), + merge({ + js: { + ipfsBin: './src/cli/bin.js' + } + }, overrides) + ) +} +module.exports = factory diff --git a/test/utils/interface-common-factory.js b/test/utils/interface-common-factory.js deleted file mode 100644 index ebca12f962..0000000000 --- a/test/utils/interface-common-factory.js +++ /dev/null @@ -1,134 +0,0 @@ -/* eslint-env mocha */ -'use strict' - -const each = require('async/each') -const IPFSFactory = require('ipfsd-ctl') -const ipfsClient = require('ipfs-http-client') -const callbackify = require('callbackify') -const mergeOptions = require('merge-options') -const IPFS = require('../../src') - -const DEFAULT_FACTORY_OPTIONS = { - type: 'proc', - exec: IPFS -} - -function createFactory (options) { - options = options || {} - - options.factoryOptions = options.factoryOptions || { ...DEFAULT_FACTORY_OPTIONS } - options.spawnOptions = mergeOptions({ - initOptions: { bits: 512 }, - config: { - Bootstrap: [], - Discovery: { - MDNS: { - Enabled: false - }, - webRTCStar: { - Enabled: false - } - } - }, - preload: { enabled: false } - }, options.spawnOptions) - - if (options.factoryOptions.type !== 'proc') { - options.factoryOptions.IpfsClient = options.factoryOptions.IpfsClient || ipfsClient - } - - const ipfsFactory = IPFSFactory.create(options.factoryOptions) - const callbackifiedSpawn = callbackify.variadic(ipfsFactory.spawn.bind(ipfsFactory)) - - return function createCommon () { - const nodes = [] - let setup, teardown - - if (options.createSetup) { - setup = options.createSetup({ ipfsFactory, nodes }, options) - } else { - setup = (callback) => { - callback(null, { - spawnNode (cb) { - callbackifiedSpawn(options.spawnOptions, (err, _ipfsd) => { - if (err) { - return cb(err) - } - - nodes.push(_ipfsd) - cb(null, _ipfsd.api) - }) - } - }) - } - } - - if (options.createTeardown) { - teardown = options.createTeardown({ ipfsFactory, nodes }, options) - } else { - teardown = callback => each(nodes, (node, cb) => callbackify.variadic(node.stop.bind(node))(cb), callback) - } - - return { setup, teardown } - } -} - -function createAsync (options = {}) { - return () => { - const nodes = [] - const setup = async (setupOptions = {}) => { - options.factoryOptions = mergeOptions( - options.factoryOptions ? {} : { ...DEFAULT_FACTORY_OPTIONS }, - setupOptions.factoryOptions, - options.factoryOptions - ) - - // When not an in proc daemon use the http-client js-ipfs depends on, not the one from ipfsd-ctl - if (options.factoryOptions.type !== 'proc') { - options.factoryOptions.IpfsClient = options.factoryOptions.IpfsClient || ipfsClient - } - - const ipfsFactory = IPFSFactory.create(options.factoryOptions) - - options.spawnOptions = mergeOptions( - { - config: { - Bootstrap: [], - Discovery: { - MDNS: { - Enabled: false - }, - webRTCStar: { - Enabled: false - } - } - }, - preload: { enabled: false } - }, - setupOptions.spawnOptions, - options.spawnOptions - ) - - const node = await ipfsFactory.spawn(options.spawnOptions) - - nodes.push(node) - - const id = await node.api.id() - node.api.peerId = id - - return node.api - } - - const teardown = () => { - return Promise.all(nodes.map(n => n.stop())) - } - return { - setup, - teardown - } - } -} -module.exports = { - createAsync, - create: createFactory -} diff --git a/test/utils/on-and-off.js b/test/utils/on-and-off.js index c88af2821c..abc98a1f54 100644 --- a/test/utils/on-and-off.js +++ b/test/utils/on-and-off.js @@ -5,13 +5,9 @@ const hat = require('hat') const ipfsExec = require('../utils/ipfs-exec') const clean = require('../utils/clean') +const factory = require('./factory') const os = require('os') -const DaemonFactory = require('ipfsd-ctl') -const df = DaemonFactory.create({ - IpfsClient: require('ipfs-http-client') -}) -const path = require('path') const origLocale = process.env.LC_ALL function off (tests) { @@ -46,6 +42,7 @@ function off (tests) { function on (tests) { describe('daemon on (through http-api)', function () { this.timeout(60 * 1000) + const df = factory({ type: 'js' }) const thing = { on: true } @@ -59,22 +56,14 @@ function on (tests) { // before step this.timeout(60 * 1000) - ipfsd = await df.spawn({ - type: 'js', - exec: path.resolve(`${__dirname}/../../src/cli/bin.js`), - initOptions: { bits: 512 }, - config: { Bootstrap: [] } - }) - thing.ipfs = ipfsExec(ipfsd.repoPath) - thing.ipfs.repoPath = ipfsd.repoPath + ipfsd = await df.spawn() + thing.ipfs = ipfsExec(ipfsd.path) + thing.ipfs.repoPath = ipfsd.path }) after(function () { resetLocaleToSystem() - if (ipfsd) { - this.timeout(15 * 1000) - return ipfsd.stop() - } + df.clean() }) tests(thing)