AvailabilityTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Test\Files\Storage\Wrapper;
  8. use OC\Files\Cache\Storage as StorageCache;
  9. use OC\Files\Storage\Temporary;
  10. use OC\Files\Storage\Wrapper\Availability;
  11. use OCP\Files\StorageNotAvailableException;
  12. class AvailabilityTest extends \Test\TestCase {
  13. /** @var \PHPUnit\Framework\MockObject\MockObject|StorageCache */
  14. protected $storageCache;
  15. /** @var \PHPUnit\Framework\MockObject\MockObject|Temporary */
  16. protected $storage;
  17. /** @var Availability */
  18. protected $wrapper;
  19. protected function setUp(): void {
  20. parent::setUp();
  21. $this->storageCache = $this->createMock(StorageCache::class);
  22. $this->storage = $this->createMock(Temporary::class);
  23. $this->storage->expects($this->any())
  24. ->method('getStorageCache')
  25. ->willReturn($this->storageCache);
  26. $this->wrapper = new Availability(['storage' => $this->storage]);
  27. }
  28. /**
  29. * Storage is available
  30. */
  31. public function testAvailable(): void {
  32. $this->storage->expects($this->once())
  33. ->method('getAvailability')
  34. ->willReturn(['available' => true, 'last_checked' => 0]);
  35. $this->storage->expects($this->never())
  36. ->method('test');
  37. $this->storage->expects($this->once())
  38. ->method('mkdir');
  39. $this->wrapper->mkdir('foobar');
  40. }
  41. /**
  42. * Storage marked unavailable, TTL not expired
  43. *
  44. */
  45. public function testUnavailable(): void {
  46. $this->expectException(\OCP\Files\StorageNotAvailableException::class);
  47. $this->storage->expects($this->once())
  48. ->method('getAvailability')
  49. ->willReturn(['available' => false, 'last_checked' => time()]);
  50. $this->storage->expects($this->never())
  51. ->method('test');
  52. $this->storage->expects($this->never())
  53. ->method('mkdir');
  54. $this->wrapper->mkdir('foobar');
  55. }
  56. /**
  57. * Storage marked unavailable, TTL expired
  58. */
  59. public function testUnavailableRecheck(): void {
  60. $this->storage->expects($this->once())
  61. ->method('getAvailability')
  62. ->willReturn(['available' => false, 'last_checked' => 0]);
  63. $this->storage->expects($this->once())
  64. ->method('test')
  65. ->willReturn(true);
  66. $this->storage->expects($this->exactly(2))
  67. ->method('setAvailability')
  68. ->withConsecutive(
  69. [$this->equalTo(false)], // prevents concurrent rechecks
  70. [$this->equalTo(true)] // sets correct availability
  71. );
  72. $this->storage->expects($this->once())
  73. ->method('mkdir');
  74. $this->wrapper->mkdir('foobar');
  75. }
  76. /**
  77. * Storage marked available, but throws StorageNotAvailableException
  78. *
  79. */
  80. public function testAvailableThrowStorageNotAvailable(): void {
  81. $this->expectException(\OCP\Files\StorageNotAvailableException::class);
  82. $this->storage->expects($this->once())
  83. ->method('getAvailability')
  84. ->willReturn(['available' => true, 'last_checked' => 0]);
  85. $this->storage->expects($this->never())
  86. ->method('test');
  87. $this->storage->expects($this->once())
  88. ->method('mkdir')
  89. ->will($this->throwException(new StorageNotAvailableException()));
  90. $this->storageCache->expects($this->once())
  91. ->method('setAvailability')
  92. ->with($this->equalTo(false));
  93. $this->wrapper->mkdir('foobar');
  94. }
  95. /**
  96. * Storage available, but call fails
  97. * Method failure does not indicate storage unavailability
  98. */
  99. public function testAvailableFailure(): void {
  100. $this->storage->expects($this->once())
  101. ->method('getAvailability')
  102. ->willReturn(['available' => true, 'last_checked' => 0]);
  103. $this->storage->expects($this->never())
  104. ->method('test');
  105. $this->storage->expects($this->once())
  106. ->method('mkdir')
  107. ->willReturn(false);
  108. $this->storage->expects($this->never())
  109. ->method('setAvailability');
  110. $this->wrapper->mkdir('foobar');
  111. }
  112. /**
  113. * Storage available, but throws exception
  114. * Standard exception does not indicate storage unavailability
  115. *
  116. */
  117. public function testAvailableThrow(): void {
  118. $this->expectException(\Exception::class);
  119. $this->storage->expects($this->once())
  120. ->method('getAvailability')
  121. ->willReturn(['available' => true, 'last_checked' => 0]);
  122. $this->storage->expects($this->never())
  123. ->method('test');
  124. $this->storage->expects($this->once())
  125. ->method('mkdir')
  126. ->will($this->throwException(new \Exception()));
  127. $this->storage->expects($this->never())
  128. ->method('setAvailability');
  129. $this->wrapper->mkdir('foobar');
  130. }
  131. }