|
| 1 | +/* eslint max-nested-callbacks: ["error", 8] */ |
| 2 | +/* eslint-env mocha */ |
| 3 | +'use strict' |
| 4 | + |
| 5 | +const chai = require('chai') |
| 6 | +const dirtyChai = require('dirty-chai') |
| 7 | +const expect = chai.expect |
| 8 | +chai.use(dirtyChai) |
| 9 | +const hat = require('hat') |
| 10 | +const sinon = require('sinon') |
| 11 | + |
| 12 | +const IPFS = require('../../src/core') |
| 13 | +const migrator = require('ipfs-repo-migrations') |
| 14 | +const repoVersion = require('ipfs-repo').repoVersion |
| 15 | + |
| 16 | +// This gets replaced by `create-repo-browser.js` in the browser |
| 17 | +const createTempRepo = require('../utils/create-repo-nodejs.js') |
| 18 | + |
| 19 | +async function initRepo (repo) { |
| 20 | + const ipfs = new IPFS({ |
| 21 | + repo: repo, |
| 22 | + init: false, |
| 23 | + start: false |
| 24 | + }) |
| 25 | + |
| 26 | + await ipfs.init({ bits: 512, pass: hat() }) |
| 27 | +} |
| 28 | + |
| 29 | +function setConfig (repo, key, value) { |
| 30 | + return new Promise(((resolve, reject) => { |
| 31 | + repo.config.set(key, value, (e) => { |
| 32 | + if (e) { |
| 33 | + return reject(e) |
| 34 | + } |
| 35 | + |
| 36 | + resolve() |
| 37 | + }) |
| 38 | + } |
| 39 | + )) |
| 40 | +} |
| 41 | + |
| 42 | +describe('migrations', () => { |
| 43 | + |
| 44 | + let repo |
| 45 | + let migrateStub |
| 46 | + let currentVersionStub |
| 47 | + let getLatestMigrationVersionStub |
| 48 | + |
| 49 | + before(() => { |
| 50 | + currentVersionStub = sinon.stub(migrator, 'getCurrentRepoVersion') |
| 51 | + migrateStub = sinon.stub(migrator, 'migrate') |
| 52 | + getLatestMigrationVersionStub = sinon.stub(migrator, 'getLatestMigrationVersion') |
| 53 | + }) |
| 54 | + |
| 55 | + after(() => { |
| 56 | + currentVersionStub.restore() |
| 57 | + migrateStub.restore() |
| 58 | + }) |
| 59 | + |
| 60 | + beforeEach(() => { |
| 61 | + sinon.reset() |
| 62 | + repo = createTempRepo() |
| 63 | + }) |
| 64 | + |
| 65 | + afterEach((done) => repo.teardown(done)) |
| 66 | + |
| 67 | + it('should migrate by default', (done) => { |
| 68 | + migrateStub.resolves() |
| 69 | + currentVersionStub.resolves(repoVersion - 1) |
| 70 | + getLatestMigrationVersionStub.returns(repoVersion) |
| 71 | + |
| 72 | + initRepo(repo) |
| 73 | + .then(() => { |
| 74 | + |
| 75 | + // Should not migrate when repo does not exists/is not initialized |
| 76 | + expect(migrateStub.called).to.be.false() |
| 77 | + |
| 78 | + const migratingIpfs = new IPFS({ |
| 79 | + repo: repo, |
| 80 | + init: false, |
| 81 | + start: false |
| 82 | + }) |
| 83 | + |
| 84 | + migratingIpfs.on('ready', () => { |
| 85 | + expect(migrateStub.called).to.be.true() |
| 86 | + done() |
| 87 | + }) |
| 88 | + }) |
| 89 | + }) |
| 90 | + |
| 91 | + it('should not migrate when repoDisableAutoMigration is true', (done) => { |
| 92 | + migrateStub.resolves() |
| 93 | + currentVersionStub.resolves(repoVersion - 1) |
| 94 | + getLatestMigrationVersionStub.returns(repoVersion) |
| 95 | + |
| 96 | + initRepo(repo) |
| 97 | + .then(() => setConfig(repo, 'repoDisableAutoMigration', true)) |
| 98 | + .then(() => { |
| 99 | + const migratingIpfs = new IPFS({ |
| 100 | + repo: repo, |
| 101 | + init: false, |
| 102 | + start: false |
| 103 | + }) |
| 104 | + |
| 105 | + migratingIpfs.on('ready', () => { |
| 106 | + expect(migrateStub.called).to.be.false() |
| 107 | + done() |
| 108 | + }) |
| 109 | + }) |
| 110 | + }) |
| 111 | + |
| 112 | + it('should not migrate when versions matches', (done) => { |
| 113 | + migrateStub.resolves() |
| 114 | + currentVersionStub.resolves(repoVersion) |
| 115 | + getLatestMigrationVersionStub.returns(repoVersion) |
| 116 | + |
| 117 | + initRepo(repo) |
| 118 | + .then(() => { |
| 119 | + const migratingIpfs = new IPFS({ |
| 120 | + repo: repo, |
| 121 | + init: false, |
| 122 | + start: false |
| 123 | + }) |
| 124 | + |
| 125 | + migratingIpfs.on('ready', () => { |
| 126 | + expect(migrateStub.called).to.be.false() |
| 127 | + done() |
| 128 | + }) |
| 129 | + }) |
| 130 | + }) |
| 131 | + |
| 132 | + it('should not migrate when current repo versions is higher then expected', (done) => { |
| 133 | + migrateStub.resolves() |
| 134 | + currentVersionStub.resolves(repoVersion + 1) |
| 135 | + getLatestMigrationVersionStub.returns(repoVersion) |
| 136 | + |
| 137 | + initRepo(repo) |
| 138 | + .then(() => { |
| 139 | + const migratingIpfs = new IPFS({ |
| 140 | + repo: repo, |
| 141 | + init: false, |
| 142 | + start: false |
| 143 | + }) |
| 144 | + |
| 145 | + migratingIpfs.on('ready', () => { |
| 146 | + expect(migrateStub.called).to.be.false() |
| 147 | + done() |
| 148 | + }) |
| 149 | + }) |
| 150 | + }) |
| 151 | + |
| 152 | + it('should fail if ipfs-repo-migrations does not contain expected migration', (done) => { |
| 153 | + getLatestMigrationVersionStub.returns(repoVersion - 1) |
| 154 | + migrateStub.resolves() |
| 155 | + currentVersionStub.resolves(repoVersion - 1) |
| 156 | + |
| 157 | + initRepo(repo) |
| 158 | + .then(() => { |
| 159 | + const migratingIpfs = new IPFS({ |
| 160 | + repo: repo, |
| 161 | + init: false, |
| 162 | + start: false |
| 163 | + }) |
| 164 | + |
| 165 | + migratingIpfs.on('error', (err) => { |
| 166 | + expect(err).to.exist() |
| 167 | + expect(err.message).to.match(/The ipfs-repo-migrations package does not have migration for version: /) |
| 168 | + done() |
| 169 | + }) |
| 170 | + }) |
| 171 | + }) |
| 172 | +}) |
0 commit comments