Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

Commit

Permalink
test: add ipns dht tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Nov 30, 2018
1 parent 4085089 commit d73c6d2
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 94 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"form-data": "^2.3.3",
"go-ipfs-dep": "~0.4.18",
"hat": "0.0.3",
"ipfs": "~0.33.0",
"ipfs": "ipfs/js-ipfs#feat/ipns-over-dht",
"ipfs-api": "^26.1.2",
"ipfs-unixfs": "~0.1.16",
"ipfsd-ctl": "~0.40.0",
Expand Down
284 changes: 191 additions & 93 deletions test/ipns.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,121 +7,219 @@ const expect = chai.expect
chai.use(dirtyChai)

const series = require('async/series')
const parallel = require('async/parallel')
const os = require('os')
const path = require('path')
const hat = require('hat')

const DaemonFactory = require('ipfsd-ctl')

const spawnJsDaemon = (dir, callback) => {
DaemonFactory.create({ type: 'js' })
.spawn({
repoPath: dir,
disposable: false,
initOptions: { bits: 512 },
args: ['--offline']
}, callback)
}

const spawnGoDaemon = (dir, callback) => {
DaemonFactory.create()
.spawn({
repoPath: dir,
disposable: false,
initOptions: { bits: 1024 },
args: ['--offline']
}, callback)
}

const ipfsRef = '/ipfs/QmPFVLPmp9zv5Z5KUqLhe2EivAGccQW2r7M7jhVJGLZoZU'

const publishAndResolve = (publisherDaemon, resolverDaemon, callback) => {
let nodeId
let sameDaemon = false

if (typeof resolverDaemon === 'function') {
callback = resolverDaemon
resolverDaemon = publisherDaemon
sameDaemon = true
}

const stopPublisherAndStartResolverDaemon = (callback) => {
series([
(cb) => publisherDaemon.stop(cb),
(cb) => setTimeout(cb, 2000),
(cb) => resolverDaemon.start(cb)
], callback)
}

series([
(cb) => publisherDaemon.init(cb),
(cb) => publisherDaemon.start(cb),
(cb) => publisherDaemon.api.id((err, res) => {
expect(err).to.not.exist()
nodeId = res.id
cb()
}),
(cb) => publisherDaemon.api.name.publish(ipfsRef, { resolve: false }, cb),
(cb) => sameDaemon ? cb() : stopPublisherAndStartResolverDaemon(cb),
(cb) => {
resolverDaemon.api.name.resolve(nodeId, { local: true }, (err, res) => {
describe('ipns', () => {
describe('ipns locally using the same repo across implementations', () => {
const spawnJsDaemon = (dir, callback) => {
DaemonFactory.create({ type: 'js' })
.spawn({
repoPath: dir,
disposable: false,
initOptions: { bits: 512 },
args: ['--offline']
}, callback)
}

const spawnGoDaemon = (dir, callback) => {
DaemonFactory.create()
.spawn({
repoPath: dir,
disposable: false,
initOptions: { bits: 1024 },
args: ['--offline']
}, callback)
}

const publishAndResolve = (publisherDaemon, resolverDaemon, callback) => {
let nodeId
let sameDaemon = false

if (typeof resolverDaemon === 'function') {
callback = resolverDaemon
resolverDaemon = publisherDaemon
sameDaemon = true
}

const stopPublisherAndStartResolverDaemon = (callback) => {
series([
(cb) => publisherDaemon.stop(cb),
(cb) => setTimeout(cb, 2000),
(cb) => resolverDaemon.start(cb)
], callback)
}

series([
(cb) => publisherDaemon.init(cb),
(cb) => publisherDaemon.start(cb),
(cb) => publisherDaemon.api.id((err, res) => {
expect(err).to.not.exist()
nodeId = res.id
cb()
}),
(cb) => publisherDaemon.api.name.publish(ipfsRef, { resolve: false }, cb),
(cb) => sameDaemon ? cb() : stopPublisherAndStartResolverDaemon(cb),
(cb) => {
resolverDaemon.api.name.resolve(nodeId, { local: true }, (err, res) => {
expect(err).to.not.exist()
expect(res).to.equal(ipfsRef)
cb()
})
},
(cb) => resolverDaemon.stop(cb),
(cb) => setTimeout(cb, 2000),
(cb) => resolverDaemon.cleanup(cb)
], callback)
}

it('should publish an ipns record to a js daemon and resolve it using the same js daemon', function (done) {
this.timeout(120 * 1000)
const dir = path.join(os.tmpdir(), hat())

spawnJsDaemon(dir, (err, jsDaemon) => {
expect(err).to.not.exist()
expect(res).to.equal(ipfsRef)
cb()
publishAndResolve(jsDaemon, done)
})
},
(cb) => resolverDaemon.stop(cb),
(cb) => setTimeout(cb, 2000),
(cb) => resolverDaemon.cleanup(cb)
], callback)
}

describe('ipns locally using the same repo across implementations', () => {
it('should publish an ipns record to a js daemon and resolve it using the same js daemon', function (done) {
this.timeout(120 * 1000)
const dir = path.join(os.tmpdir(), hat())

spawnJsDaemon(dir, (err, jsDaemon) => {
expect(err).to.not.exist()
publishAndResolve(jsDaemon, done)
})
})

it('should publish an ipns record to a go daemon and resolve it using the same go daemon', function (done) {
this.timeout(160 * 1000)
const dir = path.join(os.tmpdir(), hat())
it('should publish an ipns record to a go daemon and resolve it using the same go daemon', function (done) {
this.timeout(160 * 1000)
const dir = path.join(os.tmpdir(), hat())

spawnGoDaemon(dir, (err, goDaemon) => {
expect(err).to.not.exist()
publishAndResolve(goDaemon, done)
spawnGoDaemon(dir, (err, goDaemon) => {
expect(err).to.not.exist()
publishAndResolve(goDaemon, done)
})
})
})

it('should publish an ipns record to a js daemon and resolve it using a go daemon through the reuse of the same repo', function (done) {
this.timeout(120 * 1000)
const dir = path.join(os.tmpdir(), hat())
it('should publish an ipns record to a js daemon and resolve it using a go daemon through the reuse of the same repo', function (done) {
this.timeout(120 * 1000)
const dir = path.join(os.tmpdir(), hat())

series([
(cb) => spawnJsDaemon(dir, cb),
(cb) => spawnGoDaemon(dir, cb)
], (err, daemons) => {
expect(err).to.not.exist()

publishAndResolve(daemons[0], daemons[1], done)
})
})

series([
(cb) => spawnJsDaemon(dir, cb),
(cb) => spawnGoDaemon(dir, cb)
], (err, daemons) => {
expect(err).to.not.exist()
it('should publish an ipns record to a go daemon and resolve it using a js daemon through the reuse of the same repo', function (done) {
this.timeout(160 * 1000)
const dir = path.join(os.tmpdir(), hat())

publishAndResolve(daemons[0], daemons[1], done)
series([
(cb) => spawnGoDaemon(dir, cb),
(cb) => spawnJsDaemon(dir, cb)
], (err, daemons) => {
expect(err).to.not.exist()

publishAndResolve(daemons[0], daemons[1], done)
})
})
})

it('should publish an ipns record to a go daemon and resolve it using a js daemon through the reuse of the same repo', function (done) {
this.timeout(160 * 1000)
const dir = path.join(os.tmpdir(), hat())
describe('ipns over dht', () => {
const spawnJsDaemon = (callback) => {
DaemonFactory.create({ type: 'js' })
.spawn({
disposable: true,
initOptions: { bits: 512 },
args: ['--enable-dht-experiment'], // enable dht
config: { Bootstrap: [] }
}, callback)
}

const spawnGoDaemon = (callback) => {
DaemonFactory.create()
.spawn({
disposable: true,
initOptions: { bits: 1024 },
config: { Bootstrap: [] }
}, callback)
}

let nodeAId
let nodeBId
let nodes = []

// Spawn daemons
before(function (done) {
// CI takes longer to instantiate the daemon, so we need to increase the timeout
this.timeout(80 * 1000)
series([
(cb) => spawnGoDaemon(cb),
(cb) => spawnJsDaemon(cb),
(cb) => spawnGoDaemon(cb)
], (err, daemons) => {
expect(err).to.not.exist()
nodes = daemons
done()
})
})

series([
(cb) => spawnGoDaemon(dir, cb),
(cb) => spawnJsDaemon(dir, cb)
], (err, daemons) => {
expect(err).to.not.exist()
// Get node ids
before(function (done) {
this.timeout(100 * 1000)
parallel([
(cb) => nodes[0].api.id(cb),
(cb) => nodes[1].api.id(cb)
], (err, ids) => {
expect(err).to.not.exist()
expect(ids).to.exist()
expect(ids[0].id).to.exist()
expect(ids[1].id).to.exist()
nodeAId = ids[0]
nodeBId = ids[1]
parallel([
(cb) => nodes[2].api.swarm.connect(ids[0].addresses[0], cb), // C => A
(cb) => nodes[2].api.swarm.connect(ids[1].addresses[0], cb) // C => B
], done)
})
})

after(function (done) {
this.timeout(60 * 1000)
parallel(nodes.map((node) => (cb) => node.stop(cb)), done)
})

publishAndResolve(daemons[0], daemons[1], done)
it('should publish the record to a go node and resolve it using a js node', function (done) {
this.timeout(50 * 1000)
series([
(cb) => nodes[0].api.name.publish(ipfsRef, { resolve: false }, cb),
(cb) => nodes[1].api.name.resolve(nodeAId.id, cb)
], (err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
expect(res[0].value).to.equal(ipfsRef)
expect(res[0].name).to.equal(nodeAId.id)
expect(res[1]).to.equal(ipfsRef)
done()
})
})

it('should publish the record to a js node and resolve it using a go node', function (done) {
this.timeout(50 * 1000)
series([
(cb) => nodes[1].api.name.publish(ipfsRef, { resolve: false }, cb),
(cb) => nodes[0].api.name.resolve(nodeBId.id, cb)
], (err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
expect(res[0].value).to.equal(ipfsRef)
expect(res[0].name).to.equal(nodeBId.id)
expect(res[1]).to.equal(ipfsRef)
done()
})
})
})
})

0 comments on commit d73c6d2

Please sign in to comment.