From 7a1b9d6e5aff2044a778a36e699491607abc448b Mon Sep 17 00:00:00 2001 From: David Dias Date: Wed, 10 Aug 2016 19:44:17 +0100 Subject: [PATCH 1/3] feat(factory): use the factory mode --- package.json | 2 +- test/api/files.spec.js | 16 +++++++++++++++- test/api/object.spec.js | 12 +++++++++++- test/notes.org | 12 ------------ 4 files changed, 27 insertions(+), 15 deletions(-) delete mode 100644 test/notes.org diff --git a/package.json b/package.json index 6f8915b59..31de399a5 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "aegir": "^6.0.0", "chai": "^3.5.0", "gulp": "^3.9.1", - "interface-ipfs-core": "^0.6.0", + "interface-ipfs-core": "^0.7.2", "ipfsd-ctl": "^0.14.0", "passthrough-counter": "^1.0.0", "pre-commit": "^1.1.3", diff --git a/test/api/files.spec.js b/test/api/files.spec.js index 731a37395..383875c67 100644 --- a/test/api/files.spec.js +++ b/test/api/files.spec.js @@ -15,7 +15,21 @@ testfile = require('fs').readFileSync(path.join(__dirname, '/../testfile.txt')) // Load the add/cat/get/ls commands from interface-ipfs-core const common = { setup: function (cb) { - cb(null, apiClients.a) + 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')) + } + } + }) }, teardown: function (cb) { cb() diff --git a/test/api/object.spec.js b/test/api/object.spec.js index ae50909dd..8b22987b7 100644 --- a/test/api/object.spec.js +++ b/test/api/object.spec.js @@ -7,7 +7,17 @@ const test = require('interface-ipfs-core') const common = { setup: function (cb) { - cb(null, apiClients.a) + let c = 0 + cb(null, { + spawnNode: (path, config, callback) => { + 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')) + } + } + }) }, teardown: function (cb) { cb() diff --git a/test/notes.org b/test/notes.org deleted file mode 100644 index 17cff15fb..000000000 --- a/test/notes.org +++ /dev/null @@ -1,12 +0,0 @@ -* broken -config replace -* left to test -** mount -needs unmount command? -** diag -** object put protobuf -** swarm -** dht -** ping -** name -resolve depends on publish From e40f7d5b41fd09695c173a6eb8f42c54b839fe66 Mon Sep 17 00:00:00 2001 From: David Dias Date: Thu, 11 Aug 2016 14:36:14 +0100 Subject: [PATCH 2/3] refactor(tests): avoid having a tasks folder (legacy of pre-aegir) and clean up the tests folder to make the organization more obvious --- .gitignore | 4 ++-- gulpfile.js | 8 +++++++- tasks/test.js | 10 ---------- test/browser.js | 2 +- test/{ => data}/15mb.random | Bin test/{ => data}/r-config.json | 0 test/{ => data}/test-folder/add.js | 0 test/{ => data}/test-folder/cat.js | 0 test/{ => data}/test-folder/files/hello.txt | 0 test/{ => data}/test-folder/files/ipfs.txt | 0 test/{ => data}/test-folder/hello-link | 0 test/{ => data}/test-folder/ipfs-add.js | 0 test/{ => data}/test-folder/ls.js | 0 test/{ => data}/test-folder/version.js | 0 test/{ => data}/testconfig.json | 0 test/{ => data}/testfile.txt | 0 test/{api => interface-ipfs-core}/bitswap.spec.js | 0 test/{api => interface-ipfs-core}/block.spec.js | 0 test/{api => interface-ipfs-core}/bootstrap.spec.js | 0 test/{api => interface-ipfs-core}/commands.spec.js | 0 test/{api => interface-ipfs-core}/config.spec.js | 4 ++-- test/{api => interface-ipfs-core}/dht.spec.js | 0 test/{api => interface-ipfs-core}/diag.spec.js | 0 test/{api => interface-ipfs-core}/files.spec.js | 5 ++--- test/{api => interface-ipfs-core}/get.spec.js | 8 ++------ test/{api => interface-ipfs-core}/id.spec.js | 0 test/{api => interface-ipfs-core}/index.spec.js | 0 test/{api => interface-ipfs-core}/log.spec.js | 0 test/{api => interface-ipfs-core}/ls.spec.js | 0 test/{api => interface-ipfs-core}/mount.spec.js | 0 test/{api => interface-ipfs-core}/name.spec.js | 0 test/{api => interface-ipfs-core}/object.spec.js | 8 ++++++++ test/{api => interface-ipfs-core}/pin.spec.js | 0 test/{api => interface-ipfs-core}/ping.spec.js | 0 test/{api => interface-ipfs-core}/refs.spec.js | 0 test/{api => interface-ipfs-core}/repo.spec.js | 0 test/{api => interface-ipfs-core}/send.spec.js | 0 test/{api => interface-ipfs-core}/swarm.spec.js | 0 test/{api => interface-ipfs-core}/update.spec.js | 0 test/{api => interface-ipfs-core}/version.spec.js | 0 test/{ => ipfs-api}/constructor.spec.js | 4 ++-- test/{ => ipfs-api}/request-api.spec.js | 6 +++--- test/node.js | 2 +- test/{setup.js => setup/setup-ipfs-api-clients.js} | 2 +- tasks/daemons.js => test/setup/spawn-daemons.js | 5 +++-- 45 files changed, 34 insertions(+), 34 deletions(-) delete mode 100644 tasks/test.js rename test/{ => data}/15mb.random (100%) rename test/{ => data}/r-config.json (100%) rename test/{ => data}/test-folder/add.js (100%) rename test/{ => data}/test-folder/cat.js (100%) rename test/{ => data}/test-folder/files/hello.txt (100%) rename test/{ => data}/test-folder/files/ipfs.txt (100%) rename test/{ => data}/test-folder/hello-link (100%) rename test/{ => data}/test-folder/ipfs-add.js (100%) rename test/{ => data}/test-folder/ls.js (100%) rename test/{ => data}/test-folder/version.js (100%) rename test/{ => data}/testconfig.json (100%) rename test/{ => data}/testfile.txt (100%) rename test/{api => interface-ipfs-core}/bitswap.spec.js (100%) rename test/{api => interface-ipfs-core}/block.spec.js (100%) rename test/{api => interface-ipfs-core}/bootstrap.spec.js (100%) rename test/{api => interface-ipfs-core}/commands.spec.js (100%) rename test/{api => interface-ipfs-core}/config.spec.js (96%) rename test/{api => interface-ipfs-core}/dht.spec.js (100%) rename test/{api => interface-ipfs-core}/diag.spec.js (100%) rename test/{api => interface-ipfs-core}/files.spec.js (98%) rename test/{api => interface-ipfs-core}/get.spec.js (94%) rename test/{api => interface-ipfs-core}/id.spec.js (100%) rename test/{api => interface-ipfs-core}/index.spec.js (100%) rename test/{api => interface-ipfs-core}/log.spec.js (100%) rename test/{api => interface-ipfs-core}/ls.spec.js (100%) rename test/{api => interface-ipfs-core}/mount.spec.js (100%) rename test/{api => interface-ipfs-core}/name.spec.js (100%) rename test/{api => interface-ipfs-core}/object.spec.js (73%) rename test/{api => interface-ipfs-core}/pin.spec.js (100%) rename test/{api => interface-ipfs-core}/ping.spec.js (100%) rename test/{api => interface-ipfs-core}/refs.spec.js (100%) rename test/{api => interface-ipfs-core}/repo.spec.js (100%) rename test/{api => interface-ipfs-core}/send.spec.js (100%) rename test/{api => interface-ipfs-core}/swarm.spec.js (100%) rename test/{api => interface-ipfs-core}/update.spec.js (100%) rename test/{api => interface-ipfs-core}/version.spec.js (100%) rename test/{ => ipfs-api}/constructor.spec.js (89%) rename test/{ => ipfs-api}/request-api.spec.js (90%) rename test/{setup.js => setup/setup-ipfs-api-clients.js} (96%) rename tasks/daemons.js => test/setup/spawn-daemons.js (93%) diff --git a/.gitignore b/.gitignore index da170aa32..0234febe2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ node_modules *.log -test/tmp-disposable-nodes-addrs.json +test/setup/tmp-disposable-nodes-addrs.json dist lib -coverage \ No newline at end of file +coverage diff --git a/gulpfile.js b/gulpfile.js index 246ec395c..cfc02a545 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -2,5 +2,11 @@ const gulp = require('gulp') -require('./tasks/test') +require('./test/setup/spawn-daemons') + +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']) + require('aegir/gulp')(gulp) diff --git a/tasks/test.js b/tasks/test.js deleted file mode 100644 index 5b3062738..000000000 --- a/tasks/test.js +++ /dev/null @@ -1,10 +0,0 @@ -'use strict' - -const gulp = require('gulp') - -require('./daemons') - -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']) diff --git a/test/browser.js b/test/browser.js index cc2566c73..ebfd1aada 100644 --- a/test/browser.js +++ b/test/browser.js @@ -1,3 +1,3 @@ 'use strict' -require('./setup') +require('./setup/setup-ipfs-api-clients') diff --git a/test/15mb.random b/test/data/15mb.random similarity index 100% rename from test/15mb.random rename to test/data/15mb.random diff --git a/test/r-config.json b/test/data/r-config.json similarity index 100% rename from test/r-config.json rename to test/data/r-config.json diff --git a/test/test-folder/add.js b/test/data/test-folder/add.js similarity index 100% rename from test/test-folder/add.js rename to test/data/test-folder/add.js diff --git a/test/test-folder/cat.js b/test/data/test-folder/cat.js similarity index 100% rename from test/test-folder/cat.js rename to test/data/test-folder/cat.js diff --git a/test/test-folder/files/hello.txt b/test/data/test-folder/files/hello.txt similarity index 100% rename from test/test-folder/files/hello.txt rename to test/data/test-folder/files/hello.txt diff --git a/test/test-folder/files/ipfs.txt b/test/data/test-folder/files/ipfs.txt similarity index 100% rename from test/test-folder/files/ipfs.txt rename to test/data/test-folder/files/ipfs.txt diff --git a/test/test-folder/hello-link b/test/data/test-folder/hello-link similarity index 100% rename from test/test-folder/hello-link rename to test/data/test-folder/hello-link diff --git a/test/test-folder/ipfs-add.js b/test/data/test-folder/ipfs-add.js similarity index 100% rename from test/test-folder/ipfs-add.js rename to test/data/test-folder/ipfs-add.js diff --git a/test/test-folder/ls.js b/test/data/test-folder/ls.js similarity index 100% rename from test/test-folder/ls.js rename to test/data/test-folder/ls.js diff --git a/test/test-folder/version.js b/test/data/test-folder/version.js similarity index 100% rename from test/test-folder/version.js rename to test/data/test-folder/version.js diff --git a/test/testconfig.json b/test/data/testconfig.json similarity index 100% rename from test/testconfig.json rename to test/data/testconfig.json diff --git a/test/testfile.txt b/test/data/testfile.txt similarity index 100% rename from test/testfile.txt rename to test/data/testfile.txt diff --git a/test/api/bitswap.spec.js b/test/interface-ipfs-core/bitswap.spec.js similarity index 100% rename from test/api/bitswap.spec.js rename to test/interface-ipfs-core/bitswap.spec.js diff --git a/test/api/block.spec.js b/test/interface-ipfs-core/block.spec.js similarity index 100% rename from test/api/block.spec.js rename to test/interface-ipfs-core/block.spec.js diff --git a/test/api/bootstrap.spec.js b/test/interface-ipfs-core/bootstrap.spec.js similarity index 100% rename from test/api/bootstrap.spec.js rename to test/interface-ipfs-core/bootstrap.spec.js diff --git a/test/api/commands.spec.js b/test/interface-ipfs-core/commands.spec.js similarity index 100% rename from test/api/commands.spec.js rename to test/interface-ipfs-core/commands.spec.js diff --git a/test/api/config.spec.js b/test/interface-ipfs-core/config.spec.js similarity index 96% rename from test/api/config.spec.js rename to test/interface-ipfs-core/config.spec.js index 86914958e..ea0a9610c 100644 --- a/test/api/config.spec.js +++ b/test/interface-ipfs-core/config.spec.js @@ -65,7 +65,7 @@ describe('.config', () => { return done() } - apiClients.c.config.replace(path.join(__dirname, '/../r-config.json'), (err, res) => { + apiClients.c.config.replace(path.join(__dirname, '/../data/r-config.json'), (err, res) => { expect(err).to.not.exist expect(res).to.be.equal(null) done() @@ -126,7 +126,7 @@ describe('.config', () => { return } - return apiClients.c.config.replace(path.join(__dirname, '/../r-config.json')) + return apiClients.c.config.replace(path.join(__dirname, '/../data/r-config.json')) .then((res) => { expect(res).to.be.equal(null) }) diff --git a/test/api/dht.spec.js b/test/interface-ipfs-core/dht.spec.js similarity index 100% rename from test/api/dht.spec.js rename to test/interface-ipfs-core/dht.spec.js diff --git a/test/api/diag.spec.js b/test/interface-ipfs-core/diag.spec.js similarity index 100% rename from test/api/diag.spec.js rename to test/interface-ipfs-core/diag.spec.js diff --git a/test/api/files.spec.js b/test/interface-ipfs-core/files.spec.js similarity index 98% rename from test/api/files.spec.js rename to test/interface-ipfs-core/files.spec.js index 383875c67..33e422996 100644 --- a/test/api/files.spec.js +++ b/test/interface-ipfs-core/files.spec.js @@ -7,10 +7,9 @@ const expect = require('chai').expect const isNode = require('detect-node') const path = require('path') const test = require('interface-ipfs-core') +const fs = require('fs') -let testfile - -testfile = require('fs').readFileSync(path.join(__dirname, '/../testfile.txt')) +const testfile = fs.readFileSync(path.join(__dirname, '/../data/testfile.txt')) // Load the add/cat/get/ls commands from interface-ipfs-core const common = { diff --git a/test/api/get.spec.js b/test/interface-ipfs-core/get.spec.js similarity index 94% rename from test/api/get.spec.js rename to test/interface-ipfs-core/get.spec.js index 77a9f7451..4d593c399 100644 --- a/test/api/get.spec.js +++ b/test/interface-ipfs-core/get.spec.js @@ -7,21 +7,17 @@ const expect = require('chai').expect const isNode = require('detect-node') const fs = require('fs') -// const bl = require('bl') const concat = require('concat-stream') const through = require('through2') const streamEqual = require('stream-equal') - const path = require('path') -// const extract = require('tar-stream').extract - -const testfile = fs.readFileSync(path.join(__dirname, '/../testfile.txt')) +const testfile = fs.readFileSync(path.join(__dirname, '/../data/testfile.txt')) let testfileBig if (isNode) { - const tfbPath = path.join(__dirname, '/../15mb.random') + const tfbPath = path.join(__dirname, '/../data/15mb.random') testfileBig = fs.createReadStream(tfbPath, { bufferSize: 128 }) } diff --git a/test/api/id.spec.js b/test/interface-ipfs-core/id.spec.js similarity index 100% rename from test/api/id.spec.js rename to test/interface-ipfs-core/id.spec.js diff --git a/test/api/index.spec.js b/test/interface-ipfs-core/index.spec.js similarity index 100% rename from test/api/index.spec.js rename to test/interface-ipfs-core/index.spec.js diff --git a/test/api/log.spec.js b/test/interface-ipfs-core/log.spec.js similarity index 100% rename from test/api/log.spec.js rename to test/interface-ipfs-core/log.spec.js diff --git a/test/api/ls.spec.js b/test/interface-ipfs-core/ls.spec.js similarity index 100% rename from test/api/ls.spec.js rename to test/interface-ipfs-core/ls.spec.js diff --git a/test/api/mount.spec.js b/test/interface-ipfs-core/mount.spec.js similarity index 100% rename from test/api/mount.spec.js rename to test/interface-ipfs-core/mount.spec.js diff --git a/test/api/name.spec.js b/test/interface-ipfs-core/name.spec.js similarity index 100% rename from test/api/name.spec.js rename to test/interface-ipfs-core/name.spec.js diff --git a/test/api/object.spec.js b/test/interface-ipfs-core/object.spec.js similarity index 73% rename from test/api/object.spec.js rename to test/interface-ipfs-core/object.spec.js index 8b22987b7..c63fd2850 100644 --- a/test/api/object.spec.js +++ b/test/interface-ipfs-core/object.spec.js @@ -10,6 +10,14 @@ const common = { let c = 0 cb(null, { spawnNode: (path, config, callback) => { + if (typeof path === 'function') { + callback = path + path = undefined + } + if (typeof config === 'function') { + callback = config + config = undefined + } switch (c) { case 0: callback(null, apiClients.a); c++; break case 1: callback(null, apiClients.b); c++; break diff --git a/test/api/pin.spec.js b/test/interface-ipfs-core/pin.spec.js similarity index 100% rename from test/api/pin.spec.js rename to test/interface-ipfs-core/pin.spec.js diff --git a/test/api/ping.spec.js b/test/interface-ipfs-core/ping.spec.js similarity index 100% rename from test/api/ping.spec.js rename to test/interface-ipfs-core/ping.spec.js diff --git a/test/api/refs.spec.js b/test/interface-ipfs-core/refs.spec.js similarity index 100% rename from test/api/refs.spec.js rename to test/interface-ipfs-core/refs.spec.js diff --git a/test/api/repo.spec.js b/test/interface-ipfs-core/repo.spec.js similarity index 100% rename from test/api/repo.spec.js rename to test/interface-ipfs-core/repo.spec.js diff --git a/test/api/send.spec.js b/test/interface-ipfs-core/send.spec.js similarity index 100% rename from test/api/send.spec.js rename to test/interface-ipfs-core/send.spec.js diff --git a/test/api/swarm.spec.js b/test/interface-ipfs-core/swarm.spec.js similarity index 100% rename from test/api/swarm.spec.js rename to test/interface-ipfs-core/swarm.spec.js diff --git a/test/api/update.spec.js b/test/interface-ipfs-core/update.spec.js similarity index 100% rename from test/api/update.spec.js rename to test/interface-ipfs-core/update.spec.js diff --git a/test/api/version.spec.js b/test/interface-ipfs-core/version.spec.js similarity index 100% rename from test/api/version.spec.js rename to test/interface-ipfs-core/version.spec.js diff --git a/test/constructor.spec.js b/test/ipfs-api/constructor.spec.js similarity index 89% rename from test/constructor.spec.js rename to test/ipfs-api/constructor.spec.js index 952b12c25..ea6050f7f 100644 --- a/test/constructor.spec.js +++ b/test/ipfs-api/constructor.spec.js @@ -3,11 +3,11 @@ const expect = require('chai').expect -const ipfsAPI = require('../src/index.js') +const ipfsAPI = require('./../../src/index.js') describe('ipfsAPI constructor tests', () => { describe('parameter permuations', () => { - const apiAddrs = require('./tmp-disposable-nodes-addrs.json') + const apiAddrs = require('./../setup/tmp-disposable-nodes-addrs.json') const apiAddr = apiAddrs.a.split('/') function clientWorks (client, done) { diff --git a/test/request-api.spec.js b/test/ipfs-api/request-api.spec.js similarity index 90% rename from test/request-api.spec.js rename to test/ipfs-api/request-api.spec.js index 505edd892..79219b7d7 100644 --- a/test/request-api.spec.js +++ b/test/ipfs-api/request-api.spec.js @@ -3,12 +3,12 @@ const expect = require('chai').expect const isNode = require('detect-node') -const ipfsAPI = require('../src/index.js') +const ipfsAPI = require('./../../src/index.js') const noop = () => {} describe('ipfsAPI request tests', () => { describe('requestAPI', () => { - const apiAddrs = require('./tmp-disposable-nodes-addrs.json') + const apiAddrs = require('./../setup/tmp-disposable-nodes-addrs.json') const apiAddr = apiAddrs.a.split('/') it('excludes port from URL if config.port is falsy', (done) => { @@ -57,7 +57,7 @@ describe('ipfsAPI request tests', () => { res.end() }).listen(6001, () => { ipfsAPI('/ip4/127.0.0.1/tcp/6001') - .config.replace('test/r-config.json', (err) => { + .config.replace('test/data/r-config.json', (err) => { expect(err).to.not.exist server.close(done) }) diff --git a/test/node.js b/test/node.js index cc2566c73..ebfd1aada 100644 --- a/test/node.js +++ b/test/node.js @@ -1,3 +1,3 @@ 'use strict' -require('./setup') +require('./setup/setup-ipfs-api-clients') diff --git a/test/setup.js b/test/setup/setup-ipfs-api-clients.js similarity index 96% rename from test/setup.js rename to test/setup/setup-ipfs-api-clients.js index ba768a0ef..33cc2ae17 100644 --- a/test/setup.js +++ b/test/setup/setup-ipfs-api-clients.js @@ -1,7 +1,7 @@ /* eslint-env mocha */ 'use strict' -const ipfsAPI = require('../src/index.js') +const ipfsAPI = require('./../../src/index.js') const apiAddrs = require('./tmp-disposable-nodes-addrs.json') // a, b, c diff --git a/tasks/daemons.js b/test/setup/spawn-daemons.js similarity index 93% rename from tasks/daemons.js rename to test/setup/spawn-daemons.js index ad1c1909d..380cd176e 100644 --- a/tasks/daemons.js +++ b/test/setup/spawn-daemons.js @@ -1,5 +1,6 @@ 'use strict' -/* eslint max-nested-callbacks: ["error", 8] */ // TODO reduce nesteness +// TODO reduce the callbacks nestness +/* eslint max-nested-callbacks: ["error", 8] */ const gulp = require('gulp') const fs = require('fs') @@ -21,7 +22,7 @@ function startDisposableDaemons (callback) { function finish () { counter++ if (counter === 3) { - const targetPath = path.join(__dirname, '/../test/tmp-disposable-nodes-addrs.json') + const targetPath = path.join(__dirname, '/tmp-disposable-nodes-addrs.json') fs.writeFileSync(targetPath, JSON.stringify(apiAddrs)) callback(ipfsNodes) } From d5ec781ee6e943eae9cdb069de636d47a9ccf97a Mon Sep 17 00:00:00 2001 From: David Dias Date: Fri, 12 Aug 2016 11:54:13 +0100 Subject: [PATCH 3/3] feat(factory): create the factory (with socket.io) and attach it to the tests --- gulpfile.js | 9 +-- package.json | 3 + test/factory/daemon-spawner.js | 89 +++++++++++++++++++++++++ test/factory/factory-client.js | 54 +++++++++++++++ test/factory/factory-server-routes.js | 34 ++++++++++ test/factory/factory-server.js | 28 ++++++++ test/factory/factory-tasks.js | 20 ++++++ test/interface-ipfs-core/dht.spec.js | 1 - test/interface-ipfs-core/files.spec.js | 45 +++++++------ test/interface-ipfs-core/get.spec.js | 24 ++++++- test/interface-ipfs-core/object.spec.js | 31 +++------ test/setup/spawn-daemons.js | 3 +- 12 files changed, 288 insertions(+), 53 deletions(-) create mode 100644 test/factory/daemon-spawner.js create mode 100644 test/factory/factory-client.js create mode 100644 test/factory/factory-server-routes.js create mode 100644 test/factory/factory-server.js create mode 100644 test/factory/factory-tasks.js diff --git a/gulpfile.js b/gulpfile.js index cfc02a545..c3484e169 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -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) diff --git a/package.json b/package.json index 31de399a5..f4950b6f7 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/test/factory/daemon-spawner.js b/test/factory/daemon-spawner.js new file mode 100644 index 000000000..f4f9f9498 --- /dev/null +++ b/test/factory/daemon-spawner.js @@ -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) + }) + }) +} diff --git a/test/factory/factory-client.js b/test/factory/factory-client.js new file mode 100644 index 000000000..07fa65c2c --- /dev/null +++ b/test/factory/factory-client.js @@ -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') + } +} diff --git a/test/factory/factory-server-routes.js b/test/factory/factory-server-routes.js new file mode 100644 index 000000000..4b0d655fe --- /dev/null +++ b/test/factory/factory-server-routes.js @@ -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') + }) + } +} diff --git a/test/factory/factory-server.js b/test/factory/factory-server.js new file mode 100644 index 000000000..6482714f2 --- /dev/null +++ b/test/factory/factory-server.js @@ -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 :) + }) +} diff --git a/test/factory/factory-tasks.js b/test/factory/factory-tasks.js new file mode 100644 index 000000000..d89d10e1f --- /dev/null +++ b/test/factory/factory-tasks.js @@ -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) +}) diff --git a/test/interface-ipfs-core/dht.spec.js b/test/interface-ipfs-core/dht.spec.js index 78bde491f..c24ab049e 100644 --- a/test/interface-ipfs-core/dht.spec.js +++ b/test/interface-ipfs-core/dht.spec.js @@ -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() diff --git a/test/interface-ipfs-core/files.spec.js b/test/interface-ipfs-core/files.spec.js index 33e422996..d504fb596 100644 --- a/test/interface-ipfs-core/files.spec.js +++ b/test/interface-ipfs-core/files.spec.js @@ -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 diff --git a/test/interface-ipfs-core/get.spec.js b/test/interface-ipfs-core/get.spec.js index 4d593c399..4a15577f7 100644 --- a/test/interface-ipfs-core/get.spec.js +++ b/test/interface-ipfs-core/get.spec.js @@ -8,6 +8,7 @@ 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') @@ -15,9 +16,9 @@ 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 }) } @@ -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() diff --git a/test/interface-ipfs-core/object.spec.js b/test/interface-ipfs-core/object.spec.js index c63fd2850..9375bf087 100644 --- a/test/interface-ipfs-core/object.spec.js +++ b/test/interface-ipfs-core/object.spec.js @@ -1,34 +1,19 @@ /* eslint-env mocha */ -/* globals apiClients */ 'use strict' const test = require('interface-ipfs-core') +const FactoryClient = require('../factory/factory-client') + +let fc const common = { - setup: function (cb) { - let c = 0 - cb(null, { - spawnNode: (path, config, callback) => { - if (typeof path === 'function') { - callback = path - path = undefined - } - if (typeof config === 'function') { - callback = config - config = 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) } } diff --git a/test/setup/spawn-daemons.js b/test/setup/spawn-daemons.js index 380cd176e..986073732 100644 --- a/test/setup/spawn-daemons.js +++ b/test/setup/spawn-daemons.js @@ -5,12 +5,11 @@ const gulp = require('gulp') const fs = require('fs') const path = require('path') +const ipfsd = require('ipfsd-ctl') let daemons function startDisposableDaemons (callback) { - const ipfsd = require('ipfsd-ctl') - // a, b, c const ipfsNodes = {}