Skip to content

Commit

Permalink
Add more tests for getMemory, getCPUName and getUptime
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
  • Loading branch information
kesselb committed Apr 18, 2020
1 parent efeed34 commit dd50612
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 39 deletions.
62 changes: 23 additions & 39 deletions lib/OperatingSystems/DefaultOs.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,10 @@
*/
class DefaultOs {

/** @var string */
protected $cpuinfo;

/** @var string */
protected $meminfo;

/** @var string */
protected $uptime;

/**
* @return bool
*/
public function supported() {
public function supported(): bool {
return true;
}

Expand All @@ -52,28 +43,23 @@ public function supported() {
public function getMemory(): array {
$data = ['MemTotal' => -1, 'MemFree' => -1, 'MemAvailable' => -1, 'SwapTotal' => -1, 'SwapFree' => -1];

if ($this->meminfo === null) {
$this->meminfo = $this->readContent('/proc/meminfo');
}

if ($this->meminfo === '') {
try {
$meminfo = $this->readContent('/proc/meminfo');
} catch (\RuntimeException $e) {
return $data;
}

$matches = [];
$pattern = '/(?<Key>(?:MemTotal|MemFree|MemAvailable|SwapTotal|SwapFree)+):\s+(?<Value>\d+)\s+(?<Unit>\w{2})/';

if (preg_match_all($pattern, $this->meminfo, $matches) === false) {
$result = preg_match_all($pattern, $meminfo, $matches);
if ($result === 0 || $result === false) {
return $data;
}

$keys = array_map('trim', $matches['Key']);
$values = array_map('trim', $matches['Value']);
$units = array_map('trim', $matches['Unit']);

foreach ($keys as $i => $key) {
$value = (int)$values[$i];
$unit = $units[$i];
foreach ($matches['Key'] as $i => $key) {
$value = (int)$matches['Value'][$i];
$unit = $matches['Unit'][$i];

if ($unit === 'kB') {
$value *= 1024;
Expand All @@ -93,18 +79,17 @@ public function getMemory(): array {
public function getCPUName(): string {
$data = 'Unknown Processor';

if ($this->cpuinfo === null) {
$this->cpuinfo = $this->readContent('/proc/cpuinfo');
}

if ($this->cpuinfo === '') {
try {
$cpuinfo = $this->readContent('/proc/cpuinfo');
} catch (\RuntimeException $e) {
return $data;
}

$matches = [];
$pattern = '/model name\s:\s(.+)/';

if (preg_match_all($pattern, $this->cpuinfo, $matches) === false) {
$result = preg_match_all($pattern, $cpuinfo, $matches);
if ($result === 0 || $result === false) {
return $data;
}

Expand Down Expand Up @@ -136,17 +121,15 @@ public function getTime() {
public function getUptime(): int {
$data = -1;

if ($this->uptime === null) {
$this->uptime = $this->readContent('/proc/uptime');
}

if ($this->uptime === '') {
try {
$uptime = $this->readContent('/proc/uptime');
} catch (\RuntimeException $e) {
return $data;
}

[$uptime,] = array_map('intval', explode(' ', $this->uptime));
[$uptimeInSeconds,] = array_map('intval', explode(' ', $uptime));

return $uptime;
return $uptimeInSeconds;
}

/**
Expand Down Expand Up @@ -255,10 +238,11 @@ public function getDiskInfo(): array {
}

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

protected function executeCommand(string $command): string {
Expand Down
26 changes: 26 additions & 0 deletions tests/data/cpuinfo_one_core
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 94
model name : Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz
stepping : 3
microcode : 0xd6
cpu MHz : 800.022
cache size : 6144 KB
physical id : 0
siblings : 4
core id : 0
cpu cores : 4
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 22
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp md_clear flush_l1d
bugs : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs taa itlb_multihit
bogomips : 6399.96
clflush size : 64
cache_alignment : 64
address sizes : 39 bits physical, 48 bits virtual
power management:
52 changes: 52 additions & 0 deletions tests/lib/DefaultOsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ public function testGetMemory(): void {
$this->assertEquals(917756 * 1024, $memory['SwapFree']);
}

public function testGetMemoryNoData(): void {
$this->os->method('readContent')
->with('/proc/meminfo')
->willThrowException(new \RuntimeException('Unable to read: "/proc/meminfo"'));

$this->assertSame(['MemTotal' => -1, 'MemFree' => -1, 'MemAvailable' => -1, 'SwapTotal' => -1, 'SwapFree' => -1], $this->os->getMemory());
}

public function testGetMemoryInvalidData(): void {
$this->os->method('readContent')
->with('/proc/meminfo')
->willReturn('invalid_data');

$this->assertSame(['MemTotal' => -1, 'MemFree' => -1, 'MemAvailable' => -1, 'SwapTotal' => -1, 'SwapFree' => -1], $this->os->getMemory());
}

public function testGetCPUName(): void {
$this->os->method('readContent')
->with('/proc/cpuinfo')
Expand All @@ -77,6 +93,30 @@ public function testGetCPUName(): void {
$this->assertEquals('Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz (4 cores)', $this->os->getCPUName());
}

public function testGetCPUNameOneCore(): void {
$this->os->method('readContent')
->with('/proc/cpuinfo')
->willReturn(file_get_contents(__DIR__ . '/../data/cpuinfo_one_core'));

$this->assertEquals('Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz (1 core)', $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')
Expand All @@ -85,6 +125,14 @@ public function testGetUptime(): void {
$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 -TP')
Expand Down Expand Up @@ -143,4 +191,8 @@ public function testGetDiskInfoInvalidCommandOutput(): void {

$this->assertSame([], $this->os->getDiskInfo());
}

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

0 comments on commit dd50612

Please sign in to comment.