-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
255 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
var run = require('comandante') | ||
var _ = require('lodash') | ||
var Q = require('kew') | ||
var ipfs = require('ipfs-api') | ||
var waterfall = require('promise-waterfall') | ||
|
||
function configureNode (node, conf, cb) { | ||
var delay = 0 | ||
waterfall(_.map(conf, function (value, key) { | ||
return function () { | ||
var def = Q.defer() | ||
run('ipfs', ['config', key, value]) | ||
.on('error', function (err) { cb(err) }) | ||
.on('end', function () { def.resolve() }) | ||
return def.promise | ||
} | ||
})).then(function () { | ||
cb(null, true) | ||
}) | ||
} | ||
|
||
var ctl = function (path) { | ||
var env = process.env | ||
env.IPFS_PATH = path | ||
return { | ||
path: path, | ||
init: function (opts, cb) { | ||
var t = this | ||
if (!cb) cb = opts | ||
var buf = '' | ||
run('ipfs', ['init'], {env: env}) | ||
.on('error', function (err) { cb(err) }) | ||
.on('data', function (data) { buf += data }) | ||
.on('end', function () { | ||
configureNode(t, opts, function (err) { | ||
if (err) return cb(err) | ||
cb(null, buf) | ||
}) | ||
}) | ||
}, | ||
getConf: function (key, cb) { | ||
var result = '' | ||
run('ipfs', ['config', key]) | ||
.on('error', function (err) { cb(err) }) | ||
.on('data', function (data) { result += data }) | ||
.on('end', function () { cb(null, result.trim()) }) | ||
}, | ||
daemon: function (opts, cb) { | ||
if (!cb) cb = opts | ||
var t = this | ||
var running = run('ipfs', ['daemon'], {env: env}) | ||
|
||
running | ||
.on('error', function (err) { cb(err) }) | ||
.on('data', function (data) { | ||
if ((data + '').match(/API server listening/)) { | ||
t.getConf('Addresses.API', function (err, value) { | ||
if (err) throw err | ||
var split = value.split('/') | ||
t.pid = running.pid | ||
cb(null, ipfs('127.0.0.1', split[split.length - 1]), t.pid) | ||
}) | ||
} | ||
}) | ||
}, | ||
stop: function (cb) { | ||
if (this.pid) { | ||
run('kill', [this.pid]) | ||
.on('error', function (err) { cb(err) }) | ||
.on('end', function () { cb(null) }) | ||
} else { | ||
// not started, no problem | ||
cb(null) | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = ctl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
var run = require('comandante') | ||
var ipfsd = require('../index.js') | ||
var assert = require('assert') | ||
var fs = require('fs') | ||
var rimraf = require('rimraf') | ||
|
||
var tempDirectories = [] | ||
function tmpDirectory () { | ||
var dir = '/tmp/ipfs_' + (Math.random() + '').substr(2) | ||
tempDirectories.push(dir) | ||
return dir | ||
} | ||
|
||
function randomPort () { | ||
// pick a really high port to make collisions unlikely enough | ||
return Math.floor((Math.random() * (65535 - 10000))) + 10000 | ||
} | ||
|
||
function cleanup (done) { | ||
for (var i = 0; i < tempDirectories.length; i++) { | ||
rimraf(tempDirectories[i], function () {}) | ||
} | ||
tempDirectories = [] | ||
done() | ||
} | ||
|
||
describe('single node', function () { | ||
var apiPort = randomPort() | ||
var gatewayPort = randomPort() | ||
|
||
var api = '/ip4/127.0.0.1/tcp/' + apiPort | ||
var gateway = '/ip4/127.0.0.1/tcp/' + gatewayPort | ||
|
||
var node = ipfsd(tmpDirectory()) | ||
|
||
describe('init', function () { | ||
var result | ||
|
||
// 10 seconds should be enough for everyone | ||
this.timeout(10000) | ||
|
||
before(function (done) { | ||
node.init({'Addresses.Gateway': gateway, | ||
'Addresses.API': api}, | ||
function (err, res) { | ||
if (err) throw err | ||
result = res | ||
done() | ||
}) | ||
}) | ||
|
||
it('should have setup a node', function () { | ||
assert(result.match(/initializing ipfs node at/)) | ||
}) | ||
}) | ||
|
||
describe('check gateway conf', function () { | ||
var testgateway = '' | ||
|
||
before(function (done) { | ||
node.getConf('Addresses.Gateway', function (err, res) { | ||
if (err) throw err | ||
testgateway = res | ||
done() | ||
}) | ||
}) | ||
|
||
it('should have the correct gateway addr configured', function () { | ||
assert.equal(testgateway.trim(), gateway) | ||
}) | ||
}) | ||
|
||
describe('check api conf', function () { | ||
var testapi = '' | ||
|
||
before(function (done) { | ||
node.getConf('Addresses.API', function (err, res) { | ||
if (err) throw err | ||
testapi = res | ||
done() | ||
}) | ||
}) | ||
|
||
it('should have the correct api addr configured', function () { | ||
assert.equal(testapi.trim(), api) | ||
}) | ||
}) | ||
|
||
describe('start daemon', function () { | ||
var ipfs | ||
|
||
this.timeout(10000) | ||
|
||
before(function (done) { | ||
node.daemon(function (err, api) { | ||
if (err) throw err | ||
ipfs = api | ||
done() | ||
}) | ||
}) | ||
|
||
it('should have started the daemon and returned an api', function () { | ||
assert(ipfs) | ||
assert(ipfs.add) | ||
}) | ||
|
||
var store, retrieve | ||
|
||
before(function (done) { | ||
var blorb = Buffer('blorb') | ||
ipfs.block.put(blorb, function (err, res) { | ||
if (err) throw err | ||
store = res.Key | ||
|
||
ipfs.block.get(res.Key, function (err, res) { | ||
if (err) throw err | ||
retrieve = res | ||
|
||
var buf = '' | ||
res | ||
.on('data', function (data) { buf += data }) | ||
.on('end', function () { | ||
retrieve = buf | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
it('should be able to store objects', function () { | ||
assert.equal(store, 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ') | ||
}) | ||
|
||
it('should be able to retrieve objects', function () { | ||
assert.equal(retrieve, 'blorb') | ||
}) | ||
}) | ||
|
||
describe('stop deamon', function () { | ||
var killed = false | ||
before(function (done) { | ||
node.stop(function (err) { | ||
if (err) throw err | ||
killed = true | ||
done() | ||
}) | ||
}) | ||
|
||
it('should have killed the process', function () { | ||
assert(killed) | ||
}) | ||
}) | ||
|
||
after(function (done) { | ||
cleanup(done) | ||
}) | ||
}) |