Skip to content

Commit ef0c364

Browse files
authored
🌊 Streams: Improve integration tests (elastic#213115)
I noticed that a couple integration tests were not actually validating whether the documents got routed the right way (the number and string tests). This PR fixes this by introducing a helper that can easily do the same check we had a couple of times in there.
1 parent d744538 commit ef0c364

File tree

2 files changed

+22
-39
lines changed

2 files changed

+22
-39
lines changed

‎x-pack/test/api_integration/deployment_agnostic/apis/observability/streams/full_flow.ts

+11-39
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
enableStreams,
1717
fetchDocument,
1818
forkStream,
19+
indexAndAssertTargetStream,
1920
indexDocument,
2021
} from './helpers/requests';
2122

@@ -153,10 +154,7 @@ export default function ({ getService }: DeploymentAgnosticFtrProviderContext) {
153154
stream: 'somethingelse', // a field named stream should work as well
154155
}),
155156
};
156-
const response = await indexDocument(esClient, 'logs', doc);
157-
expect(response.result).to.eql('created');
158-
const result = await fetchDocument(esClient, 'logs', response._id);
159-
expect(result._index).to.match(/^\.ds\-logs-.*/);
157+
const result = await indexAndAssertTargetStream(esClient, 'logs', doc);
160158
expect(result._source).to.eql({
161159
'@timestamp': '2024-01-01T00:00:00.000Z',
162160
message: 'test',
@@ -191,11 +189,7 @@ export default function ({ getService }: DeploymentAgnosticFtrProviderContext) {
191189
message: 'test',
192190
}),
193191
};
194-
const response = await indexDocument(esClient, 'logs', doc);
195-
expect(response.result).to.eql('created');
196-
197-
const result = await fetchDocument(esClient, 'logs.nginx', response._id);
198-
expect(result._index).to.match(/^\.ds\-logs.nginx-.*/);
192+
const result = await indexAndAssertTargetStream(esClient, 'logs.nginx', doc);
199193
expect(result._source).to.eql({
200194
'@timestamp': '2024-01-01T00:00:10.000Z',
201195
message: 'test',
@@ -225,11 +219,7 @@ export default function ({ getService }: DeploymentAgnosticFtrProviderContext) {
225219
message: 'test',
226220
}),
227221
};
228-
const response = await indexDocument(esClient, 'logs', doc);
229-
expect(response.result).to.eql('created');
230-
231-
const result = await fetchDocument(esClient, 'logs.nginx.access', response._id);
232-
expect(result._index).to.match(/^\.ds\-logs.nginx.access-.*/);
222+
const result = await indexAndAssertTargetStream(esClient, 'logs.nginx.access', doc);
233223
expect(result._source).to.eql({
234224
'@timestamp': '2024-01-01T00:00:20.000Z',
235225
message: 'test',
@@ -259,11 +249,7 @@ export default function ({ getService }: DeploymentAgnosticFtrProviderContext) {
259249
message: 'test',
260250
}),
261251
};
262-
const response = await indexDocument(esClient, 'logs', doc);
263-
expect(response.result).to.eql('created');
264-
265-
const result = await fetchDocument(esClient, 'logs.nginx', response._id);
266-
expect(result._index).to.match(/^\.ds\-logs.nginx-.*/);
252+
const result = await indexAndAssertTargetStream(esClient, 'logs.nginx', doc);
267253
expect(result._source).to.eql({
268254
'@timestamp': '2024-01-01T00:00:20.000Z',
269255
message: 'test',
@@ -299,10 +285,8 @@ export default function ({ getService }: DeploymentAgnosticFtrProviderContext) {
299285
message: 'test',
300286
}),
301287
};
302-
const response1 = await indexDocument(esClient, 'logs', doc1);
303-
expect(response1.result).to.eql('created');
304-
const response2 = await indexDocument(esClient, 'logs', doc2);
305-
expect(response2.result).to.eql('created');
288+
await indexAndAssertTargetStream(esClient, 'logs.number-test', doc1);
289+
await indexAndAssertTargetStream(esClient, 'logs.number-test', doc2);
306290
});
307291

308292
it('Fork logs to logs.string-test', async () => {
@@ -334,11 +318,8 @@ export default function ({ getService }: DeploymentAgnosticFtrProviderContext) {
334318
message: 'status_code: 400',
335319
}),
336320
};
337-
const response1 = await indexDocument(esClient, 'logs', doc1);
338-
expect(response1.result).to.eql('created');
339-
340-
const response2 = await indexDocument(esClient, 'logs', doc2);
341-
expect(response2.result).to.eql('created');
321+
await indexAndAssertTargetStream(esClient, 'logs.string-test', doc1);
322+
await indexAndAssertTargetStream(esClient, 'logs.string-test', doc2);
342323
});
343324

344325
it('Fork logs to logs.weird-characters', async () => {
@@ -369,17 +350,8 @@ export default function ({ getService }: DeploymentAgnosticFtrProviderContext) {
369350
'weird fieldname': 'Keep where it is',
370351
},
371352
};
372-
const response1 = await indexDocument(esClient, 'logs', doc1);
373-
expect(response1.result).to.eql('created');
374-
375-
const result1 = await fetchDocument(esClient, 'logs.weird-characters', response1._id);
376-
expect(result1._index).to.match(/^\.ds\-logs.weird-characters-.*/);
377-
378-
const response2 = await indexDocument(esClient, 'logs', doc2);
379-
expect(response2.result).to.eql('created');
380-
381-
const result2 = await fetchDocument(esClient, 'logs', response2._id);
382-
expect(result2._index).to.match(/^\.ds\-logs-.*/);
353+
await indexAndAssertTargetStream(esClient, 'logs.weird-characters', doc1);
354+
await indexAndAssertTargetStream(esClient, 'logs', doc2);
383355
});
384356
});
385357
});

‎x-pack/test/api_integration/deployment_agnostic/apis/observability/streams/helpers/requests.ts

+11
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ export async function indexDocument(esClient: Client, index: string, document: J
2626
return response;
2727
}
2828

29+
export async function indexAndAssertTargetStream(
30+
esClient: Client,
31+
target: string,
32+
document: JsonObject
33+
) {
34+
const response = await esClient.index({ index: 'logs', document, refresh: 'wait_for' });
35+
const result = await fetchDocument(esClient, target, response._id);
36+
expect(result._index).to.match(new RegExp(`^\.ds\-${target}-.*`));
37+
return result;
38+
}
39+
2940
export async function fetchDocument(esClient: Client, index: string, id: string) {
3041
const query = {
3142
ids: { values: [id] },

0 commit comments

Comments
 (0)