Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions tests/Unit/AppHost/Observability/BundledManifestFileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

/**
* Tests for BundledManifestFile::decode: a valid object, a missing file,
* invalid JSON and JSON that is not an object or array.
*
* SPDX-License-Identifier: EUPL-1.2
* SPDX-FileCopyrightText: 2026 Conduction B.V.
*
* @category Test
* @package OCA\OpenRegister\Tests\Unit\AppHost\Observability
*
* @author Conduction Development Team <info@conduction.nl>
* @copyright 2026 Conduction B.V.
* @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
*
* @link https://www.OpenRegister.nl
*/

declare(strict_types=1);

namespace OCA\OpenRegister\Tests\Unit\AppHost\Observability;

use OCA\OpenRegister\AppHost\Observability\BundledManifestFile;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;

/**
* Reads manifests from a temporary app directory.
*/
class BundledManifestFileTest extends TestCase {

private string $appPath = '';

protected function setUp(): void {
$this->appPath = sys_get_temp_dir().'/bmf-a2-'.getmypid();
mkdir($this->appPath.'/src', 0777, true);
}

protected function tearDown(): void {
@unlink($this->appPath.'/src/manifest.json');
@rmdir($this->appPath.'/src');
@rmdir($this->appPath);
}

private function write(string $contents): void {
file_put_contents($this->appPath.'/src/manifest.json', $contents);
}

public function testAValidObjectIsDecodedWithoutWarning(): void {
$this->write('{"observability":{"metrics":[{"name":"m"}]}}');
$logger = $this->createMock(LoggerInterface::class);
$logger->expects($this->never())->method('warning');

$this->assertSame(
['observability' => ['metrics' => [['name' => 'm']]]],
BundledManifestFile::decode($this->appPath, 'myapp', $logger, 'Tag')
);
}

public function testAJsonArrayIsAcceptedAsDecoded(): void {
$this->write('[]');
$logger = $this->createMock(LoggerInterface::class);
$logger->expects($this->never())->method('warning');

$this->assertSame([], BundledManifestFile::decode($this->appPath, 'myapp', $logger, 'Tag'));
}

public function testAMissingManifestIsNullWithoutWarning(): void {
$logger = $this->createMock(LoggerInterface::class);
$logger->expects($this->never())->method('warning');

$this->assertNull(BundledManifestFile::decode($this->appPath, 'myapp', $logger, 'Tag'));
}

public function testInvalidJsonIsNullAndWarnsWithTagAndAppId(): void {
$this->write('{"broken":');
$logger = $this->createMock(LoggerInterface::class);
$logger->expects($this->once())->method('warning')->with(
'[AppHost\\Scheduling] manifest.json for "myapp" is invalid JSON',
$this->callback(static fn (array $context): bool => isset($context['file'], $context['line']) === true)
);

$this->assertNull(BundledManifestFile::decode($this->appPath, 'myapp', $logger, 'AppHost\\Scheduling'));
}

public function testAScalarJsonDocumentIsNullAndWarns(): void {
$this->write('42');
$logger = $this->createMock(LoggerInterface::class);
$logger->expects($this->once())->method('warning')->with(
'[Tag] manifest.json for "other" is invalid JSON',
$this->anything()
);

$this->assertNull(BundledManifestFile::decode($this->appPath, 'other', $logger, 'Tag'));
}
}//end class
104 changes: 104 additions & 0 deletions tests/Unit/AppHost/Observability/PrometheusRendererBehaviourTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

/**
* Tests for PrometheusRenderer beyond PrometheusRendererTest: prefixes that
* need `app_`, label keys that need `_`, HELP escaping, float values and the
* exact document layout.
*
* SPDX-License-Identifier: EUPL-1.2
* SPDX-FileCopyrightText: 2026 Conduction B.V.
*
* @category Test
* @package OCA\OpenRegister\Tests\Unit\AppHost\Observability
*
* @author Conduction Development Team <info@conduction.nl>
* @copyright 2026 Conduction B.V.
* @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
*
* @link https://www.OpenRegister.nl
*/

declare(strict_types=1);

namespace OCA\OpenRegister\Tests\Unit\AppHost\Observability;

use OCA\OpenRegister\AppHost\Observability\MetricSample;
use OCA\OpenRegister\AppHost\Observability\PrometheusRenderer;
use PHPUnit\Framework\TestCase;

/**
* Exact-output checks of the renderer.
*/
class PrometheusRendererBehaviourTest extends TestCase {

public function testAPrefixStartingWithADigitGetsAppPrefix(): void {
$out = (new PrometheusRenderer())->render('2fa', [MetricSample::single('m', 'gauge', 'H', 1)]);

$this->assertSame("# HELP app_2fa_m H\n# TYPE app_2fa_m gauge\napp_2fa_m 1\n\n", $out);
}

public function testAnAppIdOfOnlyUnsafeCharactersBecomesApp(): void {
$out = (new PrometheusRenderer())->render('--', [MetricSample::single('m', 'gauge', 'H', 1)]);

$this->assertStringContainsString("\napp__m 1\n", $out);
}

public function testUnderscoresAreTrimmedFromThePrefix(): void {
$out = (new PrometheusRenderer())->render('_myapp_', [MetricSample::single('m', 'gauge', 'H', 1)]);

$this->assertStringContainsString("\nmyapp_m 1\n", $out);
}

public function testMetricNameAndLabelKeysAreSanitised(): void {
$out = (new PrometheusRenderer())->render(
'app',
[new MetricSample('cases.total', 'gauge', 'H', [['labels' => ['1st' => 'a', 'b-c' => 'b', 'ok' => 'c'], 'value' => 2]])]
);

$this->assertStringContainsString('app_cases_total{_1st="a",b_c="b",ok="c"} 2', $out);
}

public function testHelpBackslashAndNewlineAreEscaped(): void {
$out = (new PrometheusRenderer())->render('app', [MetricSample::single('m', 'gauge', "a\\b\nc", 1)]);

$this->assertStringContainsString('# HELP app_m a\\\\b\\nc'."\n", $out);
}

public function testUnknownTypeRendersAsGauge(): void {
$out = (new PrometheusRenderer())->render('app', [MetricSample::single('m', 'histogram', 'H', 1)]);

$this->assertStringContainsString("# TYPE app_m gauge\n", $out);
}

public function testFloatValuesKeepFractionAndIntegralFloatsLoseIt(): void {
$out = (new PrometheusRenderer())->render(
'app',
[
new MetricSample(
'm',
'gauge',
'H',
[
['labels' => ['k' => 'a'], 'value' => 1.5],
['labels' => ['k' => 'b'], 'value' => 3.0],
]
),
]
);

$this->assertSame(
"# HELP app_m H\n# TYPE app_m gauge\napp_m{k=\"a\"} 1.5\napp_m{k=\"b\"} 3\n\n",
$out
);
}

public function testAFamilyWithoutSamplesStillGetsHelpAndTypeAndNoSamplesRenderNothing(): void {
$renderer = new PrometheusRenderer();

$this->assertSame(
"# HELP app_m H\n# TYPE app_m counter\n\n",
$renderer->render('app', [new MetricSample('m', 'counter', 'H', [])])
);
$this->assertSame("\n", $renderer->render('app', []));
}
}//end class
Loading
Loading