testing and clients are 'online' and working know to get the tests running againts an actual ES node

This commit is contained in:
Spencer Alger
2013-09-27 12:46:45 -07:00
parent 0ac81e1989
commit e21fb21ee3
228 changed files with 51083 additions and 921 deletions

View File

@ -1,52 +0,0 @@
describe('Log (the log bridge)', function () {
var Log = require('../src/lib/Log');
describe('level', function () {
it('should return 2 for "warning"', function () {
Log.level('warning', 1)
.should.equal(2);
});
it('should return 2 for 2', function () {
Log.level('2', 1)
.should.equal(2);
});
it('should return the default for an invalid level', function () {
Log.level('invalid level', 2)
.should.equal(2);
});
});
describe('join', function () {
it('should join strings', function () {
Log.join(['one', 'two']).should.equal('one two');
});
it('should flatten nested arrays', function () {
Log.join(['one', ['three', 'four']])
.should.equal('one three,four');
});
it('should flatten arguments', function () {
(function() {
Log.join(arguments).should.equal('one two');
}('one', 'two'));
});
});
describe('When an instance has no outputs', function () {
var log = new Log([]); // no log outputs
it('error should not emit an event and return false', function () {
});
});
});

View File

@ -1,72 +0,0 @@
describe('StdIo Logger', function () {
var Log = require('../src/lib/Log')
, log = new Log([])
, StdIo = require('../src/lib/loggers/StdIo')
, warningLogger;
beforeEach(function () {
if (warningLogger) {
warningLogger.cleanUpListeners();
}
// new logger in warning mode
warningLogger = new StdIo({
level: 2,
type: 'StdIo'
}, log);
});
it('should log error messages', function (done) {
warningLogger.write = function (to, label, colorize, what) {
label.should.equal('ERROR');
what.should.be.an.instanceof(Array);
what[0].should.equal('Test Error Message');
done();
};
log.error('Test Error Message');
});
it('should log warnings', function (done) {
warningLogger.write = function (to, label, colorize, what) {
label.should.equal('WARNING');
what.should.equal('Test Warning Message');
done();
};
log.warn('Test Warning', 'Message');
});
it('should NOT log info messages', function (done) {
if (log.info('Info')) {
done(new Error('There shouldn\'t be listeners for info logs'));
} else {
done();
}
});
it('should NOT log debug messages', function (done) {
if (log.debug('Debug')) {
done(new Error('There shouldn\'t be listeners for debug logs'));
} else {
done();
}
});
it('should NOT log trace messages', function (done) {
if (log.trace('curl "http://localhost:9200" -d "{ \"query\": ... }"')) {
done(new Error('There shouldn\'t be listeners for trace logs'));
} else {
done();
}
});
});

View File

@ -1,107 +0,0 @@
var _ = require('../src/lib/Utils');
describe('Utils', function () {
describe('isArrayOfObjects', function () {
var is;
beforeEach(function () {
is = [{}, {}];
});
it('should identify an array of objects', function () {
_.isArrayOfObjects(is).should.equal(true);
});
it('should identify a non object in the array', function () {
is.push(' not ');
_.isArrayOfObjects(is).should.equal(false);
});
});
describe('isArrayOfStrings', function () {
var is;
beforeEach(function () {
is = ['spencer', 'poop'];
});
it('should identify an array of strings', function () {
_.isArrayOfStrings(is).should.equal(true);
});
it('should identify a non string in the array', function () {
is.push({});
_.isArrayOfStrings(is).should.equal(false);
});
});
describe('isArrayOfArrays', function () {
var is;
beforeEach(function () {
is = [['im'], ['usefull']];
});
it('should identify an array of arrays', function () {
_.isArrayOfArrays(is).should.equal(true);
});
it('should identify a non array in the array', function () {
is.push({});
_.isArrayOfArrays(is).should.equal(false);
});
});
describe('isArrayOfFinites', function () {
var is;
beforeEach(function () {
is = [11123, 666];
});
it('should identify an array of objects', function () {
_.isArrayOfFinites(is).should.equal(true);
});
it('should identify a non object in the array', function () {
is.push(Infinity);
_.isArrayOfFinites(is).should.equal(false);
});
});
describe('isArrayOfFunctions', function () {
var is;
beforeEach(function () {
is = [console.error, console.log];
});
it('should identify an array of functions', function () {
_.isArrayOfFunctions(is).should.equal(true);
});
it('should identify a non function in the array', function () {
is.push('not');
_.isArrayOfFunctions(is).should.equal(false);
});
});
describe('isArrayOfRegExps', function () {
var is;
beforeEach(function () {
is = [/.*/, new RegExp('a')];
});
it('should identify an array of regular expressions', function () {
_.isArrayOfRegExps(is).should.equal(true);
});
it('should identify a non regular expression in the array', function () {
is.push('not');
_.isArrayOfRegExps(is).should.equal(false);
});
});
});

View File

@ -0,0 +1,389 @@
var fs = require('fs')
, path = require('path')
, async = require('async')
, assert = require('assert')
, jsYaml = require('js-yaml')
, indexPrefix = 'yaml_tests_'
, nodeunit = require('nodeunit')
, _ = require('../../src/lib/utils')
, es = require('../../src/elasticsearch');
/**
* Where do our tests live?
* @type {[type]}
*/
var TEST_DIR = path.resolve(__dirname, '../../es_api_spec/test/');
/**
* This will be the object passed to nodeunit, which will run the tests.
* @type {Object}
*/
var nodeunitTests = {
setUp: function (done) {
// RESET the test cluster (deleting indices etc)
// the response var and the stash should be cleared.
done();
}
};
var client = new es.Client({
hosts: ['localhost:9200'],
log: {
level: 'trace'
}
});
/**
* recursively crawl the directory, looking for yaml files which will be passed to loadFile
* @param {String} dir - The directory to crawl
* @return {undefined}
*/
function loadDir(dir) {
fs.readdirSync(dir).forEach(function (fileName) {
var location = path.join(dir, fileName)
, stat = fs.statSync(location);
if (stat.isFile() && fileName.match(/\.yaml$/)) {
loadFile(location);
}
else if (stat.isDirectory()) {
loadDir(location);
}
});
}
/**
* read the file's contents, parse the yaml, pass to makeTest
* @param {String} path - Full path to yaml file
* @return {undefined}
*/
function loadFile(location) {
var fileContents = fs.readFileSync(location, { encoding:'utf8' });
var relativeName = path.relative(TEST_DIR, location);
var groupName = path.dirname(relativeName);
nodeunitTests[groupName] = (nodeunitTests[groupName] || {});
var itterator = _.bind(makeTest, null,
nodeunitTests[groupName],
path.basename(relativeName)
);
jsYaml.loadAll(fileContents, itterator, { filename: location });
}
/**
* Read the test descriptions from a yaml document (usually only one test, per doc but
* sometimes multiple docs per file)
* @param {Object} tests The object to place the tests, which is a part of the spec
* delivered to nodeunit
* @param {String} fileName The filename that this yaml document came from
* @param {Object} testConfigs The yaml document
* @return {undefined}
*/
function makeTest(tests, fileName, testConfigs) {
_.forOwn(testConfigs, function (config, description) {
/**
* convert the config from: [ {name:args}, ... ] to: [ {name:"", args:"" } ]
* so it's easier to work with, taking into consideration the possibility
* that each "set" _could_ have more than one action
*/
// creates [ [ {name:"", args:"" }, ... ], ... ]
var actionSets = _.map(config, function (set) {
return _.map(_.pairs(set), function (pair) {
return {
name: pair[0],
args: pair[1]
};
});
});
var actionList = _.reduce(actionSets, function(note, set) {
return note.concat(set);
}, []);
if (actionList.length) {
tests[fileName + '::' + description] = function (test) {
var outcome = new YamlTest(actionList, test);
};
}
});
}
/**
* The version that ES is running, in comparable string form XXX-XXX-XXX, fetched when needed
* @type {String}
*/
var ES_VERSION = null;
/**
* Regular Expression to extract version numbers from a version string
* @type {RegExp}
*/
var versionExp = '([\\d\\.]+)(?:\\.\\w+)?';
var versionRegExp = new RegExp(versionExp);
var versionRangeRegExp = new RegExp(versionExp + '\\s*\\-\\s*' + versionExp);
/**
* Accepts a list of actions and searched for skips if it finds one it will:
* - query elasticsearch to learn it's version
*
* - parse out the the version and remove
* @param {Array} actions - An array of action objects.
* @param {Function} done - callback for when complete
*/
function filterSkips(actions, done) {
var rangeString, range;
for (var i = 0; i < actions.length; i++) {
if (actions[i].name === 'skip') {
rangeString = actions[i].args.version;
return ES_VERSION == null ? getEsVersion() : haveEsVersion();
}
}
done(); //only called if skip is never found
function versionToComparableString(version) {
var parts = _.map(version.split('.'), function (part) {
part = '' + _.parseInt(part);
return (new Array(4 - part.length)).join('0') + part;
});
while(parts.length < 3) {
parts.push('000');
}
return parts.join('-');
}
function getEsVersion() {
client.info().then(function (resp) {
assert(ES_VERSION = versionRegExp.exec(resp.version.number));
ES_VERSION = versionToComparableString(ES_VERSION[1]);
haveEsVersion();
});
}
function haveEsVersion() {
range = versionRegExp.exec(rangeString);
if (!range) {
throw new Error('Unable to parse version string '+rangeString);
}
range = _.map(_.last(range, 2), versionToComparableString);
if (ES_VERSION >= range[0] && ES_VERSION <= range[1]) {
// remove this and the rest of the skips
actions.splice(i);
done();
} else {
// just remove this skip
actions.splice(i, 1);
// check again incase there are other skips in the list
filterSkips(actions, done);
}
}
}
function YamlTest(actions, test) {
this.actions = actions;
this.test = test;
this._stash = {};
this._last_request = null;
filterSkips(actions, _.bindKey(this, 'run'));
}
YamlTest.prototype = {
run: function () {
async.eachSeries(
this.actions,
_.bind(function (action, done) {
var method = this['do_' + action.name];
this.test.ok(method, 'method exists');
if (method.length > 1) {
// it's async
method.call(this, action.args, done);
} else {
// its a sync test
method.call(this, action.args);
done();
}
}, this),
_.bindKey(this.test, 'done')
);
},
/**
* Get a value from the last response, using dot-notation
*
* Example
* ===
*
* get '_source.tags.1'
*
* from {
* _source: {
* tags: [
* 'one',
* 'two'
* ]
* }
* }
*
* returns 'two'
*
* @param {string} path - The dot-notation path to the value needed.
* @return {*} - The value requested, or undefined if it was not found
*/
get: function (path) {
var steps = path.split('.')
, from = path[0] === '$' ? this._stash : this._last_request
, i;
for (i = 0; from != null && i < steps.length; i++) {
from = from[steps[i]];
}
return from;
},
/**
* Do a request, as outline
* @param {[type]} args [description]
* @param {Function} done [description]
* @return {[type]} [description]
*/
do_do: function (args, done) {
this._last_request = null;
var action = Object.keys(args).pop()
, params = args[action]
, callee = client
, parent;
if (params.index) {
params.index = params.index.replace(/^test_/, indexPrefix);
}
action.split('.').forEach(function (step) {
step = _.camelCase(step);
if(callee[step]) {
// reference to the previous parent, used to set context
parent = callee;
// find the function that this action refers to
callee = callee[step];
} else {
console.log('tried to find', step, 'on', callee);
console.log('parent is', parent);
throw new Error('unable to find step');
}
}, this);
if (typeof callee === 'function') {
callee.call(parent, params)
.then(function (resp) {
done();
})
.fail(function (error) {
throw new Error('The call failed');
});
} else {
throw new Error('stepped in do_do, did not find a function');
}
},
/**
* Set a value from the respose into the stash
*
* Example
* ====
* { _id: id } # stash the value of `response._id` as `id`
*
* @param {Object} args - The object set to the "set" key in the test
* @return {undefined}
*/
do_set: function (args) {
_.forOwn(args, function (name, path) {
this._stash[name] = this.get(path);
}, this);
},
/**
* Test that the specified path exists in the response and has a
* true value (eg. not 0, false, undefined, null or the empty string)
*
* @param {string} path - Path to the response value to test
* @return {undefined}
*/
do_is_true: function (path) {
this.test.ok(this.get(path));
},
/**
* Test that the specified path exists in the response and has a
* false value (eg. 0, false, undefined, null or the empty string)
* @param {string} path - Path to the response value to test
* @return {undefined}
*/
do_is_false: function (path) {
this.test.ok(!this.get(path));
},
/**
* Test that the response field (arg key) matches the value specified
* @param {Object} args - Hash of fields->values that need to be checked
* @return {undefined}
*/
do_match: function (args) {
_.forOwn(args, function (val, path) {
this.test.deepEqual(this.get(path), val);
}, this);
},
/**
* Test that the response field (arg key) is less than the value specified
* @param {Object} args - Hash of fields->values that need to be checked
* @return {undefined}
*/
do_lt: function (args) {
_.forOwn(args, function (num, path) {
this.test.ok(this.get(path) < num);
}, this);
},
/**
* Test that the response field (arg key) is greater than the value specified
* @param {Object} args - Hash of fields->values that need to be checked
* @return {undefined}
*/
do_gt: function (args) {
_.forOwn(args, function (num, path) {
this.test.ok(this.get(path) > num);
}, this);
},
/**
* Test that the response field (arg key) has a length equal to that specified.
* For object values, checks the length of the keys.
* @param {Object} args - Hash of fields->values that need to be checked
* @return {undefined}
*/
do_length: function (args) {
_.forOwn(args, function (len, path) {
this.test.equal(_.size(this.get(path)), len);
}, this);
}
};
loadDir(TEST_DIR);
module.exports['YAML Tests'] = nodeunitTests;

View File

@ -1,5 +0,0 @@
--recursive
--require should
--reporter spec
--ui bdd
--g *.test.js

View File

@ -1,87 +0,0 @@
/* node transport function tests */
// TODO: add check to see if any data in ES, fail if so.
'use strict';
var esj = require('../../dist/elasticsearch-node.js');
var _c = new esj.Client();
exports.transportNode = {
setUp: function(done) {
done();
},
'hosts': function(test) {
test.expect(4);
_c.options.hosts = ['foo:9200','bar:9200'];
test.equal(_c.options.hosts.length, 2, 'should be 2');
test.equal(_c.options.hosts[1], 'bar:9200', 'should be bar:9200');
_c.options.hosts = ['localhost:9200'];
test.equal(_c.options.hosts.length, 1, 'should be 1');
test.equal(_c.options.hosts[0], 'localhost:9200', 'should be localhost:9200');
test.done();
},
'options': function(test) {
test.expect(6);
var _n = new esj.Client();
test.equal(_c.options.sniff_on_start, false, 'should be false');
test.equal(_c.options.sniff_after_requests, 0, 'should be 0');
test.equal(_c.options.sniff_on_connection_fail, false, 'should be false');
test.equal(_c.options.max_retries, 3, 'should be 3');
_c.options.max_retries = 5;
test.equal(_c.options.max_retries, 5, '_c max_retries should be 5');
test.equal(_n.options.max_retries, 3, '_n max_retries should be 3');
test.done();
},
// Create an index with put
'put': function(test) {
test.expect(1);
_c.transport.put('/foo',{},'{"foo":1}',function(res) {
test.equal(res.data.ok,true,'index should be created');
test.done();
});
},
'post': function(test) {
test.expect(1);
_c.transport.post('/foo/bar/baz',{},'{"foo":1}',function(res) {
test.equal(res.data.ok,true,'document should be created');
test.done();
});
},
'get success': function(test) {
test.expect(1);
_c.transport.get('/foo/bar/baz',{},'',function(res) {
test.deepEqual(res.data._source,{foo:1},'should contain document source');
test.done();
});
},
'get error': function(test) {
test.expect(1);
_c.transport.get('/foo/bar',{},'',function(data){},function(res) {
test.equal(res.data,'No handler found for uri [/foo/bar?] and method [GET]','End point should not exist');
test.done();
});
},
'del': function(test) {
test.expect(1);
_c.transport.del('/foo',{},'',function(res) {
test.equal(res.data.ok,true,'index should be deleted');
test.done();
});
},
'error callback': function(test) {
test.expect(1);
_c.options.hosts = ['localhost:1'];
_c.transport.get('/foo/bar',{},'',function(res){
test.equal(res.data,'Test failed','Success function should not be called');
test.done();
},function(res) {
test.equal(res.code,'ECONNREFUSED','Connection should be refused');
test.done();
});
}
};

22
test/unit/client.test.js Normal file
View File

@ -0,0 +1,22 @@
/* node transport function tests */
'use strict';
var Client = require('../../src/lib/client')
, _ = require('../../src/lib/utils')
, api = _.requireDir(module, '../../src/api')
, c;
exports['Client instances creation'] = {
'setUp': function (done) {
c = new Client();
done();
},
'api is inherited': function(test) {
test.equals(c.bulk, api.bulk);
test.equals(c.cluster.node_stats, api.cluster.node_stats);
test.done();
}
};

70
test/unit/log.test.js Normal file
View File

@ -0,0 +1,70 @@
var Log = require('../../src/lib/log');
exports['Log::parseLevels'] = {
'parses a string': function (test) {
test.deepEqual(Log.parseLevels('warning'), ['error', 'warning']);
test.done();
},
'filters an array': function (test) {
test.deepEqual(Log.parseLevels(['trace', 'not a level']), ['trace']);
test.done();
},
'returns nothing as a defauls': function (test) {
test.ok(!Log.parseLevels());
test.done();
}
};
exports['Log::join'] = {
'joins strings': function (test) {
test.equal(Log.join(['one', 'two']), 'one two');
test.done();
},
'flattens nested arrays': function (test) {
test.equal(Log.join(['one', ['three', 'four']]), 'one three,four');
test.done();
},
'flattens arguments': function (test) {
(function() {
test.equal(Log.join(arguments), 'one two');
}('one', 'two'));
test.done();
}
};
/**
* Empty log bridge (no outputs)
* @type {Log}
*/
var log;
exports['Log instance with no outputs'] = {
setUp: function (done) {
log = new Log([]);
done();
},
tearDown: function (done) {
done();
},
'should not emit any events': function (test) {
log.emit = function () {
test.ok(false, 'Emit should not have been called');
};
log.error('Error Message');
test.done();
}
};

View File

@ -0,0 +1,72 @@
var Log = require('../../src/lib/log')
, log = new Log([])
, Stdio = require('../../src/lib/loggers/stdio')
, _ = require('../../src/lib/utils')
, warningLogger;
exports['Stdio Logger'] = {
setUp: function (done) {
if (warningLogger) {
warningLogger.cleanUpListeners();
}
// new logger in warning mode
warningLogger = new Stdio({
levels: ['error', 'warning']
}, log);
done();
},
'logs error messages': function (test) {
test.expect(3);
warningLogger.write = function (to, label, colorize, what) {
test.equal(label, 'ERROR');
test.ok(_.isArray(what), 'Messages logged from calls to error should be an array');
test.equal(what[0], 'Test Error Message');
test.done();
};
log.error('Test Error Message');
},
'should log warnings': function (test) {
test.expect(2);
warningLogger.write = function (to, label, colorize, what) {
test.equal(label, 'WARNING');
test.equal(what, 'Test Warning Message');
test.done();
};
log.warning('Test Warning', 'Message');
},
'should NOT log info messages': function (test) {
if (log.info('Info')) {
test.ok(false, 'There shouldn\'t be listeners for info logs');
} else {
test.done();
}
},
'should NOT log debug messages': function (test) {
if (log.debug('Debug')) {
test.ok(false, 'There shouldn\'t be listeners for debug logs');
} else {
test.done();
}
},
'should NOT log trace messages': function (test) {
if (log.trace('curl "http://localhost:9200" -d "{ \"query\": ... }"')) {
test.ok(false, 'There shouldn\'t be listeners for trace logs');
} else {
test.done();
}
}
};

62
test/unit/utils.test.js Normal file
View File

@ -0,0 +1,62 @@
var _ = require('../../src/lib/utils');
exports.Utils = {
'isArrayOf<Type>': (function (test) {
var things = {
Object: {
is: [[], console.log]
},
PlainObject: {
is: [{}, {}]
},
String: {
is: ['steamy', 'poop'],
not: {}
},
Array: {
is: [['im'], ['usefull']],
},
Finite: {
is: [11123, 666],
not: Infinity
},
Function: {
is: [console.error, console.log],
},
RegExp: {
is: [/.*/, new RegExp('a')],
}
};
return _.map(things, function (thing, name) {
return function (test) {
// ident an array of objects
test.equal(_['isArrayOf' + name + 's'](thing.is), true);
// notice a string in the array
thing.is.push(thing.not || ' not ');
test.equal(_['isArrayOf' + name + 's'](thing.is), false);
test.done();
};
});
})(),
CustomMap: {
'return object for object': function (test) {
var out = _.map({a:1, b:2}, function (val) { return val * 2; });
test.deepEqual(out, {a:2, b:4});
test.done();
},
'reutrn array for anything else': function (test) {
var std = _.map([1, 2, 3], function (val) { return val * 2; });
test.ok(_.isArray(std));
test.deepEqual(
std,
_.map('123', function (val) { return val * 2; })
);
test.done();
},
}
};