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

Commit e8b2c3c

Browse files
test: cleanup cli tests
1 parent 561b053 commit e8b2c3c

File tree

19 files changed

+350
-814
lines changed

19 files changed

+350
-814
lines changed

src/cli/commands/bitswap/stat.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ module.exports = {
2323
stats.Wantlist = stats.Wantlist || []
2424
stats.Peers = stats.Peers || []
2525

26-
console.log(`
27-
bitswap status
26+
console.log(`bitswap status
2827
blocks received: ${stats.BlocksReceived}
2928
dup blocks received: ${stats.DupBlksReceived}
3029
dup data received: ${stats.DupDataReceived}B

src/cli/commands/block/stat.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const utils = require('../../utils')
4-
const bs58 = require('bs58')
54
const debug = require('debug')
65
const log = debug('cli:block')
76
log.error = debug('cli:block:error')
@@ -24,7 +23,7 @@ module.exports = {
2423
throw err
2524
}
2625

27-
console.log('Key:', bs58.encode(stats.key).toString())
26+
console.log('Key:', stats.key)
2827
console.log('Size:', stats.size)
2928
})
3029
})

src/cli/commands/bootstrap/add.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports = {
1717
if (err) {
1818
throw err
1919
}
20+
2021
ipfs.bootstrap.add(argv.peer, (err, list) => {
2122
if (err) {
2223
throw err

src/core/components/block.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module.exports = function block (self) {
3333
return callback(err)
3434
}
3535
callback(null, {
36-
key: hash,
36+
key: multihash.toB58String(hash),
3737
size: block.data.length
3838
})
3939
})

src/http-api/resources/block.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const bs58 = require('bs58')
3+
const mh = require('multihashes')
44
const multipart = require('ipfs-multipart')
55
const Block = require('ipfs-block')
66
const debug = require('debug')
@@ -17,7 +17,7 @@ exports.parseKey = (request, reply) => {
1717

1818
try {
1919
return reply({
20-
key: new Buffer(bs58.decode(request.query.arg))
20+
key: mh.fromB58String(request.query.arg)
2121
})
2222
} catch (err) {
2323
log.error(err)
@@ -93,7 +93,7 @@ exports.put = {
9393
}
9494

9595
return reply({
96-
Key: bs58.encode(block.key).toString(),
96+
Key: mh.toB58String(block.key),
9797
Size: block.data.length
9898
})
9999
})
@@ -112,7 +112,7 @@ exports.del = {
112112
if (err) {
113113
log.error(err)
114114
return reply({
115-
Message: 'Failed to get block stats: ' + err,
115+
Message: 'Failed to delete block: ' + err,
116116
Code: 0
117117
}).code(500)
118118
}
@@ -129,7 +129,7 @@ exports.stat = {
129129
// main route handler which is called after the above `parseArgs`, but only if the args were valid
130130
handler: (request, reply) => {
131131
const key = request.pre.args.key
132-
132+
console.log('fetching', key)
133133
request.server.app.ipfs.block.stat(key, (err, block) => {
134134
if (err) {
135135
log.error(err)
@@ -140,7 +140,7 @@ exports.stat = {
140140
}
141141

142142
return reply({
143-
Key: bs58.encode(block.key).toString(),
143+
Key: block.key,
144144
Size: block.size
145145
})
146146
})

src/http-api/resources/bootstrap.js

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,64 @@
11
'use strict'
22

33
const boom = require('boom')
4+
const multiaddr = require('multiaddr')
45

56
exports = module.exports
67

8+
// common pre request handler that parses the args and returns `key` which is assigned to `request.pre.args`
9+
exports.parseKey = (request, reply) => {
10+
if (!request.query.arg) {
11+
return reply("Argument 'multiaddr' is required").code(400).takeover()
12+
}
13+
14+
try {
15+
return reply({
16+
addr: multiaddr(request.query.arg)
17+
})
18+
} catch (err) {
19+
return reply({
20+
Message: 'Not a valid multiaddr',
21+
Code: 0
22+
}).code(500).takeover()
23+
}
24+
}
25+
726
exports.list = (request, reply) => {
8-
request.server.app.ipfs.bootstrap.list((err, list) => {
27+
const ipfs = request.server.app.ipfs
28+
ipfs.bootstrap.list((err, list) => {
929
if (err) {
1030
return reply(boom.badRequest(err))
1131
}
1232
return reply(list)
1333
})
1434
}
1535

16-
exports.add = (request, reply) => {
17-
// request.server.app.ipfs.id((err, id) => {
18-
// if (err) { return reply(boom.badRequest(err)) }
19-
// return reply(id)
20-
// })
36+
exports.add = {
37+
parseArgs: exports.parseKey,
38+
handler (request, reply) {
39+
const ipfs = request.server.app.ipfs
40+
const addr = request.pre.args.addr
41+
42+
ipfs.bootstrap.add(addr.toString(), (err, list) => {
43+
if (err) {
44+
return reply(boom.badRequest(err))
45+
}
46+
return reply()
47+
})
48+
}
2149
}
2250

23-
exports.rm = (request, reply) => {
24-
// request.server.app.ipfs.id((err, id) => {
25-
// if (err) { return reply(boom.badRequest(err)) }
26-
// return reply(id)
27-
// })
51+
exports.rm = {
52+
parseArgs: exports.parseKey,
53+
handler (request, reply) {
54+
const ipfs = request.server.app.ipfs
55+
const addr = request.pre.args.addr
56+
57+
ipfs.bootstrap.rm(addr.toString(), (err, list) => {
58+
if (err) {
59+
return reply(boom.badRequest(err))
60+
}
61+
return reply()
62+
})
63+
}
2864
}

src/http-api/routes/bootstrap.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const resources = require('./../resources')
4-
const Joi = require('joi')
54

65
module.exports = (server) => {
76
const api = server.select('API')
@@ -17,14 +16,11 @@ module.exports = (server) => {
1716
api.route({
1817
method: '*',
1918
path: '/api/v0/bootstrap/add',
20-
handler: resources.bootstrap.add,
2119
config: {
22-
validate: {
23-
query: {
24-
arg: Joi.string().required(), // multiaddr to add
25-
default: Joi.boolean()
26-
}
27-
}
20+
pre: [
21+
{ method: resources.bootstrap.add.parseArgs, assign: 'args' }
22+
],
23+
handler: resources.bootstrap.add.handler
2824
}
2925
})
3026

@@ -39,14 +35,11 @@ module.exports = (server) => {
3935
api.route({
4036
method: '*',
4137
path: '/api/v0/bootstrap/rm',
42-
handler: resources.bootstrap.rm,
4338
config: {
44-
validate: {
45-
query: {
46-
arg: Joi.string().required(), // multiaddr to rm
47-
all: Joi.boolean()
48-
}
49-
}
39+
pre: [
40+
{ method: resources.bootstrap.rm.parseArgs, assign: 'args' }
41+
],
42+
handler: resources.bootstrap.rm.handler
5043
}
5144
})
5245
}

test/cli/test-bitswap.js

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,32 @@
22
'use strict'
33

44
const expect = require('chai').expect
5-
const nexpect = require('nexpect')
65
const Block = require('ipfs-block')
7-
const _ = require('lodash')
86
const bs58 = require('bs58')
97

108
const HttpAPI = require('../../src/http-api')
119
const createTempNode = require('../utils/temp-node')
1210
const repoPath = require('./index').repoPath
11+
const ipfs = require('../utils/ipfs')(repoPath)
1312

14-
describe('bitswap', function () {
15-
this.timeout(40000)
16-
const env = _.clone(process.env)
17-
env.IPFS_PATH = repoPath
18-
19-
let ipfs
13+
describe('bitswap', () => {
14+
let node
2015

2116
before((done) => {
22-
createTempNode(38, (err, _ipfs) => {
17+
createTempNode(38, (err, _node) => {
2318
expect(err).to.not.exist
24-
ipfs = _ipfs
25-
ipfs.goOnline(done)
19+
node = _node
20+
node.goOnline(done)
2621
})
2722
})
2823

24+
after((done) => {
25+
node.goOffline(done)
26+
})
27+
2928
describe('api running', () => {
3029
const block = new Block('hello')
3130
const key = bs58.encode(block.key)
32-
3331
let httpAPI
3432

3533
before((done) => {
@@ -41,42 +39,32 @@ describe('bitswap', function () {
4139
httpAPI.stop(done)
4240
})
4341

44-
it('wantlist', (done) => {
42+
it('wantlist', () => {
4543
const api = httpAPI.server.select('API')
4644

4745
api.inject({
4846
method: 'GET',
4947
url: `/api/v0/block/get?arg=${key}`
50-
}, (res) => {})
48+
}, () => {})
5149

52-
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'bitswap', 'wantlist'], {env})
53-
.run((err, stdout, exitcode) => {
54-
expect(err).to.not.exist
55-
expect(exitcode).to.equal(0)
56-
expect(stdout).to.be.eql([
57-
key
58-
])
59-
done()
60-
})
50+
return ipfs('bitswap wantlist').then((out) => {
51+
expect(out).to.be.eql(key)
52+
})
6153
})
6254

63-
it('stat', (done) => {
64-
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'bitswap', 'stat'], {env})
65-
.run((err, stdout, exitcode) => {
66-
expect(err).to.not.exist
67-
expect(exitcode).to.equal(0)
68-
expect(stdout).to.be.eql([
69-
'bitswap status',
70-
' blocks received: 0',
71-
' dup blocks received: 0',
72-
' dup data received: 0B',
73-
' wantlist [1 keys]',
74-
` ${key}`,
75-
' partners [0]',
76-
' '
77-
])
78-
done()
79-
})
55+
it('stat', () => {
56+
return ipfs('bitswap stat').then((out) => {
57+
expect(out).to.be.eql([
58+
'bitswap status',
59+
' blocks received: 0',
60+
' dup blocks received: 0',
61+
' dup data received: 0B',
62+
' wantlist [1 keys]',
63+
` ${key}`,
64+
' partners [0]',
65+
' '
66+
].join('\n'))
67+
})
8068
})
8169
})
8270
})

0 commit comments

Comments
 (0)