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

Commit

Permalink
feat(tests): factory-http
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Aug 23, 2016
1 parent ac5352e commit 08a4b19
Show file tree
Hide file tree
Showing 15 changed files with 203 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/http-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -64,6 +65,7 @@ exports = module.exports = function HttpApi (repo) {
port: api[4],
labels: 'API'
})

this.server.connection({
host: gateway[2],
port: gateway[4],
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion test/core/both/test-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/core/both/test-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/core/both/test-generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/core/both/test-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 9 additions & 1 deletion test/http-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
Empty file.
Empty file.
Empty file.
20 changes: 20 additions & 0 deletions test/http-api/interface-ipfs-core-over-ipfs-api/test-object.js
Original file line number Diff line number Diff line change
@@ -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)
Empty file.
File renamed without changes.
68 changes: 68 additions & 0 deletions test/utils/factory-http/default-config.json
Original file line number Diff line number Diff line change
@@ -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
}
}
99 changes: 99 additions & 0 deletions test/utils/factory-http/index.js
Original file line number Diff line number Diff line change
@@ -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
)
}
}
}

0 comments on commit 08a4b19

Please sign in to comment.