-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathvitest.setup.e2e.ts
64 lines (53 loc) · 2.11 KB
/
vitest.setup.e2e.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// eslint-disable no-await-in-loop
import type { TestProject } from 'vitest/node'
import { $ } from 'zx'
declare module 'vitest' {
export interface ProvidedContext {
connectionString: string
credentialsUsername: string
credentialsPassword: string
credentialsEndpoint: string
}
}
let configured = false
/**
* Sets up the test project by providing necessary environment variables for YDB connection.
* If the YDB_CONNECTION_STRING environment variable is set, it uses the provided credentials.
* Otherwise, it starts a local YDB Docker container, waits for it to become healthy, and then provides
* the connection details.
*
* @param project - The test project to configure with YDB connection details.
*/
export async function setup(project: TestProject) {
if (process.env['YDB_CONNECTION_STRING']) {
project.provide('connectionString', process.env['YDB_CONNECTION_STRING'])
project.provide('credentialsUsername', process.env['YDB_STATIC_CREDENTIALS_USER']!)
project.provide('credentialsPassword', process.env['YDB_STATIC_CREDENTIALS_PASSWORD']!)
project.provide('credentialsEndpoint', process.env['YDB_STATIC_CREDENTIALS_ENDPOINT']!)
return
}
await $`docker run --rm --detach --publish 2135 --publish 2136 --publish 8765 --publish 9092 --hostname localhost --platform linux/amd64 --name ydb ydbplatform/local-ydb:25.1.1.3`
let signal = AbortSignal.timeout(30 * 1000)
while ((await $`docker inspect -f {{.State.Health.Status}} ydb`.text()).trim() !== 'healthy') {
signal.throwIfAborted()
await $`sleep 1`
}
let [ipv4, _ipv6] = await $`docker port ydb 2136/tcp`.lines()
project.provide('connectionString', `grpc://${ipv4}/local`)
project.provide('credentialsUsername', 'root')
project.provide('credentialsPassword', '1234')
project.provide('credentialsEndpoint', `grpc://${ipv4}`)
configured = true
}
/**
* Tears down the YDB Docker container if it has been configured.
*
* This function checks if the YDB environment has been configured.
* If configured, it removes the YDB Docker container forcefully.
*/
export async function teardown() {
if (!configured) {
return
}
await $`docker rm -f ydb`
}