Пример #1
0
        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();
            });
        }
Пример #2
0
        public void SetLastFailedRefreshTime()
        {
            InstanceProfileCredentials instance = new InstanceProfileCredentials("a", "b", "c", DateTime.Now.ToString(), 100000);

            instance.SetLastFailedRefreshTime();

            // DONE WITH NO EXCEPTION
        }
        public void ShouldRefresh()
        {
            InstanceProfileCredentials instance = new InstanceProfileCredentials("a", "b", "c", DateTime.Now.ToString(), 100000);

            Assert.True(instance.ShouldRefresh());

            instance.SetLastFailedRefreshTime();

            Assert.False(instance.ShouldRefresh());
        }
        public void SetLastFailedRefreshTime()
        {
            var instance = new InstanceProfileCredentials("a", "b", "c", DateTime.Now.ToString(), 100000);

            instance.SetLastFailedRefreshTime();
        }