|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Google Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +namespace Google\Cloud\Bigtable\Tests\System; |
| 19 | + |
| 20 | +use Google\ApiCore\ApiException; |
| 21 | +use Google\Cloud\Bigtable\Admin\V2\Client\BigtableInstanceAdminClient; |
| 22 | +use Google\Cloud\Bigtable\Admin\V2\Client\BigtableTableAdminClient; |
| 23 | +use Google\Cloud\Bigtable\Admin\V2\Cluster; |
| 24 | +use Google\Cloud\Bigtable\Admin\V2\ColumnFamily; |
| 25 | +use Google\Cloud\Bigtable\Admin\V2\CreateInstanceRequest; |
| 26 | +use Google\Cloud\Bigtable\Admin\V2\CreateTableRequest; |
| 27 | +use Google\Cloud\Bigtable\Admin\V2\DeleteInstanceRequest; |
| 28 | +use Google\Cloud\Bigtable\Admin\V2\DeleteTableRequest; |
| 29 | +use Google\Cloud\Bigtable\Admin\V2\GetInstanceRequest; |
| 30 | +use Google\Cloud\Bigtable\Admin\V2\GetTableRequest; |
| 31 | +use Google\Cloud\Bigtable\Admin\V2\Instance; |
| 32 | +use Google\Cloud\Bigtable\Admin\V2\Table; |
| 33 | +use Google\Cloud\Bigtable\BigtableClient; |
| 34 | +use Google\Cloud\Bigtable\Mutations; |
| 35 | +use Google\Cloud\Bigtable\V2\RowFilter; |
| 36 | +use Google\Cloud\Core\Testing\System\SystemTestCase; |
| 37 | + |
| 38 | +class UniverseDomainTest extends SystemTestCase |
| 39 | +{ |
| 40 | + private static $instanceAdminClient; |
| 41 | + private static $tableAdminClient; |
| 42 | + private static $bigtableClient; |
| 43 | + private static $tableId; |
| 44 | + private static $instanceId; |
| 45 | + private static $clusterId; |
| 46 | + private static $projectId; |
| 47 | + private static $locationId; |
| 48 | + |
| 49 | + /** |
| 50 | + * @beforeClass |
| 51 | + */ |
| 52 | + public static function setUpTestFixtures(): void |
| 53 | + { |
| 54 | + if (!$keyFilePath = getenv('GOOGLE_CLOUD_PHP_TESTS_UNIVERSE_DOMAIN_KEY_PATH')) { |
| 55 | + self::markTestSkipped('Set GOOGLE_CLOUD_PHP_TESTS_UNIVERSE_DOMAIN_KEY_PATH to run system tests'); |
| 56 | + } |
| 57 | + |
| 58 | + // This can be found for your universe by running "gcloud compute zones list" |
| 59 | + if (!$locationId = getenv('GOOGLE_CLOUD_PHP_TESTS_UNIVERSE_DOMAIN_LOCATION')) { |
| 60 | + self::markTestSkipped('Set GOOGLE_CLOUD_PHP_TESTS_UNIVERSE_DOMAIN_LOCATION to run system tests'); |
| 61 | + } |
| 62 | + |
| 63 | + if (!$credentials = json_decode(file_get_contents($keyFilePath), true)) { |
| 64 | + throw new \Exception('unable to decode key file'); |
| 65 | + } |
| 66 | + if (!isset($credentials['universe_domain'])) { |
| 67 | + throw new \Exception('The provided key file does not contain universe domain credentials'); |
| 68 | + } |
| 69 | + |
| 70 | + self::$projectId = $credentials['project_id']; |
| 71 | + self::$instanceId = uniqid(BigtableTestCase::INSTANCE_ID_PREFIX); |
| 72 | + self::$clusterId = uniqid(BigtableTestCase::CLUSTER_ID_PREFIX); |
| 73 | + self::$tableId = BigtableTestCase::TABLE_ID; |
| 74 | + self::$locationId = $locationId; |
| 75 | + |
| 76 | + self::$instanceAdminClient = new BigtableInstanceAdminClient([ |
| 77 | + 'credentials' => $keyFilePath, |
| 78 | + 'projectId' => self::$projectId, |
| 79 | + 'universeDomain' => $credentials['universe_domain'], |
| 80 | + ]); |
| 81 | + self::$tableAdminClient = new BigtableTableAdminClient([ |
| 82 | + 'credentials' => $keyFilePath, |
| 83 | + 'projectId' => self::$projectId, |
| 84 | + 'universeDomain' => $credentials['universe_domain'], |
| 85 | + ]); |
| 86 | + self::$bigtableClient = new BigtableClient([ |
| 87 | + 'credentials' => $keyFilePath, |
| 88 | + 'projectId' => self::$projectId, |
| 89 | + 'universeDomain' => $credentials['universe_domain'], |
| 90 | + ]); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Test creating a Bigtable instance with universe domain credentials |
| 95 | + */ |
| 96 | + public function testCreateInstanceWithUniverseDomain() |
| 97 | + { |
| 98 | + $formattedParent = self::$instanceAdminClient->projectName(self::$projectId); |
| 99 | + $instance = new Instance(); |
| 100 | + $instance->setDisplayName(self::$instanceId); |
| 101 | + |
| 102 | + $cluster = new Cluster(); |
| 103 | + $cluster->setLocation( |
| 104 | + self::$instanceAdminClient->locationName( |
| 105 | + self::$projectId, |
| 106 | + self::$locationId |
| 107 | + ) |
| 108 | + ); |
| 109 | + $cluster->setServeNodes(1); |
| 110 | + |
| 111 | + $request = new CreateInstanceRequest([ |
| 112 | + 'parent' => $formattedParent, |
| 113 | + 'instance_id' => self::$instanceId, |
| 114 | + 'instance' => $instance, |
| 115 | + 'clusters' => [ |
| 116 | + self::$clusterId => $cluster |
| 117 | + ] |
| 118 | + ]); |
| 119 | + |
| 120 | + $operationResponse = self::$instanceAdminClient->createInstance($request); |
| 121 | + $operationResponse->pollUntilComplete(); |
| 122 | + |
| 123 | + $this->assertTrue($operationResponse->operationSucceeded(), 'Failed to create instance'); |
| 124 | + |
| 125 | + // Get the result to verify the instance was created |
| 126 | + $result = $operationResponse->getResult(); |
| 127 | + $this->assertNotNull($result); |
| 128 | + $this->assertStringEndsWith('/' . self::$instanceId, $result->getName()); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Test creating a table with universe domain credentials |
| 133 | + * |
| 134 | + * @depends testCreateInstanceWithUniverseDomain |
| 135 | + */ |
| 136 | + public function testCreateTableWithUniverseDomain() |
| 137 | + { |
| 138 | + $instanceName = self::$instanceAdminClient->instanceName(self::$projectId, self::$instanceId); |
| 139 | + $tableName = self::$tableAdminClient->tableName(self::$projectId, self::$instanceId, self::$tableId); |
| 140 | + |
| 141 | + // Create table with column families |
| 142 | + $table = new Table(); |
| 143 | + $columnFamily = new ColumnFamily(); |
| 144 | + $table->setColumnFamilies([ |
| 145 | + 'cf1' => $columnFamily |
| 146 | + ]); |
| 147 | + |
| 148 | + $createTableRequest = (new CreateTableRequest()) |
| 149 | + ->setParent($instanceName) |
| 150 | + ->setTableId(self::$tableId) |
| 151 | + ->setTable($table); |
| 152 | + |
| 153 | + $response = self::$tableAdminClient->createTable($createTableRequest); |
| 154 | + |
| 155 | + $this->assertNotNull($response); |
| 156 | + $this->assertEquals($tableName, $response->getName()); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Test writing and reading data with universe domain credentials. |
| 161 | + * |
| 162 | + * @depends testCreateTableWithUniverseDomain |
| 163 | + */ |
| 164 | + public function testListsObjectsWithUniverseDomain() |
| 165 | + { |
| 166 | + $table = self::$bigtableClient->table(self::$instanceId, self::$tableId); |
| 167 | + |
| 168 | + $greetings = ['Hello World!', 'Hello Cloud Bigtable!', 'Hello PHP!']; |
| 169 | + $entries = []; |
| 170 | + $columnFamilyId = 'cf1'; |
| 171 | + $column = 'greeting'; |
| 172 | + foreach ($greetings as $i => $value) { |
| 173 | + $rowKey = sprintf('greeting%s', $i); |
| 174 | + $rowMutation = new Mutations(); |
| 175 | + $rowMutation->upsert($columnFamilyId, $column, $value, time() * 1000 * 1000); |
| 176 | + $entries[$rowKey] = $rowMutation; |
| 177 | + } |
| 178 | + $table->mutateRows($entries); |
| 179 | + |
| 180 | + $key = 'greeting0'; |
| 181 | + |
| 182 | + // Only retrieve the most recent version of the cell. |
| 183 | + $rowFilter = (new RowFilter())->setCellsPerColumnLimitFilter(1); |
| 184 | + |
| 185 | + $column = 'greeting'; |
| 186 | + $columnFamilyId = 'cf1'; |
| 187 | + |
| 188 | + $row = $table->readRow($key, [ |
| 189 | + 'filter' => $rowFilter |
| 190 | + ]); |
| 191 | + |
| 192 | + $columnFamilyId = 'cf1'; |
| 193 | + $column = 'greeting'; |
| 194 | + |
| 195 | + $partialRows = iterator_to_array($table->readRows([])->readAll()); |
| 196 | + $this->assertCount(3, $partialRows); |
| 197 | + $this->assertEquals('Hello World!', $partialRows['greeting0'][$columnFamilyId][$column][0]['value']); |
| 198 | + $this->assertEquals('Hello Cloud Bigtable!', $partialRows['greeting1'][$columnFamilyId][$column][0]['value']); |
| 199 | + $this->assertEquals('Hello PHP!', $partialRows['greeting2'][$columnFamilyId][$column][0]['value']); |
| 200 | + } |
| 201 | + /** |
| 202 | + * Test deleting a table with universe domain credentials. |
| 203 | + * |
| 204 | + * @depends testListsObjectsWithUniverseDomain |
| 205 | + */ |
| 206 | + public function testDeleteTableWithUniverseDomain() |
| 207 | + { |
| 208 | + $tableName = self::$tableAdminClient->tableName(self::$projectId, self::$instanceId, self::$tableId); |
| 209 | + |
| 210 | + $deleteTableRequest = (new DeleteTableRequest()) |
| 211 | + ->setName($tableName); |
| 212 | + |
| 213 | + self::$tableAdminClient->deleteTable($deleteTableRequest); |
| 214 | + |
| 215 | + // Verify the table was deleted by trying to get it (should throw an exception) |
| 216 | + $getTableRequest = (new GetTableRequest()) |
| 217 | + ->setName($tableName); |
| 218 | + |
| 219 | + try { |
| 220 | + self::$tableAdminClient->getTable($getTableRequest); |
| 221 | + $this->fail('Expected exception was not thrown'); |
| 222 | + } catch (ApiException $e) { |
| 223 | + $this->assertEquals('NOT_FOUND', $e->getStatus()); |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + /** |
| 228 | + * Test deleting a Bigtable instance with universe domain credentials. |
| 229 | + * |
| 230 | + * @depends testDeleteTableWithUniverseDomain |
| 231 | + */ |
| 232 | + public function testDeleteInstanceWithUniverseDomain() |
| 233 | + { |
| 234 | + $instanceName = self::$instanceAdminClient->instanceName(self::$projectId, self::$instanceId); |
| 235 | + |
| 236 | + $deleteRequest = new DeleteInstanceRequest([ |
| 237 | + 'name' => $instanceName |
| 238 | + ]); |
| 239 | + |
| 240 | + self::$instanceAdminClient->deleteInstance($deleteRequest); |
| 241 | + |
| 242 | + // Verify the instance was deleted by trying to get it (should throw an exception) |
| 243 | + $getRequest = new GetInstanceRequest([ |
| 244 | + 'name' => $instanceName |
| 245 | + ]); |
| 246 | + try { |
| 247 | + self::$instanceAdminClient->getInstance($getRequest); |
| 248 | + $this->fail('Expected exception was not thrown'); |
| 249 | + } catch (ApiException $e) { |
| 250 | + $this->assertEquals('NOT_FOUND', $e->getStatus()); |
| 251 | + } |
| 252 | + } |
| 253 | +} |
0 commit comments