RootMountProviderTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Files\Mount;
  8. use OC\Files\Mount\RootMountProvider;
  9. use OC\Files\ObjectStore\ObjectStoreStorage;
  10. use OC\Files\ObjectStore\S3;
  11. use OC\Files\Storage\LocalRootStorage;
  12. use OC\Files\Storage\StorageFactory;
  13. use OCP\IConfig;
  14. use Psr\Log\LoggerInterface;
  15. use Test\TestCase;
  16. /**
  17. * @group DB
  18. */
  19. class RootMountProviderTest extends TestCase {
  20. private StorageFactory $loader;
  21. protected function setUp(): void {
  22. parent::setUp();
  23. $this->loader = new StorageFactory();
  24. }
  25. private function getConfig(array $systemConfig): IConfig {
  26. $config = $this->createMock(IConfig::class);
  27. $config->method('getSystemValue')
  28. ->willReturnCallback(function (string $key, $default) use ($systemConfig) {
  29. return $systemConfig[$key] ?? $default;
  30. });
  31. return $config;
  32. }
  33. private function getProvider(array $systemConfig): RootMountProvider {
  34. $config = $this->getConfig($systemConfig);
  35. $provider = new RootMountProvider($config, $this->createMock(LoggerInterface::class));
  36. return $provider;
  37. }
  38. public function testLocal(): void {
  39. $provider = $this->getProvider([
  40. 'datadirectory' => '/data',
  41. ]);
  42. $mounts = $provider->getRootMounts($this->loader);
  43. $this->assertCount(1, $mounts);
  44. $mount = $mounts[0];
  45. $this->assertEquals('/', $mount->getMountPoint());
  46. /** @var LocalRootStorage $storage */
  47. $storage = $mount->getStorage();
  48. $this->assertInstanceOf(LocalRootStorage::class, $storage);
  49. $this->assertEquals('/data/', $storage->getSourcePath(''));
  50. }
  51. public function testObjectStore(): void {
  52. $provider = $this->getProvider([
  53. 'objectstore' => [
  54. 'class' => "OC\Files\ObjectStore\S3",
  55. 'arguments' => [
  56. 'bucket' => 'nextcloud',
  57. 'autocreate' => true,
  58. 'key' => 'minio',
  59. 'secret' => 'minio123',
  60. 'hostname' => 'localhost',
  61. 'port' => 9000,
  62. 'use_ssl' => false,
  63. 'use_path_style' => true,
  64. 'uploadPartSize' => 52428800,
  65. ],
  66. ],
  67. ]);
  68. $mounts = $provider->getRootMounts($this->loader);
  69. $this->assertCount(1, $mounts);
  70. $mount = $mounts[0];
  71. $this->assertEquals('/', $mount->getMountPoint());
  72. /** @var ObjectStoreStorage $storage */
  73. $storage = $mount->getStorage();
  74. $this->assertInstanceOf(ObjectStoreStorage::class, $storage);
  75. $class = new \ReflectionClass($storage);
  76. $prop = $class->getProperty('objectStore');
  77. $prop->setAccessible(true);
  78. /** @var S3 $objectStore */
  79. $objectStore = $prop->getValue($storage);
  80. $this->assertEquals('nextcloud', $objectStore->getBucket());
  81. }
  82. public function testObjectStoreMultiBucket(): void {
  83. $provider = $this->getProvider([
  84. 'objectstore_multibucket' => [
  85. 'class' => "OC\Files\ObjectStore\S3",
  86. 'arguments' => [
  87. 'bucket' => 'nextcloud',
  88. 'autocreate' => true,
  89. 'key' => 'minio',
  90. 'secret' => 'minio123',
  91. 'hostname' => 'localhost',
  92. 'port' => 9000,
  93. 'use_ssl' => false,
  94. 'use_path_style' => true,
  95. 'uploadPartSize' => 52428800,
  96. ],
  97. ],
  98. ]);
  99. $mounts = $provider->getRootMounts($this->loader);
  100. $this->assertCount(1, $mounts);
  101. $mount = $mounts[0];
  102. $this->assertEquals('/', $mount->getMountPoint());
  103. /** @var ObjectStoreStorage $storage */
  104. $storage = $mount->getStorage();
  105. $this->assertInstanceOf(ObjectStoreStorage::class, $storage);
  106. $class = new \ReflectionClass($storage);
  107. $prop = $class->getProperty('objectStore');
  108. $prop->setAccessible(true);
  109. /** @var S3 $objectStore */
  110. $objectStore = $prop->getValue($storage);
  111. $this->assertEquals('nextcloud0', $objectStore->getBucket());
  112. }
  113. }