Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/serverinfo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoeland Jago Douma <rullzer@users.noreply.github.com>2020-04-30 09:55:01 +0300
committerGitHub <noreply@github.com>2020-04-30 09:55:01 +0300
commit9dfa5ddb2aabb1772b37ced95f8ca991e20f6c7f (patch)
tree92255ce9f6c9973856ad0ad40cf28533c16389dc
parent36a38f725870c4279b0c8e41e38559d48e1d9388 (diff)
parentdd50612bfbffa51e092f9bcb70bcd114240c25df (diff)
Merge pull request #202 from nextcloud/enh/more-tests-memory-cpunamev19.0.0beta7v19.0.0beta6v19.0.0RC1
More tests for getMemory, getCPUName and getUptime
-rw-r--r--lib/OperatingSystems/DefaultOs.php62
-rw-r--r--tests/data/cpuinfo_one_core26
-rw-r--r--tests/lib/DefaultOsTest.php52
3 files changed, 101 insertions, 39 deletions
diff --git a/lib/OperatingSystems/DefaultOs.php b/lib/OperatingSystems/DefaultOs.php
index f0b5dd3..4ba800e 100644
--- a/lib/OperatingSystems/DefaultOs.php
+++ b/lib/OperatingSystems/DefaultOs.php
@@ -27,19 +27,10 @@ namespace OCA\ServerInfo\OperatingSystems;
*/
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;
}
@@ -52,28 +43,23 @@ class DefaultOs {
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;
@@ -93,18 +79,17 @@ class DefaultOs {
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;
}
@@ -136,17 +121,15 @@ class DefaultOs {
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;
}
/**
@@ -255,10 +238,11 @@ class DefaultOs {
}
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 {
diff --git a/tests/data/cpuinfo_one_core b/tests/data/cpuinfo_one_core
new file mode 100644
index 0000000..1be32f4
--- /dev/null
+++ b/tests/data/cpuinfo_one_core
@@ -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:
diff --git a/tests/lib/DefaultOsTest.php b/tests/lib/DefaultOsTest.php
index 32aa8e1..5415e3a 100644
--- a/tests/lib/DefaultOsTest.php
+++ b/tests/lib/DefaultOsTest.php
@@ -69,6 +69,22 @@ class DefaultOsTest extends TestCase {
$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')
@@ -77,6 +93,30 @@ class DefaultOsTest extends TestCase {
$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')
@@ -85,6 +125,14 @@ class DefaultOsTest extends TestCase {
$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')
@@ -143,4 +191,8 @@ class DefaultOsTest extends TestCase {
$this->assertSame([], $this->os->getDiskInfo());
}
+
+ public function testSupported(): void {
+ $this->assertTrue($this->os->supported());
+ }
}