From 7319b49df603cd1722e27be487e899eb14787317 Mon Sep 17 00:00:00 2001 From: spalger Date: Mon, 16 Nov 2015 14:53:13 -0600 Subject: [PATCH 1/4] [transport] use the regex used by elasticsearch-py See https://github.com/elastic/elasticsearch-py/blob/24b48852403ae3dd98b714f1914dd423c2c8ad68/elasticsearch/transport.py#L12 --- src/lib/nodes_to_host.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/nodes_to_host.js b/src/lib/nodes_to_host.js index d6a6226e9..abcb48e79 100644 --- a/src/lib/nodes_to_host.js +++ b/src/lib/nodes_to_host.js @@ -1,5 +1,5 @@ var _ = require('./utils'); -var extractHostPartsRE = /\[\/*([^:]+):(\d+)\]/; +var extractHostPartsRE = /^([\.:0-9a-f]*):([0-9]+)?$/; function makeNodeParser(hostProp) { return function (nodes) { From eda443b5b61b1fb4e8188f1f6d0a42f28e0f7e35 Mon Sep 17 00:00:00 2001 From: spalger Date: Mon, 16 Nov 2015 15:12:05 -0600 Subject: [PATCH 2/4] [transport] maintain support for 1.x nodes formatted urls --- src/lib/nodes_to_host.js | 2 +- test/.jshintrc | 1 + test/fixtures/short_node_list.2.0.json | 16 +++++ test/unit/specs/nodes_to_host_callback.js | 78 ++++++++++++++++------- 4 files changed, 73 insertions(+), 24 deletions(-) create mode 100644 test/fixtures/short_node_list.2.0.json diff --git a/src/lib/nodes_to_host.js b/src/lib/nodes_to_host.js index abcb48e79..eba61081a 100644 --- a/src/lib/nodes_to_host.js +++ b/src/lib/nodes_to_host.js @@ -1,5 +1,5 @@ var _ = require('./utils'); -var extractHostPartsRE = /^([\.:0-9a-f]*):([0-9]+)?$/; +var extractHostPartsRE = /(?:^([\.:0-9a-f]*):([0-9]+)?$|\[\/*([^:]+):(\d+)\])/; function makeNodeParser(hostProp) { return function (nodes) { diff --git a/test/.jshintrc b/test/.jshintrc index ef050dd78..6c7ea6e67 100644 --- a/test/.jshintrc +++ b/test/.jshintrc @@ -32,6 +32,7 @@ "globals": { "describe": true, + "context": true, "before": true, "after": true, "it": true, diff --git a/test/fixtures/short_node_list.2.0.json b/test/fixtures/short_node_list.2.0.json new file mode 100644 index 000000000..8d8248136 --- /dev/null +++ b/test/fixtures/short_node_list.2.0.json @@ -0,0 +1,16 @@ +{ + "id1": { + "name": "Headknocker", + "transport_address": "10.10.10.100:9300", + "hostname": "Spencers-MacBook-Pro.local", + "version": "0.90.5", + "http_address": "10.10.10.100:9205" + }, + "id2": { + "name": "Buttknocker", + "transport_address": "10.10.10.101:9300", + "hostname": "Johns-MacBook-Pro.local", + "version": "0.90.5", + "http_address": "10.10.10.101:9205" + } +} diff --git a/test/unit/specs/nodes_to_host_callback.js b/test/unit/specs/nodes_to_host_callback.js index 282150c54..4102fb0bf 100644 --- a/test/unit/specs/nodes_to_host_callback.js +++ b/test/unit/specs/nodes_to_host_callback.js @@ -4,32 +4,64 @@ describe('Nodes to host callback', function () { // example node list that would come back from "GET _cluster/nodes" var nodes = require('../../fixtures/short_node_list.json'); + var nodes20 = require('../../fixtures/short_node_list.2.0.json'); - it('properly creates host objects', function () { - var hosts = callback(nodes); - expect(hosts.length).to.be(2); - expect(hosts[0]).to.eql({ - host: '10.10.10.100', - port: 9205, - _meta: { - id: 'id1', - name: 'Headknocker', - hostname: 'Spencers-MacBook-Pro.local', - version: '0.90.5' - } - }); - expect(hosts[1]).to.eql({ - host: '10.10.10.101', - port: 9205, - _meta: { - id: 'id2', - name: 'Buttknocker', - hostname: 'Johns-MacBook-Pro.local', - version: '0.90.5' - } + context('pre 2.0 nodes style', function () { + it('properly creates host objects', function () { + var hosts = callback(nodes); + expect(hosts.length).to.be(2); + expect(hosts[0]).to.eql({ + host: '10.10.10.100', + port: 9205, + _meta: { + id: 'id1', + name: 'Headknocker', + hostname: 'Spencers-MacBook-Pro.local', + version: '0.90.5' + } + }); + expect(hosts[1]).to.eql({ + host: '10.10.10.101', + port: 9205, + _meta: { + id: 'id2', + name: 'Buttknocker', + hostname: 'Johns-MacBook-Pro.local', + version: '0.90.5' + } + }); }); }); + context('2.0 nodes style', function () { + it('properly creates host objects', function () { + var hosts = callback(nodes20); + expect(hosts.length).to.be(2); + expect(hosts[0]).to.eql({ + host: '10.10.10.100', + port: 9205, + _meta: { + id: 'id1', + name: 'Headknocker', + hostname: 'Spencers-MacBook-Pro.local', + version: '0.90.5' + } + }); + expect(hosts[1]).to.eql({ + host: '10.10.10.101', + port: 9205, + _meta: { + id: 'id2', + name: 'Buttknocker', + hostname: 'Johns-MacBook-Pro.local', + version: '0.90.5' + } + }); + }); + }); + + + it('ignores hosts that don\'t have an http_host property', function () { var hosts = callback({ node_id: { @@ -50,4 +82,4 @@ describe('Nodes to host callback', function () { }).to.throwException(/does not match the expected pattern/); }); -}); \ No newline at end of file +}); From 015f55497ad11979505fee642f7d53cf47e1ae09 Mon Sep 17 00:00:00 2001 From: spalger Date: Mon, 16 Nov 2015 15:16:28 -0600 Subject: [PATCH 3/4] [transport] split regex in two, so that capturing groups still align --- src/lib/nodes_to_host.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/lib/nodes_to_host.js b/src/lib/nodes_to_host.js index eba61081a..416d4beaf 100644 --- a/src/lib/nodes_to_host.js +++ b/src/lib/nodes_to_host.js @@ -1,5 +1,7 @@ var _ = require('./utils'); -var extractHostPartsRE = /(?:^([\.:0-9a-f]*):([0-9]+)?$|\[\/*([^:]+):(\d+)\])/; + +var extractHostPartsRE1x = /\[\/*([^:]+):(\d+)\]/; +var extractHostPartsRE = /^([\.:0-9a-f]*):([0-9]+)?$/; function makeNodeParser(hostProp) { return function (nodes) { @@ -10,8 +12,12 @@ function makeNodeParser(hostProp) { var hostnameMatches = extractHostPartsRE.exec(node[hostProp]); if (!hostnameMatches) { - throw new Error('node\'s ' + hostProp + ' property (' + JSON.stringify(node[hostProp]) + - ') does not match the expected pattern ' + extractHostPartsRE + '.'); + hostnameMatches = extractHostPartsRE1x.exec(node[hostProp]); + } + + if (!hostnameMatches) { + throw new Error('expected node\'s ' + hostProp + ' property (' + JSON.stringify(node[hostProp]) + + ') to match either ' + extractHostPartsRE + ' or ' + extractHostPartsRE1x + '.'); } hosts.push({ From 4c106c11b081d4d2e03aa0e8bd1d5271f2e96a8d Mon Sep 17 00:00:00 2001 From: spalger Date: Mon, 16 Nov 2015 15:19:14 -0600 Subject: [PATCH 4/4] [transport] fix test regex --- test/unit/specs/nodes_to_host_callback.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/specs/nodes_to_host_callback.js b/test/unit/specs/nodes_to_host_callback.js index 4102fb0bf..6e6f4806d 100644 --- a/test/unit/specs/nodes_to_host_callback.js +++ b/test/unit/specs/nodes_to_host_callback.js @@ -79,7 +79,7 @@ describe('Nodes to host callback', function () { http_address: 'not actually an http host' } }); - }).to.throwException(/does not match the expected pattern/); + }).to.throwException(/expected.*property.*match either/); }); });