Skip to content

Commit

Permalink
added api tests
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaElastic committed Nov 30, 2023
1 parent fb3f477 commit c3e3e8d
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
42 changes: 42 additions & 0 deletions x-pack/plugins/fleet/server/services/output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1679,4 +1679,46 @@ describe('Output Service', () => {
expect(hosts).toEqual(['http://localhost:9200']);
});
});

describe('getLatestOutputHealth', () => {
it('should return unkown state if no hits', async () => {
esClientMock.search.mockResolvedValue({
hits: {
hits: [],
},
} as any);

const response = await outputService.getLatestOutputHealth(esClientMock, 'id');

expect(response).toEqual({
state: 'UNKOWN',
message: '',
timestamp: '',
});
});

it('should return state from hits', async () => {
esClientMock.search.mockResolvedValue({
hits: {
hits: [
{
_source: {
state: 'DEGRADED',
message: 'connection error',
'@timestamp': '2023-11-30T14:25:31Z',
},
},
],
},
} as any);

const response = await outputService.getLatestOutputHealth(esClientMock, 'id');

expect(response).toEqual({
state: 'DEGRADED',
message: 'connection error',
timestamp: '2023-11-30T14:25:31Z',
});
});
});
});
51 changes: 50 additions & 1 deletion x-pack/test/fleet_api_integration/apis/outputs/crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
*/

import expect from '@kbn/expect';
import { GLOBAL_SETTINGS_SAVED_OBJECT_TYPE } from '@kbn/fleet-plugin/common/constants';
import {
GLOBAL_SETTINGS_SAVED_OBJECT_TYPE,
OUTPUT_HEALTH_DATA_STREAM,
} from '@kbn/fleet-plugin/common/constants';
import { FtrProviderContext } from '../../../api_integration/ftr_provider_context';
import { skipIfNoDockerRegistry } from '../../helpers';
import { setupFleetAndAgents } from '../agents/services';
Expand Down Expand Up @@ -189,6 +192,52 @@ export default function (providerContext: FtrProviderContext) {
});
});

describe('GET /outputs/{outputId}/health', () => {
before(async () => {
await es.index({
refresh: 'wait_for',
index: OUTPUT_HEALTH_DATA_STREAM,
document: {
state: 'HEALTHY',
message: '',
'@timestamp': Date.parse('2023-11-29T14:25:31Z'),
output: defaultOutputId,
},
});

await es.index({
refresh: 'wait_for',
index: OUTPUT_HEALTH_DATA_STREAM,
document: {
state: 'DEGRADED',
message: 'connection error',
'@timestamp': Date.parse('2023-11-30T14:25:31Z'),
output: defaultOutputId,
},
});

await es.index({
refresh: 'wait_for',
index: OUTPUT_HEALTH_DATA_STREAM,
document: {
state: 'HEALTHY',
message: '',
'@timestamp': Date.parse('2023-11-31T14:25:31Z'),
output: 'remote2',
},
});
});
it('should allow return the latest output health', async () => {
const { body: outputHealth } = await supertest
.get(`/api/fleet/outputs/${defaultOutputId}/health`)
.expect(200);

expect(outputHealth.state).to.equal('DEGRADED');
expect(outputHealth.message).to.equal('connection error');
expect(outputHealth.timestamp).not.to.be.empty();
});
});

describe('PUT /outputs/{outputId}', () => {
it('should explicitly set port on ES hosts', async function () {
await supertest
Expand Down

0 comments on commit c3e3e8d

Please sign in to comment.