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

Commit

Permalink
feat(factory): create the factory (with socket.io) and attach it to t…
Browse files Browse the repository at this point in the history
…he tests
  • Loading branch information
daviddias committed Aug 12, 2016
1 parent e40f7d5 commit d5ec781
Show file tree
Hide file tree
Showing 12 changed files with 288 additions and 53 deletions.
9 changes: 5 additions & 4 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
const gulp = require('gulp')

require('./test/setup/spawn-daemons')
require('./test/factory/factory-tasks')

gulp.task('test:node:before', ['daemons:start'])
gulp.task('test:node:after', ['daemons:stop'])
gulp.task('test:browser:before', ['daemons:start'])
gulp.task('test:browser:after', ['daemons:stop'])
gulp.task('test:node:before', ['daemons:start', 'factory:start'])
gulp.task('test:node:after', ['daemons:stop', 'factory:stop'])
gulp.task('test:browser:before', ['daemons:start', 'factory:start'])
gulp.task('test:browser:after', ['daemons:stop', 'factory:stop'])

require('aegir/gulp')(gulp)
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@
"aegir": "^6.0.0",
"chai": "^3.5.0",
"gulp": "^3.9.1",
"hapi": "^14.1.0",
"interface-ipfs-core": "^0.7.2",
"ipfsd-ctl": "^0.14.0",
"passthrough-counter": "^1.0.0",
"pre-commit": "^1.1.3",
"socket.io": "^1.4.8",
"socket.io-client": "^1.4.8",
"stream-equal": "^0.1.8",
"stream-http": "^2.3.1",
"streamifier": "^0.1.1"
Expand Down
89 changes: 89 additions & 0 deletions test/factory/daemon-spawner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
'use strict'

// const defaultConfig = require('./default-config.json')
const ipfsd = require('ipfsd-ctl')
const series = require('run-series')

module.exports = Factory

function Factory () {
if (!(this instanceof Factory)) {
return new Factory()
}

const nodes = []

this.spawnNode = (repoPath, config, callback) => {
if (typeof repoPath === 'function') {
callback = repoPath
repoPath = undefined
}
if (typeof config === 'function') {
callback = config
config = undefined
}

// if (!repoPath) {
// repoPath = '/tmp/.ipfs-' + Math.random()
// .toString()
// .substring(2, 8)
// }

// TODO
// - [ ] Support custom repoPath
// - [ ] Support custom config
// This will come once the new ipfsd-ctl is
// complete: https://github.com/ipfs/js-ipfsd-ctl/pull/89

spawnEphemeralNode((err, node) => {
if (err) {
return callback(err)
}
nodes.push(node)
callback(null, node.apiAddr)
})
}

this.dismantle = function (callback) {
series(
nodes.map((node) => {
return node.stopDaemon
}), callback)
}
}

function spawnEphemeralNode (callback) {
ipfsd.disposable((err, node) => {
if (err) {
return callback(err)
}
// Note: we have to set each config value
// independently since the config/replace endpoint
// doesn't work as expected
series([
(cb) => {
node.setConfig('Bootstrap', null, cb)
},
(cb) => {
node.setConfig('Discovery', '{}', cb)
},
(cb) => {
const headers = {
HTTPHeaders: {
'Access-Control-Allow-Origin': ['*']
}
}
node.setConfig('API', JSON.stringify(headers), cb)
},
(cb) => {
node.startDaemon(cb)
}
], (err) => {
if (err) {
return callback(err)
}

callback(null, node)
})
})
}
54 changes: 54 additions & 0 deletions test/factory/factory-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict'

const io = require('socket.io-client')
const ipfsAPI = require('../../src')

module.exports = Factory

function Factory () {
if (!(this instanceof Factory)) {
return new Factory()
}
const sioOptions = {
transports: ['websocket'],
'force new connection': true
}
const sioUrl = 'http://localhost:55155'
let sioConnected = false
let ioC

this.spawnNode = (repoPath, config, callback) => {
if (typeof repoPath === 'function') {
callback = repoPath
repoPath = undefined
}
if (typeof config === 'function') {
callback = config
config = undefined
}

if (sioConnected) {
spawnNode()
} else {
ioC = io.connect(sioUrl, sioOptions)
ioC.on('connect_error', callback)
ioC.on('connect', () => {
sioConnected = true
spawnNode()
})
}

function spawnNode () {
ioC.once('fc-node', (apiAddr) => {
const ipfs = ipfsAPI(apiAddr)
callback(null, ipfs)
})
ioC.emit('fs-spawn-node', repoPath, config)
}
}

this.dismantle = function (callback) {
ioC.once('fc-nodes-shutdown', callback)
ioC.emit('fs-dismantle')
}
}
34 changes: 34 additions & 0 deletions test/factory/factory-server-routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict'

const SocketIO = require('socket.io')
const DaemonSpawner = require('./daemon-spawner')

module.exports = (http) => {
const io = new SocketIO(http.listener)
io.on('connection', handle)

const ds = new DaemonSpawner()

function handle (socket) {
socket.on('fs-spawn-node', spawnNode.bind(socket))
socket.on('fs-dismantle', dismantle.bind(socket))
}

function spawnNode (repoPath, config) {
ds.spawnNode(repoPath, config, (err, apiAddr) => {
if (err) {
throw err
}
this.emit('fc-node', apiAddr)
})
}

function dismantle () {
ds.dismantle((err) => {
if (err) {
throw err
}
this.emit('fc-nodes-shutdown')
})
}
}
28 changes: 28 additions & 0 deletions test/factory/factory-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict'

const Hapi = require('hapi')

const port = Number(process.env.PORT) || 55155
const options = {
connections: {
routes: {
cors: true
}
}
}

module.exports = (callback) => {
const http = new Hapi.Server(options)

http.connection({ port: port })

http.start((err) => {
if (err) {
return callback(err)
}
require('./factory-server-routes')(http)

callback(null, http)
// note: http.stop(callback) to stop the server :)
})
}
20 changes: 20 additions & 0 deletions test/factory/factory-tasks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'

const gulp = require('gulp')
const factoryServer = require('./factory-server')

let factory

gulp.task('factory:start', (done) => {
factoryServer((err, http) => {
if (err) {
throw err
}
factory = http
done()
})
})

gulp.task('factory:stop', (done) => {
factory.stop(done)
})
1 change: 0 additions & 1 deletion test/interface-ipfs-core/dht.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ describe('.dht', () => {
// non ipns or pk hashes fail to fetch, known bug
// bug: https://github.com/ipfs/go-ipfs/issues/1923#issuecomment-152932234
// apiClients.a.dht.get('scope', (err, value) => {
// console.log('->>', err, value)
// expect(err).to.not.exist
// expect(value).to.be.equal('interplanetary')
// done()
Expand Down
45 changes: 24 additions & 21 deletions test/interface-ipfs-core/files.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,41 @@ const expect = require('chai').expect
const isNode = require('detect-node')
const path = require('path')
const test = require('interface-ipfs-core')
const bs58 = require('bs58')
const fs = require('fs')

const FactoryClient = require('../factory/factory-client')
const testfile = fs.readFileSync(path.join(__dirname, '/../data/testfile.txt'))

// Load the add/cat/get/ls commands from interface-ipfs-core
// add, cat, get and ls tests from interface-ipfs-core
let fc

const common = {
setup: function (cb) {
let c = 0
cb(null, {
spawnNode: (path, config, callback) => {
if (typeof path === 'function') {
callback = path
path = undefined
}
switch (c) {
case 0: callback(null, apiClients.a); c++; break
case 1: callback(null, apiClients.b); c++; break
case 2: callback(null, apiClients.c); c++; break
default: callback(new Error('no more nodes available'))
}
}
})
setup: function (callback) {
fc = new FactoryClient()
callback(null, fc)
},
teardown: function (cb) {
cb()
teardown: function (callback) {
fc.dismantle(callback)
}
}

test.files(common)

// Describe the (mfs) tests
// mfs tests
describe('.files (pseudo mfs)', () => {
it('add file for testing', (done) => {
apiClients.a.files.add(testfile, (err, res) => {
expect(err).to.not.exist

expect(res).to.have.length(1)
const mh = bs58.encode(res[0].node.multihash()).toString()
expect(mh).to.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
expect(res[0].path).to.equal(mh)
expect(res[0].node.links).to.have.length(0)
done()
})
})

it('files.mkdir', (done) => {
apiClients.a.files.mkdir('/test-folder', function (err) {
expect(err).to.not.exist
Expand Down
24 changes: 22 additions & 2 deletions test/interface-ipfs-core/get.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ const expect = require('chai').expect
const isNode = require('detect-node')
const fs = require('fs')
const concat = require('concat-stream')
const bs58 = require('bs58')
const through = require('through2')
const streamEqual = require('stream-equal')
const path = require('path')

const testfile = fs.readFileSync(path.join(__dirname, '/../data/testfile.txt'))

let testfileBig

let tfbPath
if (isNode) {
const tfbPath = path.join(__dirname, '/../data/15mb.random')
tfbPath = path.join(__dirname, '/../data/15mb.random')
testfileBig = fs.createReadStream(tfbPath, { bufferSize: 128 })
}

Expand Down Expand Up @@ -85,6 +86,25 @@ describe('.get', () => {
})
})

it('add a BIG file (for testing get)', (done) => {
if (!isNode) {
return done()
}

const bigFile = fs.readFileSync(tfbPath)

apiClients.a.files.add(bigFile, (err, res) => {
expect(err).to.not.exist

expect(res).to.have.length(1)
expect(res[0].node.links).to.have.length(58)
const mh = bs58.encode(res[0].node.multihash()).toString()
expect(res[0].path).to.equal(mh)
expect(mh).to.equal('Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq')
done()
})
})

it('get BIG file', (done) => {
if (!isNode) {
return done()
Expand Down
Loading

0 comments on commit d5ec781

Please sign in to comment.