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

Commit

Permalink
test: cleanup cli tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Sep 18, 2016
1 parent de09176 commit 2ccd14d
Show file tree
Hide file tree
Showing 19 changed files with 350 additions and 814 deletions.
3 changes: 1 addition & 2 deletions src/cli/commands/bitswap/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ module.exports = {
stats.Wantlist = stats.Wantlist || []
stats.Peers = stats.Peers || []

console.log(`
bitswap status
console.log(`bitswap status
blocks received: ${stats.BlocksReceived}
dup blocks received: ${stats.DupBlksReceived}
dup data received: ${stats.DupDataReceived}B
Expand Down
3 changes: 1 addition & 2 deletions src/cli/commands/block/stat.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const utils = require('../../utils')
const bs58 = require('bs58')
const debug = require('debug')
const log = debug('cli:block')
log.error = debug('cli:block:error')
Expand All @@ -24,7 +23,7 @@ module.exports = {
throw err
}

console.log('Key:', bs58.encode(stats.key).toString())
console.log('Key:', stats.key)
console.log('Size:', stats.size)
})
})
Expand Down
1 change: 1 addition & 0 deletions src/cli/commands/bootstrap/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = {
if (err) {
throw err
}

ipfs.bootstrap.add(argv.peer, (err, list) => {
if (err) {
throw err
Expand Down
2 changes: 1 addition & 1 deletion src/core/components/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = function block (self) {
return callback(err)
}
callback(null, {
key: hash,
key: multihash.toB58String(hash),
size: block.data.length
})
})
Expand Down
12 changes: 6 additions & 6 deletions src/http-api/resources/block.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const bs58 = require('bs58')
const mh = require('multihashes')
const multipart = require('ipfs-multipart')
const Block = require('ipfs-block')
const debug = require('debug')
Expand All @@ -17,7 +17,7 @@ exports.parseKey = (request, reply) => {

try {
return reply({
key: new Buffer(bs58.decode(request.query.arg))
key: mh.fromB58String(request.query.arg)
})
} catch (err) {
log.error(err)
Expand Down Expand Up @@ -93,7 +93,7 @@ exports.put = {
}

return reply({
Key: bs58.encode(block.key).toString(),
Key: mh.toB58String(block.key),
Size: block.data.length
})
})
Expand All @@ -112,7 +112,7 @@ exports.del = {
if (err) {
log.error(err)
return reply({
Message: 'Failed to get block stats: ' + err,
Message: 'Failed to delete block: ' + err,
Code: 0
}).code(500)
}
Expand All @@ -129,7 +129,7 @@ exports.stat = {
// main route handler which is called after the above `parseArgs`, but only if the args were valid
handler: (request, reply) => {
const key = request.pre.args.key

console.log('fetching', key)
request.server.app.ipfs.block.stat(key, (err, block) => {
if (err) {
log.error(err)
Expand All @@ -140,7 +140,7 @@ exports.stat = {
}

return reply({
Key: bs58.encode(block.key).toString(),
Key: block.key,
Size: block.size
})
})
Expand Down
58 changes: 47 additions & 11 deletions src/http-api/resources/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,64 @@
'use strict'

const boom = require('boom')
const multiaddr = require('multiaddr')

exports = module.exports

// common pre request handler that parses the args and returns `key` which is assigned to `request.pre.args`
exports.parseKey = (request, reply) => {
if (!request.query.arg) {
return reply("Argument 'multiaddr' is required").code(400).takeover()
}

try {
return reply({
addr: multiaddr(request.query.arg)
})
} catch (err) {
return reply({
Message: 'Not a valid multiaddr',
Code: 0
}).code(500).takeover()
}
}

exports.list = (request, reply) => {
request.server.app.ipfs.bootstrap.list((err, list) => {
const ipfs = request.server.app.ipfs
ipfs.bootstrap.list((err, list) => {
if (err) {
return reply(boom.badRequest(err))
}
return reply(list)
})
}

exports.add = (request, reply) => {
// request.server.app.ipfs.id((err, id) => {
// if (err) { return reply(boom.badRequest(err)) }
// return reply(id)
// })
exports.add = {
parseArgs: exports.parseKey,
handler (request, reply) {
const ipfs = request.server.app.ipfs
const addr = request.pre.args.addr

ipfs.bootstrap.add(addr.toString(), (err, list) => {
if (err) {
return reply(boom.badRequest(err))
}
return reply()
})
}
}

exports.rm = (request, reply) => {
// request.server.app.ipfs.id((err, id) => {
// if (err) { return reply(boom.badRequest(err)) }
// return reply(id)
// })
exports.rm = {
parseArgs: exports.parseKey,
handler (request, reply) {
const ipfs = request.server.app.ipfs
const addr = request.pre.args.addr

ipfs.bootstrap.rm(addr.toString(), (err, list) => {
if (err) {
return reply(boom.badRequest(err))
}
return reply()
})
}
}
23 changes: 8 additions & 15 deletions src/http-api/routes/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const resources = require('./../resources')
const Joi = require('joi')

module.exports = (server) => {
const api = server.select('API')
Expand All @@ -17,14 +16,11 @@ module.exports = (server) => {
api.route({
method: '*',
path: '/api/v0/bootstrap/add',
handler: resources.bootstrap.add,
config: {
validate: {
query: {
arg: Joi.string().required(), // multiaddr to add
default: Joi.boolean()
}
}
pre: [
{ method: resources.bootstrap.add.parseArgs, assign: 'args' }
],
handler: resources.bootstrap.add.handler
}
})

Expand All @@ -39,14 +35,11 @@ module.exports = (server) => {
api.route({
method: '*',
path: '/api/v0/bootstrap/rm',
handler: resources.bootstrap.rm,
config: {
validate: {
query: {
arg: Joi.string().required(), // multiaddr to rm
all: Joi.boolean()
}
}
pre: [
{ method: resources.bootstrap.rm.parseArgs, assign: 'args' }
],
handler: resources.bootstrap.rm.handler
}
})
}
68 changes: 28 additions & 40 deletions test/cli/test-bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,32 @@
'use strict'

const expect = require('chai').expect
const nexpect = require('nexpect')
const Block = require('ipfs-block')
const _ = require('lodash')
const bs58 = require('bs58')

const HttpAPI = require('../../src/http-api')
const createTempNode = require('../utils/temp-node')
const repoPath = require('./index').repoPath
const ipfs = require('../utils/ipfs')(repoPath)

describe('bitswap', function () {
this.timeout(40000)
const env = _.clone(process.env)
env.IPFS_PATH = repoPath

let ipfs
describe('bitswap', () => {
let node

before((done) => {
createTempNode(38, (err, _ipfs) => {
createTempNode(38, (err, _node) => {
expect(err).to.not.exist
ipfs = _ipfs
ipfs.goOnline(done)
node = _node
node.goOnline(done)
})
})

after((done) => {
node.goOffline(done)
})

describe('api running', () => {
const block = new Block('hello')
const key = bs58.encode(block.key)

let httpAPI

before((done) => {
Expand All @@ -41,42 +39,32 @@ describe('bitswap', function () {
httpAPI.stop(done)
})

it('wantlist', (done) => {
it('wantlist', () => {
const api = httpAPI.server.select('API')

api.inject({
method: 'GET',
url: `/api/v0/block/get?arg=${key}`
}, (res) => {})
}, () => {})

nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'bitswap', 'wantlist'], {env})
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)
expect(stdout).to.be.eql([
key
])
done()
})
return ipfs('bitswap wantlist').then((out) => {
expect(out).to.be.eql(key)
})
})

it('stat', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'bitswap', 'stat'], {env})
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)
expect(stdout).to.be.eql([
'bitswap status',
' blocks received: 0',
' dup blocks received: 0',
' dup data received: 0B',
' wantlist [1 keys]',
` ${key}`,
' partners [0]',
' '
])
done()
})
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'))
})
})
})
})

0 comments on commit 2ccd14d

Please sign in to comment.