nsone-opentsdb-mock-server

0.2.0 • Public • Published

Mock Server

NPM version Build Status Coverage Status Dependencies

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.

Install

For use in Node.js,

$ npm install opentsdb-mock-server

For use in the browser, use browserify.

Usage

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...

db.url

The mock OpenTSDB URL endpoint.

console.log( db.url );

db.query

A mock OpenTSDB HTTP request query URL.

console.log( db.query );

db.aggregators

Mock OpenTSDB aggregation functions.

console.log( db.aggregators );
// returns [...]

db.metrics

Mock OpenTSDB metrics.

console.log( db.metrics );
// returns [...]

db.config

Mock OpenTSDB configuration.

console.log( db.config );
// returns {...}

db.version

Mock OpenTSDB version information.

console.log( db.version );
// returns {...}

db.dropcaches

Mock OpenTSDB response to a request to drop caches.

console.log( db.dropcaches );
// returns {...}

db.createServer( clbk )

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...' );
});

Routes

The application has the following routes...

GET /api/query

Mocks the OpenTSDB query API.

var request = require( 'request' );
 
request({
    'method': 'GET',
    'uri': db.query
}, function onResponse( error, response, body ) {
    console.log( body );
});

GET /api/aggregators

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 );
});

GET /api/suggest

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 );
});

GET /api/config

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 );
});

GET /api/version

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 );
});

GET /api/dropcaches

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 );
});

GET /bad_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 );
});

GET /bad_json

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 );
    }
});

GET /no_data

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 );
});

GET /good_data

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 );
});

Examples

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

Tests

Unit

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.

Test Coverage

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

License

MIT license.


Copyright

Copyright © 2014. Athan Reines.

Package Sidebar

Install

npm i nsone-opentsdb-mock-server

Weekly Downloads

4

Version

0.2.0

License

none

Last publish

Collaborators

  • jabyrd3