Co-authored-by: Pete Hampton <pjhampton@users.noreply.github.com>
This commit is contained in:
parent
f6b686490d
commit
ac09981bb6
|
@ -115,23 +115,7 @@ const getMockedEsClient = (esClientMock: jest.Mock) => {
|
|||
};
|
||||
|
||||
describe('error handling', () => {
|
||||
it('handles a 404 when searching for space usage', async () => {
|
||||
const { features, licensing, usageCollection, usageStatsService } = setup({
|
||||
license: { isAvailable: true, type: 'basic' },
|
||||
});
|
||||
const collector = getSpacesUsageCollector(usageCollection as any, {
|
||||
kibanaIndexConfig$: Rx.of({ kibana: { index: '.kibana' } }),
|
||||
features,
|
||||
licensing,
|
||||
usageStatsServicePromise: Promise.resolve(usageStatsService),
|
||||
});
|
||||
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
|
||||
esClient.search.mockRejectedValue({ status: 404 });
|
||||
|
||||
await collector.fetch(getMockFetchContext(esClient));
|
||||
});
|
||||
|
||||
it('throws error for a non-404', async () => {
|
||||
it('throws error if cluster unavailable', async () => {
|
||||
const { features, licensing, usageCollection, usageStatsService } = setup({
|
||||
license: { isAvailable: true, type: 'basic' },
|
||||
});
|
||||
|
@ -143,7 +127,7 @@ describe('error handling', () => {
|
|||
});
|
||||
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
|
||||
|
||||
const statusCodes = [401, 402, 403, 500];
|
||||
const statusCodes = [401, 402, 403, 404, 500];
|
||||
for (const statusCode of statusCodes) {
|
||||
const error = { status: statusCode };
|
||||
esClient.search.mockRejectedValue(error);
|
||||
|
|
|
@ -47,42 +47,31 @@ async function getSpacesUsage(
|
|||
}
|
||||
|
||||
const knownFeatureIds = features.getKibanaFeatures().map((feature) => feature.id);
|
||||
|
||||
let resp: SpacesAggregationResponse | undefined;
|
||||
try {
|
||||
// @ts-expect-error `SearchResponse['hits']['total']` incorrectly expects `number` type instead of `{ value: number }`.
|
||||
({ body: resp } = await esClient.search({
|
||||
index: kibanaIndex,
|
||||
body: {
|
||||
track_total_hits: true,
|
||||
query: {
|
||||
term: {
|
||||
type: {
|
||||
value: 'space',
|
||||
},
|
||||
const { body: resp } = (await esClient.search({
|
||||
index: kibanaIndex,
|
||||
body: {
|
||||
track_total_hits: true,
|
||||
query: {
|
||||
term: {
|
||||
type: {
|
||||
value: 'space',
|
||||
},
|
||||
},
|
||||
aggs: {
|
||||
disabledFeatures: {
|
||||
terms: {
|
||||
field: 'space.disabledFeatures',
|
||||
include: knownFeatureIds,
|
||||
size: knownFeatureIds.length,
|
||||
},
|
||||
},
|
||||
},
|
||||
size: 0,
|
||||
},
|
||||
}));
|
||||
} catch (err) {
|
||||
if (err.status === 404) {
|
||||
return null;
|
||||
}
|
||||
aggs: {
|
||||
disabledFeatures: {
|
||||
terms: {
|
||||
field: 'space.disabledFeatures',
|
||||
include: knownFeatureIds,
|
||||
size: knownFeatureIds.length,
|
||||
},
|
||||
},
|
||||
},
|
||||
size: 0,
|
||||
},
|
||||
})) as { body: SpacesAggregationResponse };
|
||||
|
||||
throw err;
|
||||
}
|
||||
|
||||
const { hits, aggregations } = resp!;
|
||||
const { hits, aggregations } = resp;
|
||||
|
||||
const count = hits?.total?.value ?? 0;
|
||||
const disabledFeatureBuckets = aggregations?.disabledFeatures?.buckets ?? [];
|
||||
|
|
Loading…
Reference in a new issue