From 017ab8b766a7cc3c68856be3a4df78a765c27914 Mon Sep 17 00:00:00 2001 From: Jacob Heun Date: Tue, 21 May 2019 17:05:19 +0200 Subject: [PATCH 1/4] fix(cli): make swarm addrs more resilient Dont fail if decapsulation of ipfs fails. Go node addresses typically wont contain their peerid --- src/cli/commands/swarm/addrs.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/cli/commands/swarm/addrs.js b/src/cli/commands/swarm/addrs.js index aef6704416..a5f7e83758 100644 --- a/src/cli/commands/swarm/addrs.js +++ b/src/cli/commands/swarm/addrs.js @@ -21,7 +21,14 @@ module.exports = { print(`${peer.id.toB58String()} (${count})`) peer.multiaddrs.forEach((addr) => { - const res = addr.decapsulate('ipfs').toString() + let res + try { + res = addr.decapsulate('ipfs').toString() + } catch (_) { + // peer addresses dont need to have /ipfs/ as we know their peerId + // and can encapsulate on dial. + res = addr.toString() + } print(`\t${res}`) }) }) From a51756b8238c805dc27a29310b5fbf441ebf27ff Mon Sep 17 00:00:00 2001 From: Jacob Heun Date: Tue, 21 May 2019 23:50:14 +0200 Subject: [PATCH 2/4] test: add test for addrs handler --- test/cli/swarm.js | 137 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 97 insertions(+), 40 deletions(-) diff --git a/test/cli/swarm.js b/test/cli/swarm.js index 805bd1affe..798d673dd2 100644 --- a/test/cli/swarm.js +++ b/test/cli/swarm.js @@ -6,10 +6,15 @@ const chai = require('chai') const dirtyChai = require('dirty-chai') const expect = chai.expect chai.use(dirtyChai) -const series = require('async/series') +const sinon = require('sinon') const ipfsExec = require('../utils/ipfs-exec') const path = require('path') const parallel = require('async/parallel') +const addrsCommand = require('../../src/cli/commands/swarm/addrs') + +const multiaddr = require('multiaddr') +const PeerInfo = require('peer-info') +const PeerId = require('peer-id') const DaemonFactory = require('ipfsd-ctl') const df = DaemonFactory.create({ type: 'js' }) @@ -25,50 +30,54 @@ const config = { } describe('swarm', () => { - let bMultiaddr - let ipfsA - - let nodes = [] - before(function (done) { - // CI takes longer to instantiate the daemon, so we need to increase the - // timeout for the before step - this.timeout(80 * 1000) - - series([ - (cb) => { - df.spawn({ - exec: path.resolve(`${__dirname}/../../src/cli/bin.js`), - config, - initOptions: { bits: 512 } - }, (err, node) => { - expect(err).to.not.exist() - ipfsA = ipfsExec(node.repoPath) - nodes.push(node) - cb() - }) - }, - (cb) => { - df.spawn({ - exec: path.resolve(`${__dirname}/../../src/cli/bin.js`), - config, - initOptions: { bits: 512 } - }, (err, node) => { - expect(err).to.not.exist() - node.api.id((err, id) => { + afterEach(() => { + sinon.restore() + }) + + describe('daemon on (through http-api)', function () { + this.timeout(60 * 1000) + + let bMultiaddr + let ipfsA + + let nodes = [] + before(function (done) { + // CI takes longer to instantiate the daemon, so we need to increase the + // timeout for the before step + this.timeout(80 * 1000) + + parallel([ + (cb) => { + df.spawn({ + exec: path.resolve(`${__dirname}/../../src/cli/bin.js`), + config, + initOptions: { bits: 512 } + }, (err, node) => { expect(err).to.not.exist() - bMultiaddr = id.addresses[0] + ipfsA = ipfsExec(node.repoPath) nodes.push(node) cb() }) - }) - } - ], done) - }) - - after((done) => parallel(nodes.map((node) => (cb) => node.stop(cb)), done)) + }, + (cb) => { + df.spawn({ + exec: path.resolve(`${__dirname}/../../src/cli/bin.js`), + config, + initOptions: { bits: 512 } + }, (err, node) => { + expect(err).to.not.exist() + node.api.id((err, id) => { + expect(err).to.not.exist() + bMultiaddr = id.addresses[0] + nodes.push(node) + cb() + }) + }) + } + ], done) + }) - describe('daemon on (through http-api)', function () { - this.timeout(60 * 1000) + after((done) => parallel(nodes.map((node) => (cb) => node.stop(cb)), done)) it('connect', () => { return ipfsA('swarm', 'connect', bMultiaddr).then((out) => { @@ -108,4 +117,52 @@ describe('swarm', () => { }) }) }) + + describe('handlers', () => { + let peerInfo + const ipfs = { + swarm: { addrs: () => {} } + } + const argv = { + resolve: () => {}, + getIpfs: () => ipfs + } + + describe('addrs', () => { + before((done) => { + PeerId.create({ bits: 512 }, (err, peerId) => { + if (err) return done(err) + peerInfo = new PeerInfo(peerId) + done() + }) + }) + + it('should return addresses for all peers', (done) => { + const printSpy = sinon.stub(process.stdout, 'write') + sinon.stub(argv, 'resolve').callsFake(promise => { + promise.then(() => { + const output = printSpy.getCalls().map(call => call.args[0]) + printSpy.restore() + expect(output).to.eql([ + `${peerInfo.id.toB58String()} (2)\n`, + `\t/ip4/127.0.0.1/tcp/4001\n`, + `\t/ip4/127.0.0.1/tcp/4001/ws\n` + ]) + done() + }) + }) + + sinon.stub(peerInfo.multiaddrs, '_multiaddrs').value([ + multiaddr('/ip4/127.0.0.1/tcp/4001'), + multiaddr(`/ip4/127.0.0.1/tcp/4001/ws/ipfs/${peerInfo.id.toB58String()}`) + ]) + + sinon.stub(ipfs.swarm, 'addrs').returns( + Promise.resolve([peerInfo]) + ) + + addrsCommand.handler(argv) + }) + }) + }) }) From fa67c0681ab08f78d1ee8b930e21ab28e8cdfba5 Mon Sep 17 00:00:00 2001 From: Jacob Heun Date: Wed, 22 May 2019 11:10:17 +0200 Subject: [PATCH 3/4] refactor(cli): have swarm addrs return the output for printing --- src/cli/commands/swarm/addrs.js | 16 ++++++++++------ test/cli/swarm.js | 15 ++++++--------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/cli/commands/swarm/addrs.js b/src/cli/commands/swarm/addrs.js index a5f7e83758..c7d81dceeb 100644 --- a/src/cli/commands/swarm/addrs.js +++ b/src/cli/commands/swarm/addrs.js @@ -1,7 +1,5 @@ 'use strict' -const print = require('../../utils').print - module.exports = { command: 'addrs', @@ -16,11 +14,12 @@ module.exports = { argv.resolve((async () => { const ipfs = await argv.getIpfs() const res = await ipfs.swarm.addrs() - res.forEach((peer) => { + + const output = res.map((peer) => { const count = peer.multiaddrs.size - print(`${peer.id.toB58String()} (${count})`) + const peerAddrs = [`${peer.id.toB58String()} (${count})`] - peer.multiaddrs.forEach((addr) => { + peer.multiaddrs.toArray().map((addr) => { let res try { res = addr.decapsulate('ipfs').toString() @@ -29,9 +28,14 @@ module.exports = { // and can encapsulate on dial. res = addr.toString() } - print(`\t${res}`) + peerAddrs.push(`\t${res}`) }) + + return peerAddrs.join('\n') }) + + // Return the output for printing + return { data: output.join('\n'), argv} })()) } } diff --git a/test/cli/swarm.js b/test/cli/swarm.js index 798d673dd2..63ec10ca7a 100644 --- a/test/cli/swarm.js +++ b/test/cli/swarm.js @@ -138,16 +138,13 @@ describe('swarm', () => { }) it('should return addresses for all peers', (done) => { - const printSpy = sinon.stub(process.stdout, 'write') sinon.stub(argv, 'resolve').callsFake(promise => { - promise.then(() => { - const output = printSpy.getCalls().map(call => call.args[0]) - printSpy.restore() - expect(output).to.eql([ - `${peerInfo.id.toB58String()} (2)\n`, - `\t/ip4/127.0.0.1/tcp/4001\n`, - `\t/ip4/127.0.0.1/tcp/4001/ws\n` - ]) + promise.then(({ data }) => { + expect(data).to.eql([ + `${peerInfo.id.toB58String()} (2)`, + `\t/ip4/127.0.0.1/tcp/4001`, + `\t/ip4/127.0.0.1/tcp/4001/ws` + ].join('\n')) done() }) }) From bc97992815ed4f5ce4970509a07760e1d46d97bd Mon Sep 17 00:00:00 2001 From: Jacob Heun Date: Wed, 22 May 2019 11:26:13 +0200 Subject: [PATCH 4/4] chore: fix linting --- src/cli/commands/swarm/addrs.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli/commands/swarm/addrs.js b/src/cli/commands/swarm/addrs.js index c7d81dceeb..1fe59a8405 100644 --- a/src/cli/commands/swarm/addrs.js +++ b/src/cli/commands/swarm/addrs.js @@ -35,7 +35,7 @@ module.exports = { }) // Return the output for printing - return { data: output.join('\n'), argv} + return { data: output.join('\n'), argv } })()) } }