Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/OperatingSystems/FreeBSD.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ public function getNetworkInfo(): array {
if (count($gw[0]) > 0) {
$result['gateway'] = implode(', ', array_map('trim', $gw[0]));
}
$resolvConf = $this->readContent('/etc/resolv.conf');
if (preg_match_all('/^\s*nameserver\s+(\S+)/m', $resolvConf, $matches)) {
$result['dns'] = implode(', ', array_unique($matches[1]));
}
} catch (RuntimeException) {
// okay
}
Expand Down Expand Up @@ -239,6 +243,17 @@ public function getThermalZones(): array {
return [];
}

/**
* @throws RuntimeException
*/
protected function readContent(string $filename): string {
$data = @file_get_contents($filename);
if ($data === false || $data === '') {
throw new RuntimeException('Unable to read: "' . $filename . '"');
}
return trim($data);
}

/**
* Execute a command with shell_exec.
*
Expand Down
16 changes: 15 additions & 1 deletion tests/lib/FreeBSDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected function setUp(): void {
->disableOriginalClone()
->disableArgumentCloning()
->disallowMockingUnknownTypes()
->onlyMethods(['executeCommand', 'getNetInterfaces'])
->onlyMethods(['readContent', 'executeCommand', 'getNetInterfaces'])
->getMock();
}

Expand Down Expand Up @@ -194,6 +194,9 @@ public function testGetNetworkInfo(): void {
$this->os->method('executeCommand')
->with('netstat -rn')
->willReturn("Routing tables\n\ndefault 192.0.2.1 UGS\n");
$this->os->method('readContent')
->with('/etc/resolv.conf')
->willThrowException(new RuntimeException('Unable to read: "/etc/resolv.conf"'));

$networkInfo = $this->os->getNetworkInfo();

Expand All @@ -202,6 +205,17 @@ public function testGetNetworkInfo(): void {
$this->assertArrayHasKey('hostname', $networkInfo);
}

public function testGetNetworkInfoListsTheNameservers(): void {
$this->os->method('executeCommand')
->with('netstat -rn')
->willReturn("Routing tables\n\ndefault 192.0.2.1 UGS\n");
$this->os->method('readContent')
->with('/etc/resolv.conf')
->willReturn("# Generated by resolvconf\nsearch example.org\nnameserver 192.0.2.53\n nameserver 2001:db8::53\nnameserver 192.0.2.53\n");

$this->assertSame('192.0.2.53, 2001:db8::53', $this->os->getNetworkInfo()['dns']);
}

public function testGetNetworkInterfacesError(): void {
$this->os->method('getNetInterfaces')
->willThrowException(new RuntimeException('Unable to get network interfaces'));
Expand Down
Loading