From 940c4beec468ca195a7ee1d65a8036e36a06a480 Mon Sep 17 00:00:00 2001 From: Rudolf Meijering Date: Mon, 15 Mar 2021 17:17:41 +0100 Subject: [PATCH] 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 # Conflicts: # x-pack/test/functional/es_archives/saved_objects_management/spaces_integration/mappings.json # x-pack/test/saved_object_api_integration/common/fixtures/es_archiver/saved_objects/spaces/mappings.json # x-pack/test/spaces_api_integration/common/fixtures/es_archiver/saved_objects/spaces/mappings.json --- .../src/lib/archives/parse.test.ts | 36 +++++++++++++++++-- .../kbn-es-archiver/src/lib/archives/parse.ts | 2 ++ .../spaces_integration/mappings.json | 4 +-- .../saved_objects/spaces/mappings.json | 4 +-- .../saved_objects/spaces/mappings.json | 4 +-- 5 files changed, 41 insertions(+), 9 deletions(-) diff --git a/packages/kbn-es-archiver/src/lib/archives/parse.test.ts b/packages/kbn-es-archiver/src/lib/archives/parse.test.ts index f12a9a25caf0c..ae8fc7216ba27 100644 --- a/packages/kbn-es-archiver/src/lib/archives/parse.test.ts +++ b/packages/kbn-es-archiver/src/lib/archives/parse.test.ts @@ -9,7 +9,12 @@ import Stream, { PassThrough, Readable, Writable, Transform } from 'stream'; import { createGzip } from 'zlib'; -import { createConcatStream, createListStream, createPromiseFromStreams } from '@kbn/utils'; +import { + createConcatStream, + createListStream, + createPromiseFromStreams, + kibanaPackageJson, +} from '@kbn/utils'; import { createParseArchiveStreams } from './parse'; @@ -54,6 +59,17 @@ describe('esArchiver createParseArchiveStreams', () => { expect(output).toEqual([{ a: 1 }, 1]); }); + it('replaces $KIBANA_PACKAGE_VERSION with the current kibana version', async () => { + const output = await createPromiseFromStreams([ + createListStream([ + Buffer.from('{"$KIBANA'), + Buffer.from('_PACKAGE_VERSION": "enabled"}'), + ]), + ...createParseArchiveStreams({ gzip: false }), + ]); + return expect(output).toEqual({ [kibanaPackageJson.version]: 'enabled' }); + }); + it('provides each JSON object as soon as it is parsed', async () => { let onReceived: (resolved: any) => void; const receivedPromise = new Promise((resolve) => (onReceived = resolve)); @@ -74,11 +90,13 @@ describe('esArchiver createParseArchiveStreams', () => { createConcatStream([]), ] as [Readable, ...Writable[]]); - input.write(Buffer.from('{"a": 1}\n\n{"a":')); + // before emitting a result, the buffer waits until it at least receives toReplace.length bytes + // so we need a long second object to ensure that the first gets emitted. + input.write(Buffer.from('{"a": 1}\n\n{"propertyNameLongerThanToReplace":')); expect(await receivedPromise).toEqual({ a: 1 }); input.write(Buffer.from('2}')); input.end(); - expect(await finalPromise).toEqual([{ a: 1 }, { a: 2 }]); + expect(await finalPromise).toEqual([{ a: 1 }, { propertyNameLongerThanToReplace: 2 }]); }); }); @@ -136,6 +154,18 @@ describe('esArchiver createParseArchiveStreams', () => { expect(output).toEqual([{ a: 1 }, { a: 2 }]); }); + + it('replaces $KIBANA_PACKAGE_VERSION with the current kibana version', async () => { + const output = await createPromiseFromStreams([ + createListStream([ + Buffer.from('{"$KIBANA_PACKAGE'), + Buffer.from('_VERSION": "enabled"}'), + ]), + createGzip(), + ...createParseArchiveStreams({ gzip: true }), + ]); + return expect(output).toEqual({ [kibanaPackageJson.version]: 'enabled' }); + }); }); it('parses blank files', async () => { diff --git a/packages/kbn-es-archiver/src/lib/archives/parse.ts b/packages/kbn-es-archiver/src/lib/archives/parse.ts index e13f5667c6aee..a2657fbd661ad 100644 --- a/packages/kbn-es-archiver/src/lib/archives/parse.ts +++ b/packages/kbn-es-archiver/src/lib/archives/parse.ts @@ -13,6 +13,7 @@ import { createSplitStream, createReplaceStream, createMapStream, + kibanaPackageJson, } from '@kbn/utils'; import { RECORD_SEPARATOR } from './constants'; @@ -21,6 +22,7 @@ export function createParseArchiveStreams({ gzip = false } = {}) { return [ gzip ? createGunzip() : new PassThrough(), createReplaceStream('\r\n', '\n'), + createReplaceStream('$KIBANA_PACKAGE_VERSION', kibanaPackageJson.version), createSplitStream(RECORD_SEPARATOR), createFilterStream((l) => !!l.match(/[^\s]/)), createMapStream((json) => JSON.parse(json.trim())), diff --git a/x-pack/test/functional/es_archives/saved_objects_management/spaces_integration/mappings.json b/x-pack/test/functional/es_archives/saved_objects_management/spaces_integration/mappings.json index 84fa42f6c9f54..cfcd38c1fbb59 100644 --- a/x-pack/test/functional/es_archives/saved_objects_management/spaces_integration/mappings.json +++ b/x-pack/test/functional/es_archives/saved_objects_management/spaces_integration/mappings.json @@ -2,10 +2,10 @@ "type": "index", "value": { "aliases": { - ".kibana_7.13.0": {}, + ".kibana_$KIBANA_PACKAGE_VERSION": {}, ".kibana": {} }, - "index": ".kibana_7.13.0_001", + "index": ".kibana_$KIBANA_PACKAGE_VERSION_001", "mappings": { "doc": { "dynamic": "false", diff --git a/x-pack/test/saved_object_api_integration/common/fixtures/es_archiver/saved_objects/spaces/mappings.json b/x-pack/test/saved_object_api_integration/common/fixtures/es_archiver/saved_objects/spaces/mappings.json index ebac5090328da..56db744fbce36 100644 --- a/x-pack/test/saved_object_api_integration/common/fixtures/es_archiver/saved_objects/spaces/mappings.json +++ b/x-pack/test/saved_object_api_integration/common/fixtures/es_archiver/saved_objects/spaces/mappings.json @@ -2,10 +2,10 @@ "type": "index", "value": { "aliases": { - ".kibana_7.13.0": {}, + ".kibana_$KIBANA_PACKAGE_VERSION": {}, ".kibana": {} }, - "index": ".kibana_7.13.0_001", + "index": ".kibana_$KIBANA_PACKAGE_VERSION_001", "mappings": { "doc": { "dynamic": "false", diff --git a/x-pack/test/spaces_api_integration/common/fixtures/es_archiver/saved_objects/spaces/mappings.json b/x-pack/test/spaces_api_integration/common/fixtures/es_archiver/saved_objects/spaces/mappings.json index ebac5090328da..56db744fbce36 100644 --- a/x-pack/test/spaces_api_integration/common/fixtures/es_archiver/saved_objects/spaces/mappings.json +++ b/x-pack/test/spaces_api_integration/common/fixtures/es_archiver/saved_objects/spaces/mappings.json @@ -2,10 +2,10 @@ "type": "index", "value": { "aliases": { - ".kibana_7.13.0": {}, + ".kibana_$KIBANA_PACKAGE_VERSION": {}, ".kibana": {} }, - "index": ".kibana_7.13.0_001", + "index": ".kibana_$KIBANA_PACKAGE_VERSION_001", "mappings": { "doc": { "dynamic": "false",