> "$WM_TEST_LOG" if [ "$1" = 'show' ]; then printf '%s\n' 'LoadState=loaded' "ActiveState=${WM_TEST_ACTIVE:-inactive}" 'SubState=dead' 'MainPID=0' "UnitFileState=${WM_TEST_ENABLED:-disabled}" 'NeedDaemonReload=no' exit 0 fi if [ "$1" = 'daemon-reload' ] && [ "$WM_FAIL_RELOAD" = '1' ]; then echo 'Simulated failure' >&2 exit 1 fi exit 0 SH); chmod($mock, 0755); putenv("WM_CONFIG_DIR={$work}/projects"); putenv("WM_UNIT_DIR={$work}/units"); putenv("WM_SYSTEMCTL={$mock}"); putenv("WM_TEST_LOG={$callLog}"); putenv('NO_COLOR=1'); $php = PHP_BINARY; $wm = dirname(__DIR__) . '/bin/wm'; $assertions = 0; function expect(bool $condition, string $reason): void { global $assertions; $assertions++; if (!$condition) { throw new RuntimeException("Assertion failed: {$reason}"); } } function cli(array $args): array { global $php, $wm; return Process::run([$php, $wm, ...$args]); } function fixture(string $name, string $type): string { global $work; $dir = "{$work}/apps/{$name}"; mkdir($dir, 0755, true); file_put_contents("{$dir}/start.php", " [$type === 'webman' ? 'workerman/webman' : 'workerman/workerman' => '^2.0']])); return $dir; } try { $auth = fixture('auth', 'webman'); $chat = fixture('chat', 'workerman'); mkdir($auth . '/runtime', 0755); $res = cli(['help']); expect($res['code'] === 0 && str_contains($res['stdout'], 'add '), 'help'); $res = cli(['add', 'auth', $auth, '--user', 'nobody', '--dry-run', '--json']); expect($res['code'] === 0, 'webman dry run'); $preview = json_decode($res['stdout'], true); expect(str_contains($preview['unit'], 'Type=forking'), 'webman forking'); expect(str_contains($preview['unit'], 'PIDFile=' . $auth . '/runtime/webman.pid'), 'webman PID path'); expect(!is_file("{$work}/projects/auth.json"), 'dry run no writes'); $res = cli(['add', 'auth', $auth, '--user', 'nobody', '--enable', '--now', '--json']); expect($res['code'] === 0, 'add webman enabled and started: ' . $res['stderr'] . $res['stdout']); expect(is_file("{$work}/projects/auth.json"), 'registry written'); expect(is_file("{$work}/units/wm-auth.service"), 'unit written'); expect(str_contains(file_get_contents($callLog), 'daemon-reload'), 'reload invoked'); expect(str_contains(file_get_contents($callLog), 'enable wm-auth.service'), 'enable invoked'); expect(str_contains(file_get_contents($callLog), 'start wm-auth.service'), 'start invoked'); $res = cli(['add', 'chat', $chat, '--user', 'nobody', '--json']); expect($res['code'] === 0, 'add workerman'); $unit = file_get_contents("{$work}/units/wm-chat.service"); expect(str_contains($unit, 'Type=exec'), 'workerman foreground'); expect(!str_contains($unit, 'PIDFile='), 'workerman no explicit pid'); expect(str_contains($unit, 'ExecReload=/bin/kill -USR1 $MAINPID'), 'signal reload'); $res = cli(['add', 'again', $chat, '--user', 'nobody', '--mode', 'daemon', '--json']); expect($res['code'] !== 0 && str_contains($res['stdout'], '--pid-file'), 'workerman daemon requires pid'); $res = cli(['ls', '--json']); $listing = json_decode($res['stdout'], true); expect($listing['total'] === 2, 'list count'); expect($listing['projects'][0]['name'] === 'auth', 'list sorted'); // Color is a view concern: active=green, enabled=cyan, root=yellow; JSON stays unchanged. putenv('WM_TEST_ACTIVE=active'); putenv('WM_TEST_ENABLED=enabled'); $res = cli(['ls', '--color=always']); expect($res['code'] === 0, 'colored list works'); expect(str_contains($res['stdout'], "\033[1;32m") && str_contains($res['stdout'], '● active'), 'active appears green'); expect(str_contains($res['stdout'], "\033[1;36m") && str_contains($res['stdout'], '◆ enabled'), 'enabled appears cyan'); expect(str_contains($res['stdout'], '2 projects') && str_contains($res['stdout'], '2 active') && str_contains($res['stdout'], '2 enabled'), 'status summary counts'); $res = cli(['status', 'auth', '--color=always']); expect(str_contains($res['stdout'], '● active') && str_contains($res['stdout'], '◆ enabled'), 'status detail shares color scheme'); $res = cli(['status', 'auth', '--no-ansi']); expect(str_contains($res['stdout'], 'ActiveEnterTimestamp '), 'long detail keys retain spacing'); $res = cli(['ls', '--color=always', '--no-ansi']); expect(!str_contains($res['stdout'], "\033[") && str_contains($res['stdout'], '● active'), '--no-ansi suppresses all colors'); $res = cli(['ls', '--color=always', '--json']); expect($res['code'] === 0 && !str_contains($res['stdout'], "\033[") && json_decode($res['stdout'], true)['projects'][0]['enabled'] === 'enabled', 'JSON always plain and structurally unchanged'); $res = cli(['ls', '--color=invalid']); expect($res['code'] !== 0, 'invalid color option rejected'); putenv('WM_TEST_ACTIVE=failed'); putenv('WM_TEST_ENABLED=disabled'); $res = cli(['ls', '--color=always']); expect(str_contains($res['stdout'], "\033[1;31m") && str_contains($res['stdout'], '● failed'), 'failed appears red'); expect(str_contains($res['stdout'], '◇ disabled') && str_contains($res['stdout'], "\033[90m"), 'disabled appears gray'); putenv('WM_TEST_ACTIVE'); putenv('WM_TEST_ENABLED'); putenv('COLUMNS=74'); $res = cli(['ls', '--no-ansi']); expect(!str_contains($res['stdout'], 'PATH'), 'narrow terminal omits path column'); putenv('COLUMNS=160'); $res = cli(['ls', '--no-ansi']); expect(str_contains($res['stdout'], 'PATH'), 'wide terminal includes path column'); putenv('COLUMNS'); $res = cli(['sync', '--json']); expect($res['code'] === 0 && json_decode($res['stdout'], true)['changed'] === [], 'sync idempotent'); $res = cli(['status', 'chat', '--json']); expect($res['code'] === 0 && json_decode($res['stdout'], true)['project']['mode'] === 'foreground', 'status'); $res = cli(['add', 'Auth; rm -rf /', $auth, '--json']); expect($res['code'] !== 0, 'reject malicious name'); $res = cli(['start', '--all', '--json']); expect($res['code'] === 0, 'start all'); $res = cli(['status', '--all', '--json']); expect($res['code'] === 0 && json_decode($res['stdout'], true)['total'] === 2, 'status --all'); $res = cli(['enable', 'auth', '--now', '--json']); expect($res['code'] === 0 && str_contains(file_get_contents($callLog), 'enable --now wm-auth.service'), 'enable --now'); $res = cli(['disable', 'chat', '--now', '--json']); expect($res['code'] === 0 && str_contains(file_get_contents($callLog), 'disable --now wm-chat.service'), 'disable --now'); $res = cli(['remove', 'chat', '--yes', '--json']); expect($res['code'] === 0, 'remove'); expect(!is_file("{$work}/projects/chat.json") && !is_file("{$work}/units/wm-chat.service"), 'remove unit/config'); expect(is_file("{$chat}/start.php"), 'remove preserves source'); // A failed systemctl daemon-reload must roll back a new registration. putenv('WM_FAIL_RELOAD=1'); $res = cli(['add', 'failcase', $chat, '--user', 'nobody', '--json']); expect($res['code'] !== 0, 'mock systemctl failure'); expect(!is_file("{$work}/projects/failcase.json"), 'registry rollback'); expect(!is_file("{$work}/units/wm-failcase.service"), 'unit rollback'); putenv('WM_FAIL_RELOAD'); // Detect a running PID outside systemd; refuse to start a duplicate. file_put_contents($auth . '/runtime/webman.pid', (string) getmypid()); $res = cli(['restart', 'auth', '--json']); expect($res['code'] !== 0 && str_contains($res['stdout'], 'unmanaged PID'), 'existing unmanaged daemon protected'); unlink($auth . '/runtime/webman.pid'); // Recursive import preview never changes registry. $res = cli(['import', "{$work}/apps", '--recursive', '--dry-run', '--user', 'nobody', '--json']); expect($res['code'] === 0, 'import preview'); $import = json_decode($res['stdout'], true); expect(count($import['found']) === 1 && $import['found'][0]['name'] === 'chat', 'import finds unregistered workerman'); $res = cli(['import', "{$work}/apps", '--recursive', '--yes', '--user', 'nobody', '--json']); expect($res['code'] === 0 && in_array('chat', json_decode($res['stdout'], true)['added']), 'import registers'); // Registry/sync should preserve a single source of truth and recover drift. file_put_contents("{$work}/units/wm-chat.service", Systemd::MARKER . "\n# drift\n"); $res = cli(['sync', '--dry-run', '--json']); expect($res['code'] === 0 && in_array('chat', json_decode($res['stdout'], true)['changed']), 'sync preview detects drift'); expect(str_contains(file_get_contents("{$work}/units/wm-chat.service"), "# drift"), 'sync preview does not write'); file_put_contents("{$work}/units/wm-chat.service", "# broken\n"); $res = cli(['sync', '--json']); expect($res['code'] !== 0, 'sync refuses unmanaged unit'); unlink("{$work}/units/wm-chat.service"); $res = cli(['sync', '--json']); expect($res['code'] === 0 && is_file("{$work}/units/wm-chat.service"), 'sync restores missing unit'); // Root is a regular, explicitly permitted service account. It is highlighted, never blocked. $dns = fixture('dns', 'workerman'); $res = cli(['add', 'dns', $dns, '--user', 'root', '--enable', '--now', '--json']); expect($res['code'] === 0, 'explicit root registration can enable and start without extra flags: ' . $res['stderr'] . $res['stdout']); $unit = file_get_contents("{$work}/units/wm-dns.service"); expect(str_contains($unit, 'User=root'), 'systemd unit permits User=root'); $res = cli(['ls', '--json']); $entries = json_decode($res['stdout'], true)['projects']; $rootRows = array_values(array_filter($entries, static fn (array $row): bool => $row['name'] === 'dns')); expect(count($rootRows) === 1 && $rootRows[0]['user'] === 'root', 'list JSON reports root user'); $res = cli(['ls', '--no-ansi']); expect(str_contains($res['stdout'], 'root !'), 'list marks root without disrupting operation'); $res = cli(['sync', 'dns', '--json']); expect($res['code'] === 0, 'sync accepts root account'); $res = cli(['add', 'authroot', $auth, '--user', 'root', '--dry-run', '--json']); expect($res['code'] === 0 && str_contains(json_decode($res['stdout'], true)['unit'], 'User=root'), 'webman also allows root account'); if (is_executable('/usr/bin/systemd-analyze')) { $res = Process::run(['/usr/bin/systemd-analyze', 'verify', "{$work}/units/wm-dns.service"]); expect($res['code'] === 0, 'systemd accepts User=root: ' . $res['stderr']); } $res = cli(['add', 'auto-user', $dns, '--dry-run', '--json']); expect($res['code'] === 0 && json_decode($res['stdout'], true)['project']['user'] === \WM\Runtime::currentUser(), 'unspecified service user defaults to current user'); $dnsImport = fixture('dnsimport', 'workerman'); $res = cli(['import', $dnsImport, '--user', 'root', '--yes', '--json']); expect($res['code'] === 0, 'import accepts root account'); $imported = json_decode(file_get_contents("{$work}/projects/dnsimport.json"), true); expect($imported['user'] === 'root', 'import preserves root account'); $res = cli(['add', 'chat2', $chat, '--user', 'nobody', '--mode', 'daemon', '--pid-file', $chat . '/custom.pid', '--json']); expect($res['code'] === 0 && str_contains(file_get_contents("{$work}/units/wm-chat2.service"), 'PIDFile=' . $chat . '/custom.pid'), 'explicit workerman daemon PID'); $res = cli(['add', 'escape', $auth, '--entry', '../outside.php', '--user', 'nobody', '--json']); expect($res['code'] !== 0, 'reject path traversal'); $special = fixture('special dir #1', 'webman'); $res = cli(['add', 'special', $special, '--user', 'nobody', '--json']); expect($res['code'] === 0, 'spaces in directory supported'); $unit = file_get_contents("{$work}/units/wm-special.service"); expect(str_contains($unit, 'WorkingDirectory=' . str_replace([' ', '#'], ['\\x20', '\\x23'], $special)), 'systemd path escapes'); if (is_executable('/usr/bin/systemd-analyze')) { $res = Process::run(['/usr/bin/systemd-analyze', 'verify', "{$work}/units/wm-special.service"]); expect($res['code'] === 0, 'systemd-analyze verify special: ' . $res['stderr']); } // The generated unit can be checked by systemd's own parser. if (is_executable('/usr/bin/systemd-analyze')) { $res = Process::run(['/usr/bin/systemd-analyze', 'verify', "{$work}/units/wm-auth.service"]); expect($res['code'] === 0, 'systemd-analyze verify: ' . $res['stderr']); } echo "PASS: {$assertions} assertions\n"; } finally { // Test runner never performs real systemctl operations. $delete = static function (string $dir) use (&$delete): void { foreach (scandir($dir) ?: [] as $file) { if ($file === '.' || $file === '..') { continue; } $path = $dir . '/' . $file; if (is_dir($path) && !is_link($path)) { $delete($path); } else { unlink($path); } } rmdir($dir); }; $delete($work); }