opsAgentId = (int) $agents->register('OPs', 'ops')['agent']['id']; $this->devAgentId = (int) $agents->register('Dev', 'entwickler')['agent']['id']; $this->repository = new HostConfigRepository($db); } public function testCreateStoresHost(): void { $host = $this->repository->create([ 'agent_id' => $this->opsAgentId, 'hostname' => 'Webserver', 'address' => '192.168.178.10', ]); self::assertSame('Webserver', $host['hostname']); self::assertSame('192.168.178.10', $host['address']); self::assertSame($this->opsAgentId, (int) $host['agent_id']); } public function testCreateRejectsDuplicateHostnameForSameAgent(): void { $this->repository->create(['agent_id' => $this->opsAgentId, 'hostname' => 'Web', 'address' => '10.0.0.1']); $this->expectException(InvalidArgumentException::class); $this->repository->create(['agent_id' => $this->opsAgentId, 'hostname' => 'Web', 'address' => '10.0.0.2']); } public function testCreateRejectsValueWithWhitespace(): void { $this->expectException(InvalidArgumentException::class); $this->repository->create([ 'agent_id' => $this->opsAgentId, 'hostname' => 'Mein Server', 'address' => '10.0.0.1', ]); } public function testCreateRejectsAgentWithoutOpsRole(): void { $this->expectException(InvalidArgumentException::class); $this->repository->create(['agent_id' => $this->devAgentId, 'hostname' => 'Web', 'address' => '10.0.0.1']); } public function testAllForAgentOnlyReturnsOwnHosts(): void { $db = Database::inMemory(); $agents = new AgentRepository($db); $first = (int) $agents->register('OPs A', 'ops')['agent']['id']; $second = (int) $agents->register('OPs B', 'ops')['agent']['id']; $repository = new HostConfigRepository($db); $repository->create(['agent_id' => $first, 'hostname' => 'A', 'address' => '10.0.0.1']); $repository->create(['agent_id' => $second, 'hostname' => 'B', 'address' => '10.0.0.2']); $hosts = $repository->allForAgent($first); self::assertCount(1, $hosts); self::assertSame('A', $hosts[0]['hostname']); } public function testRenderHostsConfMatchesCheckScriptFormat(): void { $this->repository->create(['agent_id' => $this->opsAgentId, 'hostname' => 'Web', 'address' => '10.0.0.1']); $this->repository->create(['agent_id' => $this->opsAgentId, 'hostname' => 'Mail', 'address' => '10.0.0.2']); $conf = $this->repository->renderHostsConf($this->opsAgentId); $lines = array_values(array_filter( explode("\n", $conf), fn (string $line) => $line !== '' && !str_starts_with($line, '#') )); self::assertSame(['Web 10.0.0.1', 'Mail 10.0.0.2'], $lines); } public function testParseHostsConfSkipsCommentsAndBlankLines(): void { $rows = HostConfigRepository::parseHostsConf("# Kommentar\n\nWeb 10.0.0.1\nMail\t10.0.0.2\nkaputt\n"); self::assertSame( [['hostname' => 'Web', 'address' => '10.0.0.1'], ['hostname' => 'Mail', 'address' => '10.0.0.2']], $rows ); } public function testImportForAgentOnlyFillsEmptyList(): void { $imported = $this->repository->importForAgent($this->opsAgentId, [ ['hostname' => 'Web', 'address' => '10.0.0.1'], ['hostname' => 'Mail', 'address' => '10.0.0.2'], ]); self::assertSame(2, $imported); $again = $this->repository->importForAgent($this->opsAgentId, [ ['hostname' => 'Alt', 'address' => '10.0.0.9'], ]); self::assertSame(0, $again); self::assertCount(2, $this->repository->allForAgent($this->opsAgentId)); } public function testImportForAgentDoesNotRefillAnEmptiedList(): void { $this->repository->importForAgent($this->opsAgentId, [['hostname' => 'Web', 'address' => '10.0.0.1']]); foreach ($this->repository->allForAgent($this->opsAgentId) as $host) { $this->repository->delete((int) $host['id']); } // Genau der Fall, der ohne Marker die im Frontend geloeschten Hosts zurueckholen // wuerde: der Ping-Rechner hat seine alte hosts.conf lokal noch. $imported = $this->repository->importForAgent($this->opsAgentId, [['hostname' => 'Web', 'address' => '10.0.0.1']]); self::assertSame(0, $imported); self::assertSame([], $this->repository->allForAgent($this->opsAgentId)); } public function testUpdateChangesHostnameAndAddress(): void { $host = $this->repository->create([ 'agent_id' => $this->opsAgentId, 'hostname' => 'Web', 'address' => '10.0.0.1', ]); $updated = $this->repository->update((int) $host['id'], ['hostname' => 'Web2', 'address' => '10.0.0.5']); self::assertSame('Web2', $updated['hostname']); self::assertSame('10.0.0.5', $updated['address']); } public function testDeleteRemovesHostAndReturnsIt(): void { $host = $this->repository->create([ 'agent_id' => $this->opsAgentId, 'hostname' => 'Web', 'address' => '10.0.0.1', ]); $deleted = $this->repository->delete((int) $host['id']); self::assertSame('Web', $deleted['hostname']); self::assertSame([], $this->repository->allForAgent($this->opsAgentId)); } }