This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathdaemon.js
137 lines (117 loc) · 3.6 KB
/
daemon.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
/* eslint-env mocha */
'use strict'
const expect = require('chai').expect
const clean = require('../utils/clean')
const ipfsCmd = require('../utils/ipfs-exec')
const isWindows = require('../utils/platforms').isWindows
const os = require('os')
const path = require('path')
const hat = require('hat')
const fs = require('fs')
const skipOnWindows = isWindows() ? it.skip : it
const checkLock = (repo, cb) => {
// skip on windows
// https://github.com/ipfs/js-ipfsd-ctl/pull/155#issuecomment-326983530
if (!isWindows) {
if (fs.existsSync(path.join(repo, 'repo.lock'))) {
cb(new Error('repo.lock not removed'))
}
if (fs.existsSync(path.join(repo, 'api'))) {
cb(new Error('api file not removed'))
}
}
cb()
}
function testSignal (ipfs, sig) {
return ipfs('init').then(() => {
return ipfs('config', 'Addresses', JSON.stringify({
API: '/ip4/127.0.0.1/tcp/0',
Gateway: '/ip4/127.0.0.1/tcp/0'
}), '--json')
}).then(() => {
const proc = ipfs('daemon')
return new Promise((resolve, reject) => {
proc.stdout.on('data', (data) => {
if (data.toString().includes(`Daemon is ready`)) {
if (proc.kill(sig)) {
resolve()
} else {
reject(new Error(`Unable to ${sig} process`))
}
}
})
proc.stderr.on('data', (data) => {
if (data.toString().length > 0) {
reject(new Error(data))
}
})
})
})
}
describe('daemon', () => {
let repoPath
let ipfs
beforeEach(() => {
repoPath = path.join(os.tmpdir(), 'ipfs-test-not-found-' + hat())
ipfs = ipfsCmd(repoPath)
})
afterEach(() => clean(repoPath))
skipOnWindows('do not crash if Addresses.Swarm is empty', function (done) {
this.timeout(100 * 1000)
// These tests are flaky, but retrying 3 times seems to make it work 99% of the time
this.retries(3)
ipfs('init').then(() => {
return ipfs('config', 'Addresses', JSON.stringify({
Swarm: [],
API: '/ip4/127.0.0.1/tcp/0',
Gateway: '/ip4/127.0.0.1/tcp/0'
}), '--json')
}).then(() => {
const res = ipfs('daemon')
const timeout = setTimeout(() => {
done(new Error('Daemon did not get ready in time'))
}, 1000 * 120)
res.stdout.on('data', (data) => {
const line = data.toString()
if (line.includes('Daemon is ready')) {
clearTimeout(timeout)
res.kill()
done()
}
})
}).catch(err => done(err))
})
skipOnWindows('should handle SIGINT gracefully', function (done) {
this.timeout(100 * 1000)
testSignal(ipfs, 'SIGINT').then(() => {
checkLock(repoPath, done)
}).catch(done)
})
skipOnWindows('should handle SIGTERM gracefully', function (done) {
this.timeout(100 * 1000)
testSignal(ipfs, 'SIGTERM').then(() => {
checkLock(repoPath, done)
}).catch(done)
})
skipOnWindows('should handle SIGHUP gracefully', function (done) {
this.timeout(100 * 1000)
testSignal(ipfs, 'SIGHUP').then(() => {
checkLock(repoPath, done)
}).catch(done)
})
it('gives error if user hasn\'t run init before', function (done) {
this.timeout(100 * 1000)
const expectedError = 'no initialized ipfs repo found in ' + repoPath
ipfs('daemon').catch((err) => {
expect(err.stdout).to.have.string(expectedError)
done()
})
})
it('should present ipfs path help when option help is received', function (done) {
this.timeout(100 * 1000)
ipfs('daemon --help').then((res) => {
expect(res).to.have.string('export IPFS_PATH=/path/to/ipfsrepo')
done()
})
})
})