remove hardcoded "default" assets for empty state checks that were fixed in 8.1 (#134895)

This commit is contained in:
Shivindera Singh 2022-06-27 18:24:02 +02:00 committed by GitHub
parent 56512e1d41
commit 01c6343579
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 12 additions and 525 deletions

View file

@ -103,182 +103,4 @@ describe('isUserDataIndex', () => {
expect(isUserDataIndex(fleetAssetIndex)).toBe(false);
});
test('metrics-endpoint.metadata_current_default is not data index', () => {
const fleetAssetIndex: MatchedItem = {
name: 'metrics-endpoint.metadata_current_default',
tags: [{ key: 'index', name: 'Index', color: 'default' }],
item: {
name: 'metrics-endpoint.metadata_current_default',
attributes: [ResolveIndexResponseItemIndexAttrs.OPEN],
},
};
expect(isUserDataIndex(fleetAssetIndex)).toBe(false);
});
test('apm sources are not user sources', () => {
const apmSources: MatchedItem[] = [
{
name: 'apm-7.14.1-error',
tags: [
{
key: 'alias',
name: 'Alias',
color: 'default',
},
],
item: {
name: 'apm-7.14.1-error',
indices: ['apm-7.14.1-error-000001'],
},
},
{
name: 'apm-7.14.1-error-000001',
tags: [
{
key: 'index',
name: 'Index',
color: 'default',
},
],
item: {
name: 'apm-7.14.1-error-000001',
aliases: ['apm-7.14.1-error'],
attributes: [ResolveIndexResponseItemIndexAttrs.OPEN],
},
},
{
name: 'apm-7.14.1-metric',
tags: [
{
key: 'alias',
name: 'Alias',
color: 'default',
},
],
item: {
name: 'apm-7.14.1-metric',
indices: ['apm-7.14.1-metric-000001'],
},
},
{
name: 'apm-7.14.1-metric-000001',
tags: [
{
key: 'index',
name: 'Index',
color: 'default',
},
],
item: {
name: 'apm-7.14.1-metric-000001',
aliases: ['apm-7.14.1-metric'],
attributes: [ResolveIndexResponseItemIndexAttrs.OPEN],
},
},
{
name: 'apm-7.14.1-onboarding-2021.08.25',
tags: [
{
key: 'index',
name: 'Index',
color: 'default',
},
],
item: {
name: 'apm-7.14.1-onboarding-2021.08.25',
attributes: [ResolveIndexResponseItemIndexAttrs.OPEN],
},
},
{
name: 'apm-7.14.1-profile',
tags: [
{
key: 'alias',
name: 'Alias',
color: 'default',
},
],
item: {
name: 'apm-7.14.1-profile',
indices: ['apm-7.14.1-profile-000001'],
},
},
{
name: 'apm-7.14.1-profile-000001',
tags: [
{
key: 'index',
name: 'Index',
color: 'default',
},
],
item: {
name: 'apm-7.14.1-profile-000001',
aliases: ['apm-7.14.1-profile'],
attributes: [ResolveIndexResponseItemIndexAttrs.OPEN],
},
},
{
name: 'apm-7.14.1-span',
tags: [
{
key: 'alias',
name: 'Alias',
color: 'default',
},
],
item: {
name: 'apm-7.14.1-span',
indices: ['apm-7.14.1-span-000001'],
},
},
{
name: 'apm-7.14.1-span-000001',
tags: [
{
key: 'index',
name: 'Index',
color: 'default',
},
],
item: {
name: 'apm-7.14.1-span-000001',
aliases: ['apm-7.14.1-span'],
attributes: [ResolveIndexResponseItemIndexAttrs.OPEN],
},
},
{
name: 'apm-7.14.1-transaction',
tags: [
{
key: 'alias',
name: 'Alias',
color: 'default',
},
],
item: {
name: 'apm-7.14.1-transaction',
indices: ['apm-7.14.1-transaction-000001'],
},
},
{
name: 'apm-7.14.1-transaction-000001',
tags: [
{
key: 'index',
name: 'Index',
color: 'default',
},
],
item: {
name: 'apm-7.14.1-transaction-000001',
aliases: ['apm-7.14.1-transaction'],
attributes: [ResolveIndexResponseItemIndexAttrs.OPEN],
},
},
];
expect(apmSources.some(isUserDataIndex)).toBe(false);
});
});

View file

@ -36,13 +36,8 @@ export function isUserDataIndex(source: MatchedItem) {
// filter out sources from DEFAULT_ASSETS_TO_IGNORE
if (source.name === DEFAULT_ASSETS_TO_IGNORE.LOGS_DATA_STREAM_TO_IGNORE) return false;
if (source.name === DEFAULT_ASSETS_TO_IGNORE.METRICS_DATA_STREAM_TO_IGNORE) return false;
if (source.name === DEFAULT_ASSETS_TO_IGNORE.METRICS_ENDPOINT_INDEX_TO_IGNORE) return false;
if (source.name === DEFAULT_ASSETS_TO_IGNORE.ENT_SEARCH_LOGS_DATA_STREAM_TO_IGNORE) return false;
// filter out empty sources created by apm server
if (source.name.startsWith('apm-')) return false;
return true;
}

View file

@ -29,11 +29,8 @@ export const RUNTIME_FIELD_TYPES = [
*/
export const DEFAULT_ASSETS_TO_IGNORE = {
LOGS_INDEX_PATTERN: 'logs-*',
METRICS_INDEX_PATTERN: 'metrics-*',
LOGS_DATA_STREAM_TO_IGNORE: 'logs-elastic_agent', // ignore ds created by Fleet server itself
METRICS_DATA_STREAM_TO_IGNORE: 'metrics-elastic_agent', // ignore ds created by Fleet server itself
ENT_SEARCH_LOGS_DATA_STREAM_TO_IGNORE: 'logs-enterprise_search.api-default', // ignore ds created by enterprise search
METRICS_ENDPOINT_INDEX_TO_IGNORE: 'metrics-endpoint.metadata_current_default', // ignore index created by Fleet endpoint package installed by default in Cloud
};
/**

View file

@ -12,7 +12,6 @@ import { fieldFormatsMock } from '@kbn/field-formats-plugin/common/mocks';
import { UiSettingsCommon, SavedObjectsClientCommon, SavedObject } from '../types';
import { stubbedSavedObjectIndexPattern } from '../data_view.stub';
import { DEFAULT_ASSETS_TO_IGNORE } from '../constants';
const createFieldsFetcher = jest.fn().mockImplementation(() => ({
getFieldsForWildcard: jest.fn().mockImplementation(() => {
@ -405,41 +404,6 @@ describe('IndexPatterns', () => {
expect(uiSettings.set).toBeCalledTimes(0);
});
test('when setting default it prefers user created data views', async () => {
savedObjectsClient.find = jest.fn().mockResolvedValue([
{
id: 'id1',
version: 'a',
attributes: { title: DEFAULT_ASSETS_TO_IGNORE.LOGS_INDEX_PATTERN },
},
{
id: 'id2',
version: 'a',
attributes: { title: DEFAULT_ASSETS_TO_IGNORE.METRICS_INDEX_PATTERN },
},
{
id: 'id3',
version: 'a',
attributes: { title: 'user-data-view' },
},
]);
savedObjectsClient.get = jest
.fn()
.mockImplementation((type: string, id: string) =>
Promise.resolve({ id, version: 'a', attributes: { title: 'title' } })
);
const defaultDataViewResult = await indexPatterns.getDefaultDataView();
expect(defaultDataViewResult).toBeInstanceOf(DataView);
expect(defaultDataViewResult?.id).toBe('id3');
// make sure we're not pulling from cache
expect(savedObjectsClient.get).toBeCalledTimes(1);
expect(savedObjectsClient.find).toBeCalledTimes(1);
expect(uiSettings.remove).toBeCalledTimes(0);
expect(uiSettings.set).toBeCalledTimes(1);
});
test('dont set defaultIndex without capability allowing advancedSettings save', async () => {
savedObjectsClient.find = jest.fn().mockResolvedValue([
{

View file

@ -12,7 +12,7 @@ import { castEsToKbnFieldTypeName } from '@kbn/field-types';
import { FieldFormatsStartCommon, FORMATS_UI_SETTINGS } from '@kbn/field-formats-plugin/common';
import { SavedObjectNotFound } from '@kbn/kibana-utils-plugin/common';
import uuid from 'uuid';
import { DATA_VIEW_SAVED_OBJECT_TYPE, DEFAULT_ASSETS_TO_IGNORE } from '..';
import { DATA_VIEW_SAVED_OBJECT_TYPE } from '..';
import { SavedObjectsClientCommon } from '../types';
import { createDataViewCache } from '.';
@ -977,15 +977,7 @@ export class DataViewsService {
}
if (!defaultId && patterns.length >= 1 && (await this.hasUserDataView().catch(() => true))) {
// try to set first user created data view as default,
// otherwise fallback to any data view
const userDataViews = patterns.filter(
(pattern) =>
pattern.title !== DEFAULT_ASSETS_TO_IGNORE.LOGS_INDEX_PATTERN &&
pattern.title !== DEFAULT_ASSETS_TO_IGNORE.METRICS_INDEX_PATTERN
);
defaultId = userDataViews[0]?.id ?? patterns[0].id;
defaultId = patterns[0].id;
if (await this.getCanSaveAdvancedSettings()) {
await this.config.set('defaultIndex', defaultId);
}

View file

@ -18,13 +18,8 @@ export class HasData {
// filter out indices that start with `.`
if (source.name.startsWith('.')) return false;
// filter out empty sources created by apm server
if (source.name.startsWith('apm-')) return false;
// filter out sources from DEFAULT_ASSETS_TO_IGNORE
if (source.name === DEFAULT_ASSETS_TO_IGNORE.LOGS_DATA_STREAM_TO_IGNORE) return false;
if (source.name === DEFAULT_ASSETS_TO_IGNORE.METRICS_DATA_STREAM_TO_IGNORE) return false;
if (source.name === DEFAULT_ASSETS_TO_IGNORE.METRICS_ENDPOINT_INDEX_TO_IGNORE) return false;
if (source.name === DEFAULT_ASSETS_TO_IGNORE.ENT_SEARCH_LOGS_DATA_STREAM_TO_IGNORE)
return false;

View file

@ -42,134 +42,4 @@ describe('hasUserIndexPattern', () => {
});
expect(await hasUserIndexPattern({ esClient, soClient })).toEqual(true);
});
describe('when only metrics-* and logs-* index patterns exist', () => {
beforeEach(() => {
soClient.find.mockResolvedValue({
page: 1,
per_page: 100,
total: 2,
saved_objects: [
{
id: '1',
references: [],
type: 'index-pattern',
score: 99,
attributes: { title: 'metrics-*' },
},
{
id: '2',
references: [],
type: 'index-pattern',
score: 99,
attributes: { title: 'logs-*' },
},
],
});
});
it('calls indices.resolveIndex for the index patterns', async () => {
esClient.indices.resolveIndex.mockResponse({
indices: [],
data_streams: [],
aliases: [],
});
await hasUserIndexPattern({ esClient, soClient });
expect(esClient.indices.resolveIndex).toHaveBeenCalledWith({
name: 'logs-*,metrics-*',
});
});
it('returns false if no logs or metrics data_streams exist', async () => {
esClient.indices.resolveIndex.mockResponse({
indices: [],
data_streams: [],
aliases: [],
});
expect(await hasUserIndexPattern({ esClient, soClient })).toEqual(false);
});
it('returns true if any index exists', async () => {
esClient.indices.resolveIndex.mockResponse({
indices: [{ name: 'logs', attributes: [] }],
data_streams: [],
aliases: [],
});
expect(await hasUserIndexPattern({ esClient, soClient })).toEqual(true);
});
it('returns false if only metrics-elastic_agent data stream exists', async () => {
esClient.indices.resolveIndex.mockResponse({
indices: [],
data_streams: [
{
name: 'metrics-elastic_agent',
timestamp_field: '@timestamp',
backing_indices: ['.ds-metrics-elastic_agent'],
},
],
aliases: [],
});
expect(await hasUserIndexPattern({ esClient, soClient })).toEqual(false);
});
it('returns false if only logs-elastic_agent data stream exists', async () => {
esClient.indices.resolveIndex.mockResponse({
indices: [],
data_streams: [
{
name: 'logs-elastic_agent',
timestamp_field: '@timestamp',
backing_indices: ['.ds-logs-elastic_agent'],
},
],
aliases: [],
});
expect(await hasUserIndexPattern({ esClient, soClient })).toEqual(false);
});
it('returns false if only logs-enterprise_search.api-default data stream exists', async () => {
esClient.indices.resolveIndex.mockResponse({
indices: [],
data_streams: [
{
name: 'logs-enterprise_search.api-default',
timestamp_field: '@timestamp',
backing_indices: ['.ds-logs-enterprise_search.api-default-2022.03.07-000001'],
},
],
aliases: [],
});
expect(await hasUserIndexPattern({ esClient, soClient })).toEqual(false);
});
it('returns false if only metrics-endpoint.metadata_current_default index exists', async () => {
esClient.indices.resolveIndex.mockResponse({
indices: [
{
name: 'metrics-endpoint.metadata_current_default',
attributes: ['open'],
},
],
aliases: [],
data_streams: [],
});
expect(await hasUserIndexPattern({ esClient, soClient })).toEqual(false);
});
it('returns true if any other data stream exists', async () => {
esClient.indices.resolveIndex.mockResponse({
indices: [],
data_streams: [
{
name: 'other',
timestamp_field: '@timestamp',
backing_indices: ['.ds-other'],
},
],
aliases: [],
});
expect(await hasUserIndexPattern({ esClient, soClient })).toEqual(true);
});
});
});

View file

@ -20,7 +20,6 @@ interface Deps {
}
export const getIndexPattern = async ({
esClient,
soClient,
}: Deps): Promise<SavedObjectsFindResponse<DataViewSavedObjectAttrs, unknown>> =>
soClient.find<DataViewSavedObjectAttrs>({
@ -42,34 +41,19 @@ export const hasUserIndexPattern = async (
if (indexPatterns.total === 0) {
return false;
}
// If there are any index patterns that are not the default metrics-* and logs-* ones created by Fleet,
// assume there are user created index patterns
if (
indexPatterns.saved_objects.some(
(ip) =>
ip.attributes.title !== DEFAULT_ASSETS_TO_IGNORE.METRICS_INDEX_PATTERN &&
ip.attributes.title !== DEFAULT_ASSETS_TO_IGNORE.LOGS_INDEX_PATTERN
)
) {
return true;
}
const resolveResponse = await esClient.indices.resolveIndex({
name: `${DEFAULT_ASSETS_TO_IGNORE.LOGS_INDEX_PATTERN},${DEFAULT_ASSETS_TO_IGNORE.METRICS_INDEX_PATTERN}`,
name: `${DEFAULT_ASSETS_TO_IGNORE.LOGS_INDEX_PATTERN}`,
});
const hasAnyNonDefaultFleetIndices = resolveResponse.indices.some(
(ds) => ds.name !== DEFAULT_ASSETS_TO_IGNORE.METRICS_ENDPOINT_INDEX_TO_IGNORE
);
if (resolveResponse) {
const hasAnyNonDefaultFleetDataStreams = resolveResponse.data_streams.some(
(ds) =>
ds.name !== DEFAULT_ASSETS_TO_IGNORE.LOGS_DATA_STREAM_TO_IGNORE &&
ds.name !== DEFAULT_ASSETS_TO_IGNORE.ENT_SEARCH_LOGS_DATA_STREAM_TO_IGNORE
);
if (hasAnyNonDefaultFleetIndices) return true;
const hasAnyNonDefaultFleetDataStreams = resolveResponse.data_streams.some(
(ds) =>
ds.name !== DEFAULT_ASSETS_TO_IGNORE.METRICS_DATA_STREAM_TO_IGNORE &&
ds.name !== DEFAULT_ASSETS_TO_IGNORE.LOGS_DATA_STREAM_TO_IGNORE &&
ds.name !== DEFAULT_ASSETS_TO_IGNORE.ENT_SEARCH_LOGS_DATA_STREAM_TO_IGNORE
);
if (hasAnyNonDefaultFleetDataStreams) return true;
return false;
if (hasAnyNonDefaultFleetDataStreams) return false;
}
return true;
};

View file

@ -77,64 +77,4 @@ describe('preview has_data_views route', () => {
body: { hasDataView: true, hasUserDataView: true },
});
});
it('should send hasDataView: true, hasUserDataView: false when default data view exists', async () => {
const mockESClientResolveIndexResponse = { indices: [], aliases: [], data_streams: [] };
const mockSOClientFindResponse = {
page: 1,
per_page: 100,
total: 1,
saved_objects: [
{
type: 'index-pattern',
id: '12345',
namespaces: ['default'],
attributes: { title: 'logs-*' },
},
],
};
const mockESClient = {
indices: {
resolveIndex: jest.fn().mockResolvedValue(mockESClientResolveIndexResponse),
},
};
const mockSOClient = { find: jest.fn().mockResolvedValue(mockSOClientFindResponse) };
const mockContext = {
core: {
elasticsearch: { client: { asCurrentUser: mockESClient } },
savedObjects: { client: mockSOClient },
},
};
const mockRequest = httpServerMock.createKibanaRequest({
body: {},
query: {},
});
const mockResponse = httpServerMock.createResponseFactory();
registerHasDataViewsRoute(mockCoreSetup.http.createRouter());
const mockRouter = mockCoreSetup.http.createRouter.mock.results[0].value;
const handler = mockRouter.get.mock.calls[0][1];
await handler(mockContext as unknown as RequestHandlerContext, mockRequest, mockResponse);
expect(mockSOClient.find.mock.calls[0][0]).toMatchInlineSnapshot(`
Object {
"fields": Array [
"title",
],
"perPage": 100,
"search": "*",
"searchFields": Array [
"title",
],
"type": "index-pattern",
}
`);
expect(mockResponse.ok).toBeCalled();
expect(mockResponse.ok.mock.calls[0][0]).toEqual({
body: { hasDataView: true, hasUserDataView: false },
});
});
});

View file

@ -70,78 +70,6 @@ export default function ({ getService }: FtrProviderContext) {
expect(response.status).to.be(200);
expect(response.body.result).to.be(true);
});
it('should return false if only metric-* index pattern without data', async () => {
await supertest.post(config.path).send({
override: true,
[config.serviceKey]: {
title: 'metrics-*',
allowNoIndex: true,
},
});
const response = await supertest.get(servicePath);
expect(response.status).to.be(200);
expect(response.body.result).to.be(false);
});
it('should return true if metric-* index pattern with user data', async () => {
await es.index({
index: 'metrics-test',
body: {
foo: 'bar',
},
});
await supertest.post(config.path).send({
override: true,
[config.serviceKey]: {
title: 'metrics-*',
},
});
const response = await supertest.get(servicePath);
expect(response.status).to.be(200);
expect(response.body.result).to.be(true);
});
it('should return false if only logs-* index pattern without data', async () => {
await supertest.post(config.path).send({
override: true,
[config.serviceKey]: {
title: 'logs-*',
},
});
const response = await supertest.get(servicePath);
expect(response.status).to.be(200);
expect(response.body.result).to.be(false);
});
it('should return true if logs-* index pattern with user data', async () => {
await es.index({
index: 'logs-test',
body: {
foo: 'bar',
},
});
await supertest.post(config.path).send({
override: true,
[config.serviceKey]: {
title: 'logs-*',
},
});
const response = await supertest.get(servicePath);
expect(response.status).to.be(200);
expect(response.body.result).to.be(true);
});
// TODO: should setup fleet first similar to x-pack/test/fleet_functional/apps/home/welcome.ts
// but it is skipped due to flakiness https://github.com/elastic/kibana/issues/109017
it('should return false if logs-* with .ds-logs-elastic_agent only');
it('should return false if metrics-* with .ds-metrics-elastic_agent only');
});
});
});