Handling a 404 when the space's telemetry collector runs (#55921)

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
Brandon Kobel 2020-02-18 11:47:01 -08:00 committed by GitHub
parent 7cf33c14dd
commit 7763a6055a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 19 deletions

View file

@ -66,6 +66,38 @@ const defaultCallClusterMock = jest.fn().mockResolvedValue({
},
});
describe('error handling', () => {
it('handles a 404 when searching for space usage', async () => {
const { features, licensing, usageCollecion } = setup({
license: { isAvailable: true, type: 'basic' },
});
const { fetch: getSpacesUsage } = getSpacesUsageCollector(usageCollecion as any, {
kibanaIndex: '.kibana',
features,
licensing,
});
await getSpacesUsage(jest.fn().mockRejectedValue({ status: 404 }));
});
it('throws error for a non-404', async () => {
const { features, licensing, usageCollecion } = setup({
license: { isAvailable: true, type: 'basic' },
});
const { fetch: getSpacesUsage } = getSpacesUsageCollector(usageCollecion as any, {
kibanaIndex: '.kibana',
features,
licensing,
});
const statusCodes = [401, 402, 403, 500];
for (const statusCode of statusCodes) {
const error = { status: statusCode };
await expect(getSpacesUsage(jest.fn().mockRejectedValue(error))).rejects.toBe(error);
}
});
});
describe('with a basic license', () => {
let usageStats: UsageStats;
beforeAll(async () => {

View file

@ -50,31 +50,40 @@ async function getSpacesUsage(
const knownFeatureIds = features.getFeatures().map(feature => feature.id);
const resp = await callCluster<SpacesAggregationResponse>('search', {
index: kibanaIndex,
body: {
track_total_hits: true,
query: {
term: {
type: {
value: 'space',
let resp: SpacesAggregationResponse | undefined;
try {
resp = await callCluster<SpacesAggregationResponse>('search', {
index: kibanaIndex,
body: {
track_total_hits: true,
query: {
term: {
type: {
value: 'space',
},
},
},
},
aggs: {
disabledFeatures: {
terms: {
field: 'space.disabledFeatures',
include: knownFeatureIds,
size: knownFeatureIds.length,
aggs: {
disabledFeatures: {
terms: {
field: 'space.disabledFeatures',
include: knownFeatureIds,
size: knownFeatureIds.length,
},
},
},
size: 0,
},
size: 0,
},
});
});
} catch (err) {
if (err.status === 404) {
return {} as UsageStats;
}
const { hits, aggregations } = resp;
throw err;
}
const { hits, aggregations } = resp!;
const count = get(hits, 'total.value', 0);
const disabledFeatureBuckets = get(aggregations, 'disabledFeatures.buckets', []);