Skip to content
This repository was archived by the owner on Oct 1, 2021. It is now read-only.

Commit 7bb51b0

Browse files
committed
Migration 8 tests
License: MIT Signed-off-by: Adam Uhlir <[email protected]>
1 parent 78a6b72 commit 7bb51b0

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

migrations/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ const emptyMigration = {
1212
}
1313

1414
module.exports = [
15+
require('./migration-8')
1516
]

migrations/migration-8/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const Datastore = require('datastore-fs')
44
const path = require('path')
55
const base32 = require('base32.js')
66
const Key = require('interface-datastore').Key
7-
const log = require('debug')('jsipfs-repo-migrations:migration-8')
7+
const log = require('debug')('ipfs-repo-migrations:migration-8')
88

99
const KEY_PREFIX = 'key_'
1010

test/migrations/migration-8-test.js

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const chai = require('chai')
5+
chai.use(require('dirty-chai'))
6+
const chaiAsPromised = require("chai-as-promised")
7+
chai.use(chaiAsPromised)
8+
const expect = chai.expect
9+
10+
const path = require('path')
11+
const migration = require('../../migrations/migration-8')
12+
const Key = require('interface-datastore').Key
13+
const Datastore = require('datastore-fs')
14+
15+
const log = require('debug')('js-ipfs-repo-migrations:migration-8')
16+
17+
const fixtures = [
18+
['aAa', 'key_mfawc'],
19+
['bbb', 'key_mjrge'],
20+
['self', 'key_onswyzq']
21+
]
22+
23+
async function bootstrapKeys (dir, encoded) {
24+
const store = new Datastore(path.join(dir, 'keys'), { extension: '.data', createIfMissing: true })
25+
await store.open()
26+
27+
let name
28+
for (let keyNames of fixtures) {
29+
name = encoded ? keyNames[1] : keyNames[0]
30+
await store.put(new Key(`/pkcs8/${name}`), '')
31+
await store.put(new Key(`/info/${name}`), '')
32+
}
33+
34+
await store.close()
35+
}
36+
37+
async function validateKeys (dir, shouldBeEncoded) {
38+
const store = new Datastore(path.join(dir, 'keys'), { extension: '.data', createIfMissing: false })
39+
await store.open()
40+
41+
let name
42+
for (let keyNames of fixtures) {
43+
name = shouldBeEncoded ? keyNames[1] : keyNames[0]
44+
expect(await store.has(new Key(`/pkcs8/${name}`))).to.be.true()
45+
expect(await store.has(new Key(`/info/${name}`))).to.be.true()
46+
}
47+
48+
await store.close()
49+
}
50+
51+
module.exports = (setup, cleanup) => {
52+
let dir
53+
54+
beforeEach(async () => {
55+
dir = await setup()
56+
})
57+
afterEach(() => cleanup(dir))
58+
59+
it('should migrate forward', async () => {
60+
await bootstrapKeys(dir, false)
61+
await migration.migrate(dir)
62+
await validateKeys(dir, true)
63+
})
64+
65+
it('should migrate backward', async () => {
66+
await bootstrapKeys(dir, true)
67+
await migration.revert(dir)
68+
await validateKeys(dir, false)
69+
})
70+
71+
it('should fail to migrate backward with invalid key name', async () => {
72+
const store = new Datastore(path.join(dir, 'keys'), { extension: '.data', createIfMissing: true })
73+
await store.open()
74+
75+
await store.put(new Key('/pkcs8/mfawc'), '')
76+
await store.put(new Key('/info/mfawc'), '')
77+
78+
await store.close()
79+
80+
expect(migration.revert(dir)).to.eventually.rejectedWith('Unknown format of key\'s name!')
81+
})
82+
}

test/node.js

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ describe('Node specific tests', () => {
3535
require('./version-test')(repoSetup, repoCleanup)
3636
})
3737

38+
describe('migrations tests', () => {
39+
require('./migrations/migration-8-test')(repoSetup, repoCleanup)
40+
})
41+
3842
describe('init tests', () => {
3943
require('./init-test')(repoSetup, repoCleanup)
4044
})

0 commit comments

Comments
 (0)