-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathrepo-test.js
375 lines (308 loc) · 9.51 KB
/
repo-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* eslint-env mocha */
'use strict'
const { expect } = require('./utils/chai')
const tempDir = require('ipfs-utils/src/temp-dir')
const IPFSRepo = require('../')
const Errors = require('../src/errors')
const bytes = require('bytes')
module.exports = (repo) => {
describe('IPFS Repo Tests', () => {
it('check if Repo exists', async () => {
const exists = await repo.exists()
expect(exists).to.equal(true)
})
it('exposes the path', () => {
expect(repo.path).to.be.a('string')
})
describe('config', () => {
it('get config', async () => {
const config = await repo.config.get()
expect(config).to.be.a('object')
})
it('set config', async () => {
await repo.config.set({ a: 'b' })
const config = await repo.config.get()
expect(config).to.deep.equal({ a: 'b' })
})
it('get config key', async () => {
const value = await repo.config.get('a')
expect(value).to.equal('b')
})
it('set config key', async () => {
await repo.config.set('c.x', 'd')
const config = await repo.config.get()
expect(config).to.deep.equal({ a: 'b', c: { x: 'd' } })
})
})
describe('spec', () => {
it('get spec', async () => {
await repo.spec.get()
})
it('set spec', async () => {
await repo.spec.set({ a: 'b' })
const spec = await repo.spec.get()
expect(spec).to.deep.equal({ a: 'b' })
})
})
describe('version', () => {
afterEach(async () => {
await repo.version.set(8)
})
it('get version', async () => {
const version = await repo.version.get()
expect(version).to.equal(8)
})
it('set version', async () => {
const v1 = 9000
await repo.version.set(v1)
expect(await repo.version.get()).to.equal(v1)
})
it('returns true when requested version is the same as the actual version', async () => {
await repo.version.set(5)
expect(await repo.version.check(5)).to.be.true()
})
it('returns false when requesting a past version', async () => {
await repo.version.set(5)
expect(await repo.version.check(4)).to.be.false()
})
it('returns false when requesting a future version', async () => {
await repo.version.set(1)
expect(await repo.version.check(2)).to.be.false()
})
it('treats v6 and v7 as the same', async () => {
await repo.version.set(7)
expect(await repo.version.check(6)).to.be.true()
})
it('treats v7 and v6 as the same', async () => {
await repo.version.set(6)
expect(await repo.version.check(7)).to.be.true()
})
})
describe('lifecycle', () => {
it('close and open', async () => {
await repo.close()
await repo.open()
await repo.close()
await repo.open()
const version = await repo.version.get()
expect(version).to.exist()
})
it('close twice throws error', async () => {
await repo.close()
try {
await repo.close()
} catch (err) {
expect(err.code).to.eql(Errors.ERR_REPO_ALREADY_CLOSED)
return
}
expect.fail('Did not throw')
})
it('should close all the datastores', async () => {
let count = 0
class FakeDatastore {
constructor () {
this.data = {}
}
async open () {}
// eslint-disable-next-line require-await
async put (key, val) {
this.data[key.toString()] = val
}
async get (key) {
const exists = await this.has(key)
if (!exists) throw Errors.notFoundError()
return this.data[key.toString()]
}
// eslint-disable-next-line require-await
async has (key) {
return this.data[key.toString()] !== undefined
}
// eslint-disable-next-line require-await
async delete (key) {
delete this.data[key.toString()]
}
batch () {}
query (q) {}
// eslint-disable-next-line require-await
async close () {
count++
}
}
const repo = new IPFSRepo(tempDir(), {
lock: 'memory',
storageBackends: {
root: FakeDatastore,
blocks: FakeDatastore,
keys: FakeDatastore,
datastore: FakeDatastore,
pins: FakeDatastore
}
})
await repo.init({})
await repo.open()
await repo.close()
expect(count).to.be.eq(5)
})
it('open twice throws error', async () => {
await repo.open()
try {
await repo.open()
} catch (err) {
expect(err.code).to.eql(Errors.ERR_REPO_ALREADY_OPEN)
return
}
expect.fail('Did not throw')
})
it('should throw non-already-open errors when opening the root', async () => {
const otherRepo = new IPFSRepo(tempDir())
const err = new Error('wat')
otherRepo.root.open = () => {
throw err
}
try {
await otherRepo.init({})
} catch (err2) {
expect(err2).to.deep.equal(err)
}
})
it('should ignore non-already-open errors when opening the root', async () => {
const otherRepo = new IPFSRepo(tempDir())
const err = new Error('Already open')
let threwError = false
otherRepo.root.open = () => {
threwError = true
throw err
}
await otherRepo._openRoot()
expect(threwError).to.be.true()
})
})
describe('locking', () => {
class ExplodingDatastore {
constructor () {
throw new Error('wat')
}
}
let otherRepo
afterEach(async () => {
try {
await otherRepo.close()
} catch (_) {
// ignore error
}
})
it('should remove the lockfile when opening the repo fails', async () => {
otherRepo = new IPFSRepo(tempDir(), {
storageBackends: {
datastore: ExplodingDatastore
}
})
try {
await otherRepo.init({})
await otherRepo.open()
} catch (err) {
expect(otherRepo.lockfile).to.be.null()
}
})
it('should re-throw the original error even when removing the lockfile fails', async () => {
otherRepo = new IPFSRepo(tempDir(), {
storageBackends: {
datastore: ExplodingDatastore
}
})
otherRepo._closeLock = () => {
throw new Error('derp')
}
try {
await otherRepo.init({})
await otherRepo.open()
} catch (err) {
expect(err.message).to.equal('wat')
}
})
it('should throw when repos are not initialised', async () => {
otherRepo = new IPFSRepo(tempDir(), {
storageBackends: {
datastore: ExplodingDatastore
}
})
try {
await otherRepo.open()
} catch (err) {
expect(err.code).to.equal(Errors.ERR_REPO_NOT_INITIALIZED)
}
})
it('should throw when config is not set', async () => {
otherRepo = new IPFSRepo(tempDir())
otherRepo.config.exists = () => {
return false
}
otherRepo.spec.exists = () => {
return true
}
otherRepo.version.check = () => {
return null
}
try {
await otherRepo.open()
} catch (err) {
expect(err.code).to.equal(Errors.ERR_REPO_NOT_INITIALIZED)
}
})
it('should return the max storage stat when set', async () => {
const maxStorage = '1GB'
otherRepo = new IPFSRepo(tempDir())
await otherRepo.init({})
await otherRepo.open()
await otherRepo.config.set('Datastore.StorageMax', maxStorage)
const stat = await otherRepo.stat({})
expect(stat).to.have.property('storageMax')
expect(stat.storageMax.toNumber()).to.equal(bytes(maxStorage))
})
it('should throw unexpected errors when closing', async () => {
otherRepo = new IPFSRepo(tempDir())
await otherRepo.init({})
await otherRepo.open()
const err = new Error('wat')
otherRepo.apiAddr.delete = () => {
throw err
}
try {
await otherRepo.close()
throw new Error('Should have thrown')
} catch (err2) {
expect(err2).to.equal(err)
}
})
it('should swallow expected errors when closing', async () => {
otherRepo = new IPFSRepo(tempDir())
await otherRepo.init({})
await otherRepo.open()
const err = new Error('ENOENT')
otherRepo.apiAddr.delete = () => {
throw err
}
await otherRepo.close()
})
it('should throw unexpected errors when checking if the repo has been initialised', async () => {
otherRepo = new IPFSRepo(tempDir())
otherRepo.config.exists = () => {
return true
}
otherRepo.version.check = () => {
return true
}
const err = new Error('ENOENT')
otherRepo.spec.exists = () => {
throw err
}
try {
await otherRepo.open()
throw new Error('Should have thrown')
} catch (err2) {
expect(err2).to.equal(err)
}
})
})
})
}