Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bigtable/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@
"require": {
"google/cloud-bigtable": "^1.0.0",
"psr/cache": "^1.0"
},
"autoload-dev": {
"psr-4": {
"Google\\Cloud\\Samples\\Bigtable\\Tests\\": "test"
}
}
}
98 changes: 98 additions & 0 deletions bigtable/test/BigtableTestTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

/**
* Copyright 2019 Google LLC.
*
* 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\Samples\Bigtable\Tests;

use Exception;
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
use Google\Cloud\Bigtable\Admin\V2\BigtableTableAdminClient;
use Google\Cloud\Bigtable\Admin\V2\ColumnFamily;
use Google\Cloud\Bigtable\Admin\V2\Table;
use Google\Cloud\Bigtable\BigtableClient;
use Google\Cloud\TestUtils\TestTrait;
use Google\Cloud\TestUtils\ExponentialBackoffTrait;

trait BigtableTestTrait
{
use TestTrait;
use ExponentialBackoffTrait;

private static $instanceAdminClient;
private static $tableAdminClient;
private static $bigtableClient;
private static $instanceId;
private static $tableId;

public static function setUpBigtableVars()
{
self::checkProjectEnvVarBeforeClass();
self::$instanceAdminClient = new BigtableInstanceAdminClient();
self::$tableAdminClient = new BigtableTableAdminClient();
self::$bigtableClient = new BigtableClient([
'projectId' => self::$projectId,
]);
}

public static function createDevInstance($instanceIdPrefix)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we have this check an environment variable for an instance to use and if that is set return that instance id instead? Would make it so much easier to deal with with testing locally. Also, if that variable is used then, it shouldn't delete the instance, but should delete the table.

{
$instanceId = uniqid($instanceIdPrefix);
$output = self::runSnippet('create_dev_instance', [
self::$projectId,
$instanceId,
$instanceId,
]);

// Verify the instance was created successfully
if (false !== strpos($output, 'Error: ')) {
throw new Exception('Error creating instance: ' . $output);
}

return $instanceId;
}

public static function createTable($tableIdPrefix, $columns = [])
{
$tableId = uniqid($tableIdPrefix);

$formattedParent = self::$tableAdminClient
->instanceName(self::$projectId, self::$instanceId);

$columns = $columns ?: ['stats_summary'];
$table = (new Table())->setColumnFamilies(array_combine(
$columns,
array_fill(0, count($columns), new ColumnFamily)
));

self::$tableAdminClient->createtable(
$formattedParent,
$tableId,
$table
);

return $tableId;
}

public static function deleteBigtableInstance()
{
$instanceName = self::$instanceAdminClient->instanceName(
self::$projectId,
self::$instanceId
);
self::$instanceAdminClient->deleteInstance($instanceName);
}
}
125 changes: 73 additions & 52 deletions bigtable/test/bigtableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,55 @@

namespace Google\Cloud\Samples\Bigtable\Tests;

use Google\Cloud\Bigtable\BigtableClient;
use PHPUnit\Framework\TestCase;

use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
use Google\Cloud\Bigtable\Admin\V2\BigtableTableAdminClient;
use Google\Cloud\TestUtils\ExponentialBackoffTrait;
use Google\Cloud\Bigtable\Admin\V2\Table\View;
use Google\Cloud\TestUtils\TestTrait;
use Google\ApiCore\ApiException;
use Google\Cloud\Bigtable\Admin\V2\Table\View;
use PHPUnit\Framework\TestCase;

final class BigtableTest extends TestCase
{
use TestTrait, ExponentialBackoffTrait;
use BigtableTestTrait;

const INSTANCE_ID_PREFIX = 'php-instance-';
const CLUSTER_ID_PREFIX = 'php-cluster-';
const TABLE_ID_PREFIX = 'php-table-';
private static $instanceAdminClient;
private static $tableAdminClient;
private static $bigtableClient;
private static $instanceId;

private static $clusterId;

public static function setUpBeforeClass()
{
self::checkProjectEnvVarBeforeClass();
self::$instanceAdminClient = new BigtableInstanceAdminClient();
self::$tableAdminClient = new BigtableTableAdminClient();
self::$bigtableClient = new BigtableClient([
'projectId' => self::$projectId,
]);

self::$instanceId = uniqid(self::INSTANCE_ID_PREFIX);
self::$clusterId = uniqid(self::CLUSTER_ID_PREFIX);

self::create_production_instance(self::$projectId, self::$instanceId, self::$clusterId);
self::setUpBigtableVars();
}

public function setUp()
{
$this->useResourceExhaustedBackoff();
}

public function testCreateProductionInstance()
{
self::$instanceId = uniqid(self::INSTANCE_ID_PREFIX);
self::$clusterId = uniqid(self::CLUSTER_ID_PREFIX);

$content = self::runSnippet('create_production_instance', [
self::$projectId,
self::$instanceId,
self::$clusterId
]);

$instanceName = self::$instanceAdminClient->instanceName(
self::$projectId,
self::$instanceId
);

$this->checkInstance($instanceName);
}

/**
* @depends testCreateProductionInstance
*/
public function testCreateAndDeleteCluster()
{
// Create a new cluster as last cluster in an instance cannot be deleted
$clusterId = uniqid(self::CLUSTER_ID_PREFIX);

$content = self::runSnippet('create_cluster', [
Expand All @@ -57,7 +61,11 @@ public function testCreateAndDeleteCluster()
]);
$array = explode(PHP_EOL, $content);

$clusterName = self::$instanceAdminClient->clusterName(self::$projectId, self::$instanceId, $clusterId);
$clusterName = self::$instanceAdminClient->clusterName(
self::$projectId,
self::$instanceId,
$clusterId
);

$this->checkCluster($clusterName);

Expand Down Expand Up @@ -95,23 +103,9 @@ public function testCreateDevInstance()
$this->cleanInstance(self::$projectId, $instanceId);
}

public function testCreateProductionInstance()
{
$instanceId = uniqid(self::INSTANCE_ID_PREFIX);
$clusterId = uniqid(self::CLUSTER_ID_PREFIX);

$content = self::runSnippet('create_production_instance', [
self::$projectId,
$instanceId,
$clusterId
]);

$instanceName = self::$instanceAdminClient->instanceName(self::$projectId, $instanceId);

$this->checkInstance($instanceName);
$this->cleanInstance(self::$projectId, $instanceId);
}

/**
* @depends testCreateProductionInstance
*/
public function testListInstances()
{
$content = self::runSnippet('list_instance', [
Expand All @@ -125,6 +119,9 @@ public function testListInstances()
$this->assertContains(self::$instanceId, $array);
}

/**
* @depends testCreateProductionInstance
*/
public function testListTable()
{
$tableId = uniqid(self::TABLE_ID_PREFIX);
Expand All @@ -141,6 +138,9 @@ public function testListTable()
$this->assertContains('projects/' . self::$projectId . '/instances/' . self::$instanceId . '/tables/' . $tableId, $array);
}

/**
* @depends testCreateProductionInstance
*/
public function testListColumnFamilies()
{
$tableId = uniqid(self::TABLE_ID_PREFIX);
Expand All @@ -166,6 +166,9 @@ public function testListColumnFamilies()
$this->assertContains('{"gcRule":{"union":{"rules":[{"maxNumVersions":2},{"maxAge":"432000s"}]}}}', $array);
}

/**
* @depends testCreateProductionInstance
*/
public function testListInstanceClusters()
{
$content = self::runSnippet('list_instance_clusters', [
Expand All @@ -179,7 +182,10 @@ public function testListInstanceClusters()
$this->assertContains('projects/' . self::$projectId . '/instances/' . self::$instanceId . '/clusters/' . self::$clusterId, $array);
}

public function testcreate_table()
/**
* @depends testCreateProductionInstance
*/
public function testCreateTable()
{
$tableId = uniqid(self::TABLE_ID_PREFIX);

Expand All @@ -194,6 +200,9 @@ public function testcreate_table()
$this->checkTable($tableName);
}

/**
* @depends testCreateProductionInstance
*/
public function testCreateFamilyGcUnion()
{
$tableId = uniqid(self::TABLE_ID_PREFIX);
Expand Down Expand Up @@ -226,6 +235,9 @@ public function testCreateFamilyGcUnion()
$this->checkRule($tableName, 'cf3', $gcRuleCompare);
}

/**
* @depends testCreateProductionInstance
*/
public function testCreateFamilyGcNested()
{
$tableId = uniqid(self::TABLE_ID_PREFIX);
Expand Down Expand Up @@ -267,6 +279,9 @@ public function testCreateFamilyGcNested()
$this->checkRule($tableName, 'cf5', $gcRuleCompare);
}

/**
* @depends testCreateProductionInstance
*/
public function testCreateFamilyGcMaxVersions()
{
$tableId = uniqid(self::TABLE_ID_PREFIX);
Expand All @@ -290,6 +305,9 @@ public function testCreateFamilyGcMaxVersions()
$this->checkRule($tableName, 'cf2', $gcRuleCompare);
}

/**
* @depends testCreateProductionInstance
*/
public function testCreateFamilyGcMaxAge()
{
$tableId = uniqid(self::TABLE_ID_PREFIX);
Expand All @@ -313,6 +331,9 @@ public function testCreateFamilyGcMaxAge()
$this->checkRule($tableName, 'cf1', $gcRuleCompare);
}

/**
* @depends testCreateProductionInstance
*/
public function testCreateFamilyGcIntersection()
{
$tableId = uniqid(self::TABLE_ID_PREFIX);
Expand Down Expand Up @@ -345,6 +366,9 @@ public function testCreateFamilyGcIntersection()
$this->checkRule($tableName, 'cf4', $gcRuleCompare);
}

/**
* @depends testCreateProductionInstance
*/
public function testDeleteTable()
{
$tableId = uniqid(self::TABLE_ID_PREFIX);
Expand All @@ -369,6 +393,9 @@ public function testDeleteTable()
}
}

/**
* @depends testCreateProductionInstance
*/
public function testHelloWorld()
{
$this->requireGrpc();
Expand All @@ -395,15 +422,9 @@ public function testHelloWorld()
$this->assertContains(sprintf('Deleted %s table.', $tableId), $array);
}

private static function create_production_instance($projectId, $instanceId, $clusterId)
{
$content = self::runSnippet('create_production_instance', [
$projectId,
$instanceId,
$clusterId
]);
}

/**
* @depends testCreateProductionInstance
*/
public function testDeleteInstance()
{
$instanceName = self::$instanceAdminClient->instanceName(self::$projectId, self::$instanceId);
Expand Down
Loading