|
45 | 45 | use Google\Cloud\Kms\V1\MacVerifyRequest; |
46 | 46 | use Google\Cloud\Kms\V1\ProtectionLevel; |
47 | 47 | 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; |
48 | 51 | use Google\Cloud\TestUtils\TestTrait; |
49 | 52 | use Google\Protobuf\FieldMask; |
50 | 53 | use PHPUnit\Framework\TestCase; |
@@ -847,67 +850,65 @@ public function testDeleteCryptoKey() |
847 | 850 |
|
848 | 851 | $keyName = $client->cryptoKeyName(self::$projectId, self::$locationId, self::$keyRingId, $keyId); |
849 | 852 | 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'); |
853 | 856 | } 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. |
858 | 857 | $this->assertEquals(\Google\Rpc\Code::NOT_FOUND, $e->getCode()); |
859 | 858 | } |
860 | 859 |
|
861 | 860 | return $keyId; |
862 | 861 | } |
863 | 862 |
|
864 | | - /** |
865 | | - * @depends testDeleteCryptoKey |
866 | | - */ |
867 | | - public function testListRetiredResources($deletedKeyId) |
| 863 | + public function testListAndGetRetiredResource() |
868 | 864 | { |
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)); |
873 | 873 |
|
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); |
877 | 881 |
|
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) |
884 | 888 | $parent = $client->locationName(self::$projectId, self::$locationId); |
885 | | - $listRequest = (new \Google\Cloud\Kms\V1\ListRetiredResourcesRequest())->setParent($parent); |
| 889 | + $listRequest = (new ListRetiredResourcesRequest())->setParent($parent); |
886 | 890 |
|
887 | 891 | $retiredResource = null; |
888 | 892 | foreach ($client->listRetiredResources($listRequest) as $res) { |
889 | | - if (strpos($res->getOriginalResource(), $deletedKeyId) !== false) { |
| 893 | + if (strpos($res->getOriginalResource(), $keyId) !== false) { |
890 | 894 | $retiredResource = $res; |
891 | 895 | break; |
892 | 896 | } |
893 | 897 | } |
894 | | - |
895 | | - if (!$retiredResource) { |
896 | | - $this->markTestSkipped('Could not find retired resource for retrieval test.'); |
897 | | - return; |
898 | | - } |
899 | 898 |
|
| 899 | + $this->assertNotNull($retiredResource, "Could not find retired resource for retrieval test."); |
| 900 | + |
900 | 901 | $parts = explode('/', $retiredResource->getName()); |
901 | 902 | $retiredResourceId = end($parts); |
902 | 903 |
|
903 | | - list(, $output) = $this->runFunctionSnippet('get_retired_resource', [ |
| 904 | + list($response, $output) = $this->runFunctionSnippet('get_retired_resource', [ |
904 | 905 | self::$projectId, |
905 | 906 | self::$locationId, |
906 | 907 | $retiredResourceId |
907 | 908 | ]); |
908 | 909 |
|
| 910 | + $this->assertStringContainsString($keyId, $response->getOriginalResource()); |
909 | 911 | $this->assertStringContainsString('Retired Resource Name', $output); |
910 | | - $this->assertStringContainsString($deletedKeyId, $output); |
911 | 912 | } |
912 | 913 |
|
913 | 914 | public function testDeleteCryptoKeyVersion() |
|
0 commit comments