Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Index Management] Add data streams tests for serverless #171926

Merged
merged 6 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,85 +9,21 @@ import expect from '@kbn/expect';

import { DataStream } from '@kbn/index-management-plugin/common';
import { FtrProviderContext } from '../../../ftr_provider_context';
// @ts-ignore
import { API_BASE_PATH } from './constants';
import { datastreamsHelpers } from './lib/datastreams.helpers';

export default function ({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const es = getService('es');

const createDataStream = async (name: string) => {
// A data stream requires an index template before it can be created.
await es.indices.putIndexTemplate({
name,
body: {
// We need to match the names of backing indices with this template.
index_patterns: [name + '*'],
template: {
mappings: {
properties: {
'@timestamp': {
type: 'date',
},
},
},
lifecycle: {
// @ts-expect-error @elastic/elasticsearch enabled prop is not typed yet
enabled: true,
},
},
data_stream: {},
},
});

await es.indices.createDataStream({ name });
};

const updateIndexTemplateMappings = async (name: string, mappings: any) => {
await es.indices.putIndexTemplate({
name,
body: {
// We need to match the names of backing indices with this template.
index_patterns: [name + '*'],
template: {
mappings,
},
data_stream: {},
},
});
};

const getDatastream = async (name: string) => {
const {
data_streams: [datastream],
} = await es.indices.getDataStream({ name });
return datastream;
};

const getMapping = async (name: string) => {
const res = await es.indices.getMapping({ index: name });

return Object.values(res)[0]!.mappings;
};

const deleteComposableIndexTemplate = async (name: string) => {
await es.indices.deleteIndexTemplate({ name });
};

const deleteDataStream = async (name: string) => {
await es.indices.deleteDataStream({ name });
await deleteComposableIndexTemplate(name);
};

const assertDataStreamStorageSizeExists = (storageSize: string, storageSizeBytes: number) => {
// Storage size of a document doesn't look like it would be deterministic (could vary depending
// on how ES, Lucene, and the file system interact), so we'll just assert its presence and
// type.
expect(storageSize).to.be.ok();
expect(typeof storageSize).to.be('string');
expect(storageSizeBytes).to.be.ok();
expect(typeof storageSizeBytes).to.be('number');
};
const {
createDataStream,
deleteDataStream,
assertDataStreamStorageSizeExists,
deleteComposableIndexTemplate,
updateIndexTemplateMappings,
getMapping,
getDatastream,
} = datastreamsHelpers(getService);

describe('Data streams', function () {
describe('Get', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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 expect from '@kbn/expect';
import { FtrProviderContext } from '../../../../ftr_provider_context';

export function datastreamsHelpers(getService: FtrProviderContext['getService']) {
const es = getService('es');

const createDataStream = async (name: string) => {
// A data stream requires an index template before it can be created.
await es.indices.putIndexTemplate({
name,
body: {
// We need to match the names of backing indices with this template.
index_patterns: [name + '*'],
template: {
mappings: {
properties: {
'@timestamp': {
type: 'date',
},
},
},
lifecycle: {
// @ts-expect-error @elastic/elasticsearch enabled prop is not typed yet
enabled: true,
},
},
data_stream: {},
},
});

await es.indices.createDataStream({ name });
};

const updateIndexTemplateMappings = async (name: string, mappings: any) => {
await es.indices.putIndexTemplate({
name,
body: {
// We need to match the names of backing indices with this template.
index_patterns: [name + '*'],
template: {
mappings,
},
data_stream: {},
},
});
};

const getDatastream = async (name: string) => {
const {
data_streams: [datastream],
} = await es.indices.getDataStream({ name });
return datastream;
};

const getMapping = async (name: string) => {
const res = await es.indices.getMapping({ index: name });

return Object.values(res)[0]!.mappings;
};

const deleteComposableIndexTemplate = async (name: string) => {
await es.indices.deleteIndexTemplate({ name });
};

const deleteDataStream = async (name: string) => {
await es.indices.deleteDataStream({ name });
await deleteComposableIndexTemplate(name);
};

const assertDataStreamStorageSizeExists = (storageSize: string, storageSizeBytes: number) => {
// Storage size of a document doesn't look like it would be deterministic (could vary depending
// on how ES, Lucene, and the file system interact), so we'll just assert its presence and
// type.
expect(storageSize).to.be.ok();
expect(typeof storageSize).to.be('string');
expect(storageSizeBytes).to.be.ok();
expect(typeof storageSizeBytes).to.be('number');
};

return {
createDataStream,
updateIndexTemplateMappings,
getDatastream,
getMapping,
deleteComposableIndexTemplate,
deleteDataStream,
assertDataStreamStorageSizeExists,
};
}
4 changes: 4 additions & 0 deletions x-pack/test/api_integration/services/index_management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ import { FtrProviderContext } from '../ftr_provider_context';
import { indicesApi } from '../apis/management/index_management/lib/indices.api';
import { mappingsApi } from '../apis/management/index_management/lib/mappings.api';
import { indicesHelpers } from '../apis/management/index_management/lib/indices.helpers';
import { datastreamsHelpers } from '../apis/management/index_management/lib/datastreams.helpers';

export function IndexManagementProvider({ getService }: FtrProviderContext) {
return {
indices: {
api: indicesApi(getService),
helpers: indicesHelpers(getService),
},
datastreams: {
helpers: datastreamsHelpers(getService),
},
mappings: {
api: mappingsApi(getService),
},
Expand Down
Loading
Loading