|
1 | 1 | import * as os from 'node:os' |
2 | 2 | import * as path from 'node:path' |
3 | | -import { |
4 | | - GenericContainer, |
5 | | - type StartedTestContainer, |
6 | | - Wait, |
7 | | -} from 'testcontainers' |
8 | 3 | import * as Instance from '../Instance.js' |
9 | 4 | import { deepAssign, toArgs } from '../internal/utils.js' |
10 | 5 | import { execa } from '../processes/execa.js' |
@@ -210,113 +205,3 @@ export declare namespace tempo { |
210 | 205 | port?: number | undefined |
211 | 206 | } & Record<string, unknown> |
212 | 207 | } |
213 | | - |
214 | | -/** |
215 | | - * Defines a Tempo instance. |
216 | | - * |
217 | | - * @example |
218 | | - * ```ts |
219 | | - * const instance = Instance.tempoDocker({ port: 8545 }) |
220 | | - * await instance.start() |
221 | | - * // ... |
222 | | - * await instance.stop() |
223 | | - * ``` |
224 | | - */ |
225 | | -export const tempoDocker = Instance.define( |
226 | | - (parameters?: tempoDocker.Parameters) => { |
227 | | - const { |
228 | | - containerName = `tempo.${crypto.randomUUID()}`, |
229 | | - image = 'ghcr.io/tempoxyz/tempo:latest', |
230 | | - log: log_, |
231 | | - ...args |
232 | | - } = parameters || {} |
233 | | - |
234 | | - const log = (() => { |
235 | | - try { |
236 | | - return JSON.parse(log_ as string) |
237 | | - } catch { |
238 | | - return log_ |
239 | | - } |
240 | | - })() |
241 | | - const RUST_LOG = log && typeof log !== 'boolean' ? log : '' |
242 | | - |
243 | | - const name = 'tempo' |
244 | | - let container: StartedTestContainer | undefined |
245 | | - |
246 | | - return { |
247 | | - _internal: { |
248 | | - args, |
249 | | - }, |
250 | | - host: args.host ?? 'localhost', |
251 | | - name, |
252 | | - port: args.port ?? 8545, |
253 | | - async start({ port = args.port }, { emitter }) { |
254 | | - const promise = Promise.withResolvers<void>() |
255 | | - |
256 | | - const c = new GenericContainer(image) |
257 | | - .withPlatform('linux/x86_64') |
258 | | - .withNetworkMode('host') |
259 | | - .withExtraHosts([ |
260 | | - { host: 'host.docker.internal', ipAddress: 'host-gateway' }, |
261 | | - { host: 'localhost', ipAddress: 'host-gateway' }, |
262 | | - ]) |
263 | | - .withName(containerName) |
264 | | - .withEnvironment({ RUST_LOG }) |
265 | | - .withCommand(command({ ...args, port })) |
266 | | - .withWaitStrategy(Wait.forLogMessage(/RPC HTTP server started/)) |
267 | | - .withLogConsumer((stream) => { |
268 | | - stream.on('data', (data) => { |
269 | | - const message = data.toString() |
270 | | - emitter.emit('message', message) |
271 | | - emitter.emit('stdout', message) |
272 | | - if (log) console.log(message) |
273 | | - if (message.includes('shutting down')) |
274 | | - promise.reject(new Error(`Failed to start: ${message}`)) |
275 | | - }) |
276 | | - stream.on('error', (error) => { |
277 | | - if (log) console.error(error.message) |
278 | | - emitter.emit('message', error.message) |
279 | | - emitter.emit('stderr', error.message) |
280 | | - promise.reject(new Error(`Failed to start: ${error.message}`)) |
281 | | - }) |
282 | | - }) |
283 | | - .withStartupTimeout(10_000) |
284 | | - |
285 | | - c.start() |
286 | | - .then((c) => { |
287 | | - container = c |
288 | | - promise.resolve() |
289 | | - }) |
290 | | - .catch(promise.reject) |
291 | | - |
292 | | - return promise.promise |
293 | | - }, |
294 | | - async stop() { |
295 | | - if (!container) return |
296 | | - await container.stop() |
297 | | - container = undefined |
298 | | - }, |
299 | | - } |
300 | | - }, |
301 | | -) |
302 | | - |
303 | | -export declare namespace tempoDocker { |
304 | | - export type Parameters = Omit<tempo.Parameters, 'binary'> & { |
305 | | - /** |
306 | | - * Name of the container. |
307 | | - */ |
308 | | - containerName?: string | undefined |
309 | | - /** |
310 | | - * Docker image to use. |
311 | | - */ |
312 | | - image?: string | undefined |
313 | | - /** |
314 | | - * Host the server will listen on. |
315 | | - */ |
316 | | - host?: string | undefined |
317 | | - /** |
318 | | - * Port the server will listen on. |
319 | | - */ |
320 | | - port?: number | undefined |
321 | | - } |
322 | | -} |
0 commit comments