Skip to content

Commit

Permalink
test: add slo definition client test
Browse files Browse the repository at this point in the history
  • Loading branch information
kdelemme committed Apr 12, 2024
1 parent 22bde75 commit 38fd514
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 0 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import {
ElasticsearchClientMock,
elasticsearchServiceMock,
httpServiceMock,
loggingSystemMock,
} from '@kbn/core/server/mocks';
import { MockedLogger } from '@kbn/logging-mocks';
import { createSLO } from './fixtures/slo';
import { createSLORepositoryMock } from './mocks';
import { SloDefinitionClient } from './slo_definition_client';
import { SLORepository } from './slo_repository';
import { createTempSummaryDocument } from './summary_transform_generator/helpers/create_temp_summary';

describe('SLODefinitionClient', () => {
let esClientMock: ElasticsearchClientMock;
let loggerMock: jest.Mocked<MockedLogger>;
let mockRepository: jest.Mocked<SLORepository>;
let sloDefinitionClient: SloDefinitionClient;

jest.useFakeTimers().setSystemTime(new Date('2024-01-01'));

beforeEach(() => {
esClientMock = elasticsearchServiceMock.createElasticsearchClient();
loggerMock = loggingSystemMock.createLogger();
mockRepository = createSLORepositoryMock();

sloDefinitionClient = new SloDefinitionClient(mockRepository, esClientMock, loggerMock);
});

describe('happy path', () => {
it('fetches the SLO Definition from the SLO repository when no remoteName is specified', async () => {
const slo = createSLO({ id: 'fixed-id' });
mockRepository.findById.mockResolvedValueOnce(slo);

const response = await sloDefinitionClient.execute('fixed-id', 'default');

expect(response).toEqual({ slo: slo });
});

it('fetches the SLO Definition from the remote summary index when a remoteName is specified', async () => {
const slo = createSLO({ id: 'fixed-id' });
const summaryDoc = createTempSummaryDocument(
slo,
'default',
httpServiceMock.createStartContract().basePath
);
esClientMock.search.mockResolvedValueOnce({
took: 100,
timed_out: false,
_shards: {
total: 0,
successful: 0,
skipped: 0,
failed: 0,
},
hits: {
hits: [{ _source: summaryDoc, _index: '', _id: '' }],
},
});

const response = await sloDefinitionClient.execute('fixed-id', 'default', 'remote_cluster');

expect(response).toMatchSnapshot();
expect(esClientMock.search.mock.calls[0][0]).toMatchInlineSnapshot(`
Object {
"index": "remote_cluster:.slo-observability.summary-v3*",
"query": Object {
"bool": Object {
"filter": Array [
Object {
"term": Object {
"spaceId": "default",
},
},
Object {
"term": Object {
"slo.id": "fixed-id",
},
},
],
},
},
}
`);
});
});
});

0 comments on commit 38fd514

Please sign in to comment.