Skip to content

Commit a7f7a5f

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

3 files changed

Lines changed: 40 additions & 35 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: 34 additions & 33 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;
@@ -847,67 +850,65 @@ public function testDeleteCryptoKey()
847850

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)
863+
public function testListAndGetRetiredResource()
868864
{
869-
list(, $output) = $this->runFunctionSnippet('list_retired_resources', [
870-
self::$projectId,
871-
self::$locationId
872-
]);
865+
// Create a key to delete
866+
$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));
873873

874-
$this->assertStringContainsString('Retired Resource Name', $output);
875-
$this->assertStringContainsString($deletedKeyId, $output);
876-
}
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);
877881

878-
/**
879-
* @depends testDeleteCryptoKey
880-
*/
881-
public function testGetRetiredResource($deletedKeyId)
882-
{
883-
$client = new KeyManagementServiceClient();
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);
889+
$listRequest = (new ListRetiredResourcesRequest())->setParent($parent);
886890

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-
}
899898

899+
$this->assertNotNull($retiredResource, "Could not find retired resource for retrieval test.");
900+
900901
$parts = explode('/', $retiredResource->getName());
901902
$retiredResourceId = end($parts);
902903

903-
list(, $output) = $this->runFunctionSnippet('get_retired_resource', [
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
}
912913

913914
public function testDeleteCryptoKeyVersion()

0 commit comments

Comments
 (0)