Repository navigation
Expand file tree
/
Copy pathtest.js
More file actions
221 lines (201 loc) · 6.25 KB
/
Copy pathtest.js
File metadata and controls
221 lines (201 loc) · 6.25 KB
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
// Copyright IBM Corp. 2017,2019. All Rights Reserved.
// Node module: loopback-connector-couchdb2
// This file is licensed under the Apache License 2.0.
// License text available at https://opensource.org/licenses/Apache-2.0
'use strict';
const _ = require('lodash');
const async = require('async');
const spawn = require('child_process').spawn;
const docker = new require('dockerode')();
const fmt = require('util').format;
const http = require('http');
const ms = require('ms');
// we don't pass any node flags, so we can call _mocha instead the wrapper
const mochaBin = require.resolve('mocha/bin/_mocha');
process.env.COUCHDB_DATABASE = 'test-db';
process.env.COUCHDB_PASSWORD = 'pass';
process.env.COUCHDB_USERNAME = 'admin';
// these are placeholders. They get set dynamically based on what IP and port
// get assigned by docker.
process.env.COUCHDB_PORT = 'TBD';
process.env.COUCHDB_HOST = 'TBD';
process.env.COUCHDB_URL = 'TBD';
const CONNECT_RETRIES = 30;
const CONNECT_DELAY = ms('5s');
let containerToDelete = null;
async.waterfall([
dockerStart('klaemo/couchdb:2.0.0'),
sleep(ms('2s')),
setCouchDBEnv,
waitFor('/_all_dbs'),
createDB('test-db'),
run([mochaBin, '--timeout', '40000', '--require', 'strong-mocha-interfaces', '--require', 'test/init.js', '--ui', 'strong-bdd']),
], function(testErr) {
dockerCleanup(function(cleanupErr) {
if (cleanupErr) {
console.error('error cleaning up:', cleanupErr);
}
if (testErr) {
console.error('error running tests:', testErr);
process.exit(1);
}
});
});
function sleep(n) {
return function delayedPassThrough() {
const args = [].slice.call(arguments);
// last argument is the callback
const next = args.pop();
// prepend `null` to indicate no error
args.unshift(null);
setTimeout(function() {
next.apply(null, args);
}, n);
};
}
function dockerStart(imgName) {
return function pullAndStart(next) {
console.log('pulling image: %s', imgName);
docker.pull(imgName, function(err, stream) {
if (err) return next(err);
docker.modem.followProgress(stream, function(err, output) {
if (err) {
return next(err);
}
console.log('starting container from image: %s', imgName);
docker.createContainer({
Image: imgName,
HostConfig: {
PublishAllPorts: true,
},
Env: [
'COUCHDB_USER=' + process.env.COUCHDB_USERNAME,
'COUCHDB_PASSWORD=' + process.env.COUCHDB_PASSWORD,
],
}, function(err, container) {
console.log('recording container for later cleanup: ', container.id);
containerToDelete = container;
if (err) {
return next(err);
}
container.start(function(err, data) {
next(err, container);
});
});
});
});
};
}
function setCouchDBEnv(container, next) {
container.inspect(function(err, c) {
// if swarm, Node.Ip will be set to actual node's IP
// if not swarm, but remote docker, use docker host's IP
// if local docker, use localhost
const host = _.get(c, 'Node.IP', _.get(docker, 'modem.host', '127.0.0.1'));
// container's port 80 is dynamically mapped to an external port
const port = _.get(c,
['NetworkSettings', 'Ports', '5984/tcp', '0', 'HostPort']);
process.env.COUCHDB_PORT = port;
process.env.COUCHDB_HOST = host;
const usr = process.env.COUCHDB_USERNAME;
const pass = process.env.COUCHDB_PASSWORD;
process.env.COUCHDB_URL = 'http://' + usr + ':' + pass + '@' +
host + ':' + port;
console.log('env:', _.pick(process.env, [
'COUCHDB_URL',
'COUCHDB_HOST',
'COUCHDB_PORT',
'COUCHDB_USERNAME',
'COUCHDB_PASSWORD',
'COUCHDB_DATABASE',
]));
next(null, container);
});
}
function waitFor(path) {
return function waitForPath(container, next) {
const opts = {
host: process.env.COUCHDB_HOST,
port: process.env.COUCHDB_PORT,
auth: process.env.COUCHDB_USERNAME + ':' + process.env.COUCHDB_PASSWORD,
path: path,
};
console.log('waiting for instance to respond');
return ping(null, CONNECT_RETRIES);
function ping(err, tries) {
console.log('ping (%d/%d)', CONNECT_RETRIES - tries, CONNECT_RETRIES);
if (tries < 1) {
next(err || new Error('failed to contact CouchDB'));
}
http.get(opts, function(res) {
res.pipe(devNull());
res.on('error', tryAgain);
res.on('end', function() {
if (res.statusCode === 200) {
setImmediate(next, null, container);
} else {
tryAgain();
}
});
}).on('error', tryAgain);
function tryAgain(err) {
setTimeout(ping, CONNECT_DELAY, err, tries - 1);
}
}
};
}
function createDB(db) {
return function create(container, next) {
const opts = {
method: 'PUT',
path: '/' + db,
host: process.env.COUCHDB_HOST,
port: process.env.COUCHDB_PORT,
auth: process.env.COUCHDB_USERNAME + ':' + process.env.COUCHDB_PASSWORD,
};
console.log('creating db: %j', db);
http.request(opts, function(res) {
res.pipe(devNull());
res.on('error', next);
res.on('end', function() {
setImmediate(next, null, container);
});
})
.on('error', next)
.end();
};
}
function run(cmd) {
return function spawnNode(container, next) {
spawn(process.execPath, cmd, {stdio: 'inherit'})
.on('error', next)
.on('exit', onExit);
function onExit(code, sig) {
if (code) {
next(new Error(fmt('mocha exited with code: %j, sig: %j', code, sig)));
} else {
next();
}
}
};
}
// clean up any previous containers
function dockerCleanup(next) {
if (containerToDelete) {
console.log('cleaning up container: %s', containerToDelete.id);
containerToDelete.remove({force: true}, function(err) {
next(err);
});
} else {
setImmediate(next);
}
}
// A Writable Stream that just consumes a stream. Useful for draining readable
// streams so that they 'end' properly, like sometimes-empty http responses.
function devNull() {
return new require('stream').Writable({
write: function(_chunk, _encoding, cb) {
return cb(null);
},
});
}