-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added tests for check handler, all tests passing
- Loading branch information
1 parent
78d91e2
commit 9c1edf9
Showing
4 changed files
with
110 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const checkHandler = require("../../../lib/custom-services/healthcheck-handlers/check"); | ||
|
||
const _statusMap = { | ||
service1: "serving", | ||
service2: "notserving" | ||
}; | ||
|
||
const mockSend = jest.fn(); | ||
const mockThrow = jest.fn(); | ||
const mockGetStatus = jest.fn(x => { | ||
if (!x) { | ||
return mockServer._statusMap; | ||
} | ||
return mockServer._statusMap[x]; | ||
}); | ||
|
||
const mockServer = { | ||
_statusMap, | ||
getStatus: mockGetStatus, | ||
check: checkHandler | ||
}; | ||
|
||
function callBuilder(payload) { | ||
return { | ||
body: payload, | ||
send: mockSend, | ||
throw: mockThrow | ||
}; | ||
} | ||
|
||
describe("Tests for health check handler", () => { | ||
beforeEach(() => { | ||
mockSend.mockClear(); | ||
mockThrow.mockClear(); | ||
}); | ||
|
||
it("Passes back whole status map if no services passed", () => { | ||
mockServer.check(callBuilder({ services: [] })); | ||
expect(mockSend.mock.calls[0][0].hasOwnProperty("health")).toBeTruthy(); | ||
expect( | ||
mockSend.mock.calls[0][0]["health"][0].service === "service1" || | ||
mockSend.mock.calls[0][0]["health"][0].service === "service2" | ||
).toBeTruthy(); | ||
}); | ||
|
||
it("uses getStatus method on Server", () => { | ||
mockServer.check(callBuilder({ services: ["service1"] })); | ||
expect(mockGetStatus.mock.calls.length).toBeGreaterThanOrEqual(1); | ||
}); | ||
|
||
it("handles if specific service name passed", () => { | ||
mockServer.check(callBuilder({ services: ["service1"] })); | ||
expect(mockSend.mock.calls[0][0].hasOwnProperty("health")).toBeTruthy(); | ||
expect(mockSend.mock.calls[0][0]["health"].length).toEqual(1); | ||
}); | ||
|
||
it("emits an error if wrong service name passed", () => { | ||
mockServer.check(callBuilder({ services: ["service3"] })); | ||
expect(mockThrow.mock.calls[0][0]).toBeInstanceOf(Error); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,35 @@ | ||
module.exports = function(call) { | ||
console.log(call.body); | ||
console.log("this inside of check", this); | ||
const { services: requestedServices } = call.body; | ||
const healthArray = []; | ||
if (!requestedServices.length) { | ||
const statusMap = this.getStatus(); | ||
const services = Object.keys(statusMap); | ||
for (let i = 0; i < services.length; i++) { | ||
healthArray.push({ | ||
service: services[i], | ||
status: statusMap[services[i]] | ||
}); | ||
} | ||
return call.send({ health: healthArray }); | ||
} | ||
// loop through statusMap append the ones in the services array | ||
// if at any point it is not possible | ||
for (let i = 0; i < requestedServices.length; i++) { | ||
const curStatus = this.getStatus(requestedServices[i]); | ||
if (curStatus === null || curStatus === undefined) { | ||
call.throw( | ||
new Error( | ||
"Requested a health check for service not in Server statusMap." | ||
) | ||
); | ||
} | ||
healthArray.push({ | ||
service: requestedServices, | ||
status: curStatus | ||
}); | ||
} | ||
return call.send({ | ||
health: healthArray | ||
}); | ||
// return health array | ||
}; |