Files
XC_VM/tests/Unit/LbArchiveGateTest.php
T
Claude 488c923972 fix(build): strip stale LB manifest paths and gate stale/routed entries
The LB strip lists still named files by their pre-PSR-4 www/ paths, so
those entries matched nothing and the files kept shipping under Public/:
Public/admin/api.php, Public/admin/proxy_api.php, Public/stream/auth.php
and Public/stream/probe.php reached every LB. auth.php and probe.php are
routed by the LB nginx but fatal there on the stripped Domain/User.
LB_DIRS still listed the removed resources/ and www/ trees, and
LB_DIRS_TO_REMOVE listed the non-existent Domain/Auth. The gate never
noticed, because it only checked the SENSITIVE prefixes.

Makefile:
- Drop the dead entries (resources, www, Domain/Auth, GeoLite2-City.mmdb,
  the nine www/* files).
- Strip the MAIN-only files that no LB route or LB code needs: the four
  endpoints above, PlayerV2, AdminAPIWrapper, ResellerAPIWrapper,
  ActiveCodeApiController, ResellerApiDispatcher, ResellerTableRenderer,
  Cli/migration_logic.php and DbMigrateCommand (on an LB it would run
  MAIN's schema migrations). The viewer-API controllers still ship
  because the LB nginx still routes them; they go later with the routes.
- lb_delete_files_list now always writes the LB deleted_files.txt as the
  LB-scoped git deletions plus every stripped code file (Cli, Core,
  Domain, Infrastructure, Public, Streaming; never bin/config/content).
  An update only deletes listed files, so without this the files newly
  stripped here would stay on LBs that are already installed. The
  retired resources/ and www/ trees stay in the git-deletion scope
  (LB_RETIRED_DIRS), so their old entries are still applied.

lb_configs/nginx.conf: stop routing /stream/auth, /stream/probe and
/probe.php. They now get nginx's 404 instead of a PHP fatal. MAIN nginx
is unchanged.

verify-lb-archive.sh now also fails when:
- an LB_* entry matches no tracked path (STALE);
- a script the LB nginx executes is stripped (MISSING): every
  SCRIPT_FILENAME, and Public/<scope>/<handler>.php for each handler the
  /stream/ and /admin/ gateway locations accept.
SENSITIVE covers the new strips, CacheHandlerCommand and
CacheEngineCronJob. The manifest lookups now use here-strings, because
under pipefail an early `grep -q` exit could SIGPIPE the writer and hide
a leak. LbArchiveGateTest runs the real script with each failure mode
injected through MAKEFLAGS.

Behaviour: the LB archive loses 26 files that no LB route reaches (or
that fatal there today), and the next LB update deletes them. It also
deletes any lingering Domain/User or Domain/Device files. Fresh LBs
never had those, so an older LB that still carries them loses rtmp and
viewer-API paths that only worked because of them, just like a fresh LB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TQHeypQpfAXTdCysmi7b6a
2026-09-25 01:14:19 +00:00

122 lines
4.6 KiB
PHP

<?php
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
/**
* tools/ci/verify-lb-archive.sh (make verify-lb-archive) guards the Makefile's
* LB lists. It must fail on an entry that matches no tracked path (a strip rule
* that became a no-op after a rename, as the old www/* entries did), on
* privileged code left in the manifest, and on a stripped file that
* lb_configs/nginx.conf still routes to.
*
* Each case overrides one Makefile list through MAKEFLAGS, as `make VAR=...`
* on the command line would, and runs the real script against the checkout.
*/
#[Group('skip-on-panel')]
final class LbArchiveGateTest extends TestCase {
private string $rRoot;
protected function setUp(): void {
// The gate reads the Makefile and `git ls-files`, so it only runs in a
// git checkout of the repo (not on a flat /home/xc_vm deployment).
$this->rRoot = dirname(__DIR__, 2);
if (!is_file($this->rRoot . '/Makefile') || !is_file($this->rRoot . '/tools/ci/verify-lb-archive.sh') || !file_exists($this->rRoot . '/.git')) {
$this->markTestSkipped('the LB archive gate runs only in a git checkout of the repo');
}
}
public function testCurrentListsPass(): void {
[$rCode, $rOutput] = $this->runGate([]);
$this->assertSame(0, $rCode, $rOutput);
$this->assertStringStartsWith('OK: ', $rOutput);
$this->assertDoesNotMatchRegularExpression('/^(STALE|LEAK|MISSING|FAIL):/m', $rOutput);
}
public function testStaleFileEntryFails(): void {
[$rCode, $rOutput] = $this->runGate(['LB_FILES_TO_REMOVE' => array_merge($this->makeList('LB_FILES_TO_REMOVE'), ['www/admin/api.php'])]);
$this->assertSame(1, $rCode, $rOutput);
$this->assertStringContainsString("STALE: 'www/admin/api.php'", $rOutput);
}
public function testStaleDirectoryEntryFails(): void {
[$rCode, $rOutput] = $this->runGate(['LB_DIRS' => array_merge($this->makeList('LB_DIRS'), ['www'])]);
$this->assertSame(1, $rCode, $rOutput);
$this->assertStringContainsString("STALE: 'www'", $rOutput);
}
public function testPrivilegedFileLeftInTheManifestFails(): void {
$rFiles = array_values(array_diff($this->makeList('LB_FILES_TO_REMOVE'), ['Public/stream/auth.php']));
[$rCode, $rOutput] = $this->runGate(['LB_FILES_TO_REMOVE' => $rFiles]);
$this->assertSame(1, $rCode, $rOutput);
$this->assertStringContainsString("LEAK: 'Public/stream/auth.php'", $rOutput);
}
/** @return array<string, array{string}> */
public static function routedFiles(): array {
return [
'SCRIPT_FILENAME' => ['Public/index.php'],
'stream handler' => ['Public/stream/live.php'],
'admin handler' => ['Public/admin/vod.php'],
];
}
#[DataProvider('routedFiles')]
public function testStrippingARoutedFileFails(string $rPath): void {
[$rCode, $rOutput] = $this->runGate(['LB_FILES_TO_REMOVE' => array_merge($this->makeList('LB_FILES_TO_REMOVE'), [$rPath])]);
$this->assertSame(1, $rCode, $rOutput);
$this->assertMatchesRegularExpression('#^MISSING: .*' . preg_quote($rPath, '#') . '#m', $rOutput);
}
/** @return string[] The current value of a Makefile list. */
private function makeList(string $rName): array {
[$rCode, $rOutput] = $this->runProcess(['make', '-s', 'print-' . $rName], []);
$this->assertSame(0, $rCode, $rOutput);
return preg_split('/\s+/', trim($rOutput), -1, PREG_SPLIT_NO_EMPTY);
}
/**
* @param array<string, string[]> $rOverrides Makefile lists to replace.
* @return array{int, string} Exit code and combined output.
*/
private function runGate(array $rOverrides): array {
$rFlags = [];
foreach ($rOverrides as $rName => $rValues) {
$this->assertSame([], preg_grep('#^[A-Za-z0-9_./-]+$#', $rValues, PREG_GREP_INVERT), 'a path MAKEFLAGS cannot carry');
// MAKEFLAGS separates words with spaces; a space inside a value is escaped.
$rFlags[] = $rName . '=' . implode('\\ ', $rValues);
}
return $this->runProcess(['bash', 'tools/ci/verify-lb-archive.sh'], $rFlags);
}
/**
* @param string[] $rCommand
* @param string[] $rFlags Variable overrides passed to every make call.
* @return array{int, string}
*/
private function runProcess(array $rCommand, array $rFlags): array {
$rEnv = getenv();
unset($rEnv['MAKEFLAGS'], $rEnv['MFLAGS'], $rEnv['MAKELEVEL'], $rEnv['MAKEOVERRIDES']);
if ($rFlags !== []) {
$rEnv['MAKEFLAGS'] = '-- ' . implode(' ', $rFlags);
}
$rProcess = proc_open($rCommand, [1 => ['pipe', 'w'], 2 => ['redirect', 1]], $rPipes, $this->rRoot, $rEnv);
if (!is_resource($rProcess)) {
$this->fail('could not start ' . implode(' ', $rCommand));
}
$rOutput = (string) stream_get_contents($rPipes[1]);
fclose($rPipes[1]);
return [proc_close($rProcess), $rOutput];
}
}