AvailabilityTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Test\Files\Storage\Wrapper;
  22. use OC\Files\Cache\Storage as StorageCache;
  23. use OC\Files\Storage\Temporary;
  24. use OC\Files\Storage\Wrapper\Availability;
  25. use OCP\Files\StorageNotAvailableException;
  26. class AvailabilityTest extends \Test\TestCase {
  27. /** @var \PHPUnit\Framework\MockObject\MockObject|StorageCache */
  28. protected $storageCache;
  29. /** @var \PHPUnit\Framework\MockObject\MockObject|Temporary */
  30. protected $storage;
  31. /** @var Availability */
  32. protected $wrapper;
  33. protected function setUp(): void {
  34. parent::setUp();
  35. $this->storageCache = $this->createMock(StorageCache::class);
  36. $this->storage = $this->createMock(Temporary::class);
  37. $this->storage->expects($this->any())
  38. ->method('getStorageCache')
  39. ->willReturn($this->storageCache);
  40. $this->wrapper = new Availability(['storage' => $this->storage]);
  41. }
  42. /**
  43. * Storage is available
  44. */
  45. public function testAvailable() {
  46. $this->storage->expects($this->once())
  47. ->method('getAvailability')
  48. ->willReturn(['available' => true, 'last_checked' => 0]);
  49. $this->storage->expects($this->never())
  50. ->method('test');
  51. $this->storage->expects($this->once())
  52. ->method('mkdir');
  53. $this->wrapper->mkdir('foobar');
  54. }
  55. /**
  56. * Storage marked unavailable, TTL not expired
  57. *
  58. */
  59. public function testUnavailable() {
  60. $this->expectException(\OCP\Files\StorageNotAvailableException::class);
  61. $this->storage->expects($this->once())
  62. ->method('getAvailability')
  63. ->willReturn(['available' => false, 'last_checked' => time()]);
  64. $this->storage->expects($this->never())
  65. ->method('test');
  66. $this->storage->expects($this->never())
  67. ->method('mkdir');
  68. $this->wrapper->mkdir('foobar');
  69. }
  70. /**
  71. * Storage marked unavailable, TTL expired
  72. */
  73. public function testUnavailableRecheck() {
  74. $this->storage->expects($this->once())
  75. ->method('getAvailability')
  76. ->willReturn(['available' => false, 'last_checked' => 0]);
  77. $this->storage->expects($this->once())
  78. ->method('test')
  79. ->willReturn(true);
  80. $this->storage->expects($this->exactly(2))
  81. ->method('setAvailability')
  82. ->withConsecutive(
  83. [$this->equalTo(false)], // prevents concurrent rechecks
  84. [$this->equalTo(true)] // sets correct availability
  85. );
  86. $this->storage->expects($this->once())
  87. ->method('mkdir');
  88. $this->wrapper->mkdir('foobar');
  89. }
  90. /**
  91. * Storage marked available, but throws StorageNotAvailableException
  92. *
  93. */
  94. public function testAvailableThrowStorageNotAvailable() {
  95. $this->expectException(\OCP\Files\StorageNotAvailableException::class);
  96. $this->storage->expects($this->once())
  97. ->method('getAvailability')
  98. ->willReturn(['available' => true, 'last_checked' => 0]);
  99. $this->storage->expects($this->never())
  100. ->method('test');
  101. $this->storage->expects($this->once())
  102. ->method('mkdir')
  103. ->will($this->throwException(new StorageNotAvailableException()));
  104. $this->storageCache->expects($this->once())
  105. ->method('setAvailability')
  106. ->with($this->equalTo(false));
  107. $this->wrapper->mkdir('foobar');
  108. }
  109. /**
  110. * Storage available, but call fails
  111. * Method failure does not indicate storage unavailability
  112. */
  113. public function testAvailableFailure() {
  114. $this->storage->expects($this->once())
  115. ->method('getAvailability')
  116. ->willReturn(['available' => true, 'last_checked' => 0]);
  117. $this->storage->expects($this->never())
  118. ->method('test');
  119. $this->storage->expects($this->once())
  120. ->method('mkdir')
  121. ->willReturn(false);
  122. $this->storage->expects($this->never())
  123. ->method('setAvailability');
  124. $this->wrapper->mkdir('foobar');
  125. }
  126. /**
  127. * Storage available, but throws exception
  128. * Standard exception does not indicate storage unavailability
  129. *
  130. */
  131. public function testAvailableThrow() {
  132. $this->expectException(\Exception::class);
  133. $this->storage->expects($this->once())
  134. ->method('getAvailability')
  135. ->willReturn(['available' => true, 'last_checked' => 0]);
  136. $this->storage->expects($this->never())
  137. ->method('test');
  138. $this->storage->expects($this->once())
  139. ->method('mkdir')
  140. ->will($this->throwException(new \Exception()));
  141. $this->storage->expects($this->never())
  142. ->method('setAvailability');
  143. $this->wrapper->mkdir('foobar');
  144. }
  145. }