Skip to content

Commit

Permalink
initial storage system tests (#231)
Browse files Browse the repository at this point in the history
* initial storage system tests

* Fix capitalization of namespace (#233)

* initial pubsub system tests (#230)

* initial datastore system tests (#228)

* initial datastore system tests

* Fix capitalization of namespace (#233)

* initial pubsub system tests (#230)
  • Loading branch information
dwsupplee authored and jdpedrie committed Nov 8, 2016
1 parent 7f63a7f commit 3508b73
Show file tree
Hide file tree
Showing 8 changed files with 128,625 additions and 0 deletions.
73 changes: 73 additions & 0 deletions tests/system/Storage/ManageAclTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\Tests\System\Storage;

use Google\Cloud\Storage\Acl;
use Google\Cloud\Exception\NotFoundException;

class ManageAclTest extends StorageTestCase
{
public function testManageBucketAcl()
{
$kind = 'storage#bucketAccessControl';
$this->assertAcl(self::$bucket->acl(), $kind);
}

public function testManageDefaultObjectAcl()
{
$kind = 'storage#objectAccessControl';
$this->assertAcl(self::$bucket->defaultAcl(), $kind);
}

public function testManageObjectAcl()
{
$kind = 'storage#objectAccessControl';
$this->assertAcl(self::$object->acl(), $kind);
}

private function assertAcl($acl, $kind)
{
$user = '[email protected]';
$found = true;
$accessItems = $acl->get();

foreach ($accessItems as $item) {
$this->assertEquals($kind, $item['kind']);
}

$acl->add($user, Acl::ROLE_READER);
$item = $acl->get(['entity' => $user]);
$this->assertEquals($kind, $item['kind']);
$this->assertEquals(Acl::ROLE_READER, $item['role']);

$acl->update($user, Acl::ROLE_OWNER);
$item = $acl->get(['entity' => $user]);
$this->assertEquals($kind, $item['kind']);
$this->assertEquals(Acl::ROLE_OWNER, $item['role']);

$acl->delete($user);

try {
$acl->get(['entity' => $user]);
} catch (NotFoundException $ex) {
$found = false;
}

$this->assertFalse($found);
}
}
86 changes: 86 additions & 0 deletions tests/system/Storage/ManageBucketsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\Tests\System\Storage;

class ManageBucketsTest extends StorageTestCase
{
public function testListsBuckets()
{
$foundBuckets = [];
$bucketsToCreate = [
uniqid(self::TESTING_PREFIX),
uniqid(self::TESTING_PREFIX)
];

foreach ($bucketsToCreate as $bucketToCreate) {
self::$deletionQueue[] = self::$client->createBucket($bucketToCreate);
}

$buckets = self::$client->buckets(['prefix' => self::TESTING_PREFIX]);

foreach ($buckets as $bucket) {
foreach ($bucketsToCreate as $key => $bucketToCreate) {
if ($bucket->name() === $bucketToCreate) {
$foundBuckets[$key] = $bucket->name();
}
}
}

$this->assertEquals($bucketsToCreate, $foundBuckets);
}

public function testCreatesBucket()
{
$name = uniqid(self::TESTING_PREFIX);
$options = [
'location' => 'ASIA',
'storageClass' => 'NEARLINE',
'versioning' => [
'enabled' => true
]
];
$this->assertFalse(self::$client->bucket($name)->exists());

$bucket = self::$client->createBucket($name, $options);
self::$deletionQueue[] = $bucket;

$this->assertTrue(self::$client->bucket($name)->exists());
$this->assertEquals($name, $bucket->name());
$this->assertEquals($options['location'], $bucket->info()['location']);
$this->assertEquals($options['storageClass'], $bucket->info()['storageClass']);
$this->assertEquals($options['versioning'], $bucket->info()['versioning']);
}

public function testUpdateBucket()
{
$options = [
'website' => [
'mainPageSuffix' => 'index.html',
'notFoundPage' => '404.html'
]
];
$info = self::$bucket->update($options);

$this->assertEquals($options['website'], $info['website']);
}

public function testReloadBucket()
{
$this->assertEquals('storage#bucket', self::$bucket->reload()['kind']);
}
}
168 changes: 168 additions & 0 deletions tests/system/Storage/ManageObjectsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\Tests\System\Storage;

use Psr\Http\Message\StreamInterface;

class ManageObjectsTest extends StorageTestCase
{
public function testListsObjects()
{
$foundObjects = [];
$objectsToCreate = [
uniqid(self::TESTING_PREFIX),
uniqid(self::TESTING_PREFIX)
];

foreach ($objectsToCreate as $objectToCreate) {
self::$deletionQueue[] = self::$bucket->upload('somedata', ['name' => $objectToCreate]);
}

$objects = self::$bucket->objects(['prefix' => self::TESTING_PREFIX]);

foreach ($objects as $object) {
foreach ($objectsToCreate as $key => $objectToCreate) {
if ($object->name() === $objectToCreate) {
$foundObjects[$key] = $object->name();
}
}
}

$this->assertEquals($objectsToCreate, $foundObjects);
}

public function testObjectExists()
{
$object = self::$bucket->upload('somedata', ['name' => uniqid(self::TESTING_PREFIX)]);
$this->assertTrue($object->exists());
$object->delete();
$this->assertFalse($object->exists());
}

public function testUpdateObject()
{
$metadata = [
'metadata' => [
'location' => 'test'
]
];
$info = self::$object->update($metadata);

$this->assertEquals($metadata['metadata'], $info['metadata']);
}

public function testCopiesObject()
{
$name = uniqid(self::TESTING_PREFIX);
$copiedObject = self::$object->copy(self::$bucket, [
'name' => $name
]);

$this->assertEquals($name, $copiedObject->name());

return $copiedObject;
}

/**
* @depends testCopiesObject
*/
public function testRenamesObject($object)
{
$name = uniqid(self::TESTING_PREFIX);
$newObject = $object->rename($name);
$this->assertFalse($object->exists());

$this->assertEquals($name, $newObject->name());
return $newObject;
}

/**
* @depends testRenamesObject
*/
public function testComposeObjects($object)
{
self::$deletionQueue[] = $object;
$expectedContent = $object->downloadAsString();
$expectedContent .= self::$object->downloadAsString();
$name = uniqid(self::TESTING_PREFIX) . '.txt';
$composedObject = self::$bucket->compose(
[$object, self::$object],
$name
);
self::$deletionQueue[] = $composedObject;

$this->assertEquals($name, $composedObject->name());
$this->assertEquals($expectedContent, $composedObject->downloadAsString());
}

public function testRotatesCustomerSuppliedEncrpytion()
{
$data = 'somedata';
$key = openssl_random_pseudo_bytes(32);
$sha = hash('SHA256', $key, true);
$options = [
'name' => uniqid(self::TESTING_PREFIX),
'encryptionKey' => $key,
'encryptionKeySHA256' => $sha
];
$object = self::$bucket->upload($data, $options);
self::$deletionQueue[] = $object;

$dkey = openssl_random_pseudo_bytes(32);
$dsha = hash('SHA256', $dkey, true);
$rewriteOptions = [
'name' => uniqid(self::TESTING_PREFIX),
'encryptionKey' => $key,
'encryptionKeySHA256' => $sha,
'destinationEncryptionKey' => $dkey,
'destinationEncryptionKeySHA256' => $dsha
];

$rewrittenObject = $object->rewrite(self::$bucket, $rewriteOptions);

$this->assertEquals(base64_encode($dsha), $rewrittenObject->info()['customerEncryption']['keySha256']);
$rewrittenObject->delete();
}

public function testDownloadsAsString()
{
$content = self::$object->downloadAsString();

$this->assertTrue(is_string($content));
}

public function testDownloadsAsStream()
{
$content = self::$object->downloadAsStream();

$this->assertInstanceOf(StreamInterface::class, $content);
}

public function testDownloadsToFile()
{
$contents = self::$object->downloadAsString();
$stream = self::$object->downloadToFile('php://temp');

$this->assertEquals($contents, (string) $stream);
}

public function testReloadObject()
{
$this->assertEquals('storage#object', self::$object->reload()['kind']);
}
}
66 changes: 66 additions & 0 deletions tests/system/Storage/StorageTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\Tests\System\Storage;

use Google\Cloud\ExponentialBackoff;
use Google\Cloud\Storage\StorageClient;

class StorageTestCase extends \PHPUnit_Framework_TestCase
{
const TESTING_PREFIX = 'gcloud_testing_';

protected static $bucket;
protected static $client;
protected static $deletionQueue = [];
protected static $object;
private static $hasSetUp = false;

public static function setUpBeforeClass()
{
if (self::$hasSetUp) {
return;
}

self::$client = new StorageClient([
'keyFilePath' => getenv('GOOGLE_CLOUD_PHP_TESTS_KEY_PATH')
]);
self::$bucket = self::$client->createBucket(uniqid(self::TESTING_PREFIX));
self::$object = self::$bucket->upload('somedata', ['name' => uniqid(self::TESTING_PREFIX)]);
self::$hasSetUp = true;
}

public static function tearDownFixtures()
{
if (!self::$hasSetUp) {
return;
}

self::$deletionQueue[] = self::$object;
self::$deletionQueue[] = self::$bucket;

$backoff = new ExponentialBackoff(8);

foreach (self::$deletionQueue as $item) {
$backoff->execute(function () use ($item) {
$item->delete();
});
}
}
}


Loading

0 comments on commit 3508b73

Please sign in to comment.