check for readable host id files before read (#1400)
* check for readable host id files before read * reset vfs between tests
This commit is contained in:
parent
f29277abc0
commit
c632d960a5
|
@ -52,8 +52,9 @@ final class Host implements ResourceDetectorInterface
|
|||
$paths = [self::PATH_ETC_MACHINEID, self::PATH_VAR_LIB_DBUS_MACHINEID];
|
||||
|
||||
foreach ($paths as $path) {
|
||||
if (file_exists($this->dir . $path)) {
|
||||
return trim(file_get_contents($this->dir . $path));
|
||||
$file = $this->dir . $path;
|
||||
if (is_file($file) && is_readable($file)) {
|
||||
return trim(file_get_contents($file));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -62,13 +63,14 @@ final class Host implements ResourceDetectorInterface
|
|||
|
||||
private function getBsdId(): ?string
|
||||
{
|
||||
if (file_exists($this->dir . self::PATH_ETC_HOSTID)) {
|
||||
return trim(file_get_contents($this->dir . self::PATH_ETC_HOSTID));
|
||||
$file = $this->dir . self::PATH_ETC_HOSTID;
|
||||
if (is_file($file) && is_readable($file)) {
|
||||
return trim(file_get_contents($file));
|
||||
}
|
||||
|
||||
$out = exec('kenv -q smbios.system.uuid');
|
||||
$out = exec('which kenv && kenv -q smbios.system.uuid');
|
||||
|
||||
if ($out !== false) {
|
||||
if ($out) {
|
||||
return $out;
|
||||
}
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ namespace OpenTelemetry\Tests\Unit\SDK\Resource\Detectors;
|
|||
use OpenTelemetry\SDK\Resource\Detectors\Host;
|
||||
use OpenTelemetry\SemConv\ResourceAttributes;
|
||||
use org\bovigo\vfs\vfsStream;
|
||||
use org\bovigo\vfs\vfsStreamDirectory;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
@ -14,6 +15,12 @@ use PHPUnit\Framework\TestCase;
|
|||
#[CoversClass(Host::class)]
|
||||
class HostTest extends TestCase
|
||||
{
|
||||
public function setUp(): void
|
||||
{
|
||||
//reset vfs between tests
|
||||
vfsStream::setup('/');
|
||||
}
|
||||
|
||||
public function test_host_get_resource(): void
|
||||
{
|
||||
$resourceDetector = new Host();
|
||||
|
@ -72,4 +79,33 @@ class HostTest extends TestCase
|
|||
['BSD', $etc_hostid, '1234567890'],
|
||||
];
|
||||
}
|
||||
|
||||
#[DataProvider('nonReadableFileProvider')]
|
||||
public function test_file_not_readable(string $os, vfsStreamDirectory $root): void
|
||||
{
|
||||
$resourceDetector = new Host($root->url(), $os);
|
||||
$resource = $resourceDetector->getResource();
|
||||
|
||||
$hostId = $resource->getAttributes()->get(ResourceAttributes::HOST_ID);
|
||||
$this->assertNull($hostId);
|
||||
}
|
||||
|
||||
public static function nonReadableFileProvider(): array
|
||||
{
|
||||
$root = vfsStream::setup('/');
|
||||
$etc = vfsStream::newDirectory('etc')->at($root);
|
||||
vfsStream::newFile('machine-id')
|
||||
->at($etc)
|
||||
->setContent('you-cant-see-me')
|
||||
->chmod(0222);
|
||||
vfsStream::newFile('hostid')
|
||||
->at($etc)
|
||||
->setContent('you-cant-see-me')
|
||||
->chmod(0222);
|
||||
|
||||
return [
|
||||
['Linux', $root],
|
||||
['BSD', $root],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue