Skip to content

Commit 37c0c7a

Browse files
committed
test: remove unnecessary retry loop and assertion checks in list/get RetiredResources samples
1 parent 0810169 commit 37c0c7a

3 files changed

Lines changed: 48 additions & 43 deletions

File tree

kms/src/get_retired_resource.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function get_retired_resource(
2727
string $projectId = 'my-project',
2828
string $locationId = 'us-east1',
2929
string $retiredResourceId = 'my-retired-resource'
30-
): void {
30+
): mixed {
3131
// Create the Cloud KMS client.
3232
$client = new KeyManagementServiceClient();
3333

@@ -41,6 +41,8 @@ function get_retired_resource(
4141

4242
printf('Retired Resource Name: %s' . PHP_EOL, $response->getName());
4343
printf('Original Resource: %s' . PHP_EOL, $response->getOriginalResource());
44+
45+
return $response;
4446
}
4547
// [END kms_get_retired_resource]
4648

kms/src/list_retired_resources.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
function list_retired_resources(
2727
string $projectId = 'my-project',
2828
string $locationId = 'us-east1'
29-
): void {
29+
): mixed {
3030
// Create the Cloud KMS client.
3131
$client = new KeyManagementServiceClient();
3232

@@ -43,6 +43,8 @@ function list_retired_resources(
4343
printf('Original Resource: %s' . PHP_EOL, $retiredResource->getOriginalResource());
4444
printf('Delete Time: %s' . PHP_EOL, $retiredResource->getDeleteTime()->getSeconds());
4545
}
46+
47+
return $response;
4648
}
4749
// [END kms_list_retired_resources]
4850

kms/test/kmsTest.php

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
use Google\Cloud\Kms\V1\MacVerifyRequest;
4646
use Google\Cloud\Kms\V1\ProtectionLevel;
4747
use Google\Cloud\Kms\V1\UpdateCryptoKeyRequest;
48+
use Google\Cloud\Kms\V1\DeleteCryptoKeyRequest;
49+
use Google\Cloud\Kms\V1\ListRetiredResourcesRequest;
50+
use Google\Cloud\Kms\V1\GetCryptoKeyRequest;
4851
use Google\Cloud\TestUtils\TestTrait;
4952
use Google\Protobuf\FieldMask;
5053
use PHPUnit\Framework\TestCase;
@@ -820,19 +823,19 @@ public function testDeleteCryptoKey()
820823
$client = new KeyManagementServiceClient();
821824
$keyRingName = $client->keyRingName(self::$projectId, self::$locationId, self::$keyRingId);
822825
$keyId = self::randomId();
823-
826+
824827
// Create an ASYMMETRIC_SIGN key (no initial version created by default for this purpose).
825828
$key = (new CryptoKey())
826829
->setPurpose(CryptoKeyPurpose::ASYMMETRIC_SIGN)
827830
->setVersionTemplate((new CryptoKeyVersionTemplate)
828831
->setAlgorithm(CryptoKeyVersionAlgorithm::EC_SIGN_P256_SHA256));
829-
832+
830833
$request = (new CreateCryptoKeyRequest())
831834
->setParent($keyRingName)
832835
->setCryptoKeyId($keyId)
833836
->setCryptoKey($key)
834837
->setSkipInitialVersionCreation(true);
835-
838+
836839
$client->createCryptoKey($request);
837840

838841
// Delete it.
@@ -844,72 +847,70 @@ public function testDeleteCryptoKey()
844847
]);
845848

846849
$this->assertStringContainsString('Deleted crypto key', $output);
847-
850+
848851
$keyName = $client->cryptoKeyName(self::$projectId, self::$locationId, self::$keyRingId, $keyId);
849852
try {
850-
$getKeyRequest = (new \Google\Cloud\Kms\V1\GetCryptoKeyRequest())->setName($keyName);
851-
$deletedKey = $client->getCryptoKey($getKeyRequest);
852-
$this->assertEquals(CryptoKey\State::DELETED, $deletedKey->getState());
853+
$getKeyRequest = (new GetCryptoKeyRequest())->setName($keyName);
854+
$client->getCryptoKey($getKeyRequest);
855+
$this->fail('Key should be deleted');
853856
} catch (\Google\ApiCore\ApiException $e) {
854-
// If the key is not found, it might be due to eventual consistency or it's effectively deleted.
855-
// However, typically it SHOULD exist in DELETED state.
856-
// If it returns NOT_FOUND, that is also a valid "deleted" state for some configurations or consistency windows.
857-
// Let's accept NOT_FOUND as valid for this test.
858857
$this->assertEquals(\Google\Rpc\Code::NOT_FOUND, $e->getCode());
859858
}
860859

861860
return $keyId;
862861
}
863862

864-
/**
865-
* @depends testDeleteCryptoKey
866-
*/
867-
public function testListRetiredResources($deletedKeyId)
868-
{
869-
list(, $output) = $this->runFunctionSnippet('list_retired_resources', [
870-
self::$projectId,
871-
self::$locationId
872-
]);
873-
874-
$this->assertStringContainsString('Retired Resource Name', $output);
875-
$this->assertStringContainsString($deletedKeyId, $output);
876-
}
877-
878-
/**
879-
* @depends testDeleteCryptoKey
880-
*/
881-
public function testGetRetiredResource($deletedKeyId)
863+
public function testListAndGetRetiredResource()
882864
{
865+
// Create a key to delete
883866
$client = new KeyManagementServiceClient();
867+
$keyRingName = $client->keyRingName(self::$projectId, self::$locationId, self::$keyRingId);
868+
$keyId = self::randomId();
869+
$key = (new CryptoKey())
870+
->setPurpose(CryptoKeyPurpose::ASYMMETRIC_SIGN)
871+
->setVersionTemplate((new CryptoKeyVersionTemplate)
872+
->setAlgorithm(CryptoKeyVersionAlgorithm::EC_SIGN_P256_SHA256));
873+
874+
// Create key (with no initial version)
875+
$request = (new CreateCryptoKeyRequest())
876+
->setParent($keyRingName)
877+
->setCryptoKeyId($keyId)
878+
->setCryptoKey($key)
879+
->setSkipInitialVersionCreation(true);
880+
$client->createCryptoKey($request);
881+
882+
// Delete it
883+
$keyName = $client->cryptoKeyName(self::$projectId, self::$locationId, self::$keyRingId, $keyId);
884+
$deleteRequest = (new DeleteCryptoKeyRequest())->setName($keyName);
885+
$client->deleteCryptoKey($deleteRequest);
886+
887+
// Find the retired resource ID first (needed for the snippet)
884888
$parent = $client->locationName(self::$projectId, self::$locationId);
885-
$listRequest = (new \Google\Cloud\Kms\V1\ListRetiredResourcesRequest())->setParent($parent);
886-
889+
$listRequest = (new ListRetiredResourcesRequest())->setParent($parent);
890+
887891
$retiredResource = null;
888892
foreach ($client->listRetiredResources($listRequest) as $res) {
889-
if (strpos($res->getOriginalResource(), $deletedKeyId) !== false) {
893+
if (strpos($res->getOriginalResource(), $keyId) !== false) {
890894
$retiredResource = $res;
891895
break;
892896
}
893897
}
894-
895-
if (!$retiredResource) {
896-
$this->markTestSkipped('Could not find retired resource for retrieval test.');
897-
return;
898-
}
898+
899+
$this->assertNotNull($retiredResource, 'Could not find retired resource for retrieval test.');
899900

900901
$parts = explode('/', $retiredResource->getName());
901902
$retiredResourceId = end($parts);
902-
903-
list(, $output) = $this->runFunctionSnippet('get_retired_resource', [
903+
904+
list($response, $output) = $this->runFunctionSnippet('get_retired_resource', [
904905
self::$projectId,
905906
self::$locationId,
906907
$retiredResourceId
907908
]);
908909

910+
$this->assertStringContainsString($keyId, $response->getOriginalResource());
909911
$this->assertStringContainsString('Retired Resource Name', $output);
910-
$this->assertStringContainsString($deletedKeyId, $output);
911912
}
912-
913+
913914
public function testDeleteCryptoKeyVersion()
914915
{
915916
$this->markTestSkipped('Skipping deleteCryptoKeyVersion test due to complexity of destroying a key version.');

0 commit comments

Comments
 (0)