diff --git a/src/core/index.js b/src/core/index.js index 4785d0737b..66b3bb2160 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -29,7 +29,7 @@ function IPFS (repo) { const dagS = new DAGService(blockS) var peerInfo var libp2pNode - var peers = {} + var peerInfoBook = {} this.load = (callback) => { repo.exists((err, exists) => { @@ -331,14 +331,14 @@ function IPFS (repo) { return callback(OFFLINE_ERROR) } - callback(null, peers) + callback(null, peerInfoBook) }, // all the addrs we know addrs: (callback) => { if (!libp2pNode) { return callback(OFFLINE_ERROR) } - + // TODO notImpl() }, localAddrs: (callback) => { @@ -363,7 +363,8 @@ function IPFS (repo) { ma = ma.toString().replace(/\/ipfs\/(.*)/, '') // FIXME remove this when multiaddr supports ipfs peer.multiaddr.add(multiaddr(ma)) - peers[peer.id.toB58String()] = peer + peerInfoBook[peer.id.toB58String()] = peer + libp2pNode.swarm.dial(peer, (err) => { callback(err, id) }) diff --git a/src/http-api/index.js b/src/http-api/index.js index e0cf86d95e..cdae357ec8 100644 --- a/src/http-api/index.js +++ b/src/http-api/index.js @@ -10,8 +10,13 @@ log.error = debug('api:error') exports = module.exports -exports.start = (callback) => { - const ipfs = exports.ipfs = new IPFS() +exports.start = (repo, callback) => { + if (typeof repo === 'function') { + callback = repo + repo = undefined + } + + const ipfs = exports.ipfs = new IPFS(repo) ipfs.load(() => { const repoPath = process.env.IPFS_PATH || os.homedir() + '/.ipfs' try { @@ -67,7 +72,9 @@ exports.start = (callback) => { exports.stop = (callback) => { const repoPath = process.env.IPFS_PATH || os.homedir() + '/.ipfs' fs.unlinkSync(repoPath + '/api') + console.log('->', 'going to stop libp2p') exports.ipfs.libp2p.stop(() => { + console.log('->', 'going to stop api server') exports.server.stop(callback) }) } diff --git a/test/core-tests/test-swarm-node.js b/test/core-tests/test-swarm-node.js index cb4eadb934..30f7fd5ed3 100644 --- a/test/core-tests/test-swarm-node.js +++ b/test/core-tests/test-swarm-node.js @@ -60,7 +60,7 @@ describe('swarm', function () { it('localAddrs', (done) => { ipfsB.libp2p.swarm.localAddrs((err, res) => { expect(err).to.not.exist - expect(res).to.have.length.above(1) + expect(res.length).to.equal(1) done() }) }) diff --git a/test/http-api-tests/index.js b/test/http-api-tests/index.js index 8f9c46f808..50889ca360 100644 --- a/test/http-api-tests/index.js +++ b/test/http-api-tests/index.js @@ -23,10 +23,24 @@ describe('http api', () => { after((done) => { api.stop((err) => { + console.log('->', 'going to clean up') expect(err).to.not.exist + var cleaned = false + + // cleaning up on CI takes a millenia + setTimeout(() => { + if (!cleaned) { + cleaned = true + console.log('->', 'last after all') + done() + } + }, 2000) rimraf(repoTests, (err) => { expect(err).to.not.exist - done() + if (!cleaned) { + cleaned = true + done() + } }) }) }) diff --git a/test/http-api-tests/test-swarm.js b/test/http-api-tests/test-swarm.js index 1b582e18dc..e108ab4d3f 100644 --- a/test/http-api-tests/test-swarm.js +++ b/test/http-api-tests/test-swarm.js @@ -9,7 +9,7 @@ describe('swarm', function () { describe('api', () => { var api - var ipfs + var ipfs // tmp node var ipfsAddr before((done) => { @@ -28,69 +28,71 @@ describe('swarm', function () { }) }) - it('api', (done) => { + after((done) => { + console.log('after all') + ipfs.libp2p.stop(() => { + console.log('after all called') + done() + }) + }) + + it('gets the api obj', (done) => { api = require('../../src/http-api').server.select('API') done() }) - describe('/swarm/connect', () => { - it('returns 400 for request without argument', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/swarm/connect' - }, (res) => { - expect(res.statusCode).to.equal(400) - expect(res.result).to.be.a('string') - done() - }) - }) - - it('returns 500 for request with invalid argument', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/swarm/connect?arg=invalid' - }, (res) => { - expect(res.statusCode).to.equal(500) - expect(res.result.Code).to.equal(0) - expect(res.result.Message).to.be.a('string') - done() - }) - }) - - it('returns value', (done) => { - api.inject({ - method: 'GET', - url: `/api/v0/swarm/connect?arg=${ipfsAddr}` - }, (res) => { - expect(res.statusCode).to.equal(200) - done() - }) - }) - }) - - describe('/swarm/peers', () => { - it('returns value', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/swarm/peers' - }, (res) => { - expect(res.statusCode).to.equal(200) - expect(res.result.Strings).to.have.length.above(0) - done() - }) - }) - }) - - describe('/swarm/addrs/local', () => { - it('returns value', (done) => { - api.inject({ - method: 'GET', - url: '/api/v0/swarm/addrs/local' - }, (res) => { - expect(res.statusCode).to.equal(200) - expect(res.result.Strings).to.have.length.above(0) - done() - }) + it('/swarm/connect returns 400 for request without argument', (done) => { + api.inject({ + method: 'GET', + url: '/api/v0/swarm/connect' + }, (res) => { + expect(res.statusCode).to.equal(400) + expect(res.result).to.be.a('string') + done() + }) + }) + + it('/swarm/connect returns 500 for request with invalid argument', (done) => { + api.inject({ + method: 'GET', + url: '/api/v0/swarm/connect?arg=invalid' + }, (res) => { + expect(res.statusCode).to.equal(500) + expect(res.result.Code).to.equal(0) + expect(res.result.Message).to.be.a('string') + done() + }) + }) + + it('/swarm/connect returns value', (done) => { + api.inject({ + method: 'GET', + url: `/api/v0/swarm/connect?arg=${ipfsAddr}` + }, (res) => { + expect(res.statusCode).to.equal(200) + done() + }) + }) + + it('/swarm/peers returns value', (done) => { + api.inject({ + method: 'GET', + url: '/api/v0/swarm/peers' + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.result.Strings).to.have.length.above(0) + done() + }) + }) + + it('/swarm/addrs/local returns value', (done) => { + api.inject({ + method: 'GET', + url: '/api/v0/swarm/addrs/local' + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.result.Strings).to.have.length.above(0) + done() }) }) }) @@ -116,51 +118,53 @@ describe('swarm', function () { }) }) + after((done) => { + console.log('after all') + ipfs.libp2p.stop(() => { + console.log('after all called') + done() + }) + }) + it('start IPFS API ctl', (done) => { ctl = APIctl('/ip4/127.0.0.1/tcp/6001') done() }) - describe('ipfs.swarm.connect', () => { - it('returns error for request without argument', (done) => { - ctl.swarm.connect(null, (err, result) => { - expect(err).to.exist - done() - }) + it('ipfs.swarm.connect returns error for request without argument', (done) => { + ctl.swarm.connect(null, (err, result) => { + expect(err).to.exist + done() }) + }) - it('returns error for request with invalid argument', (done) => { - ctl.swarm.connect('invalid', (err, result) => { - expect(err).to.exist - done() - }) + it('ipfs.swarm.connect returns error for request with invalid argument', (done) => { + ctl.swarm.connect('invalid', (err, result) => { + expect(err).to.exist + done() }) + }) - it('returns value', (done) => { - ctl.swarm.connect(ipfsAddr, (err, result) => { - expect(err).to.not.exist - done() - }) + it('ipfs.swarm.connect returns value', (done) => { + ctl.swarm.connect(ipfsAddr, (err, result) => { + expect(err).to.not.exist + done() }) }) - describe('ipfs.swarm.peers', () => { - it('returns value', (done) => { - ctl.swarm.peers((err, result) => { - expect(err).to.not.exist - expect(result.Strings).to.have.length.above(0) - done() - }) + it('ipfs.swarm.peers returns value', (done) => { + ctl.swarm.peers((err, result) => { + expect(err).to.not.exist + expect(result.Strings).to.have.length.above(0) + done() }) }) - describe('ipfs.swarm.localAddrs', () => { - it('returns value', (done) => { - ctl.swarm.localAddrs((err, result) => { - expect(err).to.not.exist - expect(result.Strings).to.have.length.above(0) - done() - }) + it('ipfs.swarm.localAddrsreturns value', (done) => { + ctl.swarm.localAddrs((err, result) => { + expect(err).to.not.exist + expect(result.Strings).to.have.length.above(0) + done() }) }) }) diff --git a/test/utils/temp-node.js b/test/utils/temp-node.js index ae0ea58d48..4055a15b62 100644 --- a/test/utils/temp-node.js +++ b/test/utils/temp-node.js @@ -10,8 +10,7 @@ function setAddresses (repo, num, callback) { expect(err).to.not.exist config.Addresses = { Swarm: [ - `/ip4/127.0.0.1/tcp/1000${num}`, - `/ip6/::/tcp/1000${num}` + `/ip4/127.0.0.1/tcp/1000${num}` ], API: `/ip4/127.0.0.1/tcp/1100${num}`, Gateway: `/ip4/127.0.0.1/tcp/1200${num}`