|
| 1 | +import { expect, test } from 'bun:test'; |
| 2 | +import * as fs from 'node:fs'; |
| 3 | +import * as os from 'node:os'; |
| 4 | +import * as path from 'node:path'; |
| 5 | + |
| 6 | +const initializer = fs.readFileSync( |
| 7 | + path.resolve(__dirname, '../../docker/package-init.sh'), |
| 8 | + 'utf8' |
| 9 | +); |
| 10 | + |
| 11 | +for (const oldRuntimeExists of [false, true]) { |
| 12 | + test(`cached PVC refreshes Node and Bun launchers with ${ |
| 13 | + oldRuntimeExists ? 'retained' : 'removed' |
| 14 | + } old runtimes`, () => { |
| 15 | + const root = fs.mkdtempSync( |
| 16 | + path.join(os.tmpdir(), 'cached-package-init-') |
| 17 | + ); |
| 18 | + try { |
| 19 | + const packages = path.join(root, 'pkgs'); |
| 20 | + const workspace = path.join(root, 'workspace'); |
| 21 | + const manifest = path.join(root, 'javascript-packages.txt'); |
| 22 | + fs.mkdirSync(workspace); |
| 23 | + fs.writeFileSync(manifest, 'runtime-fixture@1.0.0\n'); |
| 24 | + const runtimes = [ |
| 25 | + { |
| 26 | + directory: 'node/24.22.1', |
| 27 | + binary: 'bin/node', |
| 28 | + args: ['hello world', '--version'], |
| 29 | + }, |
| 30 | + { |
| 31 | + directory: 'bun/1.4.2', |
| 32 | + binary: 'bun', |
| 33 | + args: ['run', 'hello world', '--version'], |
| 34 | + }, |
| 35 | + ]; |
| 36 | + for (const runtime of runtimes) { |
| 37 | + const destination = path.join(packages, runtime.directory); |
| 38 | + const fixture = path.join( |
| 39 | + destination, |
| 40 | + 'node_modules/runtime-fixture' |
| 41 | + ); |
| 42 | + fs.mkdirSync(fixture, { recursive: true }); |
| 43 | + fs.mkdirSync( |
| 44 | + path.dirname(path.join(destination, runtime.binary)), |
| 45 | + { recursive: true } |
| 46 | + ); |
| 47 | + fs.writeFileSync( |
| 48 | + path.join(fixture, 'package.json'), |
| 49 | + '{"version": "1.0.0"}' |
| 50 | + ); |
| 51 | + fs.writeFileSync( |
| 52 | + path.join(destination, '.package-installed'), |
| 53 | + 'installed' |
| 54 | + ); |
| 55 | + fs.writeFileSync( |
| 56 | + path.join(destination, '.env'), |
| 57 | + 'preserve runtime environment' |
| 58 | + ); |
| 59 | + fs.writeFileSync( |
| 60 | + path.join(destination, runtime.binary), |
| 61 | + '#!/bin/bash\nprintf "<%s>\\n" "$@"\n', |
| 62 | + { mode: 0o755 } |
| 63 | + ); |
| 64 | + fs.writeFileSync( |
| 65 | + path.join(destination, 'run'), |
| 66 | + '#!/bin/bash\nexit 99\n', |
| 67 | + { mode: 0o755 } |
| 68 | + ); |
| 69 | + } |
| 70 | + const python = path.join(packages, 'python/3.14.4'); |
| 71 | + for (const name of [ |
| 72 | + 'PIL', |
| 73 | + 'markitdown', |
| 74 | + 'chdb', |
| 75 | + 'statsmodels', |
| 76 | + 'rasterio', |
| 77 | + ]) { |
| 78 | + fs.mkdirSync( |
| 79 | + path.join(python, 'lib/python3.14/site-packages', name), |
| 80 | + { recursive: true } |
| 81 | + ); |
| 82 | + } |
| 83 | + fs.writeFileSync( |
| 84 | + path.join(python, 'lib/python3.14/.sandbox-pipe-runtime-v1'), |
| 85 | + 'installed' |
| 86 | + ); |
| 87 | + fs.writeFileSync( |
| 88 | + path.join(python, '.package-installed'), |
| 89 | + 'installed' |
| 90 | + ); |
| 91 | + fs.mkdirSync(path.join(packages, 'bash/5.2.0'), { |
| 92 | + recursive: true, |
| 93 | + }); |
| 94 | + fs.writeFileSync( |
| 95 | + path.join(packages, 'bash/5.2.0/.package-installed'), |
| 96 | + 'installed' |
| 97 | + ); |
| 98 | + fs.writeFileSync( |
| 99 | + path.join(packages, '.initialized'), |
| 100 | + 'original initialization' |
| 101 | + ); |
| 102 | + |
| 103 | + const forbidden = path.join(root, 'forbidden'); |
| 104 | + fs.mkdirSync(forbidden); |
| 105 | + for (const binary of [ |
| 106 | + 'curl', |
| 107 | + 'wget', |
| 108 | + 'make', |
| 109 | + 'npm', |
| 110 | + 'tar', |
| 111 | + 'unzip', |
| 112 | + ]) { |
| 113 | + fs.writeFileSync( |
| 114 | + path.join(forbidden, binary), |
| 115 | + '#!/bin/bash\necho "unexpected reinstall" >&2\nexit 98\n', |
| 116 | + { mode: 0o755 } |
| 117 | + ); |
| 118 | + } |
| 119 | + const script = path.join(root, 'package-init.sh'); |
| 120 | + fs.writeFileSync( |
| 121 | + script, |
| 122 | + initializer |
| 123 | + .split('/pkgs') |
| 124 | + .join(packages) |
| 125 | + .split('/mnt/data/node_modules') |
| 126 | + .join(path.join(workspace, 'node_modules')) |
| 127 | + ); |
| 128 | + const env = { |
| 129 | + ...process.env, |
| 130 | + PATH: `${forbidden}:${process.env.PATH}`, |
| 131 | + FORCE_REBUILD: 'false', |
| 132 | + PYTHON_VERSION: '3.14.4', |
| 133 | + NODE_VERSION: '24.22.1', |
| 134 | + BUN_VERSION: '1.4.2', |
| 135 | + BASH_PACKAGE_VERSION: '5.2.0', |
| 136 | + JS_PACKAGE_MANIFEST: manifest, |
| 137 | + }; |
| 138 | + for (let pass = 0; pass < 2; pass += 1) { |
| 139 | + const initialized = Bun.spawnSync({ |
| 140 | + cmd: ['bash', script], |
| 141 | + env, |
| 142 | + }); |
| 143 | + expect(initialized.stderr.toString()).toBe(''); |
| 144 | + expect(initialized.exitCode).toBe(0); |
| 145 | + expect(initialized.stdout.toString()).toContain( |
| 146 | + 'Packages already initialized' |
| 147 | + ); |
| 148 | + expect( |
| 149 | + fs.readFileSync(path.join(packages, '.initialized'), 'utf8') |
| 150 | + ).toBe('original initialization'); |
| 151 | + for (const runtime of runtimes) { |
| 152 | + const destination = path.join(packages, runtime.directory); |
| 153 | + const oldModules = path.join( |
| 154 | + packages, |
| 155 | + runtime.directory.split('/')[0], |
| 156 | + '0.0.1/node_modules' |
| 157 | + ); |
| 158 | + if (oldRuntimeExists) |
| 159 | + fs.mkdirSync(oldModules, { recursive: true }); |
| 160 | + const link = path.join(workspace, 'node_modules'); |
| 161 | + fs.rmSync(link, { force: true }); |
| 162 | + fs.symlinkSync(oldModules, link); |
| 163 | + const launched = Bun.spawnSync({ |
| 164 | + cmd: [ |
| 165 | + path.join(destination, 'run'), |
| 166 | + 'hello world', |
| 167 | + '--version', |
| 168 | + ], |
| 169 | + }); |
| 170 | + expect(launched.stderr.toString()).toBe(''); |
| 171 | + expect(launched.exitCode).toBe(0); |
| 172 | + expect(launched.stdout.toString()).toBe( |
| 173 | + runtime.args.map(arg => `<${arg}>\n`).join('') |
| 174 | + ); |
| 175 | + expect(fs.readlinkSync(link)).toBe( |
| 176 | + path.join(destination, 'node_modules') |
| 177 | + ); |
| 178 | + expect( |
| 179 | + fs.readFileSync( |
| 180 | + path.join(destination, '.package-installed'), |
| 181 | + 'utf8' |
| 182 | + ) |
| 183 | + ).toBe('installed'); |
| 184 | + expect( |
| 185 | + fs.readFileSync(path.join(destination, '.env'), 'utf8') |
| 186 | + ).toBe('preserve runtime environment'); |
| 187 | + expect( |
| 188 | + fs |
| 189 | + .readdirSync(destination) |
| 190 | + .some(name => name.startsWith('.run.')) |
| 191 | + ).toBe(false); |
| 192 | + } |
| 193 | + } |
| 194 | + } finally { |
| 195 | + fs.rmSync(root, { recursive: true, force: true }); |
| 196 | + } |
| 197 | + }); |
| 198 | +} |
0 commit comments