|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | +import { spawnSync } from 'child_process'; |
| 4 | + |
| 5 | +const dockerfilePath = path.resolve(__dirname, '../../containers/agent/Dockerfile'); |
| 6 | + |
| 7 | +const readDockerfile = (): string => fs.readFileSync(dockerfilePath, 'utf-8'); |
| 8 | + |
| 9 | +const readBrowserPackages = (): string[] => { |
| 10 | + const declaration = readDockerfile().match(/BROWSER_PKGS="([^"]+)"/)?.[1]; |
| 11 | + expect(declaration).toBeDefined(); |
| 12 | + return declaration!.split(/\s+/).filter(Boolean); |
| 13 | +}; |
| 14 | + |
| 15 | +/** |
| 16 | + * Extracts the shell loop that appends the browser packages to PKGS, resolving |
| 17 | + * Ubuntu 24.04 `t64` package renames, so it can be exercised with a stubbed |
| 18 | + * `apt-cache`. |
| 19 | + */ |
| 20 | +const readResolutionScript = (): string => { |
| 21 | + const block = readDockerfile().match(/for pkg in \$BROWSER_PKGS; do[\s\S]*?done && \\/)?.[0]; |
| 22 | + |
| 23 | + expect(block).toBeDefined(); |
| 24 | + return block! |
| 25 | + .replace(/done && \\\s*$/, 'done') |
| 26 | + .split('\n') |
| 27 | + .map((line) => line.trim().replace(/\s*\\$/, '')) |
| 28 | + .join('\n'); |
| 29 | +}; |
| 30 | + |
| 31 | +const runResolution = (t64Packages: string[]): string => { |
| 32 | + const script = ` |
| 33 | +set -eu |
| 34 | +apt-cache() { |
| 35 | + for available in ${t64Packages.map((pkg) => `"${pkg}"`).join(' ')}; do |
| 36 | + if [ "$2" = "$available" ]; then return 0; fi |
| 37 | + done |
| 38 | + return 100 |
| 39 | +} |
| 40 | +PKGS="iptables" |
| 41 | +BROWSER_PKGS="${readBrowserPackages().join(' ')}" |
| 42 | +${readResolutionScript()} |
| 43 | +echo "$PKGS" |
| 44 | +`; |
| 45 | + |
| 46 | + const result = spawnSync('bash', ['-c', script], { encoding: 'utf-8' }); |
| 47 | + expect(result.status).toBe(0); |
| 48 | + return result.stdout.trim(); |
| 49 | +}; |
| 50 | + |
| 51 | +describe('agent Dockerfile Chromium runtime dependencies', () => { |
| 52 | + it('preinstalls the shared libraries Playwright-managed Chromium needs', () => { |
| 53 | + const packages = readBrowserPackages(); |
| 54 | + |
| 55 | + for (const required of [ |
| 56 | + 'libasound2', |
| 57 | + 'libatk-bridge2.0-0', |
| 58 | + 'libatk1.0-0', |
| 59 | + 'libatspi2.0-0', |
| 60 | + 'libcups2', |
| 61 | + 'libdbus-1-3', |
| 62 | + 'libdrm2', |
| 63 | + 'libgbm1', |
| 64 | + 'libglib2.0-0', |
| 65 | + 'libnspr4', |
| 66 | + 'libnss3', |
| 67 | + 'libpango-1.0-0', |
| 68 | + 'libxcomposite1', |
| 69 | + 'libxdamage1', |
| 70 | + 'libxfixes3', |
| 71 | + 'libxkbcommon0', |
| 72 | + 'libxrandr2', |
| 73 | + 'fonts-liberation', |
| 74 | + ]) { |
| 75 | + expect(packages).toContain(required); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + it('declares the base (22.04) package names, never the t64 variants', () => { |
| 80 | + for (const pkg of readBrowserPackages()) { |
| 81 | + expect(pkg.endsWith('t64')).toBe(false); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + it('keeps the 22.04 names when the base image has no t64 packages', () => { |
| 86 | + const resolved = runResolution([]); |
| 87 | + |
| 88 | + expect(resolved.split(/\s+/)).toEqual(['iptables', ...readBrowserPackages()]); |
| 89 | + }); |
| 90 | + |
| 91 | + it('prefers t64 packages when the base image provides them', () => { |
| 92 | + const resolved = runResolution(['libasound2t64', 'libcups2t64']); |
| 93 | + |
| 94 | + expect(resolved).toContain('libasound2t64'); |
| 95 | + expect(resolved).toContain('libcups2t64'); |
| 96 | + expect(resolved.split(/\s+/)).not.toContain('libasound2'); |
| 97 | + expect(resolved.split(/\s+/)).not.toContain('libcups2'); |
| 98 | + expect(resolved).toContain('libnss3'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('installs the resolved browser packages with the other agent packages', () => { |
| 102 | + expect(readDockerfile()).toMatch(/apt_install_retry \$PKGS/); |
| 103 | + }); |
| 104 | +}); |
0 commit comments