Skip to content

Commit

Permalink
[JRD-331] Added etcd stuff and test
Browse files Browse the repository at this point in the history
  • Loading branch information
Flamínio Maranhão committed Sep 14, 2015
1 parent 61c5b81 commit 6a9b5a3
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 18 deletions.
63 changes: 51 additions & 12 deletions app/lib/etcd-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ require('colors');

var MC_SERVICE_KEY = 'mc';
var DCM_SERVICE_KEY = 'dcm';
var IPE_SERVICE_KEY = 'ipe';

var dcmURL = null;
var ipeURL = null;
var serviceManager = null;
var registered = false;

Expand Down Expand Up @@ -38,6 +40,7 @@ EtcdManager.prototype.init = function () {
serviceManager = new ServiceManager(process.env.ETCD_SERVER);

this.findDCM();
this.findIPE();
this.register();
}
};
Expand Down Expand Up @@ -67,29 +70,56 @@ EtcdManager.prototype.register = function () {
}
};


/**
* Try to find Development Container Manager ('dcm') service. It saves the
* URL at this.dcmURL.
*
* @function
* (Internal) Helper function to find specifics services in Etcd Manager
*
* @param serviceKey The Key to be found in the Etdc Manager
* @param serviceName Friendly name to the service, only useful for logging
* @param _cb Callback function which will receive the service URL
*/
EtcdManager.prototype.findDCM = function () {
function serviceFinder (serviceKey, serviceName, _cb) {
serviceName = serviceName || serviceKey;
if (serviceManager) {
var promise = serviceManager.findService(DCM_SERVICE_KEY);
var promise = serviceManager.findService(serviceKey);

promise.then(
function (url) {
console.log('Development Container Manager found at etcd'
.green);
dcmURL = url;
var msg = serviceName + 'found at etcd'
console.log(msg.green);
_cb(url);
}, function (err) {
console.log('[ERROR] Finding DCM with etcd: '.red +
JSON.stringify(err));
dcmURL = null;
var msg = '[ERROR] Finding ' + serviceName + ' with etcd: ';
console.log(msg.red + JSON.stringify(err));
_cb(null);
});
} else {
_cb(null);
}
};


/**
* Try to find Development Container Manager ('dcm') service. It saves the
* URL at this.dcmURL.
*
* @function
*/
EtcdManager.prototype.findDCM = function () {
serviceFinder(DCM_SERVICE_KEY, 'Development Container Manager', function(url){
dcmURL = url;
});
}

/**
* Try to find the Mogno-IPE host ('ipe'). It saves the URL at this.ipeURL.
*/
EtcdManager.prototype.findIPE = function () {
serviceFinder(IPE_SERVICE_KEY, 'Mogno IPE', function(url){
ipeURL = url;
});
}

/**
* Try to get DCM url retrieved by etcd.
*
Expand All @@ -100,4 +130,13 @@ EtcdManager.prototype.getDcmUrl = function () {
return dcmURL;
};

/**
* Try to get IPE url retrieved by etcd.
*
* @return {string} ipeURL A url to access the mogno-IPE address
*/
EtcdManager.prototype.getIpeUrl = function () {
return ipeURL;
};

module.exports = EtcdManager;
2 changes: 1 addition & 1 deletion app/routes/infra.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module.exports = function (app) {
var infra = app.controllers.infra;

app.get('/createContainer', infra.createContainer);
app.post('/createContainer', infra.createContainer);

return this;
};
14 changes: 9 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
'use strict';
var consign = require('consign');
var express = require('express');
var config = require('config');
var ws = require('socket.io');
var app = express();
var consign = require('consign');
var express = require('express');
var config = require('config');
var ws = require('socket.io');
var bodyParser = require('body-parser');
var app = express();

var htPort = config.get('htPort');
var wsPort = config.get('wsPort');

var EtcdManager = require('./app/lib/etcd-manager.js');
var etcdManager = new EtcdManager();

// Parse JSON data in post requests
app.use(bodyParser.json());

app.ws = ws(wsPort);

consign({cwd: 'app'})
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"license": "MIT",
"dependencies": {
"body-parser": "^1.13.3",
"carbono-json-messages": "0.0.1",
"carbono-service-manager": "^0.0.3",
"colors": "^1.1.2",
Expand Down
27 changes: 27 additions & 0 deletions test/test-1-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,33 @@ describe('Routing tests --> ', function () {
});
}
);

});

describe('IPE calls ar working well when:', function() {
it('I can request an specific container',
function(done){
var load = {
projectId: 'myProject001',
machineAlias: 'simonfan/code-machine'
};
server
.post('/createContainer', load)
.expect(200);
done();
}
);

it('Previous request should fail when I forget important data',
function(done){
var load = {
projectId: 'myProject001',
}
server
.post('/createContainer', load)
.expect(400)
done();
}
);
})
});

0 comments on commit 6a9b5a3

Please sign in to comment.