From 08a4b19694143699138cbb708e9d5215bf456500 Mon Sep 17 00:00:00 2001 From: David Dias Date: Tue, 23 Aug 2016 16:10:35 +0100 Subject: [PATCH] feat(tests): factory-http --- src/http-api/index.js | 3 + test/core/both/test-config.js | 2 +- test/core/both/test-files.js | 2 +- test/core/both/test-generic.js | 2 +- test/core/both/test-object.js | 2 +- test/http-api/index.js | 10 +- .../test-block.js | 0 .../test-config.js | 0 .../test-files.js | 0 .../test-object.js | 20 ++++ .../test-swarm.js | 0 .../default-config.json | 0 test/utils/{factory => factory-core}/index.js | 0 test/utils/factory-http/default-config.json | 68 +++++++++++++ test/utils/factory-http/index.js | 99 +++++++++++++++++++ 15 files changed, 203 insertions(+), 5 deletions(-) create mode 100644 test/http-api/interface-ipfs-core-over-ipfs-api/test-block.js create mode 100644 test/http-api/interface-ipfs-core-over-ipfs-api/test-config.js create mode 100644 test/http-api/interface-ipfs-core-over-ipfs-api/test-files.js create mode 100644 test/http-api/interface-ipfs-core-over-ipfs-api/test-object.js create mode 100644 test/http-api/interface-ipfs-core-over-ipfs-api/test-swarm.js rename test/utils/{factory => factory-core}/default-config.json (100%) rename test/utils/{factory => factory-core}/index.js (100%) create mode 100644 test/utils/factory-http/default-config.json create mode 100644 test/utils/factory-http/index.js diff --git a/src/http-api/index.js b/src/http-api/index.js index e847577e6c..91b93ed130 100644 --- a/src/http-api/index.js +++ b/src/http-api/index.js @@ -7,6 +7,7 @@ const fs = require('fs') const path = require('path') const IPFSRepo = require('ipfs-repo') const fsbs = require('fs-blob-store') +const multiaddr = require('multiaddr') const log = debug('api') log.error = debug('api:error') @@ -64,6 +65,7 @@ exports = module.exports = function HttpApi (repo) { port: api[4], labels: 'API' }) + this.server.connection({ host: gateway[2], port: gateway[4], @@ -80,6 +82,7 @@ exports = module.exports = function HttpApi (repo) { } const api = this.server.select('API') const gateway = this.server.select('Gateway') + this.apiMultiaddr = multiaddr('/ip4/127.0.0.1/tcp/' + api.info.port) console.log('API is listening on: %s', api.info.uri) console.log('Gateway (readonly) is listening on: %s', gateway.info.uri) callback() diff --git a/test/core/both/test-config.js b/test/core/both/test-config.js index 5c7c2e5399..44cf2afb2f 100644 --- a/test/core/both/test-config.js +++ b/test/core/both/test-config.js @@ -3,7 +3,7 @@ 'use strict' const test = require('interface-ipfs-core') -const IPFSFactory = require('../../utils/factory') +const IPFSFactory = require('../../utils/factory-core') let factory diff --git a/test/core/both/test-files.js b/test/core/both/test-files.js index e6772ce706..e65d650405 100644 --- a/test/core/both/test-files.js +++ b/test/core/both/test-files.js @@ -2,7 +2,7 @@ 'use strict' const test = require('interface-ipfs-core') -const IPFSFactory = require('../../utils/factory') +const IPFSFactory = require('../../utils/factory-core') let factory diff --git a/test/core/both/test-generic.js b/test/core/both/test-generic.js index e8be3759fd..56be1b6bb0 100644 --- a/test/core/both/test-generic.js +++ b/test/core/both/test-generic.js @@ -3,7 +3,7 @@ 'use strict' const test = require('interface-ipfs-core') -const IPFSFactory = require('../../utils/factory') +const IPFSFactory = require('../../utils/factory-core') let factory diff --git a/test/core/both/test-object.js b/test/core/both/test-object.js index 4ce9e4fd0e..5b3b091d0b 100644 --- a/test/core/both/test-object.js +++ b/test/core/both/test-object.js @@ -3,7 +3,7 @@ 'use strict' const test = require('interface-ipfs-core') -const IPFSFactory = require('../../utils/factory') +const IPFSFactory = require('../../utils/factory-core') let factory diff --git a/test/http-api/index.js b/test/http-api/index.js index b93a7c252f..c6f25f7a26 100644 --- a/test/http-api/index.js +++ b/test/http-api/index.js @@ -47,7 +47,15 @@ describe('HTTP API', () => { }) }) - describe('## interface-ipfs-core tests over ipfs-api', () => {}) // TODO + describe.only('## interface-ipfs-core tests over ipfs-api', () => { + const tests = fs.readdirSync(path.join(__dirname, + '/interface-ipfs-core-over-ipfs-api')) + tests.filter((file) => { + return file.match(/test-.*\.js/) + }).forEach((file) => { + require('./interface-ipfs-core-over-ipfs-api/' + file) + }) + }) describe('## custom ipfs-api tests', () => { const tests = fs.readdirSync(path.join(__dirname, '/ipfs-api')) diff --git a/test/http-api/interface-ipfs-core-over-ipfs-api/test-block.js b/test/http-api/interface-ipfs-core-over-ipfs-api/test-block.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/http-api/interface-ipfs-core-over-ipfs-api/test-config.js b/test/http-api/interface-ipfs-core-over-ipfs-api/test-config.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/http-api/interface-ipfs-core-over-ipfs-api/test-files.js b/test/http-api/interface-ipfs-core-over-ipfs-api/test-files.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/http-api/interface-ipfs-core-over-ipfs-api/test-object.js b/test/http-api/interface-ipfs-core-over-ipfs-api/test-object.js new file mode 100644 index 0000000000..7d5076443e --- /dev/null +++ b/test/http-api/interface-ipfs-core-over-ipfs-api/test-object.js @@ -0,0 +1,20 @@ +/* eslint-env mocha */ + +'use strict' + +const test = require('interface-ipfs-core') +const FactoryClient = require('./../../utils/factory-http') + +let fc + +const common = { + setup: function (callback) { + fc = new FactoryClient() + callback(null, fc) + }, + teardown: function (callback) { + fc.dismantle(callback) + } +} + +test.object(common) diff --git a/test/http-api/interface-ipfs-core-over-ipfs-api/test-swarm.js b/test/http-api/interface-ipfs-core-over-ipfs-api/test-swarm.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/utils/factory/default-config.json b/test/utils/factory-core/default-config.json similarity index 100% rename from test/utils/factory/default-config.json rename to test/utils/factory-core/default-config.json diff --git a/test/utils/factory/index.js b/test/utils/factory-core/index.js similarity index 100% rename from test/utils/factory/index.js rename to test/utils/factory-core/index.js diff --git a/test/utils/factory-http/default-config.json b/test/utils/factory-http/default-config.json new file mode 100644 index 0000000000..20e1d1b753 --- /dev/null +++ b/test/utils/factory-http/default-config.json @@ -0,0 +1,68 @@ +{ + "Identity": { + "PeerID": "", + "PrivKey": "" + }, + "Datastore": { + "Type": "", + "Path": "", + "StorageMax": "", + "StorageGCWatermark": 0, + "GCPeriod": "", + "Params": null, + "NoSync": false + }, + "Addresses": { + "Swarm": [ + "/ip4/127.0.0.1/tcp/0" + ], + "API": "/ip4/127.0.0.1/tcp/0", + "Gateway": "/ip4/127.0.0.1/tcp/0" + }, + "Mounts": { + "IPFS": "/ipfs", + "IPNS": "/ipns", + "FuseAllowOther": false + }, + "Version": { + "Current": "jsipfs-dev", + "Check": "error", + "CheckDate": "0001-01-01T00:00:00Z", + "CheckPeriod": "172800000000000", + "AutoUpdate": "minor" + }, + "Discovery": { + "MDNS": { + "Enabled": true, + "Interval": 10 + } + }, + "Ipns": { + "RepublishPeriod": "", + "RecordLifetime": "", + "ResolveCacheSize": 128 + }, + "Bootstrap": [], + "Tour": { + "Last": "" + }, + "Gateway": { + "HTTPHeaders": null, + "RootRedirect": "", + "Writable": false + }, + "SupernodeRouting": { + "Servers": [] + }, + "API": { + "HTTPHeaders": null + }, + "Swarm": { + "AddrFilters": null + }, + "Log": { + "MaxSizeMB": 250, + "MaxBackups": 1, + "MaxAgeDays": 0 + } +} diff --git a/test/utils/factory-http/index.js b/test/utils/factory-http/index.js new file mode 100644 index 0000000000..58cf2b211f --- /dev/null +++ b/test/utils/factory-http/index.js @@ -0,0 +1,99 @@ +'use strict' + +const PeerId = require('peer-id') +const IPFSRepo = require('ipfs-repo') +const IPFSAPI = require('ipfs-api') +const IPFS = require('../../../src/core') +const cleanRepo = require('../clean') +const HTTPAPI = require('../../../src/http-api') +const series = require('run-series') +const defaultConfig = require('./default-config.json') + +module.exports = Factory + +function Factory () { + if (!(this instanceof Factory)) { + return new Factory() + } + + const nodes = [] + + /* yields a new started node */ + 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) + } + + if (!config) { + config = JSON.parse(JSON.stringify(defaultConfig)) + const pId = PeerId.create({ bits: 32 }).toJSON() + config.Identity.PeerID = pId.id + config.Identity.PrivKey = pId.privKey + } + + // set up the repo + const repo = new IPFSRepo(repoPath, { + stores: require('fs-blob-store') + }) + repo.teardown = (done) => { + cleanRepo(repoPath) + done() + } + + // create the IPFS node + const ipfs = new IPFS(repo) + ipfs.init({ emptyRepo: true }, (err) => { + if (err) { + return callback(err) + } + repo.config.set(config, launchNode) + }) + + function launchNode () { + // create the IPFS node through the HTTP-API + const node = new HTTPAPI(repo) + nodes.push({ + httpApi: node, + repo: repo + }) + + node.start((err) => { + if (err) { + return callback(err) + } + console.log(node.apiMultiaddr) + const ctl = IPFSAPI(node.apiMultiaddr) + callback(null, ctl) + }) + } + } + + this.dismantle = function (callback) { + series(nodes.map((node) => { + return node.httpApi.stop + }), clean) + + function clean (err) { + if (err) { + return callback(err) + } + series( + nodes.map((node) => { + return node.repo.teardown + }), + callback + ) + } + } +}