Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix files:scan repair + basic tests for files:scan command #29635

Merged
merged 3 commits into from
Feb 26, 2018
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
8 changes: 7 additions & 1 deletion apps/files/lib/Command/Scan.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use OCP\IUserManager;
use OCP\Lock\ILockingProvider;
use OCP\Lock\LockedException;
use OCP\ILogger;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -58,6 +59,8 @@ class Scan extends Base {
private $lockingProvider;
/** @var IMimeTypeLoader */
private $mimeTypeLoader;
/** @var ILogger */
private $logger;
/** @var IConfig */
private $config;
/** @var float */
Expand All @@ -72,12 +75,14 @@ public function __construct(
IGroupManager $groupManager,
ILockingProvider $lockingProvider,
IMimeTypeLoader $mimeTypeLoader,
ILogger $logger,
IConfig $config
) {
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->lockingProvider = $lockingProvider;
$this->mimeTypeLoader = $mimeTypeLoader;
$this->logger = $logger;
$this->config = $config;
parent::__construct();
}
Expand Down Expand Up @@ -154,7 +159,8 @@ protected function repairAll(OutputInterface $output) {
$connection = $this->reconnectToDatabase($output);
$repairStep = new RepairMismatchFileCachePath(
$connection,
$this->mimeTypeLoader
$this->mimeTypeLoader,
$this->logger
);
$repairStep->setStorageNumericId(null);
$repairStep->setCountOnly(false);
Expand Down
261 changes: 246 additions & 15 deletions apps/files/tests/Command/ScanTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/**
* @author Sujith Haridasan <[email protected]>
* @author Vincent Petry <[email protected]>
*
* @copyright Copyright (c) 2018, ownCloud GmbH
* @license AGPL-3.0
Expand All @@ -25,6 +26,14 @@
use Symfony\Component\Console\Tester\CommandTester;
use Test\TestCase;
use Test\Traits\UserTrait;
use OCP\IUserManager;
use OCP\IConfig;
use OCP\Files\IMimeTypeLoader;
use OCP\Lock\ILockingProvider;
use OCP\IUser;
use OCP\IDBConnection;
use OCP\IGroupManager;
use OCP\ILogger;

/**
* Class ScanTest
Expand All @@ -35,29 +44,96 @@
class ScanTest extends TestCase {
use UserTrait;

/** @var CommandTester */
/**
* @var IDBConnection
*/
private $connection;

/**
* @var IUserManager
*/
private $userManager;

/**
* @var IGroupManager
*/
private $groupManager;

/**
* @var ILockingProvider | \PHPUnit_Framework_MockObject_MockObject
*/
private $lockingProvider;

/**
* @var IMimeTypeLoader | \PHPUnit_Framework_MockObject_MockObject
*/
private $mimeTypeLoader;

/**
* @var IConfig | \PHPUnit_Framework_MockObject_MockObject
*/
private $config;

/**
* @var IUser
*/
private $scanUser1;

/**
* @var IUser
*/
private $scanUser2;

/**
* @var CommandTester
*/
private $commandTester;

/**
* @var string[]
*/
private $groupsCreated = [];

protected function setUp() {
if (getenv('RUN_OBJECTSTORE_TESTS')) {
$this->markTestSkipped('not testing scanner as it does not make sense for primary object store');
}
parent::setUp();
$command = new Scan(
\OC::$server->getUserManager(), \OC::$server->getGroupManager(),
\OC::$server->getLockingProvider(), \OC::$server->getMimeTypeLoader(),
\OC::$server->getConfig());

$this->connection = \OC::$server->getDatabaseConnection();
$this->userManager = \OC::$server->getUserManager();
$this->groupManager = \OC::$server->getGroupManager();
$this->lockingProvider = $this->createMock(ILockingProvider::class);
$this->mimeTypeLoader = $this->createMock(IMimeTypeLoader::class);
$this->config = $this->createMock(IConfig::class);

$command = new Scan(
$this->userManager,
$this->groupManager,
$this->lockingProvider,
$this->mimeTypeLoader,
$this->createMock(ILogger::class),
$this->config
);
$this->commandTester = new CommandTester($command);

$this->scanUser1 = $this->createUser('scanuser1' . uniqid());
$this->scanUser2 = $this->createUser('scanuser2' . uniqid());

$user1 = $this->createUser('user1');
$this->createUser('user2');
\OC::$server->getGroupManager()->createGroup('group1');
\OC::$server->getGroupManager()->get('group1')->addUser($user1);
$this->groupManager->createGroup('group1');
$this->groupManager->get('group1')->addUser($user1);
$this->groupsCreated[] = 'group1';

$this->dataDir = \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data-autotest');

@mkdir($this->dataDir . '/' . $this->scanUser1->getUID() . '/files/toscan', 0777, true);
}

protected function tearDown() {
$this->tearDownUserTrait();
foreach ($this->groupsCreated as $group) {
\OC::$server->getGroupManager()->get($group)->delete();
$this->groupManager->get($group)->delete();
}
parent::tearDown();
}
Expand Down Expand Up @@ -97,7 +173,7 @@ public function testGroupPaginationForUsers($input, $expectedOutput) {
$numberOfUsersInGroup = 210;
for($i = 2; $i <= 210; $i++) {
$userObj = $this->createUser($user.$i);
\OC::$server->getGroupManager()->get('group1')->addUser($userObj);
$this->groupManager->get('group1')->addUser($userObj);
}

$this->commandTester->execute($input);
Expand Down Expand Up @@ -129,18 +205,18 @@ public function testMultipleGroups($input) {

$userCount = 1;
foreach ($groups as $group) {
if (\OC::$server->getGroupManager()->groupExists($group) === false) {
\OC::$server->getGroupManager()->createGroup($group);
if ($this->groupManager->groupExists($group) === false) {
$this->groupManager->createGroup($group);
$this->groupsCreated[] = $group;
for ($i = $userCount; $i <= ($userCount + 9); $i++) {
$j = $i - 1;
\OC::$server->getGroupManager()->get($group)->addUser($userObj[$j]);
$this->groupManager->get($group)->addUser($userObj[$j]);
}
$userCount = $i;
} else {
for ($i = $userCount; $i <= ($userCount + 9); $i++) {
$j = $i - 1;
\OC::$server->getGroupManager()->get($group)->addUser($userObj[$j]);
$this->groupManager->get($group)->addUser($userObj[$j]);
}
$userCount = $i;
}
Expand All @@ -159,4 +235,159 @@ public function testMultipleGroups($input) {
$this->assertContains('Starting scan for user 10 out of 10 (user30)', $output);
}
}
}

public function testScanAll() {
@mkdir($this->dataDir . '/' . $this->scanUser2->getUID() . '/files/toscan2', 0777, true);

$input = [
'--all' => true,
];

$result = $this->commandTester->execute($input);
$this->assertEquals(0, $result);

// new entry was found for both users
$storageId = $this->getStorageId('home::' . $this->scanUser1->getUID());
$entry = $this->getFileCacheEntry($storageId, 'files/toscan');
$this->assertEquals('files/toscan', $entry['path']);

$storageId2 = $this->getStorageId('home::' . $this->scanUser2->getUID());
$entry2 = $this->getFileCacheEntry($storageId2, 'files/toscan2');
$this->assertEquals('files/toscan2', $entry2['path']);

}

public function testScanOne() {
@mkdir($this->dataDir . '/' . $this->scanUser2->getUID() . '/files/toscan2', 0777, true);

$input = [
'user_id' => [$this->scanUser2->getUID()],
];

$result = $this->commandTester->execute($input);
$this->assertEquals(0, $result);

// new entry was found only for user2
$storageId = $this->getStorageId('home::' . $this->scanUser1->getUID());
$this->assertFalse($this->getFileCacheEntry($storageId, 'files/toscan'));

$storageId2 = $this->getStorageId('home::' . $this->scanUser2->getUID());
$entry2 = $this->getFileCacheEntry($storageId2, 'files/toscan2');
$this->assertEquals('files/toscan2', $entry2['path']);
}

public function maintenanceConfigsProvider() {
return [
[
[
['singleuser', false, true],
['maintenance', false, false],
],
],
[
[
['singleuser', false, false],
['maintenance', false, true],
],
],
];
}

/**
* Test running repair all
*
* @dataProvider maintenanceConfigsProvider
*/
public function testScanRepairAllInMaintenanceMode($config) {
$this->config->method('getSystemValue')
->will($this->returnValueMap($config));

$input = [
'--all' => true,
'--repair' => true,
];

$result = $this->commandTester->execute($input);

// TODO: find a way to test that repair code has run

// new entry was found
$storageId = $this->getStorageId('home::' . $this->scanUser1->getUID());
$entry = $this->getFileCacheEntry($storageId, 'files/toscan');
$this->assertEquals('files/toscan', $entry['path']);

$this->assertEquals(0, $result);
}

/**
* Returns storage numeric id for the given string id
*
* @param string $storageStringId
* @return int|null numeric id
*/
private function getStorageId($storageStringId) {
$qb = $this->connection->getQueryBuilder();
$qb->select('numeric_id')
->from('storages')
->where($qb->expr()->eq('id', $qb->createNamedParameter($storageStringId)));
$results = $qb->execute();
$result = $results->fetch();
$results->closeCursor();

if ($result) {
return (int)$result['numeric_id'];
}

return null;
}

/**
* Returns file cache DB entry for given path
*
* @param int $storageId storage numeric id
* @param string $path path
* @return array file cache DB entry
*/
private function getFileCacheEntry($storageId, $path) {
$qb = $this->connection->getQueryBuilder();
$qb->select('*')
->from('filecache')
->where($qb->expr()->eq('storage', $qb->createNamedParameter($storageId)))
->andWhere($qb->expr()->eq('path_hash', $qb->createNamedParameter(md5($path))));
$results = $qb->execute();
$result = $results->fetch();
$results->closeCursor();

return $result;
}

/**
* Test repair all error message when not in maintenance mode
*
*/
public function testScanRepairAllNoSingleUserMode() {
$this->config->method('getSystemValue')
->will($this->returnValueMap([
['singleuser', false, false],
['maintenance', false, false],
]));

$input = [
'--all' => true,
'--repair' => true,
];

$result = $this->commandTester->execute($input);

$this->assertEquals(1, $result);

$output = $this->commandTester->getDisplay();

$this->assertContains('Please switch to single user mode', $output);
$this->assertContains('specify a user to repair', $output);

$storageId = $this->getStorageId('home::' . $this->scanUser1->getUID());
$this->assertFalse($this->getFileCacheEntry($storageId, 'files/toscan'));
}
}

3 changes: 3 additions & 0 deletions tests/lib/Util/User/MemoryAccountMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public function getUserCount($hasLoggedIn) {
}

public function search($fieldName, $pattern, $limit, $offset) {
if ($pattern === '') {
return self::$accounts;
}
$match = array_filter(self::$accounts, function (Account $a) use ($pattern) {
return stripos($a->getUserId(), $pattern);
});
Expand Down