Skip to content

Commit e883e50

Browse files
authored
EsArchiver: support for injecting kibana version and use it for spaces tests (#94420)
* EsArchiver: support for injecting kibana version and use it for speeding up spaces tests * Use kbn/utils to get current kibana version in tests * Review feedback: improve test by splitting variable over chunks
1 parent bb26564 commit e883e50

File tree

5 files changed

+41
-9
lines changed
  • packages/kbn-es-archiver/src/lib/archives
  • x-pack/test
    • functional/es_archives/saved_objects_management/spaces_integration
    • saved_object_api_integration/common/fixtures/es_archiver/saved_objects/spaces
    • spaces_api_integration/common/fixtures/es_archiver/saved_objects/spaces

5 files changed

+41
-9
lines changed

packages/kbn-es-archiver/src/lib/archives/parse.test.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
import Stream, { PassThrough, Readable, Writable, Transform } from 'stream';
1010
import { createGzip } from 'zlib';
1111

12-
import { createConcatStream, createListStream, createPromiseFromStreams } from '@kbn/utils';
12+
import {
13+
createConcatStream,
14+
createListStream,
15+
createPromiseFromStreams,
16+
kibanaPackageJson,
17+
} from '@kbn/utils';
1318

1419
import { createParseArchiveStreams } from './parse';
1520

@@ -54,6 +59,17 @@ describe('esArchiver createParseArchiveStreams', () => {
5459
expect(output).toEqual([{ a: 1 }, 1]);
5560
});
5661

62+
it('replaces $KIBANA_PACKAGE_VERSION with the current kibana version', async () => {
63+
const output = await createPromiseFromStreams([
64+
createListStream([
65+
Buffer.from('{"$KIBANA'),
66+
Buffer.from('_PACKAGE_VERSION": "enabled"}'),
67+
]),
68+
...createParseArchiveStreams({ gzip: false }),
69+
]);
70+
return expect(output).toEqual({ [kibanaPackageJson.version]: 'enabled' });
71+
});
72+
5773
it('provides each JSON object as soon as it is parsed', async () => {
5874
let onReceived: (resolved: any) => void;
5975
const receivedPromise = new Promise((resolve) => (onReceived = resolve));
@@ -74,11 +90,13 @@ describe('esArchiver createParseArchiveStreams', () => {
7490
createConcatStream([]),
7591
] as [Readable, ...Writable[]]);
7692

77-
input.write(Buffer.from('{"a": 1}\n\n{"a":'));
93+
// before emitting a result, the buffer waits until it at least receives toReplace.length bytes
94+
// so we need a long second object to ensure that the first gets emitted.
95+
input.write(Buffer.from('{"a": 1}\n\n{"propertyNameLongerThanToReplace":'));
7896
expect(await receivedPromise).toEqual({ a: 1 });
7997
input.write(Buffer.from('2}'));
8098
input.end();
81-
expect(await finalPromise).toEqual([{ a: 1 }, { a: 2 }]);
99+
expect(await finalPromise).toEqual([{ a: 1 }, { propertyNameLongerThanToReplace: 2 }]);
82100
});
83101
});
84102

@@ -136,6 +154,18 @@ describe('esArchiver createParseArchiveStreams', () => {
136154

137155
expect(output).toEqual([{ a: 1 }, { a: 2 }]);
138156
});
157+
158+
it('replaces $KIBANA_PACKAGE_VERSION with the current kibana version', async () => {
159+
const output = await createPromiseFromStreams([
160+
createListStream([
161+
Buffer.from('{"$KIBANA_PACKAGE'),
162+
Buffer.from('_VERSION": "enabled"}'),
163+
]),
164+
createGzip(),
165+
...createParseArchiveStreams({ gzip: true }),
166+
]);
167+
return expect(output).toEqual({ [kibanaPackageJson.version]: 'enabled' });
168+
});
139169
});
140170

141171
it('parses blank files', async () => {

packages/kbn-es-archiver/src/lib/archives/parse.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
createSplitStream,
1414
createReplaceStream,
1515
createMapStream,
16+
kibanaPackageJson,
1617
} from '@kbn/utils';
1718

1819
import { RECORD_SEPARATOR } from './constants';
@@ -21,6 +22,7 @@ export function createParseArchiveStreams({ gzip = false } = {}) {
2122
return [
2223
gzip ? createGunzip() : new PassThrough(),
2324
createReplaceStream('\r\n', '\n'),
25+
createReplaceStream('$KIBANA_PACKAGE_VERSION', kibanaPackageJson.version),
2426
createSplitStream(RECORD_SEPARATOR),
2527
createFilterStream<string>((l) => !!l.match(/[^\s]/)),
2628
createMapStream<string>((json) => JSON.parse(json.trim())),

x-pack/test/functional/es_archives/saved_objects_management/spaces_integration/mappings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
"type": "index",
33
"value": {
44
"aliases": {
5-
".kibana_8.0.0": {},
5+
".kibana_$KIBANA_PACKAGE_VERSION": {},
66
".kibana": {}
77
},
8-
"index": ".kibana_8.0.0_001",
8+
"index": ".kibana_$KIBANA_PACKAGE_VERSION_001",
99
"mappings": {
1010
"dynamic": "false",
1111
"properties": {

x-pack/test/saved_object_api_integration/common/fixtures/es_archiver/saved_objects/spaces/mappings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
"type": "index",
33
"value": {
44
"aliases": {
5-
".kibana_8.0.0": {},
5+
".kibana_$KIBANA_PACKAGE_VERSION": {},
66
".kibana": {}
77
},
8-
"index": ".kibana_8.0.0_001",
8+
"index": ".kibana_$KIBANA_PACKAGE_VERSION_001",
99
"mappings": {
1010
"dynamic": "false",
1111
"properties": {

x-pack/test/spaces_api_integration/common/fixtures/es_archiver/saved_objects/spaces/mappings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
"type": "index",
33
"value": {
44
"aliases": {
5-
".kibana_8.0.0": {},
5+
".kibana_$KIBANA_PACKAGE_VERSION": {},
66
".kibana": {}
77
},
8-
"index": ".kibana_8.0.0_001",
8+
"index": ".kibana_$KIBANA_PACKAGE_VERSION_001",
99
"mappings": {
1010
"dynamic": "false"
1111
},

0 commit comments

Comments
 (0)