Skip to content
This repository has been archived by the owner on Oct 18, 2019. It is now read-only.

Feature/healthcheck json #525

Merged
merged 3 commits into from
May 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions broker-cli/commands/health-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,25 @@ const ENGINE_STATUS_CODES = Object.freeze({
*/

async function healthCheck (args, opts, logger) {
const { rpcAddress } = opts
const {
rpcAddress,
json
} = opts

try {
const client = new BrokerDaemonClient(rpcAddress)

const res = await client.adminService.healthCheck({})

if (json) {
return logger.info(JSON.stringify(res, null, 2))
}

const {
engineStatus = [],
orderbookStatus: orderbookStatuses = [],
relayerStatus = STATUS_CODES.UNKNOWN
} = await client.adminService.healthCheck({})
} = res

const healthcheckTable = new Table({
head: ['Component', 'Status'],
Expand Down Expand Up @@ -114,5 +123,6 @@ module.exports = (program) => {
program
.command('healthcheck', 'Checks the connection between Broker and the Exchange')
.option('--rpc-address [rpc-address]', RPC_ADDRESS_HELP_STRING, validations.isHost)
.option('--json', 'Export result as json', program.BOOL, false)
.action(healthCheck)
}
32 changes: 24 additions & 8 deletions broker-cli/commands/health-check.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,30 @@ describe('healthCheck', () => {
let args
let opts
let logger
let revert
let infoSpy
let errorSpy
let healthCheckStub
let brokerStub
let rpcAddress
let instanceTableStub
let tableStub
let revertTable
let jsonStub
let healthCheckResponse
let reverts

const healthCheck = program.__get__('healthCheck')

beforeEach(() => {
reverts = []
jsonStub = {
stringify: sinon.stub()
}
rpcAddress = undefined
args = {}
opts = { rpcAddress }
infoSpy = sinon.spy()
errorSpy = sinon.spy()
healthCheckStub = sinon.stub().returns({
healthCheckResponse = {
engineStatus: [
{ symbol: 'BTC', status: 'VALIDATED' },
{ symbol: 'LTC', status: 'NOT_SYNCED' }
Expand All @@ -40,15 +45,17 @@ describe('healthCheck', () => {
{ market: 'BTC/LTC', status: 'ORDERBOOK_OK' },
{ market: 'ABC/XYZ', status: 'ORDERBOOK_NOT_SYNCED' }
]
})
}
healthCheckStub = sinon.stub().returns(healthCheckResponse)
instanceTableStub = { push: sinon.stub() }
tableStub = sinon.stub().returns(instanceTableStub)
revertTable = program.__set__('Table', tableStub)

brokerStub = sinon.stub()
brokerStub.prototype.adminService = { healthCheck: healthCheckStub }

revert = program.__set__('BrokerDaemonClient', brokerStub)
reverts.push(program.__set__('Table', tableStub))
reverts.push(program.__set__('BrokerDaemonClient', brokerStub))
reverts.push(program.__set__('JSON', jsonStub))

logger = {
info: infoSpy,
Expand All @@ -57,15 +64,24 @@ describe('healthCheck', () => {
})

afterEach(() => {
revert()
revertTable()
reverts.forEach(r => r())
})

it('makes a request to the broker', async () => {
await healthCheck(args, opts, logger)
expect(healthCheckStub).to.have.been.called()
})

it('returns json if the user specifies a json flag', async () => {
opts.json = true
const result = 'result'
jsonStub.stringify.returns(result)
await healthCheck(args, opts, logger)
expect(instanceTableStub.push).to.not.have.been.called()
expect(jsonStub.stringify).to.have.been.calledWith(healthCheckResponse)
treygriffith marked this conversation as resolved.
Show resolved Hide resolved
expect(logger.info).to.have.been.calledWith(result)
})

it('adds engine statuses to the healthcheck table', async () => {
await healthCheck(args, opts, logger)
expect(instanceTableStub.push).to.have.been.calledWith(['BTC Engine', 'OK'.green])
Expand Down