-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add slo definition client test
- Loading branch information
Showing
2 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
...servability_solution/slo/server/services/__snapshots__/slo_definition_client.test.ts.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
94 changes: 94 additions & 0 deletions
94
x-pack/plugins/observability_solution/slo/server/services/slo_definition_client.test.ts
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,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", | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
} | ||
`); | ||
}); | ||
}); | ||
}); |