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

[Serverless] Unskip CLI flag test #155998

Merged
merged 3 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
60 changes: 34 additions & 26 deletions src/cli/serve/integration_tests/serverless_config_flag.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
() => {
Expand All @@ -37,51 +60,36 @@ 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
const child = spawn(process.execPath, ['scripts/kibana', `--serverless=${mode}`], {
child = spawn(process.execPath, ['scripts/kibana', `--serverless=${mode}`], {
cwd: REPO_ROOT,
});

// Wait for 5 lines in the logs
await firstValueFrom(from(child.stdout).pipe(take(5)));
// 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');
}
);
Expand Down
12 changes: 3 additions & 9 deletions src/cli/serve/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Comment on lines +128 to +130
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only created new files when --serverless was called without a project type. It should be fixed now 😅

const data = readFileSync(path, 'utf-8');
const match = data.match(/serverless: (\w+)\n/);
if (!match || match[1] !== projectType) {
Expand All @@ -139,10 +137,6 @@ function maybeSetRecentConfig(file, projectType, isDevMode, configs, method) {

configs[method](path);
} catch (err) {
if (err.code === 'ENOENT') {
return;
}

throw err;
}
}
Expand Down