|
| 1 | +/* eslint-env mocha */ |
| 2 | +'use strict' |
| 3 | + |
| 4 | +const chai = require('chai') |
| 5 | +const dirtyChai = require('dirty-chai') |
| 6 | +const series = require('async/series') |
| 7 | +const expect = chai.expect |
| 8 | +const statsTests = require('./utils/stats') |
| 9 | +const spawn = require('./utils/spawn') |
| 10 | +chai.use(dirtyChai) |
| 11 | +const CID = require('cids') |
| 12 | + |
| 13 | +module.exports = (common) => { |
| 14 | + describe('.bitswap online', () => { |
| 15 | + let ipfsA |
| 16 | + let ipfsB |
| 17 | + let withGo |
| 18 | + let ipfsBId |
| 19 | + const key = 'QmUBdnXXPyoDFXj3Hj39dNJ5VkN3QFRskXxcGaYFBB8CNR' |
| 20 | + |
| 21 | + before(function (done) { |
| 22 | + // CI takes longer to instantiate the daemon, so we need to increase the |
| 23 | + // timeout for the before step |
| 24 | + this.timeout(60 * 250) |
| 25 | + |
| 26 | + common.setup((err, factory) => { |
| 27 | + expect(err).to.not.exist() |
| 28 | + series([ |
| 29 | + (cb) => spawn.spawnNodeWithId(factory, (err, node) => { |
| 30 | + expect(err).to.not.exist() |
| 31 | + ipfsA = node |
| 32 | + withGo = node.peerId.agentVersion.startsWith('go-ipfs') |
| 33 | + cb() |
| 34 | + }), |
| 35 | + (cb) => spawn.spawnNodeWithId(factory, (err, node) => { |
| 36 | + expect(err).to.not.exist() |
| 37 | + ipfsB = node |
| 38 | + ipfsBId = node.peerId |
| 39 | + ipfsB.block.get(new CID(key)) |
| 40 | + .then(() => {}) |
| 41 | + .catch(() => {}) |
| 42 | + ipfsA.swarm.connect(ipfsBId.addresses[0], (err) => { |
| 43 | + expect(err).to.not.exist() |
| 44 | + setTimeout(cb, 350) |
| 45 | + }) |
| 46 | + }) |
| 47 | + ], done) |
| 48 | + }) |
| 49 | + }) |
| 50 | + |
| 51 | + after((done) => common.teardown(done)) |
| 52 | + |
| 53 | + it('.stat', (done) => { |
| 54 | + ipfsB.bitswap.stat((err, stats) => { |
| 55 | + expect(err).to.not.exist() |
| 56 | + statsTests.expectIsBitswap(err, stats) |
| 57 | + done() |
| 58 | + }) |
| 59 | + }) |
| 60 | + |
| 61 | + it('.wantlist', (done) => { |
| 62 | + ipfsB.bitswap.wantlist((err, list) => { |
| 63 | + expect(err).to.not.exist() |
| 64 | + expect(list.Keys).to.have.length(1) |
| 65 | + expect(list.Keys[0]['/']).to.equal(key) |
| 66 | + done() |
| 67 | + }) |
| 68 | + }) |
| 69 | + |
| 70 | + it('.wantlist peerid', (done) => { |
| 71 | + ipfsA.bitswap.wantlist(ipfsBId.id, (err, list) => { |
| 72 | + expect(err).to.not.exist() |
| 73 | + expect(list.Keys[0]['/']).to.equal(key) |
| 74 | + done() |
| 75 | + }) |
| 76 | + }) |
| 77 | + |
| 78 | + it('.unwant', function (done) { |
| 79 | + if (withGo) { |
| 80 | + this.skip() |
| 81 | + } |
| 82 | + ipfsB.bitswap.unwant(key, (err) => { |
| 83 | + expect(err).to.not.exist() |
| 84 | + ipfsB.bitswap.wantlist((err, list) => { |
| 85 | + expect(err).to.not.exist() |
| 86 | + expect(list.Keys).to.be.empty() |
| 87 | + done() |
| 88 | + }) |
| 89 | + }) |
| 90 | + }) |
| 91 | + }) |
| 92 | + |
| 93 | + describe('.bitswap offline', () => { |
| 94 | + let ipfs |
| 95 | + |
| 96 | + before(function (done) { |
| 97 | + // CI takes longer to instantiate the daemon, so we need to increase the |
| 98 | + // timeout for the before step |
| 99 | + this.timeout(60 * 1000) |
| 100 | + |
| 101 | + common.setup((err, factory) => { |
| 102 | + expect(err).to.not.exist() |
| 103 | + factory.spawnNode((err, node) => { |
| 104 | + expect(err).to.not.exist() |
| 105 | + ipfs = node |
| 106 | + ipfs.id((err, id) => { |
| 107 | + expect(err).to.not.exist() |
| 108 | + ipfs.stop((err) => { |
| 109 | + // TODO: go-ipfs returns an error, https://github.com/ipfs/go-ipfs/issues/4078 |
| 110 | + if (!id.agentVersion.startsWith('go-ipfs')) { |
| 111 | + expect(err).to.not.exist() |
| 112 | + } |
| 113 | + done() |
| 114 | + }) |
| 115 | + }) |
| 116 | + }) |
| 117 | + }) |
| 118 | + }) |
| 119 | + |
| 120 | + it('.stat gives error while offline', (done) => { |
| 121 | + ipfs.bitswap.stat((err, stats) => { |
| 122 | + expect(err).to.exist() |
| 123 | + // When run against core we get our expected error, when run |
| 124 | + // as part of the http tests we get a connection refused |
| 125 | + if (err.code !== 'ECONNREFUSED') { |
| 126 | + expect(err).to.match(/online mode/) |
| 127 | + } |
| 128 | + expect(stats).to.not.exist() |
| 129 | + done() |
| 130 | + }) |
| 131 | + }) |
| 132 | + |
| 133 | + it('.wantlist gives error if offline', (done) => { |
| 134 | + ipfs.bitswap.wantlist((err, list) => { |
| 135 | + expect(err).to.exist() |
| 136 | + // When run against core we get our expected error, when run |
| 137 | + // as part of the http tests we get a connection refused |
| 138 | + if (err.code !== 'ECONNREFUSED') { |
| 139 | + expect(err).to.match(/online mode/) |
| 140 | + } |
| 141 | + expect(list).to.not.exist() |
| 142 | + done() |
| 143 | + }) |
| 144 | + }) |
| 145 | + |
| 146 | + it('.unwant gives error if offline', (done) => { |
| 147 | + const key = 'QmUBdnXXPyoDFXj3Hj39dNJ5VkN3QFRskXxcGaYFBB8CNR' |
| 148 | + ipfs.bitswap.unwant(key, (err) => { |
| 149 | + expect(err).to.exist() |
| 150 | + // When run against core we get our expected error, when run |
| 151 | + // as part of the http tests we get a connection refused |
| 152 | + if (err.code !== 'ECONNREFUSED') { |
| 153 | + expect(err).to.match(/online mode/) |
| 154 | + } |
| 155 | + done() |
| 156 | + }) |
| 157 | + }) |
| 158 | + }) |
| 159 | +} |
0 commit comments