Example #1
0
        public async Task ErrorGettingNodes_MatchingExceptionIsThrown()
        {
            CreateLoadBalancer();
            var expectedException = new EnvironmentException("Error getting nodes");

            SetupErrorGettingNodes(expectedException);
            var actualException = Should.Throw <EnvironmentException>(() => _loadBalancer.TryGetNode(), "No nodes were discovered for service");

            actualException.InnerException.ShouldBe(expectedException);
        }
Example #2
0
        public async Task ToUpper_MethodCallFailsWithEnvironmentException_CorrectExceptionIsThrown()
        {
            var expected       = new EnvironmentException("You environment is invalid.").ThrowAndCatch();
            var messageHandler = new MockHttpMessageHandler();

            messageHandler.When("*").Respond(HttpResponseFactory.GetResponseWithException(expected));

            var actual = CreateClient(messageHandler).ToUpper("aaaa").ShouldThrow <EnvironmentException>();

            actual.Message.ShouldBe(expected.Message);
        }
Example #3
0
        public void AddTagsFromException_MultipleExceptionsWithConflict_EncryptedSameKeyTagsAreMerged()
        {
            var innerException = new EnvironmentException("", encrypted: new Tags {
                { "et1", "v1b" }, { "et3", "v3" }
            }).ThrowAndCatch();
            var exception = new RequestException("", innerException, encrypted: new Tags {
                { "et1", "v1a" }, { "et2", "v2" }
            }).ThrowAndCatch();

            var encryptedTags = exception.GetEncryptedTagsAndExtendedProperties().FormatTagsWithoutTypeSuffix().MergeDuplicateTags().ToList();

            CollectionAssert.AreEquivalent(new[] { "tags.et1", "tags.et2", "tags.et3" }, encryptedTags.Select(_ => _.Key));
            CollectionAssert.AreEquivalent(new Tags {
                { "tags.et1", "v1a\nv1b" }, { "tags.et2", "v2" }, { "tags.et3", "v3" }
            }, encryptedTags);
        }
Example #4
0
        public void AddTagsFromException_MultipleExceptionsNoConflict_UnencryptedTagsAreAggregated()
        {
            var innerException = new EnvironmentException("", unencrypted: new Tags {
                { "ut2", "v2" }
            }).ThrowAndCatch();
            var exception = new RequestException("", innerException, unencrypted: new Tags {
                { "ut1", "v1" }
            }).ThrowAndCatch();

            var unencryptedTags = exception.GetUnencryptedTags().FormatTagsWithoutTypeSuffix().MergeDuplicateTags();

            CollectionAssert.IsEmpty(exception.GetEncryptedTagsAndExtendedProperties());
            CollectionAssert.AreEquivalent(new Tags {
                { "tags.ut1", "v1" }, { "tags.ut2", "v2" }
            }, unencryptedTags);
        }
Example #5
0
        public async Task ToUpper_MethodCallFailsWithEnvironmentException_CorrectExceptionIsThrown()
        {
            var expected = new EnvironmentException("You environment is invalid.").ThrowAndCatch();
            Func <HttpClientConfiguration, HttpMessageHandler> messageHandlerFactory = _ =>
            {
                var messageHandler = new MockHttpMessageHandler();
                messageHandler.When("*").Respond(HttpResponseFactory.GetResponseWithException(ExceptionSerializer, expected));

                return(messageHandler);
            };

            unitTesting.Rebind <Func <HttpClientConfiguration, HttpMessageHandler> >().ToMethod(c => messageHandlerFactory);

            var actual = CreateClient().ToUpper("aaaa").ShouldThrow <EnvironmentException>();

            actual.Message.ShouldBe(expected.Message);
        }
Example #6
0
        public void AddTagsFromException_MultipleExceptionsWithConflict_MixedTagsAreMergedAndEncrypted()
        {
            var innerException = new EnvironmentException("", null, new Tags {
                { "et1", "v1b" }, { "mix1", "v2" }
            }, new Tags {
                { "ut1", "v1b" }, { "mix1", "v4" }
            }).ThrowAndCatch();
            var exception = new RequestException("", innerException, new Tags {
                { "et1", "v1a" }, { "mix1", "v1" }
            }, new Tags {
                { "ut1", "v1a" }, { "mix1", "v3" }
            }).ThrowAndCatch();

            var encryptedTags = exception.GetEncryptedTagsAndExtendedProperties().FormatTagsWithoutTypeSuffix().MergeDuplicateTags();
            var allTags       = exception.GetEncryptedTagsAndExtendedProperties().Concat(exception.GetUnencryptedTags()).FormatTagsWithoutTypeSuffix().MergeDuplicateTags();

            CollectionAssert.AreEquivalent(new[] { "tags.et1", "tags.mix1" }, encryptedTags.Select(_ => _.Key));
            CollectionAssert.AreEquivalent(new Tags {
                { "tags.et1", "v1a\nv1b" }, { "tags.ut1", "v1a\nv1b" }, { "tags.mix1", "v1\nv2\nv3\nv4" }
            }, allTags);
        }
Example #7
0
        public async Task <EndPointsResult> GetEndPoints(string serviceName)
        {
            var            consulQuery     = $"/v1/query/{serviceName}/execute?dc={DataCenter}";
            var            requestLog      = _httpClient.BaseAddress + consulQuery;
            string         responseContent = null;
            HttpStatusCode?statusCode      = null;

            try
            {
                using (var response = await _httpClient.GetAsync(consulQuery).ConfigureAwait(false))
                {
                    responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                    statusCode = response.StatusCode;

                    if (statusCode == HttpStatusCode.OK)
                    {
                        QueryIsOk(consulQuery, serviceName, queryExists: true);
                    }
                    else
                    {
                        var exception = new EnvironmentException("Consul response not OK",
                                                                 unencrypted: new Tags
                        {
                            { "ConsulAddress", ConsulAddress.ToString() },
                            { "ServiceDeployment", serviceName },
                            { "ConsulQuery", consulQuery },
                            { "ResponseCode", statusCode.ToString() },
                            { "Content", responseContent }
                        });

                        if (responseContent.EndsWith("Query not found", StringComparison.InvariantCultureIgnoreCase))
                        {
                            QueryIsOk(consulQuery, serviceName, queryExists: false);
                            return(ErrorResult(requestLog, ex: null, isQueryDefined: false));
                        }

                        Log.Error(_ => _("Error calling Consul", exception: exception));
                        _failedQueries.TryAdd(consulQuery, exception);
                        return(ErrorResult(requestLog, exception, true));
                    }
                }
                var serializedResponse = (ConsulQueryExecuteResponse)JsonConvert.DeserializeObject(responseContent, typeof(ConsulQueryExecuteResponse), _jsonSettings);
                var endpoints          = serializedResponse.Nodes.Select(_ => new ConsulEndPoint {
                    HostName = _.Node.Name, ModifyIndex = _.Node.ModifyIndex, Port = _.Service.Port
                }).ToArray();
                return(SuccessResult(endpoints, requestLog, responseContent));
            }
            catch (Exception ex)
            {
                Log.Error("Error calling Consul", exception: ex, unencryptedTags: new
                {
                    ServiceName   = serviceName,
                    ConsulAddress = ConsulAddress.ToString(),
                    consulQuery,
                    ResponseCode = statusCode,
                    Content      = responseContent
                });

                _failedQueries.TryAdd(consulQuery, ex);
                return(ErrorResult(requestLog, ex, true));
            }
        }