|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +import * as Fs from 'fs'; |
| 10 | +import * as Path from 'path'; |
| 11 | +import * as Os from 'os'; |
| 12 | +import * as Child from 'child_process'; |
| 13 | +import Del from 'del'; |
| 14 | +import * as Rx from 'rxjs'; |
| 15 | +import { filter, map, take, timeout } from 'rxjs/operators'; |
| 16 | + |
| 17 | +const tempDir = Path.join(Os.tmpdir(), 'kbn-config-test'); |
| 18 | + |
| 19 | +const kibanaPath = follow('../../../../scripts/kibana.js'); |
| 20 | + |
| 21 | +const TIMEOUT_MS = 20000; |
| 22 | + |
| 23 | +const envForTempDir = { |
| 24 | + env: { KBN_PATH_CONF: tempDir }, |
| 25 | +}; |
| 26 | + |
| 27 | +const TestFiles = { |
| 28 | + fileList: [] as string[], |
| 29 | + |
| 30 | + createEmptyConfigFiles(fileNames: string[], root: string = tempDir): string[] { |
| 31 | + const configFiles = []; |
| 32 | + for (const fileName of fileNames) { |
| 33 | + const filePath = Path.resolve(root, fileName); |
| 34 | + |
| 35 | + if (!Fs.existsSync(filePath)) { |
| 36 | + Fs.writeFileSync(filePath, 'dummy'); |
| 37 | + |
| 38 | + TestFiles.fileList.push(filePath); |
| 39 | + } |
| 40 | + |
| 41 | + configFiles.push(filePath); |
| 42 | + } |
| 43 | + |
| 44 | + return configFiles; |
| 45 | + }, |
| 46 | + cleanUpEmptyConfigFiles() { |
| 47 | + for (const filePath of TestFiles.fileList) { |
| 48 | + Del.sync(filePath); |
| 49 | + } |
| 50 | + TestFiles.fileList.length = 0; |
| 51 | + }, |
| 52 | +}; |
| 53 | + |
| 54 | +describe('Server configuration ordering', () => { |
| 55 | + let kibanaProcess: Child.ChildProcessWithoutNullStreams; |
| 56 | + |
| 57 | + beforeEach(() => { |
| 58 | + Fs.mkdirSync(tempDir, { recursive: true }); |
| 59 | + }); |
| 60 | + |
| 61 | + afterEach(async () => { |
| 62 | + if (kibanaProcess !== undefined) { |
| 63 | + const exitPromise = new Promise((resolve) => kibanaProcess?.once('exit', resolve)); |
| 64 | + kibanaProcess.kill('SIGKILL'); |
| 65 | + await exitPromise; |
| 66 | + } |
| 67 | + |
| 68 | + Del.sync(tempDir, { force: true }); |
| 69 | + TestFiles.cleanUpEmptyConfigFiles(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('loads default config set without any options', async function () { |
| 73 | + TestFiles.createEmptyConfigFiles(['kibana.yml']); |
| 74 | + |
| 75 | + kibanaProcess = Child.spawn(process.execPath, [kibanaPath, '--verbose'], envForTempDir); |
| 76 | + const configList = await extractConfigurationOrder(kibanaProcess); |
| 77 | + |
| 78 | + expect(configList).toEqual(['kibana.yml']); |
| 79 | + }); |
| 80 | + |
| 81 | + it('loads serverless configs when --serverless is set', async () => { |
| 82 | + TestFiles.createEmptyConfigFiles([ |
| 83 | + 'serverless.yml', |
| 84 | + 'serverless.oblt.yml', |
| 85 | + 'kibana.yml', |
| 86 | + 'serverless.recent.yml', |
| 87 | + ]); |
| 88 | + |
| 89 | + kibanaProcess = Child.spawn( |
| 90 | + process.execPath, |
| 91 | + [kibanaPath, '--verbose', '--serverless', 'oblt'], |
| 92 | + envForTempDir |
| 93 | + ); |
| 94 | + const configList = await extractConfigurationOrder(kibanaProcess); |
| 95 | + |
| 96 | + expect(configList).toEqual([ |
| 97 | + 'serverless.yml', |
| 98 | + 'serverless.oblt.yml', |
| 99 | + 'kibana.yml', |
| 100 | + 'serverless.recent.yml', |
| 101 | + ]); |
| 102 | + }); |
| 103 | + |
| 104 | + it('prefers --config options over default', async () => { |
| 105 | + const [configPath] = TestFiles.createEmptyConfigFiles([ |
| 106 | + 'potato.yml', |
| 107 | + 'serverless.yml', |
| 108 | + 'serverless.oblt.yml', |
| 109 | + 'kibana.yml', |
| 110 | + 'serverless.recent.yml', |
| 111 | + ]); |
| 112 | + |
| 113 | + kibanaProcess = Child.spawn( |
| 114 | + process.execPath, |
| 115 | + [kibanaPath, '--verbose', '--serverless', 'oblt', '--config', configPath], |
| 116 | + envForTempDir |
| 117 | + ); |
| 118 | + const configList = await extractConfigurationOrder(kibanaProcess); |
| 119 | + |
| 120 | + expect(configList).toEqual([ |
| 121 | + 'serverless.yml', |
| 122 | + 'serverless.oblt.yml', |
| 123 | + 'potato.yml', |
| 124 | + 'serverless.recent.yml', |
| 125 | + ]); |
| 126 | + }); |
| 127 | + |
| 128 | + it('defaults to "es" if --serverless and --dev are there', async () => { |
| 129 | + TestFiles.createEmptyConfigFiles([ |
| 130 | + 'serverless.yml', |
| 131 | + 'serverless.es.yml', |
| 132 | + 'kibana.yml', |
| 133 | + 'kibana.dev.yml', |
| 134 | + 'serverless.dev.yml', |
| 135 | + ]); |
| 136 | + |
| 137 | + kibanaProcess = Child.spawn( |
| 138 | + process.execPath, |
| 139 | + [kibanaPath, '--verbose', '--serverless', '--dev'], |
| 140 | + envForTempDir |
| 141 | + ); |
| 142 | + const configList = await extractConfigurationOrder(kibanaProcess); |
| 143 | + |
| 144 | + expect(configList).toEqual([ |
| 145 | + 'serverless.yml', |
| 146 | + 'serverless.es.yml', |
| 147 | + 'kibana.yml', |
| 148 | + 'serverless.recent.yml', |
| 149 | + 'kibana.dev.yml', |
| 150 | + 'serverless.dev.yml', |
| 151 | + ]); |
| 152 | + }); |
| 153 | + |
| 154 | + it('adds dev configs to the stack', async () => { |
| 155 | + TestFiles.createEmptyConfigFiles([ |
| 156 | + 'serverless.yml', |
| 157 | + 'serverless.security.yml', |
| 158 | + 'kibana.yml', |
| 159 | + 'kibana.dev.yml', |
| 160 | + 'serverless.dev.yml', |
| 161 | + ]); |
| 162 | + |
| 163 | + kibanaProcess = Child.spawn( |
| 164 | + process.execPath, |
| 165 | + [kibanaPath, '--verbose', '--serverless', 'security', '--dev'], |
| 166 | + envForTempDir |
| 167 | + ); |
| 168 | + |
| 169 | + const configList = await extractConfigurationOrder(kibanaProcess); |
| 170 | + |
| 171 | + expect(configList).toEqual([ |
| 172 | + 'serverless.yml', |
| 173 | + 'serverless.security.yml', |
| 174 | + 'kibana.yml', |
| 175 | + 'serverless.recent.yml', |
| 176 | + 'kibana.dev.yml', |
| 177 | + 'serverless.dev.yml', |
| 178 | + ]); |
| 179 | + }); |
| 180 | +}); |
| 181 | + |
| 182 | +async function extractConfigurationOrder( |
| 183 | + proc: Child.ChildProcessWithoutNullStreams |
| 184 | +): Promise<string[] | undefined> { |
| 185 | + const configMessage = await waitForMessage(proc, /[Cc]onfig.*order:/, TIMEOUT_MS); |
| 186 | + |
| 187 | + const configList = configMessage |
| 188 | + .match(/order: (.*)$/) |
| 189 | + ?.at(1) |
| 190 | + ?.split(', ') |
| 191 | + ?.map((path) => Path.basename(path)); |
| 192 | + |
| 193 | + return configList; |
| 194 | +} |
| 195 | + |
| 196 | +async function waitForMessage( |
| 197 | + proc: Child.ChildProcessWithoutNullStreams, |
| 198 | + expression: string | RegExp, |
| 199 | + timeoutMs: number |
| 200 | +): Promise<string> { |
| 201 | + const message$ = Rx.fromEvent(proc.stdout!, 'data').pipe( |
| 202 | + map((messages) => String(messages).split('\n').filter(Boolean)) |
| 203 | + ); |
| 204 | + |
| 205 | + const trackedExpression$ = message$.pipe( |
| 206 | + // We know the sighup handler will be registered before this message logged |
| 207 | + filter((messages: string[]) => messages.some((m) => m.match(expression))), |
| 208 | + take(1) |
| 209 | + ); |
| 210 | + |
| 211 | + const error$ = message$.pipe( |
| 212 | + filter((messages: string[]) => messages.some((line) => line.match(/fatal/i))), |
| 213 | + take(1), |
| 214 | + map((line) => new Error(line.join('\n'))) |
| 215 | + ); |
| 216 | + |
| 217 | + const value = await Rx.firstValueFrom( |
| 218 | + Rx.race(trackedExpression$, error$).pipe( |
| 219 | + timeout({ |
| 220 | + first: timeoutMs, |
| 221 | + with: () => |
| 222 | + Rx.throwError( |
| 223 | + () => new Error(`Config options didn't appear in logs for ${timeoutMs / 1000}s...`) |
| 224 | + ), |
| 225 | + }) |
| 226 | + ) |
| 227 | + ); |
| 228 | + |
| 229 | + if (value instanceof Error) { |
| 230 | + throw value; |
| 231 | + } |
| 232 | + |
| 233 | + if (Array.isArray(value)) { |
| 234 | + return value[0]; |
| 235 | + } else { |
| 236 | + return value; |
| 237 | + } |
| 238 | +} |
| 239 | + |
| 240 | +function follow(file: string) { |
| 241 | + return Path.relative(process.cwd(), Path.resolve(__dirname, file)); |
| 242 | +} |
0 commit comments