btrz-health-check
Advanced tools
Comparing version 1.7.1 to 2.0.0
{ | ||
"name": "btrz-health-check", | ||
"version": "1.7.1", | ||
"version": "2.0.0", | ||
"description": "A series of classes that will ping different service types to verify accessibility.", | ||
"main": "index.js", | ||
"directories": { | ||
"test": "test" | ||
}, | ||
"scripts": { | ||
"test": "NODE_ENV=test BUILD_NUMBER=123456789 node_modules/istanbul/lib/cli.js cover node_modules/mocha/bin/_mocha -- --ui bdd -t 5000 --exit", | ||
"test-w": "NODE_ENV=test BUILD_NUMBER=123456789 node_modules/mocha/bin/mocha -w", | ||
"publish": "publish" | ||
"test": "NODE_ENV=test BUILD_NUMBER=123456789 node_modules/mocha/bin/mocha --ui bdd -t 5000 --exit", | ||
"test-w": "NODE_ENV=test BUILD_NUMBER=123456789 node_modules/mocha/bin/mocha -w" | ||
}, | ||
@@ -37,6 +33,5 @@ "repository": { | ||
"devDependencies": { | ||
"chai": "latest", | ||
"istanbul": "^0.4.1", | ||
"mocha": "latest", | ||
"publish": "^0.5.0" | ||
"chai": "4.2.0", | ||
"mocha": "6.2.0", | ||
"sinon": "7.4.1" | ||
}, | ||
@@ -43,0 +38,0 @@ "dependencies": { |
@@ -1,2 +0,2 @@ | ||
# btrz-health-check [![Circle CI](https://circleci.com/gh/Betterez/btrz-health-check.svg?style=svg)](https://circleci.com/gh/Betterez/btrz-health-check) [![NPM version](https://badge-me.herokuapp.com/api/npm/btrz-health-check.png)](http://badges.enytc.com/for/npm/btrz-health-check) | ||
# btrz-health-check | ||
@@ -3,0 +3,0 @@ A series of classes that will ping different service types to verify accessibility. Used in Betterez microservices to check status of dependent services. |
@@ -13,14 +13,9 @@ "use strict"; | ||
checkStatus() { | ||
let self = this; | ||
function resolver(resolve, reject) { | ||
self.db.collectionNames(function (err) { | ||
if (err) { | ||
reject(self.serviceStatus.fails(err)); | ||
} else { | ||
resolve(self.serviceStatus.success()); | ||
} | ||
}); | ||
async checkStatus() { | ||
try { | ||
await this.db.collectionNames(); | ||
return this.serviceStatus.success(); | ||
} catch (err) { | ||
throw this.serviceStatus.fails(err); | ||
} | ||
return new Promise(resolver); | ||
} | ||
@@ -27,0 +22,0 @@ } |
@@ -19,4 +19,10 @@ "use strict"; | ||
.then(function (result) { | ||
expect(result).to.be.eql("localhost"); | ||
expect(result).not.to.be.eql(undefined); | ||
if (result === "localhost") { | ||
expect(result).to.be.eql("localhost"); | ||
} | ||
done(); | ||
}) | ||
.catch((err) => { | ||
done(err); | ||
}); | ||
@@ -32,3 +38,3 @@ }); | ||
expect(result.commit).not.to.be.null; | ||
expect(result.instanceId).to.be.eql("localhost"); | ||
expect(result.instanceId).to.not.be.eql(undefined); | ||
expect(result.build).to.be.eql("123456789"); | ||
@@ -35,0 +41,0 @@ done(); |
"use strict"; | ||
const MongoDbHealthChecker = require("../src/mongodb-health-checker").MongoDbHealthChecker; | ||
const expect = require("chai").expect; | ||
const db = {async collectionNames() { return ["some_collection"];}}; | ||
const sinon = require("sinon"); | ||
const sandbox = sinon.createSandbox(); | ||
describe("MongoDbHealthChecker", function () { | ||
let MongoDbHealthChecker = require("../src/mongodb-health-checker").MongoDbHealthChecker, | ||
expect = require("chai").expect, | ||
db = {collectionNames: function (cb) { return cb();}}; | ||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
it("should fail if not a proper mongoDriver instance", function () { | ||
@@ -12,3 +20,3 @@ function sut() { | ||
} | ||
expect(sut).to.throw(); | ||
expect(sut).to.throw("Requires a valid mongoDbDriver instance that implements 'collectionNames'"); | ||
}); | ||
@@ -20,69 +28,77 @@ | ||
} | ||
expect(sut).to.throw(); | ||
expect(sut).to.throw("Requires a valid mongoDbDriver instance that implements 'collectionNames'"); | ||
}); | ||
it("should return 200 if everything is fine", function (done) { | ||
let checker = new MongoDbHealthChecker(db); | ||
checker.checkStatus().then(function (result) { | ||
expect(result.name).to.be.eql("MongoDb"); | ||
expect(result.status).to.be.eql(200); | ||
done(); | ||
}); | ||
it("should return 200 if everything is fine", async () => { | ||
const checker = new MongoDbHealthChecker(db); | ||
const result = await checker.checkStatus(); | ||
expect(result.name).to.be.eql("MongoDb"); | ||
expect(result.status).to.be.eql(200); | ||
}); | ||
it("should allow for a custom service name", function (done) { | ||
let checker = new MongoDbHealthChecker(db, {name: "MyService"}); | ||
checker.checkStatus().then(function (result) { | ||
expect(result.name).to.be.eql("MyService"); | ||
expect(result.status).to.be.eql(200); | ||
done(); | ||
}); | ||
it("should allow for a custom service name", async () => { | ||
const checker = new MongoDbHealthChecker(db, {name: "MyService"}); | ||
const result = await checker.checkStatus(); | ||
expect(result.name).to.be.eql("MyService"); | ||
expect(result.status).to.be.eql(200); | ||
}); | ||
it("should return 500 if can't connect", function (done) { | ||
let db = | ||
{collectionNames: function (cb) { | ||
cb(new Error()); | ||
} | ||
it("should return 500 if can't connect", async () => { | ||
const db = { | ||
async collectionNames() { | ||
throw new Error("Some error"); | ||
} | ||
}; | ||
let checker = new MongoDbHealthChecker(db); | ||
checker.checkStatus().catch(function (result) { | ||
expect(result.name).to.be.eql("MongoDb"); | ||
expect(result.status).to.be.eql(500); | ||
done(); | ||
}); | ||
const checker = new MongoDbHealthChecker(db); | ||
try { | ||
await checker.checkStatus(); | ||
expect.fail("Expected .checkStatus() to reject"); | ||
} catch (err) { | ||
expect(err.name).to.be.eql("MongoDb"); | ||
expect(err.status).to.be.eql(500); | ||
} | ||
}); | ||
it("should call the logger with the error if provide it", function (done) { | ||
let db = | ||
{collectionNames: function (cb) { | ||
cb(new Error()); | ||
} | ||
}, | ||
options = { | ||
it("should call the logger with the error if provide it", async () => { | ||
const db = { | ||
async collectionNames() { | ||
throw new Error("Some error"); | ||
} | ||
}; | ||
const options = { | ||
logger: { | ||
error: function (name, err) { | ||
expect(name).to.eql("MongoDb"); | ||
expect(err).not.to.be.null; | ||
done(); | ||
} | ||
error: sandbox.stub() | ||
} | ||
}; | ||
let checker = new MongoDbHealthChecker(db, options); | ||
checker.checkStatus(); | ||
try { | ||
await checker.checkStatus(); | ||
expect.fail("Expected .checkStatus() to reject"); | ||
} catch (__) { | ||
expect(options.logger.error.calledOnce).to.be.true; | ||
const [name, err] = options.logger.error.getCall(0).args; | ||
expect(name).to.eql("MongoDb"); | ||
expect(err).not.to.be.null; | ||
} | ||
}); | ||
it("should allow for a custom service name on failures as well", function (done) { | ||
let db = | ||
{collectionNames: function (cb) { | ||
cb(new Error()); | ||
} | ||
it("should allow for a custom service name on failures as well", async () => { | ||
const db = { | ||
async collectionNames() { | ||
throw new Error("Some error"); | ||
} | ||
}; | ||
let checker = new MongoDbHealthChecker(db, {name: "MyService"}); | ||
checker.checkStatus().catch(function (result) { | ||
expect(result.name).to.be.eql("MyService"); | ||
expect(result.status).to.be.eql(500); | ||
done(); | ||
}); | ||
const checker = new MongoDbHealthChecker(db, {name: "MyService"}); | ||
try { | ||
await checker.checkStatus(); | ||
expect.fail("Expected .checkStatus() to reject"); | ||
} catch (err) { | ||
expect(err.name).to.be.eql("MyService"); | ||
expect(err.status).to.be.eql(500); | ||
} | ||
}); | ||
}); |
@@ -47,3 +47,3 @@ "use strict"; | ||
host: "127.0.0.1", | ||
port: "80" | ||
port: "81" | ||
}, | ||
@@ -61,3 +61,3 @@ checker = new SocketHealthChecker(config); | ||
host: "127.0.0.1", | ||
port: "80" | ||
port: "81" | ||
}, | ||
@@ -75,3 +75,3 @@ checker = new SocketHealthChecker(config, {name: "UDP"}); | ||
host: "127.0.0.1", | ||
port: "80" | ||
port: "81" | ||
}, | ||
@@ -78,0 +78,0 @@ options = { |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
3
648
2
28366
20