Updated all unit tests to use IdentityServices.

This commit is contained in:
Matt Butcher 2012-01-25 17:39:50 -06:00
parent ff8bed1d56
commit 4d02e28529
6 changed files with 126 additions and 44 deletions

View File

@ -19,6 +19,11 @@ class TestCase extends \PHPUnit_Framework_TestCase {
public static $ostore = NULL;
/**
* The IdentityServices instance.
*/
public static $ident;
//public function __construct(score $score = NULL, locale $locale = NULL, adapter $adapter = NULL) {
public static function setUpBeforeClass() {
@ -53,16 +58,66 @@ class TestCase extends \PHPUnit_Framework_TestCase {
protected $containerFixture = NULL;
/**
* Authenticate to a Swift account.
* @deprecated
*/
protected function swiftAuth() {
if (empty(self::$ostore)) {
$user = self::$settings['hpcloud.swift.account'];
$key = self::$settings['hpcloud.swift.key'];
$url = self::$settings['hpcloud.swift.url'];
$user = self::$settings['hpcloud.swift.account'];
$key = self::$settings['hpcloud.swift.key'];
$url = self::$settings['hpcloud.swift.url'];
self::$ostore = \HPCloud\Storage\ObjectStorage::newFromSwiftAuth($user, $key, $url);
return \HPCloud\Storage\ObjectStorage::newFromSwiftAuth($user, $key, $url);
}
/**
* Get a handle to an IdentityServices object.
*
* Authentication is performed, and the returned
* service has its tenant ID set already.
*
* @code
* <?php
* // Get the current token.
* $this->identity()->token();
* ?>
* @endcode
*/
protected function identity($reset = FALSE) {
if ($reset || empty(self::$ident)) {
$user = self::conf('hpcloud.identity.username');
$pass = self::conf('hpcloud.identity.password');
$tenantId = self::conf('hpcloud.identity.tenantId');
$url = self::conf('hpcloud.identity.url');
$is = new \HPCloud\Services\IdentityServices($url);
$token = $is->authenticateAsUser($user, $pass, $tenantId);
self::$ident = $is;
}
return self::$ident;
}
protected function objectStore($reset = FALSE) {
if ($reset || empty(self::$ostore)) {
$ident = $this->identity($reset);
$services = $ident->serviceCatalog(\HPCloud\Storage\ObjectStorage::SERVICE_TYPE);
if (empty($services)) {
throw new \Exception('No object-store service found.');
}
//$serviceURL = $services[0]['endpoints'][0]['adminURL'];
$serviceURL = $services[0]['endpoints'][0]['publicURL'];
$objStore = new \HPCloud\Storage\ObjectStorage($ident->token(), $serviceURL);
self::$ostore = $objStore;
}
@ -75,7 +130,7 @@ class TestCase extends \PHPUnit_Framework_TestCase {
protected function containerFixture() {
if (empty($this->containerFixture)) {
$store = $this->swiftAuth();
$store = $this->objectStore();
$cname = self::$settings['hpcloud.swift.container'];
try {
@ -107,7 +162,7 @@ class TestCase extends \PHPUnit_Framework_TestCase {
* The name of the container.
*/
protected function eradicateContainer($cname) {
$store = $this->swiftAuth();
$store = $this->objectStore();
try {
$container = $store->container($cname);
}
@ -133,7 +188,7 @@ class TestCase extends \PHPUnit_Framework_TestCase {
* This should be called in any method that uses containerFixture().
*/
protected function destroyContainerFixture() {
$store = $this->swiftAuth();
$store = $this->objectStore();
$cname = self::$settings['hpcloud.swift.container'];
try {

View File

@ -67,7 +67,7 @@ class ObjectTest extends \HPCloud\Tests\TestCase {
public function testSwiftAuth() {
// We know that the object works, so now we test whether it can
// communicate with Swift.
$storage = $this->swiftAuth();
$storage = $this->objectStore();
$info = $storage->accountInfo();

View File

@ -46,7 +46,7 @@ class ContainerTest extends \HPCloud\Tests\TestCase {
const FNAME = 'testSave';
const FCONTENT = 'This is a test.';
const FTYPE = 'text/plain';
const FTYPE = 'application/x-monkey-file';
public function testSave() {
@ -334,7 +334,7 @@ class ContainerTest extends \HPCloud\Tests\TestCase {
public function testCopyAcrossContainers() {
// Create a new container.
$store = $this->swiftAuth();
$store = $this->objectStore();
$cname = self::$settings['hpcloud.swift.container'] . 'COPY';
if ($store->hasContainer($cname)) {
$this->eradicateContainer($cname);
@ -377,7 +377,7 @@ class ContainerTest extends \HPCloud\Tests\TestCase {
}
public function testAcl() {
$store = $this->swiftAuth();
$store = $this->objectStore();
$cname = self::$settings['hpcloud.swift.container'] . 'PUBLIC';
if ($store->hasContainer($cname)) {

View File

@ -24,8 +24,9 @@ class ObjectStorageTest extends \HPCloud\Tests\TestCase {
/**
* Test Swift-based authentication.
* */
public function testAuthentication() {
* @group deprecated
*/
public function testSwiftAuthentication() {
$ostore = $this->swiftAuth();
@ -33,12 +34,27 @@ class ObjectStorageTest extends \HPCloud\Tests\TestCase {
$this->assertTrue(strlen($ostore->token()) > 0);
}
/**
* @group auth
*/
public function testConstructor() {
$ostore = $this->objectStore();
$this->assertInstanceOf('\HPCloud\Storage\ObjectStorage', $ostore);
$this->assertTrue(strlen($ostore->token()) > 0);
}
/**
* @group auth
* @group acl
*/
public function testCreateContainer() {
$testCollection = self::$settings['hpcloud.swift.container'];
$this->assertNotEmpty($testCollection, "Canary: container name must be in settings file.");
$store = $this->swiftAuth();
$store = $this->objectStore();//swiftAuth();
if ($store->hasContainer($testCollection)) {
$store->deleteContainer($testCollection);
@ -52,29 +68,31 @@ class ObjectStorageTest extends \HPCloud\Tests\TestCase {
}
/**
* @group auth
* @depends testCreateContainer
*/
public function testAccountInfo () {
$store = $this->swiftAuth();
$store = $this->objectStore();
$info = $store->accountInfo();
$this->assertTrue($info['count'] > 0);
$this->assertTrue($info['bytes'] > 0);
$this->assertGreaterThan(0, $info['containers']);
$this->assertGreaterThanOrEqual(0, $info['bytes']);
$this->assertGreaterThanOrEqual(0, $info['objects']);
}
/**
* @depends testCreateContainer
*/
public function testContainers() {
$store = $this->swiftAuth();
$store = $this->objectStore();
$containers = $store->containers();
$this->assertNotEmpty($containers);
//$first = array_shift($containers);
$testCollection = self::$settings['hpcloud.swift.container'];
$testCollection = self::conf('hpcloud.swift.container');
$testContainer = $containers[$testCollection];
$this->assertEquals($testCollection, $testContainer->name());
$this->assertEquals(0, $testContainer->bytes());
@ -90,7 +108,7 @@ class ObjectStorageTest extends \HPCloud\Tests\TestCase {
*/
public function testContainer() {
$testCollection = self::$settings['hpcloud.swift.container'];
$store = $this->swiftAuth();
$store = $this->objectStore();
$container = $store->container($testCollection);
@ -108,7 +126,7 @@ class ObjectStorageTest extends \HPCloud\Tests\TestCase {
*/
public function testHasContainer() {
$testCollection = self::$settings['hpcloud.swift.container'];
$store = $this->swiftAuth();
$store = $this->objectStore();
$this->assertTrue($store->hasContainer($testCollection));
$this->assertFalse($store->hasContainer('nihil'));
@ -120,7 +138,7 @@ class ObjectStorageTest extends \HPCloud\Tests\TestCase {
public function testDeleteContainer() {
$testCollection = self::$settings['hpcloud.swift.container'];
$store = $this->swiftAuth();
$store = $this->objectStore();
//$ret = $store->createContainer($testCollection);
//$this->assertTrue($store->hasContainer($testCollection));
@ -142,7 +160,7 @@ class ObjectStorageTest extends \HPCloud\Tests\TestCase {
$this->assertNotEmpty($testCollection);
$store = $this->swiftAuth();
$store = $this->objectStore();
$store->createContainer($testCollection);
$container = $store->container($testCollection);
@ -168,10 +186,11 @@ class ObjectStorageTest extends \HPCloud\Tests\TestCase {
/**
* @depends testCreateContainer
* @group acl
*/
public function testCreateContainerPublic() {
$testCollection = self::$settings['hpcloud.swift.container'] . 'PUBLIC';
$store = $this->swiftAuth();
$store = $this->objectStore();
if ($store->hasContainer($testCollection)) {
$store->deleteContainer($testCollection);
}
@ -197,7 +216,7 @@ class ObjectStorageTest extends \HPCloud\Tests\TestCase {
*/
public function testChangeContainerACL() {
$testCollection = self::$settings['hpcloud.swift.container'] . 'PUBLIC';
$store = $this->swiftAuth();
$store = $this->objectStore();
if ($store->hasContainer($testCollection)) {
$store->deleteContainer($testCollection);
}

View File

@ -16,7 +16,8 @@ use \HPCloud\Storage\ObjectStorage\Container;
class RemoteObjectTest extends \HPCloud\Tests\TestCase {
const FNAME = 'RemoteObjectTest';
const FTYPE = 'text/plain';
//const FTYPE = 'text/plain; charset=UTF-8';
const FTYPE = 'application/octet-stream; charset=UTF-8';
const FCONTENT = 'Rah rah ah ah ah. Roma roma ma. Gaga oh la la.';
const FMETA_NAME = 'Foo';
const FMETA_VALUE = 'Bar';

View File

@ -50,9 +50,9 @@ class StreamWrapperTest extends \HPCloud\Tests\TestCase {
}
$params = $add + array(
'token' => self::$ostore->token(),
'swift_endpoint' => self::$ostore->url(),
);
'token' => $this->objectStore()->token(),
'swift_endpoint' => $this->objectStore()->url(),
);
$cxt = array($scheme => $params);
return stream_context_create($cxt);
@ -60,28 +60,34 @@ class StreamWrapperTest extends \HPCloud\Tests\TestCase {
/**
* This performs authentication via context.
*
* UPDATE: This now users IdentityServices instead of deprecated
* swauth.
*/
protected function authSwiftContext($add = array(), $scheme = NULL) {
$cname = self::$settings['hpcloud.swift.container'];
$account = self::$settings['hpcloud.swift.account'];
$key = self::$settings['hpcloud.swift.key'];
$baseURL = self::$settings['hpcloud.swift.url'];
$account = self::$settings['hpcloud.identity.account'];
$key = self::$settings['hpcloud.identity.secret'];
$tenant = self::$settings['hpcloud.identity.tenantId'];
$baseURL = self::$settings['hpcloud.identity.url'];
if (empty($scheme)) {
$scheme = StreamWrapper::DEFAULT_SCHEME;
}
$params = $add + array(
'account' => $account,
'key' => $key,
'endpoint' => $baseURL,
);
'account' => $account,
'key' => $key,
'endpoint' => $baseURL,
'tenantid' => $tenant,
);
$cxt = array($scheme => $params);
return stream_context_create($cxt);
}
/**
* Add additional params to the config.
*
@ -92,11 +98,12 @@ class StreamWrapperTest extends \HPCloud\Tests\TestCase {
*/
protected function addBootstrapConfig() {
$opts = array(
'account' => self::$settings['hpcloud.swift.account'],
'key' => self::$settings['hpcloud.swift.key'],
'endpoint' => self::$settings['hpcloud.swift.url'],
'token' => self::$ostore->token(),
'swift_endpoint' => self::$ostore->url(),
'account' => self::$settings['hpcloud.identity.account'],
'key' => self::$settings['hpcloud.identity.secret'],
'endpoint' => self::$settings['hpcloud.identity.url'],
'tenantit' => self::$settings['hpcloud.identity.tenantId'],
'token' => $this->objectStore()->token(),
'swift_endpoint' => $this->objectStore()->url(),
);
\HPCloud\Bootstrap::setConfiguration($opts);
@ -108,7 +115,7 @@ class StreamWrapperTest extends \HPCloud\Tests\TestCase {
$array = stream_context_get_options($cxt);
$opts = $array['swift'];
$endpoint = self::$settings['hpcloud.swift.url'];
$endpoint = self::conf('hpcloud.identity.url');
$this->assertEquals($endpoint, $opts['endpoint'], 'A UTF-8 encoding issue.');
}