Repository: couchdb-nmo Updated Branches: refs/heads/master 3c917867c -> 909b275a7
New online check function Check if the cluster or url is online so that the command works correctly and that we can warn the user correctly Project: http://git-wip-us.apache.org/repos/asf/couchdb-nmo/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb-nmo/commit/909b275a Tree: http://git-wip-us.apache.org/repos/asf/couchdb-nmo/tree/909b275a Diff: http://git-wip-us.apache.org/repos/asf/couchdb-nmo/diff/909b275a Branch: refs/heads/master Commit: 909b275a7e6f7eae5ce35399fdfa34bf64cb4dde Parents: 3c91786 Author: Garren Smith <[email protected]> Authored: Tue Jan 5 16:42:14 2016 +0200 Committer: Garren Smith <[email protected]> Committed: Tue Jan 12 09:48:14 2016 +0200 ---------------------------------------------------------------------- README.md | 1 + doc/api/nmo-import-csv.md | 5 ++- doc/cli/nmo-import-csv.md | 11 +++++-- src/activetasks.js | 47 ++++++++++++++------------- src/import-csv.js | 72 ++++++++++++++++++++++-------------------- src/isonline.js | 28 +--------------- src/query.js | 29 +++++++++++------ src/replicate-from.js | 2 +- src/replicate-to.js | 2 +- src/replicate.js | 9 ++++-- src/savetofile.js | 28 +++++++++------- src/utils.js | 46 +++++++++++++++++++++++++++ test/activetasks.js | 39 ++++++++++++----------- test/helpers.js | 8 +++++ test/import-csv.js | 32 +++++++++++++------ test/query.js | 18 ++++++++--- test/replicate-from.js | 5 ++- test/replicate-to.js | 10 ++++-- test/replicate.js | 15 ++++++--- test/savetofile.js | 12 +++---- test/utils.js | 25 +++++++++++++++ 21 files changed, 281 insertions(+), 163 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/909b275a/README.md ---------------------------------------------------------------------- diff --git a/README.md b/README.md index 7f0f673..8a61735 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ ## Installation ``` + npm install -g nmo ``` http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/909b275a/doc/api/nmo-import-csv.md ---------------------------------------------------------------------- diff --git a/doc/api/nmo-import-csv.md b/doc/api/nmo-import-csv.md index 8961bb9..4b748cc 100644 --- a/doc/api/nmo-import-csv.md +++ b/doc/api/nmo-import-csv.md @@ -3,7 +3,7 @@ nmo-import-csv(3) -- import csv ## SYNOPSIS - nmo.commands['import-csv'](file, url, [csvOptions]) + nmo.commands['import-csv'](<cluster || url>, <database>, <file>, [csvOptions]) @@ -13,5 +13,4 @@ Import csv file into CouchDB - import-csv: -Accepts the file, url and csv options to import a file into CouchDB. -It returns a promise. +Accepts the name of the cluster or a url to the cluster. Along with the name of the database and the url to the file. CSV options can be supplied for how to import the file. http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/909b275a/doc/cli/nmo-import-csv.md ---------------------------------------------------------------------- diff --git a/doc/cli/nmo-import-csv.md b/doc/cli/nmo-import-csv.md index fa8fd82..210244f 100644 --- a/doc/cli/nmo-import-csv.md +++ b/doc/cli/nmo-import-csv.md @@ -3,7 +3,8 @@ nmo-import-csv(1) -- Bulk import CSV files ## SYNOPSIS - nmo import-csv <file> <couchdb-url> [--delimiter=','] [--columns=true] + nmo import-csv <cluster> <database> <file> [--delimiter=','] [--columns=true] + nmo import-csv <url> <database> <file> [--delimiter=','] [--columns=true] ## DESCRIPTION @@ -25,6 +26,10 @@ Imports a csv file into CouchDB. each document -## EXAMPLE +EXAMPLE: - nmo import-csv /path/to/csv http://couch-url --columns=true +This will import the file `mycsv.csv` into the `mydb` database on the cluster `mycluster`. With the columns parameter set to true. + nmo import-csv mycluster mydb mycsv.csv --columns=true + +This will import the file `mycsv.csv` into the `mydb` database at the couchdb url `http://mycouchdb.com`. + nmo import-csv http://mycouchdb.com mydb mycsv.csv --columns=true http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/909b275a/src/activetasks.js ---------------------------------------------------------------------- diff --git a/src/activetasks.js b/src/activetasks.js index 56caa04..ce0c85e 100644 --- a/src/activetasks.js +++ b/src/activetasks.js @@ -3,7 +3,7 @@ import Wreck from 'wreck'; import Promise from 'bluebird'; import log from 'npmlog'; import prettyjson from 'prettyjson'; -import {getUrlFromCluster, validUrl, removeUsernamePw } from './utils'; +import {getUrlFromCluster, validUrl, removeUsernamePw, checkNodeOnline } from './utils'; export function cli (cluster, filter) { return new Promise((resolve, reject) => { @@ -71,8 +71,7 @@ export function filterTasks (tasks, searchTerm) { export default function getActiveTask (cluster, filter) { return new Promise((resolve, reject) => { - // `cluster` argument can be a cluster or a url. - // This function checks that and returns an appropriate url + const url = getUrlFromCluster(cluster); let er = validUrl(url); @@ -82,26 +81,30 @@ export default function getActiveTask (cluster, filter) { return reject(er); } - const activetasksUrl = url + '/_active_tasks'; - const cleanedUrl = removeUsernamePw(url); - log.http('request', 'GET', cleanedUrl); - - Wreck.get(activetasksUrl, (err, res, payload) => { - if (err) { - if (err.code === 'ECONNREFUSED' || err.code === 'ENOTFOUND') { - const noNodeErr = new Error('Could not connect to ' + activetasksUrl + - ' this could mean the node is down.'); - noNodeErr.type = 'EUSAGE'; - return reject(noNodeErr); - } + checkNodeOnline(url) + .then(() => { + const activetasksUrl = url + '/_active_tasks'; + const cleanedUrl = removeUsernamePw(url); + log.http('request', 'GET', cleanedUrl); + + Wreck.get(activetasksUrl, (err, res, payload) => { + if (err) { + if (err.code === 'ECONNREFUSED' || err.code === 'ENOTFOUND') { + const noNodeErr = new Error('Could not connect to ' + activetasksUrl + + ' this could mean the node is down.'); + noNodeErr.type = 'EUSAGE'; + return reject(noNodeErr); + } - err.type = 'EUSAGE'; - return reject(err); - } + err.type = 'EUSAGE'; + return reject(err); + } - log.http(res.statusCode, cleanedUrl); - const tasks = filterTasks(JSON.parse(payload), filter); - resolve(tasks); - }); + log.http(res.statusCode, cleanedUrl); + const tasks = filterTasks(JSON.parse(payload), filter); + resolve(tasks); + }); + }) + .catch(err => reject(err)); }); } http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/909b275a/src/import-csv.js ---------------------------------------------------------------------- diff --git a/src/import-csv.js b/src/import-csv.js index cad9039..88ae9ab 100644 --- a/src/import-csv.js +++ b/src/import-csv.js @@ -3,15 +3,15 @@ import Promise from 'bluebird'; import CouchBulkImporter from 'couchbulkimporter'; import parse from 'csv-parse'; import BulkBadger from 'bulkbadger'; -import {checkUrl} from './utils'; +import {getUrlFromCluster, checkNodeOnline} from './utils'; import nmo from './nmo.js'; -export function cli (file, url, ...csvOptions) { - if (!file) { +export function cli (cluster, database, file, ...csvOptions) { + if (!file || !cluster || !database) { const msg = [ 'Usage:', '', - 'nmo import-csv [file] [couchdb-url] [...<csv options> <pairs>]', + 'nmo import-csv <cluster> <database> <file> [...<csv options> <pairs>]', ].join('\n'); const err = new Error(msg); err.type = 'EUSAGE'; @@ -19,44 +19,46 @@ export function cli (file, url, ...csvOptions) { throw err; } - const er = checkUrl(url); - if (er) { - throw er; - } const opts = {delimiter: nmo.config.get('delimiter'), columns: nmo.config.get('columns')}; - return importcsv.apply(importcsv, [file, url, opts]); + return importcsv(cluster, database, file, opts); } export default importcsv; -function importcsv (file, url, {delimiter= ',', columns= true}) { +function importcsv (cluster, database, file, {delimiter= ',', columns= true}) { return new Promise((resolve, reject) => { - const input = fs.createReadStream(file) - .on('error', (err) => { - err.message = 'Error reading file - ' + err.message; - reject(err); - }); - - const parser = parse({ - delimiter: delimiter, - columns: columns - }); - - input - .pipe(parser) - .pipe(new BulkBadger()) - .pipe(new CouchBulkImporter({ - url: url - })) - .on('error', function (err) { - err.message = 'Error uploading - ' + err.message; - reject(err); - }) - .on('finish', function () { - console.log('Upload complete!'); - resolve(); - }); + const baseUrl = getUrlFromCluster(cluster); + const url = baseUrl + '/' + database; + + checkNodeOnline(baseUrl) + .then(() => { + const input = fs.createReadStream(file) + .on('error', (err) => { + err.message = 'Error reading file - ' + err.message; + reject(err); + }); + const parser = parse({ + delimiter: delimiter, + columns: columns + }); + + input + .pipe(parser) + .pipe(new BulkBadger()) + .pipe(new CouchBulkImporter({ + url: url + })) + .on('error', function (err) { + err.message = 'Error uploading - ' + err.message; + reject(err); + }) + .on('finish', function () { + console.log('Upload complete!'); + resolve(); + }); + }) + .catch(err => reject(err)); }); } http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/909b275a/src/isonline.js ---------------------------------------------------------------------- diff --git a/src/isonline.js b/src/isonline.js index baf3ae3..6ea3b0a 100644 --- a/src/isonline.js +++ b/src/isonline.js @@ -3,7 +3,7 @@ import log from 'npmlog'; import Wreck from 'wreck'; import Promise from 'bluebird'; import nmo from './nmo.js'; -import { getClusterUrls } from './utils'; +import { getClusterUrls, isNodeOnline } from './utils'; export const cli = isOnlineCli; @@ -61,29 +61,3 @@ function isonline (...args) { }, reject); }); } - -function isNodeOnline (url) { - return new Promise((resolve, reject) => { - const er = utils.validUrl(url); - - if (er) { - er.type = 'EUSAGE'; - return reject(er); - } - const cleanedUrl = utils.removeUsernamePw(url); - log.http('request', 'GET', cleanedUrl); - - Wreck.get(url, (err, res, payload) => { - if (err && (err.code === 'ECONNREFUSED' || err.code === 'ENOTFOUND')) { - return resolve({[url]: false}); - } - - if (err) { - return reject(err); - } - - log.http(res.statusCode, cleanedUrl); - resolve({[url]: res.statusCode < 300}); - }); - }); -} http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/909b275a/src/query.js ---------------------------------------------------------------------- diff --git a/src/query.js b/src/query.js index 02adbae..bf11e57 100644 --- a/src/query.js +++ b/src/query.js @@ -1,7 +1,7 @@ import prettyjson from 'prettyjson'; import nmo from './nmo.js'; import Promise from 'bluebird'; -import { getUrlFromCluster, sendJsonToNode } from './utils'; +import { checkNodeOnline, getUrlFromCluster, sendJsonToNode } from './utils'; export function cli (cluster, dbname, ...args) { return new Promise((resolve, reject) => { @@ -84,10 +84,15 @@ export function cli (cluster, dbname, ...args) { export function run (cluster, dbname, selector) { return new Promise((resolve, reject) => { - const url = getUrlFromCluster(cluster) + '/' + dbname + '/_find'; - sendJsonToNode(url, selector) - .then(res => resolve(res)) - .catch(err => reject(err)); + const baseUrl = getUrlFromCluster(cluster); + const url = baseUrl + '/' + dbname + '/_find'; + + checkNodeOnline(baseUrl) + .then(() => { + return sendJsonToNode(url, selector) + }) + .then(res => resolve(res)) + .catch(err => reject(err)); }); } @@ -100,9 +105,15 @@ export function createIndex(cluster, dbname, fields) { type: 'json' }; - const url = getUrlFromCluster(cluster) + '/' + dbname + '/_index'; - sendJsonToNode(url, index) - .then(res => resolve(res)) - .catch(err => reject(err)); + const baseUrl = getUrlFromCluster(cluster); + const url = baseUrl + '/' + dbname + '/_index'; + + checkNodeOnline(baseUrl) + .then(() => { + return sendJsonToNode(url, index) + }) + .then(res => resolve(res)) + .catch(err => reject(err)); + }); } http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/909b275a/src/replicate-from.js ---------------------------------------------------------------------- diff --git a/src/replicate-from.js b/src/replicate-from.js index 704176c..a845879 100644 --- a/src/replicate-from.js +++ b/src/replicate-from.js @@ -42,7 +42,7 @@ export default function replicateFrom(cluster, dbname, url) { create_target: !!nmo.config.get('create-target') }); - replicate(replicatorUrl, replicator) + replicate(clusterUrl, replicatorUrl, replicator) .then(resolve) .catch(reject); }); http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/909b275a/src/replicate-to.js ---------------------------------------------------------------------- diff --git a/src/replicate-to.js b/src/replicate-to.js index 5cc567c..705eaa2 100644 --- a/src/replicate-to.js +++ b/src/replicate-to.js @@ -42,7 +42,7 @@ export default function replicateto(cluster, dbname, url) { 'create_target': !!nmo.config.get('create-target') }); - replicate(replicatorUrl, replicator) + replicate(clusterUrl, replicatorUrl, replicator) .then(resolve) .catch(reject); }); http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/909b275a/src/replicate.js ---------------------------------------------------------------------- diff --git a/src/replicate.js b/src/replicate.js index b89c8d3..e593c23 100644 --- a/src/replicate.js +++ b/src/replicate.js @@ -1,4 +1,4 @@ -import { sendJsonToNode } from './utils'; +import { sendJsonToNode, checkNodeOnline } from './utils'; export function createReplicatorDoc (source, target, options) { @@ -16,6 +16,9 @@ export function createReplicatorDoc (source, target, options) { }; } -export function replicate (replicatorUrl, doc) { - return sendJsonToNode(replicatorUrl, doc); +export function replicate (clusterUrl, replicatorUrl, doc) { + return checkNodeOnline(clusterUrl) + .then(() => { + return sendJsonToNode(replicatorUrl, doc); + }); } http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/909b275a/src/savetofile.js ---------------------------------------------------------------------- diff --git a/src/savetofile.js b/src/savetofile.js index 4c1ba82..3a09af3 100644 --- a/src/savetofile.js +++ b/src/savetofile.js @@ -4,7 +4,7 @@ import log from 'npmlog'; import Wreck from 'wreck'; import Promise from 'bluebird'; import JSONStream from 'JSONStream'; -import {getUrlFromCluster, validUrl, removeUsernamePw } from './utils'; +import {checkNodeOnline, getUrlFromCluster, validUrl, removeUsernamePw } from './utils'; import nmo from './nmo.js'; export function cli (url, dbname, file) { @@ -61,7 +61,8 @@ export function saveResToFile (res, file) { export default function savetofile (cluster, dbname, file) { return new Promise((resolve, reject) => { - const url = getUrlFromCluster(cluster) + '/' + dbname + '/_all_docs?include_docs=true'; + const baseUrl = getUrlFromCluster(cluster); + const url = baseUrl + '/' + dbname + '/_all_docs?include_docs=true'; const er = validUrl(url); if (er) { @@ -71,15 +72,20 @@ export default function savetofile (cluster, dbname, file) { const cleanedUrl = removeUsernamePw(url); log.http('request', 'GET', cleanedUrl); - Wreck.request('GET', url, {}, function (err, res) { - if (err) { - return reject(err); - } + checkNodeOnline(baseUrl) + .then(() => { + Wreck.request('GET', url, {}, function (err, res) { + if (err) { + return reject(err); + } + + log.http(res.statusCode, cleanedUrl); + saveResToFile(res, file) + .then(() => resolve()) + .catch((err) => reject(err)); + }); + }) + .catch((err) => reject(err)); - log.http(res.statusCode, cleanedUrl); - saveResToFile(res, file) - .then(() => resolve()) - .catch((err) => {reject(err);}); - }); }); } http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/909b275a/src/utils.js ---------------------------------------------------------------------- diff --git a/src/utils.js b/src/utils.js index 2f4fc38..aa80e03 100644 --- a/src/utils.js +++ b/src/utils.js @@ -90,3 +90,49 @@ export function getClusterUrls (clusterName) { } return Object.keys(nodes).map(key => nodes[key]); } + +export function checkNodeOnline (url) { + let connectError = new Error('Could not connect to Cluster with url ' + url); + connectError.type = 'EUSAGE'; + + return new Promise((resolve, reject) => { + isNodeOnline(url) + .then(res => { + if (res[url]) { + resolve(res); + return; + } + + reject(connectError); + }) + .catch(err => { + reject(connectError); + }); + }); +} + +export function isNodeOnline (url) { + return new Promise((resolve, reject) => { + const er = validUrl(url); + + if (er) { + er.type = 'EUSAGE'; + return reject(er); + } + const cleanedUrl = removeUsernamePw(url); + log.http('request', 'GET', cleanedUrl); + + Wreck.get(url, (err, res, payload) => { + if (err && (err.code === 'ECONNREFUSED' || err.code === 'ENOTFOUND')) { + return resolve({[url]: false}); + } + + if (err) { + return reject(err); + } + + log.http(res.statusCode, cleanedUrl); + resolve({[url]: res.statusCode < 300}); + }); + }); +} http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/909b275a/test/activetasks.js ---------------------------------------------------------------------- diff --git a/test/activetasks.js b/test/activetasks.js index b2b85f3..77487ce 100644 --- a/test/activetasks.js +++ b/test/activetasks.js @@ -4,10 +4,11 @@ import { createConfigFile } from './common'; import nmo from '../src/nmo.js'; import getActiveTask, { filterTasks, cli } from '../src/activetasks.js'; -import { consoleMock } from './helpers'; +import { consoleMock, mockNodeIsOnline } from './helpers'; describe('activetasks', () => { createConfigFile(); + const url = 'http://127.0.0.11'; beforeEach(() => { return nmo @@ -24,7 +25,9 @@ describe('activetasks', () => { }); it('does json', done => { - nock('http://127.0.0.11') + + mockNodeIsOnline(url); + nock(url) .get('/_active_tasks') .reply(200, []); @@ -36,13 +39,13 @@ describe('activetasks', () => { nmo .load({nmoconf: __dirname + '/fixtures/randomini', json: true}) .then(() => { - cli('http://127.0.0.11'); + cli(url); }); }); it('rejests errors', () => { - return cli('http://127.0.0.11') + return cli(url) .catch(err => { assert.ok(err); }); @@ -55,33 +58,34 @@ describe('activetasks', () => { }); }); - - it('returns no active tasks for filter', done => { console.log = consoleMock((msg, log) => { assert.ok(/for that filter/.test(msg)); done(); }); - nock('http://127.0.0.11') + mockNodeIsOnline(url); + + nock(url) .get('/_active_tasks') .reply(200, []); - cli('http://127.0.0.11', 'filter'); + cli(url, 'filter'); }); - it('returns no active tasks', done => { console.log = consoleMock(function (msg) { assert.ok(/There are no active tasks/.test(msg)); done(); }); - nock('http://127.0.0.11') + mockNodeIsOnline(url) + + nock(url) .get('/_active_tasks') .reply(200, []); - cli('http://127.0.0.11'); + cli(url); }); it('returns active tasks', done => { @@ -100,7 +104,8 @@ describe('activetasks', () => { "through_seq":"12313","type":"replication", "updated_on":1444135325,"user":null}]`; - nock('http://127.0.0.11') + mockNodeIsOnline(url); + nock(url) .get('/_active_tasks') .reply(200, resp); @@ -111,13 +116,10 @@ describe('activetasks', () => { done(); }); - cli('http://127.0.0.11'); + cli(url); }); - }); - - describe('getActiveTask', () => { it('rejects for invalid url', () => { @@ -153,11 +155,12 @@ describe('activetasks', () => { "through_seq":"12313","type":"replication", "updated_on":1444135325,"user":null}]`; - nock('http://127.0.0.11') + mockNodeIsOnline(url); + nock(url) .get('/_active_tasks') .reply(200, resp); - return getActiveTask('http://127.0.0.11') + return getActiveTask(url) .then(activetasks => { assert.deepEqual(activetasks, JSON.parse(resp)); }); http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/909b275a/test/helpers.js ---------------------------------------------------------------------- diff --git a/test/helpers.js b/test/helpers.js index 5d6b3d9..b0494ca 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -1,3 +1,11 @@ +import nock from 'nock'; + +export function mockNodeIsOnline (url) { + nock(url) + .get('/') + .reply(200); +} + const origConsole = console.log; export function consoleMock (fn) { http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/909b275a/test/import-csv.js ---------------------------------------------------------------------- diff --git a/test/import-csv.js b/test/import-csv.js index 2bbcbe1..b84ccaa 100644 --- a/test/import-csv.js +++ b/test/import-csv.js @@ -5,6 +5,7 @@ import { createConfigFile } from './common'; import nmo from '../src/nmo.js'; import {cli} from '../src/import-csv.js'; import importcsv from '../src/import-csv.js'; +import {mockNodeIsOnline} from './helpers.js'; const docs = { 'docs':[ @@ -25,7 +26,6 @@ describe('import csv', () => { beforeEach(() => { return nmo.load({nmoconf: __dirname + '/fixtures/randomini'}) - }); describe('cli', () => { @@ -39,23 +39,32 @@ describe('import csv', () => { } }); - it('throws error if bad url', (done) => { + it('throws error if database name missing', (done) => { try { - cli('file', 'bad-url'); + cli('mycluster'); } catch(e) { - assert.ok(/not a valid url/.test(e.message)); + assert.deepEqual(e.type, 'EUSAGE'); done(); } }); + it('throws error if bad url', () => { + return cli('mycluster', 'dataasename', 'bad-url') + .catch(e => { + assert.ok(/Cluster does not exist/.test(e.message)); + }); + }); + it('full integration works', () => { - nock('http://127.0.0.1:5984') + const url = 'http://127.0.0.1'; + mockNodeIsOnline(url); + nock(url) .put('/fake-csv') .reply(200) .post('/fake-csv/_bulk_docs') .reply(200); - return cli(__dirname + '/fixtures/fake.csv', 'http://127.0.0.1:5984/fake-csv', 'delimiter=',''); + return cli('clusterone', 'fake-csv', __dirname + '/fixtures/fake.csv', 'delimiter=',''); }); }); @@ -63,7 +72,9 @@ describe('import csv', () => { it('reports bad file', () => { const url = 'http://127.0.0.1:5984'; - return importcsv('bad-fake.csv', url + '/csv-upload', {}).catch(function (err) { + mockNodeIsOnline(url); + + return importcsv(url, 'csv-upload', 'bad-fake.csv', {}).catch(function (err) { assert.ok(/Error reading file -/.test(err)); }); @@ -71,12 +82,12 @@ describe('import csv', () => { it('logs error for failed request', () => { const url = 'http://127.0.0.1:5984'; - + mockNodeIsOnline(url); nock(url) .put('/csv-upload') .reply(501); - return importcsv(__dirname + '/fixtures/fake.csv', url + '/csv-upload', {}).catch(function (err) { + return importcsv(url, 'csv-upload', __dirname + '/fixtures/fake.csv', {}).catch(function (err) { assert.ok(/CouchDB server answered:/.test(err)); }); @@ -85,13 +96,14 @@ describe('import csv', () => { it('Uploads csv file to CouchDB', () => { const url = 'http://127.0.0.1:5984'; + mockNodeIsOnline(url); nock(url) .put('/csv-upload') .reply(200) .post('/csv-upload/_bulk_docs', docs) .reply(200); - return importcsv(__dirname + '/fixtures/fake.csv', url + '/csv-upload', {delimiter: ',', columns: true}) + return importcsv(url, 'csv-upload', __dirname + '/fixtures/fake.csv', {delimiter: ',', columns: true}) .catch(function (err) { throw 'error ' + err; }); http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/909b275a/test/query.js ---------------------------------------------------------------------- diff --git a/test/query.js b/test/query.js index c439066..4a033ea 100644 --- a/test/query.js +++ b/test/query.js @@ -1,5 +1,5 @@ import assert from 'assert'; -import { consoleMock } from './helpers'; +import { consoleMock, mockNodeIsOnline } from './helpers'; import { cli, run, createIndex } from '../src/query.js'; @@ -48,7 +48,9 @@ describe('Mongo Queries', () => { type : 'json' }; - nock('http://127.0.0.1') + const url = 'http://127.0.0.1'; + mockNodeIsOnline(url) + nock(url) .post('/mydb/_index', index) .reply(200, {result: 'created'}); @@ -76,8 +78,10 @@ describe('Mongo Queries', () => { describe('run', () => { it('requests find run correctly', () => { const selector = {selector: {_id: 'one'}}; + const url = 'http://127.0.0.1'; - nock('http://127.0.0.1') + mockNodeIsOnline(url); + nock(url) .post('/mydb/_find', selector) .reply(200, {docs: [{id: 'one', rev: '123'}]}); @@ -90,7 +94,9 @@ describe('Mongo Queries', () => { it('returns error', () => { var selector = {selector: {_id: 'one'}}; - nock('http://127.0.0.1') + const url = 'http://127.0.0.1'; + + nock(url) .post('/mydb/_find', selector) .reply(500, {message: 'error'}); @@ -110,7 +116,9 @@ describe('Mongo Queries', () => { type : 'json' }; - nock('http://127.0.0.1') + const url = 'http://127.0.0.1'; + mockNodeIsOnline(url); + nock(url) .post('/mydb/_index', index) .reply(200, {result: 'created'}); http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/909b275a/test/replicate-from.js ---------------------------------------------------------------------- diff --git a/test/replicate-from.js b/test/replicate-from.js index d03d753..12d5344 100644 --- a/test/replicate-from.js +++ b/test/replicate-from.js @@ -1,6 +1,7 @@ import assert from 'assert'; import { cli } from '../src/replicate-from.js'; +import {mockNodeIsOnline} from './helpers.js'; import * as common from './common.js'; import nmo from '../src/nmo.js'; @@ -39,7 +40,9 @@ describe('replicate-from', () => { 'create_target':false }; - nock('http://127.0.0.1') + const url = 'http://127.0.0.1'; + mockNodeIsOnline(url) + nock(url) .post('/_replicator', doc) .reply(200, {ok: true, id: '123', rev: '123'}); http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/909b275a/test/replicate-to.js ---------------------------------------------------------------------- diff --git a/test/replicate-to.js b/test/replicate-to.js index 078d307..92ab9d6 100644 --- a/test/replicate-to.js +++ b/test/replicate-to.js @@ -1,6 +1,7 @@ import assert from 'assert'; import { cli } from '../src/replicate-to.js'; +import {mockNodeIsOnline} from './helpers.js'; import * as common from './common.js'; import nmo from '../src/nmo.js'; @@ -38,7 +39,9 @@ describe('replicate-to', () => { 'create_target':false }; - nock('http://127.0.0.1') + const url = 'http://127.0.0.1'; + mockNodeIsOnline(url); + nock(url) .post('/_replicator', doc) .reply(200, {ok: true, id: '123', rev: '123'}); @@ -63,7 +66,10 @@ describe('replicate-to', () => { 'create_target':true }; - nock('http://127.0.0.1') + const url = 'http://127.0.0.1'; + mockNodeIsOnline(url); + + nock(url) .post('/_replicator', doc) .reply(200, {ok: true, id: '123', rev: '123'}); http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/909b275a/test/replicate.js ---------------------------------------------------------------------- diff --git a/test/replicate.js b/test/replicate.js index 741a6be..bae782d 100644 --- a/test/replicate.js +++ b/test/replicate.js @@ -1,5 +1,6 @@ import assert from 'assert'; import nock from 'nock'; +import {mockNodeIsOnline} from './helpers.js'; import {createReplicatorDoc, replicate} from '../src/replicate.js'; @@ -40,22 +41,28 @@ describe('replicate', () => { 'create_target': true }; - nock('http://127.0.0.1') + const url = 'http://127.0.0.1'; + + mockNodeIsOnline(url); + nock(url) .post('/_replicator') .reply(200, data); - return replicate('http://127.0.0.1/_replicator', payload) + return replicate(url, url + '/_replicator', payload) .then(resp => { assert.deepEqual(resp, data); }); }); it('returns error on failed replication', () => { - nock('http://127.0.0.1') + const url = 'http://127.0.0.1'; + + mockNodeIsOnline(url); + nock(url) .post('/_replicator') .reply(500, {reason: 'ERROR'}); - return replicate('http://127.0.0.1/_replicator', {}) + return replicate(url, url + '/_replicator', {}) .catch(err => { assert.deepEqual(err.type, 'EUSAGE'); }); http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/909b275a/test/savetofile.js ---------------------------------------------------------------------- diff --git a/test/savetofile.js b/test/savetofile.js index fd7bab3..e56c45a 100644 --- a/test/savetofile.js +++ b/test/savetofile.js @@ -1,5 +1,6 @@ import assert from 'assert'; import {unlinkSync, readFileSync } from 'fs'; +import {mockNodeIsOnline} from './helpers.js'; import savetofile, {cli} from '../src/savetofile.js'; import nmo from '../src/nmo.js'; @@ -61,6 +62,7 @@ describe('savetofile', () => { it('get all docs for db', () => { const url = 'http://127.0.1.20'; + mockNodeIsOnline(url); nock(url) .get('/test-db/_all_docs?include_docs=true') .reply(200, []); @@ -97,6 +99,7 @@ describe('savetofile', () => { }; const url = 'http://127.0.1.20'; + mockNodeIsOnline(url); nock(url) .get('/test-db/_all_docs?include_docs=true') .reply(200, resp); @@ -138,6 +141,7 @@ describe('savetofile', () => { }; const url = 'http://127.0.1.20'; + mockNodeIsOnline(url); nock(url) .get('/test-db/_all_docs?include_docs=true') .reply(200, resp); @@ -154,13 +158,5 @@ describe('savetofile', () => { }); }); }); - - it('returns error on failed fetch of data', () => { - return savetofile('http://127.0.0.1:5555', 'db', 'the-file') - .catch(err => { - assert.ok(/ECONNREFUSED/.test(err.message)); - }); - }); }); - }); http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/909b275a/test/utils.js ---------------------------------------------------------------------- diff --git a/test/utils.js b/test/utils.js index 1b3b076..b2c8b91 100644 --- a/test/utils.js +++ b/test/utils.js @@ -4,6 +4,7 @@ import nock from 'nock'; import * as utils from '../src/utils.js'; import * as common from './common.js'; import nmo from '../src/nmo.js'; +import { mockNodeIsOnline } from './helpers'; const nmoconf = {nmoconf: __dirname + '/fixtures/randomini'}; common.createConfigFile(); @@ -157,3 +158,27 @@ describe('utils: removeUsernamePw', () => { done(); }); }); + +describe('utils: checkNodeOnline', () => { + + it('resolves for node that is online', () => { + const url = 'http://127.0.0.45'; + mockNodeIsOnline(url); + return utils.checkNodeOnline(url) + .then(res => { + assert.ok(res[url]); + }); + + }); + + it('rejects for node that is offline', () => { + const url = 'http://127.0.0.45'; + + return utils.checkNodeOnline(url) + .catch(err => { + assert.ok(/Could not connect/.test(err.message)); + }); + + }); + +});
