Merge pull request #300 from spalger/fix/nodesToHostCallback

[transport] use the regex used by elasticsearch-py
This commit is contained in:
Spencer
2015-11-16 15:28:19 -06:00
4 changed files with 82 additions and 27 deletions

View File

@ -1,5 +1,7 @@
var _ = require('./utils');
var extractHostPartsRE = /\[\/*([^:]+):(\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({

View File

@ -32,6 +32,7 @@
"globals": {
"describe": true,
"context": true,
"before": true,
"after": true,
"it": true,

16
test/fixtures/short_node_list.2.0.json vendored Normal file
View File

@ -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"
}
}

View File

@ -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: {
@ -47,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/);
});
});
});