59 lines
2.5 KiB
PHP
59 lines
2.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Domovoy\Tests\Services;
|
|
|
|
use Domovoy\Models\Credential;
|
|
use Domovoy\Models\Device;
|
|
use Domovoy\Services\HostScan\CommandWhitelist;
|
|
use Domovoy\Services\HostScan\LinuxHostScanner;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class LinuxHostScannerTest extends TestCase
|
|
{
|
|
public function testCollectsReadOnlyCommandsAndWritesRawJson(): void
|
|
{
|
|
$storagePath = sys_get_temp_dir() . '/domovoy-host-scan-test-' . uniqid();
|
|
mkdir($storagePath, 0777, true);
|
|
$runner = new FakeCommandRunner([
|
|
'hostnamectl --static' => ['exit_code' => 0, 'stdout' => "server-1\n", 'stderr' => ''],
|
|
'cat /etc/os-release' => ['exit_code' => 0, 'stdout' => "PRETTY_NAME=\"Debian GNU/Linux 12\"\n", 'stderr' => ''],
|
|
'uname -r' => ['exit_code' => 0, 'stdout' => "6.1.0\n", 'stderr' => ''],
|
|
'df -h --output=source,size,used,avail,pcent,target' => ['exit_code' => 0, 'stdout' => "Filesystem Size Used Avail Use% Mounted on\n/dev/sda1 20G 10G 10G 50% /\n", 'stderr' => ''],
|
|
'systemctl --failed --no-pager --plain' => ['exit_code' => 0, 'stdout' => "0 loaded units listed.\n", 'stderr' => ''],
|
|
'crontab -l' => ['exit_code' => 1, 'stdout' => '', 'stderr' => 'no crontab for root'],
|
|
]);
|
|
$scanner = new LinuxHostScanner($runner, new CommandWhitelist(), $storagePath);
|
|
|
|
$device = new Device();
|
|
$device->id = 5;
|
|
$device->primaryIp = '192.168.1.10';
|
|
$credential = new Credential();
|
|
$credential->id = 9;
|
|
|
|
$result = $scanner->scan($device, $credential);
|
|
|
|
self::assertSame('server-1', $result['summary']['hostname']);
|
|
self::assertSame('Debian GNU/Linux 12', $result['summary']['os']);
|
|
self::assertSame('6.1.0', $result['summary']['kernel']);
|
|
self::assertFileExists($result['raw_path']);
|
|
self::assertStringContainsString('hostnamectl', file_get_contents($result['raw_path']));
|
|
}
|
|
}
|
|
|
|
final class FakeCommandRunner
|
|
{
|
|
/** @param array<string, array{exit_code: int, stdout: string, stderr: string}> $responses */
|
|
public function __construct(private array $responses)
|
|
{
|
|
}
|
|
|
|
/** @return array{exit_code: int, stdout: string, stderr: string} */
|
|
public function run(Credential $credential, string $host, array $command): array
|
|
{
|
|
$key = implode(' ', $command);
|
|
return $this->responses[$key] ?? ['exit_code' => 127, 'stdout' => '', 'stderr' => 'missing fake response'];
|
|
}
|
|
}
|