|
4 | 4 | const expect = require('chai').expect
|
5 | 5 | const clean = require('../utils/clean')
|
6 | 6 | const ipfsCmd = require('../utils/ipfs-exec')
|
| 7 | +const pull = require('pull-stream') |
| 8 | +const toPull = require('stream-to-pull-stream') |
| 9 | +const os = require('os') |
| 10 | +const fs = require('fs') |
| 11 | +const path = require('path') |
| 12 | + |
| 13 | +const isWindows = os.platform() === 'win32' |
| 14 | + |
| 15 | +const checkLock = (repo, cb) => { |
| 16 | + // skip on windows |
| 17 | + // https://github.com/ipfs/js-ipfsd-ctl/pull/155#issuecomment-326983530 |
| 18 | + if (!isWindows) { |
| 19 | + if (fs.existsSync(path.join(repo, 'repo.lock'))) { |
| 20 | + cb(new Error('repo.lock not removed')) |
| 21 | + } |
| 22 | + if (fs.existsSync(path.join(repo, 'api'))) { |
| 23 | + cb(new Error('api file not removed')) |
| 24 | + } |
| 25 | + } |
| 26 | + cb() |
| 27 | +} |
7 | 28 |
|
8 | 29 | describe('daemon', () => {
|
9 | 30 | let repoPath
|
10 | 31 | let ipfs
|
11 | 32 |
|
| 33 | + const killSig = (sig) => { |
| 34 | + let proc = null |
| 35 | + return ipfs('init').then(() => { |
| 36 | + proc = ipfs('daemon') |
| 37 | + return new Promise((resolve, reject) => { |
| 38 | + pull( |
| 39 | + toPull(proc.stdout), |
| 40 | + pull.collect((err, res) => { |
| 41 | + expect(err).to.not.exist() |
| 42 | + const data = res.toString() |
| 43 | + if (data.includes(`Daemon is ready`)) { |
| 44 | + if (proc.kill(sig)) { |
| 45 | + resolve() |
| 46 | + } else { |
| 47 | + reject(new Error(`Unable to ${sig} process`)) |
| 48 | + } |
| 49 | + } |
| 50 | + }) |
| 51 | + ) |
| 52 | + |
| 53 | + pull( |
| 54 | + toPull(proc.stderr), |
| 55 | + pull.collect((err, res) => { |
| 56 | + expect(err).to.not.exist() |
| 57 | + const data = res.toString() |
| 58 | + if (data.length > 0) { |
| 59 | + reject(new Error(data)) |
| 60 | + } |
| 61 | + }) |
| 62 | + ) |
| 63 | + }) |
| 64 | + }) |
| 65 | + } |
| 66 | + |
12 | 67 | beforeEach(() => {
|
13 | 68 | repoPath = '/tmp/ipfs-test-not-found-' + Math.random().toString().substring(2, 8)
|
14 | 69 | ipfs = ipfsCmd(repoPath)
|
15 | 70 | })
|
16 | 71 |
|
17 | 72 | afterEach(() => clean(repoPath))
|
18 | 73 |
|
| 74 | + it(`don't crash if Addresses.Swarm is empty`, function (done) { |
| 75 | + this.timeout(20000) |
| 76 | + ipfs('init').then(() => { |
| 77 | + return ipfs('config', 'Addresses', JSON.stringify({ |
| 78 | + API: '/ip4/0.0.0.0/tcp/0', |
| 79 | + Gateway: '/ip4/0.0.0.0/tcp/0' |
| 80 | + }), '--json') |
| 81 | + }).then(() => { |
| 82 | + return ipfs('daemon') |
| 83 | + }).then((res) => { |
| 84 | + expect(res).to.have.string('Daemon is ready') |
| 85 | + done() |
| 86 | + }).catch((err) => { |
| 87 | + done(err) |
| 88 | + }) |
| 89 | + }) |
| 90 | + |
| 91 | + it(`should handle SIGINT gracefully`, function (done) { |
| 92 | + this.timeout(20000) |
| 93 | + killSig('SIGINT').then(() => { |
| 94 | + checkLock(repoPath, done) |
| 95 | + }).catch(done) |
| 96 | + }) |
| 97 | + |
| 98 | + it(`should handle SIGTERM gracefully`, function (done) { |
| 99 | + this.timeout(20000) |
| 100 | + killSig('SIGTERM').then(() => { |
| 101 | + checkLock(repoPath, done) |
| 102 | + }).catch(done) |
| 103 | + }) |
| 104 | + |
19 | 105 | it('gives error if user hasn\'t run init before', (done) => {
|
20 | 106 | const expectedError = 'no initialized ipfs repo found in ' + repoPath
|
21 | 107 |
|
|
0 commit comments