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

Commit 52cb801

Browse files
dryajovdaviddias
authored andcommitted
feat: rework http-api tests to use ipfsd-ctl (#1234)
* feat: rework http-api tests to use ipfsd-ctl * fix: copy repo * fix: revert fixture changes * fix: revert fixture changes * fix: remove unused todo
1 parent 4416e6d commit 52cb801

File tree

12 files changed

+343
-187
lines changed

12 files changed

+343
-187
lines changed

test/http-api/extra/block.js

+25-12
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,26 @@ chai.use(dirtyChai)
88

99
const multihash = require('multihashes')
1010
const waterfall = require('async/waterfall')
11-
const getCtl = require('./utils/get-ctl.js')
1211

13-
module.exports = (http) => {
14-
let ctl = null
15-
before(() => {
16-
ctl = getCtl(http)
12+
const DaemonFactory = require('ipfsd-ctl')
13+
const df = DaemonFactory.create({ exec: 'src/cli/bin.js' })
14+
15+
describe('extra block', () => {
16+
let ipfs = null
17+
let ipfsd = null
18+
before(function (done) {
19+
this.timeout(20 * 1000)
20+
21+
df.spawn({ initOptions: { bits: 512 } }, (err, _ipfsd) => {
22+
expect(err).to.not.exist()
23+
ipfsd = _ipfsd
24+
ipfs = ipfsd.api
25+
done()
26+
})
1727
})
28+
29+
after((done) => ipfsd.stop(done))
30+
1831
describe('.block', () => {
1932
describe('.put', () => {
2033
it('updates value', (done) => {
@@ -25,7 +38,7 @@ module.exports = (http) => {
2538
}
2639

2740
waterfall([
28-
(cb) => ctl.block.put(data, cb),
41+
(cb) => ipfs.block.put(data, cb),
2942
(block, cb) => {
3043
expect(block.cid.multihash).to.eql(
3144
multihash.fromB58String(expectedResult.key)
@@ -38,14 +51,14 @@ module.exports = (http) => {
3851

3952
describe('.get', () => {
4053
it('returns error for request with invalid argument', (done) => {
41-
ctl.block.get('invalid', (err, result) => {
54+
ipfs.block.get('invalid', (err, result) => {
4255
expect(err).to.exist()
4356
done()
4457
})
4558
})
4659

4760
it('returns value', (done) => {
48-
ctl.block.get('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp', (err, result) => {
61+
ipfs.block.get('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp', (err, result) => {
4962
expect(err).to.not.exist()
5063
expect(result.data.toString())
5164
.to.equal('hello world\n')
@@ -56,21 +69,21 @@ module.exports = (http) => {
5669

5770
describe('.stat', () => {
5871
it('returns error for request without argument', (done) => {
59-
ctl.block.stat(null, (err, result) => {
72+
ipfs.block.stat(null, (err, result) => {
6073
expect(err).to.exist()
6174
done()
6275
})
6376
})
6477

6578
it('returns error for request with invalid argument', (done) => {
66-
ctl.block.stat('invalid', (err, result) => {
79+
ipfs.block.stat('invalid', (err, result) => {
6780
expect(err).to.exist()
6881
done()
6982
})
7083
})
7184

7285
it('returns value', (done) => {
73-
ctl.block.stat('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp', (err, result) => {
86+
ipfs.block.stat('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp', (err, result) => {
7487
expect(err).to.not.exist()
7588
expect(result.key)
7689
.to.equal('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp')
@@ -80,4 +93,4 @@ module.exports = (http) => {
8093
})
8194
})
8295
})
83-
}
96+
})

test/http-api/extra/bootstrap.js

+32-18
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
1+
/* eslint max-nested-callbacks: ["error", 8] */
12
/* eslint-env mocha */
23
'use strict'
34

45
const chai = require('chai')
56
const dirtyChai = require('dirty-chai')
67
const expect = chai.expect
78
chai.use(dirtyChai)
8-
const getCtl = require('./utils/get-ctl.js')
99

10-
module.exports = (http) => {
11-
let ctl = null
12-
before(() => {
13-
ctl = getCtl(http)
10+
const DaemonFactory = require('ipfsd-ctl')
11+
const df = DaemonFactory.create({ exec: 'src/cli/bin.js' })
12+
13+
describe('extra bootstrap', () => {
14+
let ipfs = null
15+
let ipfsd = null
16+
before(function (done) {
17+
this.timeout(20 * 1000)
18+
19+
df.spawn({ initOptions: { bits: 512 } }, (err, _ipfsd) => {
20+
expect(err).to.not.exist()
21+
ipfsd = _ipfsd
22+
ipfs = ipfsd.api
23+
done()
24+
})
1425
})
26+
27+
after((done) => ipfsd.stop(done))
28+
1529
describe('.bootstrap', () => {
1630
const invalidArg = 'this/Is/So/Invalid/'
1731
const validIp4 = '/ip4/101.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z'
@@ -21,35 +35,35 @@ module.exports = (http) => {
2135
this.timeout(40 * 1000)
2236

2337
it('returns an error when called with an invalid arg', (done) => {
24-
ctl.bootstrap.add(invalidArg, (err) => {
38+
ipfs.bootstrap.add(invalidArg, (err) => {
2539
expect(err).to.be.an.instanceof(Error)
2640
done()
2741
})
2842
})
2943

3044
it('returns a list of containing the bootstrap peer when called with a valid arg (ip4)', (done) => {
31-
ctl.bootstrap.add(validIp4, (err, res) => {
45+
ipfs.bootstrap.add(validIp4, (err, res) => {
3246
expect(err).to.not.exist()
3347
expect(res).to.be.eql({ Peers: [validIp4] })
3448
done()
3549
})
3650
})
3751

3852
it('prevents duplicate inserts of bootstrap peers', () => {
39-
return ctl
53+
return ipfs
4054
.bootstrap
4155
.rm(null, { all: true })
4256
.then((res) => {
4357
expect(res.Peers.length).to.equal(0)
44-
return ctl.bootstrap.add(validIp4)
58+
return ipfs.bootstrap.add(validIp4)
4559
})
4660
.then(res => {
4761
expect(res).to.be.eql({ Peers: [validIp4] })
48-
return ctl.bootstrap.add(validIp4)
62+
return ipfs.bootstrap.add(validIp4)
4963
})
5064
.then((res) => {
5165
expect(res).to.be.eql({ Peers: [validIp4] })
52-
return ctl.bootstrap.list()
66+
return ipfs.bootstrap.list()
5367
})
5468
.then((res) => {
5569
expect(res).to.exist()
@@ -60,7 +74,7 @@ module.exports = (http) => {
6074
})
6175

6276
it('returns a list of bootstrap peers when called with the default option', (done) => {
63-
ctl.bootstrap.add({ default: true }, (err, res) => {
77+
ipfs.bootstrap.add({ default: true }, (err, res) => {
6478
expect(err).to.not.exist()
6579
peers = res.Peers
6680
expect(peers).to.exist()
@@ -72,7 +86,7 @@ module.exports = (http) => {
7286

7387
describe('.list', () => {
7488
it('returns a list of peers', (done) => {
75-
ctl.bootstrap.list((err, res) => {
89+
ipfs.bootstrap.list((err, res) => {
7690
expect(err).to.not.exist()
7791
peers = res.Peers
7892
expect(peers).to.exist()
@@ -83,14 +97,14 @@ module.exports = (http) => {
8397

8498
describe('.rm', () => {
8599
it('returns an error when called with an invalid arg', (done) => {
86-
ctl.bootstrap.rm(invalidArg, (err) => {
100+
ipfs.bootstrap.rm(invalidArg, (err) => {
87101
expect(err).to.be.an.instanceof(Error)
88102
done()
89103
})
90104
})
91105

92106
it('returns empty list because no peers removed when called without an arg or options', (done) => {
93-
ctl.bootstrap.rm(null, (err, res) => {
107+
ipfs.bootstrap.rm(null, (err, res) => {
94108
expect(err).to.not.exist()
95109
peers = res.Peers
96110
expect(peers).to.exist()
@@ -100,7 +114,7 @@ module.exports = (http) => {
100114
})
101115

102116
it('returns list containing the peer removed when called with a valid arg (ip4)', (done) => {
103-
ctl.bootstrap.rm(validIp4, (err, res) => {
117+
ipfs.bootstrap.rm(validIp4, (err, res) => {
104118
expect(err).to.not.exist()
105119

106120
peers = res.Peers
@@ -111,7 +125,7 @@ module.exports = (http) => {
111125
})
112126

113127
it('returns list of all peers removed when all option is passed', (done) => {
114-
ctl.bootstrap.rm(null, { all: true }, (err, res) => {
128+
ipfs.bootstrap.rm(null, { all: true }, (err, res) => {
115129
expect(err).to.not.exist()
116130
peers = res.Peers
117131
expect(peers).to.exist()
@@ -120,4 +134,4 @@ module.exports = (http) => {
120134
})
121135
})
122136
})
123-
}
137+
})

test/http-api/extra/config.js

+60-21
Original file line numberDiff line numberDiff line change
@@ -6,69 +6,108 @@ const dirtyChai = require('dirty-chai')
66
const expect = chai.expect
77
chai.use(dirtyChai)
88

9+
const ncp = require('ncp').ncp
10+
const rimraf = require('rimraf')
11+
const waterfall = require('async/waterfall')
12+
913
const fs = require('fs')
1014
const path = require('path')
11-
const getCtl = require('./utils/get-ctl.js')
1215

13-
module.exports = (http) => {
14-
let ctl = null
15-
before(() => {
16-
ctl = getCtl(http)
17-
})
18-
describe('.config', () => {
19-
const configPath = path.join(__dirname, '../../repo-tests-run/config')
16+
const DaemonFactory = require('ipfsd-ctl')
17+
const df = DaemonFactory.create({ exec: 'src/cli/bin.js' })
18+
19+
describe('extra config', () => {
20+
const repoExample = path.join(__dirname, '../../fixtures/go-ipfs-repo')
21+
const repoTests = path.join(__dirname, '../../repo-tests-run')
22+
23+
let updatedConfig = null
24+
25+
let ipfs = null
26+
let ipfsd = null
27+
before(function (done) {
28+
this.timeout(20 * 1000)
29+
30+
ncp(repoExample, repoTests, (err) => {
31+
expect(err).to.not.exist()
32+
33+
waterfall([
34+
(cb) => df.spawn({
35+
repoPath: repoTests,
36+
initOptions: { bits: 512 },
37+
disposable: false,
38+
start: true
39+
}, cb),
40+
(_ipfsd, cb) => {
41+
ipfsd = _ipfsd
42+
ipfsd.start(cb)
43+
}
44+
], (err) => {
45+
expect(err).to.not.exist()
46+
ipfs = ipfsd.api
47+
48+
updatedConfig = () => {
49+
const file = fs.readFileSync(path.join(__dirname, '../../repo-tests-run/config'))
50+
return JSON.parse(file, 'utf8')
51+
}
2052

21-
let updatedConfig
53+
done()
54+
})
55+
})
56+
})
2257

23-
before(() => {
24-
updatedConfig = () => JSON.parse(fs.readFileSync(configPath, 'utf8'))
58+
after((done) => {
59+
rimraf(repoTests, (err) => {
60+
expect(err).to.not.exist()
61+
ipfsd.stop(done)
2562
})
63+
})
2664

65+
describe('.config', () => {
2766
it('.get returns error for request with invalid argument', (done) => {
28-
ctl.config.get('kittens', (err, res) => {
67+
ipfs.config.get('kittens', (err, res) => {
2968
expect(err).to.exist()
3069
done()
3170
})
3271
})
3372

3473
it('.get returns value for request with argument', (done) => {
35-
ctl.config.get('API.HTTPHeaders', (err, value) => {
74+
ipfs.config.get('API.HTTPHeaders', (err, value) => {
3675
expect(err).not.to.exist()
3776
expect(value).to.equal(null)
3877
done()
3978
})
4079
})
4180

4281
it('.set updates value for request with both args', (done) => {
43-
ctl.config.set('Datastore.Path', 'kitten', (err) => {
82+
ipfs.config.set('Datastore.Path', 'kitten', (err) => {
4483
expect(err).not.to.exist()
4584
done()
4685
})
4786
})
4887

4988
it('.set returns error for request with both args and JSON flag with invalid JSON argument', (done) => {
50-
ctl.config.set('Datastore.Path', 'kitten', { json: true }, (err) => {
89+
ipfs.config.set('Datastore.Path', 'kitten', { json: true }, (err) => {
5190
expect(err).to.exist()
5291
done()
5392
})
5493
})
5594

5695
it('.set updates value for request with both args and bool flag and true argument', (done) => {
57-
ctl.config.set('Datastore.Path', true, (err) => {
96+
ipfs.config.set('Datastore.Path', true, (err) => {
5897
expect(err).not.to.exist()
5998
done()
6099
})
61100
})
62101

63102
it('.set updates value for request with both args and bool flag and false argument', (done) => {
64-
ctl.config.set('Datastore.Path', false, (err) => {
103+
ipfs.config.set('Datastore.Path', false, (err) => {
65104
expect(err).not.to.exist()
66105
done()
67106
})
68107
})
69108

70109
it('.get updatedConfig', (done) => {
71-
ctl.config.get((err, config) => {
110+
ipfs.config.get((err, config) => {
72111
expect(err).not.to.exist()
73112
expect(config).to.be.eql(updatedConfig())
74113
done()
@@ -81,7 +120,7 @@ module.exports = (http) => {
81120
it('returns error if the config is invalid', (done) => {
82121
const filePath = 'test/fixtures/test-data/badconfig'
83122

84-
ctl.config.replace(filePath, (err) => {
123+
ipfs.config.replace(filePath, (err) => {
85124
expect(err).to.exist()
86125
done()
87126
})
@@ -91,12 +130,12 @@ module.exports = (http) => {
91130
const filePath = 'test/fixtures/test-data/otherconfig'
92131
const expectedConfig = JSON.parse(fs.readFileSync(filePath, 'utf8'))
93132

94-
ctl.config.replace(filePath, (err) => {
133+
ipfs.config.replace(filePath, (err) => {
95134
expect(err).not.to.exist()
96135
expect(expectedConfig).to.deep.equal(updatedConfig())
97136
done()
98137
})
99138
})
100139
})
101140
})
102-
}
141+
})

0 commit comments

Comments
 (0)