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

Commit

Permalink
test: the first pass of a big refactor of tests towards using only ip…
Browse files Browse the repository at this point in the history
…fs-factory to spawn nodes (#750)

* test: mega clean up to the tests
* fix: but in starting api on port 0
  • Loading branch information
daviddias authored Feb 1, 2017
1 parent 6e683b4 commit b910c74
Show file tree
Hide file tree
Showing 113 changed files with 845 additions and 1,130 deletions.
64 changes: 49 additions & 15 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,70 @@
const gulp = require('gulp')
const parallel = require('async/parallel')
const series = require('async/series')
const createTempNode = require('./test/utils/temp-node')
const API = require('./src/http-api')
const createTempRepo = require('./test/utils/create-repo-node.js')
const IPFS = require('./src/core')
const HTTPAPI = require('./src/http-api')
const leftPad = require('left-pad')

let nodes = []

function startNode (num, done) {
createTempNode(num, (err, node) => {
if (err) {
return done(err)
}
/*
* spawns a daemon with ports numbers starting in 10 and ending in `num`
*/
function spawnDaemon (num, callback) {
num = leftPad(num, 3, 0)

const repo = createTempRepo()

const api = new API(node.repo.path())
nodes.push(api)
api.start(done)
const node = new IPFS({
repo: repo,
EXPERIMENTAL: {
pubsub: true
}
})

series([
(cb) => node.init({ emptyRepo: true, bits: 1024 }, cb),
(cb) => {
repo.config.get((err, config) => {
if (err) { return callback(err) }

config.Addresses = {
Swarm: [
`/ip4/127.0.0.1/tcp/10${num}`,
`/ip4/127.0.0.1/tcp/20${num}/ws`
],
API: `/ip4/127.0.0.1/tcp/31${num}`,
Gateway: `/ip4/127.0.0.1/tcp/32${num}`
}

config.Discovery.MDNS.Enabled = false

repo.config.set(config, cb)
})
},
(cb) => node.load(cb),
(cb) => {
const daemon = new HTTPAPI(node.repo.path())
nodes.push(daemon)
daemon.start(cb)
}
], callback)
}

gulp.task('libnode:start', (done) => {
nodes = []
parallel([
(cb) => startNode(7, cb),
(cb) => startNode(8, cb),
(cb) => startNode(12, cb),
(cb) => startNode(13, cb)
(cb) => spawnDaemon(7, cb),
(cb) => spawnDaemon(8, cb),
(cb) => spawnDaemon(12, cb),
(cb) => spawnDaemon(13, cb)
], done)
})

gulp.task('libnode:stop', (done) => {
series(nodes.map((node) => (cb) => {
setTimeout(() => node.stop(cb), 500)
setTimeout(() => node.stop(cb), 200)
}), done)
})

Expand Down
2 changes: 2 additions & 0 deletions src/cli/commands/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ module.exports = {

handler () {
console.log('Initializing daemon...')

httpAPI = new HttpAPI(process.env.IPFS_PATH)

httpAPI.start((err) => {
if (err && err.code === 'ENOENT') {
console.log('Error: no ipfs repo found in ' + process.env.IPFS_PATH)
Expand Down
23 changes: 15 additions & 8 deletions src/http-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ log.error = debug('api:error')
const IPFS = require('../core')
const errorHandler = require('./error-handler')

function uriToMultiaddr (uri) {
const ipPort = uri.split('/')[2].split(':')
return `/ip4/${ipPort[0]}/tcp/${ipPort[1]}`
}

exports = module.exports = function HttpApi (repo) {
this.ipfs = null
this.server = null
Expand Down Expand Up @@ -49,9 +54,7 @@ exports = module.exports = function HttpApi (repo) {
fs.statSync(apiPath)
console.log('This repo is currently being used by another daemon')
process.exit(1)
} catch (err) {
fs.writeFileSync(apiPath, 'api is on by js-ipfs', {flag: 'w+'})
}
} catch (err) {}

this.ipfs.config.get((err, config) => {
if (err) {
Expand All @@ -71,9 +74,6 @@ exports = module.exports = function HttpApi (repo) {
const api = config.Addresses.API.split('/')
const gateway = config.Addresses.Gateway.split('/')

// for the CLI to know the where abouts of the API
fs.writeFileSync(apiPath, config.Addresses.API)

// select which connection with server.select(<label>) to add routes
this.server.connection({
host: api[2],
Expand Down Expand Up @@ -105,8 +105,15 @@ 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)
api.info.ma = uriToMultiaddr(api.info.uri)
gateway.info.ma = uriToMultiaddr(gateway.info.uri)

console.log('API is listening on: %s', api.info.ma)
console.log('Gateway (readonly) is listening on: %s', gateway.info.ma)

// for the CLI to know the where abouts of the API
fs.writeFileSync(apiPath, api.info.ma)

callback()
})
})
Expand Down
39 changes: 39 additions & 0 deletions test/cli/bitswap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const runOn = require('../utils/on-and-off').on

describe('bitswap', () => runOn((thing) => {
let ipfs
const key = 'QmUBdnXXPyoDFXj3Hj39dNJ5VkN3QFRskXxcGaYFBB8CNR'

before((done) => {
ipfs = thing.ipfs
ipfs('block get ' + key)
.then(() => {})
.catch(() => {})
setTimeout(done, 100)
})

it('wantlist', () => {
return ipfs('bitswap wantlist').then((out) => {
expect(out).to.eql(key)
})
})

it('stat', () => {
return ipfs('bitswap stat').then((out) => {
expect(out).to.be.eql([
'bitswap status',
' blocks received: 0',
' dup blocks received: 0',
' dup data received: 0B',
' wantlist [1 keys]',
` ${key}`,
' partners [0]',
' '
].join('\n'))
})
})
}))
48 changes: 48 additions & 0 deletions test/cli/block.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const runOnAndOff = require('../utils/on-and-off')

describe('block', () => runOnAndOff((thing) => {
let ipfs

before(() => {
ipfs = thing.ipfs
})

it('put', () => {
return ipfs('block put test/test-data/hello').then((out) => {
expect(out).to.eql('zdj7Wgpi9yzsvjJerghrdhPFpe1p1jZFyB5GKLyXEzFQyaxVk')
})
})

it('put with flags, format and mhtype', () => {
return ipfs('block put --format eth-block --mhtype keccak-256 test/test-data/eth-block')
.then((out) => expect(out).to.eql('z43AaGF23fmvRnDP56Ub9WcJCfzSfqtmzNCCvmz5eudT8dtdCDS'))
})

it('get', () => {
return ipfs('block get QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp')
.then((out) => expect(out).to.eql('hello world\n'))
})

it('stat', () => {
return ipfs('block stat QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp')
.then((out) => {
expect(out).to.eql([
'Key: QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp',
'Size: 12'
].join('\n'))
})
})

it.skip('rm', () => {
return ipfs('block rm QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp')
.then((out) => {
expect(out).to.eql(
'removed QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp'
)
})
})
}))
78 changes: 78 additions & 0 deletions test/cli/bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* eslint max-nested-callbacks: ["error", 8] */
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const runOnAndOff = require('../utils/on-and-off')

describe('bootstrap', () => runOnAndOff((thing) => {
let ipfs

before(() => {
ipfs = thing.ipfs
})

const defaultList = [
'/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
'/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
'/ip4/104.236.179.241/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
'/ip4/162.243.248.213/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
'/ip4/128.199.219.111/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
'/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
'/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
'/ip4/178.62.61.185/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
'/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx'
]

const updatedList = [
'/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
'/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
'/ip4/104.236.179.241/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
'/ip4/162.243.248.213/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
'/ip4/128.199.219.111/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
'/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
'/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
'/ip4/178.62.61.185/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
'/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx',
'/ip4/111.111.111.111/tcp/1001/ipfs/QmcyFFKfLDGJKwufn2GeitxvhricsBQyNKTkrD14psikoD'
]

it('add default', () => {
return ipfs('bootstrap add --default').then((out) => {
expect(out).to.be.eql(defaultList.join('\n'))
})
})

it('list the bootstrap nodes', () => {
return ipfs('bootstrap list').then((out) => {
expect(out).to.eql(defaultList.join('\n'))
})
})

it('add another bootstrap node', () => {
return ipfs('bootstrap add /ip4/111.111.111.111/tcp/1001/ipfs/QmcyFFKfLDGJKwufn2GeitxvhricsBQyNKTkrD14psikoD').then((out) => {
expect(out).to.be.eql('/ip4/111.111.111.111/tcp/1001/ipfs/QmcyFFKfLDGJKwufn2GeitxvhricsBQyNKTkrD14psikoD')
return ipfs('bootstrap list')
}).then((out) => {
expect(out).to.be.eql(updatedList.join('\n'))
})
})

it('rm a bootstrap node', () => {
return ipfs('bootstrap rm /ip4/111.111.111.111/tcp/1001/ipfs/QmcyFFKfLDGJKwufn2GeitxvhricsBQyNKTkrD14psikoD').then((out) => {
expect(out).to.be.eql('/ip4/111.111.111.111/tcp/1001/ipfs/QmcyFFKfLDGJKwufn2GeitxvhricsBQyNKTkrD14psikoD')
return ipfs('bootstrap list')
}).then((out) => {
expect(out).to.deep.equal(defaultList.join('\n'))
})
})

it('rm all bootstrap nodes', () => {
return ipfs('bootstrap rm --all').then((out) => {
expect(out).to.be.eql('')
return ipfs('bootstrap list')
}).then((out) => {
expect(out).to.deep.equal('')
})
})
}))
23 changes: 23 additions & 0 deletions test/cli/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const repoPath = require('./index').repoPath
const ipfsBase = require('../utils/ipfs-exec')
const runOnAndOff = require('../utils/on-and-off')

const commandCount = 61

describe('commands', () => runOnAndOff((thing) => {
let ipfs

before(() => {
ipfs = thing.ipfs
})

it('list the commands', () => {
return ipfs('commands').then((out) => {
expect(out.split('\n')).to.have.length(commandCount)
})
})
}))
Loading

0 comments on commit b910c74

Please sign in to comment.