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

Commit d1b2f5d

Browse files
test(interop): add repo tests
1 parent 789ef7e commit d1b2f5d

File tree

5 files changed

+136
-12
lines changed

5 files changed

+136
-12
lines changed

test/interop/daemons/go.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,35 @@ const ctl = require('ipfsd-ctl')
44
const waterfall = require('async/waterfall')
55

66
class GoDaemon {
7-
constructor () {
7+
constructor (opts) {
8+
opts = opts || {
9+
disposable: true,
10+
init: true
11+
}
12+
13+
this.init = opts.init
14+
this.path = opts.path
15+
this.disposable = opts.disposable
816
this.node = null
917
this.api = null
1018
}
1119

1220
start (callback) {
13-
console.log('starting go')
1421
waterfall([
15-
(cb) => ctl.disposable(cb),
22+
(cb) => {
23+
if (this.disposable) {
24+
ctl.disposable({init: this.init}, cb)
25+
} else if (this.init) {
26+
ctl.local(this.path, (err, node) => {
27+
if (err) {
28+
return cb(err)
29+
}
30+
node.init((err) => cb(err, node))
31+
})
32+
} else {
33+
ctl.local(this.path, cb)
34+
}
35+
},
1636
(node, cb) => {
1737
this.node = node
1838
this.node.startDaemon(cb)
@@ -21,7 +41,7 @@ class GoDaemon {
2141
this.api = api
2242
cb()
2343
}
24-
], callback)
44+
], (err) => callback(err))
2545
}
2646

2747
stop (callback) {

test/interop/daemons/js.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,43 @@ const os = require('os')
44
const IPFSAPI = require('ipfs-api')
55
const series = require('async/series')
66
const rimraf = require('rimraf')
7+
const IPFSRepo = require('ipfs-repo')
78

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

1112
class JsDaemon {
12-
constructor () {
13-
this.repoPath = os.tmpdir() + `${Math.ceil(Math.random() * 100)}`
14-
this.ipfs = new IPFS(this.repoPath)
13+
constructor (opts) {
14+
opts = opts || {
15+
disposable: true,
16+
init: true
17+
}
18+
19+
this.path = opts.path
20+
this.disposable = opts.disposable
21+
this.init = opts.init
22+
23+
this.path = opts.path || os.tmpdir() + `/${Math.ceil(Math.random() * 1000)}`
24+
if (this.init) {
25+
this.ipfs = new IPFS(this.path)
26+
} else {
27+
const repo = new IPFSRepo(this.path, {stores: require('fs-pull-blob-store')})
28+
this.ipfs = new IPFS(repo)
29+
}
1530
this.node = null
1631
this.api = null
1732
}
1833

1934
start (callback) {
20-
console.log('starting js', this.repoPath)
35+
console.log('starting js', this.path)
2136
series([
22-
(cb) => this.ipfs.init(cb),
37+
(cb) => {
38+
if (this.init) {
39+
this.ipfs.init(cb)
40+
} else {
41+
cb()
42+
}
43+
},
2344
(cb) => {
2445
this.node = new HTTPAPI(this.ipfs._repo)
2546
this.node.start(cb)
@@ -28,14 +49,20 @@ class JsDaemon {
2849
this.api = new IPFSAPI(this.node.apiMultiaddr)
2950
cb()
3051
}
31-
], callback)
52+
], (err) => callback(err))
3253
}
3354

3455
stop (callback) {
3556
series([
3657
(cb) => this.node.stop(cb),
37-
(cb) => rimraf(this.repoPath, cb)
38-
], callback)
58+
(cb) => {
59+
if (this.disposable) {
60+
rimraf(this.path, cb)
61+
} else {
62+
cb()
63+
}
64+
}
65+
], (err) => callback(err))
3966
}
4067
}
4168

test/interop/interop-repo.js

Whitespace-only changes.

test/interop/node.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
'use strict'
22

33
require('./index')
4+
require('./repo')

test/interop/repo.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const expect = require('chai').expect
5+
const waterfall = require('async/waterfall')
6+
const bl = require('bl')
7+
const crypto = require('crypto')
8+
const os = require('os')
9+
10+
const GoDaemon = require('./daemons/go')
11+
const JsDaemon = require('./daemons/js')
12+
13+
function catAndCheck (daemon, hash, data, callback) {
14+
waterfall([
15+
(cb) => daemon.api.cat(hash, cb),
16+
(stream, cb) => stream.pipe(bl(cb))
17+
], (err, file) => {
18+
console.log('got file')
19+
expect(err).to.not.exist
20+
expect(file).to.be.eql(data)
21+
callback()
22+
})
23+
}
24+
25+
// TODO: these are not working, will need to figure out why
26+
describe.skip('interop - repo', () => {
27+
it('read repo: go -> js', (done) => {
28+
const dir = os.tmpdir() + '/' + Math.ceil(Math.random() * 10000)
29+
const data = crypto.randomBytes(1024 * 5)
30+
31+
const goDaemon = new GoDaemon({init: true, disposable: false, path: dir})
32+
let jsDaemon
33+
34+
let hash
35+
waterfall([
36+
(cb) => goDaemon.start(cb),
37+
(cb) => goDaemon.api.add(data, cb),
38+
(res, cb) => {
39+
hash = res[0].hash
40+
catAndCheck(goDaemon, hash, data, cb)
41+
},
42+
(cb) => goDaemon.stop(cb),
43+
(cb) => {
44+
jsDaemon = new JsDaemon({init: false, disposable: false, path: dir})
45+
jsDaemon.start(cb)
46+
},
47+
(cb) => catAndCheck(goDaemon, hash, data, cb),
48+
(cb) => jsDaemon.stop(cb)
49+
], done)
50+
})
51+
52+
it.only('read repo: js -> go', (done) => {
53+
const dir = os.tmpdir() + '/' + Math.ceil(Math.random() * 10000)
54+
const data = crypto.randomBytes(1024 * 5)
55+
56+
const jsDaemon = new JsDaemon({init: true, disposable: false, path: dir})
57+
let goDaemon
58+
59+
let hash
60+
waterfall([
61+
(cb) => jsDaemon.start(cb),
62+
(cb) => jsDaemon.api.add(data, cb),
63+
(res, cb) => {
64+
hash = res[0].hash
65+
catAndCheck(jsDaemon, hash, data, cb)
66+
},
67+
(cb) => jsDaemon.stop(cb),
68+
(cb) => {
69+
goDaemon = new GoDaemon({init: false, disposable: false, path: dir})
70+
goDaemon.start(cb)
71+
},
72+
(cb) => catAndCheck(goDaemon, hash, data, cb),
73+
(cb) => goDaemon.stop(cb)
74+
], done)
75+
})
76+
})

0 commit comments

Comments
 (0)