2018-10-24 00:39:02 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-07 16:55:26 +08:00
|
|
|
require('../support/test-helper');
|
2014-11-05 22:42:28 +08:00
|
|
|
|
2015-09-22 20:55:50 +08:00
|
|
|
var fs = require('fs');
|
|
|
|
|
|
|
|
var assert = require('../support/assert');
|
2020-06-02 17:57:11 +08:00
|
|
|
const createServer = require('../../lib/server');
|
2019-10-07 16:10:51 +08:00
|
|
|
var serverOptions = require('../../lib/server-options');
|
2014-11-05 22:42:28 +08:00
|
|
|
|
2015-04-27 23:54:07 +08:00
|
|
|
describe('health checks', function () {
|
2019-10-22 01:07:24 +08:00
|
|
|
function enableHealthConfig () {
|
2015-09-22 20:55:50 +08:00
|
|
|
global.environment.health = {
|
|
|
|
enabled: true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function disableHealthConfig () {
|
2015-01-13 19:09:02 +08:00
|
|
|
global.environment.health = {
|
2015-09-22 20:55:50 +08:00
|
|
|
enabled: false
|
2014-11-05 22:42:28 +08:00
|
|
|
};
|
2015-01-30 23:50:06 +08:00
|
|
|
}
|
2015-01-13 19:09:02 +08:00
|
|
|
|
|
|
|
var healthCheckRequest = {
|
|
|
|
url: '/health',
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
host: 'localhost'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-22 20:55:50 +08:00
|
|
|
beforeEach(enableHealthConfig);
|
|
|
|
afterEach(disableHealthConfig);
|
|
|
|
|
|
|
|
var RESPONSE_OK = {
|
|
|
|
status: 200
|
|
|
|
};
|
|
|
|
|
|
|
|
var RESPONSE_FAIL = {
|
|
|
|
status: 503
|
|
|
|
};
|
2015-01-30 23:50:06 +08:00
|
|
|
|
2015-09-22 20:55:50 +08:00
|
|
|
it('returns 200 and ok=true with enabled configuration', function (done) {
|
2020-06-02 17:57:11 +08:00
|
|
|
var server = createServer(serverOptions);
|
2015-07-09 02:50:34 +08:00
|
|
|
|
2015-09-22 20:55:50 +08:00
|
|
|
assert.response(server, healthCheckRequest, RESPONSE_OK, function (res, err) {
|
|
|
|
assert.ok(!err);
|
2015-01-13 19:09:02 +08:00
|
|
|
|
2015-09-22 20:55:50 +08:00
|
|
|
var parsed = JSON.parse(res.body);
|
2015-01-13 19:09:02 +08:00
|
|
|
|
2015-09-22 20:55:50 +08:00
|
|
|
assert.ok(parsed.enabled);
|
|
|
|
assert.ok(parsed.ok);
|
2015-01-13 19:09:02 +08:00
|
|
|
|
2015-09-22 20:55:50 +08:00
|
|
|
done();
|
|
|
|
});
|
2015-01-13 19:09:02 +08:00
|
|
|
});
|
2014-11-05 22:42:28 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('error if disabled file exists', function (done) {
|
|
|
|
var errorMessage = 'Maintenance';
|
2015-03-26 01:19:40 +08:00
|
|
|
|
2015-09-22 20:55:50 +08:00
|
|
|
var readFileFn = fs.readFile;
|
2019-10-22 01:07:24 +08:00
|
|
|
fs.readFile = function (filename, callback) {
|
2015-09-22 20:55:50 +08:00
|
|
|
callback(null, errorMessage);
|
|
|
|
};
|
2020-06-02 17:57:11 +08:00
|
|
|
var server = createServer(serverOptions);
|
2015-03-26 01:19:40 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
assert.response(server, healthCheckRequest, RESPONSE_FAIL, function (res, err) {
|
2015-09-22 20:55:50 +08:00
|
|
|
fs.readFile = readFileFn;
|
2015-03-26 01:19:40 +08:00
|
|
|
|
2015-09-22 20:55:50 +08:00
|
|
|
assert.ok(!err);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.enabled);
|
|
|
|
assert.ok(!parsed.ok);
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(parsed.err, errorMessage);
|
2015-07-09 02:50:34 +08:00
|
|
|
|
2015-09-22 20:55:50 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-03-26 01:19:40 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('error if disabled file exists but has no content', function (done) {
|
2015-09-22 20:55:50 +08:00
|
|
|
var readFileFn = fs.readFile;
|
2019-10-22 01:07:24 +08:00
|
|
|
fs.readFile = function (filename, callback) {
|
2015-09-22 20:55:50 +08:00
|
|
|
callback(null, '');
|
|
|
|
};
|
2020-06-02 17:57:11 +08:00
|
|
|
var server = createServer(serverOptions);
|
2015-03-26 01:19:40 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
assert.response(server, healthCheckRequest, RESPONSE_FAIL, function (res, err) {
|
2015-09-22 20:55:50 +08:00
|
|
|
fs.readFile = readFileFn;
|
2015-03-26 01:19:40 +08:00
|
|
|
|
2015-09-22 20:55:50 +08:00
|
|
|
assert.ok(!err);
|
|
|
|
var parsed = JSON.parse(res.body);
|
2015-09-22 21:15:57 +08:00
|
|
|
assert.ok(parsed.enabled);
|
|
|
|
assert.ok(!parsed.ok);
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(parsed.err, 'Unknown error');
|
2015-09-22 20:55:50 +08:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('not err if disabled file does not exist', function (done) {
|
2015-09-22 20:55:50 +08:00
|
|
|
global.environment.disabled_file = '/tmp/ftreftrgtrccre';
|
|
|
|
|
2020-06-02 17:57:11 +08:00
|
|
|
var server = createServer(serverOptions);
|
2015-09-22 20:55:50 +08:00
|
|
|
|
|
|
|
assert.response(server, healthCheckRequest, RESPONSE_OK, function (res, err) {
|
|
|
|
assert.ok(!err);
|
|
|
|
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(parsed.enabled, true);
|
|
|
|
assert.strictEqual(parsed.ok, true);
|
2015-09-22 20:55:50 +08:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2016-01-29 02:44:25 +08:00
|
|
|
});
|
2015-01-13 18:29:19 +08:00
|
|
|
});
|