Files
MUA-Mikes-Unraid-Agent/scripts/test-admin-live.php

47 lines
3.8 KiB
PHP

<?php
/** Explicit opt-in integration test, only its own unique temporary objects. */
require_once __DIR__.'/unraid-admin-helper.php';
if(($argv[1]??'')!=='--authorized-temporary-test')throw new RuntimeException('Explicit opt-in required');
$name='MUA-r032-'.bin2hex(random_bytes(4));$container=$name.'-whoami';$image='traefik/whoami:latest';
$shareMade=false;$containerMade=false;$imageNew=false;$ok=false;
try {
// Baseline exact names and image only; no global container inventory.
$old=mua_run(['/usr/bin/docker','image','ls','--format','{{.ID}}',$image]);$imageNew=$old==='';
$spec=['action'=>'create','name'=>$name,'pool'=>'nvme-storage','description'=>'MUA r032 integration: Michael RW / Sarah RO','access'=>['michael'=>'read-write','sarah'=>'read-only']];
$plan=mua_plan('share',$spec);$shareMade=true;
$r=mua_apply_plan($plan);if(!$r['ok'])throw new RuntimeException('Share apply failed');
echo "PASS native share cfg and effective Samba RW/RO\n";
$x=['action'=>'create','name'=>$container,'image'=>$image,'ports'=>[['host_port'=>18767,'container_port'=>80]],'memory_mb'=>64,'cpus'=>0.25,'description'=>'MUA r032 isolated native renderer test'];
$plan=mua_plan('docker',$x);$containerMade=true;
$r=mua_apply_plan($plan);if(!$r['ok'])throw new RuntimeException('Docker apply failed');
$inspect=mua_json(['/usr/bin/docker','inspect',$container])[0];
if($inspect['HostConfig']['Privileged']||$inspect['Mounts']||$inspect['HostConfig']['NetworkMode']!=='bridge'||$inspect['HostConfig']['PortBindings']['80/tcp'][0]['HostIp']!=='127.0.0.1')throw new RuntimeException('Isolation mismatch');
echo "PASS native XML, dockerman label and exact isolation\n";
mua_run(['/usr/bin/docker','start',$container]);
$code=mua_run(['/usr/bin/curl','--retry','5','--retry-connrefused','--retry-delay','1','--max-time','5','-s','-o','/dev/null','-w','%{http_code}','http://127.0.0.1:18767/bench']);
if($code!=='200')throw new RuntimeException('HTTP test failed');
echo "PASS HTTP 200\n";
// A running container must not be removable through the typed operation.
try {mua_plan('docker',['action'=>'remove','name'=>$container,'remove_template'=>true]);throw new LogicException('Running removal allowed');}
catch(RuntimeException $e){if($e instanceof LogicException)throw $e;if(!str_contains($e->getMessage(),'Stop container'))throw $e;}
echo "PASS running-container removal refused\n";
$ok=true;
} finally {
if($containerMade){
$rows=mua_run(['/usr/bin/docker','ps','-a','--filter','name=^/'.$container.'$','--format','{{.ID}}']);
if($rows!==''){
$status=mua_run(['/usr/bin/docker','inspect','--format','{{.State.Status}}',$container]);
if($status==='running')mua_run(['/usr/bin/docker','stop',$container]);
mua_apply_plan(mua_plan('docker',['action'=>'remove','name'=>$container,'remove_template'=>true]));
}elseif(is_file(mua_template_path($container)))unlink(mua_template_path($container));
}
if($shareMade&&is_file(mua_cfg($name)))mua_apply_plan(mua_plan('share',['action'=>'remove','name'=>$name]));
if($imageNew && mua_run(['/usr/bin/docker','image','ls','--format','{{.ID}}',$image])!=='')mua_run(['/usr/bin/docker','image','rm',$image]);
if(is_file(mua_cfg($name))||file_exists('/mnt/user/'.$name)||file_exists('/mnt/nvme-storage/'.$name)||is_file(mua_template_path($container)))throw new RuntimeException('Own artifacts remain');
if(mua_run(['/usr/bin/docker','ps','-a','--filter','name=^/'.$container.'$','--format','{{.ID}}'])!=='')throw new RuntimeException('Container remains');
if(mua_run(['/usr/sbin/ss','-Hlnt','sport = :18767'])!=='')throw new RuntimeException('Listener remains');
echo "PASS cleanup own share/container/template and new unshared image\n";
}
if(!$ok)exit(1);
echo "LIVE PASS: $name (configuration tested, no authenticated SMB login)\n";