288 строки
8.5 KiB
PHP
288 строки
8.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
namespace OCA\ServerInfo\Tests;
|
|
|
|
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 LinuxTest extends TestCase {
|
|
/** @var Linux&MockObject */
|
|
protected $os;
|
|
|
|
protected function setUp(): void {
|
|
parent::setUp(); // TODO: Change the autogenerated stub
|
|
|
|
$this->os = $this->getMockBuilder(Linux::class)
|
|
->disableOriginalConstructor()
|
|
->disableOriginalClone()
|
|
->disableArgumentCloning()
|
|
->disallowMockingUnknownTypes()
|
|
->setMethods(['readContent', 'executeCommand', 'getNetInterfaces'])
|
|
->getMock();
|
|
}
|
|
|
|
public function testGetMemory(): void {
|
|
$this->os->method('readContent')
|
|
->with('/proc/meminfo')
|
|
->willReturn(file_get_contents(__DIR__ . '/../data/linux_meminfo'));
|
|
|
|
$memory = $this->os->getMemory();
|
|
|
|
$this->assertEquals(15947, $memory->getMemTotal());
|
|
$this->assertEquals(2386, $memory->getMemFree());
|
|
$this->assertEquals(7495, $memory->getMemAvailable());
|
|
$this->assertEquals(975, $memory->getSwapTotal());
|
|
$this->assertEquals(896, $memory->getSwapFree());
|
|
}
|
|
|
|
public function testGetMemoryNoData(): void {
|
|
$this->os->method('readContent')
|
|
->with('/proc/meminfo')
|
|
->willThrowException(new RuntimeException('Unable to read: "/proc/meminfo"'));
|
|
|
|
$this->assertEquals(new Memory(), $this->os->getMemory());
|
|
}
|
|
|
|
public function testGetMemoryInvalidData(): void {
|
|
$this->os->method('readContent')
|
|
->with('/proc/meminfo')
|
|
->willReturn('invalid_data');
|
|
|
|
$this->assertEquals(new Memory(), $this->os->getMemory());
|
|
}
|
|
|
|
public function testGetCpuName(): void {
|
|
$this->os->method('readContent')
|
|
->with('/proc/cpuinfo')
|
|
->willReturn(file_get_contents(__DIR__ . '/../data/linux_cpuinfo'));
|
|
|
|
$this->assertEquals('Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz (4 threads)', $this->os->getCpuName());
|
|
}
|
|
|
|
public function testGetCpuNameOneCore(): void {
|
|
$this->os->method('readContent')
|
|
->with('/proc/cpuinfo')
|
|
->willReturn(file_get_contents(__DIR__ . '/../data/linux_cpuinfo_one_core'));
|
|
|
|
$this->assertEquals('Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz (1 thread)', $this->os->getCpuName());
|
|
}
|
|
|
|
public function testGetCpuNamePi3b(): void {
|
|
$this->os->method('readContent')
|
|
->with('/proc/cpuinfo')
|
|
->willReturn(file_get_contents(__DIR__ . '/../data/linux_cpuinfo_pi3b'));
|
|
|
|
$this->assertEquals('Raspberry Pi 3 Model B Rev 1.2 (4 threads)', $this->os->getCpuName());
|
|
}
|
|
|
|
public function testGetCpuNamePi4b(): void {
|
|
$this->os->method('readContent')
|
|
->with('/proc/cpuinfo')
|
|
->willReturn(file_get_contents(__DIR__ . '/../data/linux_cpuinfo_pi4b'));
|
|
|
|
$this->assertEquals('Raspberry Pi 4 Model B Rev 1.2 (4 threads)', $this->os->getCpuName());
|
|
}
|
|
|
|
public function testGetCpuNameOpenPower(): void {
|
|
$this->os->method('readContent')
|
|
->with('/proc/cpuinfo')
|
|
->willReturn(file_get_contents(__DIR__ . '/../data/linux_cpuinfo_openpower'));
|
|
|
|
$this->assertEquals('POWER9, altivec supported (176 threads)', $this->os->getCpuName());
|
|
}
|
|
|
|
public function testGetCpuNameNoData(): void {
|
|
$this->os->method('readContent')
|
|
->with('/proc/cpuinfo')
|
|
->willThrowException(new RuntimeException('Unable to read: "/proc/cpuinfo"'));
|
|
|
|
$this->assertEquals('Unknown Processor', $this->os->getCpuName());
|
|
}
|
|
|
|
public function testGetCpuNameInvalidData(): void {
|
|
$this->os->method('readContent')
|
|
->with('/proc/cpuinfo')
|
|
->willReturn('invalid_data');
|
|
|
|
$this->assertEquals('Unknown Processor', $this->os->getCpuName());
|
|
}
|
|
|
|
public function testGetUptime(): void {
|
|
$this->os->method('readContent')
|
|
->with('/proc/uptime')
|
|
->willReturn(file_get_contents(__DIR__ . '/../data/linux_uptime'));
|
|
|
|
$this->assertEquals(13278, $this->os->getUptime());
|
|
}
|
|
|
|
public function testGetUptimeNoData(): void {
|
|
$this->os->method('readContent')
|
|
->with('/proc/uptime')
|
|
->willThrowException(new RuntimeException('Unable to read: "/proc/uptime"'));
|
|
|
|
$this->assertEquals(-1, $this->os->getUptime());
|
|
}
|
|
|
|
public function testGetDiskInfo(): void {
|
|
$this->os->method('executeCommand')
|
|
->with('df -TPk')
|
|
->willReturn(file_get_contents(__DIR__ . '/../data/linux_df_tp'));
|
|
|
|
$disk1 = new Disk();
|
|
$disk1->setDevice('/dev/mapper/homestead--vg-root');
|
|
$disk1->setFs('ext4');
|
|
// $size1 = 56422560;
|
|
// $available1 = 47321220;
|
|
$used1 = 9101340; // $size1 - $available1
|
|
$disk1->setUsed(8889); // ceil(9101340 / 1024); (= 8888.0273)
|
|
$disk1->setAvailable(46212); // floor(47321220 / 1024); (= 46212.1289)
|
|
$disk1->setPercent('16.13%'); // round(($used1 * 100 / $size1), 2) . '%'; (= 16.1307%)
|
|
$disk1->setMount('/');
|
|
|
|
$disk2 = new Disk();
|
|
$disk2->setDevice('/dev/mapper/homestead--vg-mysql--master');
|
|
$disk2->setFs('ext4');
|
|
// $size2 = 65531436;
|
|
// $available2 = 61902400;
|
|
$used2 = 3629036;
|
|
$disk2->setUsed(3544);
|
|
$disk2->setAvailable(60451);
|
|
$disk2->setPercent('5.54%');
|
|
$disk2->setMount('/homestead-vg/master');
|
|
|
|
$disk3 = new Disk();
|
|
$disk3->setDevice('vagrant');
|
|
$disk3->setFs('vboxsf');
|
|
// $size3 = 958123168;
|
|
// $available3 = 343292036;
|
|
$used3 = 614831132;
|
|
$disk3->setUsed(600422);
|
|
$disk3->setAvailable(335246);
|
|
$disk3->setPercent('64.17%');
|
|
$disk3->setMount('/vagrant');
|
|
|
|
$disk4 = new Disk();
|
|
$disk4->setDevice('home_vagrant_code');
|
|
$disk4->setFs('vboxsf');
|
|
// $size4 = 958123168;
|
|
// $available4 = 343292036;
|
|
$used4 = 614831132;
|
|
$disk4->setUsed(600422);
|
|
$disk4->setAvailable(335246);
|
|
$disk4->setPercent('64.17%');
|
|
$disk4->setMount('/home/vagrant/code');
|
|
|
|
$disk5 = new Disk();
|
|
$disk5->setDevice('nfs.example.com:/export');
|
|
$disk5->setFs('nfs4');
|
|
// $size5 = 14820;
|
|
// $available5 = 1230
|
|
$used5 = 13590;
|
|
$disk5->setUsed(14);
|
|
$disk5->setAvailable(1);
|
|
$disk5->setPercent('91.7%');
|
|
$disk5->setMount('/nfs');
|
|
|
|
$disk6 = new Disk();
|
|
$disk6->setDevice('198.51.100.42:/storage');
|
|
$disk6->setFs('fuse.sshfs');
|
|
// $size6 = 47929956;
|
|
// $available6 = 45419052;
|
|
$used6 = 2510904;
|
|
$disk6->setUsed(2453);
|
|
$disk6->setAvailable(44354);
|
|
$disk6->setPercent('5.24%');
|
|
$disk6->setMount('/mnt/sshfs');
|
|
|
|
$disk7 = new Disk();
|
|
$disk7->setDevice('/dev/vda5');
|
|
$disk7->setFs('ext4');
|
|
// $size7 = 205314024;
|
|
// $available7 = 84722884;
|
|
$used7 = 120591140;
|
|
$disk7->setUsed(117765);
|
|
$disk7->setAvailable(82737);
|
|
$disk7->setPercent('58.73%');
|
|
$disk7->setMount('/nextcloud/my.cloud.domain.xx');
|
|
|
|
$this->assertEquals([$disk1, $disk2, $disk3, $disk4, $disk5, $disk6, $disk7], $this->os->getDiskInfo());
|
|
}
|
|
|
|
public function testGetDiskInfoNoCommandOutput(): void {
|
|
$this->os->method('executeCommand')
|
|
->with('df -TP')
|
|
->willThrowException(new RuntimeException('No output for command "df -TP"'));
|
|
|
|
$this->assertEquals([], $this->os->getDiskInfo());
|
|
}
|
|
|
|
public function testGetDiskInfoInvalidCommandOutput(): void {
|
|
$this->os->method('executeCommand')
|
|
->with('df -TP')
|
|
->willReturn('invalid_data');
|
|
|
|
$this->assertEquals([], $this->os->getDiskInfo());
|
|
}
|
|
|
|
public function testSupported(): void {
|
|
$this->assertTrue($this->os->supported());
|
|
}
|
|
|
|
public function testGetNetworkInterfaces(): void {
|
|
$this->os->method('getNetInterfaces')
|
|
->willReturn(json_decode(file_get_contents(__DIR__ . '/../data/linux_net_get_interfaces.json'), true, 512, JSON_THROW_ON_ERROR));
|
|
$this->os->method('readContent')
|
|
->willReturnCallback(static function ($filename) {
|
|
if ($filename === '/sys/class/net/eth0/address') {
|
|
return '02:42:ac:13:00:0a';
|
|
}
|
|
if ($filename === '/sys/class/net/eth0/speed') {
|
|
return '10000';
|
|
}
|
|
if ($filename === '/sys/class/net/eth0/duplex') {
|
|
return 'full';
|
|
}
|
|
throw new RuntimeException();
|
|
});
|
|
|
|
|
|
$net1 = new NetInterface('lo', true);
|
|
$net1->addIPv4('127.0.0.1');
|
|
$net1->addIPv6('::1');
|
|
|
|
$net2 = new NetInterface('eth0', true);
|
|
$net2->addIPv4('10.0.2.15');
|
|
$net2->addIPv6('fe80::a00:27ff:fe91:f84b');
|
|
$net2->setMAC('02:42:ac:13:00:0a');
|
|
$net2->setSpeed('10 Gbps');
|
|
$net2->setDuplex('full');
|
|
|
|
$expected = [$net1, $net2];
|
|
$actual = $this->os->getNetworkInterfaces();
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
}
|
|
|
|
public function testGetNetworkInterfacesError(): void {
|
|
$this->os->method('getNetInterfaces')
|
|
->willThrowException(new RuntimeException('Unable to get network interfaces'));
|
|
|
|
$expected = [];
|
|
$actual = $this->os->getNetworkInterfaces();
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
}
|
|
}
|