Skip to content

Commit

Permalink
feat: rename DefaultOs to Linux
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
  • Loading branch information
kesselb committed Aug 4, 2023
1 parent adea3c6 commit 9b06183
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 30 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,26 @@ php occ serverinfo:update-storage-statistics -v --output=json_pretty
}
```

Show phpinfo
##### Restricted mode (>= Nextcloud 28)

To obtain information about your server, the serverinfo app reads files outside the application directory (e.g. /proc on Linux) or executes shell commands (e.g. df on Linux).

If you don't want that (for example, to avoid open_basedir warnings) enable the restricted mode.

Enable:

``php occ config:app:set --value=yes serverinfo restricted_mode``

Disable:

``php occ config:app:delete serverinfo restricted_mode``

##### Show phpinfo (>= Nextcloud 28)

Enable:

``php occ config:app:set --value=yes serverinfo phpinfo``

Disable:

``php occ config:app:delete serverinfo phpinfo``
68 changes: 68 additions & 0 deletions lib/OperatingSystems/Dummy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

/**
* @author Frank Karlitschek <frank@nextcloud.com>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OCA\ServerInfo\OperatingSystems;

use OCA\ServerInfo\Resources\Memory;

class Dummy implements IOperatingSystem {
public function supported(): bool {
return false;
}

public function getMemory(): Memory {
return new Memory();
}

public function getCpuName(): string {
return 'Unknown Processor';
}

public function getTime(): string {
return '';
}

public function getUptime(): int {
return -1;
}

public function getNetworkInfo(): array {
return [
'hostname' => \gethostname(),
'dns' => '',
'gateway' => '',
];
}

public function getNetworkInterfaces(): array {
return [];
}

public function getDiskInfo(): array {
return [];
}

public function getThermalZones(): array {
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
use OCA\ServerInfo\Resources\NetInterface;
use RuntimeException;

class DefaultOs implements IOperatingSystem {
class Linux implements IOperatingSystem {
private const AF_INET = 2;
private const AF_INET6 = 10;

Expand Down
38 changes: 22 additions & 16 deletions lib/Os.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,23 @@
namespace OCA\ServerInfo;

use OCA\ServerInfo\OperatingSystems\DefaultOs;
use OCA\ServerInfo\OperatingSystems\Dummy;
use OCA\ServerInfo\OperatingSystems\FreeBSD;
use OCA\ServerInfo\OperatingSystems\IOperatingSystem;
use OCA\ServerInfo\OperatingSystems\Linux;
use OCA\ServerInfo\Resources\Memory;
use OCP\IConfig;

class Os implements IOperatingSystem {
protected IOperatingSystem $backend;
private IOperatingSystem $backend;

/**
* Os constructor.
*/
public function __construct() {
if (PHP_OS === 'FreeBSD') {
$this->backend = new FreeBSD();
} else {
$this->backend = new DefaultOs();
}
public function __construct(IConfig $config) {
$restrictedMode = $config->getAppValue('serverinfo', 'restricted_mode', 'no') === 'yes';
$this->backend = $this->getBackend($restrictedMode);
}

public function supported(): bool {
$data = $this->backend->supported();
return $data;
return $this->backend->supported();
}

public function getHostname(): string {
Expand Down Expand Up @@ -101,16 +97,26 @@ public function getDiskData(): array {
}

public function getNetworkInfo(): array {
$data = $this->backend->getNetworkInfo();
return $data;
return $this->backend->getNetworkInfo();
}

public function getNetworkInterfaces(): array {
return $this->backend->getNetworkInterfaces();
}

public function getThermalZones(): array {
$data = $this->backend->getThermalZones();
return $data;
return $this->backend->getThermalZones();
}

private function getBackend(bool $restrictedMode): IOperatingSystem {
if (!$restrictedMode) {
if (PHP_OS === 'Linux') {
return new Linux();
}
if (PHP_OS === 'FreeBSD') {
return new FreeBSD();
}
}
return new Dummy();
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
73 changes: 73 additions & 0 deletions tests/lib/DummyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2020 Daniel Kesselberg <mail@danielkesselberg.de>
*
* @author Daniel Kesselberg <mail@danielkesselberg.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\ServerInfo\Tests;

use OCA\ServerInfo\OperatingSystems\Dummy;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

class DummyTest extends TestCase {
/** @var Dummy|MockObject */
protected $os;

protected function setUp(): void {
parent::setUp(); // TODO: Change the autogenerated stub

$this->os = new Dummy();
}

public function testGetMemory(): void {
$memory = $this->os->getMemory();

$this->assertEquals(-1, $memory->getMemTotal());
$this->assertEquals(-1, $memory->getMemFree());
$this->assertEquals(-1, $memory->getMemAvailable());
$this->assertEquals(-1, $memory->getSwapTotal());
$this->assertEquals(-1, $memory->getSwapFree());
}

public function testGetCpuName(): void {
$this->assertEquals('Unknown Processor', $this->os->getCpuName());
}

public function testGetUptime(): void {
$this->assertEquals(-1, $this->os->getUptime());
}


public function testGetDiskInfo(): void {
$this->assertEquals([], $this->os->getDiskInfo());
}

public function testSupported(): void {
$this->assertFalse($this->os->supported());
}

public function testGetNetworkInterfaces(): void {
$this->assertEquals([], $this->os->getNetworkInterfaces());
}
}
19 changes: 7 additions & 12 deletions tests/lib/DefaultOsTest.php → tests/lib/LinuxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,22 @@

namespace OCA\ServerInfo\Tests;

use OCA\ServerInfo\OperatingSystems\DefaultOs;
use OCA\ServerInfo\OperatingSystems\Linux;
use OCA\ServerInfo\Resources\Disk;
use OCA\ServerInfo\Resources\Memory;
use OCA\ServerInfo\Resources\NetInterface;
use PHPUnit\Framework\MockObject\MockObject;
use RuntimeException;
use Test\TestCase;

/**
* Class DefaultOsTest
*
* @package OCA\ServerInfo\Tests
*/
class DefaultOsTest extends TestCase {
/** @var DefaultOs&MockObject */
class LinuxTest extends TestCase {
/** @var Linux&MockObject */
protected $os;

protected function setUp(): void {
parent::setUp(); // TODO: Change the autogenerated stub

$this->os = $this->getMockBuilder(DefaultOs::class)
$this->os = $this->getMockBuilder(Linux::class)
->disableOriginalConstructor()
->disableOriginalClone()
->disableArgumentCloning()
Expand All @@ -58,7 +53,7 @@ protected function setUp(): void {
public function testGetMemory(): void {
$this->os->method('readContent')
->with('/proc/meminfo')
->willReturn(file_get_contents(__DIR__ . '/../data/meminfo'));
->willReturn(file_get_contents(__DIR__ . '/../data/linux_meminfo'));

$memory = $this->os->getMemory();

Expand Down Expand Up @@ -144,7 +139,7 @@ public function testGetCpuNameInvalidData(): void {
public function testGetUptime(): void {
$this->os->method('readContent')
->with('/proc/uptime')
->willReturn(file_get_contents(__DIR__ . '/../data/uptime'));
->willReturn(file_get_contents(__DIR__ . '/../data/linux_uptime'));

$this->assertEquals(13278, $this->os->getUptime());
}
Expand All @@ -160,7 +155,7 @@ public function testGetUptimeNoData(): void {
public function testGetDiskInfo(): void {
$this->os->method('executeCommand')
->with('df -TPk')
->willReturn(file_get_contents(__DIR__ . '/../data/df_tp'));
->willReturn(file_get_contents(__DIR__ . '/../data/linux_df_tp'));

$disk1 = new Disk();
$disk1->setDevice('/dev/mapper/homestead--vg-root');
Expand Down

0 comments on commit 9b06183

Please sign in to comment.