-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: spalger <spalger@users.noreply.github.com> Co-authored-by: spalger <spalger@users.noreply.github.com>
- Loading branch information
Showing
23 changed files
with
616 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
export * from './observe_lines'; | ||
export * from './observe_readable'; |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
packages/kbn-test/src/functional_test_runner/lib/docker_servers/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
# Functional Test Runner - Docker Servers | ||
|
||
In order to make it simpler to run some services while the functional tests are running, we've added the ability to execute docker containers while the tests execute for the purpose of exposing services to the tests. These containers are expected to expose an application via a single HTTP port and live for the life of the tests. If the application exits for any reason before the tests complete the tests will abort. | ||
|
||
To configure docker servers in your FTR config add the `dockerServers` key to your config like so: | ||
|
||
```ts | ||
// import this helper to get TypeScript support for this section of the config | ||
import { defineDockerServersConfig } from '@kbn/test'; | ||
|
||
export default function () { | ||
return { | ||
... | ||
|
||
dockerServers: defineDockerServersConfig({ | ||
// unique names are used in logging and to get the details of this server in the tests | ||
helloWorld: { | ||
/** disable this docker server unless the user sets some flag/env var */ | ||
enabled: !!process.env.HELLO_WORLD_PORT, | ||
/** the docker image to pull and run */ | ||
image: 'vad1mo/hello-world-rest', | ||
/** The port that this application will be accessible via locally */ | ||
port: process.env.HELLO_WORLD_PORT, | ||
/** The port that the container binds to in the container */ | ||
portInContainer: 5050, | ||
/** | ||
* OPTIONAL: string/regex to look for in the log, when specified the | ||
* tests won't start until a line containing this string, or matching | ||
* this expression is found. | ||
*/ | ||
waitForLogLine: /hello/, | ||
/** | ||
* OPTIONAL: function that is called when server is started, when defined | ||
* it is called to give the configuration an option to write custom delay | ||
* logic. The function is passed a DockerServer object, which is described | ||
* below, and an observable of the log lines produced by the application | ||
*/ | ||
async waitFor(server, logLine$) { | ||
await logLine$.pipe( | ||
filter(line => line.includes('...')), | ||
tap((line) => { | ||
console.log('marking server ready because this line was logged:', line); | ||
console.log('server accessible from url', server.url); | ||
}) | ||
).toPromise() | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
``` | ||
|
||
To consume the test server, use can use something like supertest to send request. Just make sure that you disable your test suite if the user doesn't choose to enable your docker server: | ||
|
||
```ts | ||
import makeSupertest from 'supertest-as-promised'; | ||
import { FtrProviderContext } from '../ftr_provider_context'; | ||
|
||
export default function ({ getService }: FtrProviderContext) { | ||
const dockerServers = getService('dockerServers'); | ||
const log = getService('log'); | ||
|
||
const server = dockerServers.get('helloWorld'); | ||
const supertest = makeSupertest(server.url); | ||
|
||
describe('test suite name', function () { | ||
if (!server.enabled) { | ||
log.warning( | ||
'disabling tests because server is not enabled, set HELLO_WORLD_PORT to run them' | ||
); | ||
this.pending = true; | ||
} | ||
|
||
it('test name', async () => { | ||
await supertest.get('/foo/bar').expect(200); | ||
}); | ||
}); | ||
} | ||
``` | ||
|
||
## `DockerServersService` | ||
|
||
The docker servers service is a core service that is always available in functional test runner tests. When you call `getService('dockerServers')` you will receive an instance of the `DockerServersService` class which has to methods: | ||
|
||
### `has(name: string): boolean` | ||
|
||
Determine if a name resolves to a known docker server. | ||
|
||
### `isEnabled(name: string): boolean` | ||
|
||
Determine if a named server is enabled. | ||
|
||
### `get(name: string): DockerServer` | ||
|
||
Get a `DockerServer` object for the server with the given name. | ||
|
||
|
||
## `DockerServer` | ||
|
||
The object passed to the `waitFor()` config function and returned by `DockerServersService#get()` | ||
|
||
```ts | ||
{ | ||
url: string; | ||
name: string; | ||
|
||
portInContainer: number; | ||
port: number; | ||
image: string; | ||
waitForLogLine?: RegExp | string; | ||
waitFor?: (server: DockerServer, logLine$: Rx.Observable<string>) => Promise<boolean>; | ||
} | ||
``` |
43 changes: 43 additions & 0 deletions
43
packages/kbn-test/src/functional_test_runner/lib/docker_servers/container_logs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import execa from 'execa'; | ||
import * as Rx from 'rxjs'; | ||
import { tap } from 'rxjs/operators'; | ||
import { ToolingLog, observeLines } from '@kbn/dev-utils'; | ||
|
||
/** | ||
* Observe the logs for a container, reflecting the log lines | ||
* to the ToolingLog and the returned Observable | ||
*/ | ||
export function observeContainerLogs(name: string, containerId: string, log: ToolingLog) { | ||
log.debug(`[docker:${name}] streaming logs from container [id=${containerId}]`); | ||
const logsProc = execa('docker', ['logs', '--follow', containerId], { | ||
stdio: ['ignore', 'pipe', 'pipe'], | ||
}); | ||
|
||
const logLine$ = new Rx.Subject<string>(); | ||
|
||
Rx.merge( | ||
observeLines(logsProc.stdout).pipe(tap((line) => log.info(`[docker:${name}] ${line}`))), | ||
observeLines(logsProc.stderr).pipe(tap((line) => log.error(`[docker:${name}] ${line}`))) | ||
).subscribe(logLine$); | ||
|
||
return logLine$.asObservable(); | ||
} |
65 changes: 65 additions & 0 deletions
65
packages/kbn-test/src/functional_test_runner/lib/docker_servers/container_running.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import execa from 'execa'; | ||
import * as Rx from 'rxjs'; | ||
import { ToolingLog } from '@kbn/dev-utils'; | ||
|
||
/** | ||
* Create an observable that errors if a docker | ||
* container exits before being unsubscribed | ||
*/ | ||
export function observeContainerRunning(name: string, containerId: string, log: ToolingLog) { | ||
return new Rx.Observable((subscriber) => { | ||
log.debug(`[docker:${name}] watching container for exit status [${containerId}]`); | ||
|
||
const exitCodeProc = execa('docker', ['wait', containerId]); | ||
|
||
let exitCode: Error | number | null = null; | ||
exitCodeProc | ||
.then(({ stdout }) => { | ||
exitCode = Number.parseInt(stdout.trim(), 10); | ||
|
||
if (Number.isFinite(exitCode)) { | ||
subscriber.error( | ||
new Error(`container [id=${containerId}] unexpectedly exitted with code ${exitCode}`) | ||
); | ||
} else { | ||
subscriber.error( | ||
new Error(`unable to parse exit code from output of "docker wait": ${stdout}`) | ||
); | ||
} | ||
}) | ||
.catch((error) => { | ||
if (error?.killed) { | ||
// ignore errors thrown because the process was killed | ||
subscriber.complete(); | ||
return; | ||
} | ||
|
||
subscriber.error( | ||
new Error(`failed to monitor process with "docker wait": ${error.message}`) | ||
); | ||
}); | ||
|
||
return () => { | ||
exitCodeProc.kill('SIGKILL'); | ||
}; | ||
}); | ||
} |
Oops, something went wrong.