public void GetMetadata1() { var mock = new Mock <ECSMetadataServiceCredentialsFetcher>() { CallBase = true }; int status = 200; string code = "ThisIsCode"; string message = "ThisIsMessage"; string requestId = "ThisIsRequestId"; HttpResponse response = new HttpResponse(); byte[] content = Encoding.GetEncoding("UTF-8").GetBytes("{\"Code\":\"" + code + "\",\"Message\":\"" + message + "\",\"RequestId\":\"" + requestId + "\"}"); response.ContentType = FormatType.JSON; response.Content = content; response.Status = status; mock.Setup(foo => foo.GetResponse( It.IsAny <HttpRequest>() )).Returns(response); ECSMetadataServiceCredentialsFetcher instance = mock.Object; string result = instance.GetMetadata(); Assert.Equal("{\"Code\":\"ThisIsCode\",\"Message\":\"ThisIsMessage\",\"RequestId\":\"ThisIsRequestId\"}", result); }
public void Fetch4() { // When Code != "Success" var mock = new Mock <ECSMetadataServiceCredentialsFetcher>() { CallBase = true }; HttpResponse response = new HttpResponse(); byte[] content = Encoding.GetEncoding("UTF-8").GetBytes("{\"Code\":\"NotSuccess\",\"Message\":\"ThisIsMessage\",\"RequestId\":\"ThisIsRequestId\",\"AccessKeyId\":\"MockAccessKeyId\",\"AccessKeySecret\":\"\",\"SecurityToken\":\"\",\"Expiration\":\"" + DateTimeMock.getNowDateTimeString() + "\"}"); response.ContentType = FormatType.JSON; response.Content = content; response.Status = 200; Exception e = new Exception(); mock.Setup(foo => foo.GetResponse( It.IsAny <HttpRequest>() )).Returns(response); ECSMetadataServiceCredentialsFetcher instance = mock.Object; Assert.Throws <ClientException>( () => { InstanceProfileCredentials credentials = instance.Fetch();; } ); }
public void FetchRetry() { var mock = new Mock <ECSMetadataServiceCredentialsFetcher>() { CallBase = true }; Exception e = new Exception(); mock.Setup(foo => foo.GetResponse( It.IsAny <HttpRequest>() )).Throws(e); ECSMetadataServiceCredentialsFetcher instance = mock.Object; Assert.Throws <ClientException>( () => { InstanceProfileCredentials credentials = instance.Fetch(3); } ); Action retryAction = () => { InstanceProfileCredentials credentials = instance.Fetch(-1);; }; var exception = Record.Exception(retryAction); Assert.Equal("Failed to connect ECS Metadata Service: Max retry times exceeded.", exception.Message); }
public void GetCredentials2() { // When Credentials is Expired // Mock Response HttpResponse response = new HttpResponse(); byte[] content = Encoding.GetEncoding("UTF-8").GetBytes("{\"Code\":\"Success\",\"Message\":\"ThisIsMessage\",\"RequestId\":\"ThisIsRequestId\",\"AccessKeyId\":\"MockAccessKeyId\",\"AccessKeySecret\":\"\",\"SecurityToken\":\"\",\"Expiration\":\"" + DateTimeMock.getExpiredDateTimeString() + "\"}"); response.ContentType = FormatType.JSON; response.Content = content; response.Status = 200; // Mock Fetcher var mockFetcher = new Mock <ECSMetadataServiceCredentialsFetcher>() { CallBase = true }; mockFetcher.Setup(foo => foo.GetResponse( It.IsAny <HttpRequest>() )).Returns(response); ECSMetadataServiceCredentialsFetcher fetcher = mockFetcher.Object; string roleName = ACKMock.GetRoleName(true); InstanceProfileCredentialsProvider instance = new InstanceProfileCredentialsProvider(roleName); AlibabaCloudCredentialsProvider provider = instance; instance.withFetcher(fetcher); Assert.Throws <ClientException>(() => { AlibabaCloudCredentials credentials = provider.GetCredentials(); // 进行有效期判断,已失效则抛出异常 }); }
public void GetCredentials4() { // When Credentials will Expired // Mock Response HttpResponse response = new HttpResponse(); string ExpiredDatetime = DateTime.Now.AddMilliseconds(800).ToString(); byte[] content = Encoding.GetEncoding("UTF-8").GetBytes("{\"Code\":\"Success\",\"Message\":\"ThisIsMessage\",\"RequestId\":\"ThisIsRequestId\",\"AccessKeyId\":\"MockAccessKeyId\",\"AccessKeySecret\":\"\",\"SecurityToken\":\"\",\"Expiration\":\"" + ExpiredDatetime + "\"}"); response.ContentType = FormatType.JSON; response.Content = content; response.Status = 200; // Mock Credentials var mockCredentials = new Mock <InstanceProfileCredentials>("MockAccessKeyId", "", "", ExpiredDatetime, 100000) { CallBase = true }; mockCredentials.Setup(foo => foo.RemainTicks()).Returns(15 * 1000 * 1000 * 10); InstanceProfileCredentials instanceProfileCredentials = mockCredentials.Object; instanceProfileCredentials.SetLastFailedRefreshTime(); // Mock Fetcher var mockFetcher = new Mock <ECSMetadataServiceCredentialsFetcher>() { CallBase = true }; mockFetcher.Setup(foo => foo.GetResponse( It.IsAny <HttpRequest>() )).Returns(response); mockFetcher.Setup(foo => foo.Fetch()).Returns(instanceProfileCredentials); ECSMetadataServiceCredentialsFetcher fetcher = mockFetcher.Object; string roleName = ACKMock.GetRoleName(true); InstanceProfileCredentialsProvider instance = new InstanceProfileCredentialsProvider(roleName); AlibabaCloudCredentialsProvider provider = instance; instance.withFetcher(fetcher); // Credentials will Expired at first. No Throw Exception (15000 >= 10000) AlibabaCloudCredentials credentials = provider.GetCredentials(); Assert.Equal("MockAccessKeyId", credentials.GetAccessKeyId()); // Credentials will Expired at Second. Throws Exception (5000 < 10000) mockCredentials.Setup(foo => foo.RemainTicks()).Returns(5000); instanceProfileCredentials = mockCredentials.Object; instanceProfileCredentials.SetLastFailedRefreshTime(); mockFetcher.Setup(foo => foo.Fetch()).Returns(instanceProfileCredentials); fetcher = mockFetcher.Object; instance.withFetcher(fetcher); Assert.Throws <ClientException>(() => { credentials = provider.GetCredentials(); }); }
public void withFetcher() { InstanceProfileCredentialsProvider instance = new InstanceProfileCredentialsProvider("roleName"); ECSMetadataServiceCredentialsFetcher fetcher = new ECSMetadataServiceCredentialsFetcher(); instance.withFetcher(fetcher); }
public void Fetch1() { // When Success var mock = new Mock <ECSMetadataServiceCredentialsFetcher>() { CallBase = true }; HttpResponse response = new HttpResponse(); byte[] content = Encoding.GetEncoding("UTF-8").GetBytes("{\"Code\":\"Success\",\"Message\":\"ThisIsMessage\",\"RequestId\":\"ThisIsRequestId\",\"AccessKeyId\":\"MockAccessKeyId\",\"AccessKeySecret\":\"\",\"SecurityToken\":\"\",\"Expiration\":\"" + DateTimeMock.getNowDateTimeString() + "\"}"); response.ContentType = FormatType.JSON; response.Content = content; response.Status = 200; mock.Setup(foo => foo.GetResponse( It.IsAny <HttpRequest>() )).Returns(response); ECSMetadataServiceCredentialsFetcher instance = mock.Object; EcsRamRoleCredential credentials = instance.Fetch();; Assert.Equal("MockAccessKeyId", credentials.GetAccessKeyId()); }
public ECSMetadataServiceCredentialsFetcher getInstance() { if (null == this.instance) { this.instance = new ECSMetadataServiceCredentialsFetcher(); } return(this.instance); }
public void withFetcher() { string roleName = ACKMock.GetRoleName(true); InstanceProfileCredentialsProvider instance = new InstanceProfileCredentialsProvider(roleName); ECSMetadataServiceCredentialsFetcher fetcher = new ECSMetadataServiceCredentialsFetcher(); instance.withFetcher(fetcher); }
public void Set() { var instance = new ECSMetadataServiceCredentialsFetcher(); Assert.Throws <ArgumentNullException>( () => { instance.SetRoleName(""); } ); instance.WithECSMetadataServiceHost("host"); instance.WithConnectionTimeout(100); instance.SetRoleName("roleName"); Assert.Equal("roleName", instance.GetRoleName()); }
public void GetCredentials1() { // When Success Get Credentials // Mock Response HttpResponse response = new HttpResponse(); byte[] content = Encoding.GetEncoding("UTF-8").GetBytes("{\"Code\":\"Success\",\"Message\":\"ThisIsMessage\",\"RequestId\":\"ThisIsRequestId\",\"AccessKeyId\":\"MockAccessKeyId\",\"AccessKeySecret\":\"\",\"SecurityToken\":\"\",\"Expiration\":\"" + DateTimeMock.getNotExpiredDateTimeString() + "\"}"); response.ContentType = FormatType.JSON; response.Content = content; response.Status = 200; // Mock Credentials var mockCredentials = new Mock <InstanceProfileCredentials>("MockAccessKeyId", "MockAccessKeySecret", "MockSecurityToken", DateTimeMock.getNotExpiredDateTimeString(), 100000) { CallBase = true }; mockCredentials.Setup(foo => foo.RemainTicks()).Returns(15 * 1000 * 1000 * 10); InstanceProfileCredentials instanceProfileCredentials = mockCredentials.Object; // Mock Fetcher var mockFetcher = new Mock <ECSMetadataServiceCredentialsFetcher>() { CallBase = true }; mockFetcher.Setup(foo => foo.GetResponse( It.IsAny <HttpRequest>() )).Returns(response); mockFetcher.Setup(foo => foo.Fetch()).Returns(instanceProfileCredentials); ECSMetadataServiceCredentialsFetcher fetcher = mockFetcher.Object; string roleName = ACKMock.GetRoleName(true); InstanceProfileCredentialsProvider instance = new InstanceProfileCredentialsProvider(roleName); instance.withFetcher(fetcher); AlibabaCloudCredentialsProvider provider = instance; AlibabaCloudCredentials credentials = provider.GetCredentials(); Assert.Equal("MockAccessKeyId", credentials.GetAccessKeyId()); }
public virtual AlibabaCloudCredentials GetInstanceRamRoleAlibabaCloudCredential() { if (null == this.regionId || this.regionId.Equals("")) { throw new ClientException("RegionID cannot be null or empty."); } else { if (defaultProfile.DefaultClientName.Equals("default")) { ECSMetadataServiceCredentialsFetcher eCSMetadataServiceCredentialsFetcher = new ECSMetadataServiceCredentialsFetcher(); eCSMetadataServiceCredentialsFetcher.SetRoleName(roleName); var ecsInstanceCredential = eCSMetadataServiceCredentialsFetcher.Fetch(); return(ecsInstanceCredential); } } return(null); }
public void GetMetadata2() { var mock = new Mock <ECSMetadataServiceCredentialsFetcher>() { CallBase = true }; WebException exception = new WebException("MockWebExceptionMessage"); mock.Setup(foo => foo.GetResponse( It.IsAny <HttpRequest>() )).Throws(exception); ECSMetadataServiceCredentialsFetcher instance = mock.Object; Assert.Throws <ClientException>( () => { string result = instance.GetMetadata(); } ); }
public void getInstance() { ECSMetadataServiceCredentialsFetcher instance = new ECSMetadataServiceCredentialsFetcher(); Assert.IsType <ECSMetadataServiceCredentialsFetcher>(instance); }