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 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 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());
        }
Пример #4
0
        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);
        }