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

[8.x] [Streams] Schema API integration tests (#204401) #205404

Merged
merged 1 commit into from
Jan 2, 2025
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
12 changes: 12 additions & 0 deletions x-pack/test/api_integration/apis/streams/helpers/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,15 @@ export async function deleteStream(supertest: Agent, id: string) {
const response = await req.send().expect(200);
return response.body;
}

export async function getUnmappedFieldsForStream(supertest: Agent, id: string) {
const req = supertest.get(`/api/streams/${id}/schema/unmapped_fields`).set('kbn-xsrf', 'xxx');
const response = await req.send().expect(200);
return response.body;
}

export async function simulateFieldsForStream(supertest: Agent, id: string, body: JsonObject) {
const req = supertest.post(`/api/streams/${id}/schema/fields_simulation`).set('kbn-xsrf', 'xxx');
const response = await req.send(body).expect(200);
return response.body;
}
1 change: 1 addition & 0 deletions x-pack/test/api_integration/apis/streams/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ export default function ({ loadTestFile }: FtrProviderContext) {
loadTestFile(require.resolve('./enrichment'));
loadTestFile(require.resolve('./classic'));
loadTestFile(require.resolve('./flush_config'));
loadTestFile(require.resolve('./schema'));
});
}
98 changes: 98 additions & 0 deletions x-pack/test/api_integration/apis/streams/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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 {
deleteStream,
enableStreams,
forkStream,
getUnmappedFieldsForStream,
indexDocument,
simulateFieldsForStream,
} from './helpers/requests';
import { FtrProviderContext } from '../../ftr_provider_context';
import { cleanUpRootStream } from './helpers/cleanup';
import { waitForDocumentInIndex } from '../../../alerting_api_integration/observability/helpers/alerting_wait_for_helpers';

export default function ({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const esClient = getService('es');
const retryService = getService('retry');
const logger = getService('log');

describe('Streams Schema', () => {
after(async () => {
await deleteStream(supertest, 'logs.nginx');
await cleanUpRootStream(esClient);
});

before(async () => {
await enableStreams(supertest);

const doc = {
'@timestamp': '2024-01-01T00:00:10.000Z',
message: '2023-01-01T00:00:10.000Z error test',
['some.field']: 'some value',
['another.field']: 'another value',
lastField: 'last value',
['log.level']: 'warning',
};

await indexDocument(esClient, 'logs', doc);
await waitForDocumentInIndex({ esClient, indexName: 'logs', retryService, logger });
});

describe('Unmapped fields API', () => {
it('Returns unmapped fields', async () => {
const response = await getUnmappedFieldsForStream(supertest, 'logs');
expect(response.unmappedFields).to.eql(['another.field', 'lastField', 'some.field']);
});
});

describe('Fields simulation API', () => {
it('Returns failure status when simulation would fail', async () => {
const response = await simulateFieldsForStream(supertest, 'logs', {
field_definitions: [{ name: 'message', type: 'boolean' }],
});

expect(response.status).to.be('failure');
expect(response.simulationError).to.be.a('string');
expect(response.documentsWithRuntimeFieldsApplied).to.be(null);
});
it('Returns success status when simulation would succeed', async () => {
const response = await simulateFieldsForStream(supertest, 'logs', {
field_definitions: [{ name: 'message', type: 'keyword' }],
});

expect(response.status).to.be('success');
expect(response.simulationError).to.be(null);
expect(response.documentsWithRuntimeFieldsApplied).length(1);
});
it('Returns unknown status when documents are missing and status cannot be determined', async () => {
const forkBody = {
stream: {
name: 'logs.nginx',
},
condition: {
field: 'log.logger',
operator: 'eq',
value: 'nginx',
},
};

await forkStream(supertest, 'logs', forkBody);
const response = await simulateFieldsForStream(supertest, 'logs.nginx', {
field_definitions: [{ name: 'message', type: 'keyword' }],
});

expect(response.status).to.be('unknown');
expect(response.simulationError).to.be(null);
expect(response.documentsWithRuntimeFieldsApplied).to.be(null);
});
});
});
}
Loading