2015-01-24 00:53:21 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Robin McCorkell <rmccorkell@karoshi.org.uk>
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
2016-05-19 09:41:01 +03:00
|
|
|
|
2015-01-24 00:53:21 +03:00
|
|
|
namespace Test\Files\Storage\Wrapper;
|
|
|
|
|
2019-08-26 14:47:35 +03:00
|
|
|
use OC\Files\Cache\Storage as StorageCache;
|
|
|
|
use OC\Files\Storage\Temporary;
|
|
|
|
use OC\Files\Storage\Wrapper\Availability;
|
|
|
|
use OCP\Files\StorageNotAvailableException;
|
|
|
|
|
2016-05-20 16:38:20 +03:00
|
|
|
class AvailabilityTest extends \Test\TestCase {
|
2019-08-26 14:47:35 +03:00
|
|
|
|
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject|StorageCache */
|
|
|
|
protected $storageCache;
|
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject|Temporary */
|
|
|
|
protected $storage;
|
|
|
|
/** @var Availability */
|
|
|
|
protected $wrapper;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2019-08-26 14:47:35 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->storageCache = $this->createMock(StorageCache::class);
|
|
|
|
|
|
|
|
$this->storage = $this->createMock(Temporary::class);
|
|
|
|
$this->storage->expects($this->any())
|
|
|
|
->method('getStorageCache')
|
|
|
|
->willReturn($this->storageCache);
|
|
|
|
|
|
|
|
$this->wrapper = new Availability(['storage' => $this->storage]);
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Storage is available
|
|
|
|
*/
|
|
|
|
public function testAvailable() {
|
2019-08-26 14:47:35 +03:00
|
|
|
$this->storage->expects($this->once())
|
2015-01-24 00:53:21 +03:00
|
|
|
->method('getAvailability')
|
|
|
|
->willReturn(['available' => true, 'last_checked' => 0]);
|
2019-08-26 14:47:35 +03:00
|
|
|
$this->storage->expects($this->never())
|
2015-01-24 00:53:21 +03:00
|
|
|
->method('test');
|
2019-08-26 14:47:35 +03:00
|
|
|
$this->storage->expects($this->once())
|
2015-01-24 00:53:21 +03:00
|
|
|
->method('mkdir');
|
|
|
|
|
2019-08-26 14:47:35 +03:00
|
|
|
$this->wrapper->mkdir('foobar');
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Storage marked unavailable, TTL not expired
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function testUnavailable() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\OCP\Files\StorageNotAvailableException::class);
|
|
|
|
|
2019-08-26 14:47:35 +03:00
|
|
|
$this->storage->expects($this->once())
|
2015-01-24 00:53:21 +03:00
|
|
|
->method('getAvailability')
|
|
|
|
->willReturn(['available' => false, 'last_checked' => time()]);
|
2019-08-26 14:47:35 +03:00
|
|
|
$this->storage->expects($this->never())
|
2015-01-24 00:53:21 +03:00
|
|
|
->method('test');
|
2019-08-26 14:47:35 +03:00
|
|
|
$this->storage->expects($this->never())
|
2015-01-24 00:53:21 +03:00
|
|
|
->method('mkdir');
|
|
|
|
|
2019-08-26 14:47:35 +03:00
|
|
|
$this->wrapper->mkdir('foobar');
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Storage marked unavailable, TTL expired
|
|
|
|
*/
|
|
|
|
public function testUnavailableRecheck() {
|
2019-08-26 14:47:35 +03:00
|
|
|
$this->storage->expects($this->once())
|
2015-01-24 00:53:21 +03:00
|
|
|
->method('getAvailability')
|
|
|
|
->willReturn(['available' => false, 'last_checked' => 0]);
|
2019-08-26 14:47:35 +03:00
|
|
|
$this->storage->expects($this->once())
|
2015-01-24 00:53:21 +03:00
|
|
|
->method('test')
|
|
|
|
->willReturn(true);
|
2019-08-26 14:47:35 +03:00
|
|
|
$this->storage->expects($this->exactly(2))
|
2015-01-24 00:53:21 +03:00
|
|
|
->method('setAvailability')
|
2016-04-27 00:30:09 +03:00
|
|
|
->withConsecutive(
|
|
|
|
[$this->equalTo(false)], // prevents concurrent rechecks
|
|
|
|
[$this->equalTo(true)] // sets correct availability
|
|
|
|
);
|
2019-08-26 14:47:35 +03:00
|
|
|
$this->storage->expects($this->once())
|
2015-01-24 00:53:21 +03:00
|
|
|
->method('mkdir');
|
|
|
|
|
2019-08-26 14:47:35 +03:00
|
|
|
$this->wrapper->mkdir('foobar');
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Storage marked available, but throws StorageNotAvailableException
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function testAvailableThrowStorageNotAvailable() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\OCP\Files\StorageNotAvailableException::class);
|
|
|
|
|
2019-08-26 14:47:35 +03:00
|
|
|
$this->storage->expects($this->once())
|
2015-01-24 00:53:21 +03:00
|
|
|
->method('getAvailability')
|
|
|
|
->willReturn(['available' => true, 'last_checked' => 0]);
|
2019-08-26 14:47:35 +03:00
|
|
|
$this->storage->expects($this->never())
|
2015-01-24 00:53:21 +03:00
|
|
|
->method('test');
|
2019-08-26 14:47:35 +03:00
|
|
|
$this->storage->expects($this->once())
|
2015-01-24 00:53:21 +03:00
|
|
|
->method('mkdir')
|
2019-08-26 14:47:35 +03:00
|
|
|
->will($this->throwException(new StorageNotAvailableException()));
|
|
|
|
$this->storageCache->expects($this->once())
|
2015-01-24 00:53:21 +03:00
|
|
|
->method('setAvailability')
|
|
|
|
->with($this->equalTo(false));
|
|
|
|
|
2019-08-26 14:47:35 +03:00
|
|
|
$this->wrapper->mkdir('foobar');
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Storage available, but call fails
|
|
|
|
* Method failure does not indicate storage unavailability
|
|
|
|
*/
|
|
|
|
public function testAvailableFailure() {
|
2019-08-26 14:47:35 +03:00
|
|
|
$this->storage->expects($this->once())
|
2015-01-24 00:53:21 +03:00
|
|
|
->method('getAvailability')
|
|
|
|
->willReturn(['available' => true, 'last_checked' => 0]);
|
2019-08-26 14:47:35 +03:00
|
|
|
$this->storage->expects($this->never())
|
2015-01-24 00:53:21 +03:00
|
|
|
->method('test');
|
2019-08-26 14:47:35 +03:00
|
|
|
$this->storage->expects($this->once())
|
2015-01-24 00:53:21 +03:00
|
|
|
->method('mkdir')
|
|
|
|
->willReturn(false);
|
2019-08-26 14:47:35 +03:00
|
|
|
$this->storage->expects($this->never())
|
2015-01-24 00:53:21 +03:00
|
|
|
->method('setAvailability');
|
|
|
|
|
2019-08-26 14:47:35 +03:00
|
|
|
$this->wrapper->mkdir('foobar');
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Storage available, but throws exception
|
|
|
|
* Standard exception does not indicate storage unavailability
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function testAvailableThrow() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Exception::class);
|
|
|
|
|
2019-08-26 14:47:35 +03:00
|
|
|
$this->storage->expects($this->once())
|
2015-01-24 00:53:21 +03:00
|
|
|
->method('getAvailability')
|
|
|
|
->willReturn(['available' => true, 'last_checked' => 0]);
|
2019-08-26 14:47:35 +03:00
|
|
|
$this->storage->expects($this->never())
|
2015-01-24 00:53:21 +03:00
|
|
|
->method('test');
|
2019-08-26 14:47:35 +03:00
|
|
|
$this->storage->expects($this->once())
|
2015-01-24 00:53:21 +03:00
|
|
|
->method('mkdir')
|
|
|
|
->will($this->throwException(new \Exception()));
|
2019-08-26 14:47:35 +03:00
|
|
|
$this->storage->expects($this->never())
|
2015-01-24 00:53:21 +03:00
|
|
|
->method('setAvailability');
|
|
|
|
|
2019-08-26 14:47:35 +03:00
|
|
|
$this->wrapper->mkdir('foobar');
|
2015-01-24 00:53:21 +03:00
|
|
|
}
|
|
|
|
}
|