Skip to content

Commit

Permalink
test: add some minimal testing for metadata storage
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 committed Oct 4, 2024
1 parent 9a3bcc3 commit 241c22d
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/private/FilesMetadata/FilesMetadataManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ public function refreshMetadata(
try {
/** @var FilesMetadata $metadata */
$metadata = $this->metadataRequestService->getMetadataFromFileId($node->getId());
$metadata->setStorageId($storageId);
} catch (FilesMetadataNotFoundException) {
$metadata = new FilesMetadata($node->getId(), $storageId);
$metadata = new FilesMetadata($node->getId());
}
$metadata->setStorageId($storageId);

// if $process is LIVE, we enforce LIVE
// if $process is NAMED, we go NAMED
Expand Down
10 changes: 9 additions & 1 deletion lib/private/FilesMetadata/Model/FilesMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ class FilesMetadata implements IFilesMetadata {
private bool $updated = false;
private int $lastUpdate = 0;
private string $syncToken = '';
private ?int $storageId = null;

public function __construct(
private int $fileId = 0,
private ?int $storageId = null,
) {
}

Expand All @@ -47,6 +47,14 @@ public function getStorageId(): ?int {
return $this->storageId;
}

/**
* Set which storage the file this metadata belongs to.
*
* This helps with sharded filecache setups to know where to store the metadata
*
* @param int $storageId
* @return void
*/
public function setStorageId(int $storageId): void {
$this->storageId = $storageId;
}
Expand Down
7 changes: 5 additions & 2 deletions lib/private/FilesMetadata/Service/MetadataRequestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ public function __construct(
}

private function getStorageId(IFilesMetadata $filesMetadata): int {
if ($filesMetadata instanceof FilesMetadata && $filesMetadata->getStorageId()) {
return $filesMetadata->getStorageId();
if ($filesMetadata instanceof FilesMetadata) {
$storage = $filesMetadata->getStorageId();
if ($storage) {
return $storage;
}
}
// all code paths that lead to saving metadata *should* have the storage id set
// this fallback is there just in case
Expand Down
98 changes: 98 additions & 0 deletions tests/lib/FilesMetadata/FilesMetadataManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Robin Appelman <[email protected]>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace Test\FilesMetadata;

use OC\BackgroundJob\JobList;
use OC\Files\Storage\Temporary;
use OC\FilesMetadata\FilesMetadataManager;
use OC\FilesMetadata\Service\IndexRequestService;
use OC\FilesMetadata\Service\MetadataRequestService;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\FilesMetadata\AMetadataEvent;
use OCP\IAppConfig;
use OCP\IDBConnection;
use OCP\Server;
use Psr\Log\LoggerInterface;
use Test\TestCase;
use Test\Traits\MountProviderTrait;
use Test\Traits\UserTrait;

/**
* @group DB
*/
class FilesMetadataManagerTest extends TestCase {
use UserTrait;
use MountProviderTrait;

private IEventDispatcher $eventDispatcher;
private JobList $jobList;
private IAppConfig $appConfig;
private LoggerInterface $logger;
private MetadataRequestService $metadataRequestService;
private IndexRequestService $indexRequestService;
private FilesMetadataManager $manager;
private IDBConnection $connection;
private Folder $userFolder;
private array $metadata = [];

protected function setUp(): void {
parent::setUp();

$this->jobList = $this->createMock(JobList::class);
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->eventDispatcher->method('dispatchTyped')->willReturnCallback(function(Event $event) {
if ($event instanceof AMetadataEvent) {
$name = $event->getNode()->getName();
if (isset($this->metadata[$name])) {
$meta = $event->getMetadata();
foreach ($this->metadata[$name] as $key => $value) {
$meta->setString($key, $value);
}
}
}
});
$this->appConfig = $this->createMock(IAppConfig::class);
$this->logger = $this->createMock(LoggerInterface::class);

$this->connection = Server::get(IDBConnection::class);
$this->metadataRequestService = new MetadataRequestService($this->connection, $this->logger);
$this->indexRequestService = new IndexRequestService($this->connection, $this->logger);
$this->manager = new FilesMetadataManager(
$this->eventDispatcher,
$this->jobList,
$this->appConfig,
$this->logger,
$this->metadataRequestService,
$this->indexRequestService,
);

$this->createUser('metatest', '');
$this->registerMount('metatest', new Temporary([]), '/metatest');

$rootFolder = Server::get(IRootFolder::class);
$this->userFolder = $rootFolder->getUserFolder('metatest');
}

public function testRefreshMetadata(): void {
$this->metadata['test.txt'] = [
'istest' => 'yes'
];
$file = $this->userFolder->newFile('test.txt', 'test');
$stored = $this->manager->refreshMetadata($file);
$this->assertEquals($file->getId(), $stored->getFileId());
$this->assertEquals('yes', $stored->getString('istest'));

$retrieved = $this->manager->getMetadata($file->getId());
$this->assertEquals($file->getId(), $retrieved->getFileId());
$this->assertEquals('yes', $retrieved->getString('istest'));
}
}

0 comments on commit 241c22d

Please sign in to comment.