Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 04:45 UTC
| 1 | import { $ } from 'bun'; |
| 2 | |
| 3 | /** |
| 4 | * Generates a unique volume name for e2e tests. |
| 5 | * Format: e2e-{suiteName}-{nodeVersion}-{timestamp} |
| 6 | * |
| 7 | * @param suiteName - Test suite name |
| 8 | * @param nodeVersion - Node.js version being tested |
| 9 | * @returns Unique volume name |
| 10 | */ |
| 11 | export function generateVolumeName(suiteName: string, nodeVersion: string): string { |
| 12 | const sanitizedSuite = suiteName.replace(/[^a-zA-Z0-9-]/g, '-'); |
| 13 | const sanitizedVersion = nodeVersion.replace(/\./g, '-'); |
| 14 | return `e2e-${sanitizedSuite}-${sanitizedVersion}-${Date.now()}`; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Creates a Docker named volume for sharing state between container runs. |
| 19 | * |
| 20 | * @param name - Unique volume name |
| 21 | * @returns The volume name (for use in mounts) |
| 22 | * @throws If volume creation fails |
| 23 | */ |
| 24 | export async function createVolume(name: string): Promise<string> { |
| 25 | const result = await $`docker volume create ${name}`.nothrow().quiet(); |
| 26 | |
| 27 | if (result.exitCode !== 0) { |
| 28 | const err = new Error(`Failed to create Docker volume: ${name}`); |
| 29 | (err as Error & { cause: Error }).cause = new Error(result.stderr.toString() || result.stdout.toString()); |
| 30 | throw err; |
| 31 | } |
| 32 | |
| 33 | return name; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Removes a Docker named volume. |
| 38 | * Logs errors but doesn't throw (cleanup should be best-effort). |
| 39 | * |
| 40 | * @param name - Volume name to remove |
| 41 | */ |
| 42 | export async function removeVolume(name: string): Promise<void> { |
| 43 | const result = await $`docker volume rm ${name}`.nothrow().quiet(); |
| 44 | |
| 45 | if (result.exitCode !== 0) { |
| 46 | // Log but don't throw - cleanup should be best-effort |
| 47 | console.warn(`[e2e] Warning: Failed to remove Docker volume ${name}: ${result.stderr.toString()}`); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Initializes a Docker volume with correct ownership for the container user. |
| 53 | * This is necessary because Docker volumes are created with root ownership by default. |
| 54 | * |
| 55 | * @param volumeName - Name of the volume to initialize |
| 56 | * @param imageTag - Docker image to use for the initialization container |
| 57 | */ |
| 58 | export async function initializeVolumeOwnership( |
| 59 | volumeName: string, |
| 60 | imageTag: string |
| 61 | ): Promise<void> { |
| 62 | // Node images run as node:node |
| 63 | const user = 'node:node'; |
| 64 | |
| 65 | // Run a quick container as root to chown the volume contents |
| 66 | const result = await $`docker run --rm --user root -v ${volumeName}:/mnt/vol ${imageTag} chown -R ${user} /mnt/vol`.nothrow().quiet(); |
| 67 | |
| 68 | if (result.exitCode !== 0) { |
| 69 | const err = new Error(`Failed to initialize volume ownership for: ${volumeName}`); |
| 70 | (err as Error & { cause: Error }).cause = new Error(result.stderr.toString() || result.stdout.toString()); |
| 71 | throw err; |
| 72 | } |
| 73 | } |