-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[Health Gateway] Update response aggregation #145761
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
12716dc
Refactor route handler to simplify dependencies handling
dokmic 5a24cfe
Refactor root route to aggregate polling results
dokmic 6ff6c90
Refactor root route dependencies to simplify testing
dokmic 16c813f
Add unit tests to cover the root route
dokmic 4ef56ea
Fix log message to log response state
dokmic d54f43d
Add additional response types to cover in unit-tests
dokmic 6a40d8a
Remove the error message from the response body, as it is an internal…
dokmic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -6,4 +6,4 @@ | |
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| export { createRootRoute } from './root'; | ||
| export { RootRoute } from './root'; | ||
252 changes: 252 additions & 0 deletions
252
packages/kbn-health-gateway-server/src/kibana/routes/root.test.ts
This file contains hidden or 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,252 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| import { Server } from '@hapi/hapi'; | ||
| import { duration } from 'moment'; | ||
| import fetch, { Response } from 'node-fetch'; | ||
| import { loggerMock, MockedLogger } from '@kbn/logging-mocks'; | ||
| import type { KibanaConfig } from '../kibana_config'; | ||
| import { RootRoute } from './root'; | ||
|
|
||
| describe('RootRoute', () => { | ||
| let kibanaConfig: KibanaConfig; | ||
| let logger: MockedLogger; | ||
| let server: Server; | ||
|
|
||
| beforeAll(async () => { | ||
| jest.spyOn(await import('node-fetch'), 'default'); | ||
| }); | ||
|
|
||
| beforeEach(async () => { | ||
| kibanaConfig = { | ||
| hosts: ['http://localhost:5601'], | ||
| requestTimeout: duration(60, 's'), | ||
| } as unknown as typeof kibanaConfig; | ||
| logger = loggerMock.create(); | ||
|
|
||
| server = new Server(); | ||
| server.route(new RootRoute(kibanaConfig, logger)); | ||
| await server.initialize(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| describe('handler', () => { | ||
| const ok = { status: 200 }; | ||
| const noContent = { status: 204 }; | ||
| const found = { status: 302 }; | ||
| const badRequest = { status: 400 }; | ||
| const unauthorized = { status: 401, headers: { 'www-authenticate': '' } }; | ||
| const forbidden = { status: 403 }; | ||
| const notFound = { status: 404 }; | ||
| const serverError = { status: 500 }; | ||
| const badGateway = { status: 502 }; | ||
| const unavailable = { status: 503 }; | ||
| const timeout = { status: 504 }; | ||
|
|
||
| it.each` | ||
| config | status | code | ||
| ${ok} | ${'healthy'} | ${200} | ||
| ${noContent} | ${'healthy'} | ${200} | ||
| ${found} | ${'healthy'} | ${200} | ||
| ${unauthorized} | ${'healthy'} | ${200} | ||
| ${forbidden} | ${'unhealthy'} | ${503} | ||
| ${notFound} | ${'unhealthy'} | ${503} | ||
| ${badRequest} | ${'unhealthy'} | ${503} | ||
| ${serverError} | ${'unhealthy'} | ${503} | ||
| ${badGateway} | ${'unhealthy'} | ${503} | ||
| ${unavailable} | ${'unhealthy'} | ${503} | ||
| ${timeout} | ${'unhealthy'} | ${503} | ||
| `( | ||
| "should return '$status' with $code when Kibana host returns $config.status", | ||
| async ({ config, status, code }) => { | ||
| (fetch as jest.MockedFunction<typeof fetch>).mockResolvedValueOnce( | ||
| new Response('', config) | ||
| ); | ||
|
|
||
| const response = server.inject({ | ||
| method: 'get', | ||
| url: '/', | ||
| }); | ||
|
|
||
| await expect(response).resolves.toEqual( | ||
| expect.objectContaining({ | ||
| statusCode: code, | ||
| result: expect.objectContaining({ | ||
| status, | ||
| hosts: [ | ||
| expect.objectContaining({ | ||
| status, | ||
| code: config.status, | ||
| host: 'http://localhost:5601', | ||
| }), | ||
| ], | ||
| }), | ||
| }) | ||
| ); | ||
| } | ||
| ); | ||
|
|
||
| it("should return 'failure' with 502 when `fetch` throws an error", async () => { | ||
| (fetch as jest.MockedFunction<typeof fetch>).mockRejectedValueOnce(new Error('Fetch Error')); | ||
| const response = server.inject({ | ||
| method: 'get', | ||
| url: '/', | ||
| }); | ||
|
|
||
| await expect(response).resolves.toEqual( | ||
| expect.objectContaining({ | ||
| statusCode: 502, | ||
| result: expect.objectContaining({ | ||
| status: 'failure', | ||
| hosts: [ | ||
| expect.objectContaining({ | ||
| status: 'failure', | ||
| host: 'http://localhost:5601', | ||
| }), | ||
| ], | ||
| }), | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| it("should return 'timeout' with 504 when `fetch` timeouts", async () => { | ||
| (fetch as jest.MockedFunction<typeof fetch>).mockImplementationOnce( | ||
| (url, { signal } = {}) => { | ||
| return new Promise((resolve, reject) => { | ||
| signal?.addEventListener('abort', () => { | ||
| reject(new DOMException('Fetch Aborted', 'AbortError')); | ||
| }); | ||
|
|
||
| jest.advanceTimersByTime(60000); | ||
| }); | ||
| } | ||
| ); | ||
|
|
||
| jest.useFakeTimers({ doNotFake: ['nextTick'] }); | ||
|
|
||
| const response = server.inject({ | ||
| method: 'get', | ||
| url: '/', | ||
| }); | ||
|
|
||
| try { | ||
| await expect(response).resolves.toEqual( | ||
| expect.objectContaining({ | ||
| statusCode: 504, | ||
| result: expect.objectContaining({ | ||
| status: 'timeout', | ||
| hosts: [ | ||
| expect.objectContaining({ | ||
| status: 'timeout', | ||
| host: 'http://localhost:5601', | ||
| }), | ||
| ], | ||
| }), | ||
| }) | ||
| ); | ||
| } finally { | ||
| jest.useRealTimers(); | ||
| } | ||
| }); | ||
|
|
||
| it("should always return 'healthy' when there are no hosts", async () => { | ||
| kibanaConfig.hosts.splice(0); | ||
|
|
||
| const response = server.inject({ | ||
| method: 'get', | ||
| url: '/', | ||
| }); | ||
|
|
||
| await expect(response).resolves.toEqual( | ||
| expect.objectContaining({ | ||
| statusCode: 200, | ||
| result: expect.objectContaining({ | ||
| status: 'healthy', | ||
| hosts: [], | ||
| }), | ||
| }) | ||
| ); | ||
| expect(fetch).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("should return 'healthy' only when all the hosts healthy", async () => { | ||
| kibanaConfig.hosts.push('http://localhost:5602'); | ||
|
|
||
| (fetch as jest.MockedFunction<typeof fetch>) | ||
| .mockResolvedValueOnce(new Response('', ok)) | ||
| .mockResolvedValueOnce(new Response('', unauthorized)); | ||
|
|
||
| const response = server.inject({ | ||
| method: 'get', | ||
| url: '/', | ||
| }); | ||
|
|
||
| await expect(response).resolves.toEqual( | ||
| expect.objectContaining({ | ||
| statusCode: 200, | ||
| result: expect.objectContaining({ | ||
| status: 'healthy', | ||
| hosts: expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| status: 'healthy', | ||
| code: ok.status, | ||
| host: 'http://localhost:5601', | ||
| }), | ||
| expect.objectContaining({ | ||
| status: 'healthy', | ||
| code: unauthorized.status, | ||
| host: 'http://localhost:5602', | ||
| }), | ||
| ]), | ||
| }), | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| it("should return 'unhealthy' when at least one host is not healthy", async () => { | ||
| kibanaConfig.hosts.push('http://localhost:5602'); | ||
|
|
||
| (fetch as jest.MockedFunction<typeof fetch>) | ||
| .mockResolvedValueOnce(new Response('', ok)) | ||
| .mockResolvedValueOnce(new Response('', serverError)); | ||
|
|
||
| const response = server.inject({ | ||
| method: 'get', | ||
| url: '/', | ||
| }); | ||
|
|
||
| await expect(response).resolves.toEqual( | ||
| expect.objectContaining({ | ||
| statusCode: 503, | ||
| result: expect.objectContaining({ | ||
| status: 'unhealthy', | ||
| hosts: expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| status: 'healthy', | ||
| code: ok.status, | ||
| host: 'http://localhost:5601', | ||
| }), | ||
| expect.objectContaining({ | ||
| status: 'unhealthy', | ||
| code: serverError.status, | ||
| host: 'http://localhost:5602', | ||
| }), | ||
| ]), | ||
| }), | ||
| }) | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.