CredentialsProviderTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\Unit\Credentials\Providers;
  3. use PHPUnit\Framework\TestCase;
  4. use AlibabaCloud\Client\AlibabaCloud;
  5. use AlibabaCloud\Client\Exception\ClientException;
  6. use AlibabaCloud\Client\Credentials\Providers\CredentialsProvider;
  7. use AlibabaCloud\Client\Tests\Unit\Credentials\Ini\VirtualAccessKeyCredential;
  8. /**
  9. * Class CredentialsProviderTest
  10. *
  11. * @package AlibabaCloud\Client\Tests\Unit\Credentials\Providers
  12. */
  13. class CredentialsProviderTest extends TestCase
  14. {
  15. /**
  16. * @before
  17. */
  18. protected function initialize()
  19. {
  20. parent::setUp();
  21. AlibabaCloud::flush();
  22. }
  23. public function testEnvironmentVariableIdEmpty()
  24. {
  25. $this->expectException(ClientException::class);
  26. $this->expectExceptionMessage("Environment variable 'ALIBABA_CLOUD_ACCESS_KEY_ID' cannot be empty");
  27. putenv('ALIBABA_CLOUD_ACCESS_KEY_ID=');
  28. $provider = CredentialsProvider::env();
  29. $provider();
  30. }
  31. public function testEnvironmentVariableSecretEmpty()
  32. {
  33. $this->expectException(ClientException::class);
  34. $this->expectExceptionMessage("Environment variable 'ALIBABA_CLOUD_ACCESS_KEY_SECRET' cannot be empty");
  35. putenv('ALIBABA_CLOUD_ACCESS_KEY_ID=id');
  36. putenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET=');
  37. $provider = CredentialsProvider::env();
  38. $provider();
  39. }
  40. /**
  41. * @throws ClientException
  42. */
  43. public function testEnvProvider()
  44. {
  45. self::assertEquals([], AlibabaCloud::all());
  46. $id = \AlibabaCloud\Client\env('ACCESS_KEY_ID');
  47. $secret = \AlibabaCloud\Client\env('ACCESS_KEY_SECRET');
  48. putenv("ALIBABA_CLOUD_ACCESS_KEY_ID=$id");
  49. putenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET=$secret");
  50. $provider = CredentialsProvider::env();
  51. $provider();
  52. self::assertTrue(AlibabaCloud::has(CredentialsProvider::getDefaultName()));
  53. }
  54. public function testIniWithLoadHomeFile()
  55. {
  56. self::assertEquals([], AlibabaCloud::all());
  57. $provider = CredentialsProvider::ini();
  58. $provider();
  59. }
  60. /**
  61. * @depends testIniWithLoadHomeFile
  62. */
  63. public function testIniEnvironmentVariableEmpty()
  64. {
  65. $this->expectException(ClientException::class);
  66. $this->expectExceptionMessage("Environment variable 'ALIBABA_CLOUD_CREDENTIALS_FILE' cannot be empty");
  67. putenv('ALIBABA_CLOUD_CREDENTIALS_FILE=');
  68. $provider = CredentialsProvider::ini();
  69. $provider();
  70. }
  71. /**
  72. * @depends testIniEnvironmentVariableEmpty
  73. * @throws ClientException
  74. */
  75. public function testIniWithFile()
  76. {
  77. self::assertEquals([], AlibabaCloud::all());
  78. $file = VirtualAccessKeyCredential::ok();
  79. putenv("ALIBABA_CLOUD_CREDENTIALS_FILE=$file");
  80. $provider = CredentialsProvider::ini();
  81. $provider();
  82. self::assertTrue(AlibabaCloud::has('ok'));
  83. }
  84. /**
  85. * @throws ClientException
  86. */
  87. public function testInstance()
  88. {
  89. self::assertEquals([], AlibabaCloud::all());
  90. putenv('ALIBABA_CLOUD_ECS_METADATA=role_name');
  91. $provider = CredentialsProvider::instance();
  92. $provider();
  93. self::assertTrue(AlibabaCloud::has(CredentialsProvider::getDefaultName()));
  94. }
  95. /**
  96. * @throws ClientException
  97. */
  98. public function testDefaultProvider()
  99. {
  100. self::assertEquals([], AlibabaCloud::all());
  101. $file = VirtualAccessKeyCredential::ok();
  102. putenv("ALIBABA_CLOUD_CREDENTIALS_FILE=$file");
  103. CredentialsProvider::defaultProvider('ok2');
  104. self::assertTrue(AlibabaCloud::has('ok'));
  105. }
  106. /**
  107. * @throws ClientException
  108. */
  109. public function testDefaultProviderWithEnv()
  110. {
  111. putenv('ALIBABA_CLOUD_ACCESS_KEY_ID=id');
  112. putenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET=secret');
  113. CredentialsProvider::defaultProvider(CredentialsProvider::getDefaultName());
  114. self::assertTrue(AlibabaCloud::has(CredentialsProvider::getDefaultName()));
  115. }
  116. public function testHasCustomChain()
  117. {
  118. self::assertFalse(CredentialsProvider::hasCustomChain());
  119. }
  120. /**
  121. * @throws ClientException
  122. */
  123. public function testFlush()
  124. {
  125. self::assertFalse(CredentialsProvider::hasCustomChain());
  126. CredentialsProvider::chain(
  127. CredentialsProvider::ini(),
  128. CredentialsProvider::env()
  129. );
  130. self::assertTrue(CredentialsProvider::hasCustomChain());
  131. CredentialsProvider::chain(
  132. CredentialsProvider::ini(),
  133. CredentialsProvider::env()
  134. );
  135. CredentialsProvider::flush();
  136. self::assertFalse(CredentialsProvider::hasCustomChain());
  137. }
  138. /**
  139. * @throws ClientException
  140. */
  141. public function testChain()
  142. {
  143. self::assertEquals([], AlibabaCloud::all());
  144. $file = VirtualAccessKeyCredential::ok();
  145. putenv("ALIBABA_CLOUD_CREDENTIALS_FILE=$file");
  146. CredentialsProvider::chain(
  147. CredentialsProvider::ini()
  148. );
  149. CredentialsProvider::customProvider(CredentialsProvider::getDefaultName());
  150. self::assertTrue(AlibabaCloud::has('ok'));
  151. self::assertTrue(CredentialsProvider::hasCustomChain());
  152. }
  153. public function testChainWithNoChainException()
  154. {
  155. $this->expectException(ClientException::class);
  156. $this->expectExceptionMessage("No providers in chain");
  157. CredentialsProvider::chain();
  158. }
  159. public function testChaiMustBeChain()
  160. {
  161. $this->expectException(ClientException::class);
  162. $this->expectExceptionMessage("Providers must all be Closures");
  163. CredentialsProvider::chain(
  164. CredentialsProvider::ini(),
  165. 'exception'
  166. );
  167. }
  168. /**
  169. * @throws ClientException
  170. */
  171. public function testDefault()
  172. {
  173. self::assertEquals('default', CredentialsProvider::getDefaultName());
  174. }
  175. public function testEnvironmentVariableEmpty()
  176. {
  177. $this->expectException(ClientException::class);
  178. $this->expectExceptionMessage("Environment variable 'ALIBABA_CLOUD_PROFILE' cannot be empty");
  179. putenv('ALIBABA_CLOUD_PROFILE=');
  180. CredentialsProvider::getDefaultName();
  181. }
  182. /**
  183. * @throws ClientException
  184. */
  185. public function testEnvironmentVariable()
  186. {
  187. putenv('ALIBABA_CLOUD_PROFILE=test');
  188. self::assertEquals('test', CredentialsProvider::getDefaultName());
  189. putenv('ALIBABA_CLOUD_PROFILE=default');
  190. }
  191. }