RootMountProviderTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2022 Robin Appelman <robin@icewind.nl>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace Test\Files\Mount;
  23. use OC\Files\Mount\RootMountProvider;
  24. use OC\Files\ObjectStore\ObjectStoreStorage;
  25. use OC\Files\ObjectStore\S3;
  26. use OC\Files\Storage\LocalRootStorage;
  27. use OC\Files\Storage\StorageFactory;
  28. use OCP\IConfig;
  29. use Psr\Log\LoggerInterface;
  30. use Test\TestCase;
  31. /**
  32. * @group DB
  33. */
  34. class RootMountProviderTest extends TestCase {
  35. private StorageFactory $loader;
  36. protected function setUp(): void {
  37. parent::setUp();
  38. $this->loader = new StorageFactory();
  39. }
  40. private function getConfig(array $systemConfig): IConfig {
  41. $config = $this->createMock(IConfig::class);
  42. $config->method('getSystemValue')
  43. ->willReturnCallback(function (string $key, $default) use ($systemConfig) {
  44. return $systemConfig[$key] ?? $default;
  45. });
  46. return $config;
  47. }
  48. private function getProvider(array $systemConfig): RootMountProvider {
  49. $config = $this->getConfig($systemConfig);
  50. $provider = new RootMountProvider($config, $this->createMock(LoggerInterface::class));
  51. return $provider;
  52. }
  53. public function testLocal() {
  54. $provider = $this->getProvider([
  55. 'datadirectory' => '/data',
  56. ]);
  57. $mounts = $provider->getRootMounts($this->loader);
  58. $this->assertCount(1, $mounts);
  59. $mount = $mounts[0];
  60. $this->assertEquals('/', $mount->getMountPoint());
  61. /** @var LocalRootStorage $storage */
  62. $storage = $mount->getStorage();
  63. $this->assertInstanceOf(LocalRootStorage::class, $storage);
  64. $this->assertEquals('/data/', $storage->getSourcePath(''));
  65. }
  66. public function testObjectStore() {
  67. $provider = $this->getProvider([
  68. 'objectstore' => [
  69. "class" => "OC\Files\ObjectStore\S3",
  70. "arguments" => [
  71. "bucket" => "nextcloud",
  72. "autocreate" => true,
  73. "key" => "minio",
  74. "secret" => "minio123",
  75. "hostname" => "localhost",
  76. "port" => 9000,
  77. "use_ssl" => false,
  78. "use_path_style" => true,
  79. "uploadPartSize" => 52428800,
  80. ],
  81. ],
  82. ]);
  83. $mounts = $provider->getRootMounts($this->loader);
  84. $this->assertCount(1, $mounts);
  85. $mount = $mounts[0];
  86. $this->assertEquals('/', $mount->getMountPoint());
  87. /** @var ObjectStoreStorage $storage */
  88. $storage = $mount->getStorage();
  89. $this->assertInstanceOf(ObjectStoreStorage::class, $storage);
  90. $class = new \ReflectionClass($storage);
  91. $prop = $class->getProperty('objectStore');
  92. $prop->setAccessible(true);
  93. /** @var S3 $objectStore */
  94. $objectStore = $prop->getValue($storage);
  95. $this->assertEquals('nextcloud', $objectStore->getBucket());
  96. }
  97. public function testObjectStoreMultiBucket() {
  98. $provider = $this->getProvider([
  99. 'objectstore_multibucket' => [
  100. "class" => "OC\Files\ObjectStore\S3",
  101. "arguments" => [
  102. "bucket" => "nextcloud",
  103. "autocreate" => true,
  104. "key" => "minio",
  105. "secret" => "minio123",
  106. "hostname" => "localhost",
  107. "port" => 9000,
  108. "use_ssl" => false,
  109. "use_path_style" => true,
  110. "uploadPartSize" => 52428800,
  111. ],
  112. ],
  113. ]);
  114. $mounts = $provider->getRootMounts($this->loader);
  115. $this->assertCount(1, $mounts);
  116. $mount = $mounts[0];
  117. $this->assertEquals('/', $mount->getMountPoint());
  118. /** @var ObjectStoreStorage $storage */
  119. $storage = $mount->getStorage();
  120. $this->assertInstanceOf(ObjectStoreStorage::class, $storage);
  121. $class = new \ReflectionClass($storage);
  122. $prop = $class->getProperty('objectStore');
  123. $prop->setAccessible(true);
  124. /** @var S3 $objectStore */
  125. $objectStore = $prop->getValue($storage);
  126. $this->assertEquals('nextcloud0', $objectStore->getBucket());
  127. }
  128. }