@@ -13,47 +13,47 @@ |
||
| 13 | 13 | use Test\TestCase; |
| 14 | 14 | |
| 15 | 15 | abstract class ExporterTestCase extends TestCase { |
| 16 | - protected IMetricFamily $exporter; |
|
| 17 | - /** @var IMetric[] */ |
|
| 18 | - protected array $metrics; |
|
| 19 | - |
|
| 20 | - abstract protected function getExporter(): IMetricFamily; |
|
| 21 | - |
|
| 22 | - protected function setUp(): void { |
|
| 23 | - parent::setUp(); |
|
| 24 | - $this->exporter = $this->getExporter(); |
|
| 25 | - $this->metrics = iterator_to_array($this->exporter->metrics()); |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - public function testNotEmptyData(): void { |
|
| 29 | - $this->assertNotEmpty($this->exporter->name()); |
|
| 30 | - $this->assertNotEmpty($this->metrics); |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - public function testValidNames(): void { |
|
| 34 | - $labelNames = []; |
|
| 35 | - foreach ($this->metrics as $metric) { |
|
| 36 | - foreach ($metric->labels as $label => $value) { |
|
| 37 | - $labelNames[$label] = $label; |
|
| 38 | - } |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - if (empty($labelNames)) { |
|
| 42 | - $this->expectNotToPerformAssertions(); |
|
| 43 | - return; |
|
| 44 | - } |
|
| 45 | - foreach ($labelNames as $label) { |
|
| 46 | - $this->assertMatchesRegularExpression('/^[a-z_][a-z0-9_]*$/i', $label); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - protected function assertLabelsAre(array $expectedLabels): void { |
|
| 52 | - $foundLabels = []; |
|
| 53 | - foreach ($this->metrics as $metric) { |
|
| 54 | - $foundLabels[] = $metric->labels; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - $this->assertSame($foundLabels, $expectedLabels); |
|
| 58 | - } |
|
| 16 | + protected IMetricFamily $exporter; |
|
| 17 | + /** @var IMetric[] */ |
|
| 18 | + protected array $metrics; |
|
| 19 | + |
|
| 20 | + abstract protected function getExporter(): IMetricFamily; |
|
| 21 | + |
|
| 22 | + protected function setUp(): void { |
|
| 23 | + parent::setUp(); |
|
| 24 | + $this->exporter = $this->getExporter(); |
|
| 25 | + $this->metrics = iterator_to_array($this->exporter->metrics()); |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + public function testNotEmptyData(): void { |
|
| 29 | + $this->assertNotEmpty($this->exporter->name()); |
|
| 30 | + $this->assertNotEmpty($this->metrics); |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + public function testValidNames(): void { |
|
| 34 | + $labelNames = []; |
|
| 35 | + foreach ($this->metrics as $metric) { |
|
| 36 | + foreach ($metric->labels as $label => $value) { |
|
| 37 | + $labelNames[$label] = $label; |
|
| 38 | + } |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + if (empty($labelNames)) { |
|
| 42 | + $this->expectNotToPerformAssertions(); |
|
| 43 | + return; |
|
| 44 | + } |
|
| 45 | + foreach ($labelNames as $label) { |
|
| 46 | + $this->assertMatchesRegularExpression('/^[a-z_][a-z0-9_]*$/i', $label); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + protected function assertLabelsAre(array $expectedLabels): void { |
|
| 52 | + $foundLabels = []; |
|
| 53 | + foreach ($this->metrics as $metric) { |
|
| 54 | + $foundLabels[] = $metric->labels; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + $this->assertSame($foundLabels, $expectedLabels); |
|
| 58 | + } |
|
| 59 | 59 | } |
@@ -16,27 +16,27 @@ |
||
| 16 | 16 | use PHPUnit\Framework\MockObject\MockObject; |
| 17 | 17 | |
| 18 | 18 | class InstanceInfoTest extends ExporterTestCase { |
| 19 | - private SystemConfig&MockObject $systemConfig; |
|
| 20 | - private ServerVersion&MockObject $serverVersion; |
|
| 21 | - |
|
| 22 | - protected function getExporter():IMetricFamily { |
|
| 23 | - $this->systemConfig = $this->createMock(SystemConfig::class); |
|
| 24 | - $this->serverVersion = $this->createMock(ServerVersion::class); |
|
| 25 | - $this->serverVersion->method('getHumanVersion')->willReturn('33.13.17 Gold'); |
|
| 26 | - $this->serverVersion->method('getVersion')->willReturn([33, 13, 17]); |
|
| 27 | - $this->serverVersion->method('getBuild')->willReturn('dev'); |
|
| 28 | - |
|
| 29 | - return new InstanceInfo($this->systemConfig, $this->serverVersion); |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - public function testMetrics(): void { |
|
| 33 | - $this->assertCount(1, $this->metrics); |
|
| 34 | - $metric = array_pop($this->metrics); |
|
| 35 | - $this->assertSame([ |
|
| 36 | - 'full_version' => '33.13.17 Gold', |
|
| 37 | - 'major_version' => '33', |
|
| 38 | - 'build' => 'dev', |
|
| 39 | - 'installed' => '0', |
|
| 40 | - ], $metric->labels); |
|
| 41 | - } |
|
| 19 | + private SystemConfig&MockObject $systemConfig; |
|
| 20 | + private ServerVersion&MockObject $serverVersion; |
|
| 21 | + |
|
| 22 | + protected function getExporter():IMetricFamily { |
|
| 23 | + $this->systemConfig = $this->createMock(SystemConfig::class); |
|
| 24 | + $this->serverVersion = $this->createMock(ServerVersion::class); |
|
| 25 | + $this->serverVersion->method('getHumanVersion')->willReturn('33.13.17 Gold'); |
|
| 26 | + $this->serverVersion->method('getVersion')->willReturn([33, 13, 17]); |
|
| 27 | + $this->serverVersion->method('getBuild')->willReturn('dev'); |
|
| 28 | + |
|
| 29 | + return new InstanceInfo($this->systemConfig, $this->serverVersion); |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + public function testMetrics(): void { |
|
| 33 | + $this->assertCount(1, $this->metrics); |
|
| 34 | + $metric = array_pop($this->metrics); |
|
| 35 | + $this->assertSame([ |
|
| 36 | + 'full_version' => '33.13.17 Gold', |
|
| 37 | + 'major_version' => '33', |
|
| 38 | + 'build' => 'dev', |
|
| 39 | + 'installed' => '0', |
|
| 40 | + ], $metric->labels); |
|
| 41 | + } |
|
| 42 | 42 | } |
@@ -21,43 +21,43 @@ |
||
| 21 | 21 | * Export some basic information about current instance |
| 22 | 22 | */ |
| 23 | 23 | class InstanceInfo implements IMetricFamily { |
| 24 | - public function __construct( |
|
| 25 | - private SystemConfig $systemConfig, |
|
| 26 | - private ServerVersion $serverVersion, |
|
| 27 | - ) { |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - #[Override] |
|
| 31 | - public function name(): string { |
|
| 32 | - return 'instance_info'; |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - #[Override] |
|
| 36 | - public function type(): MetricType { |
|
| 37 | - return MetricType::info; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - #[Override] |
|
| 41 | - public function unit(): string { |
|
| 42 | - return ''; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - #[Override] |
|
| 46 | - public function help(): string { |
|
| 47 | - return 'Basic information about Nextcloud'; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - #[Override] |
|
| 51 | - public function metrics(): Generator { |
|
| 52 | - yield new Metric( |
|
| 53 | - 1, |
|
| 54 | - [ |
|
| 55 | - 'full_version' => $this->serverVersion->getHumanVersion(), |
|
| 56 | - 'major_version' => (string)$this->serverVersion->getVersion()[0], |
|
| 57 | - 'build' => $this->serverVersion->getBuild(), |
|
| 58 | - 'installed' => $this->systemConfig->getValue('installed', false) ? '1' : '0', |
|
| 59 | - ], |
|
| 60 | - time() |
|
| 61 | - ); |
|
| 62 | - } |
|
| 24 | + public function __construct( |
|
| 25 | + private SystemConfig $systemConfig, |
|
| 26 | + private ServerVersion $serverVersion, |
|
| 27 | + ) { |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + #[Override] |
|
| 31 | + public function name(): string { |
|
| 32 | + return 'instance_info'; |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + #[Override] |
|
| 36 | + public function type(): MetricType { |
|
| 37 | + return MetricType::info; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + #[Override] |
|
| 41 | + public function unit(): string { |
|
| 42 | + return ''; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + #[Override] |
|
| 46 | + public function help(): string { |
|
| 47 | + return 'Basic information about Nextcloud'; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + #[Override] |
|
| 51 | + public function metrics(): Generator { |
|
| 52 | + yield new Metric( |
|
| 53 | + 1, |
|
| 54 | + [ |
|
| 55 | + 'full_version' => $this->serverVersion->getHumanVersion(), |
|
| 56 | + 'major_version' => (string)$this->serverVersion->getVersion()[0], |
|
| 57 | + 'build' => $this->serverVersion->getBuild(), |
|
| 58 | + 'installed' => $this->systemConfig->getValue('installed', false) ? '1' : '0', |
|
| 59 | + ], |
|
| 60 | + time() |
|
| 61 | + ); |
|
| 62 | + } |
|
| 63 | 63 | } |
@@ -53,7 +53,7 @@ |
||
| 53 | 53 | 1, |
| 54 | 54 | [ |
| 55 | 55 | 'full_version' => $this->serverVersion->getHumanVersion(), |
| 56 | - 'major_version' => (string)$this->serverVersion->getVersion()[0], |
|
| 56 | + 'major_version' => (string) $this->serverVersion->getVersion()[0], |
|
| 57 | 57 | 'build' => $this->serverVersion->getBuild(), |
| 58 | 58 | 'installed' => $this->systemConfig->getValue('installed', false) ? '1' : '0', |
| 59 | 59 | ], |