Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: rename DefaultOs to Linux #488

Merged
merged 1 commit into from
Aug 6, 2023
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
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
35 changes: 18 additions & 17 deletions lib/Os.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,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 ? 'Dummy' : PHP_OS);
}

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

public function getHostname(): string {
Expand Down Expand Up @@ -101,16 +96,22 @@ 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(string $os): IOperatingSystem {
return match ($os) {
'Linux' => new Linux(),
'FreeBSD' => new FreeBSD(),
default => 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
Loading