Beispiel #1
0
        public void VerifyUriListParsedFromString()
        {
            //Test single node
            var singleUri = "http://localhost:8080";
            var elasticConfigSingleNode = new ElasticSearchOutputConfiguration
            {
                ServiceUri         = singleUri,
                ConnectionPoolType = ""
            };
            var singleExpected = new List <Node> {
                new Node(new Uri(singleUri))
            };

            //Test many nodes
            var manyUri = "http://localhost:8080;http://localhost:8081";
            var elasticConfigManyNode = new ElasticSearchOutputConfiguration
            {
                ServiceUri         = manyUri,
                ConnectionPoolType = "Static"
            };
            var manyExpected = new List <Node>
            {
                new Node(new Uri("http://localhost:8080")),
                new Node(new Uri("http://localhost:8081"))
            };

            var healthReporterMock = new Mock <IHealthReporter>();

            var singleResult = elasticConfigSingleNode.GetConnectionPool(healthReporterMock.Object);
            var manyResult   = elasticConfigManyNode.GetConnectionPool(healthReporterMock.Object);

            Assert.Equal(singleExpected, singleResult.Nodes);
            Assert.Equal(manyExpected, manyResult.Nodes);
            healthReporterMock.Verify(m => m.ReportHealthy("ElasticSearchOutput: Using default Static connection type.", EventFlowContextIdentifiers.Configuration));
        }
        private void Initialize(ElasticSearchOutputConfiguration esOutputConfiguration)
        {
            Debug.Assert(esOutputConfiguration != null);
            Debug.Assert(this.healthReporter != null);

            this.connectionData = new ElasticSearchConnectionData
            {
                Configuration = esOutputConfiguration
            };

            string userName = esOutputConfiguration.BasicAuthenticationUserName;
            string password = esOutputConfiguration.BasicAuthenticationUserPassword;
            bool   credentialsIncomplete = string.IsNullOrWhiteSpace(userName) ^ string.IsNullOrWhiteSpace(password);

            if (credentialsIncomplete)
            {
                var errorMessage = $"{nameof(ElasticSearchOutput)}: for basic authentication to work both user name and password must be specified";
                healthReporter.ReportWarning(errorMessage, EventFlowContextIdentifiers.Configuration);
                userName = password = null;
            }

            IConnectionPool    pool = esOutputConfiguration.GetConnectionPool(healthReporter);
            ConnectionSettings connectionSettings = new ConnectionSettings(pool);

            if (!string.IsNullOrWhiteSpace(userName) && !string.IsNullOrWhiteSpace(password))
            {
                connectionSettings = connectionSettings.BasicAuthentication(userName, password);
            }

            this.connectionData.Client        = new ElasticClient(connectionSettings);
            this.connectionData.LastIndexName = null;

            if (string.IsNullOrWhiteSpace(esOutputConfiguration.IndexNamePrefix))
            {
                esOutputConfiguration.IndexNamePrefix = string.Empty;
            }
            else
            {
                string lowerCaseIndexNamePrefix = esOutputConfiguration.IndexNamePrefix.ToLowerInvariant();
                if (lowerCaseIndexNamePrefix != esOutputConfiguration.IndexNamePrefix)
                {
                    healthReporter.ReportWarning($"{nameof(ElasticSearchOutput)}: The chosen index name prefix '{esOutputConfiguration.IndexNamePrefix}' "
                                                 + "contains uppercase characters, which are not allowed by Elasticsearch. The prefix will be converted to lowercase.",
                                                 EventFlowContextIdentifiers.Configuration);
                }
                esOutputConfiguration.IndexNamePrefix = lowerCaseIndexNamePrefix + Dash;
            }

            if (string.IsNullOrWhiteSpace(esOutputConfiguration.EventDocumentTypeName))
            {
                string warning = $"{nameof(ElasticSearchOutput)}: '{nameof(ElasticSearchOutputConfiguration.EventDocumentTypeName)}' configuration parameter "
                                 + "should not be empty";
                healthReporter.ReportWarning(warning, EventFlowContextIdentifiers.Configuration);
                esOutputConfiguration.EventDocumentTypeName = ElasticSearchOutputConfiguration.DefaultEventDocumentTypeName;
            }
        }
Beispiel #3
0
        public void VerifyValidConfigCreatesConnectionPool(string connectionType, Type expectedConnectionPool)
        {
            var testUriString = "http://localhost:8080";
            var elasticConfig = new ElasticSearchOutputConfiguration
            {
                ServiceUri         = testUriString,
                ConnectionPoolType = connectionType
            };
            var healthReporterMock = new Mock <IHealthReporter>();
            var result             = elasticConfig.GetConnectionPool(healthReporterMock.Object);

            Assert.IsType(expectedConnectionPool, result);
        }
Beispiel #4
0
        public void InvalidServiceUriConfigThrows()
        {
            var invalidUri = "httpNotValidUri";
            var elasticConfigSingleNode = new ElasticSearchOutputConfiguration
            {
                ServiceUri         = invalidUri,
                ConnectionPoolType = ""
            };
            var healthReporterMock       = new Mock <IHealthReporter>();
            var expectedExceptionMessage = "ElasticSearchOutput:  required 'serviceUri' configuration parameter is invalid";

            var exception = Assert.Throws <Exception>(() => elasticConfigSingleNode.GetConnectionPool(healthReporterMock.Object));

            Assert.Equal(expectedExceptionMessage, exception.Message);
            healthReporterMock.Verify(m => m.ReportProblem(expectedExceptionMessage, EventFlowContextIdentifiers.Configuration), Times.Once);
        }