From b2477d423489b3e9ed2e705ddff1df8c4dfbb9f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Fern=C3=A1ndez=20Haro?= Date: Thu, 27 Apr 2023 13:18:03 +0200 Subject: [PATCH 1/3] [Serverless] Unskip CLI flag test --- .../serve/integration_tests/serverless_config_flag.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cli/serve/integration_tests/serverless_config_flag.test.ts b/src/cli/serve/integration_tests/serverless_config_flag.test.ts index 6c67ba6261eb4..ebbf174a99169 100644 --- a/src/cli/serve/integration_tests/serverless_config_flag.test.ts +++ b/src/cli/serve/integration_tests/serverless_config_flag.test.ts @@ -37,7 +37,7 @@ describe('cli serverless project type', () => { ); // Skipping this one because on CI it fails to read the config file - it.skip.each(['es', 'oblt', 'security'])( + it.each(['es', 'oblt', 'security'])( 'writes the serverless project type %s in config/serverless.recent.yml', async (mode) => { // Making sure `--serverless` translates into the `serverless` config entry, and validates against the accepted values @@ -45,8 +45,8 @@ describe('cli serverless project type', () => { cwd: REPO_ROOT, }); - // Wait for 5 lines in the logs - await firstValueFrom(from(child.stdout).pipe(take(5))); + // Wait for some lines in the logs + await firstValueFrom(from(child.stdout).pipe(take(20))); expect( readFileSync(resolve(getConfigDirectory(), 'serverless.recent.yml'), 'utf-8') From bdb0f32cbd26941c9b4a8875129ad0647bd7f33f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Fern=C3=A1ndez=20Haro?= Date: Thu, 27 Apr 2023 14:35:14 +0200 Subject: [PATCH 2/3] Fix missing write option --- .../serverless_config_flag.test.ts | 58 +++++++++++-------- src/cli/serve/serve.js | 12 +--- 2 files changed, 36 insertions(+), 34 deletions(-) diff --git a/src/cli/serve/integration_tests/serverless_config_flag.test.ts b/src/cli/serve/integration_tests/serverless_config_flag.test.ts index ebbf174a99169..67b6766aec711 100644 --- a/src/cli/serve/integration_tests/serverless_config_flag.test.ts +++ b/src/cli/serve/integration_tests/serverless_config_flag.test.ts @@ -6,15 +6,38 @@ * Side Public License, v 1. */ -import { spawn, spawnSync } from 'child_process'; +import { spawn, spawnSync, ChildProcessWithoutNullStreams } from 'child_process'; +import type { Readable } from 'stream'; import { readFileSync } from 'fs'; import { resolve } from 'path'; -import { filter, firstValueFrom, from, take, concatMap } from 'rxjs'; +import { filter, firstValueFrom, from, concatMap } from 'rxjs'; import { REPO_ROOT } from '@kbn/repo-info'; import { getConfigDirectory } from '@kbn/utils'; describe('cli serverless project type', () => { + let child: ChildProcessWithoutNullStreams | undefined; + + afterEach(() => { + child?.kill('SIGKILL'); + child = undefined; + }); + + async function waitForMessage(stream: Readable, expectedMsg: string) { + let leftover = ''; + return await firstValueFrom( + from(stream).pipe( + concatMap((chunk: Buffer) => { + const data = leftover + chunk.toString('utf-8'); + const msgs = data.split('\n'); + leftover = msgs.pop() ?? ''; + return msgs; + }), + filter((msg) => msg.includes(expectedMsg) || msg.includes('FATAL')) + ) + ); + } + it( 'exits with statusCode 1 and logs an error when serverless project type is invalid', () => { @@ -41,47 +64,32 @@ describe('cli serverless project type', () => { 'writes the serverless project type %s in config/serverless.recent.yml', async (mode) => { // Making sure `--serverless` translates into the `serverless` config entry, and validates against the accepted values - const child = spawn(process.execPath, ['scripts/kibana', `--serverless=${mode}`], { + child = spawn(process.execPath, ['scripts/kibana', `--serverless=${mode}`], { cwd: REPO_ROOT, }); - // Wait for some lines in the logs - await firstValueFrom(from(child.stdout).pipe(take(20))); + // Wait until Kibana starts bootstrapping (at that point the file should be present) + const found = await waitForMessage(child.stdout, 'Kibana process configured with roles'); + expect(found).not.toContain('FATAL'); expect( readFileSync(resolve(getConfigDirectory(), 'serverless.recent.yml'), 'utf-8') ).toContain(`serverless: ${mode}\n`); - - child.kill('SIGKILL'); } ); it.each(['es', 'oblt', 'security'])( 'Kibana does not crash when running project type %s', async (mode) => { - const child = spawn(process.execPath, ['scripts/kibana', `--serverless=${mode}`], { + child = spawn(process.execPath, ['scripts/kibana', `--serverless=${mode}`], { cwd: REPO_ROOT, }); // Wait until Kibana starts listening to the port - let leftover = ''; - const found = await firstValueFrom( - from(child.stdout).pipe( - concatMap((chunk: Buffer) => { - const data = leftover + chunk.toString('utf-8'); - const msgs = data.split('\n'); - leftover = msgs.pop() ?? ''; - return msgs; - }), - filter( - (msg) => - msg.includes('http server running at http://localhost:5601') || msg.includes('FATAL') - ) - ) + const found = await waitForMessage( + child.stdout, + 'http server running at http://localhost:5601' ); - - child.kill('SIGKILL'); - expect(found).not.toContain('FATAL'); } ); diff --git a/src/cli/serve/serve.js b/src/cli/serve/serve.js index c1b9f04fd8d81..45c9e04c7e558 100644 --- a/src/cli/serve/serve.js +++ b/src/cli/serve/serve.js @@ -125,11 +125,9 @@ function maybeSetRecentConfig(file, projectType, isDevMode, configs, method) { } try { - if (projectType === true) { - if (!existsSync(path)) { - writeMode('es'); - } - } else { + if (!existsSync(path)) { + writeMode(projectType === true ? 'es' : projectType); + } else if (typeof projectType === 'string') { const data = readFileSync(path, 'utf-8'); const match = data.match(/serverless: (\w+)\n/); if (!match || match[1] !== projectType) { @@ -139,10 +137,6 @@ function maybeSetRecentConfig(file, projectType, isDevMode, configs, method) { configs[method](path); } catch (err) { - if (err.code === 'ENOENT') { - return; - } - throw err; } } From 98a451ea4f3bf642342d83a082a114b04090d21d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Fern=C3=A1ndez=20Haro?= Date: Thu, 27 Apr 2023 14:53:20 +0200 Subject: [PATCH 3/3] Remove unnecessary comment --- src/cli/serve/integration_tests/serverless_config_flag.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/cli/serve/integration_tests/serverless_config_flag.test.ts b/src/cli/serve/integration_tests/serverless_config_flag.test.ts index 67b6766aec711..d603eff2eaec4 100644 --- a/src/cli/serve/integration_tests/serverless_config_flag.test.ts +++ b/src/cli/serve/integration_tests/serverless_config_flag.test.ts @@ -59,7 +59,6 @@ describe('cli serverless project type', () => { 20 * 1000 ); - // Skipping this one because on CI it fails to read the config file it.each(['es', 'oblt', 'security'])( 'writes the serverless project type %s in config/serverless.recent.yml', async (mode) => {