From 12b16ff134e774c247cffc8d5068af46c59db690 Mon Sep 17 00:00:00 2001 From: Garren Smith Date: Wed, 30 Sep 2015 16:11:07 +0200 Subject: [PATCH] Couch-config improvements * Add json support for couch-config get * Add better error handling if nodes offline --- src/couch-config.js | 15 +++++++++++++++ test/couch-config.js | 30 ++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/couch-config.js b/src/couch-config.js index 4012f12..676e386 100644 --- a/src/couch-config.js +++ b/src/couch-config.js @@ -49,6 +49,13 @@ export function get (cluster, nodes, section) { }, {}); promise.then((nodeConfigs) => { + const jsonOut = nmo.config.get('json'); + + if (jsonOut) { + console.log(nodeConfigs); + return nodeConfigs; + } + Object.keys(nodeConfigs).forEach(node => { console.log('NODE:', node); console.log(prettyjson.render(nodeConfigs[node], {})); @@ -148,6 +155,14 @@ export function getConfig (node, url) { Wreck.get(url, (err, res, payload) => { if (err) { + if (err.code === 'ECONNREFUSED' || err.code === 'ENOTFOUND') { + const noNodeErr = new Error('Could not find node ' + node + + ' this could mean the node is down.'); + noNodeErr.type = 'EUSAGE'; + return reject(noNodeErr); + } + + err.type = 'EUSAGE'; return reject(err); } diff --git a/test/couch-config.js b/test/couch-config.js index a533eb7..68eabf7 100644 --- a/test/couch-config.js +++ b/test/couch-config.js @@ -98,7 +98,7 @@ lab.experiment('couch-config', () => { lab.test('gets config bad url returns false', (done) => { getConfig('node1', 'http://127.0.0.2/') .catch(err => { - assert.deepEqual(err.code, 'ECONNREFUSED'); + assert.ok(/Could not find node/.test(err.message)); done(); }); }); @@ -159,7 +159,33 @@ lab.experiment('couch-config', () => { }); }); - lab.test('get prints config', (done) => { + lab.test('get returns json if set', done => { + const nodes = { + node1: 'http://127.0.0.100' + }; + + const resp = { + config1: 'hello', + config2: 'boom' + }; + + nock('http://127.0.0.100') + .get('/_node/node1/_config/uuid') + .reply(200, resp); + + nmo + .load({nmoconf: __dirname + '/fixtures/randomini', json: true}) + .then(() => { + + get('cluster', nodes, 'uuid') + .then(jsonresp => { + assert.deepEqual({node1: resp}, jsonresp); + done(); + }); + }); + }); + + lab.test('get prints config', done => { const nodes = { node1: 'http://127.0.0.1' };