* * @author Daniel Kesselberg * * @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 . * */ namespace OCA\ServerInfo\Tests; use OCA\ServerInfo\OperatingSystems\DefaultOs; use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; /** * Class DefaultOsTest * * @package OCA\ServerInfo\Tests */ class DefaultOsTest extends TestCase { /** @var DefaultOs|MockObject */ protected $os; protected function setUp(): void { parent::setUp(); // TODO: Change the autogenerated stub $this->os = $this->getMockBuilder(DefaultOs::class) ->disableOriginalConstructor() ->disableOriginalClone() ->disableArgumentCloning() ->disallowMockingUnknownTypes() ->setMethods(['readContent', 'executeCommand']) ->getMock(); } public function testGetMemory(): void { $this->os->method('readContent') ->with('/proc/meminfo') ->willReturn(file_get_contents(__DIR__ . '/../data/meminfo')); $memory = $this->os->getMemory(); $this->assertArrayHasKey('MemTotal', $memory); $this->assertArrayHasKey('MemFree', $memory); $this->assertArrayHasKey('MemAvailable', $memory); $this->assertArrayHasKey('SwapTotal', $memory); $this->assertArrayHasKey('SwapFree', $memory); $this->assertEquals(16330252 * 1024, $memory['MemTotal']); $this->assertEquals(2443908 * 1024, $memory['MemFree']); $this->assertEquals(7675276 * 1024, $memory['MemAvailable']); $this->assertEquals(999420 * 1024, $memory['SwapTotal']); $this->assertEquals(917756 * 1024, $memory['SwapFree']); } public function testGetCPUName(): void { $this->os->method('readContent') ->with('/proc/cpuinfo') ->willReturn(file_get_contents(__DIR__ . '/../data/cpuinfo')); $this->assertEquals('Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz (4 cores)', $this->os->getCPUName()); } public function testGetUptime(): void { $this->os->method('readContent') ->with('/proc/uptime') ->willReturn(file_get_contents(__DIR__ . '/../data/uptime')); $this->assertEquals(13278, $this->os->getUptime()); } public function testGetDiskInfo(): void { $this->os->method('executeCommand') ->with('df -TP') ->willReturn(file_get_contents(__DIR__ . '/../data/df_tp')); $disks = [ [ 'device' => '/dev/mapper/homestead--vg-root', 'fs' => 'ext4', 'used' => 6354399232, 'available' => 48456929280, 'percent' => '12%', 'mount' => '/', ], [ 'device' => '/dev/mapper/homestead--vg-mysql--master', 'fs' => 'ext4', 'used' => 263385088, 'available' => 63388057600, 'percent' => '1%', 'mount' => '/homestead-vg/master', ], [ 'device' => 'vagrant', 'fs' => 'vboxsf', 'used' => 629587079168, 'available' => 351531044864, 'percent' => '65%', 'mount' => '/vagrant', ], [ 'device' => 'home_vagrant_code', 'fs' => 'vboxsf', 'used' => 629587079168, 'available' => 351531044864, 'percent' => '65%', 'mount' => '/home/vagrant/code', ], ]; $this->assertSame($disks, $this->os->getDiskInfo()); } public function testGetDiskInfoNoCommandOutput(): void { $this->os->method('executeCommand') ->with('df -TP') ->willThrowException(new \RuntimeException('No output for command "df -TP"')); $this->assertSame([], $this->os->getDiskInfo()); } public function testGetDiskInfoInvalidCommandOutput(): void { $this->os->method('executeCommand') ->with('df -TP') ->willReturn('invalid_data'); $this->assertSame([], $this->os->getDiskInfo()); } }