Skip to content

Commit

Permalink
add master utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Hoffmann committed May 3, 2015
1 parent f0efe52 commit 9eefffd
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 14 deletions.
68 changes: 58 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,67 @@
var os = require('os');
var request = require("request");

var currentAddress = function() {
var networkInterfaces = os.networkInterfaces( );
for (var networkInterface in networkInterfaces) {
var iface = networkInterfaces[networkInterface];
var utils = function() {

for (var i = 0; i < iface.length; i++) {
var name = iface[i];
if (name.address !== '127.0.0.1' && name.family === 'IPv4' && !name.internal) {
return "http://" + name.address;
var currentAddress = function() {
var networkInterfaces = os.networkInterfaces( );
for (var networkInterface in networkInterfaces) {
var iface = networkInterfaces[networkInterface];

for (var i = 0; i < iface.length; i++) {
var name = iface[i];
if (name.address !== '127.0.0.1' && name.family === 'IPv4' && !name.internal) {
return "http://" + name.address;
}
}
}
}
};

return {
currentAddress: currentAddress
};
};

var master = function(masterUrl) {

/**
* Registers a client at the master.
*
* @param options following options have to be defined:
* - type: type of client (e.g. handler, detector, ..)
* - name: name of client
* - description: description of client.
* - address: network address of client.
* - onSuccess (optional): on success callback function.
* - onError (optional): on error callback function.
*/
var register = function (options) {
request.post({
uri: masterUrl + "/alarm/register/" + options.type,
form: {
name: options.name,
description: options.description,
url: options.address
}
}, function (error, _, body) {
if (!error) {
if (options.onSuccess) {
options.onSuccess(JSON.parse(body));
}
} else {
if (options.onError) {
options.onError(error);
}
}
});
};

return {
register: register
};
};

module.exports = {
currentAddress: currentAddress
utils: utils,
master: master
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "puresec-microservice-js",
"version": "0.1.0",
"version": "0.2.0",
"description": "microservice utilities for the puresec eco system",
"main": "index.js",
"repository": {
Expand Down
46 changes: 43 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ var testee = require('../index');
var assert = require("assert");
var sinon = require("sinon");
var os = require("os");
var request = require("request");

describe("microservice utils", function(){
describe("utils", function(){
var utils = testee.utils();

afterEach(function () {
os.networkInterfaces.restore();
Expand All @@ -23,7 +25,7 @@ describe("microservice utils", function(){
]}
);

assert.equal(testee.currentAddress(), "http://192.168.0.7");
assert.equal(utils.currentAddress(), "http://192.168.0.7");
});

it("shouldn't return network address if no external IPv4 is available", function(){
Expand All @@ -37,7 +39,45 @@ describe("microservice utils", function(){
]}
);

assert.equal(testee.currentAddress(), undefined);
assert.equal(utils.currentAddress(), undefined);
});

});

describe("master", function(){
var master = testee.master("http://some/url/to/master");

var defaultOptions = {
type: "handler",
name: "some name",
description: "some descr",
address: "http://some/client/address",
onSuccess: undefined,
onError: undefined
};

afterEach(function () {
request.post.restore();
});

it("should register a client and call success callback", function(done){
sinon.stub(request, "post").yields(null, null, JSON.stringify({id: "7"}));

defaultOptions.onSuccess = function(jsonBody) {
assert.equal(jsonBody.id, 7);
done();
};
master.register(defaultOptions);
});

it("should call onError callback if error occurs", function(done){
sinon.stub(request, "post").yields({error: "connect ECONNREFUSED"}, null, null);

defaultOptions.onError = function(error) {
assert.equal(error.error, "connect ECONNREFUSED");
done();
};
master.register(defaultOptions);
});

});

0 comments on commit 9eefffd

Please sign in to comment.