Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
test(interop): add repo tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Dec 22, 2016
1 parent 789ef7e commit d1b2f5d
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 12 deletions.
28 changes: 24 additions & 4 deletions test/interop/daemons/go.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,35 @@ const ctl = require('ipfsd-ctl')
const waterfall = require('async/waterfall')

class GoDaemon {
constructor () {
constructor (opts) {
opts = opts || {
disposable: true,
init: true
}

this.init = opts.init
this.path = opts.path
this.disposable = opts.disposable
this.node = null
this.api = null
}

start (callback) {
console.log('starting go')
waterfall([
(cb) => ctl.disposable(cb),
(cb) => {
if (this.disposable) {
ctl.disposable({init: this.init}, cb)
} else if (this.init) {
ctl.local(this.path, (err, node) => {
if (err) {
return cb(err)
}
node.init((err) => cb(err, node))
})
} else {
ctl.local(this.path, cb)
}
},
(node, cb) => {
this.node = node
this.node.startDaemon(cb)
Expand All @@ -21,7 +41,7 @@ class GoDaemon {
this.api = api
cb()
}
], callback)
], (err) => callback(err))
}

stop (callback) {
Expand Down
43 changes: 35 additions & 8 deletions test/interop/daemons/js.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,43 @@ const os = require('os')
const IPFSAPI = require('ipfs-api')
const series = require('async/series')
const rimraf = require('rimraf')
const IPFSRepo = require('ipfs-repo')

const IPFS = require('../../../src/core')
const HTTPAPI = require('../../../src/http-api')

class JsDaemon {
constructor () {
this.repoPath = os.tmpdir() + `${Math.ceil(Math.random() * 100)}`
this.ipfs = new IPFS(this.repoPath)
constructor (opts) {
opts = opts || {
disposable: true,
init: true
}

this.path = opts.path
this.disposable = opts.disposable
this.init = opts.init

this.path = opts.path || os.tmpdir() + `/${Math.ceil(Math.random() * 1000)}`
if (this.init) {
this.ipfs = new IPFS(this.path)
} else {
const repo = new IPFSRepo(this.path, {stores: require('fs-pull-blob-store')})
this.ipfs = new IPFS(repo)
}
this.node = null
this.api = null
}

start (callback) {
console.log('starting js', this.repoPath)
console.log('starting js', this.path)
series([
(cb) => this.ipfs.init(cb),
(cb) => {
if (this.init) {
this.ipfs.init(cb)
} else {
cb()
}
},
(cb) => {
this.node = new HTTPAPI(this.ipfs._repo)
this.node.start(cb)
Expand All @@ -28,14 +49,20 @@ class JsDaemon {
this.api = new IPFSAPI(this.node.apiMultiaddr)
cb()
}
], callback)
], (err) => callback(err))
}

stop (callback) {
series([
(cb) => this.node.stop(cb),
(cb) => rimraf(this.repoPath, cb)
], callback)
(cb) => {
if (this.disposable) {
rimraf(this.path, cb)
} else {
cb()
}
}
], (err) => callback(err))
}
}

Expand Down
Empty file removed test/interop/interop-repo.js
Empty file.
1 change: 1 addition & 0 deletions test/interop/node.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict'

require('./index')
require('./repo')
76 changes: 76 additions & 0 deletions test/interop/repo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const waterfall = require('async/waterfall')
const bl = require('bl')
const crypto = require('crypto')
const os = require('os')

const GoDaemon = require('./daemons/go')
const JsDaemon = require('./daemons/js')

function catAndCheck (daemon, hash, data, callback) {
waterfall([
(cb) => daemon.api.cat(hash, cb),
(stream, cb) => stream.pipe(bl(cb))
], (err, file) => {
console.log('got file')
expect(err).to.not.exist
expect(file).to.be.eql(data)
callback()
})
}

// TODO: these are not working, will need to figure out why
describe.skip('interop - repo', () => {
it('read repo: go -> js', (done) => {
const dir = os.tmpdir() + '/' + Math.ceil(Math.random() * 10000)
const data = crypto.randomBytes(1024 * 5)

const goDaemon = new GoDaemon({init: true, disposable: false, path: dir})
let jsDaemon

let hash
waterfall([
(cb) => goDaemon.start(cb),
(cb) => goDaemon.api.add(data, cb),
(res, cb) => {
hash = res[0].hash
catAndCheck(goDaemon, hash, data, cb)
},
(cb) => goDaemon.stop(cb),
(cb) => {
jsDaemon = new JsDaemon({init: false, disposable: false, path: dir})
jsDaemon.start(cb)
},
(cb) => catAndCheck(goDaemon, hash, data, cb),
(cb) => jsDaemon.stop(cb)
], done)
})

it.only('read repo: js -> go', (done) => {
const dir = os.tmpdir() + '/' + Math.ceil(Math.random() * 10000)
const data = crypto.randomBytes(1024 * 5)

const jsDaemon = new JsDaemon({init: true, disposable: false, path: dir})
let goDaemon

let hash
waterfall([
(cb) => jsDaemon.start(cb),
(cb) => jsDaemon.api.add(data, cb),
(res, cb) => {
hash = res[0].hash
catAndCheck(jsDaemon, hash, data, cb)
},
(cb) => jsDaemon.stop(cb),
(cb) => {
goDaemon = new GoDaemon({init: false, disposable: false, path: dir})
goDaemon.start(cb)
},
(cb) => catAndCheck(goDaemon, hash, data, cb),
(cb) => goDaemon.stop(cb)
], done)
})
})

0 comments on commit d1b2f5d

Please sign in to comment.