Mock OpenTSDB server.
NOTE: this server is not comprehensive and does not mock all aspects of OpenTSDB. The server mainly mocks the REST API and provides some useful routes when testing response handlers.
For use in Node.js,
$ npm install opentsdb-mock-server
For use in the browser, use browserify.
To use the module,
var createApp = require( 'opentsdb-mock-server' );
To create a new mock OpenTSDB application,
var db = createApp();
The mock OpenTSDB application has the following attributes and methods...
The mock OpenTSDB URL endpoint.
console.log( db.url );
A mock OpenTSDB HTTP request query URL.
console.log( db.query );
Mock OpenTSDB aggregation functions.
console.log( db.aggregators );
// returns [...]
Mock OpenTSDB metrics.
console.log( db.metrics );
// returns [...]
Mock OpenTSDB configuration.
console.log( db.config );
// returns {...}
Mock OpenTSDB version information.
console.log( db.version );
// returns {...}
Mock OpenTSDB response to a request to drop caches.
console.log( db.dropcaches );
// returns {...}
Creates a mock OpenTSDB application HTTP server. The provided callback
is invoked once the server is listening and ready to receive HTTP requests.
var server = db.createServer( function onListen() {
console.log( '...listening...' );
});
The application has the following routes...
Mocks the OpenTSDB query API.
var request = require( 'request' );
request({
'method': 'GET',
'uri': db.query
}, function onResponse( error, response, body ) {
console.log( body );
});
Returns the list of aggregators
supported by the mock OpenTSDB.
var request = require( 'request' );
request({
'method': 'GET',
'uri': db.url + '/api/aggregators'
}, function onResponse( error, response, body ) {
console.log( body );
});
Returns a list of metrics
.
var request = require( 'request' );
request({
'method': 'GET',
'uri': db.url + '/api/suggest'
}, function onResponse( error, response, body ) {
console.log( body );
});
Returns a mock OpenTSDB configuration.
var request = require( 'request' );
request({
'method': 'GET',
'uri': db.url + '/api/config'
}, function onResponse( error, response, body ) {
console.log( body );
});
Returns mock OpenTSDB version information.
var request = require( 'request' );
request({
'method': 'GET',
'uri': db.url + '/api/version'
}, function onResponse( error, response, body ) {
console.log( body );
});
Returns a mock OpenTSDB response to a drop cache request.
var request = require( 'request' );
request({
'method': 'GET',
'uri': db.url + '/api/dropcaches'
}, function onResponse( error, response, body ) {
console.log( body );
});
Returns an empty body; useful when testing HTTP response handling.
var request = require( 'request' );
request({
'method': 'GET',
'uri': db.url + '/bad_body'
}, function onResponse( error, response, body ) {
console.log( body );
});
Returns improperly formatted JSON; useful when testing HTTP response handling.
var request = require( 'request' );
request({
'method': 'GET',
'uri': db.url + '/bad_json'
}, function onResponse( error, response, body ) {
try {
JSON.parse( body );
} catch ( err ) {
console.log( err );
}
});
Returns an empty array
; useful when testing HTTP response handling in the event of no data.
var request = require( 'request' );
request({
'method': 'GET',
'uri': db.url + '/no_data'
}, function onResponse( error, response, body ) {
console.log( body );
});
Returns a sample successful data response.
var request = require( 'request' );
request({
'method': 'GET',
'uri': db.url + '/good_data'
}, function onResponse( error, response, body ) {
console.log( body );
});
var createApp = require( 'opentsdb-mock-server' ),
request = require( 'request' );
// Create a new mock OpenTSDB application:
var db = createApp();
// Get the mock version:
console.log( db.version );
// Get the mock URL:
console.log( db.url );
// Start the mock server:
var server = db.createServer( function onListen() {
console.log( '...listening...' );
request({
'method': 'GET',
'uri': db.query
}, function onResponse( error, response, body ) {
console.log( body );
// Close the server:
server.close();
});
});
To run the example code from the top-level application directory,
$ node ./examples/index.js
Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:
$ make test
All new feature development should have corresponding unit tests to validate correct functionality.
This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:
$ make test-cov
Istanbul creates a ./reports/coverage
directory. To access an HTML version of the report,
$ make view-cov
Copyright © 2014. Athan Reines.