EcsRamRoleProviderTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\Unit\Credentials\Providers;
  3. use ReflectionException;
  4. use AlibabaCloud\Client\SDK;
  5. use GuzzleHttp\Psr7\Request;
  6. use PHPUnit\Framework\TestCase;
  7. use AlibabaCloud\Client\AlibabaCloud;
  8. use AlibabaCloud\Client\Clients\EcsRamRoleClient;
  9. use AlibabaCloud\Client\Credentials\StsCredential;
  10. use AlibabaCloud\Client\Exception\ClientException;
  11. use AlibabaCloud\Client\Exception\ServerException;
  12. use AlibabaCloud\Client\Credentials\Providers\EcsRamRoleProvider;
  13. use AlibabaCloud\Client\Credentials\Providers\RsaKeyPairProvider;
  14. /**
  15. * Class EcsRamRoleProviderTest
  16. *
  17. * @package AlibabaCloud\Client\Tests\Unit\Credentials\Providers
  18. *
  19. * @coversDefaultClass \AlibabaCloud\Client\Credentials\Providers\EcsRamRoleProvider
  20. */
  21. class EcsRamRoleProviderTest extends TestCase
  22. {
  23. /**
  24. * @after
  25. */
  26. protected function finalize()
  27. {
  28. parent::tearDown();
  29. AlibabaCloud::cancelMock();
  30. }
  31. /**
  32. * @throws ClientException
  33. * @throws ServerException
  34. */
  35. public function testGet()
  36. {
  37. $this->expectException(ClientException::class);
  38. $this->expectExceptionMessage("Timeout or instance does not belong to Alibaba Cloud");
  39. // Setup
  40. $client = new EcsRamRoleClient('foo');
  41. $provider = new EcsRamRoleProvider($client);
  42. AlibabaCloud::mockRequestException('timed', new Request('GET', 'url'));
  43. // Test
  44. $provider->get();
  45. }
  46. /**
  47. * @throws ClientException
  48. * @throws ServerException
  49. * @throws ReflectionException
  50. */
  51. public function testGetInCache()
  52. {
  53. // Setup
  54. $client = new EcsRamRoleClient(
  55. 'foo'
  56. );
  57. $provider = new EcsRamRoleProvider($client);
  58. // Test
  59. $cacheMethod = new \ReflectionMethod(
  60. RsaKeyPairProvider::class,
  61. 'cache'
  62. );
  63. $cacheMethod->setAccessible(true);
  64. $result = [
  65. 'Expiration' => '2049-10-01 11:11:11',
  66. 'AccessKeyId' => 'foo',
  67. 'AccessKeySecret' => 'bar',
  68. 'SecurityToken' => 'token',
  69. ];
  70. $cacheMethod->invokeArgs($provider, [$result]);
  71. $actual = $provider->get();
  72. // Assert
  73. self::assertInstanceOf(StsCredential::class, $actual);
  74. }
  75. /**
  76. * @throws ServerException
  77. * @throws ClientException
  78. */
  79. public function testServerUnreachable()
  80. {
  81. // Setup
  82. $roleName = 'EcsRamRoleTest';
  83. $client = new EcsRamRoleClient($roleName);
  84. // Test
  85. $credential = new EcsRamRoleProvider($client);
  86. // Assert
  87. try {
  88. $credential->get();
  89. } catch (ClientException $e) {
  90. $this->assertEquals($e->getErrorCode(), SDK::SERVER_UNREACHABLE);
  91. }
  92. }
  93. /**
  94. * @throws ClientException
  95. */
  96. public function estInvalidCredential()
  97. {
  98. // Setup
  99. $roleName = 'EcsRamRoleTest';
  100. $client = new EcsRamRoleClient($roleName);
  101. // Test
  102. $provider = new EcsRamRoleProvider($client);
  103. // Assert
  104. try {
  105. $provider->get();
  106. } catch (ServerException $e) {
  107. $this->assertEquals(
  108. $e->getErrorCode(),
  109. SDK::INVALID_CREDENTIAL
  110. );
  111. }
  112. }
  113. /**
  114. * @throws ClientException
  115. * @throws ServerException
  116. */
  117. public function testOk()
  118. {
  119. AlibabaCloud::mockResponse(
  120. 200,
  121. [],
  122. '{
  123. "AccessKeyId" : "STS.*******",
  124. "AccessKeySecret" : "*******",
  125. "Expiration" : "2019-01-28T15:15:56Z",
  126. "SecurityToken" : "****",
  127. "LastUpdated" : "2019-01-28T09:15:55Z",
  128. "Code" : "Success"
  129. }'
  130. );
  131. $provider = new EcsRamRoleProvider(AlibabaCloud::ecsRamRoleClient('role'));
  132. $credential = $provider->get();
  133. self::assertInstanceOf(StsCredential::class, $credential);
  134. }
  135. /**
  136. * @throws ClientException
  137. * @throws ServerException
  138. */
  139. public function testNoCredentials()
  140. {
  141. $this->expectException(ServerException::class);
  142. $this->expectExceptionMessage("SDK.InvalidCredential: Result contains no credentials RequestId:");
  143. AlibabaCloud::mockResponse();
  144. $provider = new EcsRamRoleProvider(AlibabaCloud::ecsRamRoleClient('role'));
  145. $provider->get();
  146. }
  147. /**
  148. * @throws ClientException
  149. * @throws ServerException
  150. */
  151. public function test404()
  152. {
  153. $this->expectException(ClientException::class);
  154. $this->expectExceptionMessage("The role was not found in the instance");
  155. AlibabaCloud::mockResponse(404);
  156. $provider = new EcsRamRoleProvider(AlibabaCloud::ecsRamRoleClient('role'));
  157. $provider->get();
  158. }
  159. /**
  160. * @throws ClientException
  161. * @throws ServerException
  162. */
  163. public function testErrorRetrieving()
  164. {
  165. $this->expectException(ServerException::class);
  166. $this->expectExceptionMessage("SDK.InvalidCredential: Error retrieving credentials from result RequestId:");
  167. AlibabaCloud::mockResponse(500);
  168. $provider = new EcsRamRoleProvider(AlibabaCloud::ecsRamRoleClient('role'));
  169. $provider->get();
  170. }
  171. /**
  172. * @throws ClientException
  173. * @throws ServerException
  174. */
  175. public function testRequestException()
  176. {
  177. $this->expectException(ClientException::class);
  178. $this->expectExceptionMessage("Error");
  179. AlibabaCloud::mockRequestException('Error', new Request('GET', 'test'));
  180. $provider = new EcsRamRoleProvider(AlibabaCloud::ecsRamRoleClient('role'));
  181. $provider->get();
  182. }
  183. }