Adds test for corner case in health check

This commit is contained in:
Raul Ochoa 2015-09-22 14:55:50 +02:00
parent 17458f3f4e
commit ac7b02a434
2 changed files with 84 additions and 80 deletions

View File

@ -1,5 +1,4 @@
var fs = require('fs');
var step = require('step');
function HealthCheck(disableFile) {
this.disableFile = disableFile;
@ -9,26 +8,12 @@ module.exports = HealthCheck;
HealthCheck.prototype.check = function(callback) {
var self = this;
step(
function getManualDisable() {
fs.readFile(self.disableFile, this);
},
function handleDisabledFile(err, data) {
var next = this;
if (err) {
return next();
}
if (!!data) {
err = new Error(data);
err.http_status = 503;
throw err;
}
},
function handleResult(err) {
return callback(err);
fs.readFile(this.disableFile, function handleDisabledFile(err, data) {
var disabledError = null;
if (!!data) {
disabledError = new Error(data);
disabledError.http_status = 503;
}
);
return callback(disabledError);
});
};

View File

@ -1,23 +1,22 @@
require(__dirname + '/../support/test_helper');
var assert = require('../support/assert');
var fs = require('fs');
var assert = require('../support/assert');
var CartodbWindshaft = require('../../lib/cartodb/server');
var serverOptions = require('../../lib/cartodb/server_options');
var metadataBackend = {};
var tilelive = {};
var HealthCheck = require('../../lib/cartodb/monitoring/health_check');
var healthCheck = new HealthCheck(metadataBackend, tilelive);
describe('health checks', function () {
function resetHealthConfig() {
function enableHealthConfig() {
global.environment.health = {
enabled: true,
username: 'localhost',
z: 0,
x: 0,
y: 0
enabled: true
};
}
function disableHealthConfig() {
global.environment.health = {
enabled: false
};
}
@ -29,69 +28,89 @@ describe('health checks', function () {
}
};
it('returns 200 and ok=true with enabled configuration', function (done) {
resetHealthConfig();
beforeEach(enableHealthConfig);
afterEach(disableHealthConfig);
var RESPONSE_OK = {
status: 200
};
var RESPONSE_FAIL = {
status: 503
};
it('returns 200 and ok=true with enabled configuration', function (done) {
var server = new CartodbWindshaft(serverOptions);
assert.response(server,
healthCheckRequest,
{
status: 200
},
function (res, err) {
assert.ok(!err);
assert.response(server, healthCheckRequest, RESPONSE_OK, function (res, err) {
assert.ok(!err);
var parsed = JSON.parse(res.body);
var parsed = JSON.parse(res.body);
assert.ok(parsed.enabled);
assert.ok(parsed.ok);
assert.ok(parsed.enabled);
assert.ok(parsed.ok);
done();
}
);
done();
});
});
it('error if disabled file exists', function(done) {
var fs = require('fs');
var errorMessage = "Maintenance";
var readFileFn = fs.readFile;
fs.readFile = function(filename, callback) {
callback(null, "Maintenance");
};
healthCheck.check(function(err) {
assert.equal(err.message, "Maintenance");
assert.equal(err.http_status, 503);
done();
fs.readFile = readFileFn;
});
});
var readFileFn = fs.readFile;
fs.readFile = function(filename, callback) {
callback(null, errorMessage);
};
var server = new CartodbWindshaft(serverOptions);
assert.response(server, healthCheckRequest, RESPONSE_FAIL, function(res, err) {
fs.readFile = readFileFn;
assert.ok(!err);
var parsed = JSON.parse(res.body);
assert.ok(parsed.enabled);
assert.ok(!parsed.ok);
assert.equal(parsed.err, errorMessage);
done();
});
});
it('no error if disabled file exists but has no content', function(done) {
var readFileFn = fs.readFile;
fs.readFile = function(filename, callback) {
callback(null, '');
};
var server = new CartodbWindshaft(serverOptions);
assert.response(server, healthCheckRequest, RESPONSE_OK, function(res, err) {
fs.readFile = readFileFn;
assert.ok(!err);
var parsed = JSON.parse(res.body);
assert.equal(parsed.enabled, true);
assert.equal(parsed.ok, true);
done();
});
});
it('not err if disabled file does not exists', function(done) {
resetHealthConfig();
global.environment.disabled_file = '/tmp/ftreftrgtrccre';
global.environment.disabled_file = '/tmp/ftreftrgtrccre';
var server = new CartodbWindshaft(serverOptions);
var server = new CartodbWindshaft(serverOptions);
assert.response(server, healthCheckRequest, RESPONSE_OK, function (res, err) {
assert.ok(!err);
assert.response(server,
healthCheckRequest,
{
status: 200
},
function (res, err) {
assert.ok(!err);
var parsed = JSON.parse(res.body);
var parsed = JSON.parse(res.body);
assert.equal(parsed.enabled, true);
assert.equal(parsed.ok, true);
assert.equal(parsed.enabled, true);
assert.equal(parsed.ok, true);
done();
}
);
done();
});
});
});