kibana/x-pack/plugins/infra/server/lib/sources/sources.test.ts
Mykola Harmash d0a0a1f9e6
[Infra IU] Disable Metrics Explorer for serverless (#167022)
Closes #163282 

## Summary

This PR:
* Adds a `featureFlags.metricsExplorerEnabled` property to the Infra
plugin config to enable and disable Metrics Explorer depending on the
offering type
* Prevents `MetricsExplorerViewsService` initialization for serveless
based on the feature flag
* Prevents creating Metrics Explorer frontend routes when in serverless
* Prevents registration of the MetricsExplorerViews saved object when in
serverless
* Prevents initialization of the `metrics_explorer_views` API routes
when in serverless

**Trying to access Metrics Explorer in serverless**
<img width="1829" alt="CleanShot 2023-09-22 at 12 59 35@2x"
src="2b039925-0f0b-4c07-be29-bbe910de7a34">

**Trying to access views API**
<img width="1829" alt="CleanShot 2023-09-22 at 13 00 00@2x"
src="15269ec2-becd-4ee3-9b5e-d916df28a7b8">

**`infra/metrics_explorer` API still works as per ticket requirements**
<img width="1829" alt="CleanShot 2023-09-22 at 13 00 06@2x"
src="fb23f912-c6fd-46c8-9084-c17c51e5b064">


## How to test
* Checkout locally
* Enable Infra in `serverless.oblt.yml`: `xpack.infra.enabled: true`
* Run Kibana in serverless mode
* Try accessing `/app/metrics/explorer` route and make sure it's not
available
* Make sure other Infra routes (`/app/metrics/inventory` and
`/app/metrics/hosts`) still load as expected
* In Kibana dev console make sure you get 404 for `GET
kbn:/api/infra/metrics_explorer_views`
* Also check that you don't see `metrics-explorer-view` saved object in
the response for `GET
kbn:/api/kibana/management/saved_objects/_allowed_types`
* Run Kibana in non-serverless mode and make sure Metrics Explorer is
accessible and works as usual

---------

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
2023-09-29 11:27:19 +02:00

158 lines
4.6 KiB
TypeScript

/*
* 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 { SavedObject } from '@kbn/core/server';
import { MetricsDataClient } from '@kbn/metrics-data-access-plugin/server';
import { InfraConfig } from '../../types';
import { infraSourceConfigurationSavedObjectName } from './saved_object_type';
import { InfraSources } from './sources';
describe('the InfraSources lib', () => {
describe('getSourceConfiguration method', () => {
test('returns a source configuration if it exists', async () => {
const sourcesLib = new InfraSources({
config: createMockStaticConfiguration({}),
metricsClient: createMockMetricsDataClient('METRIC_ALIAS'),
});
const request: any = createRequestContext({
id: 'TEST_ID',
version: 'foo',
updated_at: '2000-01-01T00:00:00.000Z',
type: infraSourceConfigurationSavedObjectName,
attributes: {
metricAlias: 'METRIC_ALIAS',
logIndices: { type: 'index_pattern', indexPatternId: 'log_index_pattern_0' },
},
references: [
{
id: 'LOG_INDEX_PATTERN',
name: 'log_index_pattern_0',
type: 'index-pattern',
},
],
});
expect(
await sourcesLib.getSourceConfiguration(request.core.savedObjects.client, 'TEST_ID')
).toMatchObject({
id: 'TEST_ID',
version: 'foo',
updatedAt: 946684800000,
configuration: {
metricAlias: 'METRIC_ALIAS',
logIndices: { type: 'index_pattern', indexPatternId: 'LOG_INDEX_PATTERN' },
},
});
});
test('adds missing attributes from the static configuration to a source configuration', async () => {
const sourcesLib = new InfraSources({
config: createMockStaticConfiguration({
default: {
metricAlias: 'METRIC_ALIAS',
logIndices: { type: 'index_pattern', indexPatternId: 'LOG_ALIAS' },
},
}),
metricsClient: createMockMetricsDataClient('METRIC_ALIAS'),
});
const request: any = createRequestContext({
id: 'TEST_ID',
version: 'foo',
type: infraSourceConfigurationSavedObjectName,
updated_at: '2000-01-01T00:00:00.000Z',
attributes: {},
references: [],
});
expect(
await sourcesLib.getSourceConfiguration(request.core.savedObjects.client, 'TEST_ID')
).toMatchObject({
id: 'TEST_ID',
version: 'foo',
updatedAt: 946684800000,
configuration: {
metricAlias: 'METRIC_ALIAS',
logIndices: { type: 'index_pattern', indexPatternId: 'LOG_ALIAS' },
},
});
});
test('adds missing attributes from the default configuration to a source configuration', async () => {
const sourcesLib = new InfraSources({
config: createMockStaticConfiguration({}),
metricsClient: createMockMetricsDataClient(),
});
const request: any = createRequestContext({
id: 'TEST_ID',
version: 'foo',
type: infraSourceConfigurationSavedObjectName,
updated_at: '2000-01-01T00:00:00.000Z',
attributes: {},
references: [],
});
expect(
await sourcesLib.getSourceConfiguration(request.core.savedObjects.client, 'TEST_ID')
).toMatchObject({
id: 'TEST_ID',
version: 'foo',
updatedAt: 946684800000,
configuration: {
metricAlias: expect.any(String),
logIndices: expect.any(Object),
},
});
});
});
});
const createMockStaticConfiguration = (sources: any): InfraConfig => ({
alerting: {
inventory_threshold: {
group_by_page_size: 10000,
},
metric_threshold: {
group_by_page_size: 10000,
},
},
inventory: {
compositeSize: 2000,
},
featureFlags: {
metricsExplorerEnabled: true,
},
sources,
enabled: true,
});
const createMockMetricsDataClient = (metricAlias: string = 'metrics-*,metricbeat-*') =>
({
getMetricIndices: jest.fn().mockResolvedValue(metricAlias),
updateMetricIndices: jest.fn(),
} as unknown as MetricsDataClient);
const createRequestContext = (savedObject?: SavedObject<unknown>) => {
return {
core: {
savedObjects: {
client: {
async get() {
return savedObject;
},
errors: {
isNotFoundError() {
return typeof savedObject === 'undefined';
},
},
},
},
},
};
};