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

feat(factory): add ipfs factory, verify it works with object tests #407

Merged
merged 3 commits into from
Aug 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"form-data": "^1.0.0-rc4",
"gulp": "^3.9.1",
"idb-plus-blob-store": "^1.1.2",
"interface-ipfs-core": "^0.6.0",
"interface-ipfs-core": "^0.7.2",
"left-pad": "^1.1.1",
"lodash": "^4.14.1",
"ncp": "^2.0.0",
Expand Down Expand Up @@ -121,4 +121,4 @@
"kumavis <[email protected]>",
"nginnever <[email protected]>"
]
}
}
5 changes: 3 additions & 2 deletions test/cli/test-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const nexpect = require('nexpect')
const HttpAPI = require('../../src/http-api')
const repoPath = require('./index').repoPath
const _ = require('lodash')
const pkgversion = require('../../package.json').version

describe('version', () => {
const env = _.clone(process.env)
Expand All @@ -16,7 +17,7 @@ describe('version', () => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'version'], {env})
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(stdout[0]).to.equal('js-ipfs version: 0.14.1')
expect(stdout[0]).to.equal('js-ipfs version: ' + pkgversion)
expect(exitcode).to.equal(0)
done()
})
Expand Down Expand Up @@ -46,7 +47,7 @@ describe('version', () => {
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)
expect(stdout[0]).to.equal('js-ipfs version: 0.14.1')
expect(stdout[0]).to.equal('js-ipfs version: ' + pkgversion)
done()
})
})
Expand Down
12 changes: 6 additions & 6 deletions test/core/both/test-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
'use strict'

const test = require('interface-ipfs-core')
const IPFS = require('../../../src/core')
const IPFSFactory = require('../../utils/factory')

let factory

const common = {
setup: function (cb) {
const ipfs = new IPFS(require('../../utils/repo-path'))
ipfs.load(() => {
cb(null, ipfs)
})
factory = new IPFSFactory()
cb(null, factory)
},
teardown: function (cb) {
cb()
factory.dismantle(cb)
}
}

Expand Down
11 changes: 5 additions & 6 deletions test/core/both/test-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
'use strict'

const test = require('interface-ipfs-core')
const IPFSFactory = require('../../utils/factory')

const IPFS = require('../../../src/core')
let factory

const common = {
setup: function (cb) {
const ipfs = new IPFS(require('../../utils/repo-path'))
ipfs.load(() => {
cb(null, ipfs)
})
factory = new IPFSFactory()
cb(null, factory)
},
teardown: function (cb) {
cb()
factory.dismantle(cb)
}
}

Expand Down
3 changes: 2 additions & 1 deletion test/core/both/test-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use strict'

const expect = require('chai').expect
const pkgversion = require('../../../package.json').version

const IPFS = require('../../../src/core')

Expand All @@ -16,7 +17,7 @@ describe('version', () => {
it('get version', (done) => {
ipfs.version((err, version) => {
expect(err).to.not.exist
expect(version).to.equal('0.14.1')
expect(version).to.equal(pkgversion)
done()
})
})
Expand Down
3 changes: 2 additions & 1 deletion test/http-api/test-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const expect = require('chai').expect
const APIctl = require('ipfs-api')
const pkgversion = require('../../package.json').version

module.exports = (httpAPI) => {
describe('version', () => {
Expand All @@ -18,7 +19,7 @@ module.exports = (httpAPI) => {
method: 'GET',
url: '/api/v0/version'
}, (res) => {
expect(res.result.Version).to.equal('0.14.1')
expect(res.result.Version).to.equal(pkgversion)
expect(res.result).to.have.a.property('Commit')
expect(res.result).to.have.a.property('Repo')
done()
Expand Down
68 changes: 68 additions & 0 deletions test/utils/factory/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
}
}
118 changes: 118 additions & 0 deletions test/utils/factory/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
'use strict'

const PeerId = require('peer-id')
const isNode = require('detect-node')
const IPFSRepo = require('ipfs-repo')
const cleanRepo = require('../clean')
const IPFS = require('../../../src/core')
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
let store
let teardown

if (isNode) {
store = require('fs-blob-store')
teardown = (done) => {
cleanRepo(repoPath)
done()
}
} else {
const idb = window.indexedDB ||
window.mozIndexedDB ||
window.webkitIndexedDB ||
window.msIndexedDB
store = require('idb-plus-blob-store')
teardown = (done) => {
idb.deleteDatabase(repoPath)
idb.deleteDatabase(repoPath + '/blocks')
done()
}
}

const repo = new IPFSRepo(repoPath, { stores: store })
repo.teardown = teardown

// 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 () {
ipfs.load((err) => {
if (err) {
return callback(err)
}

ipfs.goOnline((err) => {
if (err) {
return callback(err)
}

nodes.push({
repo: repo,
ipfs: ipfs
})

callback(null, ipfs)
})
})
}
}

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

function clean (err) {
if (err) {
return callback(err)
}
series(
nodes.map((node) => {
return node.repo.teardown
}),
callback
)
}
}
}